2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-14 20:17:30 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
package overlay_test
|
2018-12-14 20:17:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2019-05-01 14:45:52 +01:00
|
|
|
"time"
|
2018-12-14 20:17:30 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-03-29 08:53:43 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-14 20:17:30 +00:00
|
|
|
|
|
|
|
"storj.io/storj/internal/testcontext"
|
2019-03-25 22:25:09 +00:00
|
|
|
"storj.io/storj/pkg/overlay"
|
2019-03-01 17:46:34 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2018-12-14 20:17:30 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2018-12-14 20:17:30 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
2018-12-19 18:44:03 +00:00
|
|
|
func getRatio(success, total int64) (ratio float64) {
|
|
|
|
ratio = float64(success) / float64(total)
|
|
|
|
return ratio
|
2018-12-14 20:17:30 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
func TestStatDB(t *testing.T) {
|
2018-12-27 09:56:25 +00:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
2018-12-14 20:17:30 +00:00
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
testDatabase(ctx, t, db.OverlayCache())
|
2018-12-14 20:17:30 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
func testDatabase(ctx context.Context, t *testing.T, cache overlay.DB) {
|
2018-12-17 18:47:04 +00:00
|
|
|
nodeID := storj.NodeID{1, 2, 3, 4, 5}
|
2018-12-19 18:44:03 +00:00
|
|
|
currAuditSuccess := int64(4)
|
|
|
|
currAuditCount := int64(10)
|
|
|
|
currUptimeSuccess := int64(8)
|
|
|
|
currUptimeCount := int64(25)
|
2018-12-17 18:47:04 +00:00
|
|
|
|
|
|
|
{ // TestCreateNewAndWithStats
|
2018-12-19 18:44:03 +00:00
|
|
|
auditSuccessRatio := getRatio(currAuditSuccess, currAuditCount)
|
|
|
|
uptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
|
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
nodeStats := &overlay.NodeStats{
|
2018-12-14 20:17:30 +00:00
|
|
|
AuditSuccessRatio: auditSuccessRatio,
|
|
|
|
UptimeRatio: uptimeRatio,
|
2018-12-19 18:44:03 +00:00
|
|
|
AuditCount: currAuditCount,
|
|
|
|
AuditSuccessCount: currAuditSuccess,
|
|
|
|
UptimeCount: currUptimeCount,
|
|
|
|
UptimeSuccessCount: currUptimeSuccess,
|
2018-12-14 20:17:30 +00:00
|
|
|
}
|
2018-12-19 18:44:03 +00:00
|
|
|
|
2019-04-22 10:07:50 +01:00
|
|
|
err := cache.UpdateAddress(ctx, &pb.Node{Id: nodeID})
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
stats, err := cache.CreateStats(ctx, nodeID, nodeStats)
|
|
|
|
require.NoError(t, err)
|
2018-12-19 18:44:03 +00:00
|
|
|
assert.EqualValues(t, auditSuccessRatio, stats.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, uptimeRatio, stats.UptimeRatio)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
node, err := cache.Get(ctx, nodeID)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
assert.EqualValues(t, currAuditCount, node.Reputation.AuditCount)
|
|
|
|
assert.EqualValues(t, currAuditSuccess, node.Reputation.AuditSuccessCount)
|
|
|
|
assert.EqualValues(t, auditSuccessRatio, node.Reputation.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, currUptimeCount, node.Reputation.UptimeCount)
|
|
|
|
assert.EqualValues(t, currUptimeSuccess, node.Reputation.UptimeSuccessCount)
|
|
|
|
assert.EqualValues(t, uptimeRatio, node.Reputation.UptimeRatio)
|
2018-12-17 18:47:04 +00:00
|
|
|
}
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2018-12-17 18:47:04 +00:00
|
|
|
{ // TestGetDoesNotExist
|
|
|
|
noNodeID := storj.NodeID{255, 255, 255, 255}
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
_, err := cache.Get(ctx, noNodeID)
|
2018-12-14 20:17:30 +00:00
|
|
|
assert.Error(t, err)
|
2018-12-17 18:47:04 +00:00
|
|
|
}
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-05-01 14:45:52 +01:00
|
|
|
{ // TestKnownUnreliableOrOffline
|
|
|
|
for _, tt := range []struct {
|
|
|
|
nodeID storj.NodeID
|
|
|
|
auditSuccessCount int64
|
|
|
|
auditCount int64
|
|
|
|
auditSuccessRatio float64
|
|
|
|
uptimeSuccessCount int64
|
|
|
|
uptimeCount int64
|
|
|
|
uptimeRatio float64
|
|
|
|
}{
|
|
|
|
{storj.NodeID{1}, 20, 20, 1.0, 20, 20, 1.0}, // good ratios => good
|
|
|
|
{storj.NodeID{2}, 5, 20, 0.25, 20, 20, 1}, // bad audit success, good uptime => bad
|
|
|
|
{storj.NodeID{3}, 20, 20, 1.0, 5, 20, 0.25}, // good audit success, bad uptime => bad
|
|
|
|
{storj.NodeID{4}, 0, 0, 0.0, 20, 20, 1.0}, // "bad" audit success, no audits => now considered bad
|
|
|
|
{storj.NodeID{5}, 20, 20, 1.0, 0, 0, 0.25}, // "bad" uptime success, no checks => now considered bad
|
|
|
|
{storj.NodeID{6}, 0, 1, 0.0, 5, 5, .01}, // bad audit success exactly one audit => bad
|
|
|
|
{storj.NodeID{7}, 0, 20, 0.0, 20, 20, 1.0}, // impossible math, but good ratios => good
|
|
|
|
} {
|
|
|
|
nodeStats := &overlay.NodeStats{
|
|
|
|
AuditSuccessRatio: tt.auditSuccessRatio,
|
|
|
|
UptimeRatio: tt.uptimeRatio,
|
|
|
|
AuditCount: tt.auditCount,
|
|
|
|
AuditSuccessCount: tt.auditSuccessCount,
|
|
|
|
UptimeCount: tt.uptimeCount,
|
|
|
|
UptimeSuccessCount: tt.uptimeSuccessCount,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := cache.UpdateAddress(ctx, &pb.Node{Id: tt.nodeID})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = cache.CreateStats(ctx, tt.nodeID, nodeStats)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeIds := storj.NodeIDList{
|
|
|
|
storj.NodeID{1}, storj.NodeID{2},
|
|
|
|
storj.NodeID{3}, storj.NodeID{4},
|
|
|
|
storj.NodeID{5}, storj.NodeID{6},
|
|
|
|
}
|
|
|
|
criteria := &overlay.NodeCriteria{
|
|
|
|
AuditSuccessRatio: 0.5,
|
|
|
|
UptimeSuccessRatio: 0.5,
|
|
|
|
OnlineWindow: time.Hour,
|
|
|
|
}
|
|
|
|
|
|
|
|
invalid, err := cache.KnownUnreliableOrOffline(ctx, criteria, nodeIds)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, invalid, storj.NodeID{2})
|
|
|
|
assert.Contains(t, invalid, storj.NodeID{3})
|
|
|
|
assert.Contains(t, invalid, storj.NodeID{4})
|
|
|
|
assert.Contains(t, invalid, storj.NodeID{5})
|
|
|
|
assert.Contains(t, invalid, storj.NodeID{6})
|
|
|
|
assert.Len(t, invalid, 5)
|
|
|
|
}
|
|
|
|
|
2019-03-01 17:46:34 +00:00
|
|
|
{ // TestUpdateOperator
|
|
|
|
nodeID := storj.NodeID{10}
|
2019-04-22 10:07:50 +01:00
|
|
|
err := cache.UpdateAddress(ctx, &pb.Node{Id: nodeID})
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-01 17:46:34 +00:00
|
|
|
|
2019-04-10 07:04:24 +01:00
|
|
|
update, err := cache.UpdateNodeInfo(ctx, nodeID, &pb.InfoResponse{
|
|
|
|
Operator: &pb.NodeOperator{
|
|
|
|
Wallet: "0x1111111111111111111111111111111111111111",
|
|
|
|
Email: "abc123@gmail.com",
|
|
|
|
},
|
2019-03-01 17:46:34 +00:00
|
|
|
})
|
|
|
|
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-01 17:46:34 +00:00
|
|
|
assert.NotNil(t, update)
|
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
found, err := cache.Get(ctx, nodeID)
|
2019-03-01 17:46:34 +00:00
|
|
|
assert.NotNil(t, found)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-01 17:46:34 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
assert.Equal(t, "0x1111111111111111111111111111111111111111", update.Operator.Wallet)
|
|
|
|
assert.Equal(t, "abc123@gmail.com", update.Operator.Email)
|
2019-03-01 17:46:34 +00:00
|
|
|
|
2019-04-10 07:04:24 +01:00
|
|
|
updateEmail, err := cache.UpdateNodeInfo(ctx, nodeID, &pb.InfoResponse{
|
|
|
|
Operator: &pb.NodeOperator{
|
|
|
|
Wallet: update.Operator.Wallet,
|
|
|
|
Email: "def456@gmail.com",
|
|
|
|
},
|
2019-03-01 17:46:34 +00:00
|
|
|
})
|
|
|
|
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-01 17:46:34 +00:00
|
|
|
assert.NotNil(t, updateEmail)
|
2019-04-04 17:34:36 +01:00
|
|
|
assert.Equal(t, "0x1111111111111111111111111111111111111111", updateEmail.Operator.Wallet)
|
|
|
|
assert.Equal(t, "def456@gmail.com", updateEmail.Operator.Email)
|
2019-03-01 17:46:34 +00:00
|
|
|
|
2019-04-10 07:04:24 +01:00
|
|
|
updateWallet, err := cache.UpdateNodeInfo(ctx, nodeID, &pb.InfoResponse{
|
|
|
|
Operator: &pb.NodeOperator{
|
|
|
|
Wallet: "0x2222222222222222222222222222222222222222",
|
|
|
|
Email: updateEmail.Operator.Email,
|
|
|
|
},
|
2019-03-01 17:46:34 +00:00
|
|
|
})
|
|
|
|
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-01 17:46:34 +00:00
|
|
|
assert.NotNil(t, updateWallet)
|
2019-04-04 17:34:36 +01:00
|
|
|
assert.Equal(t, "0x2222222222222222222222222222222222222222", updateWallet.Operator.Wallet)
|
|
|
|
assert.Equal(t, "def456@gmail.com", updateWallet.Operator.Email)
|
2019-03-01 17:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 18:47:04 +00:00
|
|
|
{ // TestUpdateExists
|
2018-12-19 18:44:03 +00:00
|
|
|
auditSuccessRatio := getRatio(currAuditSuccess, currAuditCount)
|
|
|
|
uptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
node, err := cache.Get(ctx, nodeID)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
assert.EqualValues(t, currAuditCount, node.Reputation.AuditCount)
|
|
|
|
assert.EqualValues(t, currAuditSuccess, node.Reputation.AuditSuccessCount)
|
|
|
|
assert.EqualValues(t, auditSuccessRatio, node.Reputation.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, currUptimeCount, node.Reputation.UptimeCount)
|
|
|
|
assert.EqualValues(t, currUptimeSuccess, node.Reputation.UptimeSuccessCount)
|
|
|
|
assert.EqualValues(t, uptimeRatio, node.Reputation.UptimeRatio)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-03-25 22:25:09 +00:00
|
|
|
updateReq := &overlay.UpdateRequest{
|
2018-12-19 18:44:03 +00:00
|
|
|
NodeID: nodeID,
|
|
|
|
AuditSuccess: true,
|
|
|
|
IsUp: false,
|
2018-12-14 20:17:30 +00:00
|
|
|
}
|
2019-04-04 17:34:36 +01:00
|
|
|
stats, err := cache.UpdateStats(ctx, updateReq)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2018-12-19 18:44:03 +00:00
|
|
|
currAuditSuccess++
|
|
|
|
currAuditCount++
|
|
|
|
currUptimeCount++
|
|
|
|
newAuditRatio := getRatio(currAuditSuccess, currAuditCount)
|
|
|
|
newUptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
|
|
|
|
|
2018-12-14 20:17:30 +00:00
|
|
|
assert.EqualValues(t, newAuditRatio, stats.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
|
2018-12-17 18:47:04 +00:00
|
|
|
}
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2018-12-17 18:47:04 +00:00
|
|
|
{ // TestUpdateUptimeExists
|
2018-12-19 18:44:03 +00:00
|
|
|
auditSuccessRatio := getRatio(currAuditSuccess, currAuditCount)
|
|
|
|
uptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
node, err := cache.Get(ctx, nodeID)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
assert.EqualValues(t, currAuditCount, node.Reputation.AuditCount)
|
|
|
|
assert.EqualValues(t, currAuditSuccess, node.Reputation.AuditSuccessCount)
|
|
|
|
assert.EqualValues(t, auditSuccessRatio, node.Reputation.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, currUptimeCount, node.Reputation.UptimeCount)
|
|
|
|
assert.EqualValues(t, currUptimeSuccess, node.Reputation.UptimeSuccessCount)
|
|
|
|
assert.EqualValues(t, uptimeRatio, node.Reputation.UptimeRatio)
|
2018-12-19 18:44:03 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
stats, err := cache.UpdateUptime(ctx, nodeID, false)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2018-12-19 18:44:03 +00:00
|
|
|
currUptimeCount++
|
|
|
|
newUptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
|
2018-12-14 20:17:30 +00:00
|
|
|
assert.EqualValues(t, auditSuccessRatio, stats.AuditSuccessRatio)
|
2018-12-19 18:44:03 +00:00
|
|
|
assert.EqualValues(t, currAuditCount, stats.AuditCount)
|
2018-12-14 20:17:30 +00:00
|
|
|
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
|
2018-12-17 18:47:04 +00:00
|
|
|
}
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-03-29 08:53:43 +00:00
|
|
|
{ // TestUpdateStatsExists
|
2018-12-19 18:44:03 +00:00
|
|
|
auditSuccessRatio := getRatio(currAuditSuccess, currAuditCount)
|
|
|
|
uptimeRatio := getRatio(currUptimeSuccess, currUptimeCount)
|
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
node, err := cache.Get(ctx, nodeID)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
assert.EqualValues(t, currAuditCount, node.Reputation.AuditCount)
|
|
|
|
assert.EqualValues(t, currAuditSuccess, node.Reputation.AuditSuccessCount)
|
|
|
|
assert.EqualValues(t, auditSuccessRatio, node.Reputation.AuditSuccessRatio)
|
|
|
|
assert.EqualValues(t, currUptimeCount, node.Reputation.UptimeCount)
|
|
|
|
assert.EqualValues(t, currUptimeSuccess, node.Reputation.UptimeSuccessCount)
|
|
|
|
assert.EqualValues(t, uptimeRatio, node.Reputation.UptimeRatio)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
stats, err := cache.UpdateStats(ctx, &overlay.UpdateRequest{
|
2019-03-29 08:53:43 +00:00
|
|
|
NodeID: nodeID,
|
|
|
|
IsUp: true,
|
|
|
|
AuditSuccess: false,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2018-12-19 18:44:03 +00:00
|
|
|
currAuditCount++
|
2019-03-29 08:53:43 +00:00
|
|
|
newAuditRatio := getRatio(stats.AuditSuccessCount, stats.AuditCount)
|
2018-12-14 20:17:30 +00:00
|
|
|
assert.EqualValues(t, newAuditRatio, stats.AuditSuccessRatio)
|
2018-12-19 18:44:03 +00:00
|
|
|
assert.EqualValues(t, currAuditCount, stats.AuditCount)
|
2019-03-29 08:53:43 +00:00
|
|
|
newUptimeRatio := getRatio(stats.UptimeSuccessCount, stats.UptimeCount)
|
|
|
|
assert.EqualValues(t, newUptimeRatio, stats.UptimeRatio)
|
2018-12-17 18:47:04 +00:00
|
|
|
}
|
2018-12-14 20:17:30 +00:00
|
|
|
}
|