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
std/net, stint, stew/endians2,
stint, stew/endians2, stew/shims/net,
eth/trie/db, types, node
{.push raises: [Defect].}

View File

@ -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

View File

@ -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,

View File

@ -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),

View File

@ -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")

View File

@ -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")

View File

@ -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: