Use results Opt for discv5 getNode and resolve (#625)

This commit is contained in:
Kim De Mey 2023-06-26 11:30:33 +02:00 committed by GitHub
parent 6b8a7b009e
commit 26ae539598
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 11 deletions

View File

@ -205,7 +205,7 @@ proc addNode*(d: Protocol, enr: EnrUri): bool =
if res: if res:
return d.addNode(r) 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. ## Get the node with id from the routing table.
d.routingTable.getNode(id) d.routingTable.getNode(id)
@ -391,7 +391,7 @@ proc registerTalkProtocol*(d: Protocol, protocolId: seq[byte],
ok() ok()
proc sendWhoareyou(d: Protocol, toId: NodeId, a: Address, 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) let key = HandshakeKey(nodeId: toId, address: a)
if not d.codec.hasHandshake(key): if not d.codec.hasHandshake(key):
let let
@ -764,7 +764,7 @@ proc queryRandom*(d: Protocol, enrField: (string, seq[byte])):
return filtered 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`. ## Resolve a `Node` based on provided `NodeId`.
## ##
## This will first look in the own routing table. If the node is known, it ## 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 ## does not reply, a lookup is done to see if it can find a (newer) record of
## the node on the network. ## the node on the network.
if id == d.localNode.id: if id == d.localNode.id:
return some(d.localNode) return Opt.some(d.localNode)
let node = d.getNode(id) let node = d.getNode(id)
if node.isSome(): 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 # TODO: Handle failures better. E.g. stop on different failures than timeout
if request.isOk() and request[].len > 0: if request.isOk() and request[].len > 0:
return some(request[][0]) return Opt.some(request[][0])
let discovered = await d.lookup(id) let discovered = await d.lookup(id)
for n in discovered: 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: if node.isSome() and node.get().record.seqNum >= n.record.seqNum:
return node return node
else: else:
return some(n) return Opt.some(n)
return node return node

View File

@ -8,13 +8,13 @@
{.push raises: [].} {.push raises: [].}
import import
std/[algorithm, times, sequtils, bitops, sets, options], std/[algorithm, times, sequtils, bitops, sets],
bearssl/rand, bearssl/rand,
stint, chronicles, metrics, chronos, stew/shims/net as stewNet, stint, chronicles, metrics, chronos, stew/shims/net as stewNet,
../../net/utils, ../../net/utils, stew/results,
"."/[node, random2, enr] "."/[node, random2, enr]
export options export results
declarePublicGauge routing_table_nodes, declarePublicGauge routing_table_nodes,
"Discovery routing table nodes", labels = ["state"] "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.add(b.replacementCache[high(b.replacementCache)])
b.replacementCache.delete(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. ## Get the `Node` with `id` as `NodeId` from the routing table.
## If no node with provided node id can be found,`none` is returned . ## If no node with provided node id can be found,`none` is returned .
let b = r.bucketForNode(id) let b = r.bucketForNode(id)
for n in b.nodes: for n in b.nodes:
if n.id == id: if n.id == id:
return some(n) return Opt.some(n)
proc contains*(r: RoutingTable, n: Node): bool = n in r.bucketForNode(n.id) proc contains*(r: RoutingTable, n: Node): bool = n in r.bucketForNode(n.id)
# Check if the routing table contains node `n`. # Check if the routing table contains node `n`.