Skip to content

Commit

Permalink
chore: adding ext-multiaddr-only CLI flag (#2141)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielmer committed Oct 25, 2023
1 parent 18b5149 commit e2dfc2e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
5 changes: 5 additions & 0 deletions apps/wakunode2/external_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ type
desc: "External multiaddresses to advertise to the network. Argument may be repeated."
name: "ext-multiaddr" }: seq[string]

extMultiAddrsOnly* {.
desc: "Only announce external multiaddresses",
defaultValue: false,
name: "ext-multiaddr-only" }: bool

maxConnections* {.
desc: "Maximum allowed number of libp2p connections."
defaultValue: 50
Expand Down
1 change: 1 addition & 0 deletions apps/wakunode2/internal_config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ proc networkConfiguration*(conf: WakuNodeConf,
extIp = extIp,
extPort = extPort,
extMultiAddrs = extMultiAddrs,
extMultiAddrsOnly = conf.extMultiAddrsOnly,
wsBindPort = Port(uint16(conf.websocketPort) + conf.portsShift),
wsEnabled = conf.websocketSupport,
wssEnabled = conf.websocketSecureSupport,
Expand Down
54 changes: 49 additions & 5 deletions tests/test_waku_netconfig.nim
Original file line number Diff line number Diff line change
Expand Up @@ -338,26 +338,70 @@ suite "Waku NetConfig":
check:
netConfig.enrMultiaddrs.contains(dns4TcpEndPoint(dns4DomainName, extPort))

asyncTest "wsHostAddress is not announced if a WS address is provided in extMultiAddrs":
asyncTest "wsHostAddress is not announced if a WS/WSS address is provided in extMultiAddrs":

let
var
conf = defaultTestWakuNodeConf()
extAddIp = ValidIpAddress.init("1.2.3.4")
extAddPort = Port(1234)
wsEnabled = true
wssEnabled = false
extMultiAddrs = @[(ip4TcpEndPoint(extAddIp, extAddPort) & wsFlag(wssEnabled))]

let netConfigRes = NetConfig.init(
var netConfigRes = NetConfig.init(
bindIp = conf.listenAddress,
bindPort = conf.tcpPort,
extMultiAddrs = extMultiAddrs
extMultiAddrs = extMultiAddrs,
wsEnabled = wsEnabled
)

assert netConfigRes.isOk(), $netConfigRes.error

let netConfig = netConfigRes.get()
var netConfig = netConfigRes.get()

check:
netConfig.announcedAddresses.len == 2 # Bind address + extAddress
netConfig.announcedAddresses[1] == extMultiAddrs[0]

# Now same test for WSS external address
wssEnabled = true
extMultiAddrs = @[(ip4TcpEndPoint(extAddIp, extAddPort) & wsFlag(wssEnabled))]

netConfigRes = NetConfig.init(
bindIp = conf.listenAddress,
bindPort = conf.tcpPort,
extMultiAddrs = extMultiAddrs,
wssEnabled = wssEnabled
)

assert netConfigRes.isOk(), $netConfigRes.error

netConfig = netConfigRes.get()

check:
netConfig.announcedAddresses.len == 2 # Bind address + extAddress
netConfig.announcedAddresses[1] == extMultiAddrs[0]

asyncTest "Only extMultiAddrs are published when enabling extMultiAddrsOnly flag":

let
conf = defaultTestWakuNodeConf()
extAddIp = ValidIpAddress.init("1.2.3.4")
extAddPort = Port(1234)
extMultiAddrs = @[ip4TcpEndPoint(extAddIp, extAddPort)]

let netConfigRes = NetConfig.init(
bindIp = conf.listenAddress,
bindPort = conf.tcpPort,
extMultiAddrs = extMultiAddrs,
extMultiAddrsOnly = true
)

assert netConfigRes.isOk(), $netConfigRes.error

let netConfig = netConfigRes.get()

check:
netConfig.announcedAddresses.len == 1 # ExtAddress
netConfig.announcedAddresses[0] == extMultiAddrs[0]

24 changes: 13 additions & 11 deletions waku/node/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ proc init*(T: type NetConfig,
extIp = none(ValidIpAddress),
extPort = none(Port),
extMultiAddrs = newSeq[MultiAddress](),
extMultiAddrsOnly: bool = false,
wsBindPort: Port = Port(8000),
wsEnabled: bool = false,
wssEnabled: bool = false,
Expand Down Expand Up @@ -125,21 +126,22 @@ proc init*(T: type NetConfig,

var announcedAddresses = newSeq[MultiAddress]()

if hostExtAddress.isSome():
announcedAddresses.add(hostExtAddress.get())
else:
announcedAddresses.add(formatListenAddress(hostAddress)) # We always have at least a bind address for the host

if not extMultiAddrsOnly:
if hostExtAddress.isSome():
announcedAddresses.add(hostExtAddress.get())
else:
announcedAddresses.add(formatListenAddress(hostAddress)) # We always have at least a bind address for the host

if wsExtAddress.isSome():
announcedAddresses.add(wsExtAddress.get())
elif wsHostAddress.isSome() and not containsWsAddress(extMultiAddrs):
# Only publish wsHostAddress if a WS address is not set in extMultiAddrs
announcedAddresses.add(wsHostAddress.get())

# External multiaddrs that the operator may have configured
if extMultiAddrs.len > 0:
announcedAddresses.add(extMultiAddrs)

if wsExtAddress.isSome():
announcedAddresses.add(wsExtAddress.get())
elif wsHostAddress.isSome() and not containsWsAddress(extMultiAddrs):
# Only publish wsHostAddress if a WS address is not set in extMultiAddrs
announcedAddresses.add(wsHostAddress.get())

let
# enrMultiaddrs are just addresses which cannot be represented in ENR, as described in
# https://rfc.vac.dev/spec/31/#many-connection-types
Expand Down

0 comments on commit e2dfc2e

Please sign in to comment.