From 8b9c8a692ec90b1901749578d6e58392c34ae8d9 Mon Sep 17 00:00:00 2001 From: kdeme Date: Tue, 31 Mar 2020 12:02:13 +0200 Subject: [PATCH] Discv5: Don't pass ip address when external ip is not known --- beacon_chain/beacon_node.nim | 9 +++------ beacon_chain/eth2_discovery.nim | 5 +++-- beacon_chain/eth2_network.nim | 34 ++++++++++++++------------------- vendor/nim-eth | 2 +- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index d8bdfb433..c1f1b46c9 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -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 diff --git a/beacon_chain/eth2_discovery.nim b/beacon_chain/eth2_discovery.nim index 427eb1f3a..bbdc281cf 100644 --- a/beacon_chain/eth2_discovery.nim +++ b/beacon_chain/eth2_discovery.nim @@ -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) diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index 65f50c0d4..2425c0bdb 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -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" diff --git a/vendor/nim-eth b/vendor/nim-eth index c3f23e591..fe6df94a1 160000 --- a/vendor/nim-eth +++ b/vendor/nim-eth @@ -1 +1 @@ -Subproject commit c3f23e5912efff98fc6c8181db579037e5a19a2c +Subproject commit fe6df94a1956509e77ff533d9d00dd35b403ea22