Fix lookupDistances function and make it public (#399)

This commit is contained in:
KonradStaniec 2021-09-10 17:26:22 +02:00 committed by GitHub
parent df6020832b
commit e219547d64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -654,15 +654,16 @@ proc talkreq*(d: Protocol, toNode: Node, protocol, request: seq[byte]):
discovery_message_requests_outgoing.inc(labelValues = ["no_response"]) discovery_message_requests_outgoing.inc(labelValues = ["no_response"])
return err("Talk response message not received in time") return err("Talk response message not received in time")
proc lookupDistances(target, dest: NodeId): seq[uint16] = proc lookupDistances*(target, dest: NodeId): seq[uint16] =
let td = logDist(target, dest) let td = logDist(target, dest)
let tdAsInt = int(td)
result.add(td) result.add(td)
var i = 1'u16 var i = 1
while result.len < lookupRequestLimit: while result.len < lookupRequestLimit:
if td + i < 256: if tdAsInt + i < 256:
result.add(td + i) result.add(td + uint16(i))
if td - i > 0'u16: if tdAsInt - i > 0:
result.add(td - i) result.add(td - uint16(i))
inc i inc i
proc lookupWorker(d: Protocol, destNode: Node, target: NodeId): proc lookupWorker(d: Protocol, destNode: Node, target: NodeId):

View File

@ -527,6 +527,15 @@ procSuite "Discovery v5 Tests":
test = verifyNodesRecords(records, fromNode, 0'u16) test = verifyNodesRecords(records, fromNode, 0'u16)
check test.len == 0 check test.len == 0
test "Calculate lookup distances":
# Log distance between zeros is zero
let dist = lookupDistances(u256(0), u256(0))
check dist == @[0'u16, 1, 2]
# Log distance between zero and one is one
let dist1 = lookupDistances(u256(0), u256(1))
check dist1 == @[1'u16, 2, 3]
asyncTest "Handshake cleanup: different ids": asyncTest "Handshake cleanup: different ids":
# Node to test the handshakes on. # Node to test the handshakes on.
let receiveNode = initDiscoveryNode( let receiveNode = initDiscoveryNode(