2023-02-26 18:18:03 +00:00
|
|
|
# Nimbus - Portal Network
|
2024-01-24 15:28:03 +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.
|
|
|
|
|
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/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
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
export LightClientFinalizationMode, beacon_network, beacon_light_client_manager
|
2022-10-28 07:49:18 +00:00
|
|
|
|
2024-02-28 17:31:45 +00:00
|
|
|
logScope:
|
|
|
|
topics = "beacon_lc"
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
type
|
2024-02-28 17:31:45 +00:00
|
|
|
LightClientHeaderCallback* = 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
|
|
|
|
|
2024-05-30 16:12:28 +00:00
|
|
|
func getFinalizedHeader*(lightClient: LightClient): ForkedLightClientHeader =
|
2023-02-26 18:18:03 +00:00
|
|
|
withForkyStore(lightClient.store[]):
|
|
|
|
when lcDataFork > LightClientDataFork.None:
|
|
|
|
var header = ForkedLightClientHeader(kind: lcDataFork)
|
|
|
|
header.forky(lcDataFork) = forkyStore.finalized_header
|
|
|
|
header
|
|
|
|
else:
|
|
|
|
default(ForkedLightClientHeader)
|
|
|
|
|
2024-05-30 16:12:28 +00:00
|
|
|
func getOptimisticHeader*(lightClient: LightClient): ForkedLightClientHeader =
|
2023-02-26 18:18:03 +00:00
|
|
|
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
|
|
|
|
2024-10-09 08:21:00 +00:00
|
|
|
func trustedBlockRoot(lightClient: LightClient): Opt[Eth2Digest] =
|
|
|
|
lightClient.network.trustedBlockRoot
|
|
|
|
|
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,
|
2024-02-28 17:31:45 +00:00
|
|
|
finalizationMode: LightClientFinalizationMode,
|
|
|
|
): T =
|
2022-10-28 07:49:18 +00:00
|
|
|
let lightClient = LightClient(
|
|
|
|
network: network,
|
|
|
|
cfg: cfg,
|
|
|
|
forkDigests: forkDigests,
|
|
|
|
getBeaconTime: getBeaconTime,
|
2024-02-28 17:31:45 +00:00
|
|
|
store: (ref ForkedLightClientStore)(),
|
|
|
|
)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
func getTrustedBlockRoot(): Option[Eth2Digest] =
|
2024-07-09 17:22:25 +00:00
|
|
|
# TODO: use Opt in LC processor
|
2024-10-09 08:21:00 +00:00
|
|
|
let trustedBlockRootOpt = lightClient.trustedBlockRoot()
|
|
|
|
if trustedBlockRootOpt.isSome():
|
|
|
|
some(trustedBlockRootOpt.value)
|
2024-07-09 17:22:25 +00:00
|
|
|
else:
|
|
|
|
none(Eth2Digest)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
proc onStoreInitialized() =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc onFinalizedHeader() =
|
|
|
|
if lightClient.onFinalizedHeader != nil:
|
2024-05-30 16:12:28 +00:00
|
|
|
lightClient.onFinalizedHeader(lightClient, lightClient.getFinalizedHeader)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
proc onOptimisticHeader() =
|
|
|
|
if lightClient.onOptimisticHeader != nil:
|
2024-05-30 16:12:28 +00:00
|
|
|
lightClient.onOptimisticHeader(lightClient, lightClient.getOptimisticHeader)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
lightClient.processor = LightClientProcessor.new(
|
2024-02-28 17:31:45 +00:00
|
|
|
dumpEnabled, dumpDirInvalid, dumpDirIncoming, cfg, genesis_validators_root,
|
|
|
|
finalizationMode, lightClient.store, getBeaconTime, getTrustedBlockRoot,
|
|
|
|
onStoreInitialized, onFinalizedHeader, onOptimisticHeader,
|
|
|
|
)
|
|
|
|
|
|
|
|
proc lightClientVerifier(
|
|
|
|
obj: SomeForkedLightClientObject
|
2024-06-14 12:21:30 +00:00
|
|
|
): Future[Result[void, VerifierError]] {.
|
|
|
|
async: (raises: [CancelledError], raw: true)
|
|
|
|
.} =
|
2024-02-28 17:31:45 +00:00
|
|
|
let resfut = Future[Result[void, VerifierError]].Raising([CancelledError]).init(
|
|
|
|
"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)
|
2024-02-28 17:31:45 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
proc updateVerifier(obj: ForkedLightClientUpdate): auto =
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClientVerifier(obj)
|
2024-02-28 17:31:45 +00:00
|
|
|
|
2023-02-26 18:18:03 +00:00
|
|
|
proc finalityVerifier(obj: ForkedLightClientFinalityUpdate): auto =
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClientVerifier(obj)
|
2024-02-28 17:31:45 +00:00
|
|
|
|
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(
|
2024-02-28 17:31:45 +00:00
|
|
|
lightClient.network, rng, getTrustedBlockRoot, bootstrapVerifier, updateVerifier,
|
|
|
|
finalityVerifier, optimisticVerifier, isLightClientStoreInitialized,
|
|
|
|
isNextSyncCommitteeKnown, 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,
|
2024-02-28 17:31:45 +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(
|
2024-02-28 17:31:45 +00:00
|
|
|
network,
|
|
|
|
rng,
|
|
|
|
dumpEnabled = false,
|
|
|
|
dumpDirInvalid = ".",
|
|
|
|
dumpDirIncoming = ".",
|
|
|
|
networkData.metadata.cfg,
|
|
|
|
forkDigests,
|
|
|
|
getBeaconTime,
|
|
|
|
networkData.genesis_validators_root,
|
|
|
|
finalizationMode,
|
|
|
|
)
|
2022-10-28 07:49:18 +00:00
|
|
|
|
|
|
|
proc start*(lightClient: LightClient) =
|
2024-10-09 08:21:00 +00:00
|
|
|
info "Starting beacon light client",
|
|
|
|
trusted_block_root = lightClient.trustedBlockRoot()
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClient.manager.start()
|
|
|
|
|
2024-09-20 12:54:36 +00:00
|
|
|
proc stop*(lightClient: LightClient) {.async: (raises: []).} =
|
2024-09-19 13:38:49 +00:00
|
|
|
info "Stopping beacon light client"
|
2024-09-20 12:54:36 +00:00
|
|
|
|
|
|
|
await lightClient.manager.stop()
|
2024-09-19 13:38:49 +00:00
|
|
|
|
2022-10-28 07:49:18 +00:00
|
|
|
proc resetToFinalizedHeader*(
|
|
|
|
lightClient: LightClient,
|
2023-02-26 18:18:03 +00:00
|
|
|
header: ForkedLightClientHeader,
|
2024-02-28 17:31:45 +00:00
|
|
|
current_sync_committee: altair.SyncCommittee,
|
|
|
|
) =
|
2022-10-28 07:49:18 +00:00
|
|
|
lightClient.processor[].resetToFinalizedHeader(header, current_sync_committee)
|