@@ -62,12 +62,27 @@ func TestGossipSubParamsValidate(t *testing.T) {
6262 }
6363}
6464
65+ func getGossipsubsOptFn (ctx context.Context , hs []host.Host , optFn func (int , host.Host ) []Option ) []* PubSub {
66+ var psubs []* PubSub
67+ for i , h := range hs {
68+ opts := optFn (i , h )
69+ psubs = append (psubs , getGossipsub (ctx , h , opts ... ))
70+ }
71+ return psubs
72+ }
73+
6574func TestSparseGossipsub (t * testing.T ) {
6675 ctx , cancel := context .WithCancel (context .Background ())
6776 defer cancel ()
6877 hosts := getDefaultHosts (t , 20 )
6978
70- psubs := getGossipsubs (ctx , hosts )
79+ psubs := getGossipsubsOptFn (ctx , hosts , func (i int , h host.Host ) []Option {
80+ lh := slog .NewJSONHandler (os .Stdout , nil )
81+ logger := slog .New (lh .WithAttrs ([]slog.Attr {slog .String ("id" , h .ID ().String ())}))
82+ return []Option {
83+ WithRPCLogger (logger ),
84+ }
85+ })
7186
7287 var msgs []* Subscription
7388 for _ , ps := range psubs {
@@ -4356,3 +4371,100 @@ outer:
43564371 }
43574372}
43584373
4374+ func TestSkipPublishingToPeersWithPartialMessageSupport (t * testing.T ) {
4375+ topicName := "test-topic"
4376+
4377+ // 3 hosts.
4378+ // hosts[0]: Publisher. Supports partial messages
4379+ // hosts[1]: Subscriber. Supports partial messages
4380+ // hosts[2]: Alternate publisher. Does not support partial messages. Only
4381+ // connected to hosts[0]
4382+ hosts := getDefaultHosts (t , 3 )
4383+
4384+ partialExt := make ([]* partialmessages.PartialMessageExtension , 2 )
4385+ logger := slog .New (slog .NewTextHandler (os .Stderr , & slog.HandlerOptions {Level : slog .LevelDebug }))
4386+
4387+ for i := range partialExt {
4388+ partialExt [i ] = & partialmessages.PartialMessageExtension {
4389+ Logger : logger ,
4390+ ValidateRPC : func (from peer.ID , rpc * pb.PartialMessagesExtension ) error {
4391+ return nil
4392+ },
4393+ EagerIWantLimitPerHeartbeat : 0 ,
4394+ IWantLimitPerHeartbeat : 1 ,
4395+ NewPartialMessage : func (topic string , groupID []byte ) (partialmessages.PartialMessage , error ) {
4396+ return & minimalTestPartialMessage {
4397+ Group : groupID ,
4398+ onExtended : func (m * minimalTestPartialMessage ) {
4399+ t .Logf ("Received new part and extended partial message" )
4400+ },
4401+ }, nil
4402+ },
4403+ }
4404+ }
4405+
4406+ psubs := make ([]* PubSub , 0 , len (hosts )- 1 )
4407+ for i , h := range hosts [:2 ] {
4408+ psub := getGossipsub (context .Background (), h , WithPartialMessagesExtension (partialExt [i ]))
4409+ psubs = append (psubs , psub )
4410+ }
4411+
4412+ nonPartialPubsub := getGossipsub (context .Background (), hosts [2 ])
4413+
4414+ denseConnect (t , hosts [:2 ])
4415+ time .Sleep (2 * time .Second )
4416+
4417+ // Connect nonPartialPubsub to the publisher
4418+ connect (t , hosts [0 ], hosts [2 ])
4419+
4420+ var topics []* Topic
4421+ var subs []* Subscription
4422+ for _ , psub := range psubs {
4423+ topic , err := psub .Join (topicName , WithSkipPublishingToPartialMessageCapablePeers ())
4424+ if err != nil {
4425+ t .Fatal (err )
4426+ }
4427+ topics = append (topics , topic )
4428+ s , err := topic .Subscribe ()
4429+ if err != nil {
4430+ t .Fatal (err )
4431+ }
4432+ subs = append (subs , s )
4433+ }
4434+
4435+ topicForNonPartial , err := nonPartialPubsub .Join (topicName )
4436+ if err != nil {
4437+ t .Fatal (err )
4438+ }
4439+
4440+ // Wait for subscriptions to propagate
4441+ time .Sleep (time .Second )
4442+
4443+ topics [0 ].Publish (context .Background (), []byte ("Hello" ))
4444+
4445+ // Publish from another peer, the publisher (psub[0]) should not forward this to psub[1].
4446+ topicForNonPartial .Publish (context .Background (), []byte ("from non-partial" ))
4447+
4448+ recvdMessage := make (chan struct {}, 1 )
4449+ ctx , cancel := context .WithCancel (context .Background ())
4450+ defer cancel ()
4451+ go func () {
4452+ msg , err := subs [1 ].Next (ctx )
4453+ if err == context .Canceled {
4454+ return
4455+ }
4456+ if err != nil {
4457+ t .Log (err )
4458+ t .Fail ()
4459+ return
4460+ }
4461+ t .Log ("Received msg" , string (msg .Data ))
4462+ recvdMessage <- struct {}{}
4463+ }()
4464+
4465+ select {
4466+ case <- recvdMessage :
4467+ t .Fatal ("Received message" )
4468+ case <- time .After (2 * time .Second ):
4469+ }
4470+ }
0 commit comments