2020-05-21 20:24:20 +00:00
|
|
|
import random
|
2019-12-06 02:16:18 +00:00
|
|
|
import chronos
|
2019-12-08 21:06:58 +00:00
|
|
|
import ../../libp2p/standard_setup
|
|
|
|
export standard_setup
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-05-21 20:24:20 +00:00
|
|
|
randomize()
|
|
|
|
|
2019-12-06 02:16:18 +00:00
|
|
|
proc generateNodes*(num: Natural, gossip: bool = false): seq[Switch] =
|
|
|
|
for i in 0..<num:
|
2019-12-08 21:06:58 +00:00
|
|
|
result.add(newStandardSwitch(gossip = gossip))
|
2019-12-06 02:16:18 +00:00
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
proc subscribeNodes*(nodes: seq[Switch]): Future[seq[Future[void]]] {.async.} =
|
2019-12-06 02:16:18 +00:00
|
|
|
for dialer in nodes:
|
|
|
|
for node in nodes:
|
|
|
|
if dialer.peerInfo.peerId != node.peerInfo.peerId:
|
2020-07-27 19:33:51 +00:00
|
|
|
await dialer.connect(node.peerInfo)
|
|
|
|
result.add(dialer.subscribePeer(node.peerInfo))
|
2020-05-21 20:24:20 +00:00
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
proc subscribeSparseNodes*(nodes: seq[Switch], degree: int = 2): Future[seq[Future[void]]] {.async.} =
|
2020-06-02 23:53:38 +00:00
|
|
|
if nodes.len < degree:
|
|
|
|
raise (ref CatchableError)(msg: "nodes count needs to be greater or equal to degree!")
|
|
|
|
|
|
|
|
for i, dialer in nodes:
|
|
|
|
if (i mod degree) != 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for node in nodes:
|
|
|
|
if dialer.peerInfo.peerId != node.peerInfo.peerId:
|
2020-07-27 19:33:51 +00:00
|
|
|
await dialer.connect(node.peerInfo)
|
|
|
|
result.add(dialer.subscribePeer(node.peerInfo))
|
2020-06-02 23:53:38 +00:00
|
|
|
|
2020-07-27 19:33:51 +00:00
|
|
|
proc subscribeRandom*(nodes: seq[Switch]): Future[seq[Future[void]]] {.async.} =
|
2020-05-21 20:24:20 +00:00
|
|
|
for dialer in nodes:
|
|
|
|
var dialed: seq[string]
|
|
|
|
while dialed.len < nodes.len - 1:
|
|
|
|
let node = sample(nodes)
|
|
|
|
if node.peerInfo.id notin dialed:
|
|
|
|
if dialer.peerInfo.id != node.peerInfo.id:
|
2020-07-27 19:33:51 +00:00
|
|
|
await dialer.connect(node.peerInfo)
|
|
|
|
result.add(dialer.subscribePeer(node.peerInfo))
|
|
|
|
dialed.add(node.peerInfo.id)
|