From e17f08fe23e1b14d64f91af2b82affb16f5cb77a Mon Sep 17 00:00:00 2001 From: Einar Rasmussen Date: Wed, 14 Oct 2026 22:42:13 -0700 Subject: [PATCH 1/3] op-reth: live test of proofs execution across reth parent states Execute the call and BLOCKHASH blocks of a deploy, call, BLOCKHASH chain through the proofs engine against a real BlockchainProvider while a simulated reth moves the parent between memory, disk and an abandoned fork with reth's own persistence rule (threshold 7 and 50, masking 0 and 30, memory-block-buffer target 5). A proofs store hook fires reth's mid-execution transitions right after the engine snapshots the parent state. The abandoned-fork cases assert the bump's state-root failure and are ignored on develop, which skips such a parent with StateForHashNotFound. Co-Authored-By: Claude Fable 5.1 (cherry picked from commit 22bbc1dd534238d6fc793ba9e42ea431d95602b3) (cherry picked from commit 852fb64a2d328e6a3c53af9eee89c4a59982a3e7) --- rust/op-reth/crates/trie/Cargo.toml | 1 + rust/op-reth/crates/trie/tests/live.rs | 74 +- .../crates/trie/tests/live/parent_states.rs | 719 ++++++++++++++++++ 3 files changed, 780 insertions(+), 14 deletions(-) create mode 100644 rust/op-reth/crates/trie/tests/live/parent_states.rs diff --git a/rust/op-reth/crates/trie/Cargo.toml b/rust/op-reth/crates/trie/Cargo.toml index e35f72af183..24d410c4b09 100644 --- a/rust/op-reth/crates/trie/Cargo.toml +++ b/rust/op-reth/crates/trie/Cargo.toml @@ -68,6 +68,7 @@ reth-db = { workspace = true, features = ["test-utils"] } reth-db-api = { workspace = true, features = ["test-utils"] } reth-trie = { workspace = true, features = ["test-utils"] } reth-provider = { workspace = true, features = ["test-utils"] } +reth-chain-state.workspace = true reth-node-api.workspace = true alloy-consensus.workspace = true alloy-genesis.workspace = true diff --git a/rust/op-reth/crates/trie/tests/live.rs b/rust/op-reth/crates/trie/tests/live.rs index 26acd703190..14d12e8a182 100644 --- a/rust/op-reth/crates/trie/tests/live.rs +++ b/rust/op-reth/crates/trie/tests/live.rs @@ -31,6 +31,9 @@ use std::sync::Arc; use tempfile::TempDir; use test_case::test_case; +#[path = "live/parent_states.rs"] +mod parent_states; + fn create_mdbx_proofs_storage() -> Arc { let path = TempDir::new().unwrap().keep(); Arc::new(MdbxProofsStorage::new(&path).unwrap()) @@ -77,6 +80,16 @@ impl TxSpec { nonce: None, } } + + /// Create a contract deployment running `initcode`. + const fn create(initcode: Bytes, gas_limit: u64) -> Self { + Self { to: TxKind::Create, data: initcode, gas_limit, value: U256::ZERO, nonce: None } + } + + /// Create a call to `contract` with `data` as input. + const fn call(contract: Address, data: Bytes, gas_limit: u64) -> Self { + Self { to: TxKind::Call(contract), data, gas_limit, value: U256::ZERO, nonce: None } + } } /// Specification for a block in the test chain @@ -208,23 +221,49 @@ where Primitives: NodePrimitives, > + NodeTypesWithDB, { - let execution_outcome = ExecutionOutcome { - bundle: execution_output.state.clone(), - receipts: vec![execution_output.receipts.clone()], - first_block: block.number(), - requests: vec![execution_output.requests.clone()], - }; + commit_blocks_to_database([(block, execution_output)], provider_factory) +} + +/// Commits consecutive blocks and their execution outputs to the database in one transaction, +/// the way reth's persistence task does. +fn commit_blocks_to_database<'a, N>( + blocks: impl IntoIterator< + Item = (&'a RecoveredBlock, &'a reth_evm::execute::BlockExecutionOutput), + >, + provider_factory: &ProviderFactory, +) -> eyre::Result<()> +where + N: ProviderNodeTypes< + Primitives: NodePrimitives, + > + NodeTypesWithDB, +{ + let mut recovered_blocks = Vec::new(); + let mut execution_outcome: Option = None; + for (block, execution_output) in blocks { + let block_outcome = ExecutionOutcome { + bundle: execution_output.state.clone(), + receipts: vec![execution_output.receipts.clone()], + first_block: block.number(), + requests: vec![execution_output.requests.clone()], + }; + match execution_outcome.as_mut() { + Some(outcome) => outcome.extend(block_outcome), + None => execution_outcome = Some(block_outcome), + } + recovered_blocks.push(block.clone()); + } + let Some(execution_outcome) = execution_outcome else { return Ok(()) }; // Calculate hashed state from execution result let state_provider = provider_factory.provider()?; let hashed_state = HashedPostStateProvider::hashed_post_state( &LatestStateProviderRef::new(&state_provider), - &execution_output.state, + &execution_outcome.bundle, )?; let provider_rw = provider_factory.provider_rw()?; provider_rw.append_blocks_with_state( - vec![block.clone()], + recovered_blocks, &execution_outcome, hashed_state.into_sorted(), )?; @@ -233,6 +272,17 @@ where Ok(()) } +/// The layout of reth's trie tables, which the initialization job copies from. +fn reth_trie_layout( + provider_factory: &ProviderFactory, +) -> RethTrieStorageLayout { + if provider_factory.cached_storage_settings().is_v2() { + RethTrieStorageLayout::Packed + } else { + RethTrieStorageLayout::Legacy + } +} + /// Runs a test scenario with the given configuration fn run_test_scenario( scenario: TestScenario, @@ -273,14 +323,10 @@ where } { - let trie_layout = if provider_factory.cached_storage_settings().is_v2() { - RethTrieStorageLayout::Packed - } else { - RethTrieStorageLayout::Legacy - }; let provider = provider_factory.db_ref(); let tx = provider.tx()?; - let initialization_job = InitializationJob::new(storage.clone(), tx, trie_layout); + let initialization_job = + InitializationJob::new(storage.clone(), tx, reth_trie_layout(&provider_factory)); initialization_job.run(last_block_number, last_block_hash)?; } diff --git a/rust/op-reth/crates/trie/tests/live/parent_states.rs b/rust/op-reth/crates/trie/tests/live/parent_states.rs new file mode 100644 index 00000000000..9099e13e662 --- /dev/null +++ b/rust/op-reth/crates/trie/tests/live/parent_states.rs @@ -0,0 +1,719 @@ +//! Executes proofs blocks through the engine while reth's persistence moves their parent between +//! memory, disk and an abandoned fork. +//! +//! reth makes a block canonical in memory first and persists it later, so by the time the engine +//! executes a block its parent can be in memory, on disk and evicted from memory, or no longer +//! canonical at all. The cases here drive reth's canonical in-memory state and database with +//! reth's own persistence rule and execute the call and `BLOCKHASH` blocks of a short chain in +//! each of those parent states. + +use super::{ + BlockSpec, TxSpec, chain_spec_with_address, commit_blocks_to_database, create_block_from_spec, + create_mdbx_proofs_storage, public_key_to_address, reth_trie_layout, +}; +use alloy_consensus::BlockHeader; +use alloy_eips::BlockNumHash; +use alloy_primitives::{Address, B256, Bytes, U256}; +use parking_lot::Mutex; +use reth_chain_state::{CanonicalInMemoryState, ExecutedBlock, NewCanonicalChain}; +use reth_chainspec::ChainSpec; +use reth_db::Database; +use reth_db_common::init::init_genesis; +use reth_ethereum_primitives::Block; +use reth_evm::{ConfigureEvm, execute::Executor}; +use reth_evm_ethereum::EthEvmConfig; +use reth_optimism_trie::{ + MdbxProofsStorage, OpProofStoragePruner, OpProofsProviderRO, OpProofsStorageResult, + OpProofsStore, + engine::{EngineError, EngineHandle}, + initialize::InitializationJob, + provider::OpProofsStateProviderRef, +}; +use reth_primitives_traits::RecoveredBlock; +use reth_provider::{ + HashedPostStateProvider, ProviderFactory, StateProvider, StateProviderFactory, + StateRootProvider, + noop::NoopProvider, + providers::BlockchainProvider, + test_utils::{MockNodeTypesWithDB, create_test_provider_factory_with_chain_spec}, +}; +use reth_revm::database::StateProviderDatabase; +use reth_trie_common::{ComputedTrieData, HashedPostState}; +use rstest::rstest; +use secp256k1::{Keypair, Secp256k1, rand::rng}; +use serial_test::serial; +use std::{fmt, sync::Arc, thread}; + +/// reth's default `--engine.memory-block-buffer-target`. +const MEMORY_BLOCK_BUFFER_TARGET: u64 = 5; + +/// Covers the contract's deployment and either of its calls. +const CONTRACT_GAS_LIMIT: u64 = 100_000; + +/// Storage slot every call to the contract sets to one. +const CALLED_SLOT: B256 = B256::ZERO; + +/// Storage slot a call with calldata sets to `BLOCKHASH(calldata)`. +const BLOCK_HASH_SLOT: B256 = B256::with_last_byte(1); + +/// Runtime code of the contract: `slot 0 = 1`, then `slot 1 = BLOCKHASH(calldata[0..32])` unless +/// the call has no calldata. +/// +/// ```text +/// PUSH1 1 PUSH1 0 SSTORE +/// CALLDATASIZE ISZERO PUSH1 0x11 JUMPI +/// PUSH1 0 CALLDATALOAD BLOCKHASH PUSH1 1 SSTORE +/// JUMPDEST STOP +/// ``` +const CONTRACT_RUNTIME: [u8; 19] = [ + 0x60, 0x01, 0x60, 0x00, 0x55, 0x36, 0x15, 0x60, 0x11, 0x57, 0x60, 0x00, 0x35, 0x40, 0x60, 0x01, + 0x55, 0x5b, 0x00, +]; + +/// Initcode returning [`CONTRACT_RUNTIME`], which follows the twelve initcode bytes: +/// `PUSH1 len PUSH1 12 PUSH1 0 CODECOPY PUSH1 len PUSH1 0 RETURN`. +fn contract_initcode() -> Bytes { + let runtime_len = CONTRACT_RUNTIME.len() as u8; + let mut initcode = + vec![0x60, runtime_len, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, runtime_len, 0x60, 0x00, 0xf3]; + initcode.extend_from_slice(&CONTRACT_RUNTIME); + initcode.into() +} + +fn block_number_calldata(number: u64) -> Bytes { + Bytes::copy_from_slice(&U256::from(number).to_be_bytes::<32>()) +} + +/// Where reth holds the parent of the block under test when the engine executes the block. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum ParentState { + /// Canonical and in memory, as every recent block is. + InMemory, + /// Persisted and evicted from memory because reth ran ahead of the engine. + PersistedAndEvicted, + /// Persisted but kept in memory by state masking. + PersistedAndMasked, + /// In memory when execution starts, persisted and evicted before execution reads the parent. + EvictedWhileExecuting, + /// Reorged out of memory before execution, never persisted. + AbandonedFork, + /// In memory when execution starts, reorged out with its ancestry before execution reads a + /// block hash. + ReorgedWhileExecuting, +} + +/// Which block of the deploy, call, `BLOCKHASH` chain the engine executes in the parent state. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum BlockContent { + /// The block calling the contract its parent deployed. + DeployAndCall, + /// The block storing `BLOCKHASH` of the deployment two blocks below it. + BlockHash, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Outcome { + /// The engine stores the block and the stored state hashes to the block's state root. + Stored, + /// The engine fails the state-root check and stores nothing. + StateRootMismatch, +} + +/// reth's persistence settings. When the canonical head runs more than `persistence_threshold` +/// blocks past the database tip, reth persists every block but the newest +/// `memory_block_buffer_target` and evicts the persisted blocks from memory, except the newest +/// `num_state_masking_blocks` of them, whose hashed state and trie it has not written yet. +#[derive(Clone, Copy, Debug)] +struct RethPersistence { + memory_block_buffer_target: u64, + num_state_masking_blocks: u64, + persistence_threshold: u64, +} + +impl RethPersistence { + fn new( + persistence_threshold: u64, + num_state_masking_blocks: u64, + memory_block_buffer_target: u64, + ) -> Self { + assert!( + memory_block_buffer_target <= persistence_threshold, + "reth keeps at most persistence_threshold blocks in memory" + ); + assert!( + num_state_masking_blocks == 0 || + num_state_masking_blocks + memory_block_buffer_target < persistence_threshold, + "reth rejects num_state_masking_blocks + memory_block_buffer_target >= persistence_threshold" + ); + Self { memory_block_buffer_target, num_state_masking_blocks, persistence_threshold } + } +} + +fn reth(persistence_threshold: u64, num_state_masking_blocks: u64) -> RethPersistence { + RethPersistence::new( + persistence_threshold, + num_state_masking_blocks, + MEMORY_BLOCK_BUFFER_TARGET, + ) +} + +/// reth's database and canonical in-memory state, driven the way reth's engine tree and +/// persistence task drive them: a block is canonical in memory first and reaches the database +/// when the persistence rule fires. +struct SimulatedReth { + /// The canonical chain above genesis, indexed by block number minus one. + canonical: Vec, + chain_spec: Arc, + evm_config: EthEvmConfig, + factory: ProviderFactory, + in_memory: CanonicalInMemoryState, + key_pair: Keypair, + /// Newest block in the database. + last_persisted: BlockNumHash, + /// Newest block whose hashed state and trie are in the database; persisted blocks above it + /// stay in memory. + last_state_trie_persisted: BlockNumHash, + nonce: u64, + persistence: RethPersistence, + provider: BlockchainProvider, +} + +impl SimulatedReth { + fn new( + chain_spec: Arc, + key_pair: Keypair, + persistence: RethPersistence, + ) -> eyre::Result { + let factory = create_test_provider_factory_with_chain_spec(chain_spec.clone()); + init_genesis(&factory)?; + let provider = BlockchainProvider::new(factory.clone())?; + let genesis = BlockNumHash::new(0, chain_spec.genesis_hash()); + Ok(Self { + canonical: Vec::new(), + evm_config: EthEvmConfig::ethereum(chain_spec.clone()), + chain_spec, + in_memory: provider.canonical_in_memory_state(), + factory, + key_pair, + last_persisted: genesis, + last_state_trie_persisted: genesis, + nonce: 0, + persistence, + provider, + }) + } + + fn num_hash(&self, number: u64) -> BlockNumHash { + match number { + 0 => BlockNumHash::new(0, self.chain_spec.genesis_hash()), + _ => self.block(number).num_hash(), + } + } + + fn head(&self) -> BlockNumHash { + self.num_hash(self.canonical.len() as u64) + } + + fn block(&self, number: u64) -> &RecoveredBlock { + self.canonical[number as usize - 1].recovered_block() + } + + const fn is_persisted(&self, block: BlockNumHash) -> bool { + self.last_persisted.number >= block.number + } + + fn is_in_memory(&self, block: BlockNumHash) -> bool { + self.in_memory.state_by_hash(block.hash).is_some() + } + + /// Appends a block to the canonical chain and runs the persistence rule. + fn commit(&mut self, spec: &BlockSpec) -> eyre::Result> { + let block = self.build_block(spec, self.head())?; + let recovered = block.recovered_block().clone(); + self.make_canonical(NewCanonicalChain::Commit { new: vec![block] }); + self.advance_persistence()?; + Ok(recovered) + } + + /// Appends empty blocks until `reached` holds, which the persistence rule must bring about + /// within one cycle. + fn commit_fillers_until(&mut self, reached: impl Fn(&Self) -> bool) -> eyre::Result<()> { + let limit = self.head().number + self.persistence.persistence_threshold + 1; + while !reached(self) { + assert!( + self.head().number < limit, + "the persistence rule did not reach the requested parent state within one cycle" + ); + self.commit(&BlockSpec::new(vec![]))?; + } + Ok(()) + } + + /// Replaces the canonical blocks from `number` up with empty blocks on the same ancestor, the + /// way a reorg to a sibling fork does. + fn reorg_from(&mut self, number: u64) -> eyre::Result<()> { + assert!(number > self.last_persisted.number, "only in-memory blocks can be reorged"); + let old = self.canonical.split_off(number as usize - 1); + let fork = self.build_block(&BlockSpec::new(vec![]), self.head())?; + assert_ne!( + fork.recovered_block().hash(), + old[0].recovered_block().hash(), + "the fork must differ from the chain it replaces" + ); + let depth = old.len(); + self.make_canonical(NewCanonicalChain::Reorg { new: vec![fork], old }); + for _ in 1..depth { + self.commit(&BlockSpec::new(vec![]))?; + } + Ok(()) + } + + fn make_canonical(&mut self, update: NewCanonicalChain) { + let tip = update.tip().clone_sealed_header(); + match &update { + NewCanonicalChain::Commit { new } | NewCanonicalChain::Reorg { new, .. } => { + self.canonical.extend(new.iter().cloned()); + } + } + self.in_memory.update_chain(update); + self.in_memory.set_canonical_head(tip); + } + + /// Builds `spec` on `parent` and executes it, giving it the state root and trie data reth + /// holds for a block it validated. + fn build_block( + &mut self, + spec: &BlockSpec, + parent: BlockNumHash, + ) -> eyre::Result { + let mut block = create_block_from_spec( + spec, + parent.number + 1, + parent.hash, + &self.chain_spec, + self.key_pair, + &mut self.nonce, + ); + let parent_state = self.provider.state_by_block_hash(parent.hash)?; + let output = self + .evm_config + .batch_executor(StateProviderDatabase::new(&parent_state)) + .execute(&block)?; + let hashed_state = parent_state.hashed_post_state(&output.state)?; + let (state_root, trie_updates) = + parent_state.state_root_with_updates(hashed_state.clone())?; + block.set_state_root(state_root); + let trie_data = ComputedTrieData::new( + Arc::new(hashed_state.into_sorted()), + Arc::new(trie_updates.into_sorted()), + ); + Ok(ExecutedBlock::new(Arc::new(block), Arc::new(output), trie_data)) + } + + /// reth's persistence rule, run after every new canonical block. + fn advance_persistence(&mut self) -> eyre::Result<()> { + let head = self.head().number; + if head - self.last_persisted.number <= self.persistence.persistence_threshold { + return Ok(()); + } + let new_db_tip = head.saturating_sub(self.persistence.memory_block_buffer_target); + if new_db_tip <= self.last_persisted.number { + return Ok(()); + } + let new_state_trie_tip = new_db_tip + .saturating_sub(self.persistence.num_state_masking_blocks) + .max(self.last_state_trie_persisted.number); + self.persist_through(new_db_tip)?; + self.evict_through(new_state_trie_tip); + Ok(()) + } + + /// Writes the canonical blocks above the database tip through `number` to the database. + fn persist_through(&mut self, number: u64) -> eyre::Result<()> { + let first = self.last_persisted.number as usize; + let blocks = &self.canonical[first..number as usize]; + commit_blocks_to_database( + blocks.iter().map(|block| (block.recovered_block(), block.execution_outcome())), + &self.factory, + )?; + self.last_persisted = self.num_hash(number); + Ok(()) + } + + /// Drops the persisted blocks through `number` from memory. + fn evict_through(&mut self, number: u64) { + assert!(number <= self.last_persisted.number, "only persisted blocks leave memory"); + self.in_memory.remove_persisted_blocks_until(self.last_persisted, number); + self.last_state_trie_persisted = self.num_hash(number); + } +} + +type ReadHook = Box; + +/// A proofs store that runs an action on reth right before the engine opens its next read view. +/// +/// The engine's execute task snapshots reth's parent state before it opens the proofs read view +/// it executes against, so an action armed here lands after the snapshot and before execution. +#[derive(Clone)] +struct StoreWithReadHook { + inner: S, + on_next_read: Arc>>, +} + +impl StoreWithReadHook { + fn new(inner: S) -> Self { + Self { inner, on_next_read: Arc::default() } + } + + fn on_next_read(&self, action: impl FnOnce() + Send + 'static) { + *self.on_next_read.lock() = Some(Box::new(action)); + } + + fn is_armed(&self) -> bool { + self.on_next_read.lock().is_some() + } +} + +impl fmt::Debug for StoreWithReadHook { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("StoreWithReadHook").field("inner", &self.inner).finish_non_exhaustive() + } +} + +impl OpProofsStore for StoreWithReadHook { + type ProviderRO<'a> + = S::ProviderRO<'a> + where + Self: 'a; + + type ProviderRw<'a> + = S::ProviderRw<'a> + where + Self: 'a; + + type Initializer<'a> + = S::Initializer<'a> + where + Self: 'a; + + fn provider_ro<'a>(&'a self) -> OpProofsStorageResult> { + let action = self.on_next_read.lock().take(); + if let Some(action) = action { + // reth's persistence runs on its own thread; so does the action, which keeps the + // engine thread's open database transactions out of its way. + thread::scope(|scope| scope.spawn(action).join()).expect("the read hook panicked"); + } + self.inner.provider_ro() + } + + fn provider_rw<'a>(&'a self) -> OpProofsStorageResult> { + self.inner.provider_rw() + } + + fn initialization_provider<'a>(&'a self) -> OpProofsStorageResult> { + self.inner.initialization_provider() + } +} + +/// The chain reth holds before the engine runs: `leading` empty blocks, the deployment, the call +/// and the `BLOCKHASH` read, with the index of the block under test. +struct ProofChain { + blocks: Vec>, + under_test: usize, +} + +impl ProofChain { + fn commit( + reth: &mut SimulatedReth, + leading: u64, + contract: Address, + content: BlockContent, + ) -> eyre::Result { + let mut blocks = Vec::new(); + for _ in 0..leading { + blocks.push(reth.commit(&BlockSpec::new(vec![]))?); + } + let deploy_number = leading + 1; + let specs = [ + BlockSpec::new(vec![TxSpec::create(contract_initcode(), CONTRACT_GAS_LIMIT)]), + BlockSpec::new(vec![TxSpec::call(contract, Bytes::new(), CONTRACT_GAS_LIMIT)]), + BlockSpec::new(vec![TxSpec::call( + contract, + block_number_calldata(deploy_number), + CONTRACT_GAS_LIMIT, + )]), + ]; + for spec in &specs { + blocks.push(reth.commit(spec)?); + } + let under_test = match content { + BlockContent::DeployAndCall => leading + 1, + BlockContent::BlockHash => leading + 2, + } as usize; + Ok(Self { blocks, under_test }) + } + + fn deploy(&self) -> &RecoveredBlock { + &self.blocks[self.blocks.len() - 3] + } + + fn under_test(&self) -> &RecoveredBlock { + &self.blocks[self.under_test] + } + + fn parent(&self) -> BlockNumHash { + self.blocks[self.under_test - 1].num_hash() + } + + /// The blocks the engine executes before the block under test. + fn preceding(&self) -> &[RecoveredBlock] { + &self.blocks[..self.under_test] + } +} + +/// How many empty blocks precede the deployment so that the parent ends up in `parent_state`. +/// +/// The first persistence cycle fires at head `persistence_threshold + 1` and evicts through +/// `persistence_threshold + 1 - memory_block_buffer_target - num_state_masking_blocks`; a +/// deployment right above that boundary is persisted but masked, so it stays in memory. +const fn leading_blocks(persistence: RethPersistence, parent_state: ParentState) -> u64 { + match parent_state { + ParentState::PersistedAndMasked => { + persistence.persistence_threshold + 1 - + persistence.memory_block_buffer_target - + persistence.num_state_masking_blocks + } + _ => 0, + } +} + +fn parent_persisted_and_evicted(reth: &SimulatedReth, parent: BlockNumHash) -> bool { + reth.is_persisted(parent) && !reth.is_in_memory(parent) +} + +/// Puts the parent into the states reth reaches before the engine executes the block. +fn arrange_parent_state( + reth: &mut SimulatedReth, + parent_state: ParentState, + chain: &ProofChain, +) -> eyre::Result<()> { + let parent = chain.parent(); + match parent_state { + ParentState::PersistedAndEvicted => { + reth.commit_fillers_until(|reth| parent_persisted_and_evicted(reth, parent))?; + } + ParentState::PersistedAndMasked => { + reth.commit_fillers_until(|reth| reth.is_persisted(parent))?; + assert!(reth.is_in_memory(parent), "masking must keep the persisted parent in memory"); + } + ParentState::AbandonedFork => reth.reorg_from(parent.number)?, + ParentState::InMemory | + ParentState::EvictedWhileExecuting | + ParentState::ReorgedWhileExecuting => {} + } + let expected_in_memory = + !matches!(parent_state, ParentState::PersistedAndEvicted | ParentState::AbandonedFork); + assert_eq!(reth.is_in_memory(parent), expected_in_memory, "parent state before execution"); + Ok(()) +} + +/// Arms the reth transitions that happen while the engine executes the block. +fn arm_in_flight_transition( + store: &StoreWithReadHook>, + reth: &Arc>, + parent_state: ParentState, + chain: &ProofChain, +) { + let parent = chain.parent(); + let deploy_number = chain.deploy().number(); + let reth = Arc::clone(reth); + match parent_state { + ParentState::EvictedWhileExecuting => store.on_next_read(move || { + reth.lock() + .commit_fillers_until(|reth| parent_persisted_and_evicted(reth, parent)) + .expect("reth persists and evicts the parent"); + }), + ParentState::ReorgedWhileExecuting => store.on_next_read(move || { + reth.lock().reorg_from(deploy_number).expect("reth reorgs below the block"); + }), + _ => {} + } +} + +fn assert_parent_state_after_execution( + reth: &SimulatedReth, + parent_state: ParentState, + chain: &ProofChain, +) { + let parent = chain.parent(); + match parent_state { + ParentState::InMemory => assert!(reth.is_in_memory(parent) && !reth.is_persisted(parent)), + ParentState::PersistedAndMasked => { + assert!(reth.is_in_memory(parent) && reth.is_persisted(parent)); + } + ParentState::PersistedAndEvicted | ParentState::EvictedWhileExecuting => { + assert!(parent_persisted_and_evicted(reth, parent)); + } + ParentState::AbandonedFork | ParentState::ReorgedWhileExecuting => { + assert!(!reth.is_in_memory(parent) && !reth.is_persisted(parent)); + assert_ne!(reth.block(parent.number).hash(), parent.hash, "a fork replaced the parent"); + if parent_state == ParentState::ReorgedWhileExecuting { + let deploy = chain.deploy(); + assert_ne!(reth.block(deploy.number()).hash(), deploy.hash()); + } + } + } +} + +/// Checks the stored block against reth's header: the proofs state hashes to the header's state +/// root, the call wrote its slot and a `BLOCKHASH` read saw the deployment on the parent's +/// ancestry. +fn assert_stored( + store: &Arc, + chain: &ProofChain, + content: BlockContent, + contract: Address, +) -> eyre::Result<()> { + let under_test = chain.under_test(); + let proofs = store.provider_ro()?; + assert_eq!(proofs.get_latest_block()?, under_test.num_hash()); + let state = OpProofsStateProviderRef::new( + Box::new(NoopProvider::default()), + proofs, + under_test.number(), + ); + assert_eq!( + state.state_root(HashedPostState::default())?, + under_test.state_root(), + "the stored state must hash to the header's state root" + ); + assert_eq!(state.storage(contract, CALLED_SLOT)?, Some(U256::from(1))); + if content == BlockContent::BlockHash { + assert_eq!( + state.storage(contract, BLOCK_HASH_SLOT)?, + Some(U256::from_be_bytes(chain.deploy().hash().0)), + "BLOCKHASH must come from the parent's ancestry" + ); + } + Ok(()) +} + +fn run_parent_state_case( + persistence: RethPersistence, + content: BlockContent, + parent_state: ParentState, + outcome: Outcome, +) -> eyre::Result<()> { + let key_pair = Keypair::new(&Secp256k1::new(), &mut rng()); + let sender = public_key_to_address(key_pair.public_key()); + let contract = sender.create(0); + let chain_spec = chain_spec_with_address(sender); + let store = create_mdbx_proofs_storage(); + let mut reth = SimulatedReth::new(chain_spec.clone(), key_pair, persistence)?; + { + let tx = reth.factory.db_ref().tx()?; + InitializationJob::new(store.clone(), tx, reth_trie_layout(&reth.factory)) + .run(0, chain_spec.genesis_hash())?; + } + let chain = ProofChain::commit( + &mut reth, + leading_blocks(persistence, parent_state), + contract, + content, + )?; + arrange_parent_state(&mut reth, parent_state, &chain)?; + + let provider = reth.provider.clone(); + let reth = Arc::new(Mutex::new(reth)); + let store_with_hook = StoreWithReadHook::new(store.clone()); + let pruner = OpProofStoragePruner::new(store_with_hook.clone(), provider.clone(), 1000); + // The engine writes its buffer to the store once it holds this many blocks, and `flush` only + // waits for that write, so the threshold is the number of blocks the case stores. + let stored_blocks = match outcome { + Outcome::Stored => chain.preceding().len() + 1, + Outcome::StateRootMismatch => chain.preceding().len(), + } as u64; + let engine = EngineHandle::spawn_with_thresholds( + EthEvmConfig::ethereum(chain_spec), + provider, + store_with_hook.clone(), + pruner, + stored_blocks, + stored_blocks + 1, + ); + for block in chain.preceding() { + engine.execute_block(block)?; + } + + arm_in_flight_transition(&store_with_hook, &reth, parent_state, &chain); + let result = engine.execute_block(chain.under_test()); + engine.flush(); + assert!( + !store_with_hook.is_armed(), + "the engine must open its proofs read view after snapshotting reth's parent state" + ); + assert_parent_state_after_execution(&reth.lock(), parent_state, &chain); + + match outcome { + Outcome::Stored => { + result?; + assert_stored(&store, &chain, content, contract) + } + Outcome::StateRootMismatch => { + let err = result.expect_err("an abandoned parent must fail the state-root check"); + assert!( + matches!( + err, + EngineError::StateRootMismatch { block_number, .. } + if block_number == chain.under_test().number() + ), + "{err:?}" + ); + assert_eq!(store.provider_ro()?.get_latest_block()?, chain.parent()); + Ok(()) + } + } +} + +/// The parent states reth reaches on its own, over reth's persistence settings and both blocks. +/// +/// reth rejects `num_state_masking_blocks + memory_block_buffer_target >= +/// persistence_threshold`, so threshold 7 only runs unmasked. +#[rstest] +#[case::in_memory(ParentState::InMemory, Outcome::Stored)] +#[case::persisted_and_evicted(ParentState::PersistedAndEvicted, Outcome::Stored)] +#[case::evicted_while_executing(ParentState::EvictedWhileExecuting, Outcome::Stored)] +#[ignore = "passes on #23046; develop skips a parent reth no longer holds (StateForHashNotFound) and stores nothing"] +#[case::abandoned_fork(ParentState::AbandonedFork, Outcome::StateRootMismatch)] +#[serial] +fn execute_block_across_reth_parent_states( + #[values(reth(7, 0), reth(50, 0), reth(50, 30))] persistence: RethPersistence, + #[values(BlockContent::DeployAndCall, BlockContent::BlockHash)] content: BlockContent, + #[case] parent_state: ParentState, + #[case] outcome: Outcome, +) -> eyre::Result<()> { + run_parent_state_case(persistence, content, parent_state, outcome) +} + +/// State masking keeps persisted blocks in memory, so the parent is on disk and in memory at once. +#[rstest] +#[serial] +fn execute_block_with_persisted_and_masked_parent( + #[values(BlockContent::DeployAndCall, BlockContent::BlockHash)] content: BlockContent, +) -> eyre::Result<()> { + run_parent_state_case(reth(50, 30), content, ParentState::PersistedAndMasked, Outcome::Stored) +} + +/// `BLOCKHASH` reads the parent's ancestry even after reth reorged it out mid-execution. +#[rstest] +#[serial] +fn execute_block_hash_read_while_reth_reorgs_below_it( + #[values(reth(7, 0), reth(50, 0), reth(50, 30))] persistence: RethPersistence, +) -> eyre::Result<()> { + run_parent_state_case( + persistence, + BlockContent::BlockHash, + ParentState::ReorgedWhileExecuting, + Outcome::Stored, + ) +} From 085152ca5c881d47acfddc96a6b7eaddb46b0c68 Mon Sep 17 00:00:00 2001 From: Einar Rasmussen Date: Wed, 14 Oct 2026 23:49:28 -0700 Subject: [PATCH 2/3] op-reth: address review of the proofs parent-state live test Persist with reth's own save_blocks writer, keep live.rs's single-block helper and share its storage initialization, state the read hook's buffered-blocks precondition and check it right after execution, assert block locations uniformly, add the BLOCKHASH case whose deployment sits below reth's memory, name the persistence settings, use a fixed signer and run the hook inline. Co-Authored-By: Claude Fable 5.1 (cherry picked from commit cf9f0ca8fe19cc87c44885fdad69e47c8e94cfa1) (cherry picked from commit 6db279118b71060163e09b3b2208f6976e625bb0) --- rust/op-reth/crates/trie/tests/live.rs | 74 ++-- .../crates/trie/tests/live/parent_states.rs | 352 +++++++++++------- 2 files changed, 248 insertions(+), 178 deletions(-) diff --git a/rust/op-reth/crates/trie/tests/live.rs b/rust/op-reth/crates/trie/tests/live.rs index 14d12e8a182..66c52a39654 100644 --- a/rust/op-reth/crates/trie/tests/live.rs +++ b/rust/op-reth/crates/trie/tests/live.rs @@ -1,6 +1,7 @@ //! End-to-end test of the live trie collector. use alloy_consensus::{BlockHeader, Header, TxEip2930, constants::ETH_TO_WEI}; +use alloy_eips::BlockNumHash; use alloy_genesis::{Genesis, GenesisAccount}; use alloy_primitives::{Address, B256, Bytes, TxKind, U256, keccak256}; use derive_more::Constructor; @@ -221,49 +222,23 @@ where Primitives: NodePrimitives, > + NodeTypesWithDB, { - commit_blocks_to_database([(block, execution_output)], provider_factory) -} - -/// Commits consecutive blocks and their execution outputs to the database in one transaction, -/// the way reth's persistence task does. -fn commit_blocks_to_database<'a, N>( - blocks: impl IntoIterator< - Item = (&'a RecoveredBlock, &'a reth_evm::execute::BlockExecutionOutput), - >, - provider_factory: &ProviderFactory, -) -> eyre::Result<()> -where - N: ProviderNodeTypes< - Primitives: NodePrimitives, - > + NodeTypesWithDB, -{ - let mut recovered_blocks = Vec::new(); - let mut execution_outcome: Option = None; - for (block, execution_output) in blocks { - let block_outcome = ExecutionOutcome { - bundle: execution_output.state.clone(), - receipts: vec![execution_output.receipts.clone()], - first_block: block.number(), - requests: vec![execution_output.requests.clone()], - }; - match execution_outcome.as_mut() { - Some(outcome) => outcome.extend(block_outcome), - None => execution_outcome = Some(block_outcome), - } - recovered_blocks.push(block.clone()); - } - let Some(execution_outcome) = execution_outcome else { return Ok(()) }; + let execution_outcome = ExecutionOutcome { + bundle: execution_output.state.clone(), + receipts: vec![execution_output.receipts.clone()], + first_block: block.number(), + requests: vec![execution_output.requests.clone()], + }; // Calculate hashed state from execution result let state_provider = provider_factory.provider()?; let hashed_state = HashedPostStateProvider::hashed_post_state( &LatestStateProviderRef::new(&state_provider), - &execution_outcome.bundle, + &execution_output.state, )?; let provider_rw = provider_factory.provider_rw()?; provider_rw.append_blocks_with_state( - recovered_blocks, + vec![block.clone()], &execution_outcome, hashed_state.into_sorted(), )?; @@ -272,15 +247,24 @@ where Ok(()) } -/// The layout of reth's trie tables, which the initialization job copies from. -fn reth_trie_layout( +/// Seeds `storage` with reth's state and trie at `block`, the proofs store's starting point. +fn initialize_storage( + storage: S, provider_factory: &ProviderFactory, -) -> RethTrieStorageLayout { - if provider_factory.cached_storage_settings().is_v2() { + block: BlockNumHash, +) -> eyre::Result<()> +where + N: NodeTypesWithDB, + S: OpProofsStore + Send + Sync + Clone + std::fmt::Debug + 'static, +{ + let trie_layout = if provider_factory.cached_storage_settings().is_v2() { RethTrieStorageLayout::Packed } else { RethTrieStorageLayout::Legacy - } + }; + let tx = provider_factory.db_ref().tx()?; + InitializationJob::new(storage, tx, trie_layout).run(block.number, block.hash)?; + Ok(()) } /// Runs a test scenario with the given configuration @@ -322,13 +306,11 @@ where last_block_number = block_number; } - { - let provider = provider_factory.db_ref(); - let tx = provider.tx()?; - let initialization_job = - InitializationJob::new(storage.clone(), tx, reth_trie_layout(&provider_factory)); - initialization_job.run(last_block_number, last_block_hash)?; - } + initialize_storage( + storage.clone(), + &provider_factory, + BlockNumHash::new(last_block_number, last_block_hash), + )?; // Execute blocks after initialization using live collector. // A single collector is shared across all blocks so the in-memory buffer accumulates diff --git a/rust/op-reth/crates/trie/tests/live/parent_states.rs b/rust/op-reth/crates/trie/tests/live/parent_states.rs index 9099e13e662..f2695b6e483 100644 --- a/rust/op-reth/crates/trie/tests/live/parent_states.rs +++ b/rust/op-reth/crates/trie/tests/live/parent_states.rs @@ -8,8 +8,8 @@ //! each of those parent states. use super::{ - BlockSpec, TxSpec, chain_spec_with_address, commit_blocks_to_database, create_block_from_spec, - create_mdbx_proofs_storage, public_key_to_address, reth_trie_layout, + BlockSpec, TxSpec, chain_spec_with_address, create_block_from_spec, create_mdbx_proofs_storage, + initialize_storage, public_key_to_address, }; use alloy_consensus::BlockHeader; use alloy_eips::BlockNumHash; @@ -17,7 +17,6 @@ use alloy_primitives::{Address, B256, Bytes, U256}; use parking_lot::Mutex; use reth_chain_state::{CanonicalInMemoryState, ExecutedBlock, NewCanonicalChain}; use reth_chainspec::ChainSpec; -use reth_db::Database; use reth_db_common::init::init_genesis; use reth_ethereum_primitives::Block; use reth_evm::{ConfigureEvm, execute::Executor}; @@ -26,12 +25,11 @@ use reth_optimism_trie::{ MdbxProofsStorage, OpProofStoragePruner, OpProofsProviderRO, OpProofsStorageResult, OpProofsStore, engine::{EngineError, EngineHandle}, - initialize::InitializationJob, provider::OpProofsStateProviderRef, }; use reth_primitives_traits::RecoveredBlock; use reth_provider::{ - HashedPostStateProvider, ProviderFactory, StateProvider, StateProviderFactory, + HashedPostStateProvider, ProviderFactory, SaveBlocksInput, StateProvider, StateProviderFactory, StateRootProvider, noop::NoopProvider, providers::BlockchainProvider, @@ -40,9 +38,9 @@ use reth_provider::{ use reth_revm::database::StateProviderDatabase; use reth_trie_common::{ComputedTrieData, HashedPostState}; use rstest::rstest; -use secp256k1::{Keypair, Secp256k1, rand::rng}; +use secp256k1::{Keypair, Secp256k1}; use serial_test::serial; -use std::{fmt, sync::Arc, thread}; +use std::{fmt, sync::Arc}; /// reth's default `--engine.memory-block-buffer-target`. const MEMORY_BLOCK_BUFFER_TARGET: u64 = 5; @@ -84,16 +82,41 @@ fn block_number_calldata(number: u64) -> Bytes { Bytes::copy_from_slice(&U256::from(number).to_be_bytes::<32>()) } +/// A fixed signer, so a failing case reproduces with the same addresses and trie shape. +fn signer() -> Keypair { + Keypair::from_seckey_byte_array(&Secp256k1::new(), [0x11; 32]).expect("valid secret key") +} + +/// Where reth holds a block. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct Location { + canonical: bool, + in_memory: bool, + persisted: bool, +} + +impl Location { + const ABANDONED: Self = Self { canonical: false, in_memory: false, persisted: false }; + const IN_MEMORY: Self = Self { canonical: true, in_memory: true, persisted: false }; + const PERSISTED_AND_EVICTED: Self = Self { canonical: true, in_memory: false, persisted: true }; + const PERSISTED_AND_MASKED: Self = Self { canonical: true, in_memory: true, persisted: true }; +} + /// Where reth holds the parent of the block under test when the engine executes the block. #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum ParentState { /// Canonical and in memory, as every recent block is. InMemory, + /// In memory while the deployment whose hash the block reads is persisted and evicted, so the + /// hash comes from below reth's in-memory chain. + InMemoryDeployEvicted, /// Persisted and evicted from memory because reth ran ahead of the engine. PersistedAndEvicted, /// Persisted but kept in memory by state masking. PersistedAndMasked, /// In memory when execution starts, persisted and evicted before execution reads the parent. + /// reth writes a block to disk before evicting it, so this checks the transition, not that a + /// dropped snapshot would be noticed. EvictedWhileExecuting, /// Reorged out of memory before execution, never persisted. AbandonedFork, @@ -102,11 +125,48 @@ enum ParentState { ReorgedWhileExecuting, } +impl ParentState { + /// Where reth holds the parent when the engine starts executing the block under test. + const fn parent_before_execution(self, persistence: RethPersistence) -> Location { + match self { + Self::InMemory | Self::EvictedWhileExecuting | Self::ReorgedWhileExecuting => { + Location::IN_MEMORY + } + Self::InMemoryDeployEvicted => Location { + persisted: persistence.num_state_masking_blocks > 0, + ..Location::IN_MEMORY + }, + Self::PersistedAndEvicted => Location::PERSISTED_AND_EVICTED, + Self::PersistedAndMasked => Location::PERSISTED_AND_MASKED, + Self::AbandonedFork => Location::ABANDONED, + } + } + + /// Where reth holds the parent once the engine has executed the block under test. + const fn parent_after_execution(self, persistence: RethPersistence) -> Location { + match self { + Self::EvictedWhileExecuting => Location::PERSISTED_AND_EVICTED, + Self::ReorgedWhileExecuting => Location::ABANDONED, + _ => self.parent_before_execution(persistence), + } + } + + /// Where reth holds the deployment after execution, for the states that move it away from + /// the parent. + const fn deploy_after_execution(self) -> Option { + match self { + Self::InMemoryDeployEvicted => Some(Location::PERSISTED_AND_EVICTED), + Self::ReorgedWhileExecuting => Some(Location::ABANDONED), + _ => None, + } + } +} + /// Which block of the deploy, call, `BLOCKHASH` chain the engine executes in the parent state. #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum BlockContent { /// The block calling the contract its parent deployed. - DeployAndCall, + Call, /// The block storing `BLOCKHASH` of the deployment two blocks below it. BlockHash, } @@ -131,30 +191,36 @@ struct RethPersistence { } impl RethPersistence { - fn new( - persistence_threshold: u64, - num_state_masking_blocks: u64, - memory_block_buffer_target: u64, - ) -> Self { + /// Settings with reth's default buffer target and no masking. + fn threshold(persistence_threshold: u64) -> Self { assert!( - memory_block_buffer_target <= persistence_threshold, + MEMORY_BLOCK_BUFFER_TARGET <= persistence_threshold, "reth keeps at most persistence_threshold blocks in memory" ); + Self { + memory_block_buffer_target: MEMORY_BLOCK_BUFFER_TARGET, + num_state_masking_blocks: 0, + persistence_threshold, + } + } + + /// reth rejects masking that leaves no room between the buffer target and the threshold. + fn masking(self, num_state_masking_blocks: u64) -> Self { assert!( - num_state_masking_blocks == 0 || - num_state_masking_blocks + memory_block_buffer_target < persistence_threshold, + num_state_masking_blocks + self.memory_block_buffer_target < self.persistence_threshold, "reth rejects num_state_masking_blocks + memory_block_buffer_target >= persistence_threshold" ); - Self { memory_block_buffer_target, num_state_masking_blocks, persistence_threshold } + Self { num_state_masking_blocks, ..self } } -} -fn reth(persistence_threshold: u64, num_state_masking_blocks: u64) -> RethPersistence { - RethPersistence::new( - persistence_threshold, - num_state_masking_blocks, - MEMORY_BLOCK_BUFFER_TARGET, - ) + /// The block the first persistence cycle evicts through: the cycle fires at head + /// `persistence_threshold + 1`, persists through `memory_block_buffer_target` below the head + /// and evicts through `num_state_masking_blocks` below that. + const fn first_eviction_boundary(self) -> u64 { + self.persistence_threshold + 1 - + self.memory_block_buffer_target - + self.num_state_masking_blocks + } } /// reth's database and canonical in-memory state, driven the way reth's engine tree and @@ -168,11 +234,10 @@ struct SimulatedReth { factory: ProviderFactory, in_memory: CanonicalInMemoryState, key_pair: Keypair, + /// Newest block evicted from memory; the persisted blocks above it are the masking window. + last_evicted: BlockNumHash, /// Newest block in the database. last_persisted: BlockNumHash, - /// Newest block whose hashed state and trie are in the database; persisted blocks above it - /// stay in memory. - last_state_trie_persisted: BlockNumHash, nonce: u64, persistence: RethPersistence, provider: BlockchainProvider, @@ -195,8 +260,8 @@ impl SimulatedReth { in_memory: provider.canonical_in_memory_state(), factory, key_pair, + last_evicted: genesis, last_persisted: genesis, - last_state_trie_persisted: genesis, nonce: 0, persistence, provider, @@ -218,17 +283,18 @@ impl SimulatedReth { self.canonical[number as usize - 1].recovered_block() } - const fn is_persisted(&self, block: BlockNumHash) -> bool { - self.last_persisted.number >= block.number - } - - fn is_in_memory(&self, block: BlockNumHash) -> bool { - self.in_memory.state_by_hash(block.hash).is_some() + fn locate(&self, block: BlockNumHash) -> Location { + let canonical = block.number <= self.head().number && self.num_hash(block.number) == block; + Location { + canonical, + in_memory: self.in_memory.state_by_hash(block.hash).is_some(), + persisted: canonical && self.last_persisted.number >= block.number, + } } /// Appends a block to the canonical chain and runs the persistence rule. fn commit(&mut self, spec: &BlockSpec) -> eyre::Result> { - let block = self.build_block(spec, self.head())?; + let block = self.build_block(spec)?; let recovered = block.recovered_block().clone(); self.make_canonical(NewCanonicalChain::Commit { new: vec![block] }); self.advance_persistence()?; @@ -254,7 +320,7 @@ impl SimulatedReth { fn reorg_from(&mut self, number: u64) -> eyre::Result<()> { assert!(number > self.last_persisted.number, "only in-memory blocks can be reorged"); let old = self.canonical.split_off(number as usize - 1); - let fork = self.build_block(&BlockSpec::new(vec![]), self.head())?; + let fork = self.build_block(&BlockSpec::new(vec![]))?; assert_ne!( fork.recovered_block().hash(), old[0].recovered_block().hash(), @@ -279,13 +345,10 @@ impl SimulatedReth { self.in_memory.set_canonical_head(tip); } - /// Builds `spec` on `parent` and executes it, giving it the state root and trie data reth + /// Builds `spec` on the head and executes it, giving it the state root and trie data reth /// holds for a block it validated. - fn build_block( - &mut self, - spec: &BlockSpec, - parent: BlockNumHash, - ) -> eyre::Result { + fn build_block(&mut self, spec: &BlockSpec) -> eyre::Result { + let parent = self.head(); let mut block = create_block_from_spec( spec, parent.number + 1, @@ -320,23 +383,31 @@ impl SimulatedReth { if new_db_tip <= self.last_persisted.number { return Ok(()); } - let new_state_trie_tip = new_db_tip + let new_evicted = new_db_tip .saturating_sub(self.persistence.num_state_masking_blocks) - .max(self.last_state_trie_persisted.number); - self.persist_through(new_db_tip)?; - self.evict_through(new_state_trie_tip); + .max(self.last_evicted.number); + self.persist(new_db_tip)?; + self.evict_through(new_evicted); Ok(()) } - /// Writes the canonical blocks above the database tip through `number` to the database. - fn persist_through(&mut self, number: u64) -> eyre::Result<()> { - let first = self.last_persisted.number as usize; - let blocks = &self.canonical[first..number as usize]; - commit_blocks_to_database( - blocks.iter().map(|block| (block.recovered_block(), block.execution_outcome())), - &self.factory, - )?; - self.last_persisted = self.num_hash(number); + /// Writes the canonical blocks above the database tip through `new_db_tip` with reth's own + /// writer. reth's `partial-persistence` feature is off on develop, so the writer cannot hold + /// back the hashed state and trie of masked blocks; it gets both frontiers at the database + /// tip, and masking only decides which persisted blocks stay in memory. + fn persist(&mut self, new_db_tip: u64) -> eyre::Result<()> { + let prev_db_tip = self.last_persisted.number; + let input = SaveBlocksInput::new( + self.canonical[prev_db_tip as usize..new_db_tip as usize].to_vec(), + prev_db_tip, + prev_db_tip, + new_db_tip, + new_db_tip, + ); + let provider_rw = self.factory.provider_rw()?; + provider_rw.save_blocks(&input)?; + provider_rw.commit()?; + self.last_persisted = self.num_hash(new_db_tip); Ok(()) } @@ -344,7 +415,7 @@ impl SimulatedReth { fn evict_through(&mut self, number: u64) { assert!(number <= self.last_persisted.number, "only persisted blocks leave memory"); self.in_memory.remove_persisted_blocks_until(self.last_persisted, number); - self.last_state_trie_persisted = self.num_hash(number); + self.last_evicted = self.num_hash(number); } } @@ -352,8 +423,10 @@ type ReadHook = Box; /// A proofs store that runs an action on reth right before the engine opens its next read view. /// -/// The engine's execute task snapshots reth's parent state before it opens the proofs read view -/// it executes against, so an action armed here lands after the snapshot and before execution. +/// The engine's execute task snapshots reth's parent state and then opens the proofs read view it +/// executes against, so an action armed here lands between the two. The engine also opens a read +/// view to find its tip whenever its block buffer is empty; the cases keep the preceding blocks +/// buffered so that read never happens while an action is armed. #[derive(Clone)] struct StoreWithReadHook { inner: S, @@ -399,9 +472,7 @@ impl OpProofsStore for StoreWithReadHook { fn provider_ro<'a>(&'a self) -> OpProofsStorageResult> { let action = self.on_next_read.lock().take(); if let Some(action) = action { - // reth's persistence runs on its own thread; so does the action, which keeps the - // engine thread's open database transactions out of its way. - thread::scope(|scope| scope.spawn(action).join()).expect("the read hook panicked"); + action(); } self.inner.provider_ro() } @@ -416,9 +487,10 @@ impl OpProofsStore for StoreWithReadHook { } /// The chain reth holds before the engine runs: `leading` empty blocks, the deployment, the call -/// and the `BLOCKHASH` read, with the index of the block under test. +/// and the `BLOCKHASH` read, with the indices of the deployment and of the block under test. struct ProofChain { blocks: Vec>, + deploy: usize, under_test: usize, } @@ -446,15 +518,16 @@ impl ProofChain { for spec in &specs { blocks.push(reth.commit(spec)?); } + let deploy = leading as usize; let under_test = match content { - BlockContent::DeployAndCall => leading + 1, - BlockContent::BlockHash => leading + 2, - } as usize; - Ok(Self { blocks, under_test }) + BlockContent::Call => deploy + 1, + BlockContent::BlockHash => deploy + 2, + }; + Ok(Self { blocks, deploy, under_test }) } fn deploy(&self) -> &RecoveredBlock { - &self.blocks[self.blocks.len() - 3] + &self.blocks[self.deploy] } fn under_test(&self) -> &RecoveredBlock { @@ -471,26 +544,17 @@ impl ProofChain { } } -/// How many empty blocks precede the deployment so that the parent ends up in `parent_state`. -/// -/// The first persistence cycle fires at head `persistence_threshold + 1` and evicts through -/// `persistence_threshold + 1 - memory_block_buffer_target - num_state_masking_blocks`; a -/// deployment right above that boundary is persisted but masked, so it stays in memory. +/// How many empty blocks precede the deployment so that the chain straddles the first eviction +/// boundary where `parent_state` needs it to: a masked parent is the first block above the +/// boundary, an evicted deployment is the boundary itself. const fn leading_blocks(persistence: RethPersistence, parent_state: ParentState) -> u64 { match parent_state { - ParentState::PersistedAndMasked => { - persistence.persistence_threshold + 1 - - persistence.memory_block_buffer_target - - persistence.num_state_masking_blocks - } + ParentState::PersistedAndMasked => persistence.first_eviction_boundary(), + ParentState::InMemoryDeployEvicted => persistence.first_eviction_boundary() - 1, _ => 0, } } -fn parent_persisted_and_evicted(reth: &SimulatedReth, parent: BlockNumHash) -> bool { - reth.is_persisted(parent) && !reth.is_in_memory(parent) -} - /// Puts the parent into the states reth reaches before the engine executes the block. fn arrange_parent_state( reth: &mut SimulatedReth, @@ -498,22 +562,25 @@ fn arrange_parent_state( chain: &ProofChain, ) -> eyre::Result<()> { let parent = chain.parent(); + let deploy = chain.deploy().num_hash(); match parent_state { - ParentState::PersistedAndEvicted => { - reth.commit_fillers_until(|reth| parent_persisted_and_evicted(reth, parent))?; - } + ParentState::PersistedAndEvicted => reth + .commit_fillers_until(|reth| reth.locate(parent) == Location::PERSISTED_AND_EVICTED)?, ParentState::PersistedAndMasked => { - reth.commit_fillers_until(|reth| reth.is_persisted(parent))?; - assert!(reth.is_in_memory(parent), "masking must keep the persisted parent in memory"); + reth.commit_fillers_until(|reth| reth.locate(parent).persisted)?; } + ParentState::InMemoryDeployEvicted => reth + .commit_fillers_until(|reth| reth.locate(deploy) == Location::PERSISTED_AND_EVICTED)?, ParentState::AbandonedFork => reth.reorg_from(parent.number)?, ParentState::InMemory | ParentState::EvictedWhileExecuting | ParentState::ReorgedWhileExecuting => {} } - let expected_in_memory = - !matches!(parent_state, ParentState::PersistedAndEvicted | ParentState::AbandonedFork); - assert_eq!(reth.is_in_memory(parent), expected_in_memory, "parent state before execution"); + assert_eq!( + reth.locate(parent), + parent_state.parent_before_execution(reth.persistence), + "parent before execution" + ); Ok(()) } @@ -530,7 +597,7 @@ fn arm_in_flight_transition( match parent_state { ParentState::EvictedWhileExecuting => store.on_next_read(move || { reth.lock() - .commit_fillers_until(|reth| parent_persisted_and_evicted(reth, parent)) + .commit_fillers_until(|reth| reth.locate(parent) == Location::PERSISTED_AND_EVICTED) .expect("reth persists and evicts the parent"); }), ParentState::ReorgedWhileExecuting => store.on_next_read(move || { @@ -540,28 +607,18 @@ fn arm_in_flight_transition( } } -fn assert_parent_state_after_execution( +fn assert_locations_after_execution( reth: &SimulatedReth, parent_state: ParentState, chain: &ProofChain, ) { - let parent = chain.parent(); - match parent_state { - ParentState::InMemory => assert!(reth.is_in_memory(parent) && !reth.is_persisted(parent)), - ParentState::PersistedAndMasked => { - assert!(reth.is_in_memory(parent) && reth.is_persisted(parent)); - } - ParentState::PersistedAndEvicted | ParentState::EvictedWhileExecuting => { - assert!(parent_persisted_and_evicted(reth, parent)); - } - ParentState::AbandonedFork | ParentState::ReorgedWhileExecuting => { - assert!(!reth.is_in_memory(parent) && !reth.is_persisted(parent)); - assert_ne!(reth.block(parent.number).hash(), parent.hash, "a fork replaced the parent"); - if parent_state == ParentState::ReorgedWhileExecuting { - let deploy = chain.deploy(); - assert_ne!(reth.block(deploy.number()).hash(), deploy.hash()); - } - } + assert_eq!( + reth.locate(chain.parent()), + parent_state.parent_after_execution(reth.persistence), + "parent after execution" + ); + if let Some(expected) = parent_state.deploy_after_execution() { + assert_eq!(reth.locate(chain.deploy().num_hash()), expected, "deployment after execution"); } } @@ -604,17 +661,13 @@ fn run_parent_state_case( parent_state: ParentState, outcome: Outcome, ) -> eyre::Result<()> { - let key_pair = Keypair::new(&Secp256k1::new(), &mut rng()); + let key_pair = signer(); let sender = public_key_to_address(key_pair.public_key()); let contract = sender.create(0); let chain_spec = chain_spec_with_address(sender); let store = create_mdbx_proofs_storage(); let mut reth = SimulatedReth::new(chain_spec.clone(), key_pair, persistence)?; - { - let tx = reth.factory.db_ref().tx()?; - InitializationJob::new(store.clone(), tx, reth_trie_layout(&reth.factory)) - .run(0, chain_spec.genesis_hash())?; - } + initialize_storage(store.clone(), &reth.factory, reth.head())?; let chain = ProofChain::commit( &mut reth, leading_blocks(persistence, parent_state), @@ -628,7 +681,9 @@ fn run_parent_state_case( let store_with_hook = StoreWithReadHook::new(store.clone()); let pruner = OpProofStoragePruner::new(store_with_hook.clone(), provider.clone(), 1000); // The engine writes its buffer to the store once it holds this many blocks, and `flush` only - // waits for that write, so the threshold is the number of blocks the case stores. + // waits for that write, so the threshold is the number of blocks the case stores. It also + // keeps the preceding blocks buffered while the block under test executes, which the read + // hook relies on. let stored_blocks = match outcome { Outcome::Stored => chain.preceding().len() + 1, Outcome::StateRootMismatch => chain.preceding().len(), @@ -647,12 +702,12 @@ fn run_parent_state_case( arm_in_flight_transition(&store_with_hook, &reth, parent_state, &chain); let result = engine.execute_block(chain.under_test()); - engine.flush(); assert!( !store_with_hook.is_armed(), - "the engine must open its proofs read view after snapshotting reth's parent state" + "the engine never opened a proofs read view, so the in-flight transition never ran" ); - assert_parent_state_after_execution(&reth.lock(), parent_state, &chain); + engine.flush(); + assert_locations_after_execution(&reth.lock(), parent_state, &chain); match outcome { Outcome::Stored => { @@ -661,14 +716,12 @@ fn run_parent_state_case( } Outcome::StateRootMismatch => { let err = result.expect_err("an abandoned parent must fail the state-root check"); - assert!( - matches!( - err, - EngineError::StateRootMismatch { block_number, .. } - if block_number == chain.under_test().number() - ), - "{err:?}" - ); + let EngineError::StateRootMismatch { block_number, expected_state_hash, .. } = err + else { + panic!("expected a state-root mismatch, got {err:?}"); + }; + assert_eq!(block_number, chain.under_test().number()); + assert_eq!(expected_state_hash, chain.under_test().state_root()); assert_eq!(store.provider_ro()?.get_latest_block()?, chain.parent()); Ok(()) } @@ -678,7 +731,8 @@ fn run_parent_state_case( /// The parent states reth reaches on its own, over reth's persistence settings and both blocks. /// /// reth rejects `num_state_masking_blocks + memory_block_buffer_target >= -/// persistence_threshold`, so threshold 7 only runs unmasked. +/// persistence_threshold`, so threshold 7 only runs unmasked. The in-memory and the two fork +/// rows never reach a persistence cycle, so the settings only exercise the simulation there. #[rstest] #[case::in_memory(ParentState::InMemory, Outcome::Stored)] #[case::persisted_and_evicted(ParentState::PersistedAndEvicted, Outcome::Stored)] @@ -687,8 +741,13 @@ fn run_parent_state_case( #[case::abandoned_fork(ParentState::AbandonedFork, Outcome::StateRootMismatch)] #[serial] fn execute_block_across_reth_parent_states( - #[values(reth(7, 0), reth(50, 0), reth(50, 30))] persistence: RethPersistence, - #[values(BlockContent::DeployAndCall, BlockContent::BlockHash)] content: BlockContent, + #[values( + RethPersistence::threshold(7), + RethPersistence::threshold(50), + RethPersistence::threshold(50).masking(30) + )] + persistence: RethPersistence, + #[values(BlockContent::Call, BlockContent::BlockHash)] content: BlockContent, #[case] parent_state: ParentState, #[case] outcome: Outcome, ) -> eyre::Result<()> { @@ -699,16 +758,45 @@ fn execute_block_across_reth_parent_states( #[rstest] #[serial] fn execute_block_with_persisted_and_masked_parent( - #[values(BlockContent::DeployAndCall, BlockContent::BlockHash)] content: BlockContent, + #[values(BlockContent::Call, BlockContent::BlockHash)] content: BlockContent, ) -> eyre::Result<()> { - run_parent_state_case(reth(50, 30), content, ParentState::PersistedAndMasked, Outcome::Stored) + run_parent_state_case( + RethPersistence::threshold(50).masking(30), + content, + ParentState::PersistedAndMasked, + Outcome::Stored, + ) +} + +/// `BLOCKHASH` of a deployment reth has evicted comes from disk while the parent is in memory. +#[rstest] +#[serial] +fn execute_block_hash_read_below_reth_memory( + #[values( + RethPersistence::threshold(7), + RethPersistence::threshold(50), + RethPersistence::threshold(50).masking(30) + )] + persistence: RethPersistence, +) -> eyre::Result<()> { + run_parent_state_case( + persistence, + BlockContent::BlockHash, + ParentState::InMemoryDeployEvicted, + Outcome::Stored, + ) } /// `BLOCKHASH` reads the parent's ancestry even after reth reorged it out mid-execution. #[rstest] #[serial] fn execute_block_hash_read_while_reth_reorgs_below_it( - #[values(reth(7, 0), reth(50, 0), reth(50, 30))] persistence: RethPersistence, + #[values( + RethPersistence::threshold(7), + RethPersistence::threshold(50), + RethPersistence::threshold(50).masking(30) + )] + persistence: RethPersistence, ) -> eyre::Result<()> { run_parent_state_case( persistence, From 37543246739532c59dc5b8a7d9ccf27fc56cdfb3 Mon Sep 17 00:00:00 2001 From: Einar Rasmussen Date: Mon, 9 Nov 2026 20:43:43 +0100 Subject: [PATCH 3/3] op-reth: adapt the proofs parent-state live test to the reth v2.6.0 bump Reth now serves in-memory state through its OverlayManager, so the simulated reth registers the blocks it makes canonical, drops the ones it reorgs out or evicts, and the abandoned_fork case holds without #23046. Co-Authored-By: Claude Fable 5.1 --- rust/Cargo.lock | 2 ++ rust/op-reth/crates/trie/Cargo.toml | 2 ++ .../crates/trie/tests/live/parent_states.rs | 20 +++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 17d3ae3c9dc..fa72de56c71 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -14327,10 +14327,12 @@ dependencies = [ "reth-revm", "reth-stages-types", "reth-storage-errors", + "reth-storage-overlay", "reth-tasks", "reth-trie", "reth-trie-common", "reth-trie-db", + "rstest", "secp256k1 0.31.1", "serde", "serial_test", diff --git a/rust/op-reth/crates/trie/Cargo.toml b/rust/op-reth/crates/trie/Cargo.toml index 24d410c4b09..5ba6a8d34c8 100644 --- a/rust/op-reth/crates/trie/Cargo.toml +++ b/rust/op-reth/crates/trie/Cargo.toml @@ -77,8 +77,10 @@ reth-db-common.workspace = true reth-ethereum-primitives.workspace = true reth-evm-ethereum.workspace = true reth-storage-errors.workspace = true +reth-storage-overlay.workspace = true secp256k1 = { workspace = true, features = ["rand", "std"] } mockall.workspace = true +rstest.workspace = true eyre.workspace = true # misc diff --git a/rust/op-reth/crates/trie/tests/live/parent_states.rs b/rust/op-reth/crates/trie/tests/live/parent_states.rs index f2695b6e483..20e4a8008ea 100644 --- a/rust/op-reth/crates/trie/tests/live/parent_states.rs +++ b/rust/op-reth/crates/trie/tests/live/parent_states.rs @@ -36,6 +36,7 @@ use reth_provider::{ test_utils::{MockNodeTypesWithDB, create_test_provider_factory_with_chain_spec}, }; use reth_revm::database::StateProviderDatabase; +use reth_storage_overlay::OverlayManager; use reth_trie_common::{ComputedTrieData, HashedPostState}; use rstest::rstest; use secp256k1::{Keypair, Secp256k1}; @@ -239,6 +240,7 @@ struct SimulatedReth { /// Newest block in the database. last_persisted: BlockNumHash, nonce: u64, + overlay_manager: OverlayManager, persistence: RethPersistence, provider: BlockchainProvider, } @@ -249,7 +251,9 @@ impl SimulatedReth { key_pair: Keypair, persistence: RethPersistence, ) -> eyre::Result { - let factory = create_test_provider_factory_with_chain_spec(chain_spec.clone()); + let overlay_manager = OverlayManager::default(); + let factory = create_test_provider_factory_with_chain_spec(chain_spec.clone()) + .with_overlay_manager(overlay_manager.clone()); init_genesis(&factory)?; let provider = BlockchainProvider::new(factory.clone())?; let genesis = BlockNumHash::new(0, chain_spec.genesis_hash()); @@ -263,6 +267,7 @@ impl SimulatedReth { last_evicted: genesis, last_persisted: genesis, nonce: 0, + overlay_manager, persistence, provider, }) @@ -336,8 +341,15 @@ impl SimulatedReth { fn make_canonical(&mut self, update: NewCanonicalChain) { let tip = update.tip().clone_sealed_header(); + if let NewCanonicalChain::Reorg { old, .. } = &update { + self.overlay_manager + .remove_blocks(old.iter().map(|block| block.recovered_block().hash())); + } match &update { NewCanonicalChain::Commit { new } | NewCanonicalChain::Reorg { new, .. } => { + for block in new { + self.overlay_manager.insert_block(block.clone()); + } self.canonical.extend(new.iter().cloned()); } } @@ -415,6 +427,11 @@ impl SimulatedReth { fn evict_through(&mut self, number: u64) { assert!(number <= self.last_persisted.number, "only persisted blocks leave memory"); self.in_memory.remove_persisted_blocks_until(self.last_persisted, number); + let evicted = self.canonical[self.last_evicted.number as usize..number as usize] + .iter() + .map(|block| block.recovered_block().hash()) + .collect::>(); + self.overlay_manager.remove_blocks(evicted); self.last_evicted = self.num_hash(number); } } @@ -737,7 +754,6 @@ fn run_parent_state_case( #[case::in_memory(ParentState::InMemory, Outcome::Stored)] #[case::persisted_and_evicted(ParentState::PersistedAndEvicted, Outcome::Stored)] #[case::evicted_while_executing(ParentState::EvictedWhileExecuting, Outcome::Stored)] -#[ignore = "passes on #23046; develop skips a parent reth no longer holds (StateForHashNotFound) and stores nothing"] #[case::abandoned_fork(ParentState::AbandonedFork, Outcome::StateRootMismatch)] #[serial] fn execute_block_across_reth_parent_states(