@@ -185,6 +185,9 @@ export default class Gossipsub extends EventEmitter {
185
185
/** Direct peers */
186
186
private readonly direct = new Set < PeerIdStr > ( )
187
187
188
+ /** Floodsub peers */
189
+ private readonly floodsubPeers = new Set < PeerIdStr > ( )
190
+
188
191
/** Cache of seen messages */
189
192
private readonly seenCache : SimpleTimeCache < void >
190
193
@@ -595,29 +598,33 @@ export default class Gossipsub extends EventEmitter {
595
598
* Add a peer to the router
596
599
*/
597
600
private addPeer ( peerId : PeerId , protocol : string , direction : ConnectionDirection ) : PeerStreams {
598
- let peerStreams = this . peers . get ( peerId . toB58String ( ) )
601
+ const peerIdStr = peerId . toB58String ( )
602
+ let peerStreams = this . peers . get ( peerIdStr )
599
603
600
604
// If peer streams already exists, do nothing
601
605
if ( peerStreams === undefined ) {
602
606
// else create a new peer streams
603
- this . log ( 'new peer %s' , peerId . toB58String ( ) )
607
+ this . log ( 'new peer %s' , peerIdStr )
604
608
605
609
peerStreams = new PeerStreams ( {
606
610
id : peerId ,
607
611
protocol
608
612
} )
609
613
610
- this . peers . set ( peerId . toB58String ( ) , peerStreams )
614
+ this . peers . set ( peerIdStr , peerStreams )
611
615
peerStreams . addListener ( 'close' , ( ) => this . removePeer ( peerId ) )
612
616
}
613
617
614
618
// Add to peer scoring
615
- this . score . addPeer ( peerId . toB58String ( ) )
619
+ this . score . addPeer ( peerIdStr )
620
+ if ( protocol === constants . FloodsubID ) {
621
+ this . floodsubPeers . add ( peerIdStr )
622
+ }
616
623
this . metrics ?. peersPerProtocol . inc ( { protocol } , 1 )
617
624
618
625
// track the connection direction. Don't allow to unset outbound
619
- if ( ! this . outbound . get ( peerId . toB58String ( ) ) ) {
620
- this . outbound . set ( peerId . toB58String ( ) , direction === 'outbound' )
626
+ if ( ! this . outbound . get ( peerIdStr ) ) {
627
+ this . outbound . set ( peerIdStr , direction === 'outbound' )
621
628
}
622
629
623
630
return peerStreams
@@ -660,6 +667,8 @@ export default class Gossipsub extends EventEmitter {
660
667
peers . delete ( id )
661
668
}
662
669
670
+ // Remove from floodsubPeers
671
+ this . floodsubPeers . delete ( id )
663
672
// Remove from gossip mapping
664
673
this . gossip . delete ( id )
665
674
// Remove from control mapping
@@ -1617,6 +1626,20 @@ export default class Gossipsub extends EventEmitter {
1617
1626
tosend . add ( peer )
1618
1627
}
1619
1628
} )
1629
+
1630
+ // As of Mar 2022, spec + golang-libp2p include this while rust-libp2p does not
1631
+ // rust-libp2p: https://github.com/libp2p/rust-libp2p/blob/6cc3b4ec52c922bfcf562a29b5805c3150e37c75/protocols/gossipsub/src/behaviour.rs#L2693
1632
+ // spec: https://github.com/libp2p/specs/blob/10712c55ab309086a52eec7d25f294df4fa96528/pubsub/gossipsub/gossipsub-v1.0.md?plain=1#L361
1633
+ this . floodsubPeers . forEach ( ( peer ) => {
1634
+ if (
1635
+ peersInTopic . has ( peer ) &&
1636
+ propagationSource !== peer &&
1637
+ ! excludePeers ?. has ( peer ) &&
1638
+ this . score . score ( peer ) >= this . opts . scoreThresholds . publishThreshold
1639
+ ) {
1640
+ tosend . add ( peer )
1641
+ }
1642
+ } )
1620
1643
}
1621
1644
1622
1645
// add mesh peers
@@ -1673,14 +1696,9 @@ export default class Gossipsub extends EventEmitter {
1673
1696
} )
1674
1697
1675
1698
// floodsub peers
1676
- peersInTopic . forEach ( ( id ) => {
1677
- const peerStreams = this . peers . get ( id )
1678
-
1679
- if (
1680
- peerStreams &&
1681
- peerStreams . protocol === constants . FloodsubID &&
1682
- this . score . score ( id ) >= this . opts . scoreThresholds . publishThreshold
1683
- ) {
1699
+ // Note: if there are no floodsub peers, we save a loop through peersInTopic Map
1700
+ this . floodsubPeers . forEach ( ( id ) => {
1701
+ if ( peersInTopic . has ( id ) && this . score . score ( id ) >= this . opts . scoreThresholds . publishThreshold ) {
1684
1702
tosend . add ( id )
1685
1703
tosendCount . floodsub ++
1686
1704
}
0 commit comments