1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package p2p
import (
"math"
"testing"
"time"
"github.com/ethereum-optimism/optimism/op-node/chaincfg"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/stretchr/testify/suite"
)
// PeerParamsTestSuite tests peer parameterization.
type PeerParamsTestSuite struct {
suite.Suite
}
// TestPeerParams runs the PeerParamsTestSuite.
func TestPeerParams(t *testing.T) {
suite.Run(t, new(PeerParamsTestSuite))
}
// TestPeerScoreConstants validates the peer score constants.
func (testSuite *PeerParamsTestSuite) TestPeerScoreConstants() {
testSuite.Equal(0.01, DecayToZero)
}
// TestNewPeerScoreThresholds validates the peer score thresholds.
//
// This is tested to ensure that the thresholds are not modified and missed in review.
func (testSuite *PeerParamsTestSuite) TestNewPeerScoreThresholds() {
thresholds := NewPeerScoreThresholds()
expected := pubsub.PeerScoreThresholds{
SkipAtomicValidation: false,
GossipThreshold: -10,
PublishThreshold: -40,
GraylistThreshold: -40,
AcceptPXThreshold: 20,
OpportunisticGraftThreshold: 0.05,
}
testSuite.Equal(expected, thresholds)
}
// TestGetPeerScoreParams validates the peer score parameters.
func (testSuite *PeerParamsTestSuite) TestGetPeerScoreParams_None() {
params, err := GetScoringParams("none", &chaincfg.Goerli)
testSuite.NoError(err)
testSuite.Nil(params)
}
// TestLightPeerScoreParams validates the light peer score params.
func (testSuite *PeerParamsTestSuite) TestGetPeerScoreParams_Light() {
cfg := chaincfg.Goerli
cfg.BlockTime = 1
slot := time.Duration(cfg.BlockTime) * time.Second
epoch := 6 * slot
oneHundredEpochs := 100 * epoch
// calculate the behavior penalty decay
duration := 10 * epoch
decay := math.Pow(DecayToZero, 1/float64(duration/slot))
testSuite.Equal(0.9261187281287935, decay)
// Test the params
scoringParams, err := GetScoringParams("light", &cfg)
peerParams := scoringParams.PeerScoring
testSuite.NoError(err)
// Topics should contain options for block topic
testSuite.Len(peerParams.Topics, 1)
topicParams, ok := peerParams.Topics[blocksTopicV1(&cfg)]
testSuite.True(ok, "should have block topic params")
testSuite.NotZero(topicParams.TimeInMeshQuantum)
testSuite.Equal(peerParams.TopicScoreCap, float64(34))
testSuite.Equal(peerParams.AppSpecificWeight, float64(1))
testSuite.Equal(peerParams.IPColocationFactorWeight, float64(-35))
testSuite.Equal(peerParams.IPColocationFactorThreshold, 10)
testSuite.Nil(peerParams.IPColocationFactorWhitelist)
testSuite.Equal(peerParams.BehaviourPenaltyWeight, float64(-16))
testSuite.Equal(peerParams.BehaviourPenaltyThreshold, float64(6))
testSuite.Equal(peerParams.BehaviourPenaltyDecay, decay)
testSuite.Equal(peerParams.DecayInterval, slot)
testSuite.Equal(peerParams.DecayToZero, DecayToZero)
testSuite.Equal(peerParams.RetainScore, oneHundredEpochs)
appParams := scoringParams.ApplicationScoring
testSuite.Positive(appParams.ValidResponseCap)
testSuite.Positive(appParams.ValidResponseWeight)
testSuite.Positive(appParams.ValidResponseDecay)
testSuite.Positive(appParams.ErrorResponseCap)
testSuite.Negative(appParams.ErrorResponseWeight)
testSuite.Positive(appParams.ErrorResponseDecay)
testSuite.Positive(appParams.RejectedPayloadCap)
testSuite.Negative(appParams.RejectedPayloadWeight)
testSuite.Positive(appParams.RejectedPayloadDecay)
testSuite.Equal(DecayToZero, appParams.DecayToZero)
testSuite.Equal(slot, appParams.DecayInterval)
}
// TestParamsZeroBlockTime validates peer score params use default slot for 0 block time.
func (testSuite *PeerParamsTestSuite) TestParamsZeroBlockTime() {
cfg := chaincfg.Goerli
cfg.BlockTime = 0
slot := 2 * time.Second
params, err := GetScoringParams("light", &cfg)
testSuite.NoError(err)
testSuite.Equal(params.PeerScoring.DecayInterval, slot)
testSuite.Equal(params.ApplicationScoring.DecayInterval, slot)
}