From 130d984d8b4f49fd06cab7e276409017bb7b0f62 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Thu, 9 May 2019 19:05:31 +0800 Subject: [PATCH] Added randomPeers iterator with protocol filter --- eth/p2p.nim | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/eth/p2p.nim b/eth/p2p.nim index f2ef064..c606689 100644 --- a/eth/p2p.nim +++ b/eth/p2p.nim @@ -154,8 +154,15 @@ proc randomPeer*(node: EthereumNode): Peer = if i == peerIdx: return peer inc i +iterator randomPeers*(node: EthereumNode, maxPeers: int, Protocol: type): Peer = + var peers = newSeqOfCap[Peer](node.peerPool.connectedNodes.len) + for peer in node.peers(Protocol): + peers.add(peer) + shuffle(peers) + if peers.len > maxPeers: peers.setLen(maxPeers) + for p in peers: yield p + proc randomPeerWith*(node: EthereumNode, Protocol: type): Peer = - mixin state var candidates = newSeq[Peer]() for p in node.peers(Protocol): candidates.add(p)