Merge pull request #52 from status-im/randomPeers-for-protocol

Added randomPeers iterator with protocol filter
This commit is contained in:
Yuriy Glukhov 2019-05-09 20:52:18 +08:00 committed by GitHub
commit 6064fb4c45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 if i == peerIdx: return peer
inc i 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 = proc randomPeerWith*(node: EthereumNode, Protocol: type): Peer =
mixin state
var candidates = newSeq[Peer]() var candidates = newSeq[Peer]()
for p in node.peers(Protocol): for p in node.peers(Protocol):
candidates.add(p) candidates.add(p)