Fix test_discovery_helpers

This commit is contained in:
Zahary Karadjov 2020-02-12 15:56:42 +02:00 committed by zah
parent 2c814db750
commit 2a3e40e298
2 changed files with 25 additions and 23 deletions

View File

@ -73,6 +73,24 @@ proc toMultiAddressStr*(enode: ENode): string =
skkey: enode.pubkey))
&"/ip4/{enode.address.ip}/tcp/{enode.address.tcpPort}/p2p/{peerId.pretty}"
proc toENode*(enrRec: enr.Record): Result[ENode, cstring] =
try:
# TODO: handle IPv6
let ipBytes = enrRec.get("ip", seq[byte])
if ipBytes.len != 4:
return err "Malformed ENR IP address"
let
ip = IpAddress(family: IpAddressFamily.IPv4,
address_v4: toArray(4, ipBytes))
udpPort = Port enrRec.get("udp", uint16)
var pubKey: keys.PublicKey
if not enrRec.get(pubKey):
return err "Failed to read public key from ENR record"
return ok ENode(pubkey: pubkey,
address: Address(ip: ip, udpPort: udpPort))
except CatchableError:
return err "Invalid ENR record"
proc parseBootstrapAddress*(address: TaintedString): Result[enr.Record, cstring] =
if address.len == 0:
return err "an empty string is not a valid bootstrap node"
@ -95,25 +113,6 @@ proc parseBootstrapAddress*(address: TaintedString): Result[enr.Record, cstring]
var enrRec: enr.Record
if enrRec.fromURI(string address):
return ok enrRec
#[[
try:
# TODO: handle IPv6
let ipBytes = enrRec.get("ip", seq[byte])
if ipBytes.len != 4:
return err "Malformed ENR IP address"
let
ip = IpAddress(family: IpAddressFamily.IPv4,
address_v4: toArray(4, ipBytes))
udpPort = Port enrRec.get("udp", uint16)
var pubKey: keys.PublicKey
if not enrRec.get(pubKey):
return err "Failed to read public key from ENR record"
return ok ENode(pubkey: pubkey,
address: Address(ip: ip, udpPort: udpPort))
except CatchableError:
# This will continue to the failure path below
discard
]]#
return err "Invalid ENR bootstrap record"
elif lowerCaseAddress.startsWith("enode:"):
return err "ENode bootstrap addresses are not supported"

View File

@ -18,10 +18,13 @@ suite "Discovery v5 utilities":
timedTest "ENR to ENode":
let enr = "enr:-Iu4QPONEndy6aWOJLWBaCLS1KRg7YPeK0qptnxJzuBW8OcFP9tLgA_ewmAvHBzn9zPG6XIgdH83Mq_5cyLF5yWRYmYBgmlkgnY0gmlwhDaZ6cGJc2VjcDI1NmsxoQK-9tWOso2Kco7L5L-zKoj-MwPfeBbEP12bxr9bqzwZV4N0Y3CCIyiDdWRwgiMo"
let enrParsed = parseBootstrapAddress(enr)
check enrParsed.isOk
let enode = enrParsed.value.toENode
check:
enrParsed.isOk
$enrParsed.value.address.ip == "193.233.153.54"
enrParsed.value.address.tcpPort == Port(9000)
$enrParsed.value.pubkey == "bef6d58eb28d8a728ecbe4bfb32a88fe3303df7816c43f5d9bc6bf5bab3c19571012d3dd5ab492b1b0d2b42e32ce32f6bafc1075dbaaabe1fa6be711be7a992a"
enode.isOk
$enode.value.address.ip == "54.153.233.193"
enode.value.address.udpPort == Port(9000)
$enode.value.pubkey == "bef6d58eb28d8a728ecbe4bfb32a88fe3303df7816c43f5d9bc6bf5bab3c19571012d3dd5ab492b1b0d2b42e32ce32f6bafc1075dbaaabe1fa6be711be7a992a"