Skip to content

Commit

Permalink
Revert "fix checkpoint block potentially not getting backfilled into …
Browse files Browse the repository at this point in the history
…DB (#5863)" (#5871)

This reverts commit 65e6f89.
  • Loading branch information
tersec authored Feb 9, 2024
1 parent 65e6f89 commit 1575478
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 143 deletions.
7 changes: 3 additions & 4 deletions AllTests-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ OK: 1/1 Fail: 0/1 Skip: 0/1
OK: 12/12 Fail: 0/12 Skip: 0/12
## Backfill
```diff
+ Backfill to genesis OK
+ Init without genesis / block OK
+ Reload backfill position OK
+ Restart after each block OK
+ backfill to genesis OK
+ reload backfill position OK
```
OK: 4/4 Fail: 0/4 Skip: 0/4
OK: 3/3 Fail: 0/3 Skip: 0/3
## Beacon chain DB [Preset: mainnet]
```diff
+ empty database [Preset: mainnet] OK
Expand Down
3 changes: 0 additions & 3 deletions beacon_chain/beacon_chain_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,6 @@ type
slot*: Slot
parent_root*: Eth2Digest

func shortLog*(v: BeaconBlockSummary): auto =
(v.slot, shortLog(v.parent_root))

# Subkeys essentially create "tables" within the key-value store by prefixing
# each entry with a table id

Expand Down
24 changes: 10 additions & 14 deletions beacon_chain/consensus_object_pools/block_clearance.nim
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ proc addBackfillBlock*(
blockRoot = shortLog(signedBlock.root)
blck = shortLog(signedBlock.message)
signature = shortLog(signedBlock.signature)
backfill = shortLog(dag.backfill)
backfill = (dag.backfill.slot, shortLog(dag.backfill.parent_root))

template blck(): untyped = signedBlock.message # shortcuts without copy
template blockRoot(): untyped = signedBlock.root
Expand Down Expand Up @@ -393,22 +393,18 @@ proc addBackfillBlock*(
if existing.isSome:
if existing.get().bid.slot == blck.slot and
existing.get().bid.root == blockRoot:
let isDuplicate = dag.containsBlock(existing.get().bid)
if isDuplicate:
debug "Duplicate block"
else:

# Special case: when starting with only a checkpoint state, we will not
# have the head block data in the database
if dag.getForkedBlock(existing.get().bid).isNone():
checkSignature()
debug "Block backfilled (known BlockId)"
dag.putBlock(signedBlock.asTrusted())

if blockRoot == dag.backfill.parent_root:
dag.backfill = blck.toBeaconBlockSummary()
debug "Block backfilled (checkpoint)"
dag.putBlock(signedBlock.asTrusted())
return ok()

return
if isDuplicate:
err(VerifierError.Duplicate)
else:
ok()
debug "Duplicate block"
return err(VerifierError.Duplicate)

# Block is older than finalized, but different from the block in our
# canonical history: it must be from an unviable branch
Expand Down
6 changes: 0 additions & 6 deletions beacon_chain/consensus_object_pools/block_pools_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,6 @@ type
## The backfill points to the oldest block with an unbroken ancestry from
## dag.tail - when backfilling, we'll move backwards in time starting
## with the parent of this block until we reach `frontfill`.
##
## - `backfill.slot` points to the earliest block that has been synced,
## or, if no blocks have been synced yet, to `checkpoint_state.slot + 1`
## which is the earliest slot that may have `parent_root` as ancestor.
## - `backfill.parent_root` is the latest block that is not yet synced.
## - Once backfill completes, `backfill.slot` refers to `GENESIS_SLOT`.

frontfillBlocks*: seq[Eth2Digest]
## A temporary cache of blocks that we could load from era files, once
Expand Down
21 changes: 6 additions & 15 deletions beacon_chain/consensus_object_pools/blockchain_dag.nim
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ proc updateFrontfillBlocks*(dag: ChainDAGRef) =
# era files match the chain
if dag.db.db.readOnly: return # TODO abstraction leak - where to put this?

if dag.frontfillBlocks.len == 0 or dag.backfill.slot > GENESIS_SLOT:
if dag.frontfillBlocks.len == 0 or dag.backfill.slot > 0:
return

info "Writing frontfill index", slots = dag.frontfillBlocks.len
Expand Down Expand Up @@ -259,9 +259,6 @@ proc containsBlock(
cfg: RuntimeConfig, db: BeaconChainDB, slot: Slot, root: Eth2Digest): bool =
db.containsBlock(root, cfg.consensusForkAtEpoch(slot.epoch))

proc containsBlock*(dag: ChainDAGRef, bid: BlockId): bool =
dag.cfg.containsBlock(dag.db, bid.slot, bid.root)

proc getForkedBlock*(db: BeaconChainDB, root: Eth2Digest):
Opt[ForkedTrustedSignedBeaconBlock] =
# When we only have a digest, we don't know which fork it's from so we try
Expand Down Expand Up @@ -1230,14 +1227,9 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,

db.getBeaconBlockSummary(backfillRoot).expect(
"Backfill block must have a summary: " & $backfillRoot)
elif dag.containsBlock(dag.tail):
else:
db.getBeaconBlockSummary(dag.tail.root).expect(
"Tail block must have a summary: " & $dag.tail.root)
else:
# Checkpoint sync, checkpoint block unavailable
BeaconBlockSummary(
slot: dag.tail.slot + 1,
parent_root: dag.tail.root)

dag.forkDigests = newClone ForkDigests.init(
cfg, getStateField(dag.headState, genesis_validators_root))
Expand All @@ -1249,9 +1241,8 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,

let finalizedTick = Moment.now()

if dag.backfill.slot > GENESIS_SLOT: # Try frontfill from era files
let backfillSlot = dag.backfill.slot - 1
dag.frontfillBlocks = newSeqOfCap[Eth2Digest](backfillSlot.int)
if dag.backfill.slot > 0: # See if we can frontfill blocks from era files
dag.frontfillBlocks = newSeqOfCap[Eth2Digest](dag.backfill.slot.int)

let
historical_roots = getStateField(dag.headState, historical_roots).asSeq()
Expand All @@ -1264,7 +1255,7 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,
# blocks from genesis to backfill, if possible.
for bid in dag.era.getBlockIds(
historical_roots, historical_summaries, Slot(0), Eth2Digest()):
if bid.slot >= backfillSlot:
if bid.slot >= dag.backfill.slot:
# If we end up in here, we failed the root comparison just below in
# an earlier iteration
fatal "Era summaries don't lead up to backfill, database or era files corrupt?",
Expand Down Expand Up @@ -1313,7 +1304,7 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,
head = shortLog(dag.head),
finalizedHead = shortLog(dag.finalizedHead),
tail = shortLog(dag.tail),
backfill = shortLog(dag.backfill),
backfill = (dag.backfill.slot, shortLog(dag.backfill.parent_root)),

loadDur = loadTick - startTick,
summariesDur = summariesTick - loadTick,
Expand Down
39 changes: 23 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 @@ -28,8 +28,7 @@ proc updateExistingState(
## 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, backfill = shortLog(dag.backfill)
error "State failed to load unexpectedly", bsi, tail = dag.tail.slot
doAssert strictVerification notin dag.updateFlags
ok

Expand All @@ -42,26 +41,23 @@ template withUpdatedExistingState(
dag.withUpdatedState(stateParam, bsiParam) do:
okBody
do:
error "State failed to load unexpectedly",
bsi, tail = dag.tail.slot, backfill = shortLog(dag.backfill)
error "State failed to load unexpectedly", bsi, tail = dag.tail.slot
doAssert strictVerification notin dag.updateFlags
failureBody

proc getExistingBlockIdAtSlot(dag: ChainDAGRef, slot: Slot): Opt[BlockSlotId] =
## Wrapper around `getBlockIdAtSlot` for blocks expected to exist.
let bsi = dag.getBlockIdAtSlot(slot)
if bsi.isNone:
error "Block failed to load unexpectedly",
slot, tail = dag.tail.slot, backfill = shortLog(dag.backfill)
error "Block failed to load unexpectedly", slot, tail = dag.tail.slot
doAssert strictVerification notin dag.updateFlags
bsi

proc existingParent(dag: ChainDAGRef, bid: BlockId): Opt[BlockId] =
## Wrapper around `parent` for parents known to exist.
let parent = dag.parent(bid)
if parent.isNone:
error "Parent failed to load unexpectedly",
bid, tail = dag.tail.slot, backfill = shortLog(dag.backfill)
error "Parent failed to load unexpectedly", bid, tail = dag.tail.slot
doAssert strictVerification notin dag.updateFlags
parent

Expand All @@ -70,8 +66,7 @@ proc getExistingForkedBlock(
## Wrapper around `getForkedBlock` for blocks expected to exist.
let bdata = dag.getForkedBlock(bid)
if bdata.isNone:
error "Block failed to load unexpectedly",
bid, tail = dag.tail.slot, backfill = shortLog(dag.backfill)
error "Block failed to load unexpectedly", bid, tail = dag.tail.slot
doAssert strictVerification notin dag.updateFlags
bdata

Expand All @@ -83,7 +78,7 @@ proc existingCurrentSyncCommitteeForPeriod(
let syncCommittee = dag.currentSyncCommitteeForPeriod(tmpState, period)
if syncCommittee.isNone:
error "Current sync committee failed to load unexpectedly",
period, tail = dag.tail.slot, backfill = shortLog(dag.backfill)
period, tail = dag.tail.slot
doAssert strictVerification notin dag.updateFlags
syncCommittee

Expand Down Expand Up @@ -365,7 +360,7 @@ proc initLightClientUpdateForPeriod(
continue
finalizedSlot = finalizedEpoch.start_slot
finalizedBsi =
if finalizedSlot >= max(dag.tail.slot, dag.backfill.slot):
if finalizedSlot >= dag.tail.slot:
dag.getExistingBlockIdAtSlot(finalizedSlot).valueOr:
dag.handleUnexpectedLightClientError(finalizedSlot)
res.err()
Expand Down Expand Up @@ -547,7 +542,7 @@ proc assignLightClientData(
when lcDataFork > LightClientDataFork.None:
if finalized_slot == forkyObject.finalized_header.beacon.slot:
forkyObject.finality_branch = attested_data.finality_branch
elif finalized_slot < max(dag.tail.slot, dag.backfill.slot):
elif finalized_slot < dag.tail.slot:
forkyObject.finalized_header.reset()
forkyObject.finality_branch.reset()
else:
Expand Down Expand Up @@ -637,13 +632,13 @@ proc createLightClientUpdate(
let
finalized_slot = attested_data.finalized_slot
finalized_bsi =
if finalized_slot >= max(dag.tail.slot, dag.backfill.slot):
if finalized_slot >= dag.tail.slot:
dag.getExistingBlockIdAtSlot(finalized_slot)
else:
Opt.none(BlockSlotId)
has_finality =
finalized_bsi.isSome and
finalized_bsi.get.bid.slot >= max(dag.tail.slot, dag.backfill.slot)
finalized_bsi.get.bid.slot >= dag.tail.slot
meta = LightClientUpdateMetadata(
attested_slot: attested_slot,
finalized_slot: finalized_slot,
Expand Down Expand Up @@ -727,7 +722,19 @@ proc initLightClientDataCache*(dag: ChainDAGRef) =
# State availability, needed for `cacheLightClientData`
dag.tail.slot,
# Block availability, needed for `LightClientHeader.execution_branch`
dag.backfill.slot))
if dag.backfill.slot != GENESIS_SLOT:
let existing = dag.getBlockIdAtSlot(dag.backfill.slot)
if existing.isSome:
if dag.getForkedBlock(existing.get.bid).isNone:
# Special case: when starting with only a checkpoint state,
# we will not have the head block data in the database
dag.backfill.slot + 1
else:
dag.backfill.slot
else:
dag.backfill.slot
else:
dag.backfill.slot))

dag.lcDataStore.cache.tailSlot = max(dag.head.slot, targetTailSlot)
if dag.head.slot < dag.lcDataStore.cache.tailSlot:
Expand Down
5 changes: 1 addition & 4 deletions beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,7 @@ proc initFullNode(
dag.finalizedHead.slot

func getBackfillSlot(): Slot =
if dag.backfill.parent_root != dag.tail.root:
dag.backfill.slot
else:
dag.tail.slot
dag.backfill.slot

func getFrontfillSlot(): Slot =
max(dag.frontfill.get(BlockId()).slot, dag.horizon)
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/trusted_node_sync.nim
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ proc doTrustedNodeSync*(
let
validatorMonitor = newClone(ValidatorMonitor.init(false, false))
dag = ChainDAGRef.init(cfg, db, validatorMonitor, {}, eraPath = eraDir)
backfillSlot = max(dag.backfill.slot, 1.Slot) - 1
backfillSlot = dag.backfill.slot
horizon = max(dag.horizon, dag.frontfill.valueOr(BlockId()).slot)

let canReindex = if backfillSlot <= horizon:
Expand All @@ -418,7 +418,7 @@ proc doTrustedNodeSync*(
# detection to kick in, in addBackfillBlock
let missingSlots = dag.backfill.slot - horizon + 1

notice "Downloading historical blocks - you can interrupt this process at any time and it will automatically be completed when you start the beacon node",
notice "Downloading historical blocks - you can interrupt this process at any time and it automatically be completed when you start the beacon node",
backfillSlot, horizon, missingSlots

var # Same averaging as SyncManager
Expand Down
Loading

0 comments on commit 1575478

Please sign in to comment.