Skip to content

Send IDONTWANT before first publish #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion gossipsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2847,7 +2847,7 @@ var _ RawTracer = &mockRawTracer{}
func TestGossipsubNoIDONTWANTToMessageSender(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := getDefaultHosts(t, 3)
hosts := getDefaultHosts(t, 2)
denseConnect(t, hosts)

psubs := make([]*PubSub, 2)
Expand Down Expand Up @@ -2886,6 +2886,67 @@ func TestGossipsubNoIDONTWANTToMessageSender(t *testing.T) {
t.Fatal("IDONTWANT should not be sent to the message sender")
case <-time.After(time.Second):
}
}

func TestGossipsubIDONTWANTBeforeFirstPublish(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
hosts := getDefaultHosts(t, 2)
denseConnect(t, hosts)

psubs := make([]*PubSub, 2)

psubs[0] = getGossipsub(ctx, hosts[0])
rpcsReceived := make(chan string)
psubs[1] = getGossipsub(ctx, hosts[1], WithRawTracer(&mockRawTracer{
onRecvRPC: func(rpc *RPC) {
if len(rpc.GetControl().GetIdontwant()) > 0 {
rpcsReceived <- "idontwant"
}
if len(rpc.GetPublish()) > 0 {
rpcsReceived <- "publish"
}
},
}))

topicString := "foobar"
var topics []*Topic
for _, ps := range psubs {
topic, err := ps.Join(topicString)
if err != nil {
t.Fatal(err)
}
topics = append(topics, topic)

_, err = ps.Subscribe(topicString)
if err != nil {
t.Fatal(err)
}
}
time.Sleep(2 * time.Second)

msg := make([]byte, GossipSubIDontWantMessageThreshold+1)
_ = topics[0].Publish(ctx, msg)

timeout := time.After(5 * time.Second)

select {
case kind := <-rpcsReceived:
if kind == "publish" {
t.Fatal("IDONTWANT should be sent before publish")
}
case <-timeout:
t.Fatal("IDONTWANT should be sent on first publish")
}

select {
case kind := <-rpcsReceived:
if kind != "publish" {
t.Fatal("Expected publish after IDONTWANT")
}
case <-timeout:
t.Fatal("Expected publish after IDONTWANT")
}

}

Expand Down
1 change: 1 addition & 0 deletions topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ func (t *Topic) validate(ctx context.Context, data []byte, opts ...PubOpt) (*Mes
}

msg := &Message{m, "", t.p.host.ID(), pub.validatorData, pub.local}
t.p.rt.PreValidation(t.p.host.ID(), []*Message{msg})
Copy link
Member

@raulk raulk May 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're working on this area, how about we rename PreValidation() to something more accurate/obvious, e.g. DispatchIDONTWANT() or even something more generic like Preprocess() or Intercept()? The spirit of the method just isn't to validate (returns nothing), and it's confusing when juxtaposed with ValidateLocal() ("what validation happens in each?").

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

names, names, names 🤣

But yes, preprocessing is what it does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No strong opinion. Preprocess seems fine.

err := t.p.val.ValidateLocal(msg)
if err != nil {
return nil, err
Expand Down