Skip to content

Commit d53e38b

Browse files
authored
Merge pull request #642 for GossipSub Params validation
2 parents ab876fc + 24b2250 commit d53e38b

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

gossipsub.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,26 @@ type GossipSubParams struct {
241241
IDontWantMessageTTL int
242242
}
243243

244+
func (params *GossipSubParams) Validate() error {
245+
if !(params.Dlo <= params.D && params.D <= params.Dhi) {
246+
return fmt.Errorf("param D=%d must be between Dlo=%d and Dhi=%d", params.D, params.Dlo, params.Dhi)
247+
}
248+
249+
if !(params.Dscore <= params.Dhi) {
250+
return fmt.Errorf("param Dscore=%d must be less than Dhi=%d", params.Dscore, params.Dhi)
251+
}
252+
253+
if !(params.Dout < params.Dlo && params.Dout < (params.D/2)) {
254+
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)
255+
}
256+
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+
261+
return nil
262+
}
263+
244264
// NewGossipSub returns a new PubSub object using the default GossipSubRouter as the router.
245265
func NewGossipSub(ctx context.Context, h host.Host, opts ...Option) (*PubSub, error) {
246266
rt := DefaultGossipSubRouter(h)
@@ -441,6 +461,9 @@ func WithDirectConnectTicks(t uint64) Option {
441461
// config to be set when instantiating the gossipsub router.
442462
func WithGossipSubParams(cfg GossipSubParams) Option {
443463
return func(ps *PubSub) error {
464+
if err := cfg.Validate(); err != nil {
465+
return err
466+
}
444467
gs, ok := ps.rt.(*GossipSubRouter)
445468
if !ok {
446469
return fmt.Errorf("pubsub router is not gossipsub")

gossipsub_test.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ func getGossipsubs(ctx context.Context, hs []host.Host, opts ...Option) []*PubSu
4848
return psubs
4949
}
5050

51+
func TestGossipSubParamsValidate(t *testing.T) {
52+
params := DefaultGossipSubParams()
53+
params.Dhi = 1
54+
params.Dscore = 10
55+
if params.Validate() == nil {
56+
t.Fatal("Params should be invalid")
57+
}
58+
}
59+
5160
func TestSparseGossipsub(t *testing.T) {
5261
ctx, cancel := context.WithCancel(context.Background())
5362
defer cancel()
@@ -1768,6 +1777,7 @@ func TestGossipsubMultipleGraftTopics(t *testing.T) {
17681777
firstPeer := hosts[0].ID()
17691778
secondPeer := hosts[1].ID()
17701779

1780+
p1Sub := psubs[0]
17711781
p2Sub := psubs[1]
17721782
p1Router := psubs[0].rt.(*GossipSubRouter)
17731783
p2Router := psubs[1].rt.(*GossipSubRouter)
@@ -1786,9 +1796,14 @@ func TestGossipsubMultipleGraftTopics(t *testing.T) {
17861796

17871797
// Send multiple GRAFT messages to second peer from
17881798
// 1st peer
1789-
p1Router.sendGraftPrune(map[peer.ID][]string{
1790-
secondPeer: {firstTopic, secondTopic, thirdTopic},
1791-
}, map[peer.ID][]string{}, map[peer.ID]bool{})
1799+
wait := make(chan struct{})
1800+
p1Sub.eval <- func() {
1801+
defer close(wait)
1802+
p1Router.sendGraftPrune(map[peer.ID][]string{
1803+
secondPeer: {firstTopic, secondTopic, thirdTopic},
1804+
}, map[peer.ID][]string{}, map[peer.ID]bool{})
1805+
}
1806+
<-wait
17921807

17931808
time.Sleep(time.Second * 1)
17941809

@@ -2748,11 +2763,14 @@ func TestGossipsubIdontwantSend(t *testing.T) {
27482763
}
27492764
}
27502765

2766+
var midsMu sync.Mutex
27512767
var expMids []string
27522768
var actMids []string
27532769

27542770
// Used to publish a message with random data
27552771
publishMsg := func() {
2772+
midsMu.Lock()
2773+
defer midsMu.Unlock()
27562774
data := make([]byte, 16)
27572775
crand.Read(data)
27582776
m := &pb.Message{Data: data}
@@ -2769,6 +2787,8 @@ func TestGossipsubIdontwantSend(t *testing.T) {
27692787

27702788
// Checks we received the right IDONTWANT messages
27712789
checkMsgs := func() {
2790+
midsMu.Lock()
2791+
defer midsMu.Unlock()
27722792
sort.Strings(actMids)
27732793
sort.Strings(expMids)
27742794

0 commit comments

Comments
 (0)