Allow distance of 256 in lookupDistances (#556)

This commit is contained in:
Justin Traglia 2022-11-15 05:36:16 -06:00 committed by GitHub
parent 4b22fcdce4
commit ff0b1a330a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -623,7 +623,7 @@ proc lookupDistances*(target, dest: NodeId): seq[uint16] =
result.add(td) result.add(td)
var i = 1 var i = 1
while result.len < lookupRequestLimit: while result.len < lookupRequestLimit:
if tdAsInt + i < 256: if tdAsInt + i <= 256:
result.add(td + uint16(i)) result.add(td + uint16(i))
if tdAsInt - i > 0: if tdAsInt - i > 0:
result.add(td - uint16(i)) result.add(td - uint16(i))

View File

@ -582,6 +582,9 @@ suite "Discovery v5 Tests":
check test.len == 1 check test.len == 1
test "Calculate lookup distances": test "Calculate lookup distances":
let rng = newRng()
let node = generateNode(PrivateKey.random(rng[]))
# Log distance between zeros is zero # Log distance between zeros is zero
let dist = lookupDistances(u256(0), u256(0)) let dist = lookupDistances(u256(0), u256(0))
check dist == @[0'u16, 1, 2] check dist == @[0'u16, 1, 2]
@ -590,6 +593,22 @@ suite "Discovery v5 Tests":
let dist1 = lookupDistances(u256(0), u256(1)) let dist1 = lookupDistances(u256(0), u256(1))
check dist1 == @[1'u16, 2, 3] check dist1 == @[1'u16, 2, 3]
# Ensure that distances are in expected order
let dist2 = lookupDistances(node.id, idAtDistance(node.id, 128))
check dist2 == @[128'u16, 129, 127]
# Test target distance of 256
let dist3 = lookupDistances(node.id, idAtDistance(node.id, 256))
check dist3 == @[256'u16, 255, 254]
# Test target distance of 255
let dist4 = lookupDistances(node.id, idAtDistance(node.id, 255))
check dist4 == @[255'u16, 256, 254]
# Test target distance of 254
let dist5 = lookupDistances(node.id, idAtDistance(node.id, 254))
check dist5 == @[254'u16, 255, 253]
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(