@@ -140,8 +140,14 @@ def __init__(
140140 self .mcache = MessageCache (gossip_window , gossip_history )
141141
142142 # Whether to flood publish to all peers instead of following gossipsub
143- # mesh/fanout logic. Kept as an option primarily for tests and
144- # compatibility with the test factory. Default is False.
143+ # mesh/fanout logic when acting as the original publisher.
144+ # When enabled, this behaves as a hybrid between FloodSub and GossipSub:
145+ # - When this node is the original publisher: Message is sent to ALL peers
146+ # who are subscribed to the topic (flood publishing behavior)
147+ # - When this node is forwarding a message: Regular GossipSub behavior is used
148+ # This provides better reliability at publication time with a reasonable
149+ # bandwidth cost since it only affects the original publisher.
150+ # Default is False.
145151 self .flood_publish = flood_publish
146152
147153 # Create heartbeat timer
@@ -306,43 +312,52 @@ def _get_peers_to_send(
306312 if topic not in self .pubsub .peer_topics :
307313 continue
308314
309- # direct peers
310- _direct_peers : set [ID ] = {_peer for _peer in self .direct_peers }
311- send_to .update (_direct_peers )
312-
313- # floodsub peers
314- floodsub_peers : set [ID ] = {
315- peer_id
316- for peer_id in self .pubsub .peer_topics [topic ]
317- if peer_id in self .peer_protocol
318- and self .peer_protocol [peer_id ] == floodsub .PROTOCOL_ID
319- }
320- send_to .update (floodsub_peers )
321-
322- # gossipsub peers
323- gossipsub_peers : set [ID ] = set ()
324- if topic in self .mesh :
325- gossipsub_peers = self .mesh [topic ]
315+ # If flood_publish is enabled and we are the original publisher,
316+ # send to all peers in the topic (flood publishing behavior)
317+ if self .flood_publish and msg_forwarder == self .pubsub .my_id :
318+ for peer in self .pubsub .peer_topics [topic ]:
319+ # TODO: add score threshold check when peer scoring is implemented
320+ # if direct peer then skip score check
321+ send_to .add (peer )
326322 else :
327- # When we publish to a topic that we have not subscribe to, we randomly
328- # pick `self.degree` number of peers who have subscribed to the topic
329- # and add them as our `fanout` peers.
330- topic_in_fanout : bool = topic in self .fanout
331- fanout_peers : set [ID ] = self .fanout [topic ] if topic_in_fanout else set ()
332- fanout_size = len (fanout_peers )
333- if not topic_in_fanout or (
334- topic_in_fanout and fanout_size < self .degree
335- ):
336- if topic in self .pubsub .peer_topics :
337- # Combine fanout peers with selected peers
338- fanout_peers .update (
339- self ._get_in_topic_gossipsub_peers_from_minus (
340- topic , self .degree - fanout_size , fanout_peers
323+ # Regular GossipSub routing logic
324+ # direct peers
325+ _direct_peers : set [ID ] = {_peer for _peer in self .direct_peers }
326+ send_to .update (_direct_peers )
327+
328+ # floodsub peers
329+ floodsub_peers : set [ID ] = {
330+ peer_id
331+ for peer_id in self .pubsub .peer_topics [topic ]
332+ if peer_id in self .peer_protocol
333+ and self .peer_protocol [peer_id ] == floodsub .PROTOCOL_ID
334+ }
335+ send_to .update (floodsub_peers )
336+
337+ # gossipsub peers
338+ gossipsub_peers : set [ID ] = set ()
339+ if topic in self .mesh :
340+ gossipsub_peers = self .mesh [topic ]
341+ else :
342+ # When we publish to a topic that we have not subscribe to, we randomly
343+ # pick `self.degree` number of peers who have subscribed to the topic
344+ # and add them as our `fanout` peers.
345+ topic_in_fanout : bool = topic in self .fanout
346+ fanout_peers : set [ID ] = self .fanout [topic ] if topic_in_fanout else set ()
347+ fanout_size = len (fanout_peers )
348+ if not topic_in_fanout or (
349+ topic_in_fanout and fanout_size < self .degree
350+ ):
351+ if topic in self .pubsub .peer_topics :
352+ # Combine fanout peers with selected peers
353+ fanout_peers .update (
354+ self ._get_in_topic_gossipsub_peers_from_minus (
355+ topic , self .degree - fanout_size , fanout_peers
356+ )
341357 )
342- )
343- self .fanout [topic ] = fanout_peers
344- gossipsub_peers = fanout_peers
345- send_to .update (gossipsub_peers )
358+ self .fanout [topic ] = fanout_peers
359+ gossipsub_peers = fanout_peers
360+ send_to .update (gossipsub_peers )
346361 # Excludes `msg_forwarder` and `origin`
347362 yield from send_to .difference ([msg_forwarder , origin ])
348363
0 commit comments