Discv5: Don't pass ip address when external ip is not known

This commit is contained in:
kdeme 2020-03-31 12:02:13 +02:00
parent daabb1b5b2
commit 8b9c8a692e
No known key found for this signature in database
GPG Key ID: 4E8DD21420AF43F5
4 changed files with 21 additions and 29 deletions

View File

@ -1196,15 +1196,12 @@ when isMainModule:
if bootstrapFile.len > 0:
let
networkKeys = getPersistentNetKeys(config)
bootstrapAddress = enode.Address(
ip: config.bootstrapAddress,
tcpPort: config.bootstrapPort,
udpPort: config.bootstrapPort)
bootstrapEnr = enr.Record.init(
1, # sequence number
networkKeys.seckey.asEthKey,
some(bootstrapAddress))
some(config.bootstrapAddress),
config.bootstrapPort,
config.bootstrapPort)
writeFile(bootstrapFile, bootstrapEnr.toURI)
echo "Wrote ", bootstrapFile

View File

@ -154,7 +154,8 @@ proc loadBootstrapFile*(bootstrapFile: string,
proc new*(T: type Eth2DiscoveryProtocol,
conf: BeaconNodeConf,
ip: IpAddress, rawPrivKeyBytes: openarray[byte]): T =
ip: Option[IpAddress], tcpPort, udpPort: Port,
rawPrivKeyBytes: openarray[byte]): T =
# TODO
# Implement more configuration options:
# * for setting up a specific key
@ -174,4 +175,4 @@ proc new*(T: type Eth2DiscoveryProtocol,
if fileExists(persistentBootstrapFile):
loadBootstrapFile(persistentBootstrapFile, bootNodes, bootEnrs, ourPubKey)
newProtocol(pk, db, ip, conf.tcpPort, conf.udpPort, bootEnrs)
newProtocol(pk, db, ip, tcpPort, udpPort, bootEnrs)

View File

@ -134,9 +134,6 @@ const
readTimeoutErrorMsg = "Exceeded read timeout for a request"
let
globalListeningAddr = parseIpAddress("0.0.0.0")
# Metrics for tracking attestation and beacon block loss
declareCounter gossip_messages_sent,
"Number of gossip messages sent by this peer"
@ -681,10 +678,11 @@ proc runDiscoveryLoop*(node: Eth2Node) {.async.} =
await sleepAsync seconds(1)
proc init*(T: type Eth2Node, conf: BeaconNodeConf,
switch: Switch, ip: IpAddress, privKey: keys.PrivateKey): T =
switch: Switch, ip: Option[IpAddress], tcpPort, udpPort: Port,
privKey: keys.PrivateKey): T =
new result
result.switch = switch
result.discovery = Eth2DiscoveryProtocol.new(conf, ip, privKey.data)
result.discovery = Eth2DiscoveryProtocol.new(conf, ip, tcpPort, udpPort, privKey.data)
result.wantedPeers = conf.maxPeers
result.peerPool = newPeerPool[Peer, PeerID](maxPeers = conf.maxPeers)
@ -829,11 +827,10 @@ proc p2pProtocolBackendImpl*(p: P2PProtocol): Backend =
result.implementProtocolInit = proc (p: P2PProtocol): NimNode =
return newCall(initProtocol, newLit(p.name), p.peerInit, p.netInit)
proc setupNat(conf: BeaconNodeConf): tuple[ip: IpAddress,
proc setupNat(conf: BeaconNodeConf): tuple[ip: Option[IpAddress],
tcpPort: Port,
udpPort: Port] {.gcsafe.} =
# defaults
result.ip = globalListeningAddr
result.tcpPort = conf.tcpPort
result.udpPort = conf.udpPort
@ -850,16 +847,15 @@ proc setupNat(conf: BeaconNodeConf): tuple[ip: IpAddress,
else:
if conf.nat.startsWith("extip:") and isIpAddress(conf.nat[6..^1]):
# any required port redirection is assumed to be done by hand
result.ip = parseIpAddress(conf.nat[6..^1])
result.ip = some(parseIpAddress(conf.nat[6..^1]))
nat = NatNone
else:
error "not a valid NAT mechanism, nor a valid IP address", value = conf.nat
quit(QuitFailure)
if nat != NatNone:
let extIP = getExternalIP(nat)
if extIP.isSome:
result.ip = extIP.get()
result.ip = getExternalIP(nat)
if result.ip.isSome:
# TODO redirectPorts in considered a gcsafety violation
# because it obtains the address of a non-gcsafe proc?
let extPorts = ({.gcsafe.}:
@ -901,10 +897,10 @@ proc getPersistentNetKeys*(conf: BeaconNodeConf): KeyPair =
proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] {.async, gcsafe.} =
var
(extIp, extTcpPort, _) = setupNat(conf)
(extIp, extTcpPort, extUdpPort) = setupNat(conf)
hostAddress = tcpEndPoint(conf.libp2pAddress, conf.tcpPort)
announcedAddresses = if extIp == globalListeningAddr: @[]
else: @[tcpEndPoint(extIp, extTcpPort)]
announcedAddresses = if extIp.isNone(): @[]
else: @[tcpEndPoint(extIp.get(), extTcpPort)]
info "Initializing networking", hostAddress,
announcedAddresses
@ -915,17 +911,15 @@ proc createEth2Node*(conf: BeaconNodeConf): Future[Eth2Node] {.async, gcsafe.} =
# are running behind a NAT).
var switch = newStandardSwitch(some keys.seckey, hostAddress,
triggerSelf = true, gossip = true)
result = Eth2Node.init(conf, switch, extIp, keys.seckey.asEthKey)
result = Eth2Node.init(conf, switch, extIp, extTcpPort, extUdpPort,
keys.seckey.asEthKey)
proc getPersistenBootstrapAddr*(conf: BeaconNodeConf,
ip: IpAddress, port: Port): enr.Record =
let
pair = getPersistentNetKeys(conf)
enodeAddress = Address(ip: ip, udpPort: port)
let pair = getPersistentNetKeys(conf)
return enr.Record.init(1'u64, # sequence number
pair.seckey.asEthKey,
some(enodeAddress))
some(ip), port, port)
proc announcedENR*(node: Eth2Node): enr.Record =
doAssert node.discovery != nil, "The Eth2Node must be initialized"

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit c3f23e5912efff98fc6c8181db579037e5a19a2c
Subproject commit fe6df94a1956509e77ff533d9d00dd35b403ea22