From 99252e49f88c113a5177763a4eb67513cc0c8e97 Mon Sep 17 00:00:00 2001 From: NIC619 Date: Sun, 21 Jul 2019 22:28:17 +0800 Subject: [PATCH] Prevent re-adding peers to mesh --- libp2p/pubsub/gossipsub.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/libp2p/pubsub/gossipsub.py b/libp2p/pubsub/gossipsub.py index f1d057c4..31ab606f 100644 --- a/libp2p/pubsub/gossipsub.py +++ b/libp2p/pubsub/gossipsub.py @@ -204,8 +204,9 @@ class GossipSub(IPubsubRouter): # Add fanout peers to mesh and notifies them with a GRAFT(topic) control message. for peer in fanout_peers: - self.mesh[topic].append(peer) - await self.emit_graft(topic, peer) + if peer not in self.mesh[topic]: + self.mesh[topic].append(peer) + await self.emit_graft(topic, peer) if topic_in_fanout: del self.fanout[topic] @@ -281,7 +282,12 @@ class GossipSub(IPubsubRouter): self.mesh[topic] ) - for peer in selected_peers: + fanout_peers_not_in_mesh = [ + peer + for peer in selected_peers + if peer not in self.mesh[topic] + ] + for peer in fanout_peers_not_in_mesh: # Add peer to mesh[topic] self.mesh[topic].append(peer) @@ -460,7 +466,8 @@ class GossipSub(IPubsubRouter): # Add peer to mesh for topic if topic in self.mesh: - self.mesh[topic].append(from_id_str) + if from_id_str not in self.mesh[topic]: + self.mesh[topic].append(from_id_str) else: # Respond with PRUNE if not subscribed to the topic await self.emit_prune(topic, sender_peer_id)