Skip to content

Commit

Permalink
chore: update according reviews !
Browse files Browse the repository at this point in the history
  • Loading branch information
darshankabariya committed Jan 3, 2025
1 parent d2b9f38 commit 712a5f5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
10 changes: 5 additions & 5 deletions waku/common/utils/parse_size_units.nim
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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))
17 changes: 10 additions & 7 deletions waku/factory/builder.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions waku/node/peer_manager/peer_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions waku/waku_relay/protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 712a5f5

Please sign in to comment.