Skip to content

Commit

Permalink
cache new LC data after creating new LC updates (#5607)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
etan-status authored Nov 18, 2023
1 parent b179cb7 commit d8144c6
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions beacon_chain/consensus_object_pools/blockchain_dag_light_client.nim
Original file line number Diff line number Diff line change
Expand Up @@ -676,23 +676,26 @@ proc initLightClientDataCache*(dag: ChainDAGRef) =
let targetTailSlot = max(dag.targetLightClientTailSlot, dag.tail.slot)
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:
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:
withState(dag.headState):
when consensusFork >= ConsensusFork.Altair:
dag.cacheLightClientData(forkyState, dag.head.bid)
else: raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
return

# 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
var res = dag.initLightClientDataForPeriod(finalizedPeriod)
let
finalizedSlot = max(dag.finalizedHead.blck.slot, targetTailSlot)
finalizedPeriod = finalizedSlot.sync_committee_period
var res =
if finalizedSlot < dag.lcDataStore.cache.tailSlot:
dag.lcDataStore.cache.tailSlot = finalizedSlot
dag.initLightClientDataForPeriod(finalizedPeriod)
else:
Opt[void].ok()

let lightClientStartTick = Moment.now()
logScope: lightClientDataMaxPeriods = dag.lcDataStore.maxPeriods
Expand Down Expand Up @@ -728,14 +731,13 @@ proc initLightClientDataCache*(dag: ChainDAGRef) =
continue
withStateAndBlck(dag.headState, bdata):
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
if i < blocks.high:
dag.createLightClientUpdates(
forkyState, forkyBlck, parentBid = blocks[i + 1])

# Cache light client data (non-finalized blocks may refer to this)
dag.cacheLightClientData(forkyState, bid)
else: raiseAssert "Unreachable"

let lightClientEndTick = Moment.now()
Expand Down Expand Up @@ -775,8 +777,8 @@ proc processNewBlockForLightClient*(
const consensusFork = typeof(signedBlock).kind
when consensusFork >= ConsensusFork.Altair:
template forkyState: untyped = state.forky(consensusFork)
dag.cacheLightClientData(forkyState, signedBlock.toBlockId())
dag.createLightClientUpdates(forkyState, signedBlock, parentBid)
dag.cacheLightClientData(forkyState, signedBlock.toBlockId())
else:
raiseAssert "Unreachable" # `tailSlot` cannot be before Altair

Expand Down

0 comments on commit d8144c6

Please sign in to comment.