reduce code duplication when collecting LC data (#5601)

Replace sections that need to be maintained with every `ConsensusFork`
related to LC data collection with a generic logic that keeps working
when unrelated parts of Ethereum change.
This commit is contained in:
Etan Kissling 2023-11-14 18:21:03 -08:00 committed by GitHub
parent 9889b840ce
commit 0919ff05c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 37 deletions

View File

@ -11,25 +11,12 @@ import
# Status libraries # Status libraries
stew/bitops2, stew/bitops2,
# Beacon chain internals # Beacon chain internals
../spec/datatypes/[phase0, altair, bellatrix, capella, deneb], ../spec/forks,
../beacon_chain_db_light_client, ../beacon_chain_db_light_client,
"."/[block_pools_types, blockchain_dag] "."/[block_pools_types, blockchain_dag]
logScope: topics = "chaindag_lc" logScope: topics = "chaindag_lc"
type
HashedBeaconStateWithSyncCommittee =
deneb.HashedBeaconState |
capella.HashedBeaconState |
bellatrix.HashedBeaconState |
altair.HashedBeaconState
TrustedSignedBeaconBlockWithSyncAggregate =
deneb.TrustedSignedBeaconBlock |
capella.TrustedSignedBeaconBlock |
bellatrix.TrustedSignedBeaconBlock |
altair.TrustedSignedBeaconBlock
template nextEpochBoundarySlot(slot: Slot): Slot = template nextEpochBoundarySlot(slot: Slot): Slot =
## Compute the first possible epoch boundary state slot of a `Checkpoint` ## Compute the first possible epoch boundary state slot of a `Checkpoint`
## referring to a block at given slot. ## referring to a block at given slot.
@ -95,8 +82,7 @@ proc existingCurrentSyncCommitteeForPeriod(
doAssert strictVerification notin dag.updateFlags doAssert strictVerification notin dag.updateFlags
syncCommittee syncCommittee
template syncCommitteeRoot( template syncCommitteeRoot(state: ForkyHashedBeaconState): Eth2Digest =
state: HashedBeaconStateWithSyncCommittee): Eth2Digest =
## Compute a root to uniquely identify `current_sync_committee` and ## Compute a root to uniquely identify `current_sync_committee` and
## `next_sync_committee`. ## `next_sync_committee`.
withEth2Hash: withEth2Hash:
@ -436,7 +422,7 @@ proc getLightClientData(
except KeyError: raiseAssert "Unreachable" except KeyError: raiseAssert "Unreachable"
proc cacheLightClientData( proc cacheLightClientData(
dag: ChainDAGRef, state: HashedBeaconStateWithSyncCommittee, bid: BlockId) = dag: ChainDAGRef, state: ForkyHashedBeaconState, bid: BlockId) =
## Cache data for a given block and its post-state to speed up creating future ## Cache data for a given block and its post-state to speed up creating future
## `LightClientUpdate` and `LightClientBootstrap` instances that refer to this ## `LightClientUpdate` and `LightClientBootstrap` instances that refer to this
## block and state. ## block and state.
@ -530,8 +516,8 @@ template lazy_bid(name: untyped): untyped {.dirty.} =
proc createLightClientUpdates( proc createLightClientUpdates(
dag: ChainDAGRef, dag: ChainDAGRef,
state: HashedBeaconStateWithSyncCommittee, state: ForkyHashedBeaconState,
blck: TrustedSignedBeaconBlockWithSyncAggregate, blck: ForkyTrustedSignedBeaconBlock,
parent_bid: BlockId, parent_bid: BlockId,
data_fork: static LightClientDataFork) = data_fork: static LightClientDataFork) =
## Create `LightClientUpdate` instances for a given block and its post-state, ## Create `LightClientUpdate` instances for a given block and its post-state,
@ -665,8 +651,8 @@ proc createLightClientUpdates(
proc createLightClientUpdates( proc createLightClientUpdates(
dag: ChainDAGRef, dag: ChainDAGRef,
state: HashedBeaconStateWithSyncCommittee, state: ForkyHashedBeaconState,
blck: TrustedSignedBeaconBlockWithSyncAggregate, blck: ForkyTrustedSignedBeaconBlock,
parent_bid: BlockId) = parent_bid: BlockId) =
# Attested block (parent) determines `LightClientUpdate` fork # Attested block (parent) determines `LightClientUpdate` fork
withLcDataFork(dag.cfg.lcDataForkAtEpoch(parent_bid.slot.epoch)): withLcDataFork(dag.cfg.lcDataForkAtEpoch(parent_bid.slot.epoch)):
@ -781,22 +767,13 @@ proc processNewBlockForLightClient*(
if signedBlock.message.slot < dag.lcDataStore.cache.tailSlot: if signedBlock.message.slot < dag.lcDataStore.cache.tailSlot:
return return
when signedBlock is deneb.TrustedSignedBeaconBlock: const consensusFork = typeof(signedBlock).kind
dag.cacheLightClientData(state.denebData, signedBlock.toBlockId()) when consensusFork >= ConsensusFork.Altair:
dag.createLightClientUpdates(state.denebData, signedBlock, parentBid) template forkyState: untyped = state.forky(consensusFork)
elif signedBlock is capella.TrustedSignedBeaconBlock: dag.cacheLightClientData(forkyState, signedBlock.toBlockId())
dag.cacheLightClientData(state.capellaData, signedBlock.toBlockId()) dag.createLightClientUpdates(forkyState, signedBlock, parentBid)
dag.createLightClientUpdates(state.capellaData, signedBlock, parentBid)
elif signedBlock is bellatrix.TrustedSignedBeaconBlock:
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: else:
{.error: "Unreachable".} raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
proc processHeadChangeForLightClient*(dag: ChainDAGRef) = proc processHeadChangeForLightClient*(dag: ChainDAGRef) =
## Update light client data to account for a new head block. ## Update light client data to account for a new head block.

View File

@ -628,7 +628,7 @@ template withState*(x: ForkedHashedBeaconState, body: untyped): untyped =
template forkyState: untyped {.inject, used.} = x.phase0Data template forkyState: untyped {.inject, used.} = x.phase0Data
body body
template forky( template forky*(
x: ForkedHashedBeaconState, kind: static ConsensusFork): untyped = x: ForkedHashedBeaconState, kind: static ConsensusFork): untyped =
when kind == ConsensusFork.Deneb: when kind == ConsensusFork.Deneb:
x.denebData x.denebData