kad: remove unnecessary randomize call (#535)

* kad: remove unnecessary randomize call

* use `rng.sample`
This commit is contained in:
Jacek Sieka 2022-09-19 10:47:30 +02:00 committed by GitHub
parent 059d319c16
commit 2734ed6bda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 5 deletions

View File

@ -8,9 +8,9 @@
{.push raises: [Defect].}
import
std/[tables, hashes, times, algorithm, sets, sequtils, random],
std/[tables, hashes, times, algorithm, sets, sequtils],
chronos, chronicles, stint, nimcrypto/keccak,
../keys,
../keys, ./discoveryv5/random2,
./enode
export sets # TODO: This should not be needed, but compilation fails otherwise
@ -276,7 +276,6 @@ proc computeSharedPrefixBits(nodes: openArray[Node]): int =
proc init(r: var RoutingTable, thisNode: Node) =
r.thisNode = thisNode
r.buckets = @[newKBucket(0.u256, high(UInt256))]
randomize() # for later `randomNodes` selection
proc splitBucket(r: var RoutingTable, index: int) =
let bucket = r.buckets[index]
@ -669,9 +668,9 @@ proc randomNodes*(k: KademliaProtocol, count: int): seq[Node] =
# insignificant compared to the time it takes for the network roundtrips when connecting
# to nodes.
while len(seen) < count:
let bucket = k.routing.buckets.sample()
let bucket = k.rng[].sample(k.routing.buckets)
if bucket.nodes.len != 0:
let node = bucket.nodes.sample()
let node = k.rng[].sample(bucket.nodes)
if node notin seen:
result.add(node)
seen.incl(node)