switch NAT module from ValidIpAddress to IpAddress (#647)

* switch NAT module from ValidIpAddress to IpAddress

* fix dcli build

* switch discv5 from ValidIpAddress to IpAddress

* fix fuzzers build

* mostly finish nim-eth to IpAddress transition
This commit is contained in:
tersec 2023-11-10 09:28:21 +03:00 committed by GitHub
parent 700360fde2
commit ca4898e24a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 109 additions and 108 deletions

View File

@ -113,8 +113,7 @@ proc getExternalIP*(natStrategy: NatStrategy, quiet = false): Option[IpAddress]
# Further more, we check if the bind address (user provided, or a "0.0.0.0" # Further more, we check if the bind address (user provided, or a "0.0.0.0"
# default) is a public IP. That's a long shot, because code paths involving a # default) is a public IP. That's a long shot, because code paths involving a
# user-provided bind address are not supposed to get here. # user-provided bind address are not supposed to get here.
proc getRoutePrefSrc( proc getRoutePrefSrc(bindIp: IpAddress): (Option[IpAddress], PrefSrcStatus) =
bindIp: ValidIpAddress): (Option[ValidIpAddress], PrefSrcStatus) =
let bindAddress = initTAddress(bindIp, Port(0)) let bindAddress = initTAddress(bindIp, Port(0))
if bindAddress.isAnyLocal(): if bindAddress.isAnyLocal():
@ -123,18 +122,20 @@ proc getRoutePrefSrc(
# No route was found, log error and continue without IP. # No route was found, log error and continue without IP.
error "No routable IP address found, check your network connection", error "No routable IP address found, check your network connection",
error = ip.error error = ip.error
return (none(ValidIpAddress), NoRoutingInfo) return (none(IpAddress), NoRoutingInfo)
elif ip.get().isGlobalUnicast(): elif ip.get().isGlobalUnicast():
return (some(ip.get()), PrefSrcIsPublic) return (some(ip.get()), PrefSrcIsPublic)
else: else:
return (none(ValidIpAddress), PrefSrcIsPrivate) return (none(IpAddress), PrefSrcIsPrivate)
elif bindAddress.isGlobalUnicast(): elif bindAddress.isGlobalUnicast():
return (some(ValidIpAddress.init(bindIp)), BindAddressIsPublic) return (some(bindIp), BindAddressIsPublic)
else: else:
return (none(ValidIpAddress), BindAddressIsPrivate) return (none(IpAddress), BindAddressIsPrivate)
# Try to detect a public IP assigned to this host, before trying NAT traversal. # Try to detect a public IP assigned to this host, before trying NAT traversal.
proc getPublicRoutePrefSrcOrExternalIP*(natStrategy: NatStrategy, bindIp: ValidIpAddress, quiet = true): Option[ValidIpAddress] = proc getPublicRoutePrefSrcOrExternalIP*(
natStrategy: NatStrategy, bindIp: IpAddress, quiet = true):
Option[IpAddress] =
let (prefSrcIp, prefSrcStatus) = getRoutePrefSrc(bindIp) let (prefSrcIp, prefSrcStatus) = getRoutePrefSrc(bindIp)
case prefSrcStatus: case prefSrcStatus:
@ -143,7 +144,7 @@ proc getPublicRoutePrefSrcOrExternalIP*(natStrategy: NatStrategy, bindIp: ValidI
of PrefSrcIsPrivate, BindAddressIsPrivate: of PrefSrcIsPrivate, BindAddressIsPrivate:
let extIp = getExternalIP(natStrategy, quiet) let extIp = getExternalIP(natStrategy, quiet)
if extIp.isSome: if extIp.isSome:
return some(ValidIpAddress.init(extIp.get)) return some(extIp.get)
proc doPortMapping(tcpPort, udpPort: Port, description: string): Option[(Port, Port)] {.gcsafe.} = proc doPortMapping(tcpPort, udpPort: Port, description: string): Option[(Port, Port)] {.gcsafe.} =
var var
@ -293,14 +294,14 @@ proc redirectPorts*(tcpPort, udpPort: Port, description: string): Option[(Port,
proc setupNat*(natStrategy: NatStrategy, tcpPort, udpPort: Port, proc setupNat*(natStrategy: NatStrategy, tcpPort, udpPort: Port,
clientId: string): clientId: string):
tuple[ip: Option[ValidIpAddress], tcpPort, udpPort: Option[Port]] = tuple[ip: Option[IpAddress], tcpPort, udpPort: Option[Port]] =
## Setup NAT port mapping and get external IP address. ## Setup NAT port mapping and get external IP address.
## If any of this fails, we don't return any IP address but do return the ## If any of this fails, we don't return any IP address but do return the
## original ports as best effort. ## original ports as best effort.
## TODO: Allow for tcp or udp port mapping to be optional. ## TODO: Allow for tcp or udp port mapping to be optional.
let extIp = getExternalIP(natStrategy) let extIp = getExternalIP(natStrategy)
if extIp.isSome: if extIp.isSome:
let ip = ValidIpAddress.init(extIp.get) let ip = extIp.get
let extPorts = ({.gcsafe.}: let extPorts = ({.gcsafe.}:
redirectPorts(tcpPort = tcpPort, redirectPorts(tcpPort = tcpPort,
udpPort = udpPort, udpPort = udpPort,
@ -310,15 +311,15 @@ proc setupNat*(natStrategy: NatStrategy, tcpPort, udpPort: Port,
(ip: some(ip), tcpPort: some(extTcpPort), udpPort: some(extUdpPort)) (ip: some(ip), tcpPort: some(extTcpPort), udpPort: some(extUdpPort))
else: else:
warn "UPnP/NAT-PMP available but port forwarding failed" warn "UPnP/NAT-PMP available but port forwarding failed"
(ip: none(ValidIpAddress), tcpPort: some(tcpPort), udpPort: some(udpPort)) (ip: none(IpAddress), tcpPort: some(tcpPort), udpPort: some(udpPort))
else: else:
warn "UPnP/NAT-PMP not available" warn "UPnP/NAT-PMP not available"
(ip: none(ValidIpAddress), tcpPort: some(tcpPort), udpPort: some(udpPort)) (ip: none(IpAddress), tcpPort: some(tcpPort), udpPort: some(udpPort))
type type
NatConfig* = object NatConfig* = object
case hasExtIp*: bool case hasExtIp*: bool
of true: extIp*: ValidIpAddress of true: extIp*: IpAddress
of false: nat*: NatStrategy of false: nat*: NatStrategy
func parseCmdArg*(T: type NatConfig, p: string): T {.raises: [ValueError].} = func parseCmdArg*(T: type NatConfig, p: string): T {.raises: [ValueError].} =
@ -334,7 +335,7 @@ func parseCmdArg*(T: type NatConfig, p: string): T {.raises: [ValueError].} =
else: else:
if p.startsWith("extip:"): if p.startsWith("extip:"):
try: try:
let ip = ValidIpAddress.init(p[6..^1]) let ip = parseIpAddress(p[6..^1])
NatConfig(hasExtIp: true, extIp: ip) NatConfig(hasExtIp: true, extIp: ip)
except ValueError: except ValueError:
let error = "Not a valid IP address: " & p[6..^1] let error = "Not a valid IP address: " & p[6..^1]
@ -346,9 +347,9 @@ func parseCmdArg*(T: type NatConfig, p: string): T {.raises: [ValueError].} =
func completeCmdArg*(T: type NatConfig, val: string): seq[string] = func completeCmdArg*(T: type NatConfig, val: string): seq[string] =
return @[] return @[]
proc setupAddress*(natConfig: NatConfig, bindIp: ValidIpAddress, proc setupAddress*(natConfig: NatConfig, bindIp: IpAddress,
tcpPort, udpPort: Port, clientId: string): tcpPort, udpPort: Port, clientId: string):
tuple[ip: Option[ValidIpAddress], tcpPort, udpPort: Option[Port]] tuple[ip: Option[IpAddress], tcpPort, udpPort: Option[Port]]
{.gcsafe.} = {.gcsafe.} =
## Set-up of the external address via any of the ways as configured in ## Set-up of the external address via any of the ways as configured in
## `NatConfig`. In case all fails an error is logged and the bind ports are ## `NatConfig`. In case all fails an error is logged and the bind ports are
@ -377,9 +378,9 @@ proc setupAddress*(natConfig: NatConfig, bindIp: ValidIpAddress,
return (prefSrcIp, some(tcpPort), some(udpPort)) return (prefSrcIp, some(tcpPort), some(udpPort))
of PrefSrcIsPrivate: of PrefSrcIsPrivate:
error "No public IP address found. Should not use --nat:none option" error "No public IP address found. Should not use --nat:none option"
return (none(ValidIpAddress), some(tcpPort), some(udpPort)) return (none(IpAddress), some(tcpPort), some(udpPort))
of BindAddressIsPrivate: of BindAddressIsPrivate:
error "Bind IP is not a public IP address. Should not use --nat:none option" error "Bind IP is not a public IP address. Should not use --nat:none option"
return (none(ValidIpAddress), some(tcpPort), some(udpPort)) return (none(IpAddress), some(tcpPort), some(udpPort))
of NatUpnp, NatPmp: of NatUpnp, NatPmp:
return setupNat(natConfig.nat, tcpPort, udpPort, clientId) return setupNat(natConfig.nat, tcpPort, udpPort, clientId)

View File

@ -14,14 +14,14 @@ import
type type
IpLimits* = object IpLimits* = object
limit*: uint limit*: uint
ips: Table[ValidIpAddress, uint] ips: Table[IpAddress, uint]
func hash*(ip: ValidIpAddress): Hash = func hash*(ip: ValidIpAddress): Hash =
case ip.family case ip.family
of IpAddressFamily.IPv6: hash(ip.address_v6) of IpAddressFamily.IPv6: hash(ip.address_v6)
of IpAddressFamily.IPv4: hash(ip.address_v4) of IpAddressFamily.IPv4: hash(ip.address_v4)
func inc*(ipLimits: var IpLimits, ip: ValidIpAddress): bool = func inc*(ipLimits: var IpLimits, ip: IpAddress): bool =
let val = ipLimits.ips.getOrDefault(ip, 0) let val = ipLimits.ips.getOrDefault(ip, 0)
if val < ipLimits.limit: if val < ipLimits.limit:
ipLimits.ips[ip] = val + 1 ipLimits.ips[ip] = val + 1
@ -29,7 +29,7 @@ func inc*(ipLimits: var IpLimits, ip: ValidIpAddress): bool =
else: else:
false false
func dec*(ipLimits: var IpLimits, ip: ValidIpAddress) = func dec*(ipLimits: var IpLimits, ip: IpAddress) =
let val = ipLimits.ips.getOrDefault(ip, 0) let val = ipLimits.ips.getOrDefault(ip, 0)
if val == 1: if val == 1:
ipLimits.ips.del(ip) ipLimits.ips.del(ip)
@ -46,7 +46,7 @@ func isGlobalUnicast*(address: IpAddress): bool =
let a = initTAddress(address, Port(0)) let a = initTAddress(address, Port(0))
a.isGlobalUnicast() a.isGlobalUnicast()
proc getRouteIpv4*(): Result[ValidIpAddress, cstring] = proc getRouteIpv4*(): Result[IpAddress, cstring] =
# Avoiding Exception with initTAddress and can't make it work with static. # Avoiding Exception with initTAddress and can't make it work with static.
# Note: `publicAddress` is only used an "example" IP to find the best route, # Note: `publicAddress` is only used an "example" IP to find the best route,
# no data is send over the network to this IP! # no data is send over the network to this IP!
@ -63,4 +63,4 @@ proc getRouteIpv4*(): Result[ValidIpAddress, cstring] =
# This should not occur really. # This should not occur really.
error "Address conversion error", exception = e.name, msg = e.msg error "Address conversion error", exception = e.name, msg = e.msg
return err("Invalid IP address") return err("Invalid IP address")
ok(ValidIpAddress.init(ip)) ok(ip)

View File

@ -165,7 +165,7 @@ template toFieldPair*(key: string, value: auto): FieldPair =
func addAddress( func addAddress(
fields: var seq[FieldPair], fields: var seq[FieldPair],
ip: Option[ValidIpAddress], ip: Option[IpAddress],
tcpPort, udpPort: Option[Port]) = tcpPort, udpPort: Option[Port]) =
## Add address information in new fields. Incomplete address ## Add address information in new fields. Incomplete address
## information is allowed (example: Port but not IP) as that information ## information is allowed (example: Port but not IP) as that information
@ -190,7 +190,7 @@ func addAddress(
func init*( func init*(
T: type Record, T: type Record,
seqNum: uint64, pk: PrivateKey, seqNum: uint64, pk: PrivateKey,
ip: Option[ValidIpAddress], ip: Option[IpAddress],
tcpPort, udpPort: Option[Port], tcpPort, udpPort: Option[Port],
extraFields: openArray[FieldPair] = []): extraFields: openArray[FieldPair] = []):
EnrResult[T] = EnrResult[T] =
@ -321,7 +321,7 @@ func update*(
func update*( func update*(
r: var Record, r: var Record,
pk: PrivateKey, pk: PrivateKey,
ip: Option[ValidIpAddress], ip: Option[IpAddress],
tcpPort, udpPort: Option[Port] = none[Port](), tcpPort, udpPort: Option[Port] = none[Port](),
extraFields: openArray[FieldPair] = []): extraFields: openArray[FieldPair] = []):
EnrResult[void] = EnrResult[void] =

View File

@ -19,7 +19,7 @@ type
NodeId* = UInt256 NodeId* = UInt256
Address* = object Address* = object
ip*: ValidIpAddress ip*: IpAddress
port*: Port port*: Port
Node* = ref object Node* = ref object
@ -58,7 +58,7 @@ func newNode*(r: Record): Result[Node, cstring] =
ok(Node(id: pk.get().toNodeId(), pubkey: pk.get(), record: r, ok(Node(id: pk.get().toNodeId(), pubkey: pk.get(), record: r,
address: none(Address))) address: none(Address)))
func update*(n: Node, pk: PrivateKey, ip: Option[ValidIpAddress], func update*(n: Node, pk: PrivateKey, ip: Option[IpAddress],
tcpPort, udpPort: Option[Port] = none[Port](), tcpPort, udpPort: Option[Port] = none[Port](),
extraFields: openArray[FieldPair] = []): Result[void, cstring] = extraFields: openArray[FieldPair] = []): Result[void, cstring] =
? n.record.update(pk, ip, tcpPort, udpPort, extraFields) ? n.record.update(pk, ip, tcpPort, udpPort, extraFields)

View File

@ -175,7 +175,7 @@ const
) )
chronicles.formatIt(Option[Port]): $it chronicles.formatIt(Option[Port]): $it
chronicles.formatIt(Option[ValidIpAddress]): $it chronicles.formatIt(Option[IpAddress]): $it
proc addNode*(d: Protocol, node: Node): bool = proc addNode*(d: Protocol, node: Node): bool =
## Add `Node` to discovery routing table. ## Add `Node` to discovery routing table.
@ -479,7 +479,7 @@ proc processClient(transp: DatagramTransport, raddr: TransportAddress):
except ValueError as e: except ValueError as e:
error "Not a valid IpAddress", exception = e.name, msg = e.msg error "Not a valid IpAddress", exception = e.name, msg = e.msg
return return
let a = Address(ip: ValidIpAddress.init(ip), port: raddr.port) let a = Address(ip: ip, port: raddr.port)
proto.receive(a, buf) proto.receive(a, buf)
@ -832,7 +832,7 @@ proc revalidateNode*(d: Protocol, n: Node) {.async.} =
discard d.addNode(nodes[][0]) discard d.addNode(nodes[][0])
# Get IP and port from pong message and add it to the ip votes # Get IP and port from pong message and add it to the ip votes
let a = Address(ip: ValidIpAddress.init(res.ip), port: Port(res.port)) let a = Address(ip: res.ip, port: Port(res.port))
d.ipVote.insert(n.id, a) d.ipVote.insert(n.id, a)
proc revalidateLoop(d: Protocol) {.async.} = proc revalidateLoop(d: Protocol) {.async.} =
@ -865,7 +865,7 @@ proc refreshLoop(d: Protocol) {.async.} =
except CancelledError: except CancelledError:
trace "refreshLoop canceled" trace "refreshLoop canceled"
proc updateExternalIp*(d: Protocol, extIp: ValidIpAddress, udpPort: Port): bool = proc updateExternalIp*(d: Protocol, extIp: IpAddress, udpPort: Port): bool =
var success = false var success = false
let let
previous = d.localNode.address previous = d.localNode.address
@ -956,7 +956,7 @@ func init*(
proc newProtocol*( proc newProtocol*(
privKey: PrivateKey, privKey: PrivateKey,
enrIp: Option[ValidIpAddress], enrIp: Option[IpAddress],
enrTcpPort, enrUdpPort: Option[Port], enrTcpPort, enrUdpPort: Option[Port],
localEnrFields: openArray[(string, seq[byte])] = [], localEnrFields: openArray[(string, seq[byte])] = [],
bootstrapRecords: openArray[Record] = [], bootstrapRecords: openArray[Record] = [],
@ -1003,7 +1003,7 @@ proc newProtocol*(
Protocol( Protocol(
privateKey: privKey, privateKey: privKey,
localNode: node, localNode: node,
bindAddress: Address(ip: ValidIpAddress.init(bindIp), port: bindPort), bindAddress: Address(ip: bindIp, port: bindPort),
codec: Codec(localNode: node, privKey: privKey, codec: Codec(localNode: node, privKey: privKey,
sessions: Sessions.init(256)), sessions: Sessions.init(256)),
bootstrapRecords: @bootstrapRecords, bootstrapRecords: @bootstrapRecords,

View File

@ -12,12 +12,12 @@ init:
privKeyB = PrivateKey.fromHex(nodeBKey)[] # receive -> decode privKeyB = PrivateKey.fromHex(nodeBKey)[] # receive -> decode
enrRecA = enr.Record.init(1, privKeyA, enrRecA = enr.Record.init(1, privKeyA,
some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(parseIpAddress("127.0.0.1")), some(Port(9000)),
some(Port(9000))).expect("Properly initialized private key") some(Port(9000))).expect("Properly initialized private key")
nodeA = newNode(enrRecA).expect("Properly initialized record") nodeA = newNode(enrRecA).expect("Properly initialized record")
enrRecB = enr.Record.init(1, privKeyB, enrRecB = enr.Record.init(1, privKeyB,
some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(parseIpAddress("127.0.0.1")), some(Port(9000)),
some(Port(9000))).expect("Properly initialized private key") some(Port(9000))).expect("Properly initialized private key")
nodeB = newNode(enrRecB).expect("Properly initialized record") nodeB = newNode(enrRecB).expect("Properly initialized record")

View File

@ -11,7 +11,7 @@ proc generate() =
let let
rng = newRng() rng = newRng()
privKey = PrivateKey.random(rng[]) privKey = PrivateKey.random(rng[])
ip = some(ValidIpAddress.init("127.0.0.1")) ip = some(parseIpAddress("127.0.0.1"))
port = some(Port(20301)) port = some(Port(20301))
block: block:

View File

@ -6,8 +6,8 @@ import
export net export net
proc localAddress*(port: int): Address = func localAddress*(port: int): Address =
Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(port)) Address(ip: parseIpAddress("127.0.0.1"), port: Port(port))
proc initDiscoveryNode*( proc initDiscoveryNode*(
rng: ref HmacDrbgContext, rng: ref HmacDrbgContext,
@ -35,12 +35,12 @@ proc initDiscoveryNode*(
protocol protocol
proc nodeIdInNodes*(id: NodeId, nodes: openArray[Node]): bool = func nodeIdInNodes*(id: NodeId, nodes: openArray[Node]): bool =
for n in nodes: for n in nodes:
if id == n.id: return true if id == n.id: return true
proc generateNode*(privKey: PrivateKey, port: int = 20302, func generateNode*(privKey: PrivateKey, port: int = 20302,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1"), ip: IpAddress = parseIpAddress("127.0.0.1"),
localEnrFields: openArray[FieldPair] = []): Node = localEnrFields: openArray[FieldPair] = []): Node =
let port = Port(port) let port = Port(port)
let enr = enr.Record.init(1, privKey, some(ip), let enr = enr.Record.init(1, privKey, some(ip),
@ -55,7 +55,7 @@ proc generateNRandomNodes*(rng: var HmacDrbgContext, n: int): seq[Node] =
res res
proc nodeAndPrivKeyAtDistance*(n: Node, rng: var HmacDrbgContext, d: uint32, proc nodeAndPrivKeyAtDistance*(n: Node, rng: var HmacDrbgContext, d: uint32,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): (Node, PrivateKey) = ip: IpAddress = parseIpAddress("127.0.0.1")): (Node, PrivateKey) =
while true: while true:
let pk = PrivateKey.random(rng) let pk = PrivateKey.random(rng)
let node = generateNode(pk, ip = ip) let node = generateNode(pk, ip = ip)
@ -63,23 +63,23 @@ proc nodeAndPrivKeyAtDistance*(n: Node, rng: var HmacDrbgContext, d: uint32,
return (node, pk) return (node, pk)
proc nodeAtDistance*(n: Node, rng: var HmacDrbgContext, d: uint32, proc nodeAtDistance*(n: Node, rng: var HmacDrbgContext, d: uint32,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): Node = ip: IpAddress = parseIpAddress("127.0.0.1")): Node =
let (node, _) = n.nodeAndPrivKeyAtDistance(rng, d, ip) let (node, _) = n.nodeAndPrivKeyAtDistance(rng, d, ip)
node node
proc nodesAtDistance*( proc nodesAtDistance*(
n: Node, rng: var HmacDrbgContext, d: uint32, amount: int, n: Node, rng: var HmacDrbgContext, d: uint32, amount: int,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): seq[Node] = ip: IpAddress = parseIpAddress("127.0.0.1")): seq[Node] =
for i in 0..<amount: for i in 0..<amount:
result.add(nodeAtDistance(n, rng, d, ip)) result.add(nodeAtDistance(n, rng, d, ip))
proc nodesAtDistanceUniqueIp*( proc nodesAtDistanceUniqueIp*(
n: Node, rng: var HmacDrbgContext, d: uint32, amount: int, n: Node, rng: var HmacDrbgContext, d: uint32, amount: int,
ip: ValidIpAddress = ValidIpAddress.init("127.0.0.1")): seq[Node] = ip: IpAddress = parseIpAddress("127.0.0.1")): seq[Node] =
var ta = initTAddress(ip, Port(0)) var ta = initTAddress(ip, Port(0))
for i in 0..<amount: for i in 0..<amount:
ta.inc() ta.inc()
result.add(nodeAtDistance(n, rng, d, ValidIpAddress.init(ta.address()))) result.add(nodeAtDistance(n, rng, d, ta.address()))
proc addSeenNode*(d: discv5_protocol.Protocol, n: Node): bool = proc addSeenNode*(d: discv5_protocol.Protocol, n: Node): bool =
# Add it as a seen node, warning: for testing convenience only! # Add it as a seen node, warning: for testing convenience only!

View File

@ -416,7 +416,7 @@ suite "Discovery v5 Tests":
test "New protocol with enr": test "New protocol with enr":
let let
privKey = PrivateKey.random(rng[]) privKey = PrivateKey.random(rng[])
ip = some(ValidIpAddress.init("127.0.0.1")) ip = some(parseIpAddress("127.0.0.1"))
port = Port(20301) port = Port(20301)
node = newProtocol(privKey, ip, some(port), some(port), bindPort = port, node = newProtocol(privKey, ip, some(port), some(port), bindPort = port,
rng = rng) rng = rng)
@ -511,7 +511,7 @@ suite "Discovery v5 Tests":
let let
port = Port(9000) port = Port(9000)
srcRecord = enr.Record.init(1, PrivateKey.random(rng[]), srcRecord = enr.Record.init(1, PrivateKey.random(rng[]),
some(ValidIpAddress.init("11.12.13.14")), some(parseIpAddress("11.12.13.14")),
some(port), some(port))[] some(port), some(port))[]
srcNode = newNode(srcRecord)[] srcNode = newNode(srcRecord)[]
pk = PrivateKey.random(rng[]) pk = PrivateKey.random(rng[])
@ -521,7 +521,7 @@ suite "Discovery v5 Tests":
block: # Duplicates block: # Duplicates
let let
record = enr.Record.init( record = enr.Record.init(
1, pk, some(ValidIpAddress.init("12.13.14.15")), 1, pk, some(parseIpAddress("12.13.14.15")),
some(port), some(port))[] some(port), some(port))[]
# Exact duplicates # Exact duplicates
@ -531,7 +531,7 @@ suite "Discovery v5 Tests":
# Node id duplicates # Node id duplicates
let recordSameId = enr.Record.init( let recordSameId = enr.Record.init(
1, pk, some(ValidIpAddress.init("212.13.14.15")), 1, pk, some(parseIpAddress("212.13.14.15")),
some(port), some(port))[] some(port), some(port))[]
records.add(recordSameId) records.add(recordSameId)
nodes = verifyNodesRecords(records, srcNode, limit, targetDistance) nodes = verifyNodesRecords(records, srcNode, limit, targetDistance)
@ -540,7 +540,7 @@ suite "Discovery v5 Tests":
block: # No address block: # No address
let let
recordNoAddress = enr.Record.init( recordNoAddress = enr.Record.init(
1, pk, none(ValidIpAddress), some(port), some(port))[] 1, pk, none(IpAddress), some(port), some(port))[]
records = [recordNoAddress] records = [recordNoAddress]
test = verifyNodesRecords(records, srcNode, limit, targetDistance) test = verifyNodesRecords(records, srcNode, limit, targetDistance)
check test.len == 0 check test.len == 0
@ -548,7 +548,7 @@ suite "Discovery v5 Tests":
block: # Invalid address - any local block: # Invalid address - any local
let let
recordInvalidAddress = enr.Record.init( recordInvalidAddress = enr.Record.init(
1, pk, some(ValidIpAddress.init("0.0.0.0")), 1, pk, some(parseIpAddress("0.0.0.0")),
some(port), some(port))[] some(port), some(port))[]
records = [recordInvalidAddress] records = [recordInvalidAddress]
test = verifyNodesRecords(records, srcNode, limit, targetDistance) test = verifyNodesRecords(records, srcNode, limit, targetDistance)
@ -557,7 +557,7 @@ suite "Discovery v5 Tests":
block: # Invalid address - site local block: # Invalid address - site local
let let
recordInvalidAddress = enr.Record.init( recordInvalidAddress = enr.Record.init(
1, pk, some(ValidIpAddress.init("10.1.2.3")), 1, pk, some(parseIpAddress("10.1.2.3")),
some(port), some(port))[] some(port), some(port))[]
records = [recordInvalidAddress] records = [recordInvalidAddress]
test = verifyNodesRecords(records, srcNode, limit, targetDistance) test = verifyNodesRecords(records, srcNode, limit, targetDistance)
@ -566,7 +566,7 @@ suite "Discovery v5 Tests":
block: # Invalid address - loopback block: # Invalid address - loopback
let let
recordInvalidAddress = enr.Record.init( recordInvalidAddress = enr.Record.init(
1, pk, some(ValidIpAddress.init("127.0.0.1")), 1, pk, some(parseIpAddress("127.0.0.1")),
some(port), some(port))[] some(port), some(port))[]
records = [recordInvalidAddress] records = [recordInvalidAddress]
test = verifyNodesRecords(records, srcNode, limit, targetDistance) test = verifyNodesRecords(records, srcNode, limit, targetDistance)
@ -575,7 +575,7 @@ suite "Discovery v5 Tests":
block: # Invalid distance block: # Invalid distance
let let
recordInvalidDistance = enr.Record.init( recordInvalidDistance = enr.Record.init(
1, pk, some(ValidIpAddress.init("12.13.14.15")), 1, pk, some(parseIpAddress("12.13.14.15")),
some(port), some(port))[] some(port), some(port))[]
records = [recordInvalidDistance] records = [recordInvalidDistance]
test = verifyNodesRecords(records, srcNode, limit, @[0'u16]) test = verifyNodesRecords(records, srcNode, limit, @[0'u16])
@ -584,7 +584,7 @@ suite "Discovery v5 Tests":
block: # Invalid distance but distance validation is disabled block: # Invalid distance but distance validation is disabled
let let
recordInvalidDistance = enr.Record.init( recordInvalidDistance = enr.Record.init(
1, pk, some(ValidIpAddress.init("12.13.14.15")), 1, pk, some(parseIpAddress("12.13.14.15")),
some(port), some(port))[] some(port), some(port))[]
records = [recordInvalidDistance] records = [recordInvalidDistance]
test = verifyNodesRecords(records, srcNode, limit) test = verifyNodesRecords(records, srcNode, limit)
@ -594,7 +594,7 @@ suite "Discovery v5 Tests":
let let
port = Port(9000) port = Port(9000)
srcRecord = enr.Record.init(1, PrivateKey.random(rng[]), srcRecord = enr.Record.init(1, PrivateKey.random(rng[]),
some(ValidIpAddress.init("127.0.0.0")), some(parseIpAddress("127.0.0.0")),
some(port), some(port))[] some(port), some(port))[]
srcNode = newNode(srcRecord)[] srcNode = newNode(srcRecord)[]
pk = PrivateKey.random(rng[]) pk = PrivateKey.random(rng[])
@ -604,7 +604,7 @@ suite "Discovery v5 Tests":
block: # valid address - lo with lo src block: # valid address - lo with lo src
let let
record = enr.Record.init( record = enr.Record.init(
1, pk, some(ValidIpAddress.init("127.0.0.1")), 1, pk, some(parseIpAddress("127.0.0.1")),
some(port), some(port))[] some(port), some(port))[]
test = verifyNodesRecords([record], srcNode, limit, targetDistance) test = verifyNodesRecords([record], srcNode, limit, targetDistance)
check test.len == 1 check test.len == 1
@ -612,7 +612,7 @@ suite "Discovery v5 Tests":
block: # valid address - global with lo src block: # valid address - global with lo src
let let
record = enr.Record.init( record = enr.Record.init(
1, pk, some(ValidIpAddress.init("1.2.3.4")), 1, pk, some(parseIpAddress("1.2.3.4")),
some(port), some(port))[] some(port), some(port))[]
test = verifyNodesRecords([record], srcNode, limit, targetDistance) test = verifyNodesRecords([record], srcNode, limit, targetDistance)
check test.len == 1 check test.len == 1
@ -621,7 +621,7 @@ suite "Discovery v5 Tests":
let let
port = Port(9000) port = Port(9000)
srcRecord = enr.Record.init(1, PrivateKey.random(rng[]), srcRecord = enr.Record.init(1, PrivateKey.random(rng[]),
some(ValidIpAddress.init("192.168.1.1")), some(parseIpAddress("192.168.1.1")),
some(port), some(port))[] some(port), some(port))[]
srcNode = newNode(srcRecord)[] srcNode = newNode(srcRecord)[]
pk = PrivateKey.random(rng[]) pk = PrivateKey.random(rng[])
@ -631,7 +631,7 @@ suite "Discovery v5 Tests":
block: # valid address - site local with site local src block: # valid address - site local with site local src
let let
record = enr.Record.init( record = enr.Record.init(
1, pk, some(ValidIpAddress.init("192.168.1.2")), 1, pk, some(parseIpAddress("192.168.1.2")),
some(port), some(port))[] some(port), some(port))[]
test = verifyNodesRecords([record], srcNode, limit, targetDistance) test = verifyNodesRecords([record], srcNode, limit, targetDistance)
check test.len == 1 check test.len == 1
@ -639,7 +639,7 @@ suite "Discovery v5 Tests":
block: # valid address - global with site local src block: # valid address - global with site local src
let let
record = enr.Record.init( record = enr.Record.init(
1, pk, some(ValidIpAddress.init("1.2.3.4")), 1, pk, some(parseIpAddress("1.2.3.4")),
some(port), some(port))[] some(port), some(port))[]
test = verifyNodesRecords([record], srcNode, limit, targetDistance) test = verifyNodesRecords([record], srcNode, limit, targetDistance)
check test.len == 1 check test.len == 1
@ -684,7 +684,7 @@ suite "Discovery v5 Tests":
let let
privKey = PrivateKey.random(rng[]) privKey = PrivateKey.random(rng[])
enrRec = enr.Record.init(1, privKey, enrRec = enr.Record.init(1, privKey,
some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(parseIpAddress("127.0.0.1")), some(Port(9000)),
some(Port(9000))).expect("Properly initialized private key") some(Port(9000))).expect("Properly initialized private key")
sendNode = newNode(enrRec).expect("Properly initialized record") sendNode = newNode(enrRec).expect("Properly initialized record")
var codec = Codec(localNode: sendNode, privKey: privKey, sessions: Sessions.init(5)) var codec = Codec(localNode: sendNode, privKey: privKey, sessions: Sessions.init(5))
@ -713,7 +713,7 @@ suite "Discovery v5 Tests":
let let
privKey = PrivateKey.random(rng[]) privKey = PrivateKey.random(rng[])
enrRec = enr.Record.init(1, privKey, enrRec = enr.Record.init(1, privKey,
some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(parseIpAddress("127.0.0.1")), some(Port(9000)),
some(Port(9000))).expect("Properly initialized private key") some(Port(9000))).expect("Properly initialized private key")
sendNode = newNode(enrRec).expect("Properly initialized record") sendNode = newNode(enrRec).expect("Properly initialized record")
var codec = Codec(localNode: sendNode, privKey: privKey, sessions: Sessions.init(5)) var codec = Codec(localNode: sendNode, privKey: privKey, sessions: Sessions.init(5))
@ -744,7 +744,7 @@ suite "Discovery v5 Tests":
a = localAddress(20303) a = localAddress(20303)
privKey = PrivateKey.random(rng[]) privKey = PrivateKey.random(rng[])
enrRec = enr.Record.init(1, privKey, enrRec = enr.Record.init(1, privKey,
some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(parseIpAddress("127.0.0.1")), some(Port(9000)),
some(Port(9000))).expect("Properly initialized private key") some(Port(9000))).expect("Properly initialized private key")
sendNode = newNode(enrRec).expect("Properly initialized record") sendNode = newNode(enrRec).expect("Properly initialized record")
var codec = Codec(localNode: sendNode, privKey: privKey, sessions: Sessions.init(5)) var codec = Codec(localNode: sendNode, privKey: privKey, sessions: Sessions.init(5))

View File

@ -258,12 +258,12 @@ suite "Discovery v5.1 Packet Encodings Test Vectors":
privKeyB = PrivateKey.fromHex(nodeBKey)[] # receive -> decode privKeyB = PrivateKey.fromHex(nodeBKey)[] # receive -> decode
enrRecA = enr.Record.init(1, privKeyA, enrRecA = enr.Record.init(1, privKeyA,
some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(parseIpAddress("127.0.0.1")), some(Port(9000)),
some(Port(9000))).expect("Properly initialized private key") some(Port(9000))).expect("Properly initialized private key")
nodeA = newNode(enrRecA).expect("Properly initialized record") nodeA = newNode(enrRecA).expect("Properly initialized record")
enrRecB = enr.Record.init(1, privKeyB, enrRecB = enr.Record.init(1, privKeyB,
some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(parseIpAddress("127.0.0.1")), some(Port(9000)),
some(Port(9000))).expect("Properly initialized private key") some(Port(9000))).expect("Properly initialized private key")
nodeB = newNode(enrRecB).expect("Properly initialized record") nodeB = newNode(enrRecB).expect("Properly initialized record")
@ -479,12 +479,12 @@ suite "Discovery v5.1 Additional Encode/Decode":
privKeyB = PrivateKey.random(rng[]) # receiver -> decode privKeyB = PrivateKey.random(rng[]) # receiver -> decode
enrRecA = enr.Record.init(1, privKeyA, enrRecA = enr.Record.init(1, privKeyA,
some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(parseIpAddress("127.0.0.1")), some(Port(9000)),
some(Port(9000))).expect("Properly initialized private key") some(Port(9000))).expect("Properly initialized private key")
nodeA = newNode(enrRecA).expect("Properly initialized record") nodeA = newNode(enrRecA).expect("Properly initialized record")
enrRecB = enr.Record.init(1, privKeyB, enrRecB = enr.Record.init(1, privKeyB,
some(ValidIpAddress.init("127.0.0.1")), some(Port(9000)), some(parseIpAddress("127.0.0.1")), some(Port(9000)),
some(Port(9000))).expect("Properly initialized private key") some(Port(9000))).expect("Properly initialized private key")
nodeB = newNode(enrRecB).expect("Properly initialized record") nodeB = newNode(enrRecB).expect("Properly initialized record")

View File

@ -68,7 +68,7 @@ suite "ENR":
test "Create from ENode address": test "Create from ENode address":
let let
keypair = KeyPair.random(rng[]) keypair = KeyPair.random(rng[])
ip = ValidIpAddress.init("10.20.30.40") ip = parseIpAddress("10.20.30.40")
port = some(Port(9000)) port = some(Port(9000))
enr = Record.init( enr = Record.init(
100, keypair.seckey, some(ip), port, port,@[])[] 100, keypair.seckey, some(ip), port, port,@[])[]
@ -92,7 +92,7 @@ suite "ENR":
keypair = KeyPair.random(rng[]) keypair = KeyPair.random(rng[])
port = none(Port) port = none(Port)
enr = Record.init( enr = Record.init(
100, keypair.seckey, none(ValidIpAddress), port, port)[] 100, keypair.seckey, none(IpAddress), port, port)[]
typedEnr = get enr.toTypedRecord() typedEnr = get enr.toTypedRecord()
check: check:
@ -123,7 +123,7 @@ suite "ENR":
pk = PrivateKey.fromHex( pk = PrivateKey.fromHex(
"5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[] "5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[]
newField = toFieldPair("test", 123'u) newField = toFieldPair("test", 123'u)
var r = Record.init(1, pk, none(ValidIpAddress), none(Port), none(Port))[] var r = Record.init(1, pk, none(IpAddress), none(Port), none(Port))[]
block: # Insert new k:v pair, update of seqNum should occur. block: # Insert new k:v pair, update of seqNum should occur.
let updated = r.update(pk, [newField]) let updated = r.update(pk, [newField])
@ -190,11 +190,11 @@ suite "ENR":
let let
pk = PrivateKey.fromHex( pk = PrivateKey.fromHex(
"5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[] "5d2908f3f09ea1ff2e327c3f623159639b00af406e9009de5fd4b910fc34049d")[]
var r = Record.init(1, pk, none(ValidIpAddress), var r = Record.init(1, pk, none(IpAddress),
some(Port(9000)), some(Port(9000)))[] some(Port(9000)), some(Port(9000)))[]
block: block:
let updated = r.update(pk, none(ValidIpAddress), let updated = r.update(pk, none(IpAddress),
some(Port(9000)), some(Port(9000))) some(Port(9000)), some(Port(9000)))
check updated.isOk() check updated.isOk()
check: check:
@ -204,7 +204,7 @@ suite "ENR":
r.seqNum == 1 r.seqNum == 1
block: block:
let updated = r.update(pk, none(ValidIpAddress), let updated = r.update(pk, none(IpAddress),
some(Port(9001)), some(Port(9002))) some(Port(9001)), some(Port(9002)))
check updated.isOk() check updated.isOk()
check: check:
@ -214,7 +214,7 @@ suite "ENR":
r.seqNum == 2 r.seqNum == 2
block: block:
let updated = r.update(pk, some(ValidIpAddress.init("10.20.30.40")), let updated = r.update(pk, some(parseIpAddress("10.20.30.40")),
some(Port(9000)), some(Port(9000))) some(Port(9000)), some(Port(9000)))
check updated.isOk() check updated.isOk()
@ -233,7 +233,7 @@ suite "ENR":
r.seqNum == 3 r.seqNum == 3
block: block:
let updated = r.update(pk, some(ValidIpAddress.init("10.20.30.40")), let updated = r.update(pk, some(parseIpAddress("10.20.30.40")),
some(Port(9001)), some(Port(9001))) some(Port(9001)), some(Port(9001)))
check updated.isOk() check updated.isOk()

View File

@ -12,9 +12,9 @@ suite "IP vote":
var var
votes = IpVote.init(2) votes = IpVote.init(2)
let let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1)) addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2)) addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
addr3 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(3)) addr3 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(3))
votes.insert(NodeId.random(rng[]), addr1); votes.insert(NodeId.random(rng[]), addr1);
votes.insert(NodeId.random(rng[]), addr1); votes.insert(NodeId.random(rng[]), addr1);
@ -32,9 +32,9 @@ suite "IP vote":
var var
votes = IpVote.init(threshold) votes = IpVote.init(threshold)
let let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1)) addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2)) addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
addr3 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(3)) addr3 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(3))
votes.insert(NodeId.random(rng[]), addr1); votes.insert(NodeId.random(rng[]), addr1);
votes.insert(NodeId.random(rng[]), addr2); votes.insert(NodeId.random(rng[]), addr2);
@ -50,9 +50,9 @@ suite "IP vote":
var var
votes = IpVote.init(threshold) votes = IpVote.init(threshold)
let let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1)) addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2)) addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
addr3 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(3)) addr3 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(3))
votes.insert(NodeId.random(rng[]), addr1); votes.insert(NodeId.random(rng[]), addr1);
votes.insert(NodeId.random(rng[]), addr2); votes.insert(NodeId.random(rng[]), addr2);
@ -68,8 +68,8 @@ suite "IP vote":
var var
votes = IpVote.init(threshold) votes = IpVote.init(threshold)
let let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1)) addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2)) addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
let nodeIdA = NodeId.random(rng[]) let nodeIdA = NodeId.random(rng[])
votes.insert(nodeIdA, addr1); votes.insert(nodeIdA, addr1);
@ -86,9 +86,9 @@ suite "IP vote":
var var
votes = IpVote.init(threshold) votes = IpVote.init(threshold)
let let
addr1 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(1)) addr1 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(1))
addr2 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(2)) addr2 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(2))
addr3 = Address(ip: ValidIpAddress.init("127.0.0.1"), port: Port(3)) addr3 = Address(ip: parseIpAddress("127.0.0.1"), port: Port(3))
let nodeIdA = NodeId.random(rng[]) let nodeIdA = NodeId.random(rng[])
votes.insert(nodeIdA, addr1); votes.insert(nodeIdA, addr1);

View File

@ -291,7 +291,7 @@ suite "Routing Table Tests":
# Further fill the bucket with nodes with different ip. # Further fill the bucket with nodes with different ip.
let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256, let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256,
int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit), int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit),
ValidIpAddress.init("192.168.0.1")) parseIpAddress("192.168.0.1"))
for n in diffIpNodes: for n in diffIpNodes:
check table.addNode(n) == Added check table.addNode(n) == Added
@ -304,7 +304,7 @@ suite "Routing Table Tests":
# Add more nodes with different ip and distance 255 to get in the new bucket # Add more nodes with different ip and distance 255 to get in the new bucket
let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 255, let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 255,
int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit - 1), int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit - 1),
ValidIpAddress.init("192.168.1.1")) parseIpAddress("192.168.1.1"))
for n in diffIpNodes: for n in diffIpNodes:
check table.addNode(n) == Added check table.addNode(n) == Added
@ -329,7 +329,7 @@ suite "Routing Table Tests":
for j in 0..<amount: for j in 0..<amount:
let nodes = node.nodesAtDistanceUniqueIp(rng[], 256 - j, let nodes = node.nodesAtDistanceUniqueIp(rng[], 256 - j,
int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit), int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit),
ValidIpAddress.init("192.168.0.1")) parseIpAddress("192.168.0.1"))
for n in nodes: for n in nodes:
check table.addNode(n) == Added check table.addNode(n) == Added
@ -340,7 +340,7 @@ suite "Routing Table Tests":
# Add a node with a different IP, should work and split a bucket once more. # Add a node with a different IP, should work and split a bucket once more.
let anotherDiffIpNode = node.nodeAtDistance(rng[], 256 - amount, let anotherDiffIpNode = node.nodeAtDistance(rng[], 256 - amount,
ValidIpAddress.init("192.168.1.1")) parseIpAddress("192.168.1.1"))
check table.addNode(anotherDiffIpNode) == Added check table.addNode(anotherDiffIpNode) == Added
let amountLeft = int(DefaultTableIpLimits.tableIpLimit mod let amountLeft = int(DefaultTableIpLimits.tableIpLimit mod
@ -361,7 +361,7 @@ suite "Routing Table Tests":
let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256, let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256,
int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit + 1), int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit + 1),
ValidIpAddress.init("192.168.0.1")) parseIpAddress("192.168.0.1"))
for n in diffIpNodes: for n in diffIpNodes:
check table.addNode(n) == Added check table.addNode(n) == Added
@ -403,7 +403,7 @@ suite "Routing Table Tests":
let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256, let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256,
int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit + 1), int(BUCKET_SIZE - DefaultTableIpLimits.bucketIpLimit + 1),
ValidIpAddress.init("192.168.0.1")) parseIpAddress("192.168.0.1"))
for n in diffIpNodes: for n in diffIpNodes:
check table.addNode(n) == Added check table.addNode(n) == Added
@ -413,7 +413,7 @@ suite "Routing Table Tests":
let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256, let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256,
int(REPLACEMENT_CACHE_SIZE - 1), int(REPLACEMENT_CACHE_SIZE - 1),
ValidIpAddress.init("192.168.1.1")) parseIpAddress("192.168.1.1"))
for n in diffIpNodes: for n in diffIpNodes:
check table.addNode(n) == ReplacementAdded check table.addNode(n) == ReplacementAdded
@ -423,7 +423,7 @@ suite "Routing Table Tests":
# Add one with different ip, to remove the first # Add one with different ip, to remove the first
let diffIpNode = node.nodeAtDistance(rng[], 256, let diffIpNode = node.nodeAtDistance(rng[], 256,
ValidIpAddress.init("192.168.2.1")) parseIpAddress("192.168.2.1"))
check table.addNode(diffIpNode) == ReplacementAdded check table.addNode(diffIpNode) == ReplacementAdded
# Now the add should work # Now the add should work
@ -435,7 +435,7 @@ suite "Routing Table Tests":
# Fill bucket # Fill bucket
let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256, BUCKET_SIZE, let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256, BUCKET_SIZE,
ValidIpAddress.init("192.168.0.1")) parseIpAddress("192.168.0.1"))
for n in diffIpNodes: for n in diffIpNodes:
check table.addNode(n) == Added check table.addNode(n) == Added
@ -460,7 +460,7 @@ suite "Routing Table Tests":
let updatedNode1 = generateNode(pk) let updatedNode1 = generateNode(pk)
# Need to do an update to get seqNum increased # Need to do an update to get seqNum increased
let updated = updatedNode1.update(pk, let updated = updatedNode1.update(pk,
some(ValidIpAddress.init("192.168.0.1")), some(parseIpAddress("192.168.0.1")),
some(Port(9000)), some(Port(9000))) some(Port(9000)), some(Port(9000)))
check updated.isOk() check updated.isOk()
check table.addNode(updatedNode1) == Existing check table.addNode(updatedNode1) == Existing
@ -478,7 +478,7 @@ suite "Routing Table Tests":
# Fill bucket # Fill bucket
let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256, BUCKET_SIZE, let diffIpNodes = node.nodesAtDistanceUniqueIp(rng[], 256, BUCKET_SIZE,
ValidIpAddress.init("192.168.0.1")) parseIpAddress("192.168.0.1"))
for n in diffIpNodes: for n in diffIpNodes:
check table.addNode(n) == Added check table.addNode(n) == Added
@ -487,7 +487,7 @@ suite "Routing Table Tests":
# For replacements we don't need to get seqNum increased as the node will # For replacements we don't need to get seqNum increased as the node will
# still get pushed in front of the queue. # still get pushed in front of the queue.
let updatedNode1 = generateNode(pk, ip = ValidIpAddress.init("192.168.1.1")) let updatedNode1 = generateNode(pk, ip = parseIpAddress("192.168.1.1"))
check table.addNode(updatedNode1) == ReplacementExisting check table.addNode(updatedNode1) == ReplacementExisting
let sameIpNodes = node.nodesAtDistance(rng[], 256, let sameIpNodes = node.nodesAtDistance(rng[], 256,
@ -509,7 +509,7 @@ suite "Routing Table Tests":
for i in 0..<DefaultTableIpLimits.bucketIpLimit + 1: for i in 0..<DefaultTableIpLimits.bucketIpLimit + 1:
# Need to do an update to get seqNum increased # Need to do an update to get seqNum increased
let updated = updatedNode1.update(pk, let updated = updatedNode1.update(pk,
some(ValidIpAddress.init("192.168.0.1")), some(parseIpAddress("192.168.0.1")),
some(Port(9000+i)), some(Port(9000+i))) some(Port(9000+i)), some(Port(9000+i)))
check updated.isOk() check updated.isOk()
check table.addNode(updatedNode1) == Existing check table.addNode(updatedNode1) == Existing

View File

@ -37,7 +37,7 @@ type
listenAddress* {. listenAddress* {.
defaultValue: defaultListenAddress(config) defaultValue: defaultListenAddress(config)
desc: "Listening address for the Discovery v5 traffic" desc: "Listening address for the Discovery v5 traffic"
name: "listen-address" }: ValidIpAddress name: "listen-address" }: IpAddress
persistingFile* {. persistingFile* {.
defaultValue: "peerstore.csv", defaultValue: "peerstore.csv",
@ -108,8 +108,8 @@ type
desc: "ENR URI of the node to send a talkReq message" desc: "ENR URI of the node to send a talkReq message"
name: "node" .}: Node name: "node" .}: Node
func defaultListenAddress*(conf: DiscoveryConf): ValidIpAddress = func defaultListenAddress*(conf: DiscoveryConf): IpAddress =
(static ValidIpAddress.init("0.0.0.0")) (static parseIpAddress("0.0.0.0"))
func defaultAdminListenAddress*(conf: DiscoveryConf): ValidIpAddress = func defaultAdminListenAddress*(conf: DiscoveryConf): ValidIpAddress =
(static ValidIpAddress.init("127.0.0.1")) (static ValidIpAddress.init("127.0.0.1"))