From 21423fca42a3abf0a0924cd2fba0ae2116a95258 Mon Sep 17 00:00:00 2001 From: kdeme Date: Thu, 17 Dec 2020 15:42:04 +0100 Subject: [PATCH] Use of discv5.1 findnode request with multiple distances --- eth/p2p/discoveryv5/protocol.nim | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/eth/p2p/discoveryv5/protocol.nim b/eth/p2p/discoveryv5/protocol.nim index fd54c5e..cf75a09 100644 --- a/eth/p2p/discoveryv5/protocol.nim +++ b/eth/p2p/discoveryv5/protocol.nim @@ -638,21 +638,16 @@ proc lookupDistances(target, dest: NodeId): seq[uint32] {.raises: [Defect].} = proc lookupWorker(d: Protocol, destNode: Node, target: NodeId): Future[seq[Node]] {.async, raises: [Exception, Defect].} = let dists = lookupDistances(target, destNode.id) - var i = 0 - # TODO: We can make use of the multiple distances here now. - # Do findNode requests with different distances until we hit limits. - while i < lookupRequestLimit and result.len < findNodeResultLimit: - let r = await d.findNode(destNode, @[dists[i]]) - # TODO: Handle failures better. E.g. stop on different failures than timeout - if r.isOk: - # TODO: I guess it makes sense to limit here also to `findNodeResultLimit`? - result.add(r[]) - inc i - else: - break - for n in result: - discard d.addNode(n) + # Instead of doing max `lookupRequestLimit` findNode requests, make use + # of the discv5.1 functionality to request nodes for multiple distances. + let r = await d.findNode(destNode, dists) + if r.isOk: + result.add(r[]) + + # Attempt to add all nodes discovered + for n in result: + discard d.addNode(n) proc lookup*(d: Protocol, target: NodeId): Future[seq[Node]] {.async, raises: [Exception, Defect].} =