Skip to content

Pseudo finalization endpoint #7103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions beacon_node/beacon_chain/src/beacon_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::light_client_optimistic_update_verification::{
Error as LightClientOptimisticUpdateError, VerifiedLightClientOptimisticUpdate,
};
use crate::light_client_server_cache::LightClientServerCache;
use crate::migrate::BackgroundMigrator;
use crate::migrate::{BackgroundMigrator, ManualFinalizationNotification};
use crate::naive_aggregation_pool::{
AggregatedAttestationMap, Error as NaiveAggregationError, NaiveAggregationPool,
SyncContributionAggregateMap,
Expand Down Expand Up @@ -118,8 +118,8 @@ use std::sync::Arc;
use std::time::Duration;
use store::iter::{BlockRootsIterator, ParentRootBlockIterator, StateRootsIterator};
use store::{
BlobSidecarListFromRoot, DatabaseBlock, Error as DBError, HotColdDB, KeyValueStore,
KeyValueStoreOp, StoreItem, StoreOp,
BlobSidecarListFromRoot, DatabaseBlock, Error as DBError, HotColdDB, HotStateSummary,
KeyValueStore, KeyValueStoreOp, StoreItem, StoreOp,
};
use task_executor::{ShutdownReason, TaskExecutor};
use tokio::sync::oneshot;
Expand Down Expand Up @@ -1711,6 +1711,41 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
}
}

pub fn manually_finalize_state(
&self,
state_root: Hash256,
checkpoint: Checkpoint,
) -> Result<(), Error> {
let HotStateSummary {
slot,
latest_block_root,
..
} = self
.store
.load_hot_state_summary(&state_root)
.map_err(BeaconChainError::DBError)?
.ok_or(BeaconChainError::MissingHotStateSummary(state_root))?;

if slot != checkpoint.epoch.start_slot(T::EthSpec::slots_per_epoch())
|| latest_block_root != *checkpoint.root
{
return Err(BeaconChainError::InvalidCheckpoint {
state_root,
checkpoint,
});
}

let notif = ManualFinalizationNotification {
state_root: state_root.into(),
checkpoint,
head_tracker: self.head_tracker.clone(),
genesis_block_root: self.genesis_block_root,
};

self.store_migrator.process_manual_finalization(notif);
Ok(())
}

/// Returns an aggregated `Attestation`, if any, that has a matching `attestation.data`.
///
/// The attestation will be obtained from `self.naive_aggregation_pool`.
Expand Down
17 changes: 16 additions & 1 deletion beacon_node/beacon_chain/src/block_verification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1767,7 +1767,22 @@ pub fn check_block_is_finalized_checkpoint_or_descendant<
fork_choice: &BeaconForkChoice<T>,
block: B,
) -> Result<B, BlockError> {
if fork_choice.is_finalized_checkpoint_or_descendant(block.parent_root()) {
// If we have a split block newer than finalization then we also ban blocks which are not
// descended from that split block. It's important not to try checking `is_descendant` if
// finality is ahead of the split and the split block has been pruned, as `is_descendant` will
// return `false` in this case.
let finalized_slot = fork_choice
.finalized_checkpoint()
.epoch
.start_slot(T::EthSpec::slots_per_epoch());
let split = chain.store.get_split_info();
let is_descendant_from_split_block = split.slot == 0
|| split.slot <= finalized_slot
|| fork_choice.is_descendant(split.block_root, block.parent_root());

if fork_choice.is_finalized_checkpoint_or_descendant(block.parent_root())
&& is_descendant_from_split_block
{
Ok(block)
} else {
// If fork choice does *not* consider the parent to be a descendant of the finalized block,
Expand Down
7 changes: 4 additions & 3 deletions beacon_node/beacon_chain/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub enum BeaconChainError {
ForkChoiceStoreError(ForkChoiceStoreError),
MissingBeaconBlock(Hash256),
MissingBeaconState(Hash256),
MissingHotStateSummary(Hash256),
SlotProcessingError(SlotProcessingError),
EpochProcessingError(EpochProcessingError),
StateAdvanceError(StateAdvanceError),
Expand Down Expand Up @@ -181,9 +182,9 @@ pub enum BeaconChainError {
execution_block_hash: Option<ExecutionBlockHash>,
},
ForkchoiceUpdate(execution_layer::Error),
FinalizedCheckpointMismatch {
head_state: Checkpoint,
fork_choice: Hash256,
InvalidCheckpoint {
state_root: Hash256,
checkpoint: Checkpoint,
},
InvalidSlot(Slot),
HeadBlockNotFullyVerified {
Expand Down
60 changes: 55 additions & 5 deletions beacon_node/beacon_chain/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,22 @@ pub enum Notification {
Finalization(FinalizationNotification),
Reconstruction,
PruneBlobs(Epoch),
ManualFinalization(ManualFinalizationNotification),
}

pub struct ManualFinalizationNotification {
pub state_root: BeaconStateHash,
pub checkpoint: Checkpoint,
pub head_tracker: Arc<HeadTracker>,
pub genesis_block_root: Hash256,
}

pub struct FinalizationNotification {
finalized_state_root: BeaconStateHash,
finalized_checkpoint: Checkpoint,
head_tracker: Arc<HeadTracker>,
prev_migration: Arc<Mutex<PrevMigration>>,
genesis_block_root: Hash256,
pub finalized_state_root: BeaconStateHash,
pub finalized_checkpoint: Checkpoint,
pub head_tracker: Arc<HeadTracker>,
pub prev_migration: Arc<Mutex<PrevMigration>>,
pub genesis_block_root: Hash256,
}

impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Hot, Cold> {
Expand Down Expand Up @@ -190,6 +198,14 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
Ok(())
}

pub fn process_manual_finalization(&self, notif: ManualFinalizationNotification) {
if let Some(Notification::ManualFinalization(notif)) =
self.send_background_notification(Notification::ManualFinalization(notif))
{
Self::run_manual_migration(self.db.clone(), notif, &self.log);
}
}

pub fn process_reconstruction(&self) {
if let Some(Notification::Reconstruction) =
self.send_background_notification(Notification::Reconstruction)
Expand Down Expand Up @@ -289,6 +305,26 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
}
}

fn run_manual_migration(
db: Arc<HotColdDB<E, Hot, Cold>>,
notif: ManualFinalizationNotification,
log: &Logger,
) {
// We create a "dummy" prev migration
let prev_migration = PrevMigration {
epoch: Epoch::new(1),
epochs_per_migration: 2,
};
let notif = FinalizationNotification {
finalized_state_root: notif.state_root,
finalized_checkpoint: notif.checkpoint,
head_tracker: notif.head_tracker,
prev_migration: Arc::new(prev_migration.into()),
genesis_block_root: notif.genesis_block_root,
};
Self::run_migration(db, notif, log);
}

/// Perform the actual work of `process_finalization`.
fn run_migration(
db: Arc<HotColdDB<E, Hot, Cold>>,
Expand Down Expand Up @@ -423,16 +459,27 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
while let Ok(notif) = rx.recv() {
let mut reconstruction_notif = None;
let mut finalization_notif = None;
let mut manual_finalization_notif = None;
let mut prune_blobs_notif = None;
match notif {
Notification::Reconstruction => reconstruction_notif = Some(notif),
Notification::Finalization(fin) => finalization_notif = Some(fin),
Notification::ManualFinalization(fin) => manual_finalization_notif = Some(fin),
Notification::PruneBlobs(dab) => prune_blobs_notif = Some(dab),
}
// Read the rest of the messages in the channel, taking the best of each type.
for notif in rx.try_iter() {
match notif {
Notification::Reconstruction => reconstruction_notif = Some(notif),
Notification::ManualFinalization(fin) => {
if let Some(current) = manual_finalization_notif.as_mut() {
if fin.checkpoint.epoch > current.checkpoint.epoch {
*current = fin;
}
} else {
manual_finalization_notif = Some(fin);
}
}
Notification::Finalization(fin) => {
if let Some(current) = finalization_notif.as_mut() {
if fin.finalized_checkpoint.epoch
Expand All @@ -455,6 +502,9 @@ impl<E: EthSpec, Hot: ItemStore<E>, Cold: ItemStore<E>> BackgroundMigrator<E, Ho
if let Some(fin) = finalization_notif {
Self::run_migration(db.clone(), fin, &log);
}
if let Some(fin) = manual_finalization_notif {
Self::run_manual_migration(db.clone(), fin, &log);
}
if let Some(dab) = prune_blobs_notif {
Self::run_prune_blobs(db.clone(), dab, &log);
}
Expand Down
Loading