From 944dfdaa9914572dd0bc3ed475c3ba0449495146 Mon Sep 17 00:00:00 2001 From: gabrielmer <101006718+gabrielmer@users.noreply.github.com> Date: Tue, 24 Oct 2023 18:39:25 +0300 Subject: [PATCH] chore: adding ext-multiaddr-only CLI flag (#2141) --- apps/wakunode2/external_config.nim | 5 +++ apps/wakunode2/internal_config.nim | 1 + tests/test_waku_netconfig.nim | 56 ++++++++++++++++++++++++++---- waku/node/config.nim | 22 ++++++------ 4 files changed, 68 insertions(+), 16 deletions(-) diff --git a/apps/wakunode2/external_config.nim b/apps/wakunode2/external_config.nim index 0ec5c1fb0..f18c4b159 100644 --- a/apps/wakunode2/external_config.nim +++ b/apps/wakunode2/external_config.nim @@ -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 diff --git a/apps/wakunode2/internal_config.nim b/apps/wakunode2/internal_config.nim index 06fc659bb..0eec05bb4 100644 --- a/apps/wakunode2/internal_config.nim +++ b/apps/wakunode2/internal_config.nim @@ -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, diff --git a/tests/test_waku_netconfig.nim b/tests/test_waku_netconfig.nim index 0c02afab1..713bdcefb 100644 --- a/tests/test_waku_netconfig.nim +++ b/tests/test_waku_netconfig.nim @@ -338,19 +338,63 @@ 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": + + var + conf = defaultTestWakuNodeConf() + extAddIp = ValidIpAddress.init("1.2.3.4") + extAddPort = Port(1234) + wsEnabled = true + wssEnabled = false + extMultiAddrs = @[(ip4TcpEndPoint(extAddIp, extAddPort) & wsFlag(wssEnabled))] + + var netConfigRes = NetConfig.init( + bindIp = conf.listenAddress, + bindPort = conf.tcpPort, + extMultiAddrs = extMultiAddrs, + wsEnabled = wsEnabled + ) + + assert netConfigRes.isOk(), $netConfigRes.error + + 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) - wssEnabled = false - extMultiAddrs = @[(ip4TcpEndPoint(extAddIp, extAddPort) & wsFlag(wssEnabled))] + extMultiAddrs = @[ip4TcpEndPoint(extAddIp, extAddPort)] let netConfigRes = NetConfig.init( bindIp = conf.listenAddress, bindPort = conf.tcpPort, - extMultiAddrs = extMultiAddrs + extMultiAddrs = extMultiAddrs, + extMultiAddrsOnly = true ) assert netConfigRes.isOk(), $netConfigRes.error @@ -358,6 +402,6 @@ suite "Waku NetConfig": let netConfig = netConfigRes.get() check: - netConfig.announcedAddresses.len == 2 # Bind address + extAddress - netConfig.announcedAddresses[1] == extMultiAddrs[0] + netConfig.announcedAddresses.len == 1 # ExtAddress + netConfig.announcedAddresses[0] == extMultiAddrs[0] diff --git a/waku/node/config.nim b/waku/node/config.nim index f90aaff1d..46af0e326 100644 --- a/waku/node/config.nim +++ b/waku/node/config.nim @@ -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, @@ -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