diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index ef0fa4fabb3..adf6790bf0f 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -1001,12 +1001,22 @@ impl> NewCanonicalChain { /// Returns the new tip for [`Self::Reorg`] and [`Self::Commit`] variants which commit at least /// 1 new block. pub fn tip(&self) -> &RecoveredBlock { + 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] { 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)] diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index d45cfcab271..9e16234ec2e 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -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 @@ -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 @@ -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); @@ -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>, 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>, + ) -> ProviderResult { + 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. diff --git a/crates/engine/tree/src/tree/tests.rs b/crates/engine/tree/src/tree/tests.rs index 6830500088a..e4b88ebc1aa 100644 --- a/crates/engine/tree/src/tree/tests.rs +++ b/crates/engine/tree/src/tree/tests.rs @@ -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::{ @@ -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; +} diff --git a/crates/ethereum/node/tests/e2e/forkchoice.rs b/crates/ethereum/node/tests/e2e/forkchoice.rs new file mode 100644 index 00000000000..6e0e5806922 --- /dev/null +++ b/crates/ethereum/node/tests/e2e/forkchoice.rs @@ -0,0 +1,288 @@ +//! Tests for canonical chain and atomic forkchoice state updates via the Engine API. + +use crate::utils::eth_payload_attributes; +use alloy_eips::BlockNumberOrTag; +use alloy_primitives::B256; +use alloy_provider::{Provider, ProviderBuilder}; +use alloy_rpc_types_engine::{ForkchoiceState, PayloadStatusEnum}; +use jsonrpsee_core::client::Error; +use reth_chainspec::{ChainSpecBuilder, MAINNET}; +use reth_e2e_test_utils::{trie::wait_for_persisted_block, E2ETestSetupBuilder}; +use reth_node_ethereum::{EthEngineTypes, EthereumNode}; +use reth_provider::{DatabaseProviderFactory, HeaderProvider}; +use reth_rpc_api::{EngineApiClient, TestingBuildBlockRequestV1}; +use reth_rpc_server_types::{RethRpcModule, RpcModuleSelection}; +use std::{sync::Arc, time::Duration}; + +#[tokio::test] +async fn invalid_forkchoice_preserves_canonical_state() -> eyre::Result<()> { + reth_tracing::init_test_tracing(); + + let chain_spec = Arc::new( + ChainSpecBuilder::default() + .chain(MAINNET.chain) + .genesis(serde_json::from_str(include_str!("../assets/genesis.json")).unwrap()) + .cancun_activated() + .build(), + ); + let (mut nodes, _) = + E2ETestSetupBuilder::::new(2, chain_spec, eth_payload_attributes) + .with_connect_nodes(false) + .with_node_config_modifier(|mut config| { + config.rpc.http_api = + Some(RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Testing])); + config + }) + .build() + .await?; + let node = nodes.pop().unwrap(); + let producer = nodes.pop().unwrap(); + let genesis = node.block_hash(0); + let engine = node.auth_server_handle().http_client(); + let producer_engine = producer.auth_server_handle().http_client(); + let rpc = ProviderBuilder::new().connect_http(node.rpc_url()); + + // Build a1 <- a2 <- a3 and b1 <- b2, both rooted at genesis. Only chain A is canonical. + let mut chains = [vec![genesis], vec![genesis]]; + for (branch, length) in [3, 2].into_iter().enumerate() { + for number in 1..=length { + let envelope = producer + .testing_build_block_v1(TestingBuildBlockRequestV1 { + parent_block_hash: *chains[branch].last().unwrap(), + payload_attributes: eth_payload_attributes(number + branch as u64 * 10), + transactions: vec![], + extra_data: None, + }) + .await?; + let payload = envelope.execution_payload; + let hash = payload.payload_inner.payload_inner.block_hash; + for client in [&producer_engine, &engine] { + let status = EngineApiClient::::new_payload_v3( + client, + payload.clone(), + vec![], + B256::ZERO, + ) + .await?; + assert_eq!(status.status, PayloadStatusEnum::Valid); + } + producer.update_forkchoice(genesis, hash).await?; + chains[branch].push(hash); + } + if branch == 0 { + let status = EngineApiClient::::fork_choice_updated_v3( + &engine, + ForkchoiceState { + head_block_hash: chains[0][3], + safe_block_hash: genesis, + finalized_block_hash: genesis, + }, + None, + ) + .await?; + assert_eq!(status.payload_status.status, PayloadStatusEnum::Valid); + } + } + let [a, b] = chains; + + for (head, safe, finalized) in [ + (b[2], a[2], a[1]), + (b[2], a[2], b[1]), + (b[2], b[1], a[1]), + (b[2], B256::repeat_byte(0xff), b[1]), + (a[3], b[1], a[1]), + (a[2], a[3], genesis), + ] { + let state = ForkchoiceState { + head_block_hash: head, + safe_block_hash: safe, + finalized_block_hash: finalized, + }; + let err = EngineApiClient::::fork_choice_updated_v3(&engine, state, None) + .await + .unwrap_err(); + let Error::Call(err) = err else { panic!("Expected an RPC error, got {err:?}") }; + assert_eq!(err.code(), -38002, "{state:?}"); + + for (tag, hash) in [ + (BlockNumberOrTag::Latest, a[3]), + (BlockNumberOrTag::Safe, genesis), + (BlockNumberOrTag::Finalized, genesis), + (BlockNumberOrTag::Number(1), a[1]), + (BlockNumberOrTag::Number(2), a[2]), + (BlockNumberOrTag::Number(3), a[3]), + ] { + assert_eq!( + rpc.get_block_by_number(tag).await?.unwrap().header.hash, + hash, + "{tag:?} changed after rejecting {state:?}" + ); + } + } + + // Safe/finalized hashes on the proposed branch must be accepted before it is canonical. + let status = EngineApiClient::::fork_choice_updated_v3( + &engine, + ForkchoiceState { + head_block_hash: b[2], + safe_block_hash: b[2], + finalized_block_hash: b[1], + }, + None, + ) + .await?; + assert_eq!(status.payload_status.status, PayloadStatusEnum::Valid); + for (tag, hash) in [ + (BlockNumberOrTag::Latest, b[2]), + (BlockNumberOrTag::Safe, b[2]), + (BlockNumberOrTag::Finalized, b[1]), + (BlockNumberOrTag::Number(1), b[1]), + (BlockNumberOrTag::Number(2), b[2]), + ] { + assert_eq!(rpc.get_block_by_number(tag).await?.unwrap().header.hash, hash); + } + assert!(rpc.get_block_by_number(BlockNumberOrTag::Number(3)).await?.is_none()); + + // Invalid payload attributes must not roll back an otherwise valid forkchoice update. + let err = EngineApiClient::::fork_choice_updated_v3( + &engine, + ForkchoiceState::same_hash(b[2]), + Some(eth_payload_attributes(1)), + ) + .await + .unwrap_err(); + let Error::Call(err) = err else { panic!("Expected an RPC error, got {err:?}") }; + assert_eq!(err.code(), -38003); + assert_eq!( + rpc.get_block_by_number(BlockNumberOrTag::Finalized).await?.unwrap().header.hash, + b[2] + ); + + Ok(()) +} + +/// Restoring a persisted head must update canonical state before disk reorg cleanup completes. +#[tokio::test] +async fn fcu_restores_reorged_out_persisted_head() -> eyre::Result<()> { + assert_fcu_restores_reorged_out_persisted_head(3).await +} + +/// A stale persisted head above a shorter sibling branch must also become canonical again. +#[tokio::test] +async fn fcu_restores_reorged_out_persisted_head_above_shorter_branch() -> eyre::Result<()> { + assert_fcu_restores_reorged_out_persisted_head(1).await +} + +/// Holds the database writer lock across both FCUs so disk cleanup cannot win the race. +async fn assert_fcu_restores_reorged_out_persisted_head(sibling_len: u64) -> eyre::Result<()> { + reth_tracing::init_test_tracing(); + + let chain_spec = Arc::new( + ChainSpecBuilder::default() + .chain(MAINNET.chain) + .genesis(serde_json::from_str(include_str!("../assets/genesis.json")).unwrap()) + .cancun_activated() + .build(), + ); + let (mut nodes, _) = + E2ETestSetupBuilder::::new(2, chain_spec, eth_payload_attributes) + .with_connect_nodes(false) + .with_tree_config_modifier(|config| { + config + .with_num_state_masking_blocks(0) + .with_persistence_threshold(0) + .with_memory_block_buffer_target(0) + }) + .with_node_config_modifier(|mut config| { + config.rpc.http_api = + Some(RpcModuleSelection::from([RethRpcModule::Eth, RethRpcModule::Testing])); + config + }) + .build() + .await?; + let node = nodes.pop().unwrap(); + let producer = nodes.pop().unwrap(); + let genesis = node.block_hash(0); + let engine = node.auth_server_handle().http_client(); + let producer_engine = producer.auth_server_handle().http_client(); + let rpc = ProviderBuilder::new().connect_http(node.rpc_url()); + + // Persist A through block 3, then import a sibling branch without canonicalizing it. + let mut chains = [vec![genesis], vec![genesis]]; + for (branch, length) in [3, sibling_len].into_iter().enumerate() { + for number in 1..=length { + let envelope = producer + .testing_build_block_v1(TestingBuildBlockRequestV1 { + parent_block_hash: *chains[branch].last().unwrap(), + payload_attributes: eth_payload_attributes(number + branch as u64 * 10), + transactions: vec![], + extra_data: None, + }) + .await?; + let payload = envelope.execution_payload; + let hash = payload.payload_inner.payload_inner.block_hash; + for client in [&producer_engine, &engine] { + let status = EngineApiClient::::new_payload_v3( + client, + payload.clone(), + vec![], + B256::ZERO, + ) + .await?; + assert_eq!(status.status, PayloadStatusEnum::Valid); + } + producer.update_forkchoice(genesis, hash).await?; + chains[branch].push(hash); + } + if branch == 0 { + node.update_forkchoice(genesis, chains[0][3]).await?; + wait_for_persisted_block(&node.inner.provider, 3, Duration::from_secs(30)).await?; + } + } + let [a, b] = chains; + + // MDBX allows only one writer. Acquire it off the executor and keep it alive while the + // persistence task attempts to remove A, leaving the old headers visible on disk. + let provider = node.inner.provider.clone(); + let disk_reorg_guard = + tokio::task::spawn_blocking(move || provider.database_provider_rw()).await??; + for head in [*b.last().unwrap(), a[3]] { + let status = tokio::time::timeout( + Duration::from_secs(10), + EngineApiClient::::fork_choice_updated_v3( + &engine, + ForkchoiceState { + head_block_hash: head, + safe_block_hash: B256::ZERO, + finalized_block_hash: B256::ZERO, + }, + None, + ), + ) + .await??; + assert_eq!(status.payload_status.status, PayloadStatusEnum::Valid); + assert_eq!( + node.inner.provider.database_provider_ro()?.sealed_header(3)?.unwrap().hash(), + a[3], + "The old tip must remain on disk throughout both FCUs" + ); + assert_eq!( + rpc.get_block_by_number(BlockNumberOrTag::Latest).await?.unwrap().header.hash, + head, + "A VALID FCU must make the requested sibling tip canonical" + ); + } + for (number, hash) in a.into_iter().enumerate() { + assert_eq!( + rpc.get_block_by_number(BlockNumberOrTag::Number(number as u64)) + .await? + .unwrap() + .header + .hash, + hash + ); + } + drop(disk_reorg_guard); + + Ok(()) +} diff --git a/crates/ethereum/node/tests/e2e/main.rs b/crates/ethereum/node/tests/e2e/main.rs index 045998ffedc..7272b4fade8 100644 --- a/crates/ethereum/node/tests/e2e/main.rs +++ b/crates/ethereum/node/tests/e2e/main.rs @@ -5,6 +5,7 @@ mod custom_genesis; mod dev; mod eip6780; mod eth; +mod forkchoice; mod invalid_payload; mod p2p; mod pool;