From cb69723ff3695e7b81fb7a95899f6e98d3096f8a Mon Sep 17 00:00:00 2001 From: web3-developer <51288821+web3-developer@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:15:20 +0800 Subject: [PATCH] Fluffy: Make number of nodes to gossip content to configurable (#2653) --- fluffy/conf.nim | 9 +++++++++ fluffy/fluffy.nim | 2 +- fluffy/network/wire/portal_protocol.nim | 9 +++++---- fluffy/network/wire/portal_protocol_config.nim | 6 ++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/fluffy/conf.nim b/fluffy/conf.nim index f2324d403..27b8f62db 100644 --- a/fluffy/conf.nim +++ b/fluffy/conf.nim @@ -45,6 +45,7 @@ const defaultTableIpLimitDesc* = $defaultPortalProtocolConfig.tableIpLimits.tableIpLimit defaultBucketIpLimitDesc* = $defaultPortalProtocolConfig.tableIpLimits.bucketIpLimit defaultBitsPerHopDesc* = $defaultPortalProtocolConfig.bitsPerHop + defaultMaxGossipNodesDesc* = $defaultPortalProtocolConfig.maxGossipNodes type TrustedDigest* = MDigest[32 * 8] @@ -235,6 +236,14 @@ type name: "bits-per-hop" .}: int + maxGossipNodes* {. + hidden, + desc: "The maximum number of nodes to send content to during gossip", + defaultValue: defaultPortalProtocolConfig.maxGossipNodes, + defaultValueDesc: $defaultMaxGossipNodesDesc, + name: "max-gossip-nodes" + .}: int + radiusConfig* {. desc: "Radius configuration for a fluffy node. Radius can be either `dynamic` " & diff --git a/fluffy/fluffy.nim b/fluffy/fluffy.nim index bbca67789..731bb6c00 100644 --- a/fluffy/fluffy.nim +++ b/fluffy/fluffy.nim @@ -159,7 +159,7 @@ proc run( let portalProtocolConfig = PortalProtocolConfig.init( config.tableIpLimit, config.bucketIpLimit, config.bitsPerHop, config.radiusConfig, - config.disablePoke, + config.disablePoke, config.maxGossipNodes, ) portalNodeConfig = PortalNodeConfig( diff --git a/fluffy/network/wire/portal_protocol.nim b/fluffy/network/wire/portal_protocol.nim index ad9b73901..9b3ecd960 100644 --- a/fluffy/network/wire/portal_protocol.nim +++ b/fluffy/network/wire/portal_protocol.nim @@ -182,6 +182,7 @@ type offerWorkers: seq[Future[void]] disablePoke: bool pingTimings: Table[NodeId, chronos.Moment] + maxGossipNodes: int PortalResult*[T] = Result[T, string] @@ -573,6 +574,7 @@ proc new*( offerQueue: newAsyncQueue[OfferRequest](concurrentOffers), disablePoke: config.disablePoke, pingTimings: Table[NodeId, chronos.Moment](), + maxGossipNodes: config.maxGossipNodes, ) proto.baseProtocol.registerTalkProtocol(@(proto.protocolId), proto).expect( @@ -1503,7 +1505,6 @@ proc neighborhoodGossip*( # in its propagation than when looking only for nodes in the own routing # table, but at the same time avoid unnecessary node lookups. # It might still cause issues in data getting propagated in a wider id range. - const maxGossipNodes = 8 let closestLocalNodes = p.routingTable.neighbours(NodeId(contentId), k = 16, seenOnly = true) @@ -1518,9 +1519,9 @@ proc neighborhoodGossip*( elif node.id != srcNodeId.get(): gossipNodes.add(node) - if gossipNodes.len >= 8: # use local nodes for gossip + if gossipNodes.len >= p.maxGossipNodes: # use local nodes for gossip portal_gossip_without_lookup.inc(labelValues = [$p.protocolId]) - let numberOfGossipedNodes = min(gossipNodes.len, maxGossipNodes) + let numberOfGossipedNodes = min(gossipNodes.len, p.maxGossipNodes) for node in gossipNodes[0 ..< numberOfGossipedNodes]: let req = OfferRequest(dst: node, kind: Direct, contentList: contentList) await p.offerQueue.addLast(req) @@ -1528,7 +1529,7 @@ proc neighborhoodGossip*( else: # use looked up nodes for gossip portal_gossip_with_lookup.inc(labelValues = [$p.protocolId]) let closestNodes = await p.lookup(NodeId(contentId)) - let numberOfGossipedNodes = min(closestNodes.len, maxGossipNodes) + let numberOfGossipedNodes = min(closestNodes.len, p.maxGossipNodes) for node in closestNodes[0 ..< numberOfGossipedNodes]: # Note: opportunistically not checking if the radius of the node is known # and thus if the node is in radius with the content. Reason is, these diff --git a/fluffy/network/wire/portal_protocol_config.nim b/fluffy/network/wire/portal_protocol_config.nim index 8c174c452..c78de53b3 100644 --- a/fluffy/network/wire/portal_protocol_config.nim +++ b/fluffy/network/wire/portal_protocol_config.nim @@ -40,17 +40,21 @@ type bitsPerHop*: int radiusConfig*: RadiusConfig disablePoke*: bool + maxGossipNodes*: int const defaultRadiusConfig* = RadiusConfig(kind: Dynamic) defaultRadiusConfigDesc* = $defaultRadiusConfig.kind defaultDisablePoke* = false + defaultMaxGossipNodes = 8 revalidationTimeout* = chronos.seconds(30) defaultPortalProtocolConfig* = PortalProtocolConfig( tableIpLimits: DefaultTableIpLimits, bitsPerHop: DefaultBitsPerHop, radiusConfig: defaultRadiusConfig, + disablePoke: defaultDisablePoke, + maxGossipNodes: defaultMaxGossipNodes, ) proc init*( @@ -60,6 +64,7 @@ proc init*( bitsPerHop: int, radiusConfig: RadiusConfig, disablePoke: bool, + maxGossipNodes: int, ): T = PortalProtocolConfig( tableIpLimits: @@ -67,6 +72,7 @@ proc init*( bitsPerHop: bitsPerHop, radiusConfig: radiusConfig, disablePoke: disablePoke, + maxGossipNodes: maxGossipNodes, ) func fromLogRadius*(T: type UInt256, logRadius: uint16): T =