2022-03-11 20:28:10 +00:00
|
|
|
# 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.
|
|
|
|
|
2022-07-29 10:53:42 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2022-03-11 20:28:10 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
# Status libraries
|
|
|
|
stew/[bitops2, objects],
|
|
|
|
# Beacon chain internals
|
|
|
|
../spec/datatypes/[phase0, altair, bellatrix],
|
2022-07-04 20:46:32 +00:00
|
|
|
../beacon_chain_db_light_client,
|
2022-03-11 20:28:10 +00:00
|
|
|
"."/[block_pools_types, blockchain_dag]
|
|
|
|
|
2022-11-02 16:23:30 +00:00
|
|
|
from ../spec/datatypes/capella import TrustedSignedBeaconBlock
|
|
|
|
|
2022-08-21 09:24:59 +00:00
|
|
|
logScope: topics = "chaindag_lc"
|
2022-03-11 20:28:10 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
HashedBeaconStateWithSyncCommittee =
|
|
|
|
bellatrix.HashedBeaconState |
|
|
|
|
altair.HashedBeaconState
|
|
|
|
|
|
|
|
TrustedSignedBeaconBlockWithSyncAggregate =
|
|
|
|
bellatrix.TrustedSignedBeaconBlock |
|
|
|
|
altair.TrustedSignedBeaconBlock
|
|
|
|
|
|
|
|
template nextEpochBoundarySlot(slot: Slot): Slot =
|
|
|
|
## Compute the first possible epoch boundary state slot of a `Checkpoint`
|
|
|
|
## referring to a block at given slot.
|
|
|
|
(slot + (SLOTS_PER_EPOCH - 1)).epoch.start_slot
|
|
|
|
|
2022-03-20 10:58:59 +00:00
|
|
|
proc updateExistingState(
|
|
|
|
dag: ChainDAGRef, state: var ForkedHashedBeaconState, bsi: BlockSlotId,
|
|
|
|
save: bool, cache: var StateCache): bool =
|
|
|
|
## Wrapper around `updateState` for states expected to exist.
|
|
|
|
let ok = dag.updateState(state, bsi, save, cache)
|
|
|
|
if not ok:
|
|
|
|
error "State failed to load unexpectedly", bsi, tail = dag.tail.slot
|
2022-07-13 13:48:09 +00:00
|
|
|
doAssert strictVerification notin dag.updateFlags
|
2022-03-20 10:58:59 +00:00
|
|
|
ok
|
|
|
|
|
|
|
|
template withUpdatedExistingState(
|
2022-06-16 09:46:35 +00:00
|
|
|
dag: ChainDAGRef, stateParam: var ForkedHashedBeaconState,
|
2022-03-20 10:58:59 +00:00
|
|
|
bsiParam: BlockSlotId, okBody: untyped, failureBody: untyped): untyped =
|
|
|
|
## Wrapper around `withUpdatedState` for states expected to exist.
|
|
|
|
block:
|
|
|
|
let bsi = bsiParam
|
2022-06-16 09:46:35 +00:00
|
|
|
dag.withUpdatedState(stateParam, bsiParam) do:
|
2022-03-20 10:58:59 +00:00
|
|
|
okBody
|
|
|
|
do:
|
|
|
|
error "State failed to load unexpectedly", bsi, tail = dag.tail.slot
|
2022-07-13 13:48:09 +00:00
|
|
|
doAssert strictVerification notin dag.updateFlags
|
2022-03-20 10:58:59 +00:00
|
|
|
failureBody
|
|
|
|
|
|
|
|
proc getExistingBlockIdAtSlot(dag: ChainDAGRef, slot: Slot): Opt[BlockSlotId] =
|
|
|
|
## Wrapper around `getBlockIdAtSlot` for blocks expected to exist.
|
|
|
|
let bsi = dag.getBlockIdAtSlot(slot)
|
|
|
|
if bsi.isErr:
|
|
|
|
error "Block failed to load unexpectedly", slot, tail = dag.tail.slot
|
2022-07-13 13:48:09 +00:00
|
|
|
doAssert strictVerification notin dag.updateFlags
|
2022-03-20 10:58:59 +00:00
|
|
|
bsi
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
proc existingParent(dag: ChainDAGRef, bid: BlockId): Opt[BlockId] =
|
2022-03-20 10:58:59 +00:00
|
|
|
## Wrapper around `parent` for parents known to exist.
|
|
|
|
let parent = dag.parent(bid)
|
|
|
|
if parent.isErr:
|
|
|
|
error "Parent failed to load unexpectedly", bid, tail = dag.tail.slot
|
2022-07-13 13:48:09 +00:00
|
|
|
doAssert strictVerification notin dag.updateFlags
|
2022-03-20 10:58:59 +00:00
|
|
|
parent
|
|
|
|
|
2022-06-14 08:19:11 +00:00
|
|
|
proc getExistingForkedBlock(
|
2022-03-20 10:58:59 +00:00
|
|
|
dag: ChainDAGRef, bid: BlockId): Opt[ForkedTrustedSignedBeaconBlock] =
|
|
|
|
## Wrapper around `getForkedBlock` for blocks expected to exist.
|
|
|
|
let bdata = dag.getForkedBlock(bid)
|
|
|
|
if bdata.isErr:
|
|
|
|
error "Block failed to load unexpectedly", bid, tail = dag.tail.slot
|
2022-07-13 13:48:09 +00:00
|
|
|
doAssert strictVerification notin dag.updateFlags
|
2022-03-20 10:58:59 +00:00
|
|
|
bdata
|
|
|
|
|
2022-06-07 17:01:11 +00:00
|
|
|
proc existingCurrentSyncCommitteeForPeriod(
|
2022-03-11 20:28:10 +00:00
|
|
|
dag: ChainDAGRef,
|
2022-03-16 07:20:40 +00:00
|
|
|
tmpState: var ForkedHashedBeaconState,
|
2022-03-20 10:58:59 +00:00
|
|
|
period: SyncCommitteePeriod): Opt[SyncCommittee] =
|
2022-06-07 17:01:11 +00:00
|
|
|
## Wrapper around `currentSyncCommitteeForPeriod` for states known to exist.
|
|
|
|
let syncCommittee = dag.currentSyncCommitteeForPeriod(tmpState, period)
|
|
|
|
if syncCommittee.isErr:
|
|
|
|
error "Current sync committee failed to load unexpectedly",
|
|
|
|
period, tail = dag.tail.slot
|
2022-07-13 13:48:09 +00:00
|
|
|
doAssert strictVerification notin dag.updateFlags
|
2022-06-07 17:01:11 +00:00
|
|
|
syncCommittee
|
2022-03-11 20:28:10 +00:00
|
|
|
|
|
|
|
template syncCommitteeRoot(
|
|
|
|
state: HashedBeaconStateWithSyncCommittee): Eth2Digest =
|
|
|
|
## Compute a root to uniquely identify `current_sync_committee` and
|
|
|
|
## `next_sync_committee`.
|
|
|
|
withEth2Hash:
|
|
|
|
h.update state.data.current_sync_committee.hash_tree_root().data
|
|
|
|
h.update state.data.next_sync_committee.hash_tree_root().data
|
|
|
|
|
|
|
|
proc syncCommitteeRootForPeriod(
|
|
|
|
dag: ChainDAGRef,
|
2022-03-16 07:20:40 +00:00
|
|
|
tmpState: var ForkedHashedBeaconState,
|
2022-03-20 10:58:59 +00:00
|
|
|
period: SyncCommitteePeriod): Opt[Eth2Digest] =
|
2022-03-11 20:28:10 +00:00
|
|
|
## Compute a root to uniquely identify `current_sync_committee` and
|
|
|
|
## `next_sync_committee` for a given sync committee period.
|
|
|
|
## For non-finalized periods, follow the chain as selected by fork choice.
|
2022-06-14 20:02:03 +00:00
|
|
|
let lowSlot = max(dag.tail.slot, dag.cfg.ALTAIR_FORK_EPOCH.start_slot)
|
|
|
|
if period < lowSlot.sync_committee_period:
|
|
|
|
return err()
|
2022-03-11 20:28:10 +00:00
|
|
|
let
|
|
|
|
periodStartSlot = period.start_slot
|
2022-06-14 20:02:03 +00:00
|
|
|
syncCommitteeSlot = max(periodStartSlot, lowSlot)
|
2022-03-20 10:58:59 +00:00
|
|
|
bsi = ? dag.getExistingBlockIdAtSlot(syncCommitteeSlot)
|
|
|
|
dag.withUpdatedExistingState(tmpState, bsi) do:
|
2022-03-16 07:20:40 +00:00
|
|
|
withState(state):
|
2022-11-02 16:23:30 +00:00
|
|
|
when stateFork >= BeaconStateFork.Capella:
|
|
|
|
raiseAssert $capellaImplementationMissing
|
|
|
|
elif stateFork >= BeaconStateFork.Altair:
|
2022-08-26 22:47:40 +00:00
|
|
|
ok forkyState.syncCommitteeRoot
|
2022-03-11 20:28:10 +00:00
|
|
|
else: raiseAssert "Unreachable"
|
2022-03-20 10:58:59 +00:00
|
|
|
do: err()
|
2022-03-11 20:28:10 +00:00
|
|
|
|
2022-06-28 20:52:29 +00:00
|
|
|
proc initLightClientDataStore*(
|
2022-07-04 20:46:32 +00:00
|
|
|
config: LightClientDataConfig,
|
|
|
|
cfg: RuntimeConfig,
|
|
|
|
db: LightClientDataDB): LightClientDataStore =
|
2022-06-27 11:24:38 +00:00
|
|
|
## Initialize light client data store.
|
2022-08-21 09:24:59 +00:00
|
|
|
let
|
|
|
|
defaultMaxPeriods = cfg.defaultLightClientDataMaxPeriods
|
|
|
|
maxPeriods = config.maxPeriods.get(distinctBase(SyncCommitteePeriod.high))
|
|
|
|
if maxPeriods < defaultMaxPeriods:
|
|
|
|
warn "Retaining fewer periods than recommended",
|
|
|
|
lightClientDataMaxPeriods = config.maxPeriods,
|
|
|
|
specRecommendation = defaultMaxPeriods
|
|
|
|
|
2022-07-04 20:46:32 +00:00
|
|
|
LightClientDataStore(
|
|
|
|
db: db,
|
2022-06-28 20:52:29 +00:00
|
|
|
serve: config.serve,
|
|
|
|
importMode: config.importMode,
|
2022-08-21 09:24:59 +00:00
|
|
|
maxPeriods: maxPeriods,
|
2022-06-28 20:52:29 +00:00
|
|
|
onLightClientFinalityUpdate: config.onLightClientFinalityUpdate,
|
|
|
|
onLightClientOptimisticUpdate: config.onLightClientOptimisticUpdate)
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
func targetLightClientTailSlot(dag: ChainDAGRef): Slot =
|
2022-06-28 05:52:23 +00:00
|
|
|
## Earliest slot for which light client data is retained.
|
|
|
|
let
|
|
|
|
maxPeriods = dag.lcDataStore.maxPeriods
|
|
|
|
headPeriod = dag.head.slot.sync_committee_period
|
|
|
|
lowSlot = max(dag.tail.slot, dag.cfg.ALTAIR_FORK_EPOCH.start_slot)
|
|
|
|
tail = max(headPeriod + 1, maxPeriods.SyncCommitteePeriod) - maxPeriods
|
|
|
|
max(tail.start_slot, lowSlot)
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
func handleUnexpectedLightClientError(dag: ChainDAGRef, buggedSlot: Slot) =
|
|
|
|
## If there is an unexpected error, adjust `tailSlot` to keep track of the
|
|
|
|
## section for which complete light client data is available, and to avoid
|
|
|
|
## failed lookups of cached light client data.
|
2022-07-13 13:48:09 +00:00
|
|
|
doAssert strictVerification notin dag.updateFlags
|
2022-06-27 11:24:38 +00:00
|
|
|
if buggedSlot >= dag.lcDataStore.cache.tailSlot:
|
|
|
|
dag.lcDataStore.cache.tailSlot = buggedSlot + 1
|
|
|
|
|
|
|
|
proc initLightClientBootstrapForPeriod(
|
|
|
|
dag: ChainDAGRef,
|
2022-06-30 13:04:39 +00:00
|
|
|
period: SyncCommitteePeriod): Opt[void] =
|
2022-06-27 11:24:38 +00:00
|
|
|
## Compute and cache `LightClientBootstrap` data for all finalized
|
|
|
|
## epoch boundary blocks within a given sync committee period.
|
|
|
|
if not dag.isNextSyncCommitteeFinalized(period):
|
2022-06-30 13:04:39 +00:00
|
|
|
return ok()
|
|
|
|
if dag.lcDataStore.db.isPeriodSealed(period):
|
|
|
|
return ok()
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
let startTick = Moment.now()
|
|
|
|
debug "Caching historic LC bootstrap data", period
|
|
|
|
defer:
|
|
|
|
let endTick = Moment.now()
|
|
|
|
debug "Historic LC bootstrap data cached", period,
|
|
|
|
cacheDur = endTick - startTick
|
|
|
|
|
|
|
|
let
|
|
|
|
periodStartSlot = period.start_slot
|
|
|
|
periodEndSlot = periodStartSlot + SLOTS_PER_SYNC_COMMITTEE_PERIOD - 1
|
|
|
|
lowSlot = max(periodStartSlot, dag.targetLightClientTailSlot)
|
|
|
|
highSlot = min(periodEndSlot, dag.finalizedHead.blck.slot)
|
|
|
|
lowBoundarySlot = lowSlot.nextEpochBoundarySlot
|
|
|
|
highBoundarySlot = highSlot.nextEpochBoundarySlot
|
|
|
|
var
|
2022-06-30 13:04:39 +00:00
|
|
|
res = ok()
|
2022-06-27 11:24:38 +00:00
|
|
|
tmpState = assignClone(dag.headState)
|
|
|
|
tmpCache: StateCache
|
|
|
|
nextBoundarySlot = lowBoundarySlot
|
|
|
|
while nextBoundarySlot <= highBoundarySlot:
|
|
|
|
defer: nextBoundarySlot += SLOTS_PER_EPOCH
|
|
|
|
let
|
|
|
|
bsi = dag.getExistingBlockIdAtSlot(nextBoundarySlot).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(nextBoundarySlot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
continue
|
|
|
|
bid = bsi.bid
|
|
|
|
boundarySlot = bid.slot.nextEpochBoundarySlot
|
|
|
|
if boundarySlot == nextBoundarySlot and bid.slot >= lowSlot and
|
2022-06-30 13:04:39 +00:00
|
|
|
not dag.lcDataStore.db.hasCurrentSyncCommitteeBranch(bid.slot):
|
2022-06-27 11:24:38 +00:00
|
|
|
if not dag.updateExistingState(
|
|
|
|
tmpState[], bid.atSlot, save = false, tmpCache):
|
|
|
|
dag.handleUnexpectedLightClientError(bid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
continue
|
2022-06-28 05:52:23 +00:00
|
|
|
let branch = withState(tmpState[]):
|
2022-06-27 11:24:38 +00:00
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
2022-08-26 22:47:40 +00:00
|
|
|
forkyState.data.build_proof(altair.CURRENT_SYNC_COMMITTEE_INDEX).get
|
2022-06-27 11:24:38 +00:00
|
|
|
else: raiseAssert "Unreachable"
|
2022-06-30 13:04:39 +00:00
|
|
|
dag.lcDataStore.db.putCurrentSyncCommitteeBranch(bid.slot, branch)
|
|
|
|
res
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
proc initLightClientUpdateForPeriod(
|
2022-06-30 13:04:39 +00:00
|
|
|
dag: ChainDAGRef, period: SyncCommitteePeriod): Opt[void] =
|
2022-06-27 11:24:38 +00:00
|
|
|
## Compute and cache the best `LightClientUpdate` within a given
|
|
|
|
## sync committee period up through the finalized head block.
|
2022-06-30 13:04:39 +00:00
|
|
|
## Non-finalized blocks are processed incrementally by other functions.
|
2022-06-27 11:24:38 +00:00
|
|
|
if not dag.isNextSyncCommitteeFinalized(period):
|
2022-06-30 13:04:39 +00:00
|
|
|
return ok()
|
|
|
|
if dag.lcDataStore.db.isPeriodSealed(period):
|
|
|
|
return ok()
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
let startTick = Moment.now()
|
|
|
|
debug "Computing best historic LC update", period
|
|
|
|
proc logBest(endTick = Moment.now()) =
|
|
|
|
# Using a helper function reduces code size as the `defer` beneath is
|
|
|
|
# replicated on every `return`, and the log statement allocates another
|
|
|
|
# copy of the arguments on the stack for each instantiation (~1 MB stack!)
|
|
|
|
debug "Best historic LC update computed",
|
2022-06-30 13:04:39 +00:00
|
|
|
period, update = dag.lcDataStore.db.getBestUpdate(period),
|
2022-06-27 11:24:38 +00:00
|
|
|
computeDur = endTick - startTick
|
|
|
|
defer: logBest()
|
|
|
|
|
|
|
|
proc maxParticipantsBlock(
|
|
|
|
dag: ChainDAGRef, highBid: BlockId, lowSlot: Slot
|
2022-06-30 13:04:39 +00:00
|
|
|
): tuple[bid: Opt[BlockId], res: Opt[void]] =
|
2022-06-27 11:24:38 +00:00
|
|
|
## Determine the earliest block with most sync committee signatures among
|
|
|
|
## ancestors of `highBid` with at least `lowSlot` as parent block slot.
|
|
|
|
## Return `err` if no block with `MIN_SYNC_COMMITTEE_PARTICIPANTS` exists.
|
2022-06-30 13:04:39 +00:00
|
|
|
## `res` in result indicates whether no unexpected errors occurred.
|
2022-06-27 11:24:38 +00:00
|
|
|
var
|
|
|
|
maxParticipants = MIN_SYNC_COMMITTEE_PARTICIPANTS
|
|
|
|
maxBid: Opt[BlockId]
|
2022-06-30 13:04:39 +00:00
|
|
|
res = Opt[void].ok()
|
2022-06-27 11:24:38 +00:00
|
|
|
bid = highBid
|
|
|
|
while true:
|
|
|
|
if bid.slot <= lowSlot:
|
|
|
|
break
|
|
|
|
let parentBid = dag.existingParent(bid).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(bid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
break
|
|
|
|
if parentBid.slot < lowSlot:
|
|
|
|
break
|
|
|
|
let
|
|
|
|
bdata = dag.getExistingForkedBlock(bid).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(bid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
break
|
|
|
|
numParticipants =
|
|
|
|
withBlck(bdata):
|
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
|
|
|
countOnes(blck.message.body.sync_aggregate.sync_committee_bits)
|
|
|
|
else: raiseAssert "Unreachable"
|
|
|
|
if numParticipants >= maxParticipants:
|
|
|
|
maxParticipants = numParticipants
|
|
|
|
maxBid.ok bid
|
|
|
|
bid = parentBid
|
2022-06-30 13:04:39 +00:00
|
|
|
(bid: maxBid, res: res)
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
# Determine the block in the period with highest sync committee participation
|
|
|
|
let
|
|
|
|
periodStartSlot = period.start_slot
|
|
|
|
periodEndSlot = periodStartSlot + SLOTS_PER_SYNC_COMMITTEE_PERIOD - 1
|
|
|
|
lowSlot = max(periodStartSlot, dag.targetLightClientTailSlot)
|
|
|
|
highSlot = min(periodEndSlot, dag.finalizedHead.blck.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
fullPeriodCovered = (dag.finalizedHead.slot > periodEndSlot)
|
2022-06-27 11:24:38 +00:00
|
|
|
highBsi = dag.getExistingBlockIdAtSlot(highSlot).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(highSlot)
|
2022-06-30 13:04:39 +00:00
|
|
|
return err()
|
2022-06-27 11:24:38 +00:00
|
|
|
highBid = highBsi.bid
|
|
|
|
maxParticipantsRes = dag.maxParticipantsBlock(highBid, lowSlot)
|
|
|
|
maxParticipantsBid = maxParticipantsRes.bid.valueOr:
|
2022-06-30 13:04:39 +00:00
|
|
|
const update = default(altair.LightClientUpdate)
|
|
|
|
if fullPeriodCovered and maxParticipantsRes.res.isOk: # No block in period
|
|
|
|
dag.lcDataStore.db.putBestUpdate(period, update)
|
|
|
|
else:
|
|
|
|
dag.lcDataStore.db.putUpdateIfBetter(period, update)
|
|
|
|
return maxParticipantsRes.res
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
# The block with highest participation may refer to a `finalized_checkpoint`
|
|
|
|
# in a different sync committee period. If that is the case, search for a
|
|
|
|
# later block with a `finalized_checkpoint` within the given sync committee
|
|
|
|
# period, despite it having a lower sync committee participation
|
|
|
|
var
|
2022-06-30 13:04:39 +00:00
|
|
|
res = ok()
|
2022-06-27 11:24:38 +00:00
|
|
|
tmpState = assignClone(dag.headState)
|
|
|
|
signatureBid {.noinit.}, finalizedBid {.noinit.}: BlockId
|
|
|
|
signatureBid.slot = FAR_FUTURE_SLOT
|
|
|
|
finalizedBid.slot = FAR_FUTURE_SLOT
|
|
|
|
while true:
|
|
|
|
if signatureBid.slot == FAR_FUTURE_SLOT:
|
|
|
|
signatureBid = maxParticipantsBid
|
|
|
|
else:
|
2022-09-29 05:55:04 +00:00
|
|
|
let signatureRes = dag.maxParticipantsBlock(highBid, signatureBid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
if signatureRes.res.isErr:
|
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
signatureBid = signatureRes.bid.valueOr:
|
|
|
|
signatureBid = maxParticipantsBid
|
|
|
|
break
|
|
|
|
let
|
|
|
|
attestedBid = dag.existingParent(signatureBid).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(signatureBid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
continue
|
|
|
|
finalizedEpoch = block:
|
|
|
|
dag.withUpdatedExistingState(tmpState[], attestedBid.atSlot) do:
|
|
|
|
withState(state):
|
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
2022-08-26 22:47:40 +00:00
|
|
|
forkyState.data.finalized_checkpoint.epoch
|
2022-06-27 11:24:38 +00:00
|
|
|
else: raiseAssert "Unreachable"
|
|
|
|
do:
|
|
|
|
dag.handleUnexpectedLightClientError(attestedBid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
continue
|
|
|
|
finalizedSlot = finalizedEpoch.start_slot
|
|
|
|
finalizedBsi =
|
|
|
|
if finalizedSlot >= dag.tail.slot:
|
|
|
|
dag.getExistingBlockIdAtSlot(finalizedSlot).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(finalizedSlot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
continue
|
|
|
|
else:
|
|
|
|
continue
|
2022-09-29 05:55:04 +00:00
|
|
|
if finalizedBsi.bid.slot >= lowSlot:
|
2022-06-27 11:24:38 +00:00
|
|
|
finalizedBid = finalizedBsi.bid
|
|
|
|
break
|
|
|
|
if signatureBid == maxParticipantsBid:
|
|
|
|
finalizedBid = finalizedBsi.bid # For fallback `break` at start of loop
|
|
|
|
|
|
|
|
# Save best light client data for given period
|
|
|
|
var update {.noinit.}: altair.LightClientUpdate
|
|
|
|
let attestedBid = dag.existingParent(signatureBid).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(signatureBid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
return err()
|
2022-06-27 11:24:38 +00:00
|
|
|
dag.withUpdatedExistingState(tmpState[], attestedBid.atSlot) do:
|
|
|
|
let bdata = dag.getExistingForkedBlock(bid).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(bid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
return err()
|
2022-06-27 11:24:38 +00:00
|
|
|
withStateAndBlck(state, bdata):
|
2022-11-02 16:23:30 +00:00
|
|
|
when stateFork >= BeaconStateFork.Capella:
|
|
|
|
raiseAssert $capellaImplementationMissing
|
|
|
|
elif stateFork >= BeaconStateFork.Altair:
|
2022-06-27 11:24:38 +00:00
|
|
|
update.attested_header = blck.toBeaconBlockHeader()
|
2022-10-03 13:08:50 +00:00
|
|
|
update.next_sync_committee = forkyState.data.next_sync_committee
|
2022-06-27 11:24:38 +00:00
|
|
|
update.next_sync_committee_branch =
|
2022-10-03 13:08:50 +00:00
|
|
|
forkyState.data.build_proof(altair.NEXT_SYNC_COMMITTEE_INDEX).get
|
2022-06-27 11:24:38 +00:00
|
|
|
if finalizedBid.slot == FAR_FUTURE_SLOT:
|
|
|
|
update.finality_branch.reset()
|
|
|
|
else:
|
|
|
|
update.finality_branch =
|
2022-10-03 13:08:50 +00:00
|
|
|
forkyState.data.build_proof(altair.FINALIZED_ROOT_INDEX).get
|
2022-06-27 11:24:38 +00:00
|
|
|
else: raiseAssert "Unreachable"
|
|
|
|
do:
|
|
|
|
dag.handleUnexpectedLightClientError(attestedBid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
return err()
|
2022-06-27 11:24:38 +00:00
|
|
|
if finalizedBid.slot == FAR_FUTURE_SLOT or finalizedBid.slot == GENESIS_SLOT:
|
|
|
|
update.finalized_header.reset()
|
|
|
|
else:
|
|
|
|
let bdata = dag.getExistingForkedBlock(finalizedBid).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(finalizedBid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
return err()
|
2022-06-27 11:24:38 +00:00
|
|
|
withBlck(bdata):
|
|
|
|
update.finalized_header = blck.toBeaconBlockHeader()
|
|
|
|
let bdata = dag.getExistingForkedBlock(signatureBid).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(signatureBid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
return err()
|
2022-06-27 11:24:38 +00:00
|
|
|
withBlck(bdata):
|
2022-11-02 16:23:30 +00:00
|
|
|
when stateFork >= BeaconStateFork.Capella:
|
|
|
|
raiseAssert $capellaImplementationMissing
|
|
|
|
elif stateFork >= BeaconStateFork.Altair:
|
2022-06-27 11:24:38 +00:00
|
|
|
update.sync_aggregate = blck.asSigned().message.body.sync_aggregate
|
|
|
|
else: raiseAssert "Unreachable"
|
|
|
|
update.signature_slot = signatureBid.slot
|
2022-06-30 13:04:39 +00:00
|
|
|
|
|
|
|
if fullPeriodCovered and res.isOk:
|
|
|
|
dag.lcDataStore.db.putBestUpdate(period, update)
|
|
|
|
else:
|
|
|
|
dag.lcDataStore.db.putUpdateIfBetter(period, update)
|
|
|
|
res
|
|
|
|
|
|
|
|
proc initLightClientDataForPeriod(
|
|
|
|
dag: ChainDAGRef, period: SyncCommitteePeriod): Opt[void] =
|
|
|
|
## Import light client data for a given sync committee period.
|
|
|
|
if dag.lcDataStore.db.isPeriodSealed(period):
|
|
|
|
return ok()
|
|
|
|
let
|
|
|
|
fullPeriodCovered = (dag.finalizedHead.slot >= (period + 1).start_slot)
|
|
|
|
res1 = dag.initLightClientBootstrapForPeriod(period)
|
|
|
|
res2 = dag.initLightClientUpdateForPeriod(period)
|
|
|
|
if res1.isErr or res2.isErr:
|
|
|
|
return err()
|
|
|
|
if fullPeriodCovered:
|
|
|
|
dag.lcDataStore.db.sealPeriod(period)
|
|
|
|
ok()
|
2022-06-27 11:24:38 +00:00
|
|
|
|
2022-03-11 20:28:10 +00:00
|
|
|
proc getLightClientData(
|
|
|
|
dag: ChainDAGRef,
|
|
|
|
bid: BlockId): CachedLightClientData =
|
|
|
|
## Fetch cached light client data about a given block.
|
|
|
|
## Data must be cached (`cacheLightClientData`) before calling this function.
|
2022-06-24 14:57:50 +00:00
|
|
|
try: dag.lcDataStore.cache.data[bid]
|
2022-03-11 20:28:10 +00:00
|
|
|
except KeyError: raiseAssert "Unreachable"
|
|
|
|
|
2022-06-14 20:13:00 +00:00
|
|
|
proc cacheLightClientData(
|
|
|
|
dag: ChainDAGRef, state: HashedBeaconStateWithSyncCommittee, bid: BlockId) =
|
2022-03-11 20:28:10 +00:00
|
|
|
## Cache data for a given block and its post-state to speed up creating future
|
|
|
|
## `LightClientUpdate` and `LightClientBootstrap` instances that refer to this
|
|
|
|
## block and state.
|
2022-06-26 17:33:06 +00:00
|
|
|
let cachedData = CachedLightClientData(
|
|
|
|
current_sync_committee_branch:
|
|
|
|
state.data.build_proof(altair.CURRENT_SYNC_COMMITTEE_INDEX).get,
|
|
|
|
next_sync_committee_branch:
|
|
|
|
state.data.build_proof(altair.NEXT_SYNC_COMMITTEE_INDEX).get,
|
|
|
|
finalized_slot:
|
|
|
|
state.data.finalized_checkpoint.epoch.start_slot,
|
|
|
|
finality_branch:
|
|
|
|
state.data.build_proof(altair.FINALIZED_ROOT_INDEX).get)
|
2022-06-24 14:57:50 +00:00
|
|
|
if dag.lcDataStore.cache.data.hasKeyOrPut(bid, cachedData):
|
2022-03-11 20:28:10 +00:00
|
|
|
doAssert false, "Redundant `cacheLightClientData` call"
|
|
|
|
|
|
|
|
proc deleteLightClientData*(dag: ChainDAGRef, bid: BlockId) =
|
|
|
|
## Delete cached light client data for a given block. This needs to be called
|
|
|
|
## when a block becomes unreachable due to finalization of a different fork.
|
2022-06-24 14:57:50 +00:00
|
|
|
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
|
2022-03-11 20:28:10 +00:00
|
|
|
return
|
|
|
|
|
2022-06-24 14:57:50 +00:00
|
|
|
dag.lcDataStore.cache.data.del bid
|
2022-03-11 20:28:10 +00:00
|
|
|
|
|
|
|
template lazy_header(name: untyped): untyped {.dirty.} =
|
|
|
|
## `createLightClientUpdates` helper to lazily load a known block header.
|
2022-06-19 05:53:10 +00:00
|
|
|
var
|
|
|
|
`name _ ptr`: ptr[BeaconBlockHeader]
|
|
|
|
`name _ ok` = true
|
|
|
|
template `assign _ name`(target: var BeaconBlockHeader, bid: BlockId): bool =
|
|
|
|
if `name _ ptr` != nil:
|
|
|
|
target = `name _ ptr`[]
|
|
|
|
elif `name _ ok`:
|
2022-05-23 12:02:54 +00:00
|
|
|
let bdata = dag.getExistingForkedBlock(bid)
|
2022-06-19 05:53:10 +00:00
|
|
|
if bdata.isErr:
|
|
|
|
dag.handleUnexpectedLightClientError(bid.slot)
|
|
|
|
`name _ ok` = false
|
|
|
|
else:
|
2022-06-14 15:31:10 +00:00
|
|
|
target = bdata.get.toBeaconBlockHeader()
|
2022-06-19 05:53:10 +00:00
|
|
|
`name _ ptr` = addr target
|
|
|
|
`name _ ok`
|
2022-03-11 20:28:10 +00:00
|
|
|
|
|
|
|
template lazy_data(name: untyped): untyped {.dirty.} =
|
|
|
|
## `createLightClientUpdates` helper to lazily load cached light client state.
|
|
|
|
var `name` {.noinit.}: CachedLightClientData
|
2022-05-23 12:02:54 +00:00
|
|
|
`name`.finalized_slot = FAR_FUTURE_SLOT
|
|
|
|
template `load _ name`(bid: BlockId) =
|
|
|
|
if `name`.finalized_slot == FAR_FUTURE_SLOT:
|
2022-03-11 20:28:10 +00:00
|
|
|
`name` = dag.getLightClientData(bid)
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
template lazy_bid(name: untyped): untyped {.dirty.} =
|
2022-06-19 05:53:10 +00:00
|
|
|
## `createLightClientUpdates` helper to lazily load a known to exist block id.
|
2022-05-23 12:02:54 +00:00
|
|
|
var
|
2022-06-19 05:53:10 +00:00
|
|
|
`name` {.noinit.}: BlockId
|
2022-05-23 12:02:54 +00:00
|
|
|
`name _ ok` = true
|
2022-06-19 05:53:10 +00:00
|
|
|
`name`.slot = FAR_FUTURE_SLOT
|
2022-05-23 12:02:54 +00:00
|
|
|
template `load _ name`(slot: Slot): bool =
|
|
|
|
if `name _ ok` and `name`.slot == FAR_FUTURE_SLOT:
|
2022-06-19 05:53:10 +00:00
|
|
|
let bsi = dag.getExistingBlockIdAtSlot(slot)
|
2022-05-23 12:02:54 +00:00
|
|
|
if bsi.isErr:
|
2022-06-19 05:53:10 +00:00
|
|
|
dag.handleUnexpectedLightClientError(slot)
|
2022-05-23 12:02:54 +00:00
|
|
|
`name _ ok` = false
|
|
|
|
else:
|
|
|
|
`name` = bsi.get.bid
|
|
|
|
`name _ ok`
|
|
|
|
|
2022-03-11 20:28:10 +00:00
|
|
|
proc createLightClientUpdates(
|
|
|
|
dag: ChainDAGRef,
|
|
|
|
state: HashedBeaconStateWithSyncCommittee,
|
|
|
|
blck: TrustedSignedBeaconBlockWithSyncAggregate,
|
2022-03-20 10:58:59 +00:00
|
|
|
parent_bid: BlockId) =
|
2022-05-23 12:02:54 +00:00
|
|
|
## Create `LightClientUpdate` instances for a given block and its post-state,
|
|
|
|
## and keep track of best / latest ones. Data about the parent block's
|
|
|
|
## post-state must be cached (`cacheLightClientData`) before calling this.
|
2022-03-11 20:28:10 +00:00
|
|
|
|
|
|
|
# Verify sync committee has sufficient participants
|
2022-06-14 21:33:18 +00:00
|
|
|
template sync_aggregate(): auto = blck.asSigned().message.body.sync_aggregate
|
2022-03-11 20:28:10 +00:00
|
|
|
template sync_committee_bits(): auto = sync_aggregate.sync_committee_bits
|
|
|
|
let num_active_participants = countOnes(sync_committee_bits).uint64
|
|
|
|
if num_active_participants < MIN_SYNC_COMMITTEE_PARTICIPANTS:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Verify attested block (parent) is recent enough and that state is available
|
2022-05-23 12:02:54 +00:00
|
|
|
template attested_bid(): auto = parent_bid
|
2022-06-27 11:24:38 +00:00
|
|
|
let attested_slot = attested_bid.slot
|
|
|
|
if attested_slot < dag.lcDataStore.cache.tailSlot:
|
2022-03-11 20:28:10 +00:00
|
|
|
return
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
# Lazy variables to hold historic data
|
2022-03-11 20:28:10 +00:00
|
|
|
lazy_header(attested_header)
|
|
|
|
lazy_data(attested_data)
|
2022-05-23 12:02:54 +00:00
|
|
|
lazy_bid(finalized_bid)
|
2022-03-11 20:28:10 +00:00
|
|
|
lazy_header(finalized_header)
|
2022-05-23 12:02:54 +00:00
|
|
|
|
|
|
|
# Update latest light client data
|
2022-06-24 14:57:50 +00:00
|
|
|
template latest(): auto = dag.lcDataStore.cache.latest
|
2022-05-23 12:02:54 +00:00
|
|
|
var
|
|
|
|
newFinality = false
|
|
|
|
newOptimistic = false
|
|
|
|
let
|
|
|
|
signature_slot = blck.message.slot
|
|
|
|
is_later =
|
|
|
|
if attested_slot != latest.attested_header.slot:
|
|
|
|
attested_slot > latest.attested_header.slot
|
2022-03-11 20:28:10 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
signature_slot > latest.signature_slot
|
|
|
|
if is_later and latest.attested_header.assign_attested_header(attested_bid):
|
|
|
|
load_attested_data(attested_bid)
|
|
|
|
let finalized_slot = attested_data.finalized_slot
|
|
|
|
if finalized_slot == latest.finalized_header.slot:
|
|
|
|
latest.finality_branch = attested_data.finality_branch
|
|
|
|
elif finalized_slot == GENESIS_SLOT:
|
|
|
|
latest.finalized_header.reset()
|
|
|
|
latest.finality_branch = attested_data.finality_branch
|
2022-06-19 05:53:10 +00:00
|
|
|
elif finalized_slot >= dag.tail.slot and
|
|
|
|
load_finalized_bid(finalized_slot) and
|
2022-05-23 12:02:54 +00:00
|
|
|
latest.finalized_header.assign_finalized_header(finalized_bid):
|
|
|
|
latest.finality_branch = attested_data.finality_branch
|
|
|
|
newFinality = true
|
2022-03-11 20:28:10 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
latest.finalized_header.reset()
|
|
|
|
latest.finality_branch.reset()
|
2022-06-14 21:33:18 +00:00
|
|
|
latest.sync_aggregate = sync_aggregate
|
2022-05-23 12:02:54 +00:00
|
|
|
latest.signature_slot = signature_slot
|
|
|
|
newOptimistic = true
|
|
|
|
|
|
|
|
# Track best light client data for current period
|
|
|
|
let
|
|
|
|
attested_period = attested_slot.sync_committee_period
|
|
|
|
signature_period = signature_slot.sync_committee_period
|
|
|
|
if attested_period == signature_period:
|
|
|
|
template next_sync_committee(): auto = state.data.next_sync_committee
|
|
|
|
|
2022-06-14 15:31:10 +00:00
|
|
|
let isCommitteeFinalized = dag.isNextSyncCommitteeFinalized(attested_period)
|
2022-05-23 12:02:54 +00:00
|
|
|
var best =
|
2022-06-14 15:31:10 +00:00
|
|
|
if isCommitteeFinalized:
|
2022-06-30 13:04:39 +00:00
|
|
|
dag.lcDataStore.db.getBestUpdate(attested_period)
|
2022-03-11 20:28:10 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
let key = (attested_period, state.syncCommitteeRoot)
|
2022-06-24 14:57:50 +00:00
|
|
|
dag.lcDataStore.cache.pendingBest.getOrDefault(key)
|
2022-05-23 12:02:54 +00:00
|
|
|
|
|
|
|
load_attested_data(attested_bid)
|
|
|
|
let
|
|
|
|
finalized_slot = attested_data.finalized_slot
|
2022-06-19 05:53:10 +00:00
|
|
|
has_finality =
|
|
|
|
finalized_slot >= dag.tail.slot and load_finalized_bid(finalized_slot)
|
2022-05-23 12:02:54 +00:00
|
|
|
meta = LightClientUpdateMetadata(
|
|
|
|
attested_slot: attested_slot,
|
|
|
|
finalized_slot: finalized_slot,
|
|
|
|
signature_slot: signature_slot,
|
|
|
|
has_sync_committee: true,
|
2022-06-19 05:53:10 +00:00
|
|
|
has_finality: has_finality,
|
2022-05-23 12:02:54 +00:00
|
|
|
num_active_participants: num_active_participants)
|
|
|
|
is_better = is_better_data(meta, best.toMeta)
|
|
|
|
if is_better and best.attested_header.assign_attested_header(attested_bid):
|
|
|
|
best.next_sync_committee = next_sync_committee
|
|
|
|
best.next_sync_committee_branch = attested_data.next_sync_committee_branch
|
|
|
|
if finalized_slot == best.finalized_header.slot:
|
|
|
|
best.finality_branch = attested_data.finality_branch
|
|
|
|
elif finalized_slot == GENESIS_SLOT:
|
|
|
|
best.finalized_header.reset()
|
|
|
|
best.finality_branch = attested_data.finality_branch
|
2022-06-19 05:53:10 +00:00
|
|
|
elif has_finality and
|
2022-05-23 12:02:54 +00:00
|
|
|
best.finalized_header.assign_finalized_header(finalized_bid):
|
|
|
|
best.finality_branch = attested_data.finality_branch
|
2022-03-11 20:28:10 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
best.finalized_header.reset()
|
|
|
|
best.finality_branch.reset()
|
2022-06-14 21:33:18 +00:00
|
|
|
best.sync_aggregate = sync_aggregate
|
2022-05-23 12:02:54 +00:00
|
|
|
best.signature_slot = signature_slot
|
2022-03-11 20:28:10 +00:00
|
|
|
|
2022-06-14 15:31:10 +00:00
|
|
|
if isCommitteeFinalized:
|
2022-06-30 13:04:39 +00:00
|
|
|
dag.lcDataStore.db.putBestUpdate(attested_period, best)
|
2022-06-27 11:24:38 +00:00
|
|
|
debug "Best LC update improved", period = attested_period, update = best
|
2022-03-11 20:28:10 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
let key = (attested_period, state.syncCommitteeRoot)
|
2022-06-24 14:57:50 +00:00
|
|
|
dag.lcDataStore.cache.pendingBest[key] = best
|
2022-06-27 11:24:38 +00:00
|
|
|
debug "Best LC update improved", period = key, update = best
|
2022-05-23 12:02:54 +00:00
|
|
|
|
2022-06-24 14:57:50 +00:00
|
|
|
if newFinality and dag.lcDataStore.onLightClientFinalityUpdate != nil:
|
|
|
|
dag.lcDataStore.onLightClientFinalityUpdate(latest)
|
|
|
|
if newOptimistic and dag.lcDataStore.onLightClientOptimisticUpdate != nil:
|
|
|
|
dag.lcDataStore.onLightClientOptimisticUpdate(latest.toOptimistic)
|
2022-03-11 20:28:10 +00:00
|
|
|
|
2022-06-27 11:24:38 +00:00
|
|
|
proc initLightClientDataCache*(dag: ChainDAGRef) =
|
|
|
|
## Initialize cached light client data
|
2022-06-24 14:57:50 +00:00
|
|
|
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
|
2022-03-11 20:28:10 +00:00
|
|
|
return
|
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
# Prune non-finalized data
|
|
|
|
dag.lcDataStore.db.delPeriodsFrom(dag.firstNonFinalizedPeriod)
|
|
|
|
|
2022-06-27 11:24:38 +00:00
|
|
|
# Initialize tail slot
|
|
|
|
let targetTailSlot = dag.targetLightClientTailSlot
|
|
|
|
dag.lcDataStore.cache.tailSlot = max(dag.head.slot, targetTailSlot)
|
2022-03-11 20:28:10 +00:00
|
|
|
|
2022-06-27 11:24:38 +00:00
|
|
|
# Import head state
|
|
|
|
if dag.head.slot < dag.lcDataStore.cache.tailSlot:
|
2022-03-11 20:28:10 +00:00
|
|
|
return
|
2022-06-27 11:24:38 +00:00
|
|
|
withState(dag.headState):
|
2022-11-02 16:23:30 +00:00
|
|
|
when stateFork >= BeaconStateFork.Capella:
|
|
|
|
raiseAssert $capellaImplementationMissing
|
|
|
|
elif stateFork >= BeaconStateFork.Altair:
|
2022-08-26 22:47:40 +00:00
|
|
|
dag.cacheLightClientData(forkyState, dag.head.bid)
|
2022-06-27 11:24:38 +00:00
|
|
|
else: raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
|
|
|
|
if dag.lcDataStore.importMode == LightClientDataImportMode.OnlyNew:
|
2022-03-11 20:28:10 +00:00
|
|
|
return
|
|
|
|
|
2022-06-27 11:24:38 +00:00
|
|
|
# Import light client data for finalized period through finalized head
|
|
|
|
let finalizedSlot = max(dag.finalizedHead.blck.slot, targetTailSlot)
|
|
|
|
if finalizedSlot >= dag.lcDataStore.cache.tailSlot:
|
|
|
|
return
|
|
|
|
dag.lcDataStore.cache.tailSlot = finalizedSlot
|
|
|
|
let finalizedPeriod = finalizedSlot.sync_committee_period
|
2022-06-30 13:04:39 +00:00
|
|
|
var res = dag.initLightClientDataForPeriod(finalizedPeriod)
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
let lightClientStartTick = Moment.now()
|
|
|
|
logScope: lightClientDataMaxPeriods = dag.lcDataStore.maxPeriods
|
2022-06-30 13:04:39 +00:00
|
|
|
debug "Initializing cached LC data", res
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
# Build list of block to process.
|
|
|
|
# As it is slow to load states in descending order,
|
|
|
|
# build a reverse todo list to then process them in ascending order
|
|
|
|
var
|
|
|
|
blocks = newSeqOfCap[BlockId](dag.head.slot - finalizedSlot + 1)
|
|
|
|
bid = dag.head.bid
|
|
|
|
while bid.slot > finalizedSlot:
|
|
|
|
blocks.add bid
|
|
|
|
bid = dag.existingParent(bid).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(bid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
break
|
|
|
|
if bid.slot == finalizedSlot:
|
|
|
|
blocks.add bid
|
|
|
|
|
|
|
|
# Process blocks (reuses `dag.headState`, but restores it to the current head)
|
|
|
|
var
|
|
|
|
tmpState = assignClone(dag.headState)
|
|
|
|
tmpCache, cache: StateCache
|
|
|
|
oldCheckpoint: Checkpoint
|
|
|
|
cpIndex = 0
|
|
|
|
for i in countdown(blocks.high, blocks.low):
|
|
|
|
bid = blocks[i]
|
|
|
|
if not dag.updateExistingState(
|
|
|
|
dag.headState, bid.atSlot, save = false, cache):
|
|
|
|
dag.handleUnexpectedLightClientError(bid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
continue
|
|
|
|
let bdata = dag.getExistingForkedBlock(bid).valueOr:
|
|
|
|
dag.handleUnexpectedLightClientError(bid.slot)
|
2022-06-30 13:04:39 +00:00
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
continue
|
|
|
|
withStateAndBlck(dag.headState, bdata):
|
2022-11-02 16:23:30 +00:00
|
|
|
when stateFork >= BeaconStateFork.Capella:
|
|
|
|
raiseAssert $capellaImplementationMissing
|
|
|
|
elif stateFork >= BeaconStateFork.Altair:
|
2022-06-27 11:24:38 +00:00
|
|
|
# Cache light client data (non-finalized blocks may refer to this)
|
|
|
|
if i != blocks.low:
|
2022-10-03 13:08:50 +00:00
|
|
|
dag.cacheLightClientData(forkyState, bid) # `dag.head` already cached
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
# Create `LightClientUpdate` instances
|
|
|
|
if i < blocks.high:
|
2022-10-03 13:08:50 +00:00
|
|
|
dag.createLightClientUpdates(
|
|
|
|
forkyState, blck, parentBid = blocks[i + 1])
|
2022-06-27 11:24:38 +00:00
|
|
|
else: raiseAssert "Unreachable"
|
|
|
|
|
|
|
|
let lightClientEndTick = Moment.now()
|
|
|
|
debug "Initialized cached LC data",
|
2022-06-30 13:04:39 +00:00
|
|
|
initDur = lightClientEndTick - lightClientStartTick, res
|
|
|
|
if res.isErr:
|
|
|
|
return
|
2022-06-27 11:24:38 +00:00
|
|
|
if dag.lcDataStore.importMode == LightClientDataImportMode.OnDemand:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Import historic data
|
|
|
|
dag.lcDataStore.cache.tailSlot = targetTailSlot
|
|
|
|
let targetTailPeriod = targetTailSlot.sync_committee_period
|
|
|
|
if targetTailPeriod < finalizedPeriod:
|
|
|
|
# `countdown` through 0 fails on distinct `uint64`
|
|
|
|
# https://github.com/nim-lang/Nim/pull/19926
|
|
|
|
var period = finalizedPeriod - 1
|
|
|
|
while period >= targetTailPeriod:
|
2022-06-30 13:04:39 +00:00
|
|
|
if dag.initLightClientDataForPeriod(period).isErr:
|
|
|
|
res.err()
|
2022-06-27 11:24:38 +00:00
|
|
|
if period <= targetTailPeriod:
|
|
|
|
break
|
|
|
|
dec period
|
2022-06-30 13:04:39 +00:00
|
|
|
debug "Historic LC data imported", res
|
2022-06-27 11:24:38 +00:00
|
|
|
|
|
|
|
proc processNewBlockForLightClient*(
|
|
|
|
dag: ChainDAGRef,
|
|
|
|
state: ForkedHashedBeaconState,
|
|
|
|
signedBlock: ForkyTrustedSignedBeaconBlock,
|
|
|
|
parentBid: BlockId) =
|
|
|
|
## Update light client data with information from a new block.
|
|
|
|
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
|
|
|
|
return
|
|
|
|
if signedBlock.message.slot < dag.lcDataStore.cache.tailSlot:
|
|
|
|
return
|
|
|
|
|
2022-11-02 16:23:30 +00:00
|
|
|
when signedBlock is capella.TrustedSignedBeaconBlock:
|
|
|
|
raiseAssert $capellaImplementationMissing
|
|
|
|
elif signedBlock is bellatrix.TrustedSignedBeaconBlock:
|
2022-06-27 11:24:38 +00:00
|
|
|
dag.cacheLightClientData(state.bellatrixData, signedBlock.toBlockId())
|
|
|
|
dag.createLightClientUpdates(state.bellatrixData, signedBlock, parentBid)
|
|
|
|
elif signedBlock is altair.TrustedSignedBeaconBlock:
|
|
|
|
dag.cacheLightClientData(state.altairData, signedBlock.toBlockId())
|
|
|
|
dag.createLightClientUpdates(state.altairData, signedBlock, parentBid)
|
|
|
|
elif signedBlock is phase0.TrustedSignedBeaconBlock:
|
|
|
|
raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
|
|
|
|
else:
|
|
|
|
{.error: "Unreachable".}
|
|
|
|
|
|
|
|
proc processHeadChangeForLightClient*(dag: ChainDAGRef) =
|
|
|
|
## Update light client data to account for a new head block.
|
|
|
|
## Note that `dag.finalizedHead` is not yet updated when this is called.
|
|
|
|
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
|
|
|
|
return
|
|
|
|
if dag.head.slot < dag.lcDataStore.cache.tailSlot:
|
|
|
|
return
|
|
|
|
|
2022-06-28 05:52:23 +00:00
|
|
|
# Update `bestUpdates` from `pendingBest` to ensure light client data
|
2022-06-27 11:24:38 +00:00
|
|
|
# only refers to sync committees as selected by fork choice
|
|
|
|
let headPeriod = dag.head.slot.sync_committee_period
|
|
|
|
if not dag.isNextSyncCommitteeFinalized(headPeriod):
|
2022-06-14 15:31:10 +00:00
|
|
|
let
|
2022-06-27 11:24:38 +00:00
|
|
|
tailPeriod = dag.lcDataStore.cache.tailSlot.sync_committee_period
|
|
|
|
lowPeriod = max(dag.firstNonFinalizedPeriod, tailPeriod)
|
2022-06-14 15:31:10 +00:00
|
|
|
if headPeriod > lowPeriod:
|
2022-03-11 20:28:10 +00:00
|
|
|
var tmpState = assignClone(dag.headState)
|
2022-06-14 15:31:10 +00:00
|
|
|
for period in lowPeriod ..< headPeriod:
|
2022-03-20 10:58:59 +00:00
|
|
|
let
|
|
|
|
syncCommitteeRoot =
|
|
|
|
dag.syncCommitteeRootForPeriod(tmpState[], period).valueOr:
|
2022-06-19 05:53:10 +00:00
|
|
|
dag.handleUnexpectedLightClientError(period.start_slot)
|
2022-03-20 10:58:59 +00:00
|
|
|
continue
|
|
|
|
key = (period, syncCommitteeRoot)
|
2022-06-30 13:04:39 +00:00
|
|
|
dag.lcDataStore.db.putBestUpdate(
|
|
|
|
period, dag.lcDataStore.cache.pendingBest.getOrDefault(key))
|
2022-06-14 15:31:10 +00:00
|
|
|
withState(dag.headState): # Common case separate to avoid `tmpState` copy
|
2022-11-02 16:23:30 +00:00
|
|
|
when stateFork >= BeaconStateFork.Capella:
|
|
|
|
raiseAssert $capellaImplementationMissing
|
|
|
|
elif stateFork >= BeaconStateFork.Altair:
|
2022-08-26 22:47:40 +00:00
|
|
|
let key = (headPeriod, forkyState.syncCommitteeRoot)
|
2022-06-30 13:04:39 +00:00
|
|
|
dag.lcDataStore.db.putBestUpdate(
|
|
|
|
headPeriod, dag.lcDataStore.cache.pendingBest.getOrDefault(key))
|
2022-06-27 11:24:38 +00:00
|
|
|
else: raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
|
2022-03-11 20:28:10 +00:00
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
proc processFinalizationForLightClient*(
|
|
|
|
dag: ChainDAGRef, oldFinalizedHead: BlockSlot) =
|
2022-03-11 20:28:10 +00:00
|
|
|
## Prune cached data that is no longer useful for creating future
|
|
|
|
## `LightClientUpdate` and `LightClientBootstrap` instances.
|
|
|
|
## This needs to be called whenever `finalized_checkpoint` changes.
|
2022-06-24 14:57:50 +00:00
|
|
|
if dag.lcDataStore.importMode == LightClientDataImportMode.None:
|
2022-03-11 20:28:10 +00:00
|
|
|
return
|
2022-06-27 11:24:38 +00:00
|
|
|
let finalizedSlot = dag.finalizedHead.slot
|
|
|
|
if finalizedSlot < dag.lcDataStore.cache.tailSlot:
|
2022-03-11 20:28:10 +00:00
|
|
|
return
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
# Cache `LightClientBootstrap` for newly finalized epoch boundary blocks
|
2022-06-27 11:24:38 +00:00
|
|
|
let
|
|
|
|
firstNewSlot = oldFinalizedHead.slot + 1
|
|
|
|
lowSlot = max(firstNewSlot, dag.lcDataStore.cache.tailSlot)
|
2022-05-23 12:02:54 +00:00
|
|
|
var boundarySlot = finalizedSlot
|
|
|
|
while boundarySlot >= lowSlot:
|
2022-03-11 20:28:10 +00:00
|
|
|
let
|
2022-05-23 12:02:54 +00:00
|
|
|
bsi = dag.getExistingBlockIdAtSlot(boundarySlot).valueOr:
|
2022-06-19 05:53:10 +00:00
|
|
|
dag.handleUnexpectedLightClientError(boundarySlot)
|
2022-03-11 20:28:10 +00:00
|
|
|
break
|
2022-05-23 12:02:54 +00:00
|
|
|
bid = bsi.bid
|
|
|
|
if bid.slot >= lowSlot:
|
2022-06-30 13:04:39 +00:00
|
|
|
dag.lcDataStore.db.putCurrentSyncCommitteeBranch(
|
|
|
|
bid.slot, dag.getLightClientData(bid).current_sync_committee_branch)
|
2022-05-23 12:02:54 +00:00
|
|
|
boundarySlot = bid.slot.nextEpochBoundarySlot
|
|
|
|
if boundarySlot < SLOTS_PER_EPOCH:
|
|
|
|
break
|
|
|
|
boundarySlot -= SLOTS_PER_EPOCH
|
2022-03-11 20:28:10 +00:00
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
# Seal sync committee periods for which data can no longer improve further
|
|
|
|
let
|
|
|
|
oldFinalizedPeriod = oldFinalizedHead.slot.sync_committee_period
|
|
|
|
newFinalizedPeriod = dag.finalizedHead.slot.sync_committee_period
|
|
|
|
if newFinalizedPeriod > oldFinalizedPeriod:
|
|
|
|
for period in countdown(newFinalizedPeriod - 1, oldFinalizedPeriod):
|
|
|
|
if dag.lcDataStore.cache.tailSlot > period.start_slot:
|
|
|
|
break
|
|
|
|
debug "Best LC update sealed",
|
|
|
|
period, update = dag.lcDataStore.db.getBestUpdate(period)
|
|
|
|
dag.lcDataStore.db.sealPeriod(period)
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
# Prune light client data that is no longer referrable by future updates
|
2022-03-11 20:28:10 +00:00
|
|
|
var bidsToDelete: seq[BlockId]
|
2022-06-24 14:57:50 +00:00
|
|
|
for bid, data in dag.lcDataStore.cache.data:
|
2022-06-14 15:31:10 +00:00
|
|
|
if bid.slot >= dag.finalizedHead.blck.slot:
|
2022-05-23 12:02:54 +00:00
|
|
|
continue
|
2022-03-11 20:28:10 +00:00
|
|
|
bidsToDelete.add bid
|
|
|
|
for bid in bidsToDelete:
|
2022-06-24 14:57:50 +00:00
|
|
|
dag.lcDataStore.cache.data.del bid
|
2022-03-11 20:28:10 +00:00
|
|
|
|
2022-06-30 13:04:39 +00:00
|
|
|
# Prune seal tracking data that is no longer relevant
|
|
|
|
let targetTailPeriod = dag.targetLightClientTailSlot.sync_committee_period
|
|
|
|
dag.lcDataStore.db.keepPeriodsFrom(targetTailPeriod)
|
2022-03-11 20:28:10 +00:00
|
|
|
|
|
|
|
# Prune best `LightClientUpdate` referring to non-finalized sync committees
|
|
|
|
# that are no longer relevant, i.e., orphaned or too old
|
2022-06-14 15:31:10 +00:00
|
|
|
let firstNonFinalizedPeriod = dag.firstNonFinalizedPeriod
|
2022-03-11 20:28:10 +00:00
|
|
|
var keysToDelete: seq[(SyncCommitteePeriod, Eth2Digest)]
|
2022-06-24 14:57:50 +00:00
|
|
|
for (period, committeeRoot) in dag.lcDataStore.cache.pendingBest.keys:
|
2022-06-14 15:31:10 +00:00
|
|
|
if period < firstNonFinalizedPeriod:
|
2022-03-11 20:28:10 +00:00
|
|
|
keysToDelete.add (period, committeeRoot)
|
|
|
|
for key in keysToDelete:
|
2022-06-24 14:57:50 +00:00
|
|
|
dag.lcDataStore.cache.pendingBest.del key
|
|
|
|
|
2022-03-11 20:28:10 +00:00
|
|
|
proc getLightClientBootstrap*(
|
|
|
|
dag: ChainDAGRef,
|
2022-03-20 10:58:59 +00:00
|
|
|
blockRoot: Eth2Digest): Opt[altair.LightClientBootstrap] =
|
2022-06-24 14:57:50 +00:00
|
|
|
if not dag.lcDataStore.serve:
|
2022-03-20 10:58:59 +00:00
|
|
|
return err()
|
2022-03-11 20:28:10 +00:00
|
|
|
|
|
|
|
let bdata = dag.getForkedBlock(blockRoot).valueOr:
|
2022-05-23 12:02:54 +00:00
|
|
|
debug "LC bootstrap unavailable: Block not found", blockRoot
|
2022-03-20 10:58:59 +00:00
|
|
|
return err()
|
2022-03-11 20:28:10 +00:00
|
|
|
|
|
|
|
withBlck(bdata):
|
2022-03-20 10:58:59 +00:00
|
|
|
let slot = blck.message.slot
|
2022-03-11 20:28:10 +00:00
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
2022-06-30 13:04:39 +00:00
|
|
|
if slot < dag.targetLightClientTailSlot:
|
2022-05-23 12:02:54 +00:00
|
|
|
debug "LC bootstrap unavailable: Block too old", slot
|
2022-03-20 10:58:59 +00:00
|
|
|
return err()
|
2022-03-11 20:28:10 +00:00
|
|
|
if slot > dag.finalizedHead.blck.slot:
|
2022-05-23 12:02:54 +00:00
|
|
|
debug "LC bootstrap unavailable: Not finalized", blockRoot
|
2022-03-20 10:58:59 +00:00
|
|
|
return err()
|
2022-06-30 13:04:39 +00:00
|
|
|
var branch = dag.lcDataStore.db.getCurrentSyncCommitteeBranch(slot)
|
2022-06-28 05:52:23 +00:00
|
|
|
if branch.isZeroMemory:
|
2022-06-24 14:57:50 +00:00
|
|
|
if dag.lcDataStore.importMode == LightClientDataImportMode.OnDemand:
|
2022-03-20 10:58:59 +00:00
|
|
|
let bsi = ? dag.getExistingBlockIdAtSlot(slot)
|
2022-03-11 20:28:10 +00:00
|
|
|
var tmpState = assignClone(dag.headState)
|
2022-03-20 10:58:59 +00:00
|
|
|
dag.withUpdatedExistingState(tmpState[], bsi) do:
|
2022-06-28 05:52:23 +00:00
|
|
|
branch = withState(state):
|
2022-03-11 20:28:10 +00:00
|
|
|
when stateFork >= BeaconStateFork.Altair:
|
2022-08-26 22:47:40 +00:00
|
|
|
forkyState.data.build_proof(
|
|
|
|
altair.CURRENT_SYNC_COMMITTEE_INDEX).get
|
2022-03-11 20:28:10 +00:00
|
|
|
else: raiseAssert "Unreachable"
|
2022-03-20 10:58:59 +00:00
|
|
|
do: return err()
|
2022-06-30 13:04:39 +00:00
|
|
|
dag.lcDataStore.db.putCurrentSyncCommitteeBranch(slot, branch)
|
2022-03-11 20:28:10 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
debug "LC bootstrap unavailable: Data not cached", slot
|
2022-03-20 10:58:59 +00:00
|
|
|
return err()
|
2022-03-11 20:28:10 +00:00
|
|
|
|
2022-03-20 10:58:59 +00:00
|
|
|
let period = slot.sync_committee_period
|
2022-03-11 20:28:10 +00:00
|
|
|
var tmpState = assignClone(dag.headState)
|
|
|
|
var bootstrap {.noinit.}: altair.LightClientBootstrap
|
|
|
|
bootstrap.header =
|
2022-06-14 15:31:10 +00:00
|
|
|
blck.toBeaconBlockHeader()
|
2022-03-11 20:28:10 +00:00
|
|
|
bootstrap.current_sync_committee =
|
2022-06-07 17:01:11 +00:00
|
|
|
? dag.existingCurrentSyncCommitteeForPeriod(tmpState[], period)
|
2022-03-11 20:28:10 +00:00
|
|
|
bootstrap.current_sync_committee_branch =
|
2022-06-28 05:52:23 +00:00
|
|
|
branch
|
2022-03-20 10:58:59 +00:00
|
|
|
return ok bootstrap
|
2022-03-11 20:28:10 +00:00
|
|
|
else:
|
2022-05-23 12:02:54 +00:00
|
|
|
debug "LC bootstrap unavailable: Block before Altair", slot
|
2022-03-20 10:58:59 +00:00
|
|
|
return err()
|
2022-05-23 12:02:54 +00:00
|
|
|
|
|
|
|
proc getLightClientUpdateForPeriod*(
|
|
|
|
dag: ChainDAGRef,
|
|
|
|
period: SyncCommitteePeriod): Option[altair.LightClientUpdate] =
|
2022-06-24 14:57:50 +00:00
|
|
|
if not dag.lcDataStore.serve:
|
2022-05-23 12:02:54 +00:00
|
|
|
return
|
|
|
|
|
2022-06-24 14:57:50 +00:00
|
|
|
if dag.lcDataStore.importMode == LightClientDataImportMode.OnDemand:
|
2022-06-30 13:04:39 +00:00
|
|
|
if dag.initLightClientUpdateForPeriod(period).isErr:
|
|
|
|
return
|
|
|
|
result = some(dag.lcDataStore.db.getBestUpdate(period))
|
2022-05-23 12:02:54 +00:00
|
|
|
let numParticipants = countOnes(result.get.sync_aggregate.sync_committee_bits)
|
|
|
|
if numParticipants < MIN_SYNC_COMMITTEE_PARTICIPANTS:
|
|
|
|
result.reset()
|
|
|
|
|
|
|
|
proc getLightClientFinalityUpdate*(
|
|
|
|
dag: ChainDAGRef): Option[altair.LightClientFinalityUpdate] =
|
2022-06-24 14:57:50 +00:00
|
|
|
if not dag.lcDataStore.serve:
|
2022-05-23 12:02:54 +00:00
|
|
|
return
|
|
|
|
|
2022-06-24 14:57:50 +00:00
|
|
|
result = some(dag.lcDataStore.cache.latest)
|
2022-05-23 12:02:54 +00:00
|
|
|
let numParticipants = countOnes(result.get.sync_aggregate.sync_committee_bits)
|
|
|
|
if numParticipants < MIN_SYNC_COMMITTEE_PARTICIPANTS:
|
|
|
|
result.reset()
|
|
|
|
|
|
|
|
proc getLightClientOptimisticUpdate*(
|
|
|
|
dag: ChainDAGRef): Option[altair.LightClientOptimisticUpdate] =
|
2022-06-24 14:57:50 +00:00
|
|
|
if not dag.lcDataStore.serve:
|
2022-05-23 12:02:54 +00:00
|
|
|
return
|
|
|
|
|
2022-06-24 14:57:50 +00:00
|
|
|
result = some(dag.lcDataStore.cache.latest.toOptimistic)
|
2022-05-23 12:02:54 +00:00
|
|
|
let numParticipants = countOnes(result.get.sync_aggregate.sync_committee_bits)
|
|
|
|
if numParticipants < MIN_SYNC_COMMITTEE_PARTICIPANTS:
|
|
|
|
result.reset()
|