From 7ef51800ed270f1f6639a9c4aa8501a022fe5fd4 Mon Sep 17 00:00:00 2001 From: nisdas Date: Thu, 10 Sep 2020 16:24:14 +0800 Subject: [PATCH 1/3] copy string topic --- gossipsub.go | 7 ++++++- gossipsub_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/gossipsub.go b/gossipsub.go index cf5f9b10..e1fddede 100644 --- a/gossipsub.go +++ b/gossipsub.go @@ -1550,7 +1550,12 @@ func (gs *GossipSubRouter) sendGraftPrune(tograft, toprune map[peer.ID][]string, for p, topics := range tograft { graft := make([]*pb.ControlGraft, 0, len(topics)) for _, topic := range topics { - graft = append(graft, &pb.ControlGraft{TopicID: &topic}) + // copy topic string here since + // the reference to the string + // topic here changes with every + // iteration of the slice. + copiedID := topic + graft = append(graft, &pb.ControlGraft{TopicID: &copiedID}) } var prune []*pb.ControlPrune diff --git a/gossipsub_test.go b/gossipsub_test.go index 6e56ee82..2f8d897b 100644 --- a/gossipsub_test.go +++ b/gossipsub_test.go @@ -1567,6 +1567,50 @@ func TestGossipsubPiggybackControl(t *testing.T) { } } +func TestGossipsubMultipleGraftTopics(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + hosts := getNetHosts(t, ctx, 2) + + psubs := getGossipsubs(ctx, hosts) + + sparseConnect(t, hosts) + + time.Sleep(time.Second * 1) + + firstTopic := "topic1" + secondTopic := "topic2" + thirdTopic := "topic3" + + firstPeer := hosts[0].ID() + secondPeer := hosts[1].ID() + + p1Router := psubs[0].rt.(*GossipSubRouter) + p2Router := psubs[1].rt.(*GossipSubRouter) + // Add topics to second peer + p2Router.mesh[firstTopic] = map[peer.ID]struct{}{} + p2Router.mesh[secondTopic] = map[peer.ID]struct{}{} + p2Router.mesh[thirdTopic] = map[peer.ID]struct{}{} + + // Send multiple GRAFT messages to second peer from + // 1st peer + p1Router.sendGraftPrune(map[peer.ID][]string{ + secondPeer: []string{firstTopic, secondTopic, thirdTopic}, + }, map[peer.ID][]string{}, map[peer.ID]bool{}) + + time.Sleep(time.Second * 1) + + if _, ok := p2Router.mesh[firstTopic][firstPeer]; !ok { + t.Errorf("First peer wasnt added to mesh of the second peer for the topic %s", firstTopic) + } + if _, ok := p2Router.mesh[secondTopic][firstPeer]; !ok { + t.Errorf("First peer wasnt added to mesh of the second peer for the topic %s", secondTopic) + } + if _, ok := p2Router.mesh[thirdTopic][firstPeer]; !ok { + t.Errorf("First peer wasnt added to mesh of the second peer for the topic %s", thirdTopic) + } +} + func TestGossipsubOpportunisticGrafting(t *testing.T) { originalGossipSubPruneBackoff := GossipSubPruneBackoff GossipSubPruneBackoff = 500 * time.Millisecond From 4714eaae0e7204bf3f45c902e896896df62050bd Mon Sep 17 00:00:00 2001 From: nisdas Date: Thu, 10 Sep 2020 16:27:18 +0800 Subject: [PATCH 2/3] clean up --- gossipsub_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/gossipsub_test.go b/gossipsub_test.go index 2f8d897b..458589bd 100644 --- a/gossipsub_test.go +++ b/gossipsub_test.go @@ -1570,10 +1570,9 @@ func TestGossipsubPiggybackControl(t *testing.T) { func TestGossipsubMultipleGraftTopics(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - hosts := getNetHosts(t, ctx, 2) + hosts := getNetHosts(t, ctx, 2) psubs := getGossipsubs(ctx, hosts) - sparseConnect(t, hosts) time.Sleep(time.Second * 1) From 93db89fe8e834a4cdfaba65f6f5c3fc8042128ae Mon Sep 17 00:00:00 2001 From: nisdas Date: Thu, 10 Sep 2020 17:19:19 +0800 Subject: [PATCH 3/3] satisfy race detector --- gossipsub_test.go | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/gossipsub_test.go b/gossipsub_test.go index 458589bd..11f9ed35 100644 --- a/gossipsub_test.go +++ b/gossipsub_test.go @@ -1584,12 +1584,21 @@ func TestGossipsubMultipleGraftTopics(t *testing.T) { firstPeer := hosts[0].ID() secondPeer := hosts[1].ID() + p2Sub := psubs[1] p1Router := psubs[0].rt.(*GossipSubRouter) p2Router := psubs[1].rt.(*GossipSubRouter) - // Add topics to second peer - p2Router.mesh[firstTopic] = map[peer.ID]struct{}{} - p2Router.mesh[secondTopic] = map[peer.ID]struct{}{} - p2Router.mesh[thirdTopic] = map[peer.ID]struct{}{} + + finChan := make(chan struct{}) + + p2Sub.eval <- func() { + // Add topics to second peer + p2Router.mesh[firstTopic] = map[peer.ID]struct{}{} + p2Router.mesh[secondTopic] = map[peer.ID]struct{}{} + p2Router.mesh[thirdTopic] = map[peer.ID]struct{}{} + + finChan <- struct{}{} + } + <-finChan // Send multiple GRAFT messages to second peer from // 1st peer @@ -1599,15 +1608,19 @@ func TestGossipsubMultipleGraftTopics(t *testing.T) { time.Sleep(time.Second * 1) - if _, ok := p2Router.mesh[firstTopic][firstPeer]; !ok { - t.Errorf("First peer wasnt added to mesh of the second peer for the topic %s", firstTopic) - } - if _, ok := p2Router.mesh[secondTopic][firstPeer]; !ok { - t.Errorf("First peer wasnt added to mesh of the second peer for the topic %s", secondTopic) - } - if _, ok := p2Router.mesh[thirdTopic][firstPeer]; !ok { - t.Errorf("First peer wasnt added to mesh of the second peer for the topic %s", thirdTopic) + p2Sub.eval <- func() { + if _, ok := p2Router.mesh[firstTopic][firstPeer]; !ok { + t.Errorf("First peer wasnt added to mesh of the second peer for the topic %s", firstTopic) + } + if _, ok := p2Router.mesh[secondTopic][firstPeer]; !ok { + t.Errorf("First peer wasnt added to mesh of the second peer for the topic %s", secondTopic) + } + if _, ok := p2Router.mesh[thirdTopic][firstPeer]; !ok { + t.Errorf("First peer wasnt added to mesh of the second peer for the topic %s", thirdTopic) + } + finChan <- struct{}{} } + <-finChan } func TestGossipsubOpportunisticGrafting(t *testing.T) {