2022-06-07 17:01:11 +00:00
|
|
|
# beacon_chain
|
2023-01-13 15:46:35 +00:00
|
|
|
# Copyright (c) 2022-2023 Status Research & Development GmbH
|
2022-06-07 17:01:11 +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.
|
|
|
|
|
2022-07-29 10:53:42 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-06-07 17:01:11 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
chronicles,
|
|
|
|
./beacon_node
|
|
|
|
|
|
|
|
logScope: topics = "beacnde"
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
const storeDataFork = LightClient.storeDataFork
|
|
|
|
|
2022-08-25 03:53:59 +00:00
|
|
|
func shouldSyncOptimistically*(node: BeaconNode, wallSlot: Slot): bool =
|
2022-08-29 12:16:35 +00:00
|
|
|
if node.eth1Monitor == nil:
|
2022-08-25 03:53:59 +00:00
|
|
|
return false
|
2022-08-29 12:16:35 +00:00
|
|
|
let optimisticHeader = node.lightClient.optimisticHeader.valueOr:
|
2022-08-25 03:53:59 +00:00
|
|
|
return false
|
|
|
|
|
2022-08-29 12:16:35 +00:00
|
|
|
shouldSyncOptimistically(
|
2023-01-13 15:46:35 +00:00
|
|
|
optimisticSlot = optimisticHeader.beacon.slot,
|
2022-08-29 12:16:35 +00:00
|
|
|
dagSlot = getStateField(node.dag.headState, slot),
|
|
|
|
wallSlot = wallSlot)
|
2022-08-25 03:53:59 +00:00
|
|
|
|
2022-06-07 17:01:11 +00:00
|
|
|
proc initLightClient*(
|
|
|
|
node: BeaconNode,
|
2022-06-21 08:29:16 +00:00
|
|
|
rng: ref HmacDrbgContext,
|
2022-06-07 17:01:11 +00:00
|
|
|
cfg: RuntimeConfig,
|
|
|
|
forkDigests: ref ForkDigests,
|
|
|
|
getBeaconTime: GetBeaconTimeFn,
|
|
|
|
genesis_validators_root: Eth2Digest) =
|
|
|
|
template config(): auto = node.config
|
|
|
|
|
2022-08-29 12:16:35 +00:00
|
|
|
# Creating a light client is not dependent on `syncLightClient`
|
2022-06-07 17:01:11 +00:00
|
|
|
# because the light client module also handles gossip subscriptions
|
|
|
|
# for broadcasting light client data as a server.
|
|
|
|
|
2022-06-10 14:16:37 +00:00
|
|
|
let
|
2022-08-25 03:53:59 +00:00
|
|
|
optimisticHandler = proc(signedBlock: ForkedMsgTrustedSignedBeaconBlock):
|
2022-06-10 14:16:37 +00:00
|
|
|
Future[void] {.async.} =
|
2022-08-29 12:16:35 +00:00
|
|
|
info "New LC optimistic block",
|
2022-06-10 14:16:37 +00:00
|
|
|
opt = signedBlock.toBlockId(),
|
|
|
|
dag = node.dag.head.bid,
|
|
|
|
wallSlot = node.currentSlot
|
2022-08-29 12:16:35 +00:00
|
|
|
withBlck(signedBlock):
|
2022-12-14 17:30:56 +00:00
|
|
|
when stateFork == BeaconStateFork.EIP4844:
|
|
|
|
debugRaiseAssert $eip4844ImplementationMissing & ": beacon_node_light_client.nim:initLightClient"
|
|
|
|
elif stateFork >= BeaconStateFork.Bellatrix:
|
2022-08-29 12:16:35 +00:00
|
|
|
if blck.message.is_execution_block:
|
|
|
|
template payload(): auto = blck.message.body.execution_payload
|
|
|
|
|
|
|
|
let eth1Monitor = node.eth1Monitor
|
|
|
|
if eth1Monitor != nil and not payload.block_hash.isZero:
|
|
|
|
# engine_newPayloadV1
|
|
|
|
discard await eth1Monitor.newExecutionPayload(payload)
|
|
|
|
|
|
|
|
# Retain optimistic head for other `forkchoiceUpdated` callers.
|
|
|
|
# May temporarily block `forkchoiceUpdatedV1` calls, e.g., Geth:
|
|
|
|
# - Refuses `newPayload`: "Ignoring payload while snap syncing"
|
|
|
|
# - Refuses `fcU`: "Forkchoice requested unknown head"
|
|
|
|
# Once DAG sync catches up or as new optimistic heads are fetched
|
|
|
|
# the situation recovers
|
|
|
|
node.consensusManager[].setOptimisticHead(
|
|
|
|
blck.toBlockId(), payload.block_hash)
|
|
|
|
|
|
|
|
# engine_forkchoiceUpdatedV1
|
|
|
|
let beaconHead = node.attestationPool[].getBeaconHead(nil)
|
|
|
|
discard await eth1Monitor.runForkchoiceUpdated(
|
|
|
|
headBlockRoot = payload.block_hash,
|
|
|
|
safeBlockRoot = beaconHead.safeExecutionPayloadHash,
|
|
|
|
finalizedBlockRoot = beaconHead.finalizedExecutionPayloadHash)
|
|
|
|
else: discard
|
|
|
|
|
2022-08-25 03:53:59 +00:00
|
|
|
optimisticProcessor = initOptimisticProcessor(
|
|
|
|
getBeaconTime, optimisticHandler)
|
2022-06-10 14:16:37 +00:00
|
|
|
|
|
|
|
lightClient = createLightClient(
|
2022-07-21 09:16:10 +00:00
|
|
|
node.network, rng, config, cfg, forkDigests, getBeaconTime,
|
|
|
|
genesis_validators_root, LightClientFinalizationMode.Strict)
|
2022-06-07 17:01:11 +00:00
|
|
|
|
2022-08-29 12:16:35 +00:00
|
|
|
if config.syncLightClient:
|
2022-08-25 03:53:59 +00:00
|
|
|
proc onOptimisticHeader(
|
2023-01-13 15:46:35 +00:00
|
|
|
lightClient: LightClient,
|
2023-01-14 21:19:50 +00:00
|
|
|
optimisticHeader: storeDataFork.LightClientHeader) =
|
2023-01-13 15:46:35 +00:00
|
|
|
optimisticProcessor.setOptimisticHeader(optimisticHeader.beacon)
|
2022-06-10 14:16:37 +00:00
|
|
|
|
|
|
|
lightClient.onOptimisticHeader = onOptimisticHeader
|
2022-08-29 12:16:35 +00:00
|
|
|
lightClient.trustedBlockRoot = config.trustedBlockRoot
|
2022-06-07 17:01:11 +00:00
|
|
|
|
2022-08-29 12:16:35 +00:00
|
|
|
elif config.trustedBlockRoot.isSome:
|
|
|
|
warn "Ignoring `trustedBlockRoot`, light client not enabled",
|
|
|
|
syncLightClient = config.syncLightClient,
|
|
|
|
trustedBlockRoot = config.trustedBlockRoot
|
2022-06-07 17:01:11 +00:00
|
|
|
|
2022-08-25 03:53:59 +00:00
|
|
|
node.optimisticProcessor = optimisticProcessor
|
2022-06-07 17:01:11 +00:00
|
|
|
node.lightClient = lightClient
|
|
|
|
|
|
|
|
proc startLightClient*(node: BeaconNode) =
|
2022-08-29 12:16:35 +00:00
|
|
|
if not node.config.syncLightClient:
|
2022-06-07 17:01:11 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
node.lightClient.start()
|
|
|
|
|
|
|
|
proc installLightClientMessageValidators*(node: BeaconNode) =
|
|
|
|
let eth2Processor =
|
2022-07-29 08:45:39 +00:00
|
|
|
if node.config.lightClientDataServe:
|
2022-06-07 17:01:11 +00:00
|
|
|
# Process gossip using both full node and light client
|
|
|
|
node.processor
|
2022-08-29 12:16:35 +00:00
|
|
|
elif node.config.syncLightClient:
|
2022-06-07 17:01:11 +00:00
|
|
|
# Only process gossip using light client
|
|
|
|
nil
|
|
|
|
else:
|
|
|
|
# Light client topics will never be subscribed to, no validators needed
|
|
|
|
return
|
|
|
|
|
|
|
|
node.lightClient.installMessageValidators(eth2Processor)
|
|
|
|
|
|
|
|
proc updateLightClientGossipStatus*(
|
|
|
|
node: BeaconNode, slot: Slot, dagIsBehind: bool) =
|
|
|
|
let isBehind =
|
2022-07-29 08:45:39 +00:00
|
|
|
if node.config.lightClientDataServe:
|
2022-06-07 17:01:11 +00:00
|
|
|
# Forward DAG's readiness to handle light client gossip
|
|
|
|
dagIsBehind
|
|
|
|
else:
|
|
|
|
# Full node is not interested in gossip
|
|
|
|
true
|
|
|
|
|
|
|
|
node.lightClient.updateGossipStatus(slot, some isBehind)
|
|
|
|
|
|
|
|
proc updateLightClientFromDag*(node: BeaconNode) =
|
2022-08-29 12:16:35 +00:00
|
|
|
if not node.config.syncLightClient:
|
2022-06-07 17:01:11 +00:00
|
|
|
return
|
2022-08-29 12:16:35 +00:00
|
|
|
if node.config.trustedBlockRoot.isSome:
|
2022-06-07 17:01:11 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
let
|
|
|
|
dagHead = node.dag.finalizedHead
|
|
|
|
dagPeriod = dagHead.slot.sync_committee_period
|
|
|
|
if dagHead.slot < node.dag.cfg.ALTAIR_FORK_EPOCH.start_slot:
|
|
|
|
return
|
|
|
|
|
|
|
|
let lcHeader = node.lightClient.finalizedHeader
|
|
|
|
if lcHeader.isSome:
|
2023-01-13 15:46:35 +00:00
|
|
|
if dagPeriod <= lcHeader.get.beacon.slot.sync_committee_period:
|
2022-06-07 17:01:11 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
let
|
|
|
|
bdata = node.dag.getForkedBlock(dagHead.blck.bid).valueOr:
|
|
|
|
return
|
2023-01-13 15:46:35 +00:00
|
|
|
header = withBlck(bdata):
|
2023-01-14 21:19:50 +00:00
|
|
|
blck.toLightClientHeader(storeDataFork)
|
2022-06-07 17:01:11 +00:00
|
|
|
current_sync_committee = block:
|
2022-12-06 12:40:13 +00:00
|
|
|
let tmpState = assignClone(node.dag.headState)
|
2022-06-07 17:01:11 +00:00
|
|
|
node.dag.currentSyncCommitteeForPeriod(tmpState[], dagPeriod).valueOr:
|
|
|
|
return
|
|
|
|
node.lightClient.resetToFinalizedHeader(header, current_sync_committee)
|