Update announced addrs in switch (#784)

This commit is contained in:
Hanno Cornelius 2021-11-30 10:12:09 +01:00 committed by GitHub
parent e226d0f852
commit 7ef51edfa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 1 deletions

View File

@ -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()
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()

View File

@ -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()