Fix discovery after introduction of ValidIpAddress (#248)

This commit is contained in:
Kim De Mey 2020-06-09 11:09:35 +02:00 committed by GitHub
parent 4e31a86533
commit 1c77cc1d3d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 18 deletions

View File

@ -1,5 +1,5 @@
import import
std/net, stint, stew/endians2, stint, stew/endians2, stew/shims/net,
eth/trie/db, types, node eth/trie/db, types, node
{.push raises: [Defect].} {.push raises: [Defect].}

View File

@ -3,8 +3,7 @@
import import
strutils, macros, algorithm, options, strutils, macros, algorithm, options,
stew/shims/net, stew/shims/net, nimcrypto, stew/base64,
nimcrypto, stew/base64,
eth/[rlp, keys] eth/[rlp, keys]
export options export options

View File

@ -1,5 +1,5 @@
import import
std/[net, hashes], nimcrypto, stint, chronos, std/hashes, nimcrypto, stint, chronos, stew/shims/net,
eth/keys, enr eth/keys, enr
{.push raises: [Defect].} {.push raises: [Defect].}
@ -8,7 +8,7 @@ type
NodeId* = UInt256 NodeId* = UInt256
Address* = object Address* = object
ip*: IpAddress ip*: ValidIpAddress
port*: Port port*: Port
Node* = ref object Node* = ref object
@ -32,7 +32,8 @@ proc newNode*(r: Record): Result[Node, cstring] =
let tr = ? r.toTypedRecord() let tr = ? r.toTypedRecord()
if tr.ip.isSome() and tr.udp.isSome(): if tr.ip.isSome() and tr.udp.isSome():
let 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())) a = Address(ip: ip, port: Port(tr.udp.get()))
ok(Node(id: pk.get().toNodeId(), pubkey: pk.get() , record: r, ok(Node(id: pk.get().toNodeId(), pubkey: pk.get() , record: r,

View File

@ -393,16 +393,18 @@ proc receive*(d: Protocol, a: Address, packet: openArray[byte]) {.gcsafe,
proc processClient(transp: DatagramTransport, raddr: TransportAddress): proc processClient(transp: DatagramTransport, raddr: TransportAddress):
Future[void] {.async, gcsafe, raises: [Exception, Defect].} = Future[void] {.async, gcsafe, raises: [Exception, Defect].} =
let proto = getUserData[Protocol](transp) let proto = getUserData[Protocol](transp)
var a: Address var ip: IpAddress
var buf = newSeq[byte]() var buf = newSeq[byte]()
try: try:
a = Address(ip: raddr.address, port: raddr.port) ip = raddr.address()
except ValueError: except ValueError:
# This should not be possible considering we bind to an IP address. # This should not be possible considering we bind to an IP address.
error "Not a valid IpAddress" error "Not a valid IpAddress"
return return
let a = Address(ip: ValidIpAddress.init(ip), port: raddr.port)
try: try:
# TODO: should we use `peekMessage()` to avoid allocation? # TODO: should we use `peekMessage()` to avoid allocation?
# TODO: This can still raise general `Exception` while it probably should # TODO: This can still raise general `Exception` while it probably should
@ -710,7 +712,7 @@ proc newProtocol*(privKey: PrivateKey, db: Database,
privateKey: privKey, privateKey: privKey,
db: db, db: db,
localNode: node, localNode: node,
bindAddress: Address(ip: bindIp, port: udpPort), bindAddress: Address(ip: ValidIpAddress.init(bindIp), port: udpPort),
whoareyouMagic: whoareyouMagic(node.id), whoareyouMagic: whoareyouMagic(node.id),
idHash: sha256.digest(node.id.toByteArrayBE).data, idHash: sha256.digest(node.id.toByteArrayBE).data,
codec: Codec(localNode: node, privKey: privKey, db: db), codec: Codec(localNode: node, privKey: privKey, db: db),

View File

@ -1,12 +1,12 @@
import import
unittest, chronos, sequtils, chronicles, tables, stint, nimcrypto, 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/[discovery_db, enr, node, types, routing_table, encoding],
eth/p2p/discoveryv5/protocol as discv5_protocol, eth/p2p/discoveryv5/protocol as discv5_protocol,
./p2p_test_helper ./p2p_test_helper
proc localAddress*(port: int): Address = 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, proc initDiscoveryNode*(privKey: PrivateKey, address: Address,
bootstrapRecords: openarray[Record] = []): bootstrapRecords: openarray[Record] = []):
@ -37,7 +37,7 @@ proc randomPacket(tag: PacketTag): seq[byte] =
proc generateNode(privKey = PrivateKey.random()[], port: int = 20302): Node = proc generateNode(privKey = PrivateKey.random()[], port: int = 20302): Node =
let port = Port(port) 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") port, port).expect("Properly intialized private key")
result = newNode(enr).expect("Properly initialized node") result = newNode(enr).expect("Properly initialized node")

View File

@ -1,5 +1,5 @@
import 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] eth/[rlp, keys] , eth/p2p/discoveryv5/[types, encoding, enr, node]
# According to test vectors: # According to test vectors:
@ -233,7 +233,7 @@ suite "Discovery v5 Additional":
test "AuthHeader encode/decode": test "AuthHeader encode/decode":
let let
privKey = PrivateKey.random()[] 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") Port(9000)).expect("Properly intialized private key")
node = newNode(enrRec).expect("Properly initialized record") node = newNode(enrRec).expect("Properly initialized record")
nonce = hexToByteArray[authTagSize]("0x27b5af763c446acd2749fe8e") nonce = hexToByteArray[authTagSize]("0x27b5af763c446acd2749fe8e")

View File

@ -1,6 +1,6 @@
import import
net, unittest, options, unittest, options,
nimcrypto/utils, nimcrypto/utils, stew/shims/net,
eth/p2p/enode, eth/p2p/discoveryv5/enr, eth/keys eth/p2p/enode, eth/p2p/discoveryv5/enr, eth/keys
suite "ENR": suite "ENR":
@ -34,7 +34,7 @@ suite "ENR":
test "Create from ENode address": test "Create from ENode address":
let let
keys = KeyPair.random()[] 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), @[])[] enr = Record.init(100, keys.seckey, some(ip), Port(9000), Port(9000), @[])[]
typedEnr = get enr.toTypedRecord() typedEnr = get enr.toTypedRecord()
@ -54,7 +54,7 @@ suite "ENR":
test "ENR without address": test "ENR without address":
let let
keys = KeyPair.random()[] 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() typedEnr = get enr.toTypedRecord()
check: check: