2021-02-26 00:23:22 +00:00
|
|
|
import std/sequtils
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
|
|
|
|
2022-04-13 16:32:35 +00:00
|
|
|
import pkg/dagger/discovery
|
2021-08-30 19:25:20 +00:00
|
|
|
import pkg/dagger/stores
|
2021-02-26 00:23:22 +00:00
|
|
|
import pkg/dagger/blocktype as bt
|
|
|
|
|
2021-04-08 13:05:04 +00:00
|
|
|
import ../examples
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
proc generateNodes*(
|
|
|
|
num: Natural,
|
2022-04-20 12:28:11 +00:00
|
|
|
blocks: openArray[bt.Block] = [],
|
|
|
|
secureManagers: openarray[SecureProtocol] = [
|
|
|
|
SecureProtocol.Noise,
|
|
|
|
]): seq[tuple[switch: Switch, blockexc: NetworkStore]] =
|
2021-02-26 00:23:22 +00:00
|
|
|
for i in 0..<num:
|
|
|
|
let
|
|
|
|
switch = newStandardSwitch(transportFlags = {ServerFlags.ReuseAddr})
|
2022-04-13 16:32:35 +00:00
|
|
|
discovery = Discovery.new(switch.peerInfo, Port(0))
|
2021-04-19 14:37:38 +00:00
|
|
|
wallet = WalletRef.example
|
2021-08-30 19:25:20 +00:00
|
|
|
network = BlockExcNetwork.new(switch)
|
2022-03-02 16:30:42 +00:00
|
|
|
localStore = CacheStore.new(blocks.mapIt( it ))
|
2022-04-13 16:32:35 +00:00
|
|
|
engine = BlockExcEngine.new(localStore, wallet, network, discovery)
|
2022-01-10 15:32:56 +00:00
|
|
|
networkStore = NetworkStore.new(engine, localStore)
|
2021-02-26 00:23:22 +00:00
|
|
|
|
2022-04-20 12:28:11 +00:00
|
|
|
switch.mount(network)
|
|
|
|
|
2021-02-26 00:23:22 +00:00
|
|
|
switch.mount(network)
|
2022-01-10 15:32:56 +00:00
|
|
|
result.add((switch, networkStore))
|
2021-02-26 00:23:22 +00:00
|
|
|
|
|
|
|
proc connectNodes*(nodes: seq[Switch]) {.async.} =
|
|
|
|
for dialer in nodes:
|
|
|
|
for node in nodes:
|
|
|
|
if dialer.peerInfo.peerId != node.peerInfo.peerId:
|
|
|
|
await dialer.connect(node.peerInfo.peerId, node.peerInfo.addrs)
|