From 2734ed6bda4512b0b3a7aa49fbbc8a4e4b86e7ea Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Mon, 19 Sep 2022 10:47:30 +0200 Subject: [PATCH] kad: remove unnecessary randomize call (#535) * kad: remove unnecessary randomize call * use `rng.sample` --- eth/p2p/kademlia.nim | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/eth/p2p/kademlia.nim b/eth/p2p/kademlia.nim index 511b7b7..dad8814 100644 --- a/eth/p2p/kademlia.nim +++ b/eth/p2p/kademlia.nim @@ -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)