improvement: create a new gossipsub constructor (#1078)

This commit is contained in:
diegomrsantos 2024-03-27 11:54:15 +01:00 committed by GitHub
parent 1a707e1264
commit bb97a9de79
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 74 additions and 34 deletions

View File

@ -49,42 +49,80 @@ declareCounter(libp2p_gossipsub_received, "number of messages received (deduplic
when defined(libp2p_expensive_metrics): when defined(libp2p_expensive_metrics):
declareCounter(libp2p_pubsub_received_messages, "number of messages received", labels = ["id", "topic"]) declareCounter(libp2p_pubsub_received_messages, "number of messages received", labels = ["id", "topic"])
proc init*(_: type[GossipSubParams]): GossipSubParams = proc init*(
_: type[GossipSubParams],
explicit = true,
pruneBackoff = 1.minutes,
unsubscribeBackoff = 5.seconds,
floodPublish = true,
gossipFactor: float64 = 0.25,
d = GossipSubD,
dLow = GossipSubDlo,
dHigh = GossipSubDhi,
dScore = GossipSubDlo,
dOut = GossipSubDlo - 1, # DLow - 1
dLazy = GossipSubD, # Like D,
heartbeatInterval = GossipSubHeartbeatInterval,
historyLength = GossipSubHistoryLength,
historyGossip = GossipSubHistoryGossip,
fanoutTTL = GossipSubFanoutTTL,
seenTTL = 2.minutes,
gossipThreshold = -100.0,
publishThreshold = -1000.0,
graylistThreshold = -10000.0,
opportunisticGraftThreshold = 0.0,
decayInterval = 1.seconds,
decayToZero = 0.01,
retainScore = 2.minutes,
appSpecificWeight = 0.0,
ipColocationFactorWeight = 0.0,
ipColocationFactorThreshold = 1.0,
behaviourPenaltyWeight = -1.0,
behaviourPenaltyDecay = 0.999,
directPeers = initTable[PeerId, seq[MultiAddress]](),
disconnectBadPeers = false,
enablePX = false,
bandwidthEstimatebps = 100_000_000, # 100 Mbps or 12.5 MBps
overheadRateLimit = Opt.none(tuple[bytes: int, interval: Duration]),
disconnectPeerAboveRateLimit = false,
maxNumElementsInNonPriorityQueue = DefaultMaxNumElementsInNonPriorityQueue): GossipSubParams =
GossipSubParams( GossipSubParams(
explicit: true, explicit: true,
pruneBackoff: 1.minutes, pruneBackoff: pruneBackoff,
unsubscribeBackoff: 5.seconds, unsubscribeBackoff: unsubscribeBackoff,
floodPublish: true, floodPublish: floodPublish,
gossipFactor: 0.25, gossipFactor: gossipFactor,
d: GossipSubD, d: d,
dLow: GossipSubDlo, dLow: dLow,
dHigh: GossipSubDhi, dHigh: dHigh,
dScore: GossipSubDlo, dScore: dScore,
dOut: GossipSubDlo - 1, # DLow - 1 dOut: dOut,
dLazy: GossipSubD, # Like D dLazy: dLazy,
heartbeatInterval: GossipSubHeartbeatInterval, heartbeatInterval: heartbeatInterval,
historyLength: GossipSubHistoryLength, historyLength: historyLength,
historyGossip: GossipSubHistoryGossip, historyGossip: historyGossip,
fanoutTTL: GossipSubFanoutTTL, fanoutTTL: fanoutTTL,
seenTTL: 2.minutes, seenTTL: seenTTL,
gossipThreshold: -100, gossipThreshold: gossipThreshold,
publishThreshold: -1000, publishThreshold: publishThreshold,
graylistThreshold: -10000, graylistThreshold: graylistThreshold,
opportunisticGraftThreshold: 0, opportunisticGraftThreshold: opportunisticGraftThreshold,
decayInterval: 1.seconds, decayInterval: decayInterval,
decayToZero: 0.01, decayToZero: decayToZero,
retainScore: 2.minutes, retainScore: retainScore,
appSpecificWeight: 0.0, appSpecificWeight: appSpecificWeight,
ipColocationFactorWeight: 0.0, ipColocationFactorWeight: ipColocationFactorWeight,
ipColocationFactorThreshold: 1.0, ipColocationFactorThreshold: ipColocationFactorThreshold,
behaviourPenaltyWeight: -1.0, behaviourPenaltyWeight: behaviourPenaltyWeight,
behaviourPenaltyDecay: 0.999, behaviourPenaltyDecay: behaviourPenaltyDecay,
disconnectBadPeers: false, directPeers: directPeers,
enablePX: false, disconnectBadPeers: disconnectBadPeers,
bandwidthEstimatebps: 100_000_000, # 100 Mbps or 12.5 MBps enablePX: enablePX,
overheadRateLimit: Opt.none(tuple[bytes: int, interval: Duration]), bandwidthEstimatebps: bandwidthEstimatebps,
disconnectPeerAboveRateLimit: false, overheadRateLimit: overheadRateLimit,
maxNumElementsInNonPriorityQueue: DefaultMaxNumElementsInNonPriorityQueue disconnectPeerAboveRateLimit: disconnectPeerAboveRateLimit,
maxNumElementsInNonPriorityQueue: maxNumElementsInNonPriorityQueue
) )
proc validateParameters*(parameters: GossipSubParams): Result[void, cstring] = proc validateParameters*(parameters: GossipSubParams): Result[void, cstring] =
@ -115,6 +153,8 @@ proc validateParameters*(parameters: GossipSubParams): Result[void, cstring] =
err("gossipsub: behaviourPenaltyWeight parameter error, Must be negative") err("gossipsub: behaviourPenaltyWeight parameter error, Must be negative")
elif parameters.behaviourPenaltyDecay < 0 or parameters.behaviourPenaltyDecay >= 1: elif parameters.behaviourPenaltyDecay < 0 or parameters.behaviourPenaltyDecay >= 1:
err("gossipsub: behaviourPenaltyDecay parameter error, Must be between 0 and 1") err("gossipsub: behaviourPenaltyDecay parameter error, Must be between 0 and 1")
elif parameters.maxNumElementsInNonPriorityQueue <= 0:
err("gossipsub: maxNumElementsInNonPriorityQueue parameter error, Must be > 0")
else: else:
ok() ok()