mirror of https://github.com/status-im/nim-eth.git
Merge pull request #52 from status-im/randomPeers-for-protocol
Added randomPeers iterator with protocol filter
This commit is contained in:
commit
6064fb4c45
|
@ -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)
|
||||||
|
|
Loading…
Reference in New Issue