disc: updateExternalIp() (#495)

* disc: updateExternalIp()

New public proc that can be used to inform the discovery subsystem about
a changed external IP (as reported by UPnP/NAT-PMP in some other module).
This commit is contained in:
Ștefan Talpalaru 2022-03-30 12:52:45 +02:00 committed by GitHub
parent c28597fee5
commit 5a78e2fe23
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 8 deletions

View File

@ -842,6 +842,22 @@ proc refreshLoop(d: Protocol) {.async.} =
except CancelledError: except CancelledError:
trace "refreshLoop canceled" trace "refreshLoop canceled"
proc updateExternalIp*(d: Protocol, extIp: ValidIpAddress, udpPort: Port): bool =
var success = false
let
previous = d.localNode.address
res = d.localNode.update(d.privateKey,
ip = some(extIp), udpPort = some(udpPort))
if res.isErr:
warn "Failed updating ENR with newly discovered external address",
previous, newExtIp = extIp, newUdpPort = udpPort, error = res.error
else:
success = true
info "Updated ENR with newly discovered external address",
previous, newExtIp = extIp, newUdpPort = udpPort, uri = toURI(d.localNode.record)
return success
proc ipMajorityLoop(d: Protocol) {.async.} = proc ipMajorityLoop(d: Protocol) {.async.} =
## When `enrAutoUpdate` is enabled, the IP:port combination returned ## When `enrAutoUpdate` is enabled, the IP:port combination returned
## by the majority will be used to update the local ENR. ## by the majority will be used to update the local ENR.
@ -869,15 +885,9 @@ proc ipMajorityLoop(d: Protocol) {.async.} =
let address = majority.get() let address = majority.get()
let previous = d.localNode.address let previous = d.localNode.address
if d.enrAutoUpdate: if d.enrAutoUpdate:
let res = d.localNode.update(d.privateKey, let success = d.updateExternalIp(address.ip, address.port)
ip = some(address.ip), udpPort = some(address.port)) if success:
if res.isErr:
warn "Failed updating ENR with newly discovered external address",
majority, previous, error = res.error
else:
discovery_enr_auto_update.inc() discovery_enr_auto_update.inc()
info "Updated ENR with newly discovered external address",
majority, previous, uri = toURI(d.localNode.record)
else: else:
warn "Discovered new external address but ENR auto update is off", warn "Discovered new external address but ENR auto update is off",
majority, previous majority, previous