2022-10-28 07:49:18 +00:00
|
|
|
# Nimbus - Portal Network
|
2024-02-28 17:31:45 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-10-28 07:49:18 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
chronos,
|
2023-02-26 18:18:03 +00:00
|
|
|
eth/p2p/discoveryv5/protocol as discv5_protocol,
|
2022-10-28 07:49:18 +00:00
|
|
|
beacon_chain/spec/forks,
|
2024-06-18 07:32:57 +00:00
|
|
|
../../network/wire/[portal_protocol, portal_protocol_config, portal_stream],
|
2024-02-28 17:31:45 +00:00
|
|
|
../../network/beacon/[beacon_init_loader, beacon_network],
|
2022-10-28 07:49:18 +00:00
|
|
|
../test_helpers
|
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
type BeaconNode* = ref object
|
2022-10-28 07:49:18 +00:00
|
|
|
discoveryProtocol*: discv5_protocol.Protocol
|
2023-10-20 10:06:25 +00:00
|
|
|
beaconNetwork*: BeaconNetwork
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
proc newLCNode*(
|
2024-02-28 17:31:45 +00:00
|
|
|
rng: ref HmacDrbgContext, port: int, networkData: NetworkInitData
|
|
|
|
): BeaconNode =
|
2022-10-28 07:49:18 +00:00
|
|
|
let
|
|
|
|
node = initDiscoveryNode(rng, PrivateKey.random(rng[]), localAddress(port))
|
2023-10-20 10:06:25 +00:00
|
|
|
db = BeaconDb.new(networkData, "", inMemory = true)
|
2022-10-28 07:49:18 +00:00
|
|
|
streamManager = StreamManager.new(node)
|
2024-06-18 07:32:57 +00:00
|
|
|
network =
|
|
|
|
BeaconNetwork.new(PortalNetwork.none, node, db, streamManager, networkData.forks)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
return BeaconNode(discoveryProtocol: node, beaconNetwork: network)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
func portalProtocol*(n: BeaconNode): PortalProtocol =
|
|
|
|
n.beaconNetwork.portalProtocol
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
func localNode*(n: BeaconNode): Node =
|
2023-02-26 18:18:03 +00:00
|
|
|
n.discoveryProtocol.localNode
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
proc start*(n: BeaconNode) =
|
|
|
|
n.beaconNetwork.start()
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
proc stop*(n: BeaconNode) {.async.} =
|
|
|
|
n.beaconNetwork.stop()
|
2023-02-26 18:18:03 +00:00
|
|
|
await n.discoveryProtocol.closeWait()
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
proc containsId*(n: BeaconNode, contentId: ContentId): bool =
|
|
|
|
n.beaconNetwork.beaconDb.get(contentId).isSome()
|