mirror of https://github.com/status-im/nim-eth.git
Use results Opt for discv5 getNode and resolve (#625)
This commit is contained in:
parent
6b8a7b009e
commit
26ae539598
|
@ -205,7 +205,7 @@ proc addNode*(d: Protocol, enr: EnrUri): bool =
|
|||
if res:
|
||||
return d.addNode(r)
|
||||
|
||||
proc getNode*(d: Protocol, id: NodeId): Option[Node] =
|
||||
proc getNode*(d: Protocol, id: NodeId): Opt[Node] =
|
||||
## Get the node with id from the routing table.
|
||||
d.routingTable.getNode(id)
|
||||
|
||||
|
@ -391,7 +391,7 @@ proc registerTalkProtocol*(d: Protocol, protocolId: seq[byte],
|
|||
ok()
|
||||
|
||||
proc sendWhoareyou(d: Protocol, toId: NodeId, a: Address,
|
||||
requestNonce: AESGCMNonce, node: Option[Node]) =
|
||||
requestNonce: AESGCMNonce, node: Opt[Node]) =
|
||||
let key = HandshakeKey(nodeId: toId, address: a)
|
||||
if not d.codec.hasHandshake(key):
|
||||
let
|
||||
|
@ -764,7 +764,7 @@ proc queryRandom*(d: Protocol, enrField: (string, seq[byte])):
|
|||
|
||||
return filtered
|
||||
|
||||
proc resolve*(d: Protocol, id: NodeId): Future[Option[Node]] {.async.} =
|
||||
proc resolve*(d: Protocol, id: NodeId): Future[Opt[Node]] {.async.} =
|
||||
## Resolve a `Node` based on provided `NodeId`.
|
||||
##
|
||||
## This will first look in the own routing table. If the node is known, it
|
||||
|
@ -772,7 +772,7 @@ proc resolve*(d: Protocol, id: NodeId): Future[Option[Node]] {.async.} =
|
|||
## does not reply, a lookup is done to see if it can find a (newer) record of
|
||||
## the node on the network.
|
||||
if id == d.localNode.id:
|
||||
return some(d.localNode)
|
||||
return Opt.some(d.localNode)
|
||||
|
||||
let node = d.getNode(id)
|
||||
if node.isSome():
|
||||
|
@ -780,7 +780,7 @@ proc resolve*(d: Protocol, id: NodeId): Future[Option[Node]] {.async.} =
|
|||
|
||||
# TODO: Handle failures better. E.g. stop on different failures than timeout
|
||||
if request.isOk() and request[].len > 0:
|
||||
return some(request[][0])
|
||||
return Opt.some(request[][0])
|
||||
|
||||
let discovered = await d.lookup(id)
|
||||
for n in discovered:
|
||||
|
@ -788,7 +788,7 @@ proc resolve*(d: Protocol, id: NodeId): Future[Option[Node]] {.async.} =
|
|||
if node.isSome() and node.get().record.seqNum >= n.record.seqNum:
|
||||
return node
|
||||
else:
|
||||
return some(n)
|
||||
return Opt.some(n)
|
||||
|
||||
return node
|
||||
|
||||
|
|
|
@ -8,13 +8,13 @@
|
|||
{.push raises: [].}
|
||||
|
||||
import
|
||||
std/[algorithm, times, sequtils, bitops, sets, options],
|
||||
std/[algorithm, times, sequtils, bitops, sets],
|
||||
bearssl/rand,
|
||||
stint, chronicles, metrics, chronos, stew/shims/net as stewNet,
|
||||
../../net/utils,
|
||||
../../net/utils, stew/results,
|
||||
"."/[node, random2, enr]
|
||||
|
||||
export options
|
||||
export results
|
||||
|
||||
declarePublicGauge routing_table_nodes,
|
||||
"Discovery routing table nodes", labels = ["state"]
|
||||
|
@ -419,13 +419,13 @@ proc replaceNode*(r: var RoutingTable, n: Node) =
|
|||
b.add(b.replacementCache[high(b.replacementCache)])
|
||||
b.replacementCache.delete(high(b.replacementCache))
|
||||
|
||||
proc getNode*(r: RoutingTable, id: NodeId): Option[Node] =
|
||||
proc getNode*(r: RoutingTable, id: NodeId): Opt[Node] =
|
||||
## Get the `Node` with `id` as `NodeId` from the routing table.
|
||||
## If no node with provided node id can be found,`none` is returned .
|
||||
let b = r.bucketForNode(id)
|
||||
for n in b.nodes:
|
||||
if n.id == id:
|
||||
return some(n)
|
||||
return Opt.some(n)
|
||||
|
||||
proc contains*(r: RoutingTable, n: Node): bool = n in r.bucketForNode(n.id)
|
||||
# Check if the routing table contains node `n`.
|
||||
|
|
Loading…
Reference in New Issue