Fix bugs in lookup and add test case (#820)

This commit is contained in:
KonradStaniec 2021-09-07 14:22:25 +02:00 committed by GitHub
parent 832a50df6e
commit 2c620c007d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 1 deletions

View File

@ -252,6 +252,7 @@ proc lookupDistances(target, dest: NodeId): seq[uint16] =
if td - i > 0'u16:
distances.add(td - i)
inc i
distances
proc lookupWorker(p: PortalProtocol, destNode: Node, target: NodeId):
Future[seq[Node]] {.async.} =
@ -261,7 +262,7 @@ proc lookupWorker(p: PortalProtocol, destNode: Node, target: NodeId):
let nodesMessage = await p.findNode(destNode, List[uint16, 256](distances))
if nodesMessage.isOk():
let records = recordsFromBytes(nodesMessage.get().enrs)
let verifiedNodes = verifyNodesRecords(records, destNode, @[0'u16])
let verifiedNodes = verifyNodesRecords(records, destNode, distances)
nodes.add(verifiedNodes)
# Attempt to add all nodes discovered

View File

@ -130,6 +130,36 @@ procSuite "Portal Tests":
await test.stopTest()
asyncTest "Portal lookup nodes":
let
node1 = initDiscoveryNode(
rng, PrivateKey.random(rng[]), localAddress(20302))
node2 = initDiscoveryNode(
rng, PrivateKey.random(rng[]), localAddress(20303))
node3 = initDiscoveryNode(
rng, PrivateKey.random(rng[]), localAddress(20304))
proto1 = PortalProtocol.new(node1, testHandler)
proto2 = PortalProtocol.new(node2, testHandler)
proto3 = PortalProtocol.new(node3, testHandler)
# Node1 knows about Node2, and Node2 knows about Node3 which hold all content
check proto1.addNode(node2.localNode) == Added
check proto2.addNode(node3.localNode) == Added
check (await proto2.ping(node3.localNode)).isOk()
let lookuResult = await proto1.lookup(node3.localNode.id)
check:
# returned result should contain node3 as it is in node2 routing table
lookuResult.contains(node3.localNode)
await node1.closeWait()
await node2.closeWait()
await node3.closeWait()
asyncTest "Portal FindContent/FoundContent - send enrs":
let test = defaultTestCase(rng)