11import { TypedEventEmitter , StrictSign , StrictNoSign , TopicValidatorResult , serviceCapabilities , serviceDependencies } from '@libp2p/interface'
2- import { peerIdFromBytes , peerIdFromString } from '@libp2p/peer-id'
2+ import { peerIdFromMultihash , peerIdFromString } from '@libp2p/peer-id'
33import { encode } from 'it-length-prefixed'
44import { pipe } from 'it-pipe'
55import { pushable } from 'it-pushable'
6+ import * as Digest from 'multiformats/hashes/digest'
67import * as constants from './constants.js'
78import {
89 ACCEPT_FROM_WHITELIST_DURATION_MS ,
@@ -73,7 +74,8 @@ import type {
7374 TopicValidatorFn ,
7475 Logger ,
7576 ComponentLogger ,
76- Topology
77+ Topology ,
78+ PrivateKey
7779} from '@libp2p/interface'
7880import type { ConnectionManager , IncomingStreamData , Registrar } from '@libp2p/interface-internal'
7981import type { Multiaddr } from '@multiformats/multiaddr'
@@ -166,13 +168,13 @@ export interface GossipsubOpts extends GossipsubOptsSpec, PubSubInit {
166168 maxOutboundStreams ?: number
167169
168170 /**
169- * Pass true to run on transient connections - data or time-limited
171+ * Pass true to run on limited connections - data or time-limited
170172 * connections that may be closed at any time such as circuit relay
171173 * connections.
172174 *
173175 * @default false
174176 */
175- runOnTransientConnection ?: boolean
177+ runOnLimitedConnection ?: boolean
176178
177179 /**
178180 * Specify max buffer size in bytes for OutboundStream.
@@ -259,6 +261,7 @@ interface AcceptFromWhitelistEntry {
259261}
260262
261263export interface GossipSubComponents {
264+ privateKey : PrivateKey
262265 peerId : PeerId
263266 peerStore : PeerStore
264267 registrar : Registrar
@@ -420,7 +423,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
420423 private status : GossipStatus = { code : GossipStatusCode . stopped }
421424 private readonly maxInboundStreams ?: number
422425 private readonly maxOutboundStreams ?: number
423- private readonly runOnTransientConnection ?: boolean
426+ private readonly runOnLimitedConnection ?: boolean
424427 private readonly allowedTopics : Set < TopicStr > | null
425428
426429 private heartbeatTimer : {
@@ -554,7 +557,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
554557
555558 this . maxInboundStreams = options . maxInboundStreams
556559 this . maxOutboundStreams = options . maxOutboundStreams
557- this . runOnTransientConnection = options . runOnTransientConnection
560+ this . runOnLimitedConnection = options . runOnLimitedConnection
558561
559562 this . allowedTopics = ( opts . allowedTopics != null ) ? new Set ( opts . allowedTopics ) : null
560563 }
@@ -591,7 +594,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
591594
592595 this . log ( 'starting' )
593596
594- this . publishConfig = await getPublishConfigFromPeerId ( this . globalSignaturePolicy , this . components . peerId )
597+ this . publishConfig = getPublishConfigFromPeerId ( this . globalSignaturePolicy , this . components . peerId , this . components . privateKey )
595598
596599 // Create the outbound inflight queue
597600 // This ensures that outbound stream creation happens sequentially
@@ -619,7 +622,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
619622 registrar . handle ( multicodec , this . onIncomingStream . bind ( this ) , {
620623 maxInboundStreams : this . maxInboundStreams ,
621624 maxOutboundStreams : this . maxOutboundStreams ,
622- runOnTransientConnection : this . runOnTransientConnection
625+ runOnLimitedConnection : this . runOnLimitedConnection
623626 } )
624627 )
625628 )
@@ -646,7 +649,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
646649 const topology : Topology = {
647650 onConnect : this . onPeerConnected . bind ( this ) ,
648651 onDisconnect : this . onPeerDisconnected . bind ( this ) ,
649- notifyOnTransient : this . runOnTransientConnection
652+ notifyOnLimitedConnection : this . runOnLimitedConnection
650653 }
651654 const registrarTopologyIds = await Promise . all (
652655 this . multicodecs . map ( async ( multicodec ) => registrar . register ( multicodec , topology ) )
@@ -817,7 +820,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
817820 try {
818821 const stream = new OutboundStream (
819822 await connection . newStream ( this . multicodecs , {
820- runOnTransientConnection : this . runOnTransientConnection
823+ runOnLimitedConnection : this . runOnLimitedConnection
821824 } ) ,
822825 ( e ) => { this . log . error ( 'outbound pipe error' , e ) } ,
823826 { maxBufferSize : this . opts . maxOutboundBufferSize }
@@ -1778,7 +1781,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
17781781 return
17791782 }
17801783
1781- const peer = peerIdFromBytes ( pi . peerID )
1784+ const peer = peerIdFromMultihash ( Digest . decode ( pi . peerID ) )
17821785 const p = peer . toString ( )
17831786
17841787 if ( this . peers . has ( p ) ) {
@@ -1895,7 +1898,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
18951898
18961899 // remove explicit peers, peers with negative scores, and backoffed peers
18971900 fanoutPeers . forEach ( ( id ) => {
1898- if ( ! this . direct . has ( id ) && this . score . score ( id ) >= 0 && ( ( backoff == null ) || ! backoff . has ( id ) ) ) {
1901+ if ( ! this . direct . has ( id ) && this . score . score ( id ) >= 0 && backoff ? .has ( id ) !== true ) {
18991902 toAdd . add ( id )
19001903 }
19011904 } )
@@ -1911,7 +1914,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
19111914 this . opts . D ,
19121915 ( id : PeerIdStr ) : boolean =>
19131916 // filter direct peers and peers with negative score
1914- ! toAdd . has ( id ) && ! this . direct . has ( id ) && this . score . score ( id ) >= 0 && ( ( backoff == null ) || ! backoff . has ( id ) )
1917+ ! toAdd . has ( id ) && ! this . direct . has ( id ) && this . score . score ( id ) >= 0 && backoff ? .has ( id ) !== true
19151918 )
19161919
19171920 newPeers . forEach ( ( peer ) => {
@@ -2610,13 +2613,13 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
26102613 try {
26112614 peerInfo = await this . components . peerStore . get ( id )
26122615 } catch ( err : any ) {
2613- if ( err . code !== 'ERR_NOT_FOUND ' ) {
2616+ if ( err . name !== 'NotFoundError ' ) {
26142617 throw err
26152618 }
26162619 }
26172620
26182621 return {
2619- peerID : id . toBytes ( ) ,
2622+ peerID : id . toMultihash ( ) . bytes ,
26202623 signedPeerRecord : peerInfo ?. peerRecordEnvelope
26212624 }
26222625 } )
@@ -2741,7 +2744,7 @@ export class GossipSub extends TypedEventEmitter<GossipsubEvents> implements Pub
27412744 ! this . direct . has ( id )
27422745 ) {
27432746 const score = getScore ( id )
2744- if ( ( ( backoff == null ) || ! backoff . has ( id ) ) && score >= 0 ) candidateMeshPeers . add ( id )
2747+ if ( backoff ? .has ( id ) !== true && score >= 0 ) candidateMeshPeers . add ( id )
27452748 // instead of having to find gossip peers after heartbeat which require another loop
27462749 // we prepare peers to gossip in a topic within heartbeat to improve performance
27472750 if ( score >= this . opts . scoreThresholds . gossipThreshold ) peersToGossip . add ( id )
0 commit comments