From 712a5f5f41b302bbf6f3ab1d80f9ef8ac38e0ccd Mon Sep 17 00:00:00 2001 From: DarshanBPatel Date: Fri, 3 Jan 2025 17:23:30 +0530 Subject: [PATCH] chore: update according reviews ! --- waku/common/utils/parse_size_units.nim | 10 +++++----- waku/factory/builder.nim | 17 ++++++++++------- waku/node/peer_manager/peer_manager.nim | 8 ++++++-- waku/waku_relay/protocol.nim | 3 +++ 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/waku/common/utils/parse_size_units.nim b/waku/common/utils/parse_size_units.nim index 3378c2c721..1812f1bea6 100644 --- a/waku/common/utils/parse_size_units.nim +++ b/waku/common/utils/parse_size_units.nim @@ -1,4 +1,4 @@ -import std/strutils, results, regex +import std/[strutils, math], results, regex proc parseMsgSize*(input: string): Result[uint64, string] = ## Parses size strings such as "1.2 KiB" or "3Kb" and returns the equivalent number of bytes @@ -54,17 +54,17 @@ proc parseCorrectMsgSize*(input: string): uint64 = proc parseRelayServiceRatio*(ratio: string): Result[(float, float), string] = let elements = ratio.split(":") if elements.len != 2: - return err("Invalid format of relay:service, ratio = " & ratio) + return err("Invalid format of relay:service, ratio = " & $ratio) let relayRatio = parseFloat(elements[0]) serviceRatio = parseFloat(elements[1]) if relayRatio < 0 or serviceRatio < 0: - return err("relay service ratio must be non-negative, ratio = " & ratio) + return err("relay service ratio must be non-negative, ratio = " & $ratio) let total = relayRatio + serviceRatio if floor(total) > 100: - return err("Total ratio cannot be greater than 100, total =" & total) + return err("Total ratio cannot be greater than 100, total =" & $total) - return ok(relayRatio / 100.0, serviceRatio / 100.0) + return ok((relayRatio / 100.0, serviceRatio / 100.0)) diff --git a/waku/factory/builder.nim b/waku/factory/builder.nim index 01b5dd9d2d..c6cbf29e95 100644 --- a/waku/factory/builder.nim +++ b/waku/factory/builder.nim @@ -115,13 +115,16 @@ proc withPeerManagerConfig*( relayServiceRatio: string, shardAware = false, ) = - let (relayRatio, serviceRatio) = parseRelayServiceRatio(relayServiceRatio).valueOr: - error "Invalid relay service ratio", ratio = relayServiceRatio, error = error - return - - builder.maxServicePeers = int(floor(float(maxConnections) * serviceRatio)) - builder.maxRelayPeers = int(ceil(float(maxConnections) * relayRatio)) - builder.shardAware = shardAware + try: + let (relayRatio, serviceRatio) = parseRelayServiceRatio(relayServiceRatio).get() + var relayPeers = int(ceil(float(maxConnections) * relayRatio)) + var servicePeers = int(floor(float(maxConnections) * serviceRatio)) + + builder.maxServicePeers = servicePeers + builder.maxRelayPeers = relayPeers + builder.shardAware = shardAware + except ValueError: + error "Invalid relay service ratio format", ratio = relayServiceRatio proc withColocationLimit*(builder: var WakuNodeBuilder, colocationLimit: int) = builder.colocationLimit = colocationLimit diff --git a/waku/node/peer_manager/peer_manager.nim b/waku/node/peer_manager/peer_manager.nim index 8a9689e09e..077657a2f2 100644 --- a/waku/node/peer_manager/peer_manager.nim +++ b/waku/node/peer_manager/peer_manager.nim @@ -987,8 +987,12 @@ proc new*( Defect, "Max number of connections can't be greater than PeerManager capacity" ) - let (relayRatio, serviceRatio) = parseRelayServiceRatio(relayServiceRatio).valueOr: - error "Invalid relay service ratio", ratio = relayServiceRatio, error = error + var relayRatio: float64 + var serviceRatio: float64 + try: + (relayRatio, serviceRatio) = parseRelayServiceRatio(relayServiceRatio).get() + except ValueError: + error "Invalid relay service ratio format", ratio = relayServiceRatio return var relayPeers = int(ceil(float(maxConnections) * relayRatio)) diff --git a/waku/waku_relay/protocol.nim b/waku/waku_relay/protocol.nim index 1d79e73366..080f12edfb 100644 --- a/waku/waku_relay/protocol.nim +++ b/waku/waku_relay/protocol.nim @@ -314,6 +314,9 @@ proc addObserver*(w: WakuRelay, observer: PubSubObserver) {.gcsafe.} = ## Observes when a message is sent/received from the GossipSub PoV procCall GossipSub(w).addObserver(observer) +proc getDHigh*(T: type WakuRelay): int = + return GossipsubParameters.dHigh + proc getNumPeersInMesh*(w: WakuRelay, pubsubTopic: PubsubTopic): Result[int, string] = ## Returns the number of peers in a mesh defined by the passed pubsub topic. ## The 'mesh' atribute is defined in the GossipSub ref object.