mirror of
https://github.com/logos-storage/logos-storage-nim-dht.git
synced 2026-01-02 13:33:08 +00:00
fix potential infinite loop in randomNodes
Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
parent
c1d2ea410d
commit
a6cfe1a084
@ -573,7 +573,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)
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user