Skip to content

Commit

Permalink
chore: backward compitibility
Browse files Browse the repository at this point in the history
  • Loading branch information
darshankabariya committed Jan 20, 2025
1 parent eef0d11 commit 16caad0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
38 changes: 18 additions & 20 deletions waku/common/utils/parse_size_units.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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))
13 changes: 11 additions & 2 deletions waku/factory/node_factory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 16caad0

Please sign in to comment.