From 16caad0947303d6771784790be17cea0fbb9d69d Mon Sep 17 00:00:00 2001 From: darshankabariya Date: Mon, 20 Jan 2025 21:23:24 +0530 Subject: [PATCH] chore: backward compitibility --- waku/common/utils/parse_size_units.nim | 38 ++++++++++++-------------- waku/factory/node_factory.nim | 13 +++++++-- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/waku/common/utils/parse_size_units.nim b/waku/common/utils/parse_size_units.nim index ec657e1957..14f41a9334 100644 --- a/waku/common/utils/parse_size_units.nim +++ b/waku/common/utils/parse_size_units.nim @@ -53,24 +53,22 @@ proc parseCorrectMsgSize*(input: string): uint64 = proc parseRelayServiceRatio*(ratio: string): Result[(float, float), string] = ## Parses a relay/service ratio string to [ float, float ]. The total should sum 100% ## e.g., (0.4, 0.6) == parseRelayServiceRatio("40:60") + let elements = ratio.split(":") + if elements.len != 2: + return err("expected format 'X:Y', ratio = " & ratio) + + var relayRatio, serviceRatio: float try: - let elements = ratio.split(":") - if elements.len != 2: - raise newException(ValueError, "expected format 'X:Y', ratio = " & ratio) - - let - relayRatio = parseFloat(elements[0]) - serviceRatio = parseFloat(elements[1]) - - if relayRatio < 0 or serviceRatio < 0: - raise newException( - ValueError, "Relay service ratio must be non-negative, ratio = " & ratio - ) - - let total = relayRatio + serviceRatio - if int(total) != 100: - raise newException(ValueError, "Total ratio should be 100, total = " & $total) - - return ok((relayRatio / 100.0, serviceRatio / 100.0)) - except ValueError as e: - raise newException(ValueError, "Invalid Format Error : " & e.msg) + relayRatio = parseFloat(elements[0]) + serviceRatio = parseFloat(elements[1]) + except ValueError: + return err("failed to parse ratio numbers: " & ratio) + + if relayRatio < 0 or serviceRatio < 0: + return err("relay service ratio must be non-negative, ratio = " & ratio) + + let total = relayRatio + serviceRatio + if int(total) != 100: + return err("total ratio should be 100, total = " & $total) + + ok((relayRatio / 100.0, serviceRatio / 100.0)) diff --git a/waku/factory/node_factory.nim b/waku/factory/node_factory.nim index 8b1eb7952f..668bf73684 100644 --- a/waku/factory/node_factory.nim +++ b/waku/factory/node_factory.nim @@ -102,9 +102,18 @@ proc initNode( ) builder.withColocationLimit(conf.colocationLimit) - # Backword compatibility for maxRelayPeers if conf.maxRelayPeers.isSome(): - error "maxRelayPeers is deprecated, using relayServiceRatio instead ( recommendation ), if relayServiceRatio is not set, by default it will be 60:40, maxRelayPeers and maxServicePeer is calculated accordingly" + let + maxRelayPeers = conf.maxRelayPeers.get() + maxConnections = conf.maxConnections + # Calculate the ratio as percentages + relayRatio = (maxRelayPeers.float / maxConnections.float) * 100 + serviceRatio = 100 - relayRatio + + # Override the relayServiceRatio in config + conf.relayServiceRatio = $relayRatio & ":" & $serviceRatio + + error "maxRelayPeers is deprecated. It is recommended to use relayServiceRatio instead. If relayServiceRatio is not set, it will be automatically calculated based on maxConnections and maxRelayPeers." builder.withPeerManagerConfig( maxConnections = conf.maxConnections,