Refactor gossipsub.join

This commit is contained in:
NIC619
2019-07-15 17:13:46 +08:00
parent 1e78c21eca
commit b5c3420c16

View File

@ -184,39 +184,25 @@ class GossipSub(IPubsubRouter):
# Create mesh[topic] if it does not yet exist # Create mesh[topic] if it does not yet exist
self.mesh[topic] = [] self.mesh[topic] = []
if topic in self.fanout and len(self.fanout[topic]) == self.degree: topic_in_fanout = topic in self.fanout
# If router already has D peers from the fanout peers of a topic fanout_peers = self.fanout[topic] if topic_in_fanout else []
# TODO: Do we remove all peers from fanout[topic]? fanout_size = len(fanout_peers)
if not topic_in_fanout or (topic_in_fanout and fanout_size < self.degree):
# Add them to mesh[topic], and notifies them with a # There are less than D peers (let this number be x)
# GRAFT(topic) control message. # in the fanout for a topic (or the topic is not in the fanout).
for peer in self.fanout[topic]: # Selects the remaining number of peers (D-x) from peers.gossipsub[topic].
self.mesh[topic].append(peer)
await self.emit_graft(topic, peer)
else:
# Otherwise, if there are less than D peers
# (let this number be x) in the fanout for a topic (or the topic is not in the fanout),
fanout_size = 0
if topic in self.fanout:
fanout_size = len(self.fanout[topic])
# then it still adds them as above (if there are any)
for peer in self.fanout[topic]:
self.mesh[topic].append(peer)
await self.emit_graft(topic, peer)
if topic in self.peers_gossipsub:
# TODO: Should we have self.fanout[topic] here or [] (as the minus variable)?
# Selects the remaining number of peers (D-x) from peers.gossipsub[topic]
gossipsub_peers_in_topic = [peer for peer in self.pubsub.peer_topics[topic] gossipsub_peers_in_topic = [peer for peer in self.pubsub.peer_topics[topic]
if peer in self.peers_gossipsub] if peer in self.peers_gossipsub]
selected_peers = \ selected_peers = \
GossipSub.select_from_minus(self.degree - fanout_size, GossipSub.select_from_minus(self.degree - fanout_size,
gossipsub_peers_in_topic, gossipsub_peers_in_topic,
self.fanout[topic] if topic in self.fanout else []) fanout_peers)
# And likewise adds them to mesh[topic] and notifies them with a # Combine fanout peers with selected peers
# GRAFT(topic) control message. fanout_peers += selected_peers
for peer in selected_peers:
# Add fanout peers to mesh and notifies them with a GRAFT(topic) control message.
for peer in fanout_peers:
self.mesh[topic].append(peer) self.mesh[topic].append(peer)
await self.emit_graft(topic, peer) await self.emit_graft(topic, peer)