Skip to content
Closed
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
16 changes: 13 additions & 3 deletions crates/chain-state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,12 +1001,22 @@ impl<N: NodePrimitives<SignedTx: SignedTransaction>> NewCanonicalChain<N> {
/// Returns the new tip for [`Self::Reorg`] and [`Self::Commit`] variants which commit at least
/// 1 new block.
pub fn tip(&self) -> &RecoveredBlock<N::Block> {
self.new_blocks().last().expect("non empty blocks").recovered_block()
}

/// Returns the blocks added to the canonical chain by this update.
pub fn new_blocks(&self) -> &[ExecutedBlock<N>] {
match self {
Self::Commit { new } | Self::Reorg { new, .. } => {
new.last().expect("non empty blocks").recovered_block()
}
Self::Commit { new } | Self::Reorg { new, .. } => new,
}
}

/// Returns whether the newly canonicalized blocks contain the given hash.
///
/// This does not include the unchanged canonical prefix before the first new block.
pub fn contains(&self, hash: B256) -> bool {
self.new_blocks().iter().any(|block| block.recovered_block().hash() == hash)
}
}

#[cfg(test)]
Expand Down
84 changes: 80 additions & 4 deletions crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,6 +1252,10 @@ where

trace!(target: "engine::tree", "fcu head hash is already canonical");

if !self.is_consistent_forkchoice_state(state, None)? {
return Ok(Some(TreeOutcome::new(OnForkChoiceUpdated::invalid_state())));
}

// Update the safe and finalized blocks and ensure their values are valid
if let Err(outcome) = self.ensure_consistent_forkchoice_state(state) {
// safe or finalized hashes are invalid
Expand Down Expand Up @@ -1320,6 +1324,10 @@ where
return Ok(Some(TreeOutcome::new(OnForkChoiceUpdated::too_deep_reorg())));
}

if !self.is_consistent_forkchoice_state(state, None)? {
return Ok(Some(TreeOutcome::new(OnForkChoiceUpdated::invalid_state())));
}

// We need to effectively unwind the _canonical_ chain to the FCU's head, which is
// part of the canonical chain. We need to update the latest block state to reflect
// the canonical ancestor. This ensures that state providers and the transaction
Expand Down Expand Up @@ -1348,6 +1356,10 @@ where

// Ensure we can apply a new chain update for the head block
if let Some(chain_update) = self.on_new_head(state.head_block_hash)? {
if !self.is_consistent_forkchoice_state(state, Some(&chain_update))? {
return Ok(Some(TreeOutcome::new(OnForkChoiceUpdated::invalid_state())));
}

let tip = chain_update.tip().clone_sealed_header();
self.on_canonical_chain_update(chain_update);

Expand Down Expand Up @@ -3360,17 +3372,81 @@ where
}

/// Attempts to find the header for the given block hash if it is canonical.
///
/// A persisted header can still be found by hash while its disk reorg is pending, so it is
/// only canonical if it is not above the canonical head and matches the canonical hash at its
/// height. The in-memory canonical hash is preferred over the persisted one.
pub fn find_canonical_header(
&self,
hash: B256,
) -> Result<Option<SealedHeader<N::BlockHeader>>, ProviderError> {
let mut canonical = self.canonical_in_memory_state.header_by_hash(hash);
if let Some(header) = self.canonical_in_memory_state.header_by_hash(hash) {
return Ok(Some(header))
}

if canonical.is_none() {
canonical = self.provider.header(hash)?.map(|header| SealedHeader::new(header, hash));
let Some(header) = self.provider.sealed_header_by_hash(hash)? else { return Ok(None) };
// Reorged-out headers remain on disk until persistence catches up, so reject old tips above
// the in-memory head and use in-memory hashes to reject old blocks at shared heights.
let number = header.number();
if number > self.canonical_in_memory_state.get_canonical_block_number() {
return Ok(None)
}
let canonical_hash =
if let Some(hash) = self.canonical_in_memory_state.hash_by_number(number) {
Some(hash)
} else {
self.provider.block_hash(number)?
};

Ok(canonical)
Ok((canonical_hash == Some(hash)).then_some(header))
}

/// Checks that nonzero safe and finalized hashes belong to the chain defined by the FCU head.
///
/// The [Engine API forkchoiceUpdated specification] requires `-38002` when a `VALID` head's
/// safe or finalized hash is outside that chain, and requires all forkchoice state updates to
/// be atomic. In direct FCU processing, this check must therefore run before canonicalizing
/// the head or updating either marker; checking only after canonicalization would leave an
/// invalid reorg applied.
///
/// `chain_update` describes a proposed commit or reorg that has not yet been applied. Its new
/// blocks and the canonical prefix below its first block form the proposed chain. Without a
/// chain update, the proposed head is already canonical, so only canonical blocks through its
/// height are eligible. Canonical-prefix hashes are resolved via
/// [`Self::find_canonical_header`], which rejects stale persisted headers whose disk reorg
/// cleanup is pending. A zero safe or finalized hash leaves that marker unchanged.
/// Returns `Ok(false)` for an unknown or off-chain hash and propagates provider errors.
///
/// [Engine API forkchoiceUpdated specification]: https://github.com/ethereum/execution-apis/blob/main/src/engine/paris.md#specification-1
fn is_consistent_forkchoice_state(
&self,
state: ForkchoiceState,
chain_update: Option<&NewCanonicalChain<N>>,
) -> ProviderResult<bool> {
let canonical_head_number = match chain_update {
Some(chain_update) => {
let new = chain_update.new_blocks();
// Only the canonical prefix below the new branch remains on the proposed chain.
new.first().expect("non empty chain").block_number() - 1
}
None => {
let Some(head) = self.find_canonical_header(state.head_block_hash)? else {
return Ok(false)
};
head.number()
}
};

for hash in [state.finalized_block_hash, state.safe_block_hash] {
if hash.is_zero() || chain_update.is_some_and(|update| update.contains(hash)) {
continue
}
let Some(header) = self.find_canonical_header(hash)? else { return Ok(false) };
if header.number() > canonical_head_number {
return Ok(false)
}
}
Ok(true)
}

/// Updates the tracked finalized block if we have it.
Expand Down
97 changes: 96 additions & 1 deletion crates/engine/tree/src/tree/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use reth_ethereum_primitives::{Block, EthPrimitives};
use reth_evm_ethereum::MockEvmConfig;
use reth_payload_builder::PayloadServiceCommand;
use reth_primitives_traits::Block as _;
use reth_provider::{test_utils::MockEthProvider, BalStoreHandle, InMemoryBalStore, RawBal};
use reth_provider::{
test_utils::MockEthProvider, BalStoreHandle, HeaderProvider, InMemoryBalStore, RawBal,
};
use reth_tasks::spawn_os_thread;
use reth_trie_common::ComputedTrieData;
use std::{
Expand Down Expand Up @@ -3350,3 +3352,96 @@ async fn test_on_backfill_sync_finished_opstack_retriggers_backfill_to_buffered_
async fn test_on_backfill_sync_finished_eth_retriggers_backfill_to_buffered_finalized() {
assert_post_backfill_recheck_retriggers_to_buffered_target(EngineApiKind::Ethereum).await;
}

/// A stale persisted hash must not count as an ancestor after an in-memory reorg.
#[test]
fn test_forkchoice_rejects_stale_persisted_prefix_hash() {
let mut test_harness = TestHarness::new(MAINNET.clone());
let old: Vec<_> = test_harness.block_builder.get_executed_blocks(0..3).collect();
test_harness = test_harness.with_blocks(old.clone());

let new: Vec<_> = test_harness
.block_builder
.create_fork(old[0].recovered_block(), 2)
.into_iter()
.map(|block| {
ExecutedBlock::new(
Arc::new(block),
Arc::new(BlockExecutionOutput::default()),
ComputedTrieData::default(),
)
})
.collect();
test_harness.tree.canonical_in_memory_state.update_chain(NewCanonicalChain::Reorg {
new: vec![new[0].clone()],
old: old[1..].to_vec(),
});

let state = ForkchoiceState {
head_block_hash: new[1].recovered_block().hash(),
safe_block_hash: old[1].recovered_block().hash(),
finalized_block_hash: old[0].recovered_block().hash(),
};
let update = NewCanonicalChain::Commit { new: vec![new[1].clone()] };
assert!(!test_harness.tree.is_consistent_forkchoice_state(state, Some(&update)).unwrap());

let state = ForkchoiceState { safe_block_hash: new[0].recovered_block().hash(), ..state };
assert!(test_harness.tree.is_consistent_forkchoice_state(state, Some(&update)).unwrap());
}

/// Reorgs from a persisted chain to a sibling branch of `sibling_len` blocks without advancing
/// persistence, then asserts that an FCU back to the reorged-out tip makes it canonical again.
async fn assert_fcu_back_to_reorged_out_head_with_pending_disk_reorg(sibling_len: u64) {
reth_tracing::init_test_tracing();
let mut test_harness = TestHarness::new(MAINNET.clone());
let old: Vec<_> = test_harness.block_builder.get_executed_blocks(0..4).collect();
test_harness = test_harness.with_blocks(old.clone());
let old_tip = old[3].recovered_block().hash();

let new: Vec<_> = test_harness
.block_builder
.create_fork(old[0].recovered_block(), sibling_len)
.into_iter()
.map(|block| {
ExecutedBlock::new(
Arc::new(block),
Arc::new(BlockExecutionOutput::default()),
ComputedTrieData::default(),
)
})
.collect();
for block in &new {
test_harness.tree.state.tree_state.insert_executed(block.clone());
}
let new_tip = new.last().unwrap().recovered_block().hash();

let fcu = |head_block_hash| ForkchoiceState {
head_block_hash,
safe_block_hash: B256::ZERO,
finalized_block_hash: B256::ZERO,
};

let outcome = test_harness.tree.on_forkchoice_updated(fcu(new_tip), None).unwrap();
assert_eq!(outcome.outcome.forkchoice_status(), ForkchoiceStatus::Valid);
assert_eq!(test_harness.tree.canonical_in_memory_state.get_canonical_head().hash(), new_tip);

// Persistence is not advanced, so the reorged-out blocks are still found on disk.
assert!(test_harness.provider.header(old_tip).unwrap().is_some());

let outcome = test_harness.tree.on_forkchoice_updated(fcu(old_tip), None).unwrap();
assert_eq!(outcome.outcome.forkchoice_status(), ForkchoiceStatus::Valid);
assert_eq!(test_harness.tree.state.tree_state.canonical_block_hash(), old_tip);
assert_eq!(test_harness.tree.canonical_in_memory_state.get_canonical_head().hash(), old_tip);
}

/// A reorged-out head must become canonical again while its disk reorg is still pending.
#[tokio::test]
async fn test_fcu_back_to_reorged_out_head_with_pending_disk_reorg() {
assert_fcu_back_to_reorged_out_head_with_pending_disk_reorg(3).await;
}

/// Same as above, but the reorged-out head is above the tip of the shorter sibling branch.
#[tokio::test]
async fn test_fcu_back_to_reorged_out_head_above_shorter_branch_with_pending_disk_reorg() {
assert_fcu_back_to_reorged_out_head_with_pending_disk_reorg(1).await;
}
Loading