Skip to content

Commit

Permalink
feat: allow usage of custom codecs (#288)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
D4nte authored Jul 19, 2022
1 parent 8f3438a commit 0e76f49
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
18 changes: 14 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -2411,7 +2411,12 @@ export class GossipSub extends EventEmitter<GossipsubEvents> 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
Expand Down Expand Up @@ -2615,7 +2620,12 @@ export class GossipSub extends EventEmitter<GossipsubEvents> 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
Expand Down Expand Up @@ -2676,7 +2686,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements Initiali
if (!peerStreams) {
return
}
if (hasGossipProtocol(peerStreams.protocol) && filter(id)) {
if (this.multicodecs.includes(peerStreams.protocol) && filter(id)) {
peers.push(id)
}
})
Expand Down
5 changes: 0 additions & 5 deletions src/utils/has-gossip-protocol.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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'

0 comments on commit 0e76f49

Please sign in to comment.