Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add safe default values for peer-store-capacity #1525

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions apps/wakunode2/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ type

peerStoreCapacity* {.
desc: "Maximum stored peers in the peerstore."
defaultValue: 100
name: "peer-store-capacity" }: int
name: "peer-store-capacity" }: Option[int]

peerPersistence* {.
desc: "Enable peer persistence.",
Expand Down Expand Up @@ -494,6 +493,11 @@ proc parseCmdArg*(T: type Port, p: string): T =
proc completeCmdArg*(T: type Port, val: string): seq[string] =
return @[]

proc parseCmdArg*(T: type Option[int], p: string): T =
try:
some(parseInt(p))
except:
raise newException(ConfigurationError, "Invalid number")

## Configuration validation

Expand Down
2 changes: 1 addition & 1 deletion apps/wakunode2/wakunode2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ proc initNode(conf: WakuNodeConf,
dns4DomainName,
discv5UdpPort,
some(conf.agentString),
some(conf.peerStoreCapacity))
conf.peerStoreCapacity)
except:
return err("failed to create waku node instance: " & getCurrentExceptionMsg())

Expand Down
2 changes: 1 addition & 1 deletion waku/v2/node/waku_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ proc new*(T: type WakuNode,
dns4DomainName = none(string),
discv5UdpPort = none(Port),
agentString = none(string), # defaults to nim-libp2p version
peerStoreCapacity = none(int), # defaults to nim-libp2p max size
peerStoreCapacity = none(int), # defaults to 1.25 maxConnections
): T {.raises: [Defect, LPError, IOError, TLSStreamProtocolError].} =
## Creates a Waku Node instance.

Expand Down
7 changes: 5 additions & 2 deletions waku/v2/node/wakuswitch.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ else:
{.push raises: [].}

import
std/options,
std/[options, math],
chronos, chronicles,
eth/keys,
libp2p/crypto/crypto,
Expand Down Expand Up @@ -76,7 +76,7 @@ proc newWakuSwitch*(
secureKeyPath: string = "",
secureCertPath: string = "",
agentString = none(string), # defaults to nim-libp2p version
peerStoreCapacity = none(int), # defaults to nim-libp2p max size
peerStoreCapacity = none(int), # defaults to 1.25 maxConnections
services: seq[switch.Service] = @[],
): Switch
{.raises: [Defect, IOError, LPError].} =
Expand All @@ -98,6 +98,9 @@ proc newWakuSwitch*(

if peerStoreCapacity.isSome():
b = b.withPeerStore(peerStoreCapacity.get())
else:
let defaultPeerStoreCapacity = int(round(float64(maxConnections)*1.25))
b = b.withPeerStore(defaultPeerStoreCapacity)
if agentString.isSome():
b = b.withAgentVersion(agentString.get())
if privKey.isSome():
Expand Down