-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add
LightClientStore
and keep it in sync
The `LightClientStore` represents client data when syncing using the light client protocol. So far, Nimbus supported serving light client data to other nodes but did not incorporate client functionality. A new startup option is introduced, `light-client-trusted-block-root`, to configure a block root from which light client sync can be started. The `LightClientStore` is updated with new data from the network but is not yet used for other purposes. Log messages are produced to reflect applied updates ("Light client object processed").
- Loading branch information
1 parent
a86bb35
commit 8f9a874
Showing
8 changed files
with
518 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# beacon_chain | ||
# Copyright (c) 2022 Status Research & Development GmbH | ||
# 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. | ||
|
||
{.push raises: [Defect].} | ||
|
||
# This implements the pre-release proposal of the libp2p based light client sync | ||
# protocol. See https://github.com/ethereum/consensus-specs/pull/2802 | ||
|
||
import | ||
"."/beacon_node, | ||
./gossip_processing/light_client_processor, | ||
./spec/datatypes/altair, | ||
./sync/light_client_manager | ||
|
||
logScope: topics = "beacnde" | ||
|
||
proc initLightClient*( | ||
node: BeaconNode, | ||
cfg: RuntimeConfig, | ||
rng: ref BrHmacDrbgContext, | ||
genesis_validators_root: Eth2Digest, | ||
getBeaconTime: GetBeaconTimeFn) = | ||
template config(): auto = node.config | ||
|
||
let store = (ref Option[LightClientStore])() | ||
|
||
func getTrustedBlockRoot(): Option[Eth2Digest] = | ||
node.lightClient.trustedBlockRoot | ||
|
||
proc getLocalWallPeriod(): SyncCommitteePeriod = | ||
node.beaconClock.now.slotOrZero.sync_committee_period | ||
|
||
func getFinalizedPeriod(): SyncCommitteePeriod = | ||
if store[].isSome: | ||
store[].get.finalized_header.slot.sync_committee_period | ||
else: | ||
GENESIS_SLOT.sync_committee_period | ||
|
||
func isLightClientStoreInitialized(): bool = | ||
store[].isSome | ||
|
||
func isNextSyncCommitteeKnown(): bool = | ||
if store[].isSome: | ||
not store[].get.next_sync_committee.isZeroMemory | ||
else: | ||
false | ||
|
||
let lightClientProcessor = LightClientProcessor.new( | ||
config.dumpEnabled, config.dumpDirInvalid, config.dumpDirIncoming, | ||
cfg, genesis_validators_root, store, getBeaconTime, getTrustedBlockRoot) | ||
|
||
proc lightClientVerifier(obj: SomeLightClientObject): | ||
Future[Result[void, BlockError]] = | ||
let resfut = newFuture[Result[void, BlockError]]("lightClientVerifier") | ||
lightClientProcessor[].addObject(MsgSource.gossip, obj, resfut) | ||
resfut | ||
let | ||
bootstrapVerifier = proc(obj: altair.LightClientBootstrap): | ||
Future[Result[void, BlockError]] = | ||
lightClientVerifier(obj) | ||
updateVerifier = proc(obj: altair.LightClientUpdate): | ||
Future[Result[void, BlockError]] = | ||
lightClientVerifier(obj) | ||
optimisticUpdateVerifier = proc(obj: OptimisticLightClientUpdate): | ||
Future[Result[void, BlockError]] = | ||
lightClientVerifier(obj) | ||
|
||
node.lightClient.trustedBlockRoot = config.lightClientTrustedBlockRoot | ||
node.lightClient.store = store | ||
node.lightClient.processor = lightClientProcessor | ||
node.lightClient.manager = LightClientManager.init( | ||
node.network, rng, getTrustedBlockRoot, | ||
bootstrapVerifier, updateVerifier, optimisticUpdateVerifier, | ||
getLocalWallPeriod, getFinalizedPeriod, | ||
isLightClientStoreInitialized, isNextSyncCommitteeKnown) | ||
|
||
proc startLightClient*(node: BeaconNode) = | ||
if node.lightClient.trustedBlockRoot.isNone: | ||
return | ||
|
||
notice "Starting light client", | ||
trusted_block_root = node.lightClient.trustedBlockRoot.get | ||
node.lightClient.manager.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.