cache new LC data after creating new LC updates (#5607)

When creating new LC updates, information about the parent block's post
state must be available (cached), but information about current block's
post state is not yet required. Caching information about the current
block's post state can be delayed, simplifying the LC data collection
logic a bit and allowing more future flexibility with the cache design.
This commit is contained in:
Etan Kissling 2023-11-17 17:17:26 -08:00 committed by GitHub
parent b179cb71a0
commit d8144c6de1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -676,23 +676,26 @@ proc initLightClientDataCache*(dag: ChainDAGRef) =
let targetTailSlot = max(dag.targetLightClientTailSlot, dag.tail.slot) let targetTailSlot = max(dag.targetLightClientTailSlot, dag.tail.slot)
dag.lcDataStore.cache.tailSlot = max(dag.head.slot, targetTailSlot) dag.lcDataStore.cache.tailSlot = max(dag.head.slot, targetTailSlot)
# Import head state # In `OnlyNew` mode, only head state needs to be cached
if dag.head.slot < dag.lcDataStore.cache.tailSlot: if dag.head.slot < dag.lcDataStore.cache.tailSlot:
return return
withState(dag.headState):
when consensusFork >= ConsensusFork.Altair:
dag.cacheLightClientData(forkyState, dag.head.bid)
else: raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
if dag.lcDataStore.importMode == LightClientDataImportMode.OnlyNew: if dag.lcDataStore.importMode == LightClientDataImportMode.OnlyNew:
withState(dag.headState):
when consensusFork >= ConsensusFork.Altair:
dag.cacheLightClientData(forkyState, dag.head.bid)
else: raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
return return
# Import light client data for finalized period through finalized head # Import light client data for finalized period through finalized head
let finalizedSlot = max(dag.finalizedHead.blck.slot, targetTailSlot) let
if finalizedSlot >= dag.lcDataStore.cache.tailSlot: finalizedSlot = max(dag.finalizedHead.blck.slot, targetTailSlot)
return finalizedPeriod = finalizedSlot.sync_committee_period
dag.lcDataStore.cache.tailSlot = finalizedSlot var res =
let finalizedPeriod = finalizedSlot.sync_committee_period if finalizedSlot < dag.lcDataStore.cache.tailSlot:
var res = dag.initLightClientDataForPeriod(finalizedPeriod) dag.lcDataStore.cache.tailSlot = finalizedSlot
dag.initLightClientDataForPeriod(finalizedPeriod)
else:
Opt[void].ok()
let lightClientStartTick = Moment.now() let lightClientStartTick = Moment.now()
logScope: lightClientDataMaxPeriods = dag.lcDataStore.maxPeriods logScope: lightClientDataMaxPeriods = dag.lcDataStore.maxPeriods
@ -728,14 +731,13 @@ proc initLightClientDataCache*(dag: ChainDAGRef) =
continue continue
withStateAndBlck(dag.headState, bdata): withStateAndBlck(dag.headState, bdata):
when consensusFork >= ConsensusFork.Altair: when consensusFork >= ConsensusFork.Altair:
# Cache light client data (non-finalized blocks may refer to this)
if i != blocks.low:
dag.cacheLightClientData(forkyState, bid) # `dag.head` already cached
# Create `LightClientUpdate` instances # Create `LightClientUpdate` instances
if i < blocks.high: if i < blocks.high:
dag.createLightClientUpdates( dag.createLightClientUpdates(
forkyState, forkyBlck, parentBid = blocks[i + 1]) forkyState, forkyBlck, parentBid = blocks[i + 1])
# Cache light client data (non-finalized blocks may refer to this)
dag.cacheLightClientData(forkyState, bid)
else: raiseAssert "Unreachable" else: raiseAssert "Unreachable"
let lightClientEndTick = Moment.now() let lightClientEndTick = Moment.now()
@ -775,8 +777,8 @@ proc processNewBlockForLightClient*(
const consensusFork = typeof(signedBlock).kind const consensusFork = typeof(signedBlock).kind
when consensusFork >= ConsensusFork.Altair: when consensusFork >= ConsensusFork.Altair:
template forkyState: untyped = state.forky(consensusFork) template forkyState: untyped = state.forky(consensusFork)
dag.cacheLightClientData(forkyState, signedBlock.toBlockId())
dag.createLightClientUpdates(forkyState, signedBlock, parentBid) dag.createLightClientUpdates(forkyState, signedBlock, parentBid)
dag.cacheLightClientData(forkyState, signedBlock.toBlockId())
else: else:
raiseAssert "Unreachable" # `tailSlot` cannot be before Altair raiseAssert "Unreachable" # `tailSlot` cannot be before Altair