Added randomPeers iterator with protocol filter

This commit is contained in:
Yuriy Glukhov 2019-05-09 19:05:31 +08:00
parent 14513b3bd8
commit 130d984d8b
No known key found for this signature in database
GPG Key ID: 733560674BB43E6C
1 changed files with 8 additions and 1 deletions

View File

@ -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)