work-around an issue with using chronicles logScopes inside async procs
This commit is contained in:
parent
b4d343805a
commit
86e69e999e
|
@ -14,6 +14,9 @@ import asyncdispatch2, eth_keys, stint, nimcrypto, chronicles, enode
|
|||
|
||||
export sets # TODO: This should not be needed, but compilation fails otherwise
|
||||
|
||||
template dbg(name: string, args: varargs[untyped]) =
|
||||
chronicles.debug(name, topic = "discovery", args)
|
||||
|
||||
logScope:
|
||||
topic = "discovery"
|
||||
|
||||
|
@ -322,7 +325,7 @@ proc bond(k: KademliaProtocol, n: Node): Future[bool] {.async.} =
|
|||
let token = k.ping(n)
|
||||
let gotPong = await k.waitPong(n, token)
|
||||
if not gotPong:
|
||||
debug "bonding failed, didn't receive pong from ", n
|
||||
dbg "bonding failed, didn't receive pong from ", n
|
||||
# Drop the failing node and schedule a populateNotFullBuckets() call to try and
|
||||
# fill its spot.
|
||||
k.routing.removeNode(n)
|
||||
|
@ -335,7 +338,7 @@ proc bond(k: KademliaProtocol, n: Node): Future[bool] {.async.} =
|
|||
discard await k.waitPing(n)
|
||||
|
||||
echo "bonding completed successfully with"
|
||||
debug "bonding completed successfully with ", n
|
||||
dbg "bonding completed successfully with ", n
|
||||
k.updateRoutingTable(n)
|
||||
return true
|
||||
|
||||
|
@ -356,7 +359,7 @@ proc lookup*(k: KademliaProtocol, nodeId: NodeId): Future[seq[Node]] {.async.} =
|
|||
k.wire.sendFindNode(remote, nodeId)
|
||||
var candidates = await k.waitNeighbours(remote)
|
||||
if candidates.len == 0:
|
||||
debug "got no candidates, returning ", "from" = remote
|
||||
dbg "got no candidates, returning ", "from" = remote
|
||||
result = candidates
|
||||
else:
|
||||
# The following line:
|
||||
|
@ -365,12 +368,12 @@ proc lookup*(k: KademliaProtocol, nodeId: NodeId): Future[seq[Node]] {.async.} =
|
|||
# 2. Removes all previously seen nodes from candidates
|
||||
# 3. Deduplicates candidates
|
||||
candidates.keepItIf(not nodesSeen.containsOrIncl(it))
|
||||
debug "got new candidates", count = candidates.len
|
||||
dbg "got new candidates", count = candidates.len
|
||||
let bonded = await all(candidates.mapIt(k.bond(it)))
|
||||
for i in 0 ..< bonded.len:
|
||||
if not bonded[i]: candidates[i] = nil
|
||||
candidates.keepItIf(not it.isNil)
|
||||
debug "bonded with candidates", count = candidates.len
|
||||
dbg "bonded with candidates", count = candidates.len
|
||||
result = candidates
|
||||
|
||||
proc excludeIfAsked(nodes: seq[Node]): seq[Node] =
|
||||
|
@ -378,10 +381,10 @@ proc lookup*(k: KademliaProtocol, nodeId: NodeId): Future[seq[Node]] {.async.} =
|
|||
sortByDistance(result, nodeId, FIND_CONCURRENCY)
|
||||
|
||||
var closest = k.routing.neighbours(nodeId)
|
||||
debug "starting lookup; initial neighbours: ", closest
|
||||
dbg "starting lookup; initial neighbours: ", closest
|
||||
var nodesToAsk = excludeIfAsked(closest)
|
||||
while nodesToAsk.len != 0:
|
||||
debug "node lookup; querying ", nodesToAsk
|
||||
dbg "node lookup; querying ", nodesToAsk
|
||||
nodesAsked.incl(nodesToAsk.toSet())
|
||||
let results = await all(nodesToAsk.mapIt(findNode(nodeId, it)))
|
||||
for candidates in results:
|
||||
|
@ -410,14 +413,14 @@ proc bootstrap*(k: KademliaProtocol, bootstrapNodes: seq[Node]) {.async.} =
|
|||
discard await k.lookupRandom()
|
||||
|
||||
proc recvPong*(k: KademliaProtocol, n: Node, token: seq[byte]) =
|
||||
debug "<<< pong", "from" = n
|
||||
dbg "<<< pong", "from" = n
|
||||
let pingid = token & @(n.node.pubkey.data)
|
||||
var future: Future[bool]
|
||||
if k.pongFutures.take(pingid, future):
|
||||
future.complete(true)
|
||||
|
||||
proc recvPing*(k: KademliaProtocol, n: Node, msgHash: any) =
|
||||
debug "<<< ping", "from" = n
|
||||
dbg "<<< ping", "from" = n
|
||||
k.updateRoutingTable(n)
|
||||
k.wire.sendPong(n, msgHash)
|
||||
|
||||
|
@ -432,19 +435,19 @@ proc recvNeighbours*(k: KademliaProtocol, remote: Node, neighbours: seq[Node]) =
|
|||
## done as part of node lookup, so the actual processing is left to the callback from
|
||||
## neighbours_callbacks, which is added (and removed after it's done or timed out) in
|
||||
## wait_neighbours().
|
||||
debug "<<< neighbours", "from" = remote, neighbours
|
||||
dbg "<<< neighbours", "from" = remote, neighbours
|
||||
let cb = k.neighboursCallbacks.getOrDefault(remote)
|
||||
if not cb.isNil:
|
||||
cb(neighbours)
|
||||
else:
|
||||
debug "unexpected neighbours, probably came too late", "from" = remote
|
||||
dbg "unexpected neighbours, probably came too late", "from" = remote
|
||||
|
||||
proc recvFindNode*(k: KademliaProtocol, remote: Node, nodeId: NodeId) =
|
||||
if remote notin k.routing:
|
||||
# FIXME: This is not correct; a node we've bonded before may have become unavailable
|
||||
# and thus removed from self.routing, but once it's back online we should accept
|
||||
# find_nodes from them.
|
||||
debug "Ignoring find_node request from unknown node ", remote
|
||||
dbg "Ignoring find_node request from unknown node ", remote
|
||||
return
|
||||
k.updateRoutingTable(remote)
|
||||
var found = k.routing.neighbours(nodeId)
|
||||
|
|
Loading…
Reference in New Issue