2023-02-26 18:18:03 +00:00
|
|
|
# Nimbus - Portal Network
|
|
|
|
# Copyright (c) 2022-2023 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.
|
|
|
|
|
2023-01-31 12:38:08 +00:00
|
|
|
{.push raises: [].}
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
chronicles,
|
|
|
|
eth/p2p/discoveryv5/random2,
|
|
|
|
beacon_chain/gossip_processing/light_client_processor,
|
|
|
|
beacon_chain/spec/datatypes/altair,
|
|
|
|
beacon_chain/beacon_clock,
|
2023-10-20 10:06:25 +00:00
|
|
|
"."/[beacon_init_loader, beacon_network, beacon_light_client_manager]
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2023-03-13 20:30:57 +00:00
|
|
|
export
|
|
|
|
LightClientFinalizationMode,
|
2023-10-20 10:06:25 +00:00
|
|
|
beacon_network, beacon_light_client_manager
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2023-10-20 10:06:25 +00:00
|
|
|
logScope: topics = "beacon_lc"
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
LightClientHeaderCallback* =
|
2023-02-26 18:18:03 +00:00
|
|
|
proc(lightClient: LightClient, header: ForkedLightClientHeader) {.
|
|
|
|
gcsafe, raises: [].}
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
LightClient* = ref object
|
2023-10-20 10:06:25 +00:00
|
|
|
network*: BeaconNetwork
|
2022-10-28 07:49:18 +00:00
|
|
|
cfg: RuntimeConfig
|
|
|
|
forkDigests: ref ForkDigests
|
2023-09-28 16:16:41 +00:00
|
|
|
getBeaconTime*: GetBeaconTimeFn
|
2023-10-09 15:49:15 +00:00
|
|
|
store*: ref ForkedLightClientStore
|
2023-09-28 16:16:41 +00:00
|
|
|
processor*: ref LightClientProcessor
|
2022-10-28 07:49:18 +00:00
|
|
|
manager: LightClientManager
|
|
|
|
onFinalizedHeader*, onOptimisticHeader*: LightClientHeaderCallback
|
|
|
|
trustedBlockRoot*: Option[Eth2Digest]
|
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
func finalizedHeader*(
|
|
|
|
lightClient: LightClient): ForkedLightClientHeader =
|
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
var header = ForkedLightClientHeader(kind: lcDataFork)
|
|
|
|
header.forky(lcDataFork) = forkyStore.finalized_header
|
|
|
|
header
|
|
|
|
else:
|
|
|
|
default(ForkedLightClientHeader)
|
|
|
|
|
|
|
|
func optimisticHeader*(
|
|
|
|
lightClient: LightClient): ForkedLightClientHeader =
|
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
var header = ForkedLightClientHeader(kind: lcDataFork)
|
|
|
|
header.forky(lcDataFork) = forkyStore.optimistic_header
|
|
|
|
header
|
|
|
|
else:
|
|
|
|
default(ForkedLightClientHeader)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type LightClient,
|
2023-10-20 10:06:25 +00:00
|
|
|
network: BeaconNetwork,
|
2022-10-28 07:49:18 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
|
|
|
dumpEnabled: bool,
|
|
|
|
dumpDirInvalid, dumpDirIncoming: string,
|
|
|
|
cfg: RuntimeConfig,
|
|
|
|
forkDigests: ref ForkDigests,
|
|
|
|
getBeaconTime: GetBeaconTimeFn,
|
|
|
|
genesis_validators_root: Eth2Digest,
|
|
|
|
finalizationMode: LightClientFinalizationMode): T =
|
|
|
|
let lightClient = LightClient(
|
|
|
|
network: network,
|
|
|
|
cfg: cfg,
|
|
|
|
forkDigests: forkDigests,
|
|
|
|
getBeaconTime: getBeaconTime,
|
2023-02-26 18:18:03 +00:00
|
|
|
store: (ref ForkedLightClientStore)())
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
func getTrustedBlockRoot(): Option[Eth2Digest] =
|
|
|
|
lightClient.trustedBlockRoot
|
|
|
|
|
|
|
|
proc onStoreInitialized() =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc onFinalizedHeader() =
|
|
|
|
if lightClient.onFinalizedHeader != nil:
|
|
|
|
lightClient.onFinalizedHeader(
|
2023-02-26 18:18:03 +00:00
|
|
|
lightClient, lightClient.finalizedHeader)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
proc onOptimisticHeader() =
|
|
|
|
if lightClient.onOptimisticHeader != nil:
|
|
|
|
lightClient.onOptimisticHeader(
|
2023-02-26 18:18:03 +00:00
|
|
|
lightClient, lightClient.optimisticHeader)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
lightClient.processor = LightClientProcessor.new(
|
|
|
|
dumpEnabled, dumpDirInvalid, dumpDirIncoming,
|
|
|
|
cfg, genesis_validators_root, finalizationMode,
|
|
|
|
lightClient.store, getBeaconTime, getTrustedBlockRoot,
|
|
|
|
onStoreInitialized, onFinalizedHeader, onOptimisticHeader)
|
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
proc lightClientVerifier(obj: SomeForkedLightClientObject):
|
2022-12-02 15:09:31 +00:00
|
|
|
Future[Result[void, VerifierError]] =
|
|
|
|
let resfut = newFuture[Result[void, VerifierError]]("lightClientVerifier")
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClient.processor[].addObject(MsgSource.gossip, obj, resfut)
|
|
|
|
resfut
|
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
proc bootstrapVerifier(obj: ForkedLightClientBootstrap): auto =
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClientVerifier(obj)
|
2023-02-26 18:18:03 +00:00
|
|
|
proc updateVerifier(obj: ForkedLightClientUpdate): auto =
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClientVerifier(obj)
|
2023-02-26 18:18:03 +00:00
|
|
|
proc finalityVerifier(obj: ForkedLightClientFinalityUpdate): auto =
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClientVerifier(obj)
|
2023-02-26 18:18:03 +00:00
|
|
|
proc optimisticVerifier(obj: ForkedLightClientOptimisticUpdate): auto =
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClientVerifier(obj)
|
|
|
|
|
|
|
|
func isLightClientStoreInitialized(): bool =
|
2023-02-26 18:18:03 +00:00
|
|
|
lightClient.store[].kind > LightClientDataFork.None
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
func isNextSyncCommitteeKnown(): bool =
|
2023-02-26 18:18:03 +00:00
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
forkyStore.is_next_sync_committee_known
|
|
|
|
else:
|
|
|
|
false
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2022-11-18 09:00:06 +00:00
|
|
|
func getFinalizedSlot(): Slot =
|
2023-02-26 18:18:03 +00:00
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
forkyStore.finalized_header.beacon.slot
|
|
|
|
else:
|
|
|
|
GENESIS_SLOT
|
|
|
|
|
|
|
|
func getOptimisticSlot(): Slot =
|
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
forkyStore.optimistic_header.beacon.slot
|
|
|
|
else:
|
|
|
|
GENESIS_SLOT
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
lightClient.manager = LightClientManager.init(
|
|
|
|
lightClient.network, rng, getTrustedBlockRoot,
|
|
|
|
bootstrapVerifier, updateVerifier, finalityVerifier, optimisticVerifier,
|
|
|
|
isLightClientStoreInitialized, isNextSyncCommitteeKnown,
|
2023-02-26 18:18:03 +00:00
|
|
|
getFinalizedSlot, getOptimisticSlot, getBeaconTime)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
lightClient
|
|
|
|
|
|
|
|
proc new*(
|
|
|
|
T: type LightClient,
|
2023-10-20 10:06:25 +00:00
|
|
|
network: BeaconNetwork,
|
2022-10-28 07:49:18 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
2023-09-28 16:16:41 +00:00
|
|
|
networkData: NetworkInitData,
|
2022-10-28 07:49:18 +00:00
|
|
|
finalizationMode: LightClientFinalizationMode): T =
|
2023-09-28 16:16:41 +00:00
|
|
|
let
|
|
|
|
getBeaconTime = networkData.clock.getBeaconTimeFn()
|
|
|
|
forkDigests = newClone networkData.forks
|
|
|
|
|
2022-10-28 07:49:18 +00:00
|
|
|
LightClient.new(
|
|
|
|
network, rng,
|
|
|
|
dumpEnabled = false, dumpDirInvalid = ".", dumpDirIncoming = ".",
|
2023-09-28 16:16:41 +00:00
|
|
|
networkData.metadata.cfg, forkDigests, getBeaconTime,
|
|
|
|
networkData.genesis_validators_root, finalizationMode)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
proc start*(lightClient: LightClient) =
|
2023-09-28 16:16:41 +00:00
|
|
|
notice "Starting beacon light client",
|
2022-10-28 07:49:18 +00:00
|
|
|
trusted_block_root = lightClient.trustedBlockRoot
|
|
|
|
lightClient.manager.start()
|
|
|
|
|
|
|
|
proc resetToFinalizedHeader*(
|
|
|
|
lightClient: LightClient,
|
2023-02-26 18:18:03 +00:00
|
|
|
header: ForkedLightClientHeader,
|
|
|
|
current_sync_committee: altair.SyncCommittee) =
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClient.processor[].resetToFinalizedHeader(header, current_sync_committee)
|
|
|
|
|