diff --git a/tests/v2/test_wakunode.nim b/tests/v2/test_wakunode.nim index ee24904f0..09b0a4ef8 100644 --- a/tests/v2/test_wakunode.nim +++ b/tests/v2/test_wakunode.nim @@ -1388,4 +1388,38 @@ asyncTest "Messages are relayed between nodes with multiple transports (websocke check: (await completionFut.withTimeout(5.seconds)) == true await node1.stop() - await node2.stop() \ No newline at end of file + await node2.stop() + +asyncTest "Peer info updates with correct announced addresses": + let + nodeKey = crypto.PrivateKey.random(Secp256k1, rng[])[] + bindIp = ValidIpAddress.init("0.0.0.0") + bindPort = Port(60000) + extIp = some(ValidIpAddress.init("127.0.0.1")) + extPort = some(Port(60002)) + node = WakuNode.new( + nodeKey, + bindIp, bindPort, + extIp, extPort) + + let + bindEndpoint = MultiAddress.init(bindIp, tcpProtocol, bindPort) + announcedEndpoint = MultiAddress.init(extIp.get(), tcpProtocol, extPort.get()) + + check: + # Check that underlying peer info contains only bindIp before starting + node.switch.peerInfo.addrs.len == 1 + node.switch.peerInfo.addrs.contains(bindEndpoint) + + node.announcedAddresses.len == 1 + node.announcedAddresses.contains(announcedEndpoint) + + await node.start() + + check: + # Check that underlying peer info is updated with announced address + node.started + node.switch.peerInfo.addrs.len == 1 + node.switch.peerInfo.addrs.contains(announcedEndpoint) + + await node.stop() diff --git a/waku/v2/node/wakunode2.nim b/waku/v2/node/wakunode2.nim index c2a11f479..667778e63 100644 --- a/waku/v2/node/wakunode2.nim +++ b/waku/v2/node/wakunode2.nim @@ -125,6 +125,22 @@ proc removeContentFilters(filters: var Filters, contentFilters: seq[ContentFilte debug "filters modified", filters=filters +proc updateSwitchPeerInfo(node: WakuNode) = + ## TODO: remove this when supported upstream + ## + ## nim-libp2p does not yet support announcing addrs + ## different from bound addrs. + ## + ## This is a temporary workaround to replace + ## peer info addrs in switch to announced + ## addresses. + ## + ## WARNING: this should only be called once the switch + ## has already been started. + + if node.announcedAddresses.len > 0: + node.switch.peerInfo.addrs = node.announcedAddresses + template tcpEndPoint(address, port): auto = MultiAddress.init(address, tcpProtocol, port) @@ -839,6 +855,9 @@ proc start*(node: WakuNode) {.async.} = info "Listening on", full = listenStr info "Discoverable ENR ", enr = node.enr.toURI() + ## Update switch peer info with announced addrs + node.updateSwitchPeerInfo() + if not node.wakuRelay.isNil: await node.startRelay()