From bc3906f4e1cc2c31dd9b98a0b6a1e51d33ac003a Mon Sep 17 00:00:00 2001 From: tuyennhv Date: Fri, 19 Apr 2024 08:08:13 +0700 Subject: [PATCH] feat: publish at least mesh_n peers (#493) * feat: publish at least mesh_n peers * chore: add more comments --- src/index.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/index.ts b/src/index.ts index 9bc059a6..a4457b02 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2036,6 +2036,20 @@ export class GossipSub extends TypedEventEmitter implements Pub tosend.add(peer) tosendCount.mesh++ }) + + // We want to publish to at least `D` peers. + // If there are insufficient peers in the mesh, publish to other topic peers + if (meshPeers.size < this.opts.D) { + // pick additional topic peers above the publishThreshold + const topicPeers = this.getRandomGossipPeers(topic, this.opts.D - meshPeers.size, (id) => { + return !meshPeers.has(id) && !this.direct.has(id) && !this.floodsubPeers.has(id) && this.score.score(id) >= this.opts.scoreThresholds.publishThreshold + }) + + topicPeers.forEach((peer) => { + tosend.add(peer) + tosendCount.mesh++ + }) + } // eslint-disable-next-line @typescript-eslint/brace-style }