2021-02-25 18:23:22 -06:00
|
|
|
import std/sequtils
|
|
|
|
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/libp2p
|
2023-08-01 16:47:57 -07:00
|
|
|
import pkg/libp2p/errors
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2022-05-19 14:56:03 -05:00
|
|
|
import pkg/codex/discovery
|
|
|
|
import pkg/codex/stores
|
|
|
|
import pkg/codex/blocktype as bt
|
|
|
|
import pkg/codex/blockexchange
|
2021-02-25 18:23:22 -06:00
|
|
|
|
2021-04-08 15:05:04 +02:00
|
|
|
import ../examples
|
|
|
|
|
2022-05-18 20:29:15 -06:00
|
|
|
type NodesComponents* =
|
|
|
|
tuple[
|
|
|
|
switch: Switch,
|
|
|
|
blockDiscovery: Discovery,
|
|
|
|
wallet: WalletRef,
|
|
|
|
network: BlockExcNetwork,
|
|
|
|
localStore: BlockStore,
|
|
|
|
peerStore: PeerCtxStore,
|
|
|
|
pendingBlocks: PendingBlocksManager,
|
|
|
|
discovery: DiscoveryEngine,
|
|
|
|
engine: BlockExcEngine,
|
|
|
|
networkStore: NetworkStore,
|
|
|
|
]
|
|
|
|
|
2021-02-25 18:23:22 -06:00
|
|
|
proc generateNodes*(
|
2023-06-22 08:11:18 -07:00
|
|
|
num: Natural, blocks: openArray[bt.Block] = []
|
|
|
|
): seq[NodesComponents] =
|
2021-02-25 18:23:22 -06:00
|
|
|
for i in 0 ..< num:
|
|
|
|
let
|
|
|
|
switch = newStandardSwitch(transportFlags = {ServerFlags.ReuseAddr})
|
2022-11-01 18:58:41 -06:00
|
|
|
discovery = Discovery.new(
|
|
|
|
switch.peerInfo.privateKey,
|
|
|
|
announceAddrs =
|
|
|
|
@[
|
|
|
|
MultiAddress.init("/ip4/127.0.0.1/tcp/0").expect(
|
|
|
|
"Should return multiaddress"
|
|
|
|
)
|
2025-01-21 21:54:46 +01:00
|
|
|
],
|
|
|
|
)
|
2021-04-19 16:37:38 +02:00
|
|
|
wallet = WalletRef.example
|
2021-08-30 13:25:20 -06:00
|
|
|
network = BlockExcNetwork.new(switch)
|
2022-03-03 03:30:42 +11:00
|
|
|
localStore = CacheStore.new(blocks.mapIt(it))
|
2022-05-18 20:29:15 -06:00
|
|
|
peerStore = PeerCtxStore.new()
|
|
|
|
pendingBlocks = PendingBlocksManager.new()
|
2024-08-26 15:18:59 +02:00
|
|
|
advertiser = Advertiser.new(localStore, discovery)
|
2022-10-27 07:44:56 -06:00
|
|
|
blockDiscovery =
|
|
|
|
DiscoveryEngine.new(localStore, peerStore, network, discovery, pendingBlocks)
|
2024-08-26 15:18:59 +02:00
|
|
|
engine = BlockExcEngine.new(
|
|
|
|
localStore, wallet, network, blockDiscovery, advertiser, peerStore,
|
|
|
|
pendingBlocks,
|
|
|
|
)
|
2022-01-10 09:32:56 -06:00
|
|
|
networkStore = NetworkStore.new(engine, localStore)
|
2021-02-25 18:23:22 -06:00
|
|
|
|
|
|
|
switch.mount(network)
|
2025-01-10 15:12:37 +01:00
|
|
|
|
|
|
|
let nc: NodesComponents = (
|
2022-05-18 20:29:15 -06:00
|
|
|
switch, discovery, wallet, network, localStore, peerStore, pendingBlocks,
|
2022-10-27 07:44:56 -06:00
|
|
|
blockDiscovery, engine, networkStore,
|
2025-01-10 15:12:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
result.add(nc)
|
2021-02-25 18:23:22 -06: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)
|
2024-02-22 11:54:45 -03:00
|
|
|
|
|
|
|
proc connectNodes*(nodes: seq[NodesComponents]) {.async.} =
|
|
|
|
await connectNodes(nodes.mapIt(it.switch))
|