2021-07-07 20:20:23 +01:00
|
|
|
// Copyright (C) 2021 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package reputation_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-10-25 21:40:41 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-07-07 20:20:23 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"storj.io/common/memory"
|
|
|
|
"storj.io/common/testcontext"
|
|
|
|
"storj.io/common/testrand"
|
|
|
|
"storj.io/storj/private/testplanet"
|
|
|
|
"storj.io/storj/satellite"
|
2021-11-08 20:51:04 +00:00
|
|
|
"storj.io/storj/satellite/overlay"
|
2021-07-07 20:20:23 +01:00
|
|
|
"storj.io/storj/satellite/reputation"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestConcurrentAudit(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 1,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
planet.Satellites[0].Audit.Chore.Loop.Stop()
|
|
|
|
data := testrand.Bytes(10 * memory.MB)
|
|
|
|
err := planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "bucket", "testpath", data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
var group errgroup.Group
|
|
|
|
n := 5
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
group.Go(func() error {
|
2021-11-08 20:51:04 +00:00
|
|
|
err := planet.Satellites[0].Reputation.Service.ApplyAudit(ctx, planet.StorageNodes[0].ID(), overlay.ReputationStatus{}, reputation.AuditSuccess)
|
2021-07-07 20:20:23 +01:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
err = group.Wait()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
node, err := planet.Satellites[0].Reputation.Service.Get(ctx, planet.StorageNodes[0].ID())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(n), node.TotalAuditCount)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestApplyAudit(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 0,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Reputation.AuditLambda = 0.123
|
|
|
|
config.Reputation.AuditWeight = 0.456
|
|
|
|
config.Reputation.AuditDQ = 0
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
nodeID := planet.StorageNodes[0].ID()
|
|
|
|
service := planet.Satellites[0].Reputation.Service
|
|
|
|
config := planet.Satellites[0].Config.Reputation
|
|
|
|
|
|
|
|
// node has not been audited yet
|
|
|
|
node, err := service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Zero(t, node.TotalAuditCount)
|
|
|
|
|
2021-11-08 20:51:04 +00:00
|
|
|
status := overlay.ReputationStatus{
|
|
|
|
Disqualified: node.Disqualified,
|
|
|
|
UnknownAuditSuspended: node.UnknownAuditSuspended,
|
|
|
|
OfflineSuspended: node.OfflineSuspended,
|
|
|
|
VettedAt: node.VettedAt,
|
|
|
|
}
|
|
|
|
err = service.ApplyAudit(ctx, nodeID, status, reputation.AuditSuccess)
|
2021-07-07 20:20:23 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
node, err = service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
auditAlpha := node.AuditReputationAlpha
|
|
|
|
auditBeta := node.AuditReputationBeta
|
2021-11-08 20:51:04 +00:00
|
|
|
status = overlay.ReputationStatus{
|
|
|
|
Disqualified: node.Disqualified,
|
|
|
|
UnknownAuditSuspended: node.UnknownAuditSuspended,
|
|
|
|
OfflineSuspended: node.OfflineSuspended,
|
|
|
|
VettedAt: node.VettedAt,
|
|
|
|
}
|
2021-07-07 20:20:23 +01:00
|
|
|
|
2021-11-08 20:51:04 +00:00
|
|
|
err = service.ApplyAudit(ctx, nodeID, status, reputation.AuditSuccess)
|
2021-07-07 20:20:23 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
stats, err := service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
2021-11-08 20:51:04 +00:00
|
|
|
status = overlay.ReputationStatus{
|
|
|
|
Disqualified: stats.Disqualified,
|
|
|
|
UnknownAuditSuspended: stats.UnknownAuditSuspended,
|
|
|
|
OfflineSuspended: stats.OfflineSuspended,
|
|
|
|
VettedAt: stats.VettedAt,
|
|
|
|
}
|
2021-07-07 20:20:23 +01:00
|
|
|
|
|
|
|
expectedAuditAlpha := config.AuditLambda*auditAlpha + config.AuditWeight
|
|
|
|
expectedAuditBeta := config.AuditLambda * auditBeta
|
2022-05-07 18:43:32 +01:00
|
|
|
require.InDelta(t, stats.AuditReputationAlpha, expectedAuditAlpha, 1e-8)
|
|
|
|
require.InDelta(t, stats.AuditReputationBeta, expectedAuditBeta, 1e-8)
|
2021-07-07 20:20:23 +01:00
|
|
|
|
|
|
|
auditAlpha = expectedAuditAlpha
|
|
|
|
auditBeta = expectedAuditBeta
|
|
|
|
|
2021-11-08 20:51:04 +00:00
|
|
|
err = service.ApplyAudit(ctx, nodeID, status, reputation.AuditFailure)
|
2021-07-07 20:20:23 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
stats, err = service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expectedAuditAlpha = config.AuditLambda * auditAlpha
|
|
|
|
expectedAuditBeta = config.AuditLambda*auditBeta + config.AuditWeight
|
2022-05-07 18:43:32 +01:00
|
|
|
require.InDelta(t, stats.AuditReputationAlpha, expectedAuditAlpha, 1e-8)
|
|
|
|
require.InDelta(t, stats.AuditReputationBeta, expectedAuditBeta, 1e-8)
|
2021-07-07 20:20:23 +01:00
|
|
|
|
|
|
|
})
|
|
|
|
}
|
2021-08-02 18:48:55 +01:00
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 0,
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
nodeID := planet.StorageNodes[0].ID()
|
|
|
|
service := planet.Satellites[0].Reputation.Service
|
|
|
|
|
|
|
|
// existing node has not been audited yet should have default reputation
|
|
|
|
// score
|
|
|
|
node, err := service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Zero(t, node.TotalAuditCount)
|
2022-05-07 18:43:32 +01:00
|
|
|
require.InDelta(t, 1, node.AuditReputationAlpha, 1e-8)
|
|
|
|
require.InDelta(t, 1, node.UnknownAuditReputationAlpha, 1e-8)
|
2021-08-02 18:48:55 +01:00
|
|
|
require.EqualValues(t, 1, node.OnlineScore)
|
|
|
|
|
|
|
|
// if a node has no entry in reputation store, it should have default
|
|
|
|
// reputation score
|
|
|
|
newNode, err := service.Get(ctx, testrand.NodeID())
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Zero(t, newNode.TotalAuditCount)
|
2022-05-07 18:43:32 +01:00
|
|
|
require.InDelta(t, 1, newNode.AuditReputationAlpha, 1e-8)
|
|
|
|
require.InDelta(t, 1, newNode.UnknownAuditReputationAlpha, 1e-8)
|
2021-08-02 18:48:55 +01:00
|
|
|
require.EqualValues(t, 1, newNode.OnlineScore)
|
|
|
|
})
|
|
|
|
}
|
2021-10-25 21:40:41 +01:00
|
|
|
|
|
|
|
func TestDisqualificationAuditFailure(t *testing.T) {
|
|
|
|
testplanet.Run(t, testplanet.Config{
|
|
|
|
SatelliteCount: 1, StorageNodeCount: 1, UplinkCount: 0,
|
|
|
|
Reconfigure: testplanet.Reconfigure{
|
|
|
|
Satellite: func(log *zap.Logger, index int, config *satellite.Config) {
|
|
|
|
config.Reputation.AuditLambda = 1
|
|
|
|
config.Reputation.AuditWeight = 1
|
|
|
|
config.Reputation.AuditDQ = 0.4
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, func(t *testing.T, ctx *testcontext.Context, planet *testplanet.Planet) {
|
|
|
|
satel := planet.Satellites[0]
|
|
|
|
nodeID := planet.StorageNodes[0].ID()
|
|
|
|
|
|
|
|
nodeInfo, err := satel.Overlay.Service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, nodeInfo.Disqualified)
|
|
|
|
|
|
|
|
err = satel.Reputation.Service.ApplyAudit(ctx, nodeID, nodeInfo.Reputation.Status, reputation.AuditFailure)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// node is not disqualified after failed audit if score is above threshold
|
|
|
|
repInfo, err := satel.Reputation.Service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, repInfo.Disqualified)
|
|
|
|
nodeInfo, err = satel.Overlay.Service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Nil(t, nodeInfo.Disqualified)
|
|
|
|
|
|
|
|
err = satel.Reputation.Service.ApplyAudit(ctx, nodeID, nodeInfo.Reputation.Status, reputation.AuditFailure)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
repInfo, err = satel.Reputation.Service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, repInfo.Disqualified)
|
|
|
|
nodeInfo, err = satel.Overlay.Service.Get(ctx, nodeID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotNil(t, nodeInfo.Disqualified)
|
|
|
|
})
|
|
|
|
}
|