Skip to content

Commit

Permalink
chore!: multicodecs is now a Set
Browse files Browse the repository at this point in the history
  • Loading branch information
D4nte committed Jun 30, 2022
1 parent b21ee49 commit 6e18cd4
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
* The signature policy to follow by default
*/
public readonly globalSignaturePolicy: typeof StrictSign | typeof StrictNoSign
public multicodecs: string[] = [constants.GossipsubIDv11, constants.GossipsubIDv10]
public multicodecs: Set<string> = new Set([constants.GossipsubIDv11, constants.GossipsubIDv10])

private publishConfig: PublishConfig | undefined

Expand Down Expand Up @@ -360,7 +360,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali

// Also wants to get notified of peers connected using floodsub
if (opts.fallbackToFloodsub) {
this.multicodecs.push(constants.FloodsubID)
this.multicodecs.add(constants.FloodsubID)
}

// From pubsub
Expand Down Expand Up @@ -480,11 +480,13 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
)

const registrar = this.components.getRegistrar()
const handlePromises: Promise<void>[] = []
// Incoming streams
// Called after a peer dials us
await Promise.all(
this.multicodecs.map((multicodec) => registrar.handle(multicodec, this.onIncomingStream.bind(this)))
this.multicodecs.forEach((multicodec) =>
handlePromises.push(registrar.handle(multicodec, this.onIncomingStream.bind(this)))
)
await Promise.all(handlePromises)

// # How does Gossipsub interact with libp2p? Rough guide from Mar 2022
//
Expand All @@ -509,9 +511,10 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
onConnect: this.onPeerConnected.bind(this),
onDisconnect: this.onPeerDisconnected.bind(this)
})
const registrarTopologyIds = await Promise.all(
this.multicodecs.map((multicodec) => registrar.register(multicodec, topology))
)

const registerPromises: Promise<string>[] = []
this.multicodecs.forEach((multicodec) => registerPromises.push(registrar.register(multicodec, topology)))
const registrarTopologyIds = await Promise.all(registerPromises)

// Schedule to start heartbeat after `GossipsubHeartbeatInitialDelay`
const heartbeatTimeout = setTimeout(this.runHeartbeat, constants.GossipsubHeartbeatInitialDelay)
Expand Down Expand Up @@ -623,7 +626,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali

Promise.resolve().then(async () => {
try {
const stream = await conn.newStream(this.multicodecs)
const stream = await conn.newStream(Array.from(this.multicodecs))
// TODO remove this non-nullish assertion after https://github.com/libp2p/js-libp2p-interfaces/pull/265 is incorporated
const peer = this.addPeer(peerId, stream.stat.protocol!, conn.stat.direction)
await peer.attachOutboundStream(stream)
Expand Down Expand Up @@ -1532,7 +1535,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
private async connect(id: PeerIdStr): Promise<void> {
this.log('Initiating connection with %s', id)
const connection = await this.components.getConnectionManager().openConnection(peerIdFromString(id))
await connection.newStream(this.multicodecs)
await connection.newStream(Array.from(this.multicodecs))
// TODO: what happens to the stream?
}

Expand Down Expand Up @@ -2339,12 +2342,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
const backoff = this.backoff.get(topic)
for (const id of shuffledPeers) {
const peerStreams = this.peers.get(id)
if (
peerStreams &&
this.multicodecs.includes(peerStreams.protocol) &&
!peers.has(id) &&
!this.direct.has(id)
) {
if (peerStreams && this.multicodecs.has(peerStreams.protocol) && !peers.has(id) && !this.direct.has(id)) {
const score = getScore(id)
if ((!backoff || !backoff.has(id)) && score >= 0) candidateMeshPeers.add(id)
// instead of having to find gossip peers after heartbeat which require another loop
Expand Down Expand Up @@ -2550,7 +2548,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
const peerStreams = this.peers.get(id)
if (
peerStreams &&
this.multicodecs.includes(peerStreams.protocol) &&
this.multicodecs.has(peerStreams.protocol) &&
!fanoutPeers.has(id) &&
!this.direct.has(id)
) {
Expand Down Expand Up @@ -2614,7 +2612,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
if (!peerStreams) {
return
}
if (this.multicodecs.includes(peerStreams.protocol) && filter(id)) {
if (this.multicodecs.has(peerStreams.protocol) && filter(id)) {
peers.push(id)
}
})
Expand Down

0 comments on commit 6e18cd4

Please sign in to comment.