Skip to content

Commit d3dc2c6

Browse files
dozyioMarcoPolo
authored andcommitted
chore: add params.Dscore validation
and adjust validation for bootstrapper case
1 parent bab0151 commit d3dc2c6

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

gossipsub.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,20 @@ type GossipSubParams struct {
242242
}
243243

244244
func (params *GossipSubParams) validate() error {
245+
if !(params.HistoryGossip <= params.HistoryLength) {
246+
return fmt.Errorf("param HistoryGossip=%d must be less than or equal to HistoryLength=%d", params.HistoryGossip, params.HistoryLength)
247+
}
248+
249+
if !(params.Dscore <= params.Dhi) {
250+
return fmt.Errorf("param Dscore=%d must be lower than or equal to Dhi=%d", params.Dscore, params.Dhi)
251+
}
252+
253+
// Bootstrappers set D=D_lo=D_hi=D_out=0
254+
// See https://github.com/libp2p/specs/blob/master/pubsub/gossipsub/gossipsub-v1.1.md#recommendations-for-network-operators
255+
if params.D == 0 && params.Dlo == 0 && params.Dhi == 0 && params.Dout == 0 {
256+
return nil
257+
}
258+
245259
if !(params.Dlo <= params.D && params.D <= params.Dhi) {
246260
return fmt.Errorf("param D=%d must be between Dlo=%d and Dhi=%d", params.D, params.Dlo, params.Dhi)
247261
}
@@ -254,10 +268,6 @@ func (params *GossipSubParams) validate() error {
254268
return fmt.Errorf("param Dout=%d must be less than Dlo=%d and Dout must not exceed D=%d / 2", params.Dout, params.Dlo, params.D)
255269
}
256270

257-
if !(params.HistoryGossip <= params.HistoryLength) {
258-
return fmt.Errorf("param HistoryGossip=%d must be less than or equal to HistoryLength=%d", params.HistoryGossip, params.HistoryLength)
259-
}
260-
261271
return nil
262272
}
263273

gossipsub_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ func TestGossipSubParamsValidate(t *testing.T) {
5757
}
5858
}
5959

60+
func TestGossipSubBootstrapParamsValidate(t *testing.T) {
61+
params := DefaultGossipSubParams()
62+
params.D = 0
63+
params.Dlo = 0
64+
params.Dhi = 0
65+
params.Dout = 0
66+
params.Dscore = 0
67+
if err := params.validate(); err != nil {
68+
t.Fatalf("Params should be valid: %v", err)
69+
}
70+
}
71+
6072
func TestSparseGossipsub(t *testing.T) {
6173
ctx, cancel := context.WithCancel(context.Background())
6274
defer cancel()

0 commit comments

Comments
 (0)