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"
|
2020-04-29 18:58:41 +01:00
|
|
|
"fmt"
|
2018-12-14 20:17:30 +00:00
|
|
|
"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"
|
2023-06-26 09:25:13 +01:00
|
|
|
"golang.org/x/exp/slices"
|
2018-12-14 20:17:30 +00:00
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/storj"
|
2022-02-25 10:43:19 +00:00
|
|
|
"storj.io/common/storj/location"
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/testcontext"
|
2018-12-27 09:56:25 +00:00
|
|
|
"storj.io/storj/satellite"
|
2023-07-07 09:31:58 +01:00
|
|
|
"storj.io/storj/satellite/nodeselection"
|
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) {
|
2020-01-19 16:29:15 +00:00
|
|
|
satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) {
|
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) {
|
2023-06-26 09:25:13 +01:00
|
|
|
{ // Test KnownReliable and Reliable
|
2020-04-29 18:58:41 +01:00
|
|
|
for i, tt := range []struct {
|
2021-03-18 19:55:06 +00:00
|
|
|
nodeID storj.NodeID
|
|
|
|
unknownAuditSuspended bool
|
|
|
|
offlineSuspended bool
|
|
|
|
disqualified bool
|
|
|
|
offline bool
|
|
|
|
gracefullyexited bool
|
2022-02-25 10:43:19 +00:00
|
|
|
countryCode string
|
2019-05-01 14:45:52 +01:00
|
|
|
}{
|
2022-02-25 10:43:19 +00:00
|
|
|
{storj.NodeID{1}, false, false, false, false, false, "DE"}, // good
|
|
|
|
{storj.NodeID{2}, false, false, true, false, false, "DE"}, // disqualified
|
|
|
|
{storj.NodeID{3}, true, false, false, false, false, "DE"}, // unknown audit suspended
|
|
|
|
{storj.NodeID{4}, false, false, false, true, false, "DE"}, // offline
|
|
|
|
{storj.NodeID{5}, false, false, false, false, true, "DE"}, // gracefully exited
|
|
|
|
{storj.NodeID{6}, false, true, false, false, false, "DE"}, // offline suspended
|
|
|
|
{storj.NodeID{7}, false, false, false, false, false, "FR"}, // excluded country
|
|
|
|
{storj.NodeID{8}, false, false, false, false, false, ""}, // good
|
2019-05-01 14:45:52 +01:00
|
|
|
} {
|
2020-04-29 18:58:41 +01:00
|
|
|
addr := fmt.Sprintf("127.0.%d.0:8080", i)
|
|
|
|
lastNet := fmt.Sprintf("127.0.%d", i)
|
|
|
|
d := overlay.NodeCheckInInfo{
|
2022-02-25 10:43:19 +00:00
|
|
|
NodeID: tt.nodeID,
|
2023-01-24 15:59:47 +00:00
|
|
|
Address: &pb.NodeAddress{Address: addr},
|
2022-02-25 10:43:19 +00:00
|
|
|
LastIPPort: addr,
|
|
|
|
LastNet: lastNet,
|
|
|
|
Version: &pb.NodeVersion{Version: "v1.0.0"},
|
|
|
|
Capacity: &pb.NodeCapacity{},
|
|
|
|
IsUp: true,
|
|
|
|
CountryCode: location.ToCountryCode(tt.countryCode),
|
2020-04-29 18:58:41 +01:00
|
|
|
}
|
|
|
|
err := cache.UpdateCheckIn(ctx, d, time.Now().UTC(), overlay.NodeSelectionConfig{})
|
2019-06-21 16:23:15 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-18 19:55:06 +00:00
|
|
|
if tt.unknownAuditSuspended {
|
2021-07-15 15:14:13 +01:00
|
|
|
err = cache.TestSuspendNodeUnknownAudit(ctx, tt.nodeID, time.Now())
|
2020-03-11 21:11:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2021-03-18 19:55:06 +00:00
|
|
|
|
|
|
|
if tt.offlineSuspended {
|
2021-07-07 20:20:23 +01:00
|
|
|
err = cache.TestSuspendNodeOffline(ctx, tt.nodeID, time.Now())
|
|
|
|
require.NoError(t, err)
|
2021-03-18 19:55:06 +00:00
|
|
|
}
|
2021-07-07 20:20:23 +01:00
|
|
|
|
2020-03-11 21:11:46 +00:00
|
|
|
if tt.disqualified {
|
2022-10-11 17:13:29 +01:00
|
|
|
_, err = cache.DisqualifyNode(ctx, tt.nodeID, time.Now(), overlay.DisqualificationReasonUnknown)
|
2020-03-11 21:11:46 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
if tt.offline {
|
|
|
|
checkInInfo := getNodeInfo(tt.nodeID)
|
|
|
|
err = cache.UpdateCheckIn(ctx, checkInInfo, time.Now().Add(-2*time.Hour), overlay.NodeSelectionConfig{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2020-04-23 20:46:16 +01:00
|
|
|
if tt.gracefullyexited {
|
|
|
|
req := &overlay.ExitStatusRequest{
|
|
|
|
NodeID: tt.nodeID,
|
|
|
|
ExitInitiatedAt: time.Now(),
|
|
|
|
ExitLoopCompletedAt: time.Now(),
|
|
|
|
ExitFinishedAt: time.Now(),
|
|
|
|
}
|
|
|
|
_, err := cache.UpdateExitStatus(ctx, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
2019-05-01 14:45:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
nodeIds := storj.NodeIDList{
|
|
|
|
storj.NodeID{1}, storj.NodeID{2},
|
2020-03-11 21:11:46 +00:00
|
|
|
storj.NodeID{3}, storj.NodeID{4},
|
2020-04-23 20:46:16 +01:00
|
|
|
storj.NodeID{5}, storj.NodeID{6},
|
2022-02-25 10:43:19 +00:00
|
|
|
storj.NodeID{7}, storj.NodeID{8},
|
|
|
|
storj.NodeID{9},
|
2021-03-18 19:55:06 +00:00
|
|
|
}
|
2023-07-07 09:31:58 +01:00
|
|
|
contains := func(nodeID storj.NodeID) func(node nodeselection.SelectedNode) bool {
|
|
|
|
return func(node nodeselection.SelectedNode) bool {
|
2023-06-26 09:25:13 +01:00
|
|
|
return node.ID == nodeID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 09:38:47 +01:00
|
|
|
online, offline, err := cache.KnownReliable(ctx, nodeIds, time.Hour, 0)
|
2019-05-01 14:45:52 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-06-26 09:25:13 +01:00
|
|
|
// unrealiable nodes shouldn't be in results
|
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{2}))) // disqualified
|
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{3}))) // unknown audit suspended
|
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{5}))) // gracefully exited
|
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{6}))) // offline suspended
|
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{9}))) // not in db
|
|
|
|
|
|
|
|
require.True(t, slices.ContainsFunc(offline, contains(storj.NodeID{4}))) // offline
|
2023-06-29 09:38:47 +01:00
|
|
|
// KnownReliable is not excluding by country anymore
|
|
|
|
require.True(t, slices.ContainsFunc(online, contains(storj.NodeID{7}))) // excluded country
|
|
|
|
|
2023-06-26 09:25:13 +01:00
|
|
|
require.Len(t, append(online, offline...), 4)
|
2021-03-18 19:55:06 +00:00
|
|
|
|
2023-06-29 09:38:47 +01:00
|
|
|
online, offline, err = cache.Reliable(ctx, time.Hour, 0)
|
2021-03-18 19:55:06 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-06-29 09:38:47 +01:00
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{2}))) // disqualified
|
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{3}))) // unknown audit suspended
|
|
|
|
|
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{5}))) // gracefully exited
|
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{6}))) // offline suspended
|
|
|
|
require.False(t, slices.ContainsFunc(append(online, offline...), contains(storj.NodeID{9}))) // not in db
|
|
|
|
|
|
|
|
require.True(t, slices.ContainsFunc(offline, contains(storj.NodeID{4}))) // offline
|
|
|
|
// Reliable is not excluding by country anymore
|
|
|
|
require.True(t, slices.ContainsFunc(online, contains(storj.NodeID{7}))) // excluded country
|
|
|
|
|
|
|
|
require.Len(t, append(online, offline...), 4)
|
2019-05-01 14:45:52 +01:00
|
|
|
}
|
|
|
|
|
2019-03-01 17:46:34 +00:00
|
|
|
{ // TestUpdateOperator
|
|
|
|
nodeID := storj.NodeID{10}
|
2020-04-29 18:58:41 +01:00
|
|
|
addr := "127.0.1.0:8080"
|
|
|
|
lastNet := "127.0.1"
|
|
|
|
d := overlay.NodeCheckInInfo{
|
|
|
|
NodeID: nodeID,
|
2023-01-24 15:59:47 +00:00
|
|
|
Address: &pb.NodeAddress{Address: addr},
|
2020-04-29 18:58:41 +01:00
|
|
|
LastIPPort: addr,
|
|
|
|
LastNet: lastNet,
|
|
|
|
Version: &pb.NodeVersion{Version: "v1.0.0"},
|
|
|
|
Capacity: &pb.NodeCapacity{},
|
|
|
|
}
|
|
|
|
err := cache.UpdateCheckIn(ctx, d, time.Now().UTC(), overlay.NodeSelectionConfig{})
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2019-03-01 17:46:34 +00:00
|
|
|
|
2020-06-16 13:16:55 +01:00
|
|
|
update, err := cache.UpdateNodeInfo(ctx, nodeID, &overlay.InfoResponse{
|
2019-04-10 07:04:24 +01:00
|
|
|
Operator: &pb.NodeOperator{
|
2021-01-18 14:33:13 +00:00
|
|
|
Wallet: "0x1111111111111111111111111111111111111111",
|
|
|
|
Email: "abc123@mail.test",
|
|
|
|
WalletFeatures: []string{"wallet_features"},
|
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)
|
2021-01-18 14:33:13 +00:00
|
|
|
require.Equal(t, []string{"wallet_features"}, update.Operator.WalletFeatures)
|
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)
|
2021-01-18 14:33:13 +00:00
|
|
|
require.Equal(t, []string{"wallet_features"}, found.Operator.WalletFeatures)
|
2019-03-01 17:46:34 +00:00
|
|
|
|
2020-06-16 13:16:55 +01:00
|
|
|
updateEmail, err := cache.UpdateNodeInfo(ctx, nodeID, &overlay.InfoResponse{
|
2019-04-10 07:04:24 +01:00
|
|
|
Operator: &pb.NodeOperator{
|
2021-01-18 14:33:13 +00:00
|
|
|
Wallet: update.Operator.Wallet,
|
|
|
|
Email: "def456@mail.test",
|
|
|
|
WalletFeatures: update.Operator.WalletFeatures,
|
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)
|
2021-01-18 14:33:13 +00:00
|
|
|
assert.Equal(t, []string{"wallet_features"}, updateEmail.Operator.WalletFeatures)
|
2019-03-01 17:46:34 +00:00
|
|
|
|
2020-06-16 13:16:55 +01:00
|
|
|
updateWallet, err := cache.UpdateNodeInfo(ctx, nodeID, &overlay.InfoResponse{
|
2019-04-10 07:04:24 +01:00
|
|
|
Operator: &pb.NodeOperator{
|
2021-01-18 14:33:13 +00:00
|
|
|
Wallet: "0x2222222222222222222222222222222222222222",
|
|
|
|
Email: updateEmail.Operator.Email,
|
|
|
|
WalletFeatures: update.Operator.WalletFeatures,
|
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, 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)
|
2021-01-18 14:33:13 +00:00
|
|
|
assert.Equal(t, []string{"wallet_features"}, updateWallet.Operator.WalletFeatures)
|
|
|
|
|
|
|
|
updateWalletFeatures, err := cache.UpdateNodeInfo(ctx, nodeID, &overlay.InfoResponse{
|
|
|
|
Operator: &pb.NodeOperator{
|
|
|
|
Wallet: updateWallet.Operator.Wallet,
|
|
|
|
Email: updateEmail.Operator.Email,
|
|
|
|
WalletFeatures: []string{"wallet_features_updated"},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, updateWalletFeatures)
|
|
|
|
assert.Equal(t, "0x2222222222222222222222222222222222222222", updateWalletFeatures.Operator.Wallet)
|
|
|
|
assert.Equal(t, "def456@mail.test", updateWalletFeatures.Operator.Email)
|
|
|
|
assert.Equal(t, []string{"wallet_features_updated"}, updateWalletFeatures.Operator.WalletFeatures)
|
2019-03-01 17:46:34 +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
|
2020-01-03 00:00:18 +00:00
|
|
|
_, err := cache.Get(ctx, nodeID)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
// update check-in when node is offline
|
2020-03-10 22:05:01 +00:00
|
|
|
err = cache.UpdateCheckIn(ctx, info, time.Now(), overlay.NodeSelectionConfig{})
|
2019-09-19 19:37:31 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-03 00:00:18 +00:00
|
|
|
_, 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-09-19 19:37:31 +01:00
|
|
|
info.IsUp = true
|
|
|
|
// update check-in when node is online
|
2020-03-10 22:05:01 +00:00
|
|
|
err = cache.UpdateCheckIn(ctx, info, time.Now(), overlay.NodeSelectionConfig{})
|
2019-09-19 19:37:31 +01:00
|
|
|
require.NoError(t, err)
|
2020-01-03 00:00:18 +00:00
|
|
|
_, err = cache.Get(ctx, nodeID)
|
2019-03-29 08:53:43 +00:00
|
|
|
require.NoError(t, err)
|
2018-12-17 18:47:04 +00:00
|
|
|
}
|
2018-12-14 20:17:30 +00:00
|
|
|
}
|