From 07b995c83f7304b26546c9fd464d8c204a848547 Mon Sep 17 00:00:00 2001 From: Mohammad Kermani Date: Mon, 19 Aug 2024 18:07:21 +0330 Subject: [PATCH] feat: add `gossipFactor` parameter to `GossipsubOpts` (#502) * fix: add missing `GossipFactor` parameter to `GossipsubOpts` Based on the spec, `GossipFactor` is a parameter other than a constant, and can be configured. * fix: fix eslint errors --- src/config.ts | 5 ++++- src/index.ts | 12 +++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index 8c6da4be..4fe6c112 100644 --- a/src/config.ts +++ b/src/config.ts @@ -9,7 +9,10 @@ export interface GossipsubOptsSpec { Dscore: number /** Dout sets the quota for the number of outbound connections to maintain in a topic mesh. */ Dout: number - /** Dlazy affects how many peers we will emit gossip to at each heartbeat. */ + /** + * Dlazy affects the minimum number of peers we will emit gossip to at each + * heartbeat. + */ Dlazy: number /** heartbeatInterval is the time between heartbeats in milliseconds */ heartbeatInterval: number diff --git a/src/index.ts b/src/index.ts index 1d38a1bd..1f821e2c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -201,6 +201,14 @@ export interface GossipsubOpts extends GossipsubOptsSpec, PubSubInit { * If true, will utilize the libp2p connection manager tagging system to prune/graft connections to peers, defaults to true */ tagMeshPeers: boolean + + /** + * Specify what percent of peers to send gossip to. If the percent results in + * a number smaller than `Dlazy`, `Dlazy` will be used instead. + * + * It should be a number between 0 and 1, with a reasonable default of 0.25 + */ + gossipFactor: number } export interface GossipsubMessage { @@ -450,6 +458,7 @@ export class GossipSub extends TypedEventEmitter implements Pub opportunisticGraftPeers: constants.GossipsubOpportunisticGraftPeers, opportunisticGraftTicks: constants.GossipsubOpportunisticGraftTicks, directConnectTicks: constants.GossipsubDirectConnectTicks, + gossipFactor: constants.GossipsubGossipFactor, ...options, scoreParams: createPeerScoreParams(options.scoreParams), scoreThresholds: createPeerScoreThresholds(options.scoreThresholds) @@ -2498,7 +2507,8 @@ export class GossipSub extends TypedEventEmitter implements Pub if (candidateToGossip.size === 0) return let target = this.opts.Dlazy - const factor = constants.GossipsubGossipFactor * candidateToGossip.size + const gossipFactor = this.opts.gossipFactor + const factor = gossipFactor * candidateToGossip.size let peersToGossip: Set | PeerIdStr[] = candidateToGossip if (factor > target) { target = factor