Skip to content

Commit aeb30a2

Browse files
nisdasvyzo
authored andcommitted
Add in Backoff Check
1 parent e02b347 commit aeb30a2

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

gossipsub.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,21 +1036,25 @@ func (gs *GossipSubRouter) Join(topic string) {
10361036

10371037
gmap, ok = gs.fanout[topic]
10381038
if ok {
1039+
backoff := gs.backoff[topic]
10391040
// these peers have a score above the publish threshold, which may be negative
10401041
// so drop the ones with a negative score
10411042
for p := range gmap {
1042-
if gs.score.Score(p) < 0 {
1043+
_, doBackOff := backoff[p]
1044+
if gs.score.Score(p) < 0 || doBackOff {
10431045
delete(gmap, p)
10441046
}
10451047
}
10461048

10471049
if len(gmap) < gs.params.D {
10481050
// we need more peers; eager, as this would get fixed in the next heartbeat
10491051
more := gs.getPeers(topic, gs.params.D-len(gmap), func(p peer.ID) bool {
1050-
// filter our current peers, direct peers, and peers with negative scores
1052+
// filter our current peers, direct peers, peers we are backing off, and
1053+
// peers with negative scores
10511054
_, inMesh := gmap[p]
10521055
_, direct := gs.direct[p]
1053-
return !inMesh && !direct && gs.score.Score(p) >= 0
1056+
_, doBackOff := backoff[p]
1057+
return !inMesh && !direct && !doBackOff && gs.score.Score(p) >= 0
10541058
})
10551059
for _, p := range more {
10561060
gmap[p] = struct{}{}
@@ -1060,10 +1064,12 @@ func (gs *GossipSubRouter) Join(topic string) {
10601064
delete(gs.fanout, topic)
10611065
delete(gs.lastpub, topic)
10621066
} else {
1067+
backoff := gs.backoff[topic]
10631068
peers := gs.getPeers(topic, gs.params.D, func(p peer.ID) bool {
1064-
// filter direct peers and peers with negative score
1069+
// filter direct peers, peers we are backing off and peers with negative score
10651070
_, direct := gs.direct[p]
1066-
return !direct && gs.score.Score(p) >= 0
1071+
_, doBackOff := backoff[p]
1072+
return !direct && !doBackOff && gs.score.Score(p) >= 0
10671073
})
10681074
gmap = peerListToMap(peers)
10691075
gs.mesh[topic] = gmap

gossipsub_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,51 @@ func TestGossipSubLeaveTopic(t *testing.T) {
18621862
}
18631863
}
18641864

1865+
func TestGossipSubJoinTopic(t *testing.T) {
1866+
ctx, cancel := context.WithCancel(context.Background())
1867+
defer cancel()
1868+
1869+
h := getNetHosts(t, ctx, 3)
1870+
psubs := []*PubSub{
1871+
getGossipsub(ctx, h[0]),
1872+
getGossipsub(ctx, h[1]),
1873+
getGossipsub(ctx, h[2]),
1874+
}
1875+
1876+
connect(t, h[0], h[1])
1877+
connect(t, h[0], h[2])
1878+
1879+
router0 := psubs[0].rt.(*GossipSubRouter)
1880+
1881+
// Add in backoff for peer.
1882+
peerMap := make(map[peer.ID]time.Time)
1883+
peerMap[h[1].ID()] = time.Now().Add(router0.params.PruneBackoff)
1884+
1885+
router0.backoff["test"] = peerMap
1886+
1887+
// Join all peers
1888+
var subs []*Subscription
1889+
for _, ps := range psubs {
1890+
sub, err := ps.Subscribe("test")
1891+
if err != nil {
1892+
t.Fatal(err)
1893+
}
1894+
subs = append(subs, sub)
1895+
}
1896+
1897+
time.Sleep(time.Second)
1898+
1899+
meshMap := router0.mesh["test"]
1900+
if len(meshMap) != 1 {
1901+
t.Fatalf("Unexpect peer included in the mesh")
1902+
}
1903+
1904+
_, ok := meshMap[h[1].ID()]
1905+
if ok {
1906+
t.Fatalf("Peer that was to be backed off is included in the mesh")
1907+
}
1908+
}
1909+
18651910
type sybilSquatter struct {
18661911
h host.Host
18671912
}

0 commit comments

Comments
 (0)