From 4b873a3bcca1501158728d8a201ceb44f8f55017 Mon Sep 17 00:00:00 2001 From: Franck Royer Date: Thu, 23 Jun 2022 10:15:00 +1000 Subject: [PATCH] feat: allow usage of custom codecs The gossip codecs are now available on `GossipSub.multicodecs`. Hence, the check can use this field instead of a function that uses the constants. This enables library consumers to set their own codecs by assigning a different value to `GossipSub.multicodecs`. --- src/index.ts | 18 ++++++++++++++---- src/utils/has-gossip-protocol.ts | 5 ----- src/utils/index.ts | 1 - 3 files changed, 14 insertions(+), 10 deletions(-) delete mode 100644 src/utils/has-gossip-protocol.ts diff --git a/src/index.ts b/src/index.ts index 791fbcce..65315a42 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,7 +11,7 @@ import { toString as uint8ArrayToString } from 'uint8arrays/to-string' import { MessageCache } from './message-cache.js' import { RPC } from './message/rpc.js' import * as constants from './constants.js' -import { createGossipRpc, shuffle, hasGossipProtocol, messageIdToString } from './utils/index.js' +import { createGossipRpc, shuffle, messageIdToString } from './utils/index.js' import { PeerScore, PeerScoreParams, @@ -2411,7 +2411,12 @@ export class GossipSub extends EventEmitter implements Initiali const backoff = this.backoff.get(topic) for (const id of shuffledPeers) { const peerStreams = this.streamsOutbound.get(id) - if (peerStreams && hasGossipProtocol(peerStreams.protocol) && !peers.has(id) && !this.direct.has(id)) { + if ( + peerStreams && + this.multicodecs.includes(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 @@ -2615,7 +2620,12 @@ export class GossipSub extends EventEmitter implements Initiali const shuffledPeers = shuffle(Array.from(peersInTopic)) for (const id of shuffledPeers) { const peerStreams = this.streamsOutbound.get(id) - if (peerStreams && hasGossipProtocol(peerStreams.protocol) && !fanoutPeers.has(id) && !this.direct.has(id)) { + if ( + peerStreams && + this.multicodecs.includes(peerStreams.protocol) && + !fanoutPeers.has(id) && + !this.direct.has(id) + ) { const score = getScore(id) if (score >= this.opts.scoreThresholds.publishThreshold) candidateFanoutPeers.push(id) // instead of having to find gossip peers after heartbeat which require another loop @@ -2676,7 +2686,7 @@ export class GossipSub extends EventEmitter implements Initiali if (!peerStreams) { return } - if (hasGossipProtocol(peerStreams.protocol) && filter(id)) { + if (this.multicodecs.includes(peerStreams.protocol) && filter(id)) { peers.push(id) } }) diff --git a/src/utils/has-gossip-protocol.ts b/src/utils/has-gossip-protocol.ts deleted file mode 100644 index 1be1f9a0..00000000 --- a/src/utils/has-gossip-protocol.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { GossipsubIDv10, GossipsubIDv11 } from '../constants.js' - -export function hasGossipProtocol(protocol: string): boolean { - return protocol === GossipsubIDv10 || protocol === GossipsubIDv11 -} diff --git a/src/utils/index.ts b/src/utils/index.ts index 4ff8a55a..bb6266c3 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,4 @@ export * from './create-gossip-rpc.js' export * from './shuffle.js' -export * from './has-gossip-protocol.js' export * from './messageIdToString.js' export { getPublishConfigFromPeerId } from './publishConfig.js'