From 0c78fc39e7219a09cd156c9a81cd27073a93b7e6 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Fri, 5 Jun 2020 18:08:50 +0300 Subject: [PATCH] Use the latest LibP2P --- beacon_chain/beacon_node.nim | 2 +- beacon_chain/conf.nim | 20 +++++----- beacon_chain/eth2_discovery.nim | 6 +-- beacon_chain/eth2_network.nim | 44 ++++++++++++---------- beacon_chain/libp2p_json_serialization.nim | 6 ++- scripts/launch_local_testnet.sh | 2 +- tests/simulation/start.sh | 2 +- tests/test_peer_connection.nim | 3 +- vendor/nim-confutils | 2 +- vendor/nim-eth | 2 +- vendor/nim-json-serialization | 2 +- vendor/nim-libp2p | 2 +- vendor/nim-stew | 2 +- 13 files changed, 52 insertions(+), 43 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 163f5628b..996cebaed 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -44,7 +44,7 @@ type # this needs to be global, so it can be set in the Ctrl+C signal handler var status = BeaconNodeStatus.Starting -template init(T: type RpcHttpServer, ip: IpAddress, port: Port): T = +template init(T: type RpcHttpServer, ip: ValidIpAddress, port: Port): T = newRpcHttpServer([initTAddress(ip, port)]) # https://github.com/ethereum/eth2.0-metrics/blob/master/metrics.md#interop-metrics diff --git a/beacon_chain/conf.nim b/beacon_chain/conf.nim index 15ce7ae07..e0156ef4b 100644 --- a/beacon_chain/conf.nim +++ b/beacon_chain/conf.nim @@ -78,7 +78,7 @@ type libp2pAddress* {. defaultValue: defaultListenAddress(config) desc: "Listening address for the Ethereum LibP2P traffic." - name: "listen-address"}: IpAddress + name: "listen-address" }: ValidIpAddress tcpPort* {. defaultValue: defaultEth2TcpPort @@ -140,7 +140,7 @@ type metricsAddress* {. defaultValue: defaultAdminListenAddress(config) desc: "Listening address of the metrics server." - name: "metrics-address" }: IpAddress + name: "metrics-address" }: ValidIpAddress metricsPort* {. defaultValue: 8008 @@ -173,7 +173,7 @@ type rpcAddress* {. defaultValue: defaultAdminListenAddress(config) desc: "Listening address of the RPC server" - name: "rpc-address" }: IpAddress + name: "rpc-address" }: ValidIpAddress dumpEnabled* {. defaultValue: false @@ -201,9 +201,9 @@ type name: "last-user-validator" }: uint64 bootstrapAddress* {. - defaultValue: parseIpAddress("127.0.0.1") + defaultValue: ValidIpAddress.init("127.0.0.1") desc: "The public IP address that will be advertised as a bootstrap node for the testnet." - name: "bootstrap-address" }: IpAddress + name: "bootstrap-address" }: ValidIpAddress bootstrapPort* {. defaultValue: defaultEth2TcpPort @@ -290,7 +290,7 @@ type rpcAddress* {. defaultValue: defaultAdminListenAddress(config) desc: "Address of the server to connect to for RPC." - name: "rpc-address" }: IpAddress + name: "rpc-address" }: ValidIpAddress validators* {. required @@ -330,13 +330,13 @@ func localValidatorsDir*(conf: BeaconNodeConf|ValidatorClientConf): string = func databaseDir*(conf: BeaconNodeConf|ValidatorClientConf): string = conf.dataDir / "db" -func defaultListenAddress*(conf: BeaconNodeConf|ValidatorClientConf): IpAddress = +func defaultListenAddress*(conf: BeaconNodeConf|ValidatorClientConf): ValidIpAddress = # TODO: How should we select between IPv4 and IPv6 # Maybe there should be a config option for this. - return static: parseIpAddress("0.0.0.0") + (static ValidIpAddress.init("0.0.0.0")) -func defaultAdminListenAddress*(conf: BeaconNodeConf|ValidatorClientConf): IpAddress = - return static: parseIpAddress("127.0.0.1") +func defaultAdminListenAddress*(conf: BeaconNodeConf|ValidatorClientConf): ValidIpAddress = + (static ValidIpAddress.init("127.0.0.1")) iterator validatorKeys*(conf: BeaconNodeConf|ValidatorClientConf): ValidatorPrivKey = for validatorKeyFile in conf.validators: diff --git a/beacon_chain/eth2_discovery.nim b/beacon_chain/eth2_discovery.nim index c9d506b43..6bbc06e9a 100644 --- a/beacon_chain/eth2_discovery.nim +++ b/beacon_chain/eth2_discovery.nim @@ -1,8 +1,8 @@ {.push raises: [Defect].} import - os, net, sequtils, strutils, - chronicles, stew/results, eth/keys, eth/trie/db, + os, sequtils, strutils, + chronicles, stew/shims/net, stew/results, eth/keys, eth/trie/db, eth/p2p/discoveryv5/[enr, protocol, discovery_db, node], conf @@ -74,7 +74,7 @@ proc loadBootstrapFile*(bootstrapFile: string, proc new*(T: type Eth2DiscoveryProtocol, conf: BeaconNodeConf, - ip: Option[IpAddress], tcpPort, udpPort: Port, + ip: Option[ValidIpAddress], tcpPort, udpPort: Port, rawPrivKeyBytes: openarray[byte], enrFields: openarray[(string, seq[byte])]): T {.raises: [Exception, Defect].} = diff --git a/beacon_chain/eth2_network.nim b/beacon_chain/eth2_network.nim index a42a97b5d..409cfc5f4 100644 --- a/beacon_chain/eth2_network.nim +++ b/beacon_chain/eth2_network.nim @@ -1,10 +1,11 @@ import # Std lib typetraits, strutils, os, random, algorithm, sequtils, - options as stdOptions, net as stdNet, + options as stdOptions, # Status libs stew/[varints, base58, endians2, results, byteutils], + stew/shims/net as stewNet, stew/shims/[macros, tables], faststreams/[inputs, outputs, buffers], snappy, snappy/framing, json_serialization, json_serialization/std/[net, options], @@ -656,17 +657,15 @@ proc toPeerInfo*(r: enr.TypedRecord): PeerInfo = var addresses = newSeq[MultiAddress]() if r.ip.isSome and r.tcp.isSome: - let ip = IpAddress(family: IpAddressFamily.IPv4, - address_v4: r.ip.get) - addresses.add MultiAddress.init(ip, TCP, Port r.tcp.get) + let ip = ipv4(r.ip.get) + addresses.add MultiAddress.init(ip, tcpProtocol, Port r.tcp.get) if r.ip6.isSome: - let ip = IpAddress(family: IpAddressFamily.IPv6, - address_v6: r.ip6.get) + let ip = ipv6(r.ip6.get) if r.tcp6.isSome: - addresses.add MultiAddress.init(ip, TCP, Port r.tcp6.get) + addresses.add MultiAddress.init(ip, tcpProtocol, Port r.tcp6.get) elif r.tcp.isSome: - addresses.add MultiAddress.init(ip, TCP, Port r.tcp.get) + addresses.add MultiAddress.init(ip, tcpProtocol, Port r.tcp.get) else: discard @@ -781,7 +780,7 @@ proc getPersistentNetMetadata*(conf: BeaconNodeConf): Eth2Metadata = result = Json.loadFile(metadataPath, Eth2Metadata) proc init*(T: type Eth2Node, conf: BeaconNodeConf, enrForkId: ENRForkID, - switch: Switch, ip: Option[IpAddress], tcpPort, udpPort: Port, + switch: Switch, ip: Option[ValidIpAddress], tcpPort, udpPort: Port, privKey: keys.PrivateKey): T = new result result.switch = switch @@ -986,7 +985,7 @@ 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: Option[IpAddress], +proc setupNat(conf: BeaconNodeConf): tuple[ip: Option[ValidIpAddress], tcpPort: Port, udpPort: Port] {.gcsafe.} = # defaults @@ -1004,17 +1003,22 @@ proc setupNat(conf: BeaconNodeConf): tuple[ip: Option[IpAddress], of "pmp": nat = NatPmp 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 = some(parseIpAddress(conf.nat[6..^1])) - nat = NatNone + if conf.nat.startsWith("extip:"): + try: + # any required port redirection is assumed to be done by hand + result.ip = some(ValidIpAddress.init(conf.nat[6..^1])) + nat = NatNone + except ValueError: + error "nor a valid IP address", address = conf.nat[6..^1] + quit QuitFailure else: - error "not a valid NAT mechanism, nor a valid IP address", value = conf.nat - quit(QuitFailure) + error "not a valid NAT mechanism", value = conf.nat + quit QuitFailure if nat != NatNone: - result.ip = getExternalIP(nat) - if result.ip.isSome: + 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.}: @@ -1039,7 +1043,7 @@ proc initAddress*(T: type MultiAddress, str: string): T = "Invalid bootstrap node multi-address") template tcpEndPoint(address, port): auto = - MultiAddress.init(address, Protocol.IPPROTO_TCP, port) + MultiAddress.init(address, tcpProtocol, port) proc getPersistentNetKeys*(conf: BeaconNodeConf): KeyPair = let @@ -1079,7 +1083,7 @@ proc createEth2Node*(conf: BeaconNodeConf, enrForkId: ENRForkID): Future[Eth2Nod keys.seckey.asEthKey) proc getPersistenBootstrapAddr*(conf: BeaconNodeConf, - ip: IpAddress, port: Port): EnrResult[enr.Record] = + ip: ValidIpAddress, port: Port): EnrResult[enr.Record] = let pair = getPersistentNetKeys(conf) return enr.Record.init(1'u64, # sequence number pair.seckey.asEthKey, diff --git a/beacon_chain/libp2p_json_serialization.nim b/beacon_chain/libp2p_json_serialization.nim index 181f84f6f..11477728e 100644 --- a/beacon_chain/libp2p_json_serialization.nim +++ b/beacon_chain/libp2p_json_serialization.nim @@ -11,5 +11,9 @@ proc writeValue*(writer: var JsonWriter, value: MultiAddress) {.inline.} = writer.writeValue $value proc readValue*(reader: var JsonReader, value: var MultiAddress) {.inline.} = - value = MultiAddress.init reader.readValue(string) + let addressRes = MultiAddress.init reader.readValue(string) + if addressRes.isOk: + value = addressRes.value + else: + raiseUnexpectedValue(reader, "Invalid MultiAddress value") diff --git a/scripts/launch_local_testnet.sh b/scripts/launch_local_testnet.sh index 0e934581a..27123126a 100755 --- a/scripts/launch_local_testnet.sh +++ b/scripts/launch_local_testnet.sh @@ -155,7 +155,7 @@ BOOTSTRAP_IP="127.0.0.1" --output-bootstrap-file="${NETWORK_DIR}/bootstrap_nodes.txt" \ --bootstrap-address=${BOOTSTRAP_IP} \ --bootstrap-port=${BASE_PORT} \ - --genesis-offset=5 # Delay in seconds + --genesis-offset=30 # Delay in seconds if [[ "$ENABLE_GRAFANA" == "1" ]]; then # Prometheus config diff --git a/tests/simulation/start.sh b/tests/simulation/start.sh index a6d48798c..0aa09e0f3 100755 --- a/tests/simulation/start.sh +++ b/tests/simulation/start.sh @@ -153,7 +153,7 @@ if [ ! -f "${SNAPSHOT_FILE}" ]; then --output-bootstrap-file="${NETWORK_BOOTSTRAP_FILE}" \ --bootstrap-address=127.0.0.1 \ --bootstrap-port=$(( BASE_P2P_PORT + MASTER_NODE )) \ - --genesis-offset=5 # Delay in seconds + --genesis-offset=15 # Delay in seconds fi fi diff --git a/tests/test_peer_connection.nim b/tests/test_peer_connection.nim index fc1ff5cdc..ea35582e8 100644 --- a/tests/test_peer_connection.nim +++ b/tests/test_peer_connection.nim @@ -1,5 +1,6 @@ import unittest, os, + stew/shims/net, chronos, confutils, ../beacon_chain/[conf, eth2_network] @@ -17,7 +18,7 @@ asyncTest "connect two nodes": c1.nat = "none" var n1PersistentAddress = c1.getPersistenBootstrapAddr( - parseIpAddress("127.0.0.1"), Port c1.tcpPort) + ValidIpAddress.init("127.0.0.1"), Port c1.tcpPort) var n1 = await createEth2Node(c1, ENRForkID()) diff --git a/vendor/nim-confutils b/vendor/nim-confutils index 6e5d57049..26667818b 160000 --- a/vendor/nim-confutils +++ b/vendor/nim-confutils @@ -1 +1 @@ -Subproject commit 6e5d570490989c753d4645ba9173ef9358d302bb +Subproject commit 26667818be48d428892b26be9535c1f0a98d1510 diff --git a/vendor/nim-eth b/vendor/nim-eth index d53cebcf5..617f5d8c1 160000 --- a/vendor/nim-eth +++ b/vendor/nim-eth @@ -1 +1 @@ -Subproject commit d53cebcf5dd35728c27088d9c95923d7baad4ac4 +Subproject commit 617f5d8c13fcf025cb0b135db3b67f6c0f3f7f04 diff --git a/vendor/nim-json-serialization b/vendor/nim-json-serialization index 8d4c31f7c..c478b7bbb 160000 --- a/vendor/nim-json-serialization +++ b/vendor/nim-json-serialization @@ -1 +1 @@ -Subproject commit 8d4c31f7c2f56e17ec3325ec054fe15ccf4e89d4 +Subproject commit c478b7bbbab6ee298a25c29c7357783d94aaecaa diff --git a/vendor/nim-libp2p b/vendor/nim-libp2p index 7b6e1c068..2aebae56c 160000 --- a/vendor/nim-libp2p +++ b/vendor/nim-libp2p @@ -1 +1 @@ -Subproject commit 7b6e1c06888a831d61d7019a07d2d59d5bd079ea +Subproject commit 2aebae56c03cb1dd14cdf9f2fa16ca921b217ff2 diff --git a/vendor/nim-stew b/vendor/nim-stew index 86ac01122..bb2221ba8 160000 --- a/vendor/nim-stew +++ b/vendor/nim-stew @@ -1 +1 @@ -Subproject commit 86ac01122c29119cd585e400e85396b6bd3cceb6 +Subproject commit bb2221ba8c8ea2d3fbbcfdb8d28796f8d3eff348