Gloas fork choice redux (#9025)

Co-Authored-By: hopinheimer <knmanas6@gmail.com>

Co-Authored-By: Michael Sproul <michael@sigmaprime.io>

Co-Authored-By: hopinheimer <48147533+hopinheimer@users.noreply.github.com>

Co-Authored-By: Eitan Seri- Levi <eserilev@gmail.com>

Co-Authored-By: dapplion <35266934+dapplion@users.noreply.github.com>

Co-Authored-By: Michael Sproul <michaelsproul@users.noreply.github.com>

Co-Authored-By: Jimmy Chen <jchen.tc@gmail.com>

Co-Authored-By: Daniel Knopik <107140945+dknopik@users.noreply.github.com>
This commit is contained in:
Michael Sproul
2026-04-03 19:35:02 +11:00
committed by GitHub
parent 99f5a92b98
commit 65c2e01612
40 changed files with 4061 additions and 834 deletions

View File

@@ -107,6 +107,8 @@ pub struct CachedHead<E: EthSpec> {
/// This value may be distinct to the `self.snapshot.beacon_state.finalized_checkpoint`.
/// This value should be used over the beacon state value in practically all circumstances.
finalized_checkpoint: Checkpoint,
/// The payload status of the head block, as determined by fork choice.
head_payload_status: proto_array::PayloadStatus,
/// The `execution_payload.block_hash` of the block at the head of the chain. Set to `None`
/// before Bellatrix.
head_hash: Option<ExecutionBlockHash>,
@@ -231,6 +233,10 @@ impl<E: EthSpec> CachedHead<E> {
finalized_hash: self.finalized_hash,
}
}
pub fn head_payload_status(&self) -> proto_array::PayloadStatus {
self.head_payload_status
}
}
/// Represents the "canonical head" of the beacon chain.
@@ -261,6 +267,7 @@ impl<T: BeaconChainTypes> CanonicalHead<T> {
pub fn new(
fork_choice: BeaconForkChoice<T>,
snapshot: Arc<BeaconSnapshot<T::EthSpec>>,
head_payload_status: proto_array::PayloadStatus,
) -> Self {
let fork_choice_view = fork_choice.cached_fork_choice_view();
let forkchoice_update_params = fork_choice.get_forkchoice_update_parameters();
@@ -268,6 +275,7 @@ impl<T: BeaconChainTypes> CanonicalHead<T> {
snapshot,
justified_checkpoint: fork_choice_view.justified_checkpoint,
finalized_checkpoint: fork_choice_view.finalized_checkpoint,
head_payload_status,
head_hash: forkchoice_update_params.head_hash,
justified_hash: forkchoice_update_params.justified_hash,
finalized_hash: forkchoice_update_params.finalized_hash,
@@ -295,9 +303,11 @@ impl<T: BeaconChainTypes> CanonicalHead<T> {
store: &BeaconStore<T>,
spec: &ChainSpec,
) -> Result<(), Error> {
let fork_choice =
let mut fork_choice =
<BeaconChain<T>>::load_fork_choice(store.clone(), reset_payload_statuses, spec)?
.ok_or(Error::MissingPersistedForkChoice)?;
let current_slot_for_head = fork_choice.fc_store().get_current_slot();
let (_, head_payload_status) = fork_choice.get_head(current_slot_for_head, spec)?;
let fork_choice_view = fork_choice.cached_fork_choice_view();
let beacon_block_root = fork_choice_view.head_block_root;
let beacon_block = store
@@ -328,6 +338,7 @@ impl<T: BeaconChainTypes> CanonicalHead<T> {
snapshot: Arc::new(snapshot),
justified_checkpoint: fork_choice_view.justified_checkpoint,
finalized_checkpoint: fork_choice_view.finalized_checkpoint,
head_payload_status,
head_hash: forkchoice_update_params.head_hash,
justified_hash: forkchoice_update_params.justified_hash,
finalized_hash: forkchoice_update_params.finalized_hash,
@@ -601,11 +612,12 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
justified_checkpoint: old_cached_head.justified_checkpoint(),
finalized_checkpoint: old_cached_head.finalized_checkpoint(),
};
let old_payload_status = old_cached_head.head_payload_status();
let mut fork_choice_write_lock = self.canonical_head.fork_choice_write_lock();
// Recompute the current head via the fork choice algorithm.
fork_choice_write_lock.get_head(current_slot, &self.spec)?;
let (_, new_payload_status) = fork_choice_write_lock.get_head(current_slot, &self.spec)?;
// Downgrade the fork choice write-lock to a read lock, without allowing access to any
// other writers.
@@ -650,9 +662,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
});
}
// Exit early if the head or justified/finalized checkpoints have not changed, there's
// nothing to do.
if new_view == old_view {
// Exit early if the head, checkpoints, and payload status have not changed.
if new_view == old_view && new_payload_status == old_payload_status {
debug!(
head = ?new_view.head_block_root,
"No change in canonical head"
@@ -709,6 +720,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
snapshot: Arc::new(new_snapshot),
justified_checkpoint: new_view.justified_checkpoint,
finalized_checkpoint: new_view.finalized_checkpoint,
head_payload_status: new_payload_status,
head_hash: new_forkchoice_update_parameters.head_hash,
justified_hash: new_forkchoice_update_parameters.justified_hash,
finalized_hash: new_forkchoice_update_parameters.finalized_hash,
@@ -736,6 +748,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
snapshot: old_cached_head.snapshot.clone(),
justified_checkpoint: new_view.justified_checkpoint,
finalized_checkpoint: new_view.finalized_checkpoint,
head_payload_status: new_payload_status,
head_hash: new_forkchoice_update_parameters.head_hash,
justified_hash: new_forkchoice_update_parameters.justified_hash,
finalized_hash: new_forkchoice_update_parameters.finalized_hash,