diff --git a/test/tracer.spec.ts b/test/tracer.spec.ts index 6a315176..26500ea1 100644 --- a/test/tracer.spec.ts +++ b/test/tracer.spec.ts @@ -8,7 +8,7 @@ describe('IWantTracer', () => { it('should track broken promises', async function () { // tests that unfullfilled promises are tracked correctly this.timeout(6000) - const t = new IWantTracer(null) + const t = new IWantTracer(constants.GossipsubIWantFollowupTime, null) const peerA = 'A' const peerB = 'B' @@ -36,7 +36,7 @@ describe('IWantTracer', () => { it('should track unbroken promises', async function () { // like above, but this time we deliver messages to fullfil the promises this.timeout(6000) - const t = new IWantTracer(null) + const t = new IWantTracer(constants.GossipsubIWantFollowupTime, null) const peerA = 'A' const peerB = 'B' diff --git a/ts/index.ts b/ts/index.ts index bb7f0971..784e70c4 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -120,6 +120,8 @@ export type GossipsubOpts = GossipsubOptsSpec & { scoreParams: Partial /** peer score thresholds */ scoreThresholds: Partial + /** customize GossipsubIWantFollowupTime in order not to apply IWANT penalties */ + gossipsubIWantFollowupTime: number dataTransform?: DataTransform metricsRegister?: MetricsRegister | null @@ -330,6 +332,7 @@ export default class Gossipsub extends EventEmitter { mcacheLength: constants.GossipsubHistoryLength, mcacheGossip: constants.GossipsubHistoryGossip, seenTTL: constants.GossipsubSeenTTL, + gossipsubIWantFollowupTime: constants.GossipsubIWantFollowupTime, ...options, scoreParams: createPeerScoreParams(options.scoreParams), scoreThresholds: createPeerScoreThresholds(options.scoreThresholds) @@ -414,7 +417,7 @@ export default class Gossipsub extends EventEmitter { this.metrics = null } - this.gossipTracer = new IWantTracer(this.metrics) + this.gossipTracer = new IWantTracer(this.opts.gossipsubIWantFollowupTime, this.metrics) /** * libp2p diff --git a/ts/tracer.ts b/ts/tracer.ts index bddb9450..5848b309 100644 --- a/ts/tracer.ts +++ b/ts/tracer.ts @@ -1,4 +1,3 @@ -import { GossipsubIWantFollowupTime } from './constants' import { messageIdToString } from './utils' import { MsgIdStr, PeerIdStr, RejectReason } from './types' import { Metrics } from './metrics' @@ -22,10 +21,11 @@ export class IWantTracer { * Necessary to know if peers are actually breaking promises or simply sending them a bit later */ private readonly requestMsByMsg = new Map() - private readonly requestMsByMsgExpire = 10 * GossipsubIWantFollowupTime + private readonly requestMsByMsgExpire: number - // eslint-disable-next-line no-useless-constructor - constructor(private readonly metrics: Metrics | null) {} + constructor(private readonly gossipsubIWantFollowupTime: number, private readonly metrics: Metrics | null) { + this.requestMsByMsgExpire = 10 * gossipsubIWantFollowupTime + } get size(): number { return this.promises.size @@ -54,7 +54,7 @@ export class IWantTracer { // If a promise for this message id and peer already exists we don't update the expiry if (!expireByPeer.has(from)) { - expireByPeer.set(from, now + GossipsubIWantFollowupTime) + expireByPeer.set(from, now + this.gossipsubIWantFollowupTime) if (this.metrics) { this.metrics.iwantPromiseStarted.inc(1)