From 1c77cc1d3d611ea2057c0bc910b5d2d36e2e63d8 Mon Sep 17 00:00:00 2001 From: Kim De Mey Date: Tue, 9 Jun 2020 11:09:35 +0200 Subject: [PATCH] Fix discovery after introduction of ValidIpAddress (#248) --- eth/p2p/discoveryv5/discovery_db.nim | 2 +- eth/p2p/discoveryv5/enr.nim | 3 +-- eth/p2p/discoveryv5/node.nim | 7 ++++--- eth/p2p/discoveryv5/protocol.nim | 8 +++++--- tests/p2p/test_discoveryv5.nim | 6 +++--- tests/p2p/test_discv5_encoding.nim | 4 ++-- tests/p2p/test_enr.nim | 8 ++++---- 7 files changed, 20 insertions(+), 18 deletions(-) diff --git a/eth/p2p/discoveryv5/discovery_db.nim b/eth/p2p/discoveryv5/discovery_db.nim index 660a2dc..0f6131a 100644 --- a/eth/p2p/discoveryv5/discovery_db.nim +++ b/eth/p2p/discoveryv5/discovery_db.nim @@ -1,5 +1,5 @@ import - std/net, stint, stew/endians2, + stint, stew/endians2, stew/shims/net, eth/trie/db, types, node {.push raises: [Defect].} diff --git a/eth/p2p/discoveryv5/enr.nim b/eth/p2p/discoveryv5/enr.nim index 04d996e..c0676e3 100644 --- a/eth/p2p/discoveryv5/enr.nim +++ b/eth/p2p/discoveryv5/enr.nim @@ -3,8 +3,7 @@ import strutils, macros, algorithm, options, - stew/shims/net, - nimcrypto, stew/base64, + stew/shims/net, nimcrypto, stew/base64, eth/[rlp, keys] export options diff --git a/eth/p2p/discoveryv5/node.nim b/eth/p2p/discoveryv5/node.nim index 8b4440d..c4a581b 100644 --- a/eth/p2p/discoveryv5/node.nim +++ b/eth/p2p/discoveryv5/node.nim @@ -1,5 +1,5 @@ import - std/[net, hashes], nimcrypto, stint, chronos, + std/hashes, nimcrypto, stint, chronos, stew/shims/net, eth/keys, enr {.push raises: [Defect].} @@ -8,7 +8,7 @@ type NodeId* = UInt256 Address* = object - ip*: IpAddress + ip*: ValidIpAddress port*: Port Node* = ref object @@ -32,7 +32,8 @@ proc newNode*(r: Record): Result[Node, cstring] = let tr = ? r.toTypedRecord() if tr.ip.isSome() and tr.udp.isSome(): let - ip = IpAddress(family: IpAddressFamily.IPv4, address_v4: tr.ip.get()) + ip = ValidIpAddress.init( + IpAddress(family: IpAddressFamily.IPv4, address_v4: tr.ip.get())) a = Address(ip: ip, port: Port(tr.udp.get())) ok(Node(id: pk.get().toNodeId(), pubkey: pk.get() , record: r, diff --git a/eth/p2p/discoveryv5/protocol.nim b/eth/p2p/discoveryv5/protocol.nim index f8b9792..9ae685e 100644 --- a/eth/p2p/discoveryv5/protocol.nim +++ b/eth/p2p/discoveryv5/protocol.nim @@ -393,16 +393,18 @@ proc receive*(d: Protocol, a: Address, packet: openArray[byte]) {.gcsafe, proc processClient(transp: DatagramTransport, raddr: TransportAddress): Future[void] {.async, gcsafe, raises: [Exception, Defect].} = let proto = getUserData[Protocol](transp) - var a: Address + var ip: IpAddress var buf = newSeq[byte]() try: - a = Address(ip: raddr.address, port: raddr.port) + ip = raddr.address() except ValueError: # This should not be possible considering we bind to an IP address. error "Not a valid IpAddress" return + let a = Address(ip: ValidIpAddress.init(ip), port: raddr.port) + try: # TODO: should we use `peekMessage()` to avoid allocation? # TODO: This can still raise general `Exception` while it probably should @@ -710,7 +712,7 @@ proc newProtocol*(privKey: PrivateKey, db: Database, privateKey: privKey, db: db, localNode: node, - bindAddress: Address(ip: bindIp, port: udpPort), + bindAddress: Address(ip: ValidIpAddress.init(bindIp), port: udpPort), whoareyouMagic: whoareyouMagic(node.id), idHash: sha256.digest(node.id.toByteArrayBE).data, codec: Codec(localNode: node, privKey: privKey, db: db), diff --git a/tests/p2p/test_discoveryv5.nim b/tests/p2p/test_discoveryv5.nim index a3e89b2..67907f3 100644 --- a/tests/p2p/test_discoveryv5.nim +++ b/tests/p2p/test_discoveryv5.nim @@ -1,12 +1,12 @@ import unittest, chronos, sequtils, chronicles, tables, stint, nimcrypto, - eth/[keys, rlp], eth/trie/db, + stew/shims/net, eth/[keys, rlp], eth/trie/db, eth/p2p/discoveryv5/[discovery_db, enr, node, types, routing_table, encoding], eth/p2p/discoveryv5/protocol as discv5_protocol, ./p2p_test_helper proc localAddress*(port: int): Address = - Address(ip: parseIpAddress("127.0.0.1"), port: Port(port)) + Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port)) proc initDiscoveryNode*(privKey: PrivateKey, address: Address, bootstrapRecords: openarray[Record] = []): @@ -37,7 +37,7 @@ proc randomPacket(tag: PacketTag): seq[byte] = proc generateNode(privKey = PrivateKey.random()[], port: int = 20302): Node = let port = Port(port) - let enr = enr.Record.init(1, privKey, some(parseIpAddress("127.0.0.1")), + let enr = enr.Record.init(1, privKey, some(ValidIpAddress.init("127.0.0.1")), port, port).expect("Properly intialized private key") result = newNode(enr).expect("Properly initialized node") diff --git a/tests/p2p/test_discv5_encoding.nim b/tests/p2p/test_discv5_encoding.nim index e141c51..9e6a303 100644 --- a/tests/p2p/test_discv5_encoding.nim +++ b/tests/p2p/test_discv5_encoding.nim @@ -1,5 +1,5 @@ import - unittest, options, sequtils, net, stew/byteutils, stint, + unittest, options, sequtils, stint, stew/byteutils, stew/shims/net, eth/[rlp, keys] , eth/p2p/discoveryv5/[types, encoding, enr, node] # According to test vectors: @@ -233,7 +233,7 @@ suite "Discovery v5 Additional": test "AuthHeader encode/decode": let privKey = PrivateKey.random()[] - enrRec = enr.Record.init(1, privKey, none(IpAddress), Port(9000), + enrRec = enr.Record.init(1, privKey, none(ValidIpAddress), Port(9000), Port(9000)).expect("Properly intialized private key") node = newNode(enrRec).expect("Properly initialized record") nonce = hexToByteArray[authTagSize]("0x27b5af763c446acd2749fe8e") diff --git a/tests/p2p/test_enr.nim b/tests/p2p/test_enr.nim index 7edac0d..77f0f7c 100644 --- a/tests/p2p/test_enr.nim +++ b/tests/p2p/test_enr.nim @@ -1,6 +1,6 @@ import - net, unittest, options, - nimcrypto/utils, + unittest, options, + nimcrypto/utils, stew/shims/net, eth/p2p/enode, eth/p2p/discoveryv5/enr, eth/keys suite "ENR": @@ -34,7 +34,7 @@ suite "ENR": test "Create from ENode address": let keys = KeyPair.random()[] - ip = parseIpAddress("10.20.30.40") + ip = ValidIpAddress.init("10.20.30.40") enr = Record.init(100, keys.seckey, some(ip), Port(9000), Port(9000), @[])[] typedEnr = get enr.toTypedRecord() @@ -54,7 +54,7 @@ suite "ENR": test "ENR without address": let keys = KeyPair.random()[] - enr = Record.init(100, keys.seckey, none(IpAddress), Port(9000), Port(9000))[] + enr = Record.init(100, keys.seckey, none(ValidIpAddress), Port(9000), Port(9000))[] typedEnr = get enr.toTypedRecord() check: