Drop head tracker for summaries DAG (#6744)

The head tracker is a persisted piece of state that must be kept in sync with the fork-choice. It has been a source of pruning issues in the past, so we want to remove it
- see https://github.com/sigp/lighthouse/issues/1785

When implementing tree-states in the hot DB we have to change the pruning routine (more details below) so we want to do those changes first in isolation.
- see https://github.com/sigp/lighthouse/issues/6580
- If you want to see the full feature of tree-states hot https://github.com/dapplion/lighthouse/pull/39

Closes https://github.com/sigp/lighthouse/issues/1785


  **Current DB migration routine**

- Locate abandoned heads with head tracker
- Use a roots iterator to collect the ancestors of those heads can be pruned
- Delete those abandoned blocks / states
- Migrate the newly finalized chain to the freezer

In summary, it computes what it has to delete and keeps the rest. Then it migrates data to the freezer. If the abandoned forks routine has a bug it can break the freezer migration.

**Proposed migration routine (this PR)**

- Migrate the newly finalized chain to the freezer
- Load all state summaries from disk
- From those, just knowing the head and finalized block compute two sets: (1) descendants of finalized (2) newly finalized chain
- Iterate all summaries, if a summary does not belong to set (1) or (2), delete

This strategy is more sound as it just checks what's there in the hot DB, computes what it has to keep and deletes the rest. Because it does not rely and 3rd pieces of data we can drop the head tracker and pruning checkpoint. Since the DB migration happens **first** now, as long as the computation of the sets to keep is correct we won't have pruning issues.
This commit is contained in:
Lion - dapplion
2025-04-07 01:23:52 -03:00
committed by GitHub
parent b5d40e3db0
commit 70850fe58d
27 changed files with 1110 additions and 983 deletions

View File

@@ -0,0 +1,147 @@
use crate::beacon_chain::BeaconChainTypes;
use crate::persisted_fork_choice::PersistedForkChoice;
use crate::schema_change::StoreError;
use crate::test_utils::{PersistedBeaconChain, BEACON_CHAIN_DB_KEY, FORK_CHOICE_DB_KEY};
use crate::BeaconForkChoiceStore;
use fork_choice::{ForkChoice, ResetPayloadStatuses};
use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use std::sync::Arc;
use store::{DBColumn, Error, HotColdDB, KeyValueStore, KeyValueStoreOp, StoreItem};
use types::{Hash256, Slot};
/// Dummy value to use for the canonical head block root, see below.
pub const DUMMY_CANONICAL_HEAD_BLOCK_ROOT: Hash256 = Hash256::repeat_byte(0xff);
pub fn upgrade_to_v23<T: BeaconChainTypes>(
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>,
) -> Result<Vec<KeyValueStoreOp>, Error> {
// 1) Set the head-tracker to empty
let Some(persisted_beacon_chain_v22) =
db.get_item::<PersistedBeaconChainV22>(&BEACON_CHAIN_DB_KEY)?
else {
return Err(Error::MigrationError(
"No persisted beacon chain found in DB. Datadir could be incorrect or DB could be corrupt".to_string()
));
};
let persisted_beacon_chain = PersistedBeaconChain {
genesis_block_root: persisted_beacon_chain_v22.genesis_block_root,
};
let mut ops = vec![persisted_beacon_chain.as_kv_store_op(BEACON_CHAIN_DB_KEY)];
// 2) Wipe out all state temporary flags. While un-used in V23, if there's a rollback we could
// end-up with an inconsistent DB.
for state_root_result in db
.hot_db
.iter_column_keys::<Hash256>(DBColumn::BeaconStateTemporary)
{
ops.push(KeyValueStoreOp::DeleteKey(
DBColumn::BeaconStateTemporary,
state_root_result?.as_slice().to_vec(),
));
}
Ok(ops)
}
pub fn downgrade_from_v23<T: BeaconChainTypes>(
db: Arc<HotColdDB<T::EthSpec, T::HotStore, T::ColdStore>>,
) -> Result<Vec<KeyValueStoreOp>, Error> {
let Some(persisted_beacon_chain) = db.get_item::<PersistedBeaconChain>(&BEACON_CHAIN_DB_KEY)?
else {
// The `PersistedBeaconChain` must exist if fork choice exists.
return Err(Error::MigrationError(
"No persisted beacon chain found in DB. Datadir could be incorrect or DB could be corrupt".to_string(),
));
};
// Recreate head-tracker from fork choice.
let Some(persisted_fork_choice) = db.get_item::<PersistedForkChoice>(&FORK_CHOICE_DB_KEY)?
else {
// Fork choice should exist if the database exists.
return Err(Error::MigrationError(
"No fork choice found in DB".to_string(),
));
};
let fc_store =
BeaconForkChoiceStore::from_persisted(persisted_fork_choice.fork_choice_store, db.clone())
.map_err(|e| {
Error::MigrationError(format!(
"Error loading fork choise store from persisted: {e:?}"
))
})?;
// Doesn't matter what policy we use for invalid payloads, as our head calculation just
// considers descent from finalization.
let reset_payload_statuses = ResetPayloadStatuses::OnlyWithInvalidPayload;
let fork_choice = ForkChoice::from_persisted(
persisted_fork_choice.fork_choice,
reset_payload_statuses,
fc_store,
&db.spec,
)
.map_err(|e| {
Error::MigrationError(format!("Error loading fork choice from persisted: {e:?}"))
})?;
let heads = fork_choice
.proto_array()
.heads_descended_from_finalization::<T::EthSpec>();
let head_roots = heads.iter().map(|node| node.root).collect();
let head_slots = heads.iter().map(|node| node.slot).collect();
let persisted_beacon_chain_v22 = PersistedBeaconChainV22 {
_canonical_head_block_root: DUMMY_CANONICAL_HEAD_BLOCK_ROOT,
genesis_block_root: persisted_beacon_chain.genesis_block_root,
ssz_head_tracker: SszHeadTracker {
roots: head_roots,
slots: head_slots,
},
};
let ops = vec![persisted_beacon_chain_v22.as_kv_store_op(BEACON_CHAIN_DB_KEY)];
Ok(ops)
}
/// Helper struct that is used to encode/decode the state of the `HeadTracker` as SSZ bytes.
///
/// This is used when persisting the state of the `BeaconChain` to disk.
#[derive(Encode, Decode, Clone)]
pub struct SszHeadTracker {
roots: Vec<Hash256>,
slots: Vec<Slot>,
}
#[derive(Clone, Encode, Decode)]
pub struct PersistedBeaconChainV22 {
/// This value is ignored to resolve the issue described here:
///
/// https://github.com/sigp/lighthouse/pull/1639
///
/// Its removal is tracked here:
///
/// https://github.com/sigp/lighthouse/issues/1784
pub _canonical_head_block_root: Hash256,
pub genesis_block_root: Hash256,
/// DEPRECATED
pub ssz_head_tracker: SszHeadTracker,
}
impl StoreItem for PersistedBeaconChainV22 {
fn db_column() -> DBColumn {
DBColumn::BeaconChain
}
fn as_store_bytes(&self) -> Vec<u8> {
self.as_ssz_bytes()
}
fn from_store_bytes(bytes: &[u8]) -> Result<Self, StoreError> {
Self::from_ssz_bytes(bytes).map_err(Into::into)
}
}