diff --git a/beacon_chain/conf.nim b/beacon_chain/conf.nim index 2306dfd72..cb63536b3 100644 --- a/beacon_chain/conf.nim +++ b/beacon_chain/conf.nim @@ -5,7 +5,7 @@ import chronicles, chronicles/options as chroniclesOptions, confutils, confutils/defs, confutils/std/net, stew/shims/net as stewNet, stew/io2, unicodedb/properties, normalize, - eth/common/eth_types as commonEthTypes, + eth/common/eth_types as commonEthTypes, eth/net/nat, eth/p2p/discoveryv5/enr, json_serialization, web3/[ethtypes, confutils_defs], spec/[crypto, keystore, digest, datatypes, network], @@ -170,7 +170,8 @@ type nat* {. desc: "Specify method to use for determining public address. " & "Must be one of: any, none, upnp, pmp, extip:" - defaultValue: "any" }: string + defaultValue: NatConfig(hasExtIp: false, nat: NatAny) + name: "nat" .}: NatConfig enrAutoUpdate* {. defaultValue: false diff --git a/beacon_chain/networking/eth2_discovery.nim b/beacon_chain/networking/eth2_discovery.nim index 06b27870e..3269c0ae1 100644 --- a/beacon_chain/networking/eth2_discovery.nim +++ b/beacon_chain/networking/eth2_discovery.nim @@ -75,7 +75,7 @@ proc loadBootstrapFile*(bootstrapFile: string, proc new*(T: type Eth2DiscoveryProtocol, config: BeaconNodeConf, - ip: Option[ValidIpAddress], tcpPort, udpPort: Port, + enrIp: Option[ValidIpAddress], enrTcpPort, enrUdpPort: Option[Port], pk: PrivateKey, enrFields: openArray[(string, seq[byte])], rng: ref BrHmacDrbgContext): T {.raises: [Exception, Defect].} = @@ -92,6 +92,6 @@ proc new*(T: type Eth2DiscoveryProtocol, if fileExists(persistentBootstrapFile): loadBootstrapFile(persistentBootstrapFile, bootstrapEnrs) - newProtocol(pk, ip, tcpPort, udpPort, enrFields, bootstrapEnrs, - bindIp = config.listenAddress, enrAutoUpdate = config.enrAutoUpdate, - rng = rng) + newProtocol(pk, enrIp, enrTcpPort, enrUdpPort, enrFields, bootstrapEnrs, + bindPort = config.udpPort, bindIp = config.listenAddress, + enrAutoUpdate = config.enrAutoUpdate, rng = rng) diff --git a/beacon_chain/networking/eth2_network.nim b/beacon_chain/networking/eth2_network.nim index 0a59c0058..d505cfd06 100644 --- a/beacon_chain/networking/eth2_network.nim +++ b/beacon_chain/networking/eth2_network.nim @@ -1062,7 +1062,7 @@ proc onConnEvent(node: Eth2Node, peerId: PeerID, event: ConnEvent) {.async.} = proc new*(T: type Eth2Node, config: BeaconNodeConf, enrForkId: ENRForkID, switch: Switch, pubsub: GossipSub, ip: Option[ValidIpAddress], - tcpPort, udpPort: Port, privKey: keys.PrivateKey, discovery: bool, + tcpPort, udpPort: Option[Port], privKey: keys.PrivateKey, discovery: bool, rng: ref BrHmacDrbgContext): T = let metadata = getPersistentNetMetadata(config) @@ -1315,49 +1315,6 @@ proc p2pProtocolBackendImpl*(p: P2PProtocol): Backend = result.implementProtocolInit = proc (p: P2PProtocol): NimNode = return newCall(initProtocol, newLit(p.name), p.peerInit, p.netInit) -proc setupNat(config: BeaconNodeConf): tuple[ip: Option[ValidIpAddress], - tcpPort: Port, - udpPort: Port] = - # defaults - result.tcpPort = config.tcpPort - result.udpPort = config.udpPort - - var nat: NatStrategy - case config.nat.toLowerAscii: - of "any": - nat = NatAny - of "none": - nat = NatNone - of "upnp": - nat = NatUpnp - of "pmp": - nat = NatPmp - else: - if config.nat.startsWith("extip:"): - try: - # any required port redirection is assumed to be done by hand - result.ip = some(ValidIpAddress.init(config.nat[6..^1])) - nat = NatNone - except ValueError: - error "nor a valid IP address", address = config.nat[6..^1] - quit QuitFailure - else: - error "not a valid NAT mechanism", value = config.nat - quit QuitFailure - - if nat != NatNone: - let extIp = getExternalIP(nat) - if extIP.isSome: - result.ip = some(ValidIpAddress.init extIp.get) - # TODO redirectPorts in considered a gcsafety violation - # because it obtains the address of a non-gcsafe proc? - let extPorts = ({.gcsafe.}: - redirectPorts(tcpPort = result.tcpPort, - udpPort = result.udpPort, - description = clientId)) - if extPorts.isSome: - (result.tcpPort, result.udpPort) = extPorts.get() - func asLibp2pKey*(key: keys.PublicKey): PublicKey = PublicKey(scheme: Secp256k1, skkey: secp.SkPublicKey(key)) @@ -1540,10 +1497,11 @@ proc createEth2Node*(rng: ref BrHmacDrbgContext, netKeys: KeyPair, enrForkId: ENRForkID): Eth2Node = var - (extIp, extTcpPort, extUdpPort) = setupNat(config) + (extIp, extTcpPort, extUdpPort) = setupAddress(config.nat, + config.listenAddress, config.tcpPort, config.udpPort, clientId) hostAddress = tcpEndPoint(config.listenAddress, config.tcpPort) - announcedAddresses = if extIp.isNone(): @[] - else: @[tcpEndPoint(extIp.get(), extTcpPort)] + announcedAddresses = if extIp.isNone() or extTcpPort.isNone(): @[] + else: @[tcpEndPoint(extIp.get(), extTcpPort.get())] debug "Initializing networking", hostAddress, network_public_key = netKeys.pubkey, diff --git a/beacon_chain/nimbus_beacon_node.nim b/beacon_chain/nimbus_beacon_node.nim index 3266192cf..dda6385f4 100644 --- a/beacon_chain/nimbus_beacon_node.nim +++ b/beacon_chain/nimbus_beacon_node.nim @@ -17,7 +17,7 @@ import chronicles, bearssl, blscurve, json_serialization/std/[options, sets, net], serialization/errors, - eth/[keys, async_utils], + eth/[keys, async_utils], eth/net/nat, eth/db/[kvstore, kvstore_sqlite3], eth/p2p/enode, eth/p2p/discoveryv5/[protocol, enr, random2], diff --git a/ncli/inspector.nim b/ncli/inspector.nim index bc1dd82e9..9b6686858 100644 --- a/ncli/inspector.nim +++ b/ncli/inspector.nim @@ -412,9 +412,11 @@ proc bootstrapDiscovery(conf: InspectorConf, if enrFields.isSome(): let fields = enrFields.get() let pairs = {"eth2": fields.eth2, "attnets": fields.attnets} - result = newProtocol(pk, host, tcpPort, udpPort, pairs, bootnodes) + result = newProtocol(pk, host, some(tcpPort), some(udpPort), pairs, + bootnodes, bindPort = udpPort) else: - result = newProtocol(pk, host, tcpPort, udpPort, [], bootnodes) + result = newProtocol(pk, host, some(tcpPort), some(udpPort), [], + bootnodes, bindPort = udpPort) result.open() result.start() diff --git a/vendor/nim-chronos b/vendor/nim-chronos index 25688cd0a..03707426e 160000 --- a/vendor/nim-chronos +++ b/vendor/nim-chronos @@ -1 +1 @@ -Subproject commit 25688cd0aa51f5e28c1e55dca1b3a9a73cb3dafa +Subproject commit 03707426e43d03cccc1de2e7284de168b79f7bf6 diff --git a/vendor/nim-eth b/vendor/nim-eth index e8c9691b3..be5e088b2 160000 --- a/vendor/nim-eth +++ b/vendor/nim-eth @@ -1 +1 @@ -Subproject commit e8c9691b35f4875d9a97193cb965fbf1a956dd7e +Subproject commit be5e088b21e06a85cac4826454412db8459ed4f1