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
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
|
|
|
"storj.io/common/testcontext"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2019-07-28 06:55:36 +01:00
|
|
|
"storj.io/storj/satellite/overlay"
|
2018-12-14 20:17:30 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/satellitedbtest"
|
|
|
|
)
|
|
|
|
|
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-08-06 23:56:12 +01:00
|
|
|
satellitedbtest.Run(t, func(t *testing.T, db satellite.DB) {
|
|
|
|
ctx := testcontext.New(t)
|
|
|
|
defer ctx.Cleanup()
|
|
|
|
|
|
|
|
testDatabase(ctx, t, overlay.NewCombinedCache(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) {
|
2019-05-01 14:45:52 +01:00
|
|
|
{ // TestKnownUnreliableOrOffline
|
|
|
|
for _, tt := range []struct {
|
2019-06-21 16:23:15 +01:00
|
|
|
nodeID storj.NodeID
|
|
|
|
auditAlpha float64
|
|
|
|
auditBeta float64
|
|
|
|
uptimeAlpha float64
|
|
|
|
uptimeBeta float64
|
2019-05-01 14:45:52 +01:00
|
|
|
}{
|
2019-06-21 16:23:15 +01:00
|
|
|
{storj.NodeID{1}, 20, 0, 20, 0}, // good reputations => good
|
|
|
|
{storj.NodeID{2}, 0, 20, 20, 0}, // bad audit rep, good uptime rep => bad
|
|
|
|
{storj.NodeID{3}, 20, 0, 0, 20}, // good audit rep, bad uptime rep => bad
|
2019-05-01 14:45:52 +01:00
|
|
|
} {
|
2019-06-21 16:23:15 +01:00
|
|
|
startingRep := overlay.NodeSelectionConfig{
|
|
|
|
AuditReputationAlpha0: tt.auditAlpha,
|
|
|
|
AuditReputationBeta0: tt.auditBeta,
|
|
|
|
UptimeReputationAlpha0: tt.uptimeAlpha,
|
|
|
|
UptimeReputationBeta0: tt.uptimeBeta,
|
2019-05-01 14:45:52 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 16:23:15 +01:00
|
|
|
err := cache.UpdateAddress(ctx, &pb.Node{Id: tt.nodeID}, startingRep)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// update stats so node disqualification is triggered
|
|
|
|
_, err = cache.UpdateStats(ctx, &overlay.UpdateRequest{
|
|
|
|
NodeID: tt.nodeID,
|
|
|
|
AuditSuccess: true,
|
|
|
|
IsUp: true,
|
|
|
|
AuditLambda: 1, AuditWeight: 1,
|
|
|
|
UptimeLambda: 1, UptimeWeight: 1,
|
|
|
|
AuditDQ: 0.9, UptimeDQ: 0.9,
|
|
|
|
})
|
2019-05-01 14:45:52 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeIds := storj.NodeIDList{
|
|
|
|
storj.NodeID{1}, storj.NodeID{2},
|
|
|
|
storj.NodeID{3}, storj.NodeID{4},
|
|
|
|
}
|
2019-06-18 18:40:28 +01:00
|
|
|
criteria := &overlay.NodeCriteria{OnlineWindow: time.Hour}
|
2019-05-01 14:45:52 +01:00
|
|
|
|
|
|
|
invalid, err := cache.KnownUnreliableOrOffline(ctx, criteria, nodeIds)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-06-21 16:23:15 +01:00
|
|
|
require.Contains(t, invalid, storj.NodeID{2}) // bad audit
|
|
|
|
require.Contains(t, invalid, storj.NodeID{3}) // bad uptime
|
|
|
|
require.Contains(t, invalid, storj.NodeID{4}) // not in db
|
|
|
|
require.Len(t, invalid, 3)
|
2019-05-01 14:45:52 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 17:46:34 +00:00
|
|
|
{ // TestUpdateOperator
|
|
|
|
nodeID := storj.NodeID{10}
|
2019-06-20 14:56:04 +01:00
|
|
|
err := cache.UpdateAddress(ctx, &pb.Node{Id: nodeID}, overlay.NodeSelectionConfig{})
|
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",
|
2019-06-18 01:28:40 +01:00
|
|
|
Email: "abc123@mail.test",
|
2019-04-10 07:04:24 +01:00
|
|
|
},
|
2019-03-01 17:46:34 +00:00
|
|
|
})
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2019-06-21 16:23:15 +01:00
|
|
|
require.NotNil(t, update)
|
|
|
|
require.Equal(t, "0x1111111111111111111111111111111111111111", update.Operator.Wallet)
|
|
|
|
require.Equal(t, "abc123@mail.test", update.Operator.Email)
|
2019-03-01 17:46:34 +00:00
|
|
|
|
2019-04-04 17:34:36 +01:00
|
|
|
found, err := cache.Get(ctx, nodeID)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2019-06-21 16:23:15 +01:00
|
|
|
require.NotNil(t, found)
|
|
|
|
require.Equal(t, "0x1111111111111111111111111111111111111111", found.Operator.Wallet)
|
|
|
|
require.Equal(t, "abc123@mail.test", found.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,
|
2019-06-18 01:28:40 +01:00
|
|
|
Email: "def456@mail.test",
|
2019-04-10 07:04:24 +01:00
|
|
|
},
|
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)
|
2019-06-18 01:28:40 +01:00
|
|
|
assert.Equal(t, "def456@mail.test", 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)
|
2019-06-18 01:28:40 +01:00
|
|
|
assert.Equal(t, "def456@mail.test", updateWallet.Operator.Email)
|
2019-03-01 17:46:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-17 18:47:04 +00:00
|
|
|
{ // TestUpdateExists
|
2019-06-21 16:23:15 +01:00
|
|
|
nodeID := storj.NodeID{1}
|
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-06-21 16:23:15 +01:00
|
|
|
auditAlpha := node.Reputation.AuditReputationAlpha
|
|
|
|
auditBeta := node.Reputation.AuditReputationBeta
|
|
|
|
uptimeAlpha := node.Reputation.UptimeReputationAlpha
|
|
|
|
uptimeBeta := node.Reputation.UptimeReputationBeta
|
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,
|
2019-06-21 16:23:15 +01:00
|
|
|
IsUp: true,
|
|
|
|
AuditLambda: 0.123, AuditWeight: 0.456,
|
|
|
|
UptimeLambda: 0.789, UptimeWeight: 0.876,
|
|
|
|
AuditDQ: 0, UptimeDQ: 0, // don't disqualify for any reason
|
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
|
|
|
|
2019-06-21 16:23:15 +01:00
|
|
|
expectedAuditAlpha := updateReq.AuditLambda*auditAlpha + updateReq.AuditWeight
|
|
|
|
expectedAuditBeta := updateReq.AuditLambda * auditBeta
|
|
|
|
expectedUptimeAlpha := updateReq.UptimeLambda*uptimeAlpha + updateReq.UptimeWeight
|
|
|
|
expectedUptimeBeta := updateReq.UptimeLambda * uptimeBeta
|
|
|
|
require.EqualValues(t, stats.AuditReputationAlpha, expectedAuditAlpha)
|
|
|
|
require.EqualValues(t, stats.AuditReputationBeta, expectedAuditBeta)
|
|
|
|
require.EqualValues(t, stats.UptimeReputationAlpha, expectedUptimeAlpha)
|
|
|
|
require.EqualValues(t, stats.UptimeReputationBeta, expectedUptimeBeta)
|
|
|
|
|
|
|
|
auditAlpha = expectedAuditAlpha
|
|
|
|
auditBeta = expectedAuditBeta
|
|
|
|
uptimeAlpha = expectedUptimeAlpha
|
|
|
|
uptimeBeta = expectedUptimeBeta
|
|
|
|
|
|
|
|
updateReq.AuditSuccess = false
|
|
|
|
updateReq.IsUp = false
|
|
|
|
stats, err = cache.UpdateStats(ctx, updateReq)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedAuditAlpha = updateReq.AuditLambda * auditAlpha
|
|
|
|
expectedAuditBeta = updateReq.AuditLambda*auditBeta + updateReq.AuditWeight
|
|
|
|
expectedUptimeAlpha = updateReq.UptimeLambda * uptimeAlpha
|
|
|
|
expectedUptimeBeta = updateReq.UptimeLambda*uptimeBeta + updateReq.UptimeWeight
|
|
|
|
require.EqualValues(t, stats.AuditReputationAlpha, expectedAuditAlpha)
|
|
|
|
require.EqualValues(t, stats.AuditReputationBeta, expectedAuditBeta)
|
|
|
|
require.EqualValues(t, stats.UptimeReputationAlpha, expectedUptimeAlpha)
|
|
|
|
require.EqualValues(t, stats.UptimeReputationBeta, expectedUptimeBeta)
|
2018-12-19 18:44:03 +00:00
|
|
|
|
2018-12-17 18:47:04 +00:00
|
|
|
}
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-09-19 19:37:31 +01:00
|
|
|
{ // test UpdateCheckIn updates the reputation correctly when the node is offline/online
|
2019-06-21 16:23:15 +01:00
|
|
|
nodeID := storj.NodeID{1}
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-09-19 19:37:31 +01:00
|
|
|
// get the existing node info that is stored in nodes table
|
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)
|
2019-06-21 16:23:15 +01:00
|
|
|
alpha := node.Reputation.UptimeReputationAlpha
|
|
|
|
beta := node.Reputation.UptimeReputationBeta
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-06-21 16:23:15 +01:00
|
|
|
lambda := 0.789
|
|
|
|
weight := 0.876
|
|
|
|
dq := float64(0) // don't disqualify for any reason
|
2018-12-19 18:44:03 +00:00
|
|
|
|
2019-09-19 19:37:31 +01:00
|
|
|
info := overlay.NodeCheckInInfo{
|
|
|
|
NodeID: nodeID,
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Address: "1.2.3.4",
|
|
|
|
},
|
|
|
|
IsUp: false,
|
|
|
|
Version: &pb.NodeVersion{
|
|
|
|
Version: "v0.0.0",
|
|
|
|
CommitHash: "",
|
|
|
|
Timestamp: time.Time{},
|
|
|
|
Release: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
config := overlay.NodeSelectionConfig{
|
|
|
|
UptimeReputationLambda: lambda,
|
|
|
|
UptimeReputationWeight: weight,
|
|
|
|
UptimeReputationDQ: dq,
|
|
|
|
}
|
|
|
|
// update check-in when node is offline
|
2019-11-15 22:43:06 +00:00
|
|
|
err = cache.UpdateCheckIn(ctx, info, time.Now().UTC(), config)
|
2019-09-19 19:37:31 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
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-06-21 16:23:15 +01:00
|
|
|
expectedAlpha := lambda * alpha
|
|
|
|
expectedBeta := lambda*beta + weight
|
2019-09-19 19:37:31 +01:00
|
|
|
// confirm the reputation is updated correctly when node is offline
|
|
|
|
require.EqualValues(t, node.Reputation.UptimeReputationAlpha, expectedAlpha)
|
|
|
|
require.EqualValues(t, node.Reputation.UptimeReputationBeta, expectedBeta)
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-06-21 16:23:15 +01:00
|
|
|
alpha = expectedAlpha
|
|
|
|
beta = expectedBeta
|
|
|
|
|
2019-09-19 19:37:31 +01:00
|
|
|
info.IsUp = true
|
|
|
|
// update check-in when node is online
|
2019-11-15 22:43:06 +00:00
|
|
|
err = cache.UpdateCheckIn(ctx, info, time.Now().UTC(), config)
|
2019-09-19 19:37:31 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
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-06-21 16:23:15 +01:00
|
|
|
expectedAlpha = lambda*alpha + weight
|
|
|
|
expectedBeta = lambda * beta
|
2019-09-19 19:37:31 +01:00
|
|
|
// confirm the reputation is updated correctly when node is online
|
|
|
|
require.EqualValues(t, node.Reputation.UptimeReputationAlpha, expectedAlpha)
|
|
|
|
require.EqualValues(t, node.Reputation.UptimeReputationBeta, expectedBeta)
|
2018-12-17 18:47:04 +00:00
|
|
|
}
|
2018-12-14 20:17:30 +00:00
|
|
|
}
|