Merge pull request #103 from codex-storage/fix-randomNodes

fix potential infinite loop in randomNodes
This commit is contained in:
Csaba Kiraly 2024-10-18 20:45:49 +02:00 committed by GitHub
commit 57f4b6f7cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 25 additions and 18 deletions

View File

@ -578,7 +578,8 @@ proc randomNodes*(r: RoutingTable, maxAmount: int,
# while it will take less total time compared to e.g. an (async) # while it will take less total time compared to e.g. an (async)
# randomLookup, the time might be wasted as all nodes are possibly seen # randomLookup, the time might be wasted as all nodes are possibly seen
# already. # already.
while len(seen) < maxAmount: # We check against the number of nodes to avoid an infinite loop in case of a filter.
while len(result) < maxAmount and len(seen) < sz:
let bucket = r.rng[].sample(r.buckets) let bucket = r.rng[].sample(r.buckets)
if bucket.nodes.len != 0: if bucket.nodes.len != 0:
let node = r.rng[].sample(bucket.nodes) let node = r.rng[].sample(bucket.nodes)

View File

@ -412,26 +412,32 @@ suite "Discovery v5 Tests":
await mainNode.closeWait() await mainNode.closeWait()
await lookupNode.closeWait() await lookupNode.closeWait()
# We no longer support field filtering test "Random nodes, also with filter":
# test "Random nodes with spr field filter": let
# let lookupNode = initDiscoveryNode(rng, PrivateKey.example(rng), localAddress(20301))
# lookupNode = initDiscoveryNode(rng, PrivateKey.example(rng), localAddress(20301)) targetNode = initDiscoveryNode(rng, PrivateKey.example(rng), localAddress(20302))
# targetNode = generateNode(PrivateKey.example(rng)) otherNode = initDiscoveryNode(rng, PrivateKey.example(rng), localAddress(20303))
# otherNode = generateNode(PrivateKey.example(rng)) anotherNode = initDiscoveryNode(rng, PrivateKey.example(rng), localAddress(20304))
# anotherNode = generateNode(PrivateKey.example(rng))
# check: check:
# lookupNode.addNode(targetNode) lookupNode.addNode(targetNode.localNode.record)
# lookupNode.addNode(otherNode) lookupNode.addNode(otherNode.localNode.record)
# lookupNode.addNode(anotherNode) lookupNode.addNode(anotherNode.localNode.record)
# let discovered = lookupNode.randomNodes(10) let discovered = lookupNode.randomNodes(10)
# check discovered.len == 3 check discovered.len == 3
# let discoveredFiltered = lookupNode.randomNodes(10, let discoveredFiltered = lookupNode.randomNodes(10,
# ("test", @[byte 1,2,3,4])) proc(n: Node) : bool = n.address.get.port == Port(20302))
# check discoveredFiltered.len == 1 and discoveredFiltered.contains(targetNode) check discoveredFiltered.len == 1 and discoveredFiltered.contains(targetNode.localNode)
let discoveredEmpty = lookupNode.randomNodes(10,
proc(n: Node) : bool = n.address.get.port == Port(20305))
check discoveredEmpty.len == 0
await lookupNode.closeWait()
await targetNode.closeWait()
await otherNode.closeWait()
await anotherNode.closeWait()
# await lookupNode.closeWait()
test "New protocol with spr": test "New protocol with spr":
let let