From e8c770d520224809986038bca964c592ee0726eb Mon Sep 17 00:00:00 2001 From: Einar Rasmussen Date: Thu, 15 Oct 2026 00:29:13 -0700 Subject: [PATCH 1/3] op-reth: live test of index-versus-execute proofs store parity Builds one Cancun chain into three proofs stores, one indexed from reth's trie data, one re-executed by the engine and one initialized at the tip and backfilled, and compares the proof window, the hashed account and storage cursors, the account and storage trie cursors and an account proof per touched address at every block. Block content covers deploy then call, SSTORE to zero, CREATE plus SELFDESTRUCT in one transaction, SELFDESTRUCT of a pre-existing contract, an EIP-161 touch and a BLOCKHASH read, over both engine flush cadences and both store backends. Lifts SimulatedReth and the contract bytecode helpers out of the parent-state test into shared modules of the live test crate. Co-Authored-By: Claude Fable 5.1 (cherry picked from commit 9503b72ef964adafb1fab405e794ea0360c42f2e) (cherry picked from commit 25e71cef99b5d49ad92de9bf5421b29faedc9c12) --- rust/op-reth/crates/trie/tests/live.rs | 74 ++- .../crates/trie/tests/live/contracts.rs | 101 +++ .../trie/tests/live/index_execute_parity.rs | 613 ++++++++++++++++++ .../crates/trie/tests/live/parent_states.rs | 352 +--------- .../crates/trie/tests/live/simulated_reth.rs | 317 +++++++++ 5 files changed, 1104 insertions(+), 353 deletions(-) create mode 100644 rust/op-reth/crates/trie/tests/live/contracts.rs create mode 100644 rust/op-reth/crates/trie/tests/live/index_execute_parity.rs create mode 100644 rust/op-reth/crates/trie/tests/live/simulated_reth.rs diff --git a/rust/op-reth/crates/trie/tests/live.rs b/rust/op-reth/crates/trie/tests/live.rs index 66c52a39654..319d8d359f7 100644 --- a/rust/op-reth/crates/trie/tests/live.rs +++ b/rust/op-reth/crates/trie/tests/live.rs @@ -1,11 +1,16 @@ //! 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_consensus::{ + BlockHeader, Header, TxEip2930, + constants::{EMPTY_WITHDRAWALS, ETH_TO_WEI}, +}; +use alloy_eips::{BlockNumHash, eip4895::Withdrawals}; use alloy_genesis::{Genesis, GenesisAccount}; use alloy_primitives::{Address, B256, Bytes, TxKind, U256, keccak256}; use derive_more::Constructor; -use reth_chainspec::{ChainSpec, ChainSpecBuilder, EthereumHardfork, MAINNET, MIN_TRANSACTION_GAS}; +use reth_chainspec::{ + ChainSpec, ChainSpecBuilder, EthereumHardfork, EthereumHardforks, MAINNET, MIN_TRANSACTION_GAS, +}; use reth_db::Database; use reth_db_common::init::init_genesis; use reth_ethereum_primitives::{Block, BlockBody, Receipt, Transaction, TransactionSigned}; @@ -32,8 +37,22 @@ use std::sync::Arc; use tempfile::TempDir; use test_case::test_case; +#[path = "live/contracts.rs"] +mod contracts; +#[path = "live/index_execute_parity.rs"] +mod index_execute_parity; #[path = "live/parent_states.rs"] mod parent_states; +#[path = "live/simulated_reth.rs"] +mod simulated_reth; + +/// Seconds between consecutive blocks. +const BLOCK_TIME: u64 = 2; + +/// Timestamp of the block at `number`. +const fn block_timestamp(number: u64) -> u64 { + number * BLOCK_TIME +} fn create_mdbx_proofs_storage() -> Arc { let path = TempDir::new().unwrap().keep(); @@ -91,6 +110,11 @@ impl TxSpec { const fn call(contract: Address, data: Bytes, gas_limit: u64) -> Self { Self { to: TxKind::Call(contract), data, gas_limit, value: U256::ZERO, nonce: None } } + + /// Send `value` along with the transaction. + fn with_value(self, value: U256) -> Self { + Self { value, ..self } + } } /// Specification for a block in the test chain @@ -109,22 +133,24 @@ struct TestScenario { blocks_after_initialization: Vec, } +/// A mainnet-shaped chain spec whose genesis allocation is `alloc`, with the hardforks still to +/// be chosen. +fn chain_spec_builder( + alloc: impl IntoIterator, +) -> ChainSpecBuilder { + ChainSpecBuilder::default() + .chain(MAINNET.chain) + .genesis(Genesis { alloc: alloc.into_iter().collect(), ..MAINNET.genesis.clone() }) +} + +/// A genesis allocation funding `address`. +fn funded(address: Address) -> (Address, GenesisAccount) { + (address, GenesisAccount { balance: U256::from(10 * ETH_TO_WEI), ..Default::default() }) +} + /// Helper to create a chain spec with a genesis account funded fn chain_spec_with_address(address: Address) -> Arc { - Arc::new( - ChainSpecBuilder::default() - .chain(MAINNET.chain) - .genesis(Genesis { - alloc: [( - address, - GenesisAccount { balance: U256::from(10 * ETH_TO_WEI), ..Default::default() }, - )] - .into(), - ..MAINNET.genesis.clone() - }) - .paris_activated() - .build(), - ) + Arc::new(chain_spec_builder([funded(address)]).paris_activated().build()) } /// Creates a block from a spec, executing transactions with the given keypair @@ -164,6 +190,9 @@ fn create_block_from_spec( .collect(); let gas_total = spec.txs.iter().map(|tx| tx.gas_limit).sum::(); + let timestamp = block_timestamp(block_number); + let shanghai = chain_spec.is_shanghai_active_at_timestamp(timestamp); + let cancun = chain_spec.is_cancun_active_at_timestamp(timestamp); Block { header: Header { @@ -175,10 +204,19 @@ fn create_block_from_spec( number: block_number, gas_limit: gas_total.max(MIN_TRANSACTION_GAS), gas_used: gas_total, + timestamp, state_root: B256::ZERO, // Will be calculated by executor + withdrawals_root: shanghai.then_some(EMPTY_WITHDRAWALS), + blob_gas_used: cancun.then_some(0), + excess_blob_gas: cancun.then_some(0), + parent_beacon_block_root: cancun.then(|| keccak256(block_number.to_be_bytes())), + ..Default::default() + }, + body: BlockBody { + transactions, + withdrawals: shanghai.then(Withdrawals::default), ..Default::default() }, - body: BlockBody { transactions, ..Default::default() }, } .try_into_recovered() .unwrap() diff --git a/rust/op-reth/crates/trie/tests/live/contracts.rs b/rust/op-reth/crates/trie/tests/live/contracts.rs new file mode 100644 index 00000000000..76ee09788bc --- /dev/null +++ b/rust/op-reth/crates/trie/tests/live/contracts.rs @@ -0,0 +1,101 @@ +//! EVM bytecode of the contracts the live tests deploy, and the storage slots they write. + +use alloy_primitives::{Address, B256, Bytes, U256}; + +/// Storage slot every call to the block-hash recorder sets to one. +pub(super) const CALLED_SLOT: B256 = B256::ZERO; + +/// Storage slot a call to the block-hash recorder with calldata sets to `BLOCKHASH(calldata)`. +pub(super) const BLOCK_HASH_SLOT: B256 = B256::with_last_byte(1); + +/// Runtime code of the block-hash recorder: `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 +/// ``` +pub(super) const BLOCK_HASH_RECORDER_RUNTIME: [u8; 19] = [ + 0x60, 0x01, 0x60, 0x00, 0x55, 0x36, 0x15, 0x60, 0x11, 0x57, 0x60, 0x00, 0x35, 0x40, 0x60, 0x01, + 0x55, 0x5b, 0x00, +]; + +/// Number of storage slots the slot writer takes from calldata. +pub(super) const SLOT_WRITER_SLOTS: usize = 8; + +/// Runtime code of the slot writer: a call with calldata stores its first eight 32-byte words +/// into `slots`, in order; a call without calldata self-destructs to `beneficiary`. +/// +/// ```text +/// CALLDATASIZE ISZERO PUSH1 JUMPI +/// PUSH1 0x00 CALLDATALOAD PUSH1 slots[0] SSTORE +/// ... +/// PUSH1 0xe0 CALLDATALOAD PUSH1 slots[7] SSTORE +/// STOP +/// JUMPDEST PUSH20 beneficiary SELFDESTRUCT +/// ``` +pub(super) fn slot_writer_runtime(beneficiary: Address, slots: [u8; SLOT_WRITER_SLOTS]) -> Vec { + let mut store_slots = Vec::new(); + for (word, slot) in slots.into_iter().enumerate() { + store_slots.extend_from_slice(&[0x60, word as u8 * 32, 0x35, 0x60, slot, 0x55]); + } + store_slots.push(0x00); + let dispatch_len = 5; + let selfdestruct_at = u8::try_from(dispatch_len + store_slots.len()).expect("short code"); + let mut code = vec![0x36, 0x15, 0x60, selfdestruct_at, 0x57]; + code.extend_from_slice(&store_slots); + code.push(0x5b); + code.push(0x73); + code.extend_from_slice(beneficiary.as_slice()); + code.push(0xff); + code +} + +/// Number of storage slots [`create_and_selfdestruct_initcode`] writes before self-destructing. +pub(super) const CREATE_AND_SELFDESTRUCT_SLOTS: u8 = 4; + +/// Initcode that stores seven into four slots and then self-destructs to `beneficiary` within +/// the creating transaction, which under EIP-6780 removes the account with its storage. +/// +/// ```text +/// PUSH1 7 PUSH1 0 SSTORE +/// ... +/// PUSH1 7 PUSH1 3 SSTORE +/// PUSH20 beneficiary SELFDESTRUCT +/// ``` +pub(super) fn create_and_selfdestruct_initcode(beneficiary: Address) -> Bytes { + let mut code = Vec::new(); + for slot in 0..CREATE_AND_SELFDESTRUCT_SLOTS { + code.extend_from_slice(&[0x60, 0x07, 0x60, slot, 0x55]); + } + code.push(0x73); + code.extend_from_slice(beneficiary.as_slice()); + code.push(0xff); + code.into() +} + +/// Initcode deploying `runtime`, which follows the twelve initcode bytes: +/// `PUSH1 len PUSH1 12 PUSH1 0 CODECOPY PUSH1 len PUSH1 0 RETURN`. +pub(super) fn initcode_returning(runtime: &[u8]) -> Bytes { + let runtime_len = u8::try_from(runtime.len()).expect("runtime fits one PUSH1"); + let mut initcode = + vec![0x60, runtime_len, 0x60, 0x0c, 0x60, 0x00, 0x39, 0x60, runtime_len, 0x60, 0x00, 0xf3]; + initcode.extend_from_slice(runtime); + initcode.into() +} + +/// Calldata for the slot writer: the value of each of its slots, in the order of its `slots`. +pub(super) fn slot_writer_calldata(values: [u64; SLOT_WRITER_SLOTS]) -> Bytes { + values + .iter() + .flat_map(|value| U256::from(*value).to_be_bytes::<32>()) + .collect::>() + .into() +} + +/// Calldata for the block-hash recorder: the number of the block to read. +pub(super) fn block_number_calldata(number: u64) -> Bytes { + Bytes::copy_from_slice(&U256::from(number).to_be_bytes::<32>()) +} diff --git a/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs b/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs new file mode 100644 index 00000000000..1463331ebb8 --- /dev/null +++ b/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs @@ -0,0 +1,613 @@ +//! Builds one chain into three proofs stores, one indexed from reth's trie data, one re-executed +//! by the engine and one initialized at the tip and backfilled, and asserts that they serve the +//! same accounts, storage, trie nodes and proofs at every block. +//! +//! The index and execute paths are the ExEx's only cross-check of each other, and `proofs init` +//! plus backfill is the third way a store comes to hold a block, so the three must agree on every +//! kind of state change a block can make. The engine's buffer is not readable from outside, so +//! the flush cadence decides whether the execute path reads a parent from the buffer or from +//! disk, and the comparison runs over the persisted history once everything is flushed. + +use super::{ + BlockSpec, TxSpec, block_timestamp, chain_spec_builder, + contracts::{ + BLOCK_HASH_RECORDER_RUNTIME, BLOCK_HASH_SLOT, CREATE_AND_SELFDESTRUCT_SLOTS, + SLOT_WRITER_SLOTS, block_number_calldata, create_and_selfdestruct_initcode, + initcode_returning, slot_writer_calldata, slot_writer_runtime, + }, + create_mdbx_proofs_storage, funded, initialize_storage, public_key_to_address, + simulated_reth::{RethPersistence, SimulatedReth, fixed_signer}, +}; +use alloy_consensus::BlockHeader; +use alloy_eips::{ + BlockNumHash, + eip1898::BlockWithParent, + eip4788::{BEACON_ROOTS_ADDRESS, BEACON_ROOTS_CODE}, +}; +use alloy_genesis::GenesisAccount; +use alloy_primitives::{Address, B256, Bytes, U256, keccak256}; +use reth_chainspec::ChainSpec; +use reth_ethereum_primitives::Block; +use reth_evm_ethereum::EthEvmConfig; +use reth_optimism_trie::{ + BackfillJob, InMemoryProofsStorage, OpProofStoragePruner, OpProofsProviderRO, OpProofsStore, + ProofWindowRange, engine::EngineHandle, proof::DatabaseProof, +}; +use reth_primitives_traits::{Account, RecoveredBlock}; +use reth_provider::DatabaseProviderFactory; +use reth_trie::{hashed_cursor::HashedCursor, proof::Proof, trie_cursor::TrieCursor}; +use reth_trie_common::{AccountProof, BranchNodeCompact, Nibbles, TrieInput}; +use rstest::rstest; +use serial_test::serial; +use std::{ + collections::{BTreeMap, BTreeSet}, + fmt, + sync::Arc, +}; + +/// Blocks in every chain: a multiple of both persistence thresholds, so the final flush leaves +/// nothing in the engine buffer. +const CHAIN_LENGTH: u64 = 10; + +/// Empty blocks before the content, which places the content across the flush at block 5 under +/// the slower cadence. +const LEADING_FILLERS: u64 = 3; + +/// Covers deploying either contract. +const DEPLOY_GAS_LIMIT: u64 = 200_000; + +/// Covers eight first-time storage writes, or four writes and a self-destruct. +const WRITE_GAS_LIMIT: u64 = 400_000; + +/// reth's persistence threshold: above the chain length, so reth keeps every block in memory +/// until the backfilled store needs them on disk. +const RETH_PERSISTENCE_THRESHOLD: u64 = 50; + +/// Slots of the EIP-4788 ring buffer, which every block writes. +const BEACON_ROOTS_RING_LENGTH: u64 = 8191; + +/// Where a self-destruct sends its balance. +const BENEFICIARY: Address = Address::repeat_byte(0xBE); + +/// An address no transaction ever funds; touching it creates nothing. +const NEVER_FUNDED: Address = Address::repeat_byte(0xE2); + +/// Three keys from `keys` whose hashes share the first nibble, the first two also the second, so +/// that a trie holding all three stores a branch node at the first nibble, which removing either +/// of the pair collapses. +/// +/// reth stores no branch node for keys that all differ in their first nibble, so a small trie of +/// such keys has nothing to update or remove. +fn colliding_keys(keys: impl IntoIterator, hash: impl Fn(K) -> B256) -> [K; 3] { + let mut by_first_nibble: BTreeMap> = BTreeMap::new(); + for key in keys { + let hashed = hash(key); + by_first_nibble.entry(hashed[0] >> 4).or_default().push((hashed[0] & 0x0f, key)); + } + for group in by_first_nibble.values() { + for (position, (second_nibble, key)) in group.iter().enumerate() { + let sibling = group[position + 1..].iter().find(|(nibble, _)| nibble == second_nibble); + let cousin = group.iter().find(|(nibble, _)| nibble != second_nibble); + if let (Some((_, sibling)), Some((_, cousin))) = (sibling, cousin) { + return [*key, *sibling, *cousin]; + } + } + } + panic!("no three keys collide on their leading nibbles") +} + +fn hashed_slot(slot: u8) -> B256 { + keccak256(B256::with_last_byte(slot)) +} + +/// The slot writer's slots: three that collide on their leading nibbles, then five under distinct +/// first nibbles, so that filling them stores branch nodes and zeroing them removes those nodes. +fn storage_slots() -> [u8; SLOT_WRITER_SLOTS] { + let colliding = colliding_keys(0..=u8::MAX, hashed_slot); + let mut taken_nibbles = BTreeSet::from([hashed_slot(colliding[0])[0] >> 4]); + let mut slots = colliding.to_vec(); + slots.extend((0..=u8::MAX).filter(|slot| taken_nibbles.insert(hashed_slot(*slot)[0] >> 4))); + slots.truncate(SLOT_WRITER_SLOTS); + slots.try_into().expect("eight slots") +} + +/// Three genesis accounts colliding on their leading hashed nibbles: the empty one EIP-161 +/// removes once a transaction touches it, its sibling and their cousin, both funded. +fn genesis_neighbors() -> [Address; 3] { + colliding_keys((0x10..=u8::MAX).map(Address::with_last_byte), keccak256) +} + +/// The account in the genesis allocation without balance, nonce or code. +fn empty_in_genesis() -> Address { + genesis_neighbors()[0] +} + +/// What the content blocks of the chain do. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum BlockContent { + /// Deploys the slot writer, then fills its eight slots. + DeployThenCall, + /// Fills the slot writer's eight slots, zeroes half of them, then all but one. + SstoreToZero, + /// One transaction creates a contract that writes storage and self-destructs, which EIP-6780 + /// removes with its storage. + CreateAndSelfdestructInOneTx, + /// Self-destructs the slot writer after filling it; after Cancun the code and storage stay. + SelfdestructPreExisting, + /// Touches an empty account from genesis, which EIP-161 removes, and an address that never + /// had anything. + Eip161Touch, + /// Stores `BLOCKHASH` of the first block. + BlockHashRead, +} + +impl BlockContent { + /// The content blocks; the first transaction creates `contract` at the sender's nonce 0. + fn blocks(self, contract: Address) -> Vec { + let deploy_slot_writer = BlockSpec::new(vec![TxSpec::create( + initcode_returning(&slot_writer_runtime(BENEFICIARY, storage_slots())), + DEPLOY_GAS_LIMIT, + )]); + let write_slots = |values| { + BlockSpec::new(vec![TxSpec::call( + contract, + slot_writer_calldata(values), + WRITE_GAS_LIMIT, + )]) + }; + match self { + Self::DeployThenCall => vec![deploy_slot_writer, write_slots([1, 2, 3, 4, 5, 6, 7, 8])], + // Zeroing the first colliding slot removes the stored branch the three share; keeping + // only the cousin leaves a lone leaf. + Self::SstoreToZero => vec![ + deploy_slot_writer, + write_slots([7; SLOT_WRITER_SLOTS]), + write_slots([0, 7, 7, 0, 0, 0, 7, 7]), + write_slots([0, 0, 7, 0, 0, 0, 0, 0]), + ], + Self::CreateAndSelfdestructInOneTx => vec![BlockSpec::new(vec![ + TxSpec::create(create_and_selfdestruct_initcode(BENEFICIARY), WRITE_GAS_LIMIT) + .with_value(U256::from(1)), + ])], + Self::SelfdestructPreExisting => vec![ + deploy_slot_writer, + write_slots([5; SLOT_WRITER_SLOTS]), + BlockSpec::new(vec![ + TxSpec::call(contract, Bytes::new(), WRITE_GAS_LIMIT).with_value(U256::from(1)), + ]), + ], + Self::Eip161Touch => vec![BlockSpec::new(vec![ + TxSpec::transfer(empty_in_genesis(), U256::ZERO), + TxSpec::transfer(NEVER_FUNDED, U256::ZERO), + ])], + Self::BlockHashRead => vec![ + BlockSpec::new(vec![TxSpec::create( + initcode_returning(&BLOCK_HASH_RECORDER_RUNTIME), + DEPLOY_GAS_LIMIT, + )]), + BlockSpec::new(vec![TxSpec::call( + contract, + block_number_calldata(1), + DEPLOY_GAS_LIMIT, + )]), + ], + } + } + + /// Checks that the chain did what the case says, on the state the stores agree on. + fn assert_effect( + self, + genesis: &BlockView, + tip: &BlockView, + first_block: &RecoveredBlock, + contract: Address, + ) -> eyre::Result<()> { + let hashed_contract = keccak256(contract); + let contract_account = tip.accounts.get(&hashed_contract); + let contract_storage = &tip.storages[&hashed_contract]; + let beneficiary_balance = tip.accounts.get(&keccak256(BENEFICIARY)).map(|a| a.balance); + match self { + Self::DeployThenCall => { + eyre::ensure!(contract_account.is_some_and(|a| a.bytecode_hash.is_some())); + eyre::ensure!(contract_storage.len() == SLOT_WRITER_SLOTS); + } + Self::SstoreToZero => { + let cousin_slot = hashed_slot(storage_slots()[2]); + eyre::ensure!(contract_storage.keys().eq([&cousin_slot]), "{contract_storage:?}"); + } + Self::CreateAndSelfdestructInOneTx => { + eyre::ensure!(contract_account.is_none()); + eyre::ensure!(contract_storage.is_empty()); + eyre::ensure!(tip.storage_tries[&hashed_contract].is_empty()); + eyre::ensure!(beneficiary_balance == Some(U256::from(1))); + } + Self::SelfdestructPreExisting => { + eyre::ensure!(contract_account.is_some_and(|a| a.bytecode_hash.is_some())); + eyre::ensure!(contract_storage.len() == SLOT_WRITER_SLOTS); + eyre::ensure!(beneficiary_balance == Some(U256::from(1))); + } + Self::Eip161Touch => { + eyre::ensure!(genesis.accounts.contains_key(&keccak256(empty_in_genesis()))); + eyre::ensure!(!tip.accounts.contains_key(&keccak256(empty_in_genesis()))); + eyre::ensure!(!tip.accounts.contains_key(&keccak256(NEVER_FUNDED))); + } + Self::BlockHashRead => { + let stored = contract_storage.get(&keccak256(BLOCK_HASH_SLOT)); + eyre::ensure!(stored == Some(&U256::from_be_bytes(first_block.hash().0))); + } + } + Ok(()) + } +} + +/// The engine's persistence and backpressure thresholds. +#[derive(Clone, Copy, Debug)] +struct FlushCadence { + backpressure: u64, + persistence: u64, +} + +impl FlushCadence { + const fn new(persistence: u64, backpressure: u64) -> Self { + Self { backpressure, persistence } + } +} + +/// Which store implementation the indexed and executed stores use. +#[derive(Clone, Copy, Debug)] +enum Backend { + InMemory, + Mdbx, +} + +/// An address whose proof, with these slots, every store must serve identically. +struct ProofTarget { + address: Address, + slots: Vec, +} + +/// Everything a store serves at one block, read through its cursors and proof builder. +struct BlockView { + accounts: BTreeMap, + account_trie: BTreeMap, + proofs: BTreeMap, + storage_tries: BTreeMap>, + storages: BTreeMap>, +} + +impl BlockView { + /// Reads the store at `number`: every account and trie node, the storage and storage trie + /// of each of `hashed_addresses`, and a proof for each target. + fn read( + store: &S, + number: u64, + hashed_addresses: &BTreeSet, + targets: &[ProofTarget], + ) -> eyre::Result { + let provider = store.provider_ro()?; + let mut storages = BTreeMap::new(); + let mut storage_tries = BTreeMap::new(); + for hashed_address in hashed_addresses { + let storage = collect_hashed(provider.storage_hashed_cursor(*hashed_address, number)?)?; + let trie = collect_trie(provider.storage_trie_cursor(*hashed_address, number)?)?; + storages.insert(*hashed_address, storage); + storage_tries.insert(*hashed_address, trie); + } + let proofs = targets + .iter() + .map(|target| { + let proof = Proof::overlay_account_proof( + provider.clone(), + number, + TrieInput::default(), + target.address, + &target.slots, + )?; + Ok((target.address, proof)) + }) + .collect::>()?; + Ok(Self { + accounts: collect_hashed(provider.account_hashed_cursor(number)?)?, + account_trie: collect_trie(provider.account_trie_cursor(number)?)?, + proofs, + storage_tries, + storages, + }) + } + + /// Every proof must hash to the header's state root; this ties the stores to reth, so that + /// three stores agreeing on the wrong state still fail. + fn verify_proofs(&self, number: u64, store: &str, state_root: B256) -> eyre::Result<()> { + for (address, proof) in &self.proofs { + proof.verify(state_root).map_err(|err| { + eyre::eyre!( + "the {store} store's proof of {address} at block {number} does not verify \ + against the header state root: {err}" + ) + })?; + } + Ok(()) + } +} + +/// The hashed addresses every store must serve at `number`: the proof targets and every account +/// any store holds, so that storage a store keeps for an account it dropped is still compared. +fn hashed_addresses_at( + stores: (&A, &B, &C), + number: u64, + targets: &[ProofTarget], +) -> eyre::Result> { + let mut hashed_addresses: BTreeSet = + targets.iter().map(|target| keccak256(target.address)).collect(); + hashed_addresses.extend( + collect_hashed(stores.0.provider_ro()?.account_hashed_cursor(number)?)?.into_keys(), + ); + hashed_addresses.extend( + collect_hashed(stores.1.provider_ro()?.account_hashed_cursor(number)?)?.into_keys(), + ); + hashed_addresses.extend( + collect_hashed(stores.2.provider_ro()?.account_hashed_cursor(number)?)?.into_keys(), + ); + Ok(hashed_addresses) +} + +/// Every entry of a hashed cursor, from the lowest key up. +fn collect_hashed(mut cursor: C) -> eyre::Result> { + let mut entries = BTreeMap::new(); + let mut entry = cursor.seek(B256::ZERO)?; + while let Some((key, value)) = entry { + entries.insert(key, value); + entry = cursor.next()?; + } + Ok(entries) +} + +/// Every node of a trie cursor, from the root path up. +fn collect_trie( + mut cursor: C, +) -> eyre::Result> { + let mut nodes = BTreeMap::new(); + let mut entry = cursor.seek(Nibbles::default())?; + while let Some((path, node)) = entry { + nodes.insert(path, node); + entry = cursor.next()?; + } + Ok(nodes) +} + +/// Fails on the first key at which `actual` differs from the indexed store's `expected`, +/// naming the block, the table and the key. +fn assert_same_table( + number: u64, + store: &str, + table: &str, + expected: &BTreeMap, + actual: &BTreeMap, +) -> eyre::Result<()> { + for key in expected.keys().chain(actual.keys()).collect::>() { + let (want, got) = (expected.get(key), actual.get(key)); + eyre::ensure!( + want == got, + "the {store} store differs from the indexed store at block {number} in {table} at \ + key {key:?}: indexed {want:?}, {store} {got:?}" + ); + } + Ok(()) +} + +fn assert_same_view( + number: u64, + store: &str, + expected: &BlockView, + actual: &BlockView, +) -> eyre::Result<()> { + assert_same_table(number, store, "HashedAccounts", &expected.accounts, &actual.accounts)?; + assert_same_table(number, store, "AccountsTrie", &expected.account_trie, &actual.account_trie)?; + for (hashed_address, storage) in &expected.storages { + let table = format!("HashedStorages[{hashed_address}]"); + assert_same_table(number, store, &table, storage, &actual.storages[hashed_address])?; + } + for (hashed_address, trie) in &expected.storage_tries { + let table = format!("StoragesTrie[{hashed_address}]"); + assert_same_table(number, store, &table, trie, &actual.storage_tries[hashed_address])?; + } + assert_same_table(number, store, "AccountProof", &expected.proofs, &actual.proofs) +} + +/// The chain: leading fillers, the content, and fillers up to [`CHAIN_LENGTH`]. +fn chain_blocks(content: BlockContent, contract: Address) -> Vec { + let mut blocks: Vec = (0..LEADING_FILLERS).map(|_| BlockSpec::new(vec![])).collect(); + blocks.extend(content.blocks(contract)); + assert!(blocks.len() as u64 <= CHAIN_LENGTH, "the content must fit the chain"); + blocks.resize_with(CHAIN_LENGTH as usize, || BlockSpec::new(vec![])); + blocks +} + +/// A Cancun chain funding `sender`, holding the empty account EIP-161 removes with its two +/// neighbors, and the EIP-4788 contract every block writes to. +fn cancun_chain_spec(sender: Address) -> Arc { + let [empty, sibling, cousin] = genesis_neighbors(); + let one_wei = GenesisAccount { balance: U256::from(1), ..Default::default() }; + let beacon_roots = + GenesisAccount { code: Some(BEACON_ROOTS_CODE.clone()), ..Default::default() }; + let alloc = [ + funded(sender), + (empty, GenesisAccount::default()), + (sibling, one_wei.clone()), + (cousin, one_wei), + (BEACON_ROOTS_ADDRESS, beacon_roots), + ]; + Arc::new(chain_spec_builder(alloc).cancun_activated().build()) +} + +/// Every address the chain can touch, with the slots the contracts write; the beacon roots +/// contract writes the timestamp and root slots of each block. +fn proof_targets(sender: Address, contract: Address) -> Vec { + let contract_slots: Vec = storage_slots() + .into_iter() + .chain(0..CREATE_AND_SELFDESTRUCT_SLOTS) + .map(B256::with_last_byte) + .collect(); + let beacon_roots_slots = (1..=CHAIN_LENGTH) + .flat_map(|number| { + let timestamp_slot = block_timestamp(number) % BEACON_ROOTS_RING_LENGTH; + [timestamp_slot, timestamp_slot + BEACON_ROOTS_RING_LENGTH] + }) + .map(|slot| B256::from(U256::from(slot))) + .collect(); + let [empty, sibling, cousin] = genesis_neighbors(); + let mut targets: Vec = + [sender, Address::ZERO, BENEFICIARY, empty, sibling, cousin, NEVER_FUNDED, contract] + .into_iter() + .map(|address| ProofTarget { address, slots: contract_slots.clone() }) + .collect(); + targets.push(ProofTarget { address: BEACON_ROOTS_ADDRESS, slots: beacon_roots_slots }); + targets +} + +fn spawn_engine( + chain_spec: &Arc, + reth: &SimulatedReth, + store: S, + cadence: FlushCadence, +) -> EngineHandle +where + S: OpProofsStore + Clone + 'static, +{ + let pruner = OpProofStoragePruner::new(store.clone(), reth.provider.clone(), 1000); + EngineHandle::spawn_with_thresholds( + EthEvmConfig::ethereum(chain_spec.clone()), + reth.provider.clone(), + store, + pruner, + cadence.persistence, + cadence.backpressure, + ) +} + +/// `flush` only waits for the save in flight and the engine starts the next save after it +/// replies, so a buffer above the threshold drains over several flushes. +fn flush_until_persisted( + engine: &EngineHandle, + store: &S, + tip: BlockNumHash, +) -> eyre::Result<()> { + for _ in 0..=CHAIN_LENGTH { + engine.flush(); + if store.provider_ro()?.get_latest_block()? == tip { + return Ok(()); + } + } + eyre::bail!("the engine did not persist through {tip:?}") +} + +fn block_with_parent(block: &RecoveredBlock) -> BlockWithParent { + BlockWithParent::new(block.parent_hash(), block.num_hash()) +} + +fn run_parity_case( + make_store: impl Fn() -> S, + cadence: FlushCadence, + content: BlockContent, +) -> eyre::Result<()> +where + S: OpProofsStore + Clone + Send + Sync + fmt::Debug + 'static, +{ + let key_pair = fixed_signer(); + let sender = public_key_to_address(key_pair.public_key()); + let contract = sender.create(0); + let chain_spec = cancun_chain_spec(sender); + let persistence = RethPersistence::threshold(RETH_PERSISTENCE_THRESHOLD); + let mut reth = SimulatedReth::new(chain_spec.clone(), key_pair, persistence)?; + let genesis = reth.head(); + + let indexed_store = make_store(); + let executed_store = make_store(); + initialize_storage(indexed_store.clone(), &reth.factory, genesis)?; + initialize_storage(executed_store.clone(), &reth.factory, genesis)?; + let indexed_engine = spawn_engine(&chain_spec, &reth, indexed_store.clone(), cadence); + let executed_engine = spawn_engine(&chain_spec, &reth, executed_store.clone(), cadence); + + let mut blocks = Vec::new(); + for spec in chain_blocks(content, contract) { + let executed = reth.commit(&spec)?; + let block = executed.recovered_block(); + indexed_engine.index_block( + block_with_parent(block), + (*executed.trie_updates()).clone(), + (*executed.hashed_state()).clone(), + )?; + executed_engine.execute_block(block)?; + blocks.push(block.clone()); + } + let tip = reth.head(); + flush_until_persisted(&indexed_engine, &indexed_store, tip)?; + flush_until_persisted(&executed_engine, &executed_store, tip)?; + + reth.persist_all()?; + let backfilled_store = create_mdbx_proofs_storage(); + initialize_storage(backfilled_store.clone(), &reth.factory, tip)?; + BackfillJob::new(reth.factory.database_provider_ro()?, backfilled_store.clone()) + .run(genesis.number)?; + + let targets = proof_targets(sender, contract); + let window = ProofWindowRange { earliest: genesis, latest: tip }; + assert_eq!(indexed_store.provider_ro()?.get_proof_window()?, window, "indexed ProofWindow"); + assert_eq!(executed_store.provider_ro()?.get_proof_window()?, window, "executed ProofWindow"); + assert_eq!( + backfilled_store.provider_ro()?.get_proof_window()?, + window, + "backfilled ProofWindow" + ); + + let mut genesis_view = None; + let mut tip_view = None; + for number in genesis.number..=tip.number { + let stores = (&indexed_store, &executed_store, &backfilled_store); + let hashed_addresses = hashed_addresses_at(stores, number, &targets)?; + let indexed = BlockView::read(&indexed_store, number, &hashed_addresses, &targets)?; + let executed = BlockView::read(&executed_store, number, &hashed_addresses, &targets)?; + let backfilled = BlockView::read(&backfilled_store, number, &hashed_addresses, &targets)?; + assert_same_view(number, "executed", &indexed, &executed)?; + assert_same_view(number, "backfilled", &indexed, &backfilled)?; + + let state_root = match number { + 0 => chain_spec.genesis_header().state_root, + _ => blocks[number as usize - 1].state_root(), + }; + indexed.verify_proofs(number, "indexed", state_root)?; + executed.verify_proofs(number, "executed", state_root)?; + backfilled.verify_proofs(number, "backfilled", state_root)?; + + if number == genesis.number { + genesis_view = Some(indexed); + } else if number == tip.number { + tip_view = Some(indexed); + } + } + let (genesis_view, tip_view) = + (genesis_view.expect("genesis read"), tip_view.expect("tip read")); + content.assert_effect(&genesis_view, &tip_view, &blocks[0], contract) +} + +/// The indexed, executed and backfilled stores serve the same state at every block, whatever the +/// blocks did, whether the engine flushed every block or every fifth, and on either backend. The +/// backfilled store is always MDBX, the only backend that supports backfill. +#[rstest] +#[case::deploy_then_call(BlockContent::DeployThenCall)] +#[case::sstore_to_zero(BlockContent::SstoreToZero)] +#[case::create_and_selfdestruct_in_one_tx(BlockContent::CreateAndSelfdestructInOneTx)] +#[case::selfdestruct_pre_existing(BlockContent::SelfdestructPreExisting)] +#[case::eip161_touch(BlockContent::Eip161Touch)] +#[case::block_hash_read(BlockContent::BlockHashRead)] +#[serial] +fn index_execute_and_backfill_stores_agree( + #[values(FlushCadence::new(1, 2), FlushCadence::new(5, 10))] cadence: FlushCadence, + #[values(Backend::InMemory, Backend::Mdbx)] backend: Backend, + #[case] content: BlockContent, +) -> eyre::Result<()> { + match backend { + Backend::InMemory => run_parity_case(InMemoryProofsStorage::new, cadence, content), + Backend::Mdbx => run_parity_case(create_mdbx_proofs_storage, cadence, content), + } +} 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 20e4a8008ea..c4e4f751dfc 100644 --- a/rust/op-reth/crates/trie/tests/live/parent_states.rs +++ b/rust/op-reth/crates/trie/tests/live/parent_states.rs @@ -8,18 +8,19 @@ //! each of those parent states. use super::{ - BlockSpec, TxSpec, chain_spec_with_address, create_block_from_spec, create_mdbx_proofs_storage, - initialize_storage, public_key_to_address, + BlockSpec, TxSpec, chain_spec_with_address, + contracts::{ + BLOCK_HASH_RECORDER_RUNTIME, BLOCK_HASH_SLOT, CALLED_SLOT, block_number_calldata, + initcode_returning, + }, + create_mdbx_proofs_storage, initialize_storage, public_key_to_address, + simulated_reth::{Location, RethPersistence, SimulatedReth, fixed_signer}, }; use alloy_consensus::BlockHeader; use alloy_eips::BlockNumHash; -use alloy_primitives::{Address, B256, Bytes, U256}; +use alloy_primitives::{Address, Bytes, U256}; use parking_lot::Mutex; -use reth_chain_state::{CanonicalInMemoryState, ExecutedBlock, NewCanonicalChain}; -use reth_chainspec::ChainSpec; -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, @@ -28,81 +29,15 @@ use reth_optimism_trie::{ provider::OpProofsStateProviderRef, }; use reth_primitives_traits::RecoveredBlock; -use reth_provider::{ - HashedPostStateProvider, ProviderFactory, SaveBlocksInput, StateProvider, StateProviderFactory, - StateRootProvider, - noop::NoopProvider, - providers::BlockchainProvider, - 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 reth_provider::{StateProvider, StateRootProvider, noop::NoopProvider}; +use reth_trie_common::HashedPostState; use rstest::rstest; -use secp256k1::{Keypair, Secp256k1}; use serial_test::serial; use std::{fmt, sync::Arc}; -/// 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>()) -} - -/// 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 { @@ -180,262 +115,6 @@ enum Outcome { 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 { - /// Settings with reth's default buffer target and no masking. - fn threshold(persistence_threshold: u64) -> Self { - assert!( - 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 + self.memory_block_buffer_target < self.persistence_threshold, - "reth rejects num_state_masking_blocks + memory_block_buffer_target >= persistence_threshold" - ); - Self { num_state_masking_blocks, ..self } - } - - /// 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 -/// 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 evicted from memory; the persisted blocks above it are the masking window. - last_evicted: BlockNumHash, - /// Newest block in the database. - last_persisted: BlockNumHash, - nonce: u64, - overlay_manager: OverlayManager, - persistence: RethPersistence, - provider: BlockchainProvider, -} - -impl SimulatedReth { - fn new( - chain_spec: Arc, - key_pair: Keypair, - persistence: RethPersistence, - ) -> eyre::Result { - 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()); - 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_evicted: genesis, - last_persisted: genesis, - nonce: 0, - overlay_manager, - 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() - } - - 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)?; - 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![]))?; - 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(); - 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()); - } - } - self.in_memory.update_chain(update); - self.in_memory.set_canonical_head(tip); - } - - /// 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) -> eyre::Result { - let parent = self.head(); - 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_evicted = new_db_tip - .saturating_sub(self.persistence.num_state_masking_blocks) - .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 `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(()) - } - - /// 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); - 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); - } -} - type ReadHook = Box; /// A proofs store that runs an action on reth right before the engine opens its next read view. @@ -520,11 +199,14 @@ impl ProofChain { ) -> eyre::Result { let mut blocks = Vec::new(); for _ in 0..leading { - blocks.push(reth.commit(&BlockSpec::new(vec![]))?); + blocks.push(reth.commit(&BlockSpec::new(vec![]))?.recovered_block().clone()); } let deploy_number = leading + 1; let specs = [ - BlockSpec::new(vec![TxSpec::create(contract_initcode(), CONTRACT_GAS_LIMIT)]), + BlockSpec::new(vec![TxSpec::create( + initcode_returning(&BLOCK_HASH_RECORDER_RUNTIME), + CONTRACT_GAS_LIMIT, + )]), BlockSpec::new(vec![TxSpec::call(contract, Bytes::new(), CONTRACT_GAS_LIMIT)]), BlockSpec::new(vec![TxSpec::call( contract, @@ -533,7 +215,7 @@ impl ProofChain { )]), ]; for spec in &specs { - blocks.push(reth.commit(spec)?); + blocks.push(reth.commit(spec)?.recovered_block().clone()); } let deploy = leading as usize; let under_test = match content { @@ -678,7 +360,7 @@ fn run_parent_state_case( parent_state: ParentState, outcome: Outcome, ) -> eyre::Result<()> { - let key_pair = signer(); + let key_pair = fixed_signer(); let sender = public_key_to_address(key_pair.public_key()); let contract = sender.create(0); let chain_spec = chain_spec_with_address(sender); diff --git a/rust/op-reth/crates/trie/tests/live/simulated_reth.rs b/rust/op-reth/crates/trie/tests/live/simulated_reth.rs new file mode 100644 index 00000000000..d520a7246f6 --- /dev/null +++ b/rust/op-reth/crates/trie/tests/live/simulated_reth.rs @@ -0,0 +1,317 @@ +//! reth's database and canonical in-memory state, driven the way reth's engine tree and +//! persistence task drive them. + +use super::{BlockSpec, create_block_from_spec}; +use alloy_eips::BlockNumHash; +use reth_chain_state::{CanonicalInMemoryState, ExecutedBlock, NewCanonicalChain}; +use reth_chainspec::ChainSpec; +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_primitives_traits::RecoveredBlock; +use reth_provider::{ + HashedPostStateProvider, ProviderFactory, SaveBlocksInput, StateProviderFactory, + StateRootProvider, + providers::BlockchainProvider, + 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; +use secp256k1::{Keypair, Secp256k1}; +use std::sync::Arc; + +/// reth's default `--engine.memory-block-buffer-target`. +pub(super) const MEMORY_BLOCK_BUFFER_TARGET: u64 = 5; + +/// A fixed signer, so a failing case reproduces with the same addresses and trie shape. +pub(super) fn fixed_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)] +pub(super) struct Location { + pub(super) canonical: bool, + pub(super) in_memory: bool, + pub(super) persisted: bool, +} + +impl Location { + pub(super) const ABANDONED: Self = + Self { canonical: false, in_memory: false, persisted: false }; + pub(super) const IN_MEMORY: Self = Self { canonical: true, in_memory: true, persisted: false }; + pub(super) const PERSISTED_AND_EVICTED: Self = + Self { canonical: true, in_memory: false, persisted: true }; + pub(super) const PERSISTED_AND_MASKED: Self = + Self { canonical: true, in_memory: true, persisted: true }; +} + +/// 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)] +pub(super) struct RethPersistence { + pub(super) memory_block_buffer_target: u64, + pub(super) num_state_masking_blocks: u64, + pub(super) persistence_threshold: u64, +} + +impl RethPersistence { + /// Settings with reth's default buffer target and no masking. + pub(super) fn threshold(persistence_threshold: u64) -> Self { + assert!( + 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. + pub(super) fn masking(self, num_state_masking_blocks: u64) -> Self { + assert!( + 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 { num_state_masking_blocks, ..self } + } + + /// 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. + pub(super) 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 +/// persistence task drive them: a block is canonical in memory first and reaches the database +/// when the persistence rule fires. +pub(super) struct SimulatedReth { + /// The canonical chain above genesis, indexed by block number minus one. + canonical: Vec, + chain_spec: Arc, + evm_config: EthEvmConfig, + pub(super) 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, + nonce: u64, + overlay_manager: OverlayManager, + pub(super) persistence: RethPersistence, + pub(super) provider: BlockchainProvider, +} + +impl SimulatedReth { + pub(super) fn new( + chain_spec: Arc, + key_pair: Keypair, + persistence: RethPersistence, + ) -> eyre::Result { + 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()); + 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_evicted: genesis, + last_persisted: genesis, + nonce: 0, + overlay_manager, + persistence, + provider, + }) + } + + pub(super) fn num_hash(&self, number: u64) -> BlockNumHash { + match number { + 0 => BlockNumHash::new(0, self.chain_spec.genesis_hash()), + _ => self.block(number).num_hash(), + } + } + + pub(super) fn head(&self) -> BlockNumHash { + self.num_hash(self.canonical.len() as u64) + } + + pub(super) fn block(&self, number: u64) -> &RecoveredBlock { + self.canonical[number as usize - 1].recovered_block() + } + + pub(super) 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. + pub(super) fn commit(&mut self, spec: &BlockSpec) -> eyre::Result { + let block = self.build_block(spec)?; + self.make_canonical(NewCanonicalChain::Commit { new: vec![block.clone()] }); + self.advance_persistence()?; + Ok(block) + } + + /// Appends empty blocks until `reached` holds, which the persistence rule must bring about + /// within one cycle. + pub(super) 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. + pub(super) 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![]))?; + 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(()) + } + + /// Writes every canonical block to the database without evicting any from memory, the state + /// a `proofs init` run finds on a node that has caught up. + pub(super) fn persist_all(&mut self) -> eyre::Result<()> { + let head = self.head().number; + if head > self.last_persisted.number { + self.persist(head)?; + } + Ok(()) + } + + 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()); + } + } + self.in_memory.update_chain(update); + self.in_memory.set_canonical_head(tip); + } + + /// 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) -> eyre::Result { + let parent = self.head(); + 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_evicted = new_db_tip + .saturating_sub(self.persistence.num_state_masking_blocks) + .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 `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(()) + } + + /// 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); + 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); + } +} From 37687f17695a1abc2fec2f6a533089ed2fcff3b1 Mon Sep 17 00:00:00 2001 From: Einar Rasmussen Date: Thu, 15 Oct 2026 01:07:44 -0700 Subject: [PATCH 2/3] op-reth: address review of the proofs parity live test Adds a snapshot-backfilled store so both backfill modes are compared, backfills in batches of three so a backfill commits more than once, and checks the chain length against the flush cadence instead of stating it. Reads each store's own account set instead of a union across stores, verifies proofs only on the reference store, and moves the store view and its comparison into a module of their own. Reuses RecoveredBlock::block_with_parent and TrieCursorIter, lets SimulatedReth spawn the engine and report state roots, and flattens the search for colliding keys. Co-Authored-By: Claude Fable 5.1 (cherry picked from commit b561b8ed89eeeaeeaefd7e162845daa8c52d1f40) (cherry picked from commit d78fb4ece0f34a998b976a47f39316dad6e678d2) --- rust/op-reth/crates/trie/tests/live.rs | 28 +- .../crates/trie/tests/live/contracts.rs | 9 +- .../trie/tests/live/index_execute_parity.rs | 605 +++++++----------- .../crates/trie/tests/live/parent_states.rs | 24 +- .../crates/trie/tests/live/simulated_reth.rs | 44 +- .../crates/trie/tests/live/store_view.rs | 151 +++++ 6 files changed, 469 insertions(+), 392 deletions(-) create mode 100644 rust/op-reth/crates/trie/tests/live/store_view.rs diff --git a/rust/op-reth/crates/trie/tests/live.rs b/rust/op-reth/crates/trie/tests/live.rs index 319d8d359f7..a8f7b3a2cc0 100644 --- a/rust/op-reth/crates/trie/tests/live.rs +++ b/rust/op-reth/crates/trie/tests/live.rs @@ -45,10 +45,15 @@ mod index_execute_parity; mod parent_states; #[path = "live/simulated_reth.rs"] mod simulated_reth; +#[path = "live/store_view.rs"] +mod store_view; /// Seconds between consecutive blocks. const BLOCK_TIME: u64 = 2; +/// Blocks between pruner runs, far beyond any test chain. +const PRUNE_INTERVAL_BLOCKS: u64 = 1000; + /// Timestamp of the block at `number`. const fn block_timestamp(number: u64) -> u64 { number * BLOCK_TIME @@ -124,6 +129,13 @@ struct BlockSpec { txs: Vec, } +impl BlockSpec { + /// A block without transactions. + const fn empty() -> Self { + Self { txs: Vec::new() } + } +} + /// Configuration for a test scenario #[derive(Debug, Constructor)] struct TestScenario { @@ -355,7 +367,8 @@ where // state between iterations (the new async-persistence architecture requires this). let evm_config = EthEvmConfig::ethereum(chain_spec.clone()); let blockchain_db = BlockchainProvider::new(provider_factory.clone())?; - let pruner = OpProofStoragePruner::new(storage.clone(), blockchain_db.clone(), 1000); + let pruner = + OpProofStoragePruner::new(storage.clone(), blockchain_db.clone(), PRUNE_INTERVAL_BLOCKS); let engine_handle = EngineHandle::spawn(evm_config, blockchain_db, storage, pruner); for (idx, block_spec) in scenario.blocks_after_initialization.iter().enumerate() { @@ -458,7 +471,7 @@ where let mut nonce_counter = 0; let incorrect_block = create_block_from_spec( - &BlockSpec::new(vec![]), + &BlockSpec::empty(), incorrect_block_number, incorrect_parent_hash, &chain_spec, @@ -467,7 +480,8 @@ where ); let blockchain_db = BlockchainProvider::new(provider_factory).unwrap(); - let pruner = OpProofStoragePruner::new(storage.clone(), blockchain_db.clone(), 1000); + let pruner = + OpProofStoragePruner::new(storage.clone(), blockchain_db.clone(), PRUNE_INTERVAL_BLOCKS); let engine_handle = EngineHandle::spawn( EthEvmConfig::ethereum(chain_spec.clone()), blockchain_db, @@ -515,7 +529,8 @@ where // Generate a second block normally let blockchain_db = BlockchainProvider::new(provider_factory.clone()).unwrap(); - let pruner = OpProofStoragePruner::new(storage.clone(), blockchain_db.clone(), 1000); + let pruner = + OpProofStoragePruner::new(storage.clone(), blockchain_db.clone(), PRUNE_INTERVAL_BLOCKS); let engine_handle = EngineHandle::spawn( EthEvmConfig::ethereum(chain_spec.clone()), blockchain_db, @@ -530,7 +545,7 @@ where let next_number = 1; let mut block = create_block_from_spec( - &BlockSpec::new(vec![]), + &BlockSpec::empty(), next_number, last_block_hash, &chain_spec, @@ -588,7 +603,8 @@ where )?; let blockchain_db = BlockchainProvider::new(provider_factory.clone()).unwrap(); - let pruner = OpProofStoragePruner::new(storage.clone(), blockchain_db.clone(), 1000); + let pruner = + OpProofStoragePruner::new(storage.clone(), blockchain_db.clone(), PRUNE_INTERVAL_BLOCKS); let engine_handle = EngineHandle::spawn( EthEvmConfig::ethereum(chain_spec.clone()), blockchain_db, diff --git a/rust/op-reth/crates/trie/tests/live/contracts.rs b/rust/op-reth/crates/trie/tests/live/contracts.rs index 76ee09788bc..72deb0c3939 100644 --- a/rust/op-reth/crates/trie/tests/live/contracts.rs +++ b/rust/op-reth/crates/trie/tests/live/contracts.rs @@ -25,8 +25,8 @@ pub(super) const BLOCK_HASH_RECORDER_RUNTIME: [u8; 19] = [ /// Number of storage slots the slot writer takes from calldata. pub(super) const SLOT_WRITER_SLOTS: usize = 8; -/// Runtime code of the slot writer: a call with calldata stores its first eight 32-byte words -/// into `slots`, in order; a call without calldata self-destructs to `beneficiary`. +/// Runtime code of the slot writer: a call with calldata stores one 32-byte calldata word per +/// entry of `slots`, in order; a call without calldata self-destructs to `beneficiary`. /// /// ```text /// CALLDATASIZE ISZERO PUSH1 JUMPI @@ -56,8 +56,9 @@ pub(super) fn slot_writer_runtime(beneficiary: Address, slots: [u8; SLOT_WRITER_ /// Number of storage slots [`create_and_selfdestruct_initcode`] writes before self-destructing. pub(super) const CREATE_AND_SELFDESTRUCT_SLOTS: u8 = 4; -/// Initcode that stores seven into four slots and then self-destructs to `beneficiary` within -/// the creating transaction, which under EIP-6780 removes the account with its storage. +/// Initcode that stores seven into the first [`CREATE_AND_SELFDESTRUCT_SLOTS`] slots and then +/// self-destructs to `beneficiary` within the creating transaction, which under EIP-6780 removes +/// the account with its storage. /// /// ```text /// PUSH1 7 PUSH1 0 SSTORE diff --git a/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs b/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs index 1463331ebb8..1957750fd3f 100644 --- a/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs +++ b/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs @@ -1,67 +1,61 @@ -//! Builds one chain into three proofs stores, one indexed from reth's trie data, one re-executed -//! by the engine and one initialized at the tip and backfilled, and asserts that they serve the +//! Builds one chain into four proofs stores, one indexed from reth's trie data, one re-executed +//! by the engine and two initialized at the tip and backfilled, and asserts that they serve the //! same accounts, storage, trie nodes and proofs at every block. //! -//! The index and execute paths are the ExEx's only cross-check of each other, and `proofs init` -//! plus backfill is the third way a store comes to hold a block, so the three must agree on every -//! kind of state change a block can make. The engine's buffer is not readable from outside, so -//! the flush cadence decides whether the execute path reads a parent from the buffer or from -//! disk, and the comparison runs over the persisted history once everything is flushed. +//! A store receives a block by indexing reth's trie data, by executing it, or through +//! `proofs init` plus backfill, so all of them must write the same state for every kind of state +//! change a block can make. The flush cadence decides whether the execute path reads a parent +//! from the engine buffer or from disk. The buffer is not readable from outside, so the +//! comparison runs over the persisted history once everything is flushed. use super::{ BlockSpec, TxSpec, block_timestamp, chain_spec_builder, contracts::{ - BLOCK_HASH_RECORDER_RUNTIME, BLOCK_HASH_SLOT, CREATE_AND_SELFDESTRUCT_SLOTS, + BLOCK_HASH_RECORDER_RUNTIME, BLOCK_HASH_SLOT, CALLED_SLOT, CREATE_AND_SELFDESTRUCT_SLOTS, SLOT_WRITER_SLOTS, block_number_calldata, create_and_selfdestruct_initcode, initcode_returning, slot_writer_calldata, slot_writer_runtime, }, create_mdbx_proofs_storage, funded, initialize_storage, public_key_to_address, simulated_reth::{RethPersistence, SimulatedReth, fixed_signer}, + store_view::{BlockView, ProofTarget, assert_same_view}, }; -use alloy_consensus::BlockHeader; use alloy_eips::{ BlockNumHash, - eip1898::BlockWithParent, eip4788::{BEACON_ROOTS_ADDRESS, BEACON_ROOTS_CODE}, }; use alloy_genesis::GenesisAccount; use alloy_primitives::{Address, B256, Bytes, U256, keccak256}; use reth_chainspec::ChainSpec; use reth_ethereum_primitives::Block; -use reth_evm_ethereum::EthEvmConfig; use reth_optimism_trie::{ - BackfillJob, InMemoryProofsStorage, OpProofStoragePruner, OpProofsProviderRO, OpProofsStore, - ProofWindowRange, engine::EngineHandle, proof::DatabaseProof, + BackfillJob, InMemoryProofsStorage, MdbxProofsStorage, OpProofsProviderRO, OpProofsStore, + ProofWindowRange, engine::EngineHandle, }; -use reth_primitives_traits::{Account, RecoveredBlock}; use reth_provider::DatabaseProviderFactory; -use reth_trie::{hashed_cursor::HashedCursor, proof::Proof, trie_cursor::TrieCursor}; -use reth_trie_common::{AccountProof, BranchNodeCompact, Nibbles, TrieInput}; use rstest::rstest; use serial_test::serial; -use std::{ - collections::{BTreeMap, BTreeSet}, - fmt, - sync::Arc, -}; +use std::{collections::BTreeSet, sync::Arc}; -/// Blocks in every chain: a multiple of both persistence thresholds, so the final flush leaves -/// nothing in the engine buffer. +/// Blocks in every chain. const CHAIN_LENGTH: u64 = 10; -/// Empty blocks before the content, which places the content across the flush at block 5 under -/// the slower cadence. +/// Empty blocks before the content, so that multi-block content straddles the flush at block 5 +/// under the slower cadence. const LEADING_FILLERS: u64 = 3; +/// Blocks per backfill transaction, so that a backfill commits more than once. +const BACKFILL_BATCH_SIZE: usize = 3; + /// Covers deploying either contract. const DEPLOY_GAS_LIMIT: u64 = 200_000; /// Covers eight first-time storage writes, or four writes and a self-destruct. const WRITE_GAS_LIMIT: u64 = 400_000; -/// reth's persistence threshold: above the chain length, so reth keeps every block in memory -/// until the backfilled store needs them on disk. -const RETH_PERSISTENCE_THRESHOLD: u64 = 50; +/// reth's persistence threshold, above the chain length so that reth's own persistence never +/// runs: where reth holds a parent is the parent-state cases' concern, and `persist_all` writes +/// the chain for the backfilled stores at the end. +const RETH_PERSISTENCE_THRESHOLD: u64 = CHAIN_LENGTH + 1; /// Slots of the EIP-4788 ring buffer, which every block writes. const BEACON_ROOTS_RING_LENGTH: u64 = 8191; @@ -72,6 +66,10 @@ const BENEFICIARY: Address = Address::repeat_byte(0xBE); /// An address no transaction ever funds; touching it creates nothing. const NEVER_FUNDED: Address = Address::repeat_byte(0xE2); +fn leading_nibble(hashed: B256) -> u8 { + hashed[0] >> 4 +} + /// Three keys from `keys` whose hashes share the first nibble, the first two also the second, so /// that a trie holding all three stores a branch node at the first nibble, which removing either /// of the pair collapses. @@ -79,40 +77,39 @@ const NEVER_FUNDED: Address = Address::repeat_byte(0xE2); /// reth stores no branch node for keys that all differ in their first nibble, so a small trie of /// such keys has nothing to update or remove. fn colliding_keys(keys: impl IntoIterator, hash: impl Fn(K) -> B256) -> [K; 3] { - let mut by_first_nibble: BTreeMap> = BTreeMap::new(); - for key in keys { - let hashed = hash(key); - by_first_nibble.entry(hashed[0] >> 4).or_default().push((hashed[0] & 0x0f, key)); - } - for group in by_first_nibble.values() { - for (position, (second_nibble, key)) in group.iter().enumerate() { - let sibling = group[position + 1..].iter().find(|(nibble, _)| nibble == second_nibble); - let cousin = group.iter().find(|(nibble, _)| nibble != second_nibble); - if let (Some((_, sibling)), Some((_, cousin))) = (sibling, cousin) { - return [*key, *sibling, *cousin]; - } - } - } - panic!("no three keys collide on their leading nibbles") + let keys: Vec<(u8, K)> = keys.into_iter().map(|key| (hash(key)[0], key)).collect(); + keys.iter() + .enumerate() + .find_map(|(index, &(leading_byte, key))| { + let (_, sibling) = + keys[index + 1..].iter().find(|(other, _)| *other == leading_byte)?; + let (_, cousin) = keys + .iter() + .find(|(other, _)| other >> 4 == leading_byte >> 4 && *other != leading_byte)?; + Some([key, *sibling, *cousin]) + }) + .expect("three keys collide on their leading nibbles") } fn hashed_slot(slot: u8) -> B256 { keccak256(B256::with_last_byte(slot)) } -/// The slot writer's slots: three that collide on their leading nibbles, then five under distinct -/// first nibbles, so that filling them stores branch nodes and zeroing them removes those nodes. +/// The slot writer's slots: three that collide on their leading nibbles, so that filling them +/// stores a branch node and zeroing them removes it, then slots under distinct first nibbles to +/// fill the writer without a second collision. fn storage_slots() -> [u8; SLOT_WRITER_SLOTS] { let colliding = colliding_keys(0..=u8::MAX, hashed_slot); - let mut taken_nibbles = BTreeSet::from([hashed_slot(colliding[0])[0] >> 4]); - let mut slots = colliding.to_vec(); - slots.extend((0..=u8::MAX).filter(|slot| taken_nibbles.insert(hashed_slot(*slot)[0] >> 4))); - slots.truncate(SLOT_WRITER_SLOTS); - slots.try_into().expect("eight slots") + let mut taken_nibbles = BTreeSet::from([leading_nibble(hashed_slot(colliding[0]))]); + let spread = + (0..=u8::MAX).filter(|&slot| taken_nibbles.insert(leading_nibble(hashed_slot(slot)))); + let slots: Vec = colliding.into_iter().chain(spread).take(SLOT_WRITER_SLOTS).collect(); + slots.try_into().expect("256 slots cover all sixteen leading nibbles") } -/// Three genesis accounts colliding on their leading hashed nibbles: the empty one EIP-161 -/// removes once a transaction touches it, its sibling and their cousin, both funded. +/// Three genesis accounts, above the precompile addresses, colliding on their leading hashed +/// nibbles: the empty one EIP-161 removes once a transaction touches it, its sibling and their +/// cousin, both funded. fn genesis_neighbors() -> [Address; 3] { colliding_keys((0x10..=u8::MAX).map(Address::with_last_byte), keccak256) } @@ -124,30 +121,32 @@ fn empty_in_genesis() -> Address { /// What the content blocks of the chain do. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -enum BlockContent { - /// Deploys the slot writer, then fills its eight slots. - DeployThenCall, - /// Fills the slot writer's eight slots, zeroes half of them, then all but one. - SstoreToZero, +enum StateChange { + /// Stores `BLOCKHASH` of the first block. + BlockHashRead, /// One transaction creates a contract that writes storage and self-destructs, which EIP-6780 /// removes with its storage. CreateAndSelfdestructInOneTx, - /// Self-destructs the slot writer after filling it; after Cancun the code and storage stay. - SelfdestructPreExisting, + /// Deploys the slot writer, then fills its eight slots. + DeployThenCall, /// Touches an empty account from genesis, which EIP-161 removes, and an address that never /// had anything. Eip161Touch, - /// Stores `BLOCKHASH` of the first block. - BlockHashRead, + /// Self-destructs the slot writer after filling it; after Cancun the code and storage stay. + SelfdestructPreExisting, + /// Fills the slot writer's eight slots, zeroes half of them, then all but one. + SstoreToZero, } -impl BlockContent { - /// The content blocks; the first transaction creates `contract` at the sender's nonce 0. +impl StateChange { + /// The content blocks. `contract` is the address the sender's nonce-0 transaction creates. fn blocks(self, contract: Address) -> Vec { - let deploy_slot_writer = BlockSpec::new(vec![TxSpec::create( - initcode_returning(&slot_writer_runtime(BENEFICIARY, storage_slots())), - DEPLOY_GAS_LIMIT, - )]); + let deploy_slot_writer = || { + BlockSpec::new(vec![TxSpec::create( + initcode_returning(&slot_writer_runtime(BENEFICIARY, storage_slots())), + DEPLOY_GAS_LIMIT, + )]) + }; let write_slots = |values| { BlockSpec::new(vec![TxSpec::call( contract, @@ -156,40 +155,42 @@ impl BlockContent { )]) }; match self { - Self::DeployThenCall => vec![deploy_slot_writer, write_slots([1, 2, 3, 4, 5, 6, 7, 8])], - // Zeroing the first colliding slot removes the stored branch the three share; keeping - // only the cousin leaves a lone leaf. - Self::SstoreToZero => vec![ - deploy_slot_writer, - write_slots([7; SLOT_WRITER_SLOTS]), - write_slots([0, 7, 7, 0, 0, 0, 7, 7]), - write_slots([0, 0, 7, 0, 0, 0, 0, 0]), + Self::BlockHashRead => vec![ + BlockSpec::new(vec![TxSpec::create( + initcode_returning(&BLOCK_HASH_RECORDER_RUNTIME), + DEPLOY_GAS_LIMIT, + )]), + BlockSpec::new(vec![TxSpec::call( + contract, + block_number_calldata(1), + WRITE_GAS_LIMIT, + )]), ], Self::CreateAndSelfdestructInOneTx => vec![BlockSpec::new(vec![ TxSpec::create(create_and_selfdestruct_initcode(BENEFICIARY), WRITE_GAS_LIMIT) .with_value(U256::from(1)), ])], + Self::DeployThenCall => { + vec![deploy_slot_writer(), write_slots([1, 2, 3, 4, 5, 6, 7, 8])] + } + Self::Eip161Touch => vec![BlockSpec::new(vec![ + TxSpec::transfer(empty_in_genesis(), U256::ZERO), + TxSpec::transfer(NEVER_FUNDED, U256::ZERO), + ])], Self::SelfdestructPreExisting => vec![ - deploy_slot_writer, + deploy_slot_writer(), write_slots([5; SLOT_WRITER_SLOTS]), BlockSpec::new(vec![ TxSpec::call(contract, Bytes::new(), WRITE_GAS_LIMIT).with_value(U256::from(1)), ]), ], - Self::Eip161Touch => vec![BlockSpec::new(vec![ - TxSpec::transfer(empty_in_genesis(), U256::ZERO), - TxSpec::transfer(NEVER_FUNDED, U256::ZERO), - ])], - Self::BlockHashRead => vec![ - BlockSpec::new(vec![TxSpec::create( - initcode_returning(&BLOCK_HASH_RECORDER_RUNTIME), - DEPLOY_GAS_LIMIT, - )]), - BlockSpec::new(vec![TxSpec::call( - contract, - block_number_calldata(1), - DEPLOY_GAS_LIMIT, - )]), + // Zeroing the first colliding slot removes the stored branch the three share; keeping + // only the cousin leaves a lone leaf. + Self::SstoreToZero => vec![ + deploy_slot_writer(), + write_slots([7; SLOT_WRITER_SLOTS]), + write_slots([0, 7, 7, 0, 0, 0, 7, 7]), + write_slots([0, 0, 7, 0, 0, 0, 0, 0]), ], } } @@ -199,7 +200,7 @@ impl BlockContent { self, genesis: &BlockView, tip: &BlockView, - first_block: &RecoveredBlock, + first_block_hash: B256, contract: Address, ) -> eyre::Result<()> { let hashed_contract = keccak256(contract); @@ -207,33 +208,34 @@ impl BlockContent { let contract_storage = &tip.storages[&hashed_contract]; let beneficiary_balance = tip.accounts.get(&keccak256(BENEFICIARY)).map(|a| a.balance); match self { - Self::DeployThenCall => { - eyre::ensure!(contract_account.is_some_and(|a| a.bytecode_hash.is_some())); - eyre::ensure!(contract_storage.len() == SLOT_WRITER_SLOTS); - } - Self::SstoreToZero => { - let cousin_slot = hashed_slot(storage_slots()[2]); - eyre::ensure!(contract_storage.keys().eq([&cousin_slot]), "{contract_storage:?}"); + Self::BlockHashRead => { + let stored = contract_storage.slots.get(&keccak256(BLOCK_HASH_SLOT)); + eyre::ensure!(stored == Some(&U256::from_be_bytes(first_block_hash.0))); } Self::CreateAndSelfdestructInOneTx => { eyre::ensure!(contract_account.is_none()); - eyre::ensure!(contract_storage.is_empty()); - eyre::ensure!(tip.storage_tries[&hashed_contract].is_empty()); + eyre::ensure!(contract_storage.slots.is_empty()); + eyre::ensure!(contract_storage.trie.is_empty()); eyre::ensure!(beneficiary_balance == Some(U256::from(1))); } - Self::SelfdestructPreExisting => { + Self::DeployThenCall => { eyre::ensure!(contract_account.is_some_and(|a| a.bytecode_hash.is_some())); - eyre::ensure!(contract_storage.len() == SLOT_WRITER_SLOTS); - eyre::ensure!(beneficiary_balance == Some(U256::from(1))); + eyre::ensure!(contract_storage.slots.len() == SLOT_WRITER_SLOTS); } Self::Eip161Touch => { eyre::ensure!(genesis.accounts.contains_key(&keccak256(empty_in_genesis()))); eyre::ensure!(!tip.accounts.contains_key(&keccak256(empty_in_genesis()))); eyre::ensure!(!tip.accounts.contains_key(&keccak256(NEVER_FUNDED))); } - Self::BlockHashRead => { - let stored = contract_storage.get(&keccak256(BLOCK_HASH_SLOT)); - eyre::ensure!(stored == Some(&U256::from_be_bytes(first_block.hash().0))); + Self::SelfdestructPreExisting => { + eyre::ensure!(contract_account.is_some_and(|a| a.bytecode_hash.is_some())); + eyre::ensure!(contract_storage.slots.len() == SLOT_WRITER_SLOTS); + eyre::ensure!(beneficiary_balance == Some(U256::from(1))); + } + Self::SstoreToZero => { + let cousin_slot = hashed_slot(storage_slots()[2]); + let remaining = &contract_storage.slots; + eyre::ensure!(remaining.keys().eq([&cousin_slot]), "{remaining:?}"); } } Ok(()) @@ -248,9 +250,8 @@ struct FlushCadence { } impl FlushCadence { - const fn new(persistence: u64, backpressure: u64) -> Self { - Self { backpressure, persistence } - } + const EVERY_BLOCK: Self = Self { backpressure: 2, persistence: 1 }; + const EVERY_FIFTH_BLOCK: Self = Self { backpressure: 10, persistence: 5 }; } /// Which store implementation the indexed and executed stores use. @@ -260,166 +261,69 @@ enum Backend { Mdbx, } -/// An address whose proof, with these slots, every store must serve identically. -struct ProofTarget { - address: Address, - slots: Vec, -} - -/// Everything a store serves at one block, read through its cursors and proof builder. -struct BlockView { - accounts: BTreeMap, - account_trie: BTreeMap, - proofs: BTreeMap, - storage_tries: BTreeMap>, - storages: BTreeMap>, -} - -impl BlockView { - /// Reads the store at `number`: every account and trie node, the storage and storage trie - /// of each of `hashed_addresses`, and a proof for each target. - fn read( - store: &S, - number: u64, - hashed_addresses: &BTreeSet, - targets: &[ProofTarget], - ) -> eyre::Result { - let provider = store.provider_ro()?; - let mut storages = BTreeMap::new(); - let mut storage_tries = BTreeMap::new(); - for hashed_address in hashed_addresses { - let storage = collect_hashed(provider.storage_hashed_cursor(*hashed_address, number)?)?; - let trie = collect_trie(provider.storage_trie_cursor(*hashed_address, number)?)?; - storages.insert(*hashed_address, storage); - storage_tries.insert(*hashed_address, trie); - } - let proofs = targets - .iter() - .map(|target| { - let proof = Proof::overlay_account_proof( - provider.clone(), - number, - TrieInput::default(), - target.address, - &target.slots, - )?; - Ok((target.address, proof)) - }) - .collect::>()?; - Ok(Self { - accounts: collect_hashed(provider.account_hashed_cursor(number)?)?, - account_trie: collect_trie(provider.account_trie_cursor(number)?)?, - proofs, - storage_tries, - storages, - }) - } - - /// Every proof must hash to the header's state root; this ties the stores to reth, so that - /// three stores agreeing on the wrong state still fail. - fn verify_proofs(&self, number: u64, store: &str, state_root: B256) -> eyre::Result<()> { - for (address, proof) in &self.proofs { - proof.verify(state_root).map_err(|err| { - eyre::eyre!( - "the {store} store's proof of {address} at block {number} does not verify \ - against the header state root: {err}" - ) - })?; - } - Ok(()) - } +/// How a backfill reads the trie state it rewinds from. +#[derive(Clone, Copy, Debug)] +enum BackfillMode { + /// Through the store's history, as `proofs init` does by default. + History, + /// Through a snapshot of the trie at the window's earliest block. + Snapshot, } -/// The hashed addresses every store must serve at `number`: the proof targets and every account -/// any store holds, so that storage a store keeps for an account it dropped is still compared. -fn hashed_addresses_at( - stores: (&A, &B, &C), - number: u64, - targets: &[ProofTarget], -) -> eyre::Result> { - let mut hashed_addresses: BTreeSet = - targets.iter().map(|target| keccak256(target.address)).collect(); - hashed_addresses.extend( - collect_hashed(stores.0.provider_ro()?.account_hashed_cursor(number)?)?.into_keys(), - ); - hashed_addresses.extend( - collect_hashed(stores.1.provider_ro()?.account_hashed_cursor(number)?)?.into_keys(), - ); - hashed_addresses.extend( - collect_hashed(stores.2.provider_ro()?.account_hashed_cursor(number)?)?.into_keys(), - ); - Ok(hashed_addresses) +/// The stores under comparison; the indexed store is the reference the others are held to. +struct ParityStores { + backfilled: Arc, + executed: S, + indexed: S, + snapshot_backfilled: Arc, } -/// Every entry of a hashed cursor, from the lowest key up. -fn collect_hashed(mut cursor: C) -> eyre::Result> { - let mut entries = BTreeMap::new(); - let mut entry = cursor.seek(B256::ZERO)?; - while let Some((key, value)) = entry { - entries.insert(key, value); - entry = cursor.next()?; +impl ParityStores { + fn assert_proof_windows(&self, expected: ProofWindowRange) -> eyre::Result<()> { + assert_proof_window(&self.indexed, "indexed", expected)?; + assert_proof_window(&self.executed, "executed", expected)?; + assert_proof_window(&self.backfilled, "backfilled", expected)?; + assert_proof_window(&self.snapshot_backfilled, "snapshot-backfilled", expected) } - Ok(entries) -} -/// Every node of a trie cursor, from the root path up. -fn collect_trie( - mut cursor: C, -) -> eyre::Result> { - let mut nodes = BTreeMap::new(); - let mut entry = cursor.seek(Nibbles::default())?; - while let Some((path, node)) = entry { - nodes.insert(path, node); - entry = cursor.next()?; + /// Every store serves the indexed store's view at `number`, and that view's proofs hash to + /// `state_root`. + fn assert_agree_at( + &self, + number: u64, + targets: &[ProofTarget], + state_root: B256, + ) -> eyre::Result<()> { + let indexed = BlockView::read(&self.indexed, number, targets)?; + let executed = BlockView::read(&self.executed, number, targets)?; + let backfilled = BlockView::read(&self.backfilled, number, targets)?; + let snapshot_backfilled = BlockView::read(&self.snapshot_backfilled, number, targets)?; + assert_same_view(number, "executed", &indexed, &executed)?; + assert_same_view(number, "backfilled", &indexed, &backfilled)?; + assert_same_view(number, "snapshot-backfilled", &indexed, &snapshot_backfilled)?; + indexed.verify_proofs(number, state_root) } - Ok(nodes) } -/// Fails on the first key at which `actual` differs from the indexed store's `expected`, -/// naming the block, the table and the key. -fn assert_same_table( - number: u64, - store: &str, - table: &str, - expected: &BTreeMap, - actual: &BTreeMap, +fn assert_proof_window( + store: &S, + name: &str, + expected: ProofWindowRange, ) -> eyre::Result<()> { - for key in expected.keys().chain(actual.keys()).collect::>() { - let (want, got) = (expected.get(key), actual.get(key)); - eyre::ensure!( - want == got, - "the {store} store differs from the indexed store at block {number} in {table} at \ - key {key:?}: indexed {want:?}, {store} {got:?}" - ); - } + let window = store.provider_ro()?.get_proof_window()?; + eyre::ensure!( + window == expected, + "the {name} store's ProofWindow is {window:?}, not {expected:?}" + ); Ok(()) } -fn assert_same_view( - number: u64, - store: &str, - expected: &BlockView, - actual: &BlockView, -) -> eyre::Result<()> { - assert_same_table(number, store, "HashedAccounts", &expected.accounts, &actual.accounts)?; - assert_same_table(number, store, "AccountsTrie", &expected.account_trie, &actual.account_trie)?; - for (hashed_address, storage) in &expected.storages { - let table = format!("HashedStorages[{hashed_address}]"); - assert_same_table(number, store, &table, storage, &actual.storages[hashed_address])?; - } - for (hashed_address, trie) in &expected.storage_tries { - let table = format!("StoragesTrie[{hashed_address}]"); - assert_same_table(number, store, &table, trie, &actual.storage_tries[hashed_address])?; - } - assert_same_table(number, store, "AccountProof", &expected.proofs, &actual.proofs) -} - /// The chain: leading fillers, the content, and fillers up to [`CHAIN_LENGTH`]. -fn chain_blocks(content: BlockContent, contract: Address) -> Vec { - let mut blocks: Vec = (0..LEADING_FILLERS).map(|_| BlockSpec::new(vec![])).collect(); - blocks.extend(content.blocks(contract)); +fn chain_blocks(change: StateChange, contract: Address) -> Vec { + let mut blocks: Vec = (0..LEADING_FILLERS).map(|_| BlockSpec::empty()).collect(); + blocks.extend(change.blocks(contract)); assert!(blocks.len() as u64 <= CHAIN_LENGTH, "the content must fit the chain"); - blocks.resize_with(CHAIN_LENGTH as usize, || BlockSpec::new(vec![])); + blocks.resize_with(CHAIN_LENGTH as usize, BlockSpec::empty); blocks } @@ -427,15 +331,16 @@ fn chain_blocks(content: BlockContent, contract: Address) -> Vec { /// neighbors, and the EIP-4788 contract every block writes to. fn cancun_chain_spec(sender: Address) -> Arc { let [empty, sibling, cousin] = genesis_neighbors(); - let one_wei = GenesisAccount { balance: U256::from(1), ..Default::default() }; - let beacon_roots = - GenesisAccount { code: Some(BEACON_ROOTS_CODE.clone()), ..Default::default() }; + let one_wei = GenesisAccount::default().with_balance(U256::from(1)); let alloc = [ funded(sender), (empty, GenesisAccount::default()), (sibling, one_wei.clone()), (cousin, one_wei), - (BEACON_ROOTS_ADDRESS, beacon_roots), + ( + BEACON_ROOTS_ADDRESS, + GenesisAccount::default().with_code(Some(BEACON_ROOTS_CODE.clone())), + ), ]; Arc::new(chain_spec_builder(alloc).cancun_activated().build()) } @@ -447,6 +352,7 @@ fn proof_targets(sender: Address, contract: Address) -> Vec { .into_iter() .chain(0..CREATE_AND_SELFDESTRUCT_SLOTS) .map(B256::with_last_byte) + .chain([CALLED_SLOT, BLOCK_HASH_SLOT]) .collect(); let beacon_roots_slots = (1..=CHAIN_LENGTH) .flat_map(|number| { @@ -465,26 +371,6 @@ fn proof_targets(sender: Address, contract: Address) -> Vec { targets } -fn spawn_engine( - chain_spec: &Arc, - reth: &SimulatedReth, - store: S, - cadence: FlushCadence, -) -> EngineHandle -where - S: OpProofsStore + Clone + 'static, -{ - let pruner = OpProofStoragePruner::new(store.clone(), reth.provider.clone(), 1000); - EngineHandle::spawn_with_thresholds( - EthEvmConfig::ethereum(chain_spec.clone()), - reth.provider.clone(), - store, - pruner, - cadence.persistence, - cadence.backpressure, - ) -} - /// `flush` only waits for the save in flight and the engine starts the next save after it /// replies, so a buffer above the threshold drains over several flushes. fn flush_until_persisted( @@ -501,113 +387,116 @@ fn flush_until_persisted( eyre::bail!("the engine did not persist through {tip:?}") } -fn block_with_parent(block: &RecoveredBlock) -> BlockWithParent { - BlockWithParent::new(block.parent_hash(), block.num_hash()) +/// Commits the chain to reth while feeding every block to an indexing engine over one store and +/// an executing engine over another, and returns the two stores once both hold the whole chain. +fn index_and_execute_chain( + reth: &mut SimulatedReth, + make_store: impl Fn() -> S, + cadence: FlushCadence, + blocks: Vec, +) -> eyre::Result<(S, S)> +where + S: OpProofsStore + Clone + 'static, +{ + let indexed = make_store(); + let executed = make_store(); + initialize_storage(indexed.clone(), &reth.factory, reth.head())?; + initialize_storage(executed.clone(), &reth.factory, reth.head())?; + let indexed_engine = + reth.spawn_engine(indexed.clone(), cadence.persistence, cadence.backpressure); + let executed_engine = + reth.spawn_engine(executed.clone(), cadence.persistence, cadence.backpressure); + for spec in &blocks { + let reth_block = reth.commit(spec)?; + let block = reth_block.recovered_block(); + indexed_engine.index_block( + block.block_with_parent(), + (*reth_block.trie_updates()).clone(), + (*reth_block.hashed_state()).clone(), + )?; + executed_engine.execute_block(block)?; + } + flush_until_persisted(&indexed_engine, &indexed, reth.head())?; + flush_until_persisted(&executed_engine, &executed, reth.head())?; + Ok((indexed, executed)) +} + +/// Builds a store the way `proofs init` does: initialized at reth's database tip, then backfilled +/// to genesis. +fn backfill_store( + reth: &SimulatedReth, + mode: BackfillMode, +) -> eyre::Result> { + let store = create_mdbx_proofs_storage(); + initialize_storage(store.clone(), &reth.factory, reth.head())?; + let job = BackfillJob::new(reth.factory.database_provider_ro()?, store.clone()) + .with_batch_size(BACKFILL_BATCH_SIZE); + match mode { + BackfillMode::History => job.run(0)?, + BackfillMode::Snapshot => job.run_with_snapshot(0)?, + } + Ok(store) } fn run_parity_case( make_store: impl Fn() -> S, cadence: FlushCadence, - content: BlockContent, + change: StateChange, ) -> eyre::Result<()> where - S: OpProofsStore + Clone + Send + Sync + fmt::Debug + 'static, + S: OpProofsStore + Clone + 'static, { + assert_eq!( + CHAIN_LENGTH % cadence.persistence, + 0, + "the final flush must leave nothing buffered" + ); let key_pair = fixed_signer(); let sender = public_key_to_address(key_pair.public_key()); let contract = sender.create(0); - let chain_spec = cancun_chain_spec(sender); let persistence = RethPersistence::threshold(RETH_PERSISTENCE_THRESHOLD); - let mut reth = SimulatedReth::new(chain_spec.clone(), key_pair, persistence)?; + let mut reth = SimulatedReth::new(cancun_chain_spec(sender), key_pair, persistence)?; let genesis = reth.head(); - let indexed_store = make_store(); - let executed_store = make_store(); - initialize_storage(indexed_store.clone(), &reth.factory, genesis)?; - initialize_storage(executed_store.clone(), &reth.factory, genesis)?; - let indexed_engine = spawn_engine(&chain_spec, &reth, indexed_store.clone(), cadence); - let executed_engine = spawn_engine(&chain_spec, &reth, executed_store.clone(), cadence); - - let mut blocks = Vec::new(); - for spec in chain_blocks(content, contract) { - let executed = reth.commit(&spec)?; - let block = executed.recovered_block(); - indexed_engine.index_block( - block_with_parent(block), - (*executed.trie_updates()).clone(), - (*executed.hashed_state()).clone(), - )?; - executed_engine.execute_block(block)?; - blocks.push(block.clone()); - } + let blocks = chain_blocks(change, contract); + let (indexed, executed) = index_and_execute_chain(&mut reth, make_store, cadence, blocks)?; let tip = reth.head(); - flush_until_persisted(&indexed_engine, &indexed_store, tip)?; - flush_until_persisted(&executed_engine, &executed_store, tip)?; - reth.persist_all()?; - let backfilled_store = create_mdbx_proofs_storage(); - initialize_storage(backfilled_store.clone(), &reth.factory, tip)?; - BackfillJob::new(reth.factory.database_provider_ro()?, backfilled_store.clone()) - .run(genesis.number)?; + let stores = ParityStores { + backfilled: backfill_store(&reth, BackfillMode::History)?, + executed, + indexed, + snapshot_backfilled: backfill_store(&reth, BackfillMode::Snapshot)?, + }; let targets = proof_targets(sender, contract); - let window = ProofWindowRange { earliest: genesis, latest: tip }; - assert_eq!(indexed_store.provider_ro()?.get_proof_window()?, window, "indexed ProofWindow"); - assert_eq!(executed_store.provider_ro()?.get_proof_window()?, window, "executed ProofWindow"); - assert_eq!( - backfilled_store.provider_ro()?.get_proof_window()?, - window, - "backfilled ProofWindow" - ); - - let mut genesis_view = None; - let mut tip_view = None; + stores.assert_proof_windows(ProofWindowRange { earliest: genesis, latest: tip })?; for number in genesis.number..=tip.number { - let stores = (&indexed_store, &executed_store, &backfilled_store); - let hashed_addresses = hashed_addresses_at(stores, number, &targets)?; - let indexed = BlockView::read(&indexed_store, number, &hashed_addresses, &targets)?; - let executed = BlockView::read(&executed_store, number, &hashed_addresses, &targets)?; - let backfilled = BlockView::read(&backfilled_store, number, &hashed_addresses, &targets)?; - assert_same_view(number, "executed", &indexed, &executed)?; - assert_same_view(number, "backfilled", &indexed, &backfilled)?; - - let state_root = match number { - 0 => chain_spec.genesis_header().state_root, - _ => blocks[number as usize - 1].state_root(), - }; - indexed.verify_proofs(number, "indexed", state_root)?; - executed.verify_proofs(number, "executed", state_root)?; - backfilled.verify_proofs(number, "backfilled", state_root)?; - - if number == genesis.number { - genesis_view = Some(indexed); - } else if number == tip.number { - tip_view = Some(indexed); - } + stores.assert_agree_at(number, &targets, reth.state_root(number))?; } - let (genesis_view, tip_view) = - (genesis_view.expect("genesis read"), tip_view.expect("tip read")); - content.assert_effect(&genesis_view, &tip_view, &blocks[0], contract) + let genesis_view = BlockView::read(&stores.indexed, genesis.number, &targets)?; + let tip_view = BlockView::read(&stores.indexed, tip.number, &targets)?; + change.assert_effect(&genesis_view, &tip_view, reth.block(1).hash(), contract) } -/// The indexed, executed and backfilled stores serve the same state at every block, whatever the -/// blocks did, whether the engine flushed every block or every fifth, and on either backend. The -/// backfilled store is always MDBX, the only backend that supports backfill. +/// The indexed, executed and both backfilled stores serve the same state at every block, whatever +/// the blocks did, whether the engine flushed every block or every fifth, and on either backend. +/// The backfilled stores are always MDBX, the only backend that supports backfill. #[rstest] -#[case::deploy_then_call(BlockContent::DeployThenCall)] -#[case::sstore_to_zero(BlockContent::SstoreToZero)] -#[case::create_and_selfdestruct_in_one_tx(BlockContent::CreateAndSelfdestructInOneTx)] -#[case::selfdestruct_pre_existing(BlockContent::SelfdestructPreExisting)] -#[case::eip161_touch(BlockContent::Eip161Touch)] -#[case::block_hash_read(BlockContent::BlockHashRead)] +#[case::block_hash_read(StateChange::BlockHashRead)] +#[case::create_and_selfdestruct_in_one_tx(StateChange::CreateAndSelfdestructInOneTx)] +#[case::deploy_then_call(StateChange::DeployThenCall)] +#[case::eip161_touch(StateChange::Eip161Touch)] +#[case::selfdestruct_pre_existing(StateChange::SelfdestructPreExisting)] +#[case::sstore_to_zero(StateChange::SstoreToZero)] #[serial] fn index_execute_and_backfill_stores_agree( - #[values(FlushCadence::new(1, 2), FlushCadence::new(5, 10))] cadence: FlushCadence, + #[values(FlushCadence::EVERY_BLOCK, FlushCadence::EVERY_FIFTH_BLOCK)] cadence: FlushCadence, #[values(Backend::InMemory, Backend::Mdbx)] backend: Backend, - #[case] content: BlockContent, + #[case] change: StateChange, ) -> eyre::Result<()> { match backend { - Backend::InMemory => run_parity_case(InMemoryProofsStorage::new, cadence, content), - Backend::Mdbx => run_parity_case(create_mdbx_proofs_storage, cadence, content), + Backend::InMemory => run_parity_case(InMemoryProofsStorage::new, cadence, change), + Backend::Mdbx => run_parity_case(create_mdbx_proofs_storage, cadence, change), } } 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 c4e4f751dfc..48dff707207 100644 --- a/rust/op-reth/crates/trie/tests/live/parent_states.rs +++ b/rust/op-reth/crates/trie/tests/live/parent_states.rs @@ -21,12 +21,9 @@ use alloy_eips::BlockNumHash; use alloy_primitives::{Address, Bytes, U256}; use parking_lot::Mutex; use reth_ethereum_primitives::Block; -use reth_evm_ethereum::EthEvmConfig; use reth_optimism_trie::{ - MdbxProofsStorage, OpProofStoragePruner, OpProofsProviderRO, OpProofsStorageResult, - OpProofsStore, - engine::{EngineError, EngineHandle}, - provider::OpProofsStateProviderRef, + MdbxProofsStorage, OpProofsProviderRO, OpProofsStorageResult, OpProofsStore, + engine::EngineError, provider::OpProofsStateProviderRef, }; use reth_primitives_traits::RecoveredBlock; use reth_provider::{StateProvider, StateRootProvider, noop::NoopProvider}; @@ -199,7 +196,7 @@ impl ProofChain { ) -> eyre::Result { let mut blocks = Vec::new(); for _ in 0..leading { - blocks.push(reth.commit(&BlockSpec::new(vec![]))?.recovered_block().clone()); + blocks.push(reth.commit(&BlockSpec::empty())?.recovered_block().clone()); } let deploy_number = leading + 1; let specs = [ @@ -365,7 +362,7 @@ fn run_parent_state_case( 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 mut reth = SimulatedReth::new(chain_spec, key_pair, persistence)?; initialize_storage(store.clone(), &reth.factory, reth.head())?; let chain = ProofChain::commit( &mut reth, @@ -375,10 +372,7 @@ fn run_parent_state_case( )?; 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. It also // keeps the preceding blocks buffered while the block under test executes, which the read @@ -387,14 +381,8 @@ fn run_parent_state_case( 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, - ); + let engine = reth.spawn_engine(store_with_hook.clone(), stored_blocks, stored_blocks + 1); + let reth = Arc::new(Mutex::new(reth)); for block in chain.preceding() { engine.execute_block(block)?; } diff --git a/rust/op-reth/crates/trie/tests/live/simulated_reth.rs b/rust/op-reth/crates/trie/tests/live/simulated_reth.rs index d520a7246f6..8e44c111255 100644 --- a/rust/op-reth/crates/trie/tests/live/simulated_reth.rs +++ b/rust/op-reth/crates/trie/tests/live/simulated_reth.rs @@ -1,14 +1,17 @@ //! reth's database and canonical in-memory state, driven the way reth's engine tree and //! persistence task drive them. -use super::{BlockSpec, create_block_from_spec}; +use super::{BlockSpec, PRUNE_INTERVAL_BLOCKS, create_block_from_spec}; +use alloy_consensus::BlockHeader; use alloy_eips::BlockNumHash; +use alloy_primitives::B256; use reth_chain_state::{CanonicalInMemoryState, ExecutedBlock, NewCanonicalChain}; use reth_chainspec::ChainSpec; 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::{OpProofStoragePruner, OpProofsStore, engine::EngineHandle}; use reth_primitives_traits::RecoveredBlock; use reth_provider::{ HashedPostStateProvider, ProviderFactory, SaveBlocksInput, StateProviderFactory, @@ -23,7 +26,7 @@ use secp256k1::{Keypair, Secp256k1}; use std::sync::Arc; /// reth's default `--engine.memory-block-buffer-target`. -pub(super) const MEMORY_BLOCK_BUFFER_TARGET: u64 = 5; +const MEMORY_BLOCK_BUFFER_TARGET: u64 = 5; /// A fixed signer, so a failing case reproduces with the same addresses and trie shape. pub(super) fn fixed_signer() -> Keypair { @@ -141,13 +144,20 @@ impl SimulatedReth { }) } - pub(super) fn num_hash(&self, number: u64) -> BlockNumHash { + fn num_hash(&self, number: u64) -> BlockNumHash { match number { 0 => BlockNumHash::new(0, self.chain_spec.genesis_hash()), _ => self.block(number).num_hash(), } } + pub(super) fn state_root(&self, number: u64) -> B256 { + match number { + 0 => self.chain_spec.genesis_header().state_root, + _ => self.block(number).state_root(), + } + } + pub(super) fn head(&self) -> BlockNumHash { self.num_hash(self.canonical.len() as u64) } @@ -156,6 +166,28 @@ impl SimulatedReth { self.canonical[number as usize - 1].recovered_block() } + /// Spawns a proofs engine over `store` that reads reth's state through this chain's provider. + pub(super) fn spawn_engine( + &self, + store: S, + persistence_threshold: u64, + backpressure_threshold: u64, + ) -> EngineHandle + where + S: OpProofsStore + Clone + 'static, + { + let pruner = + OpProofStoragePruner::new(store.clone(), self.provider.clone(), PRUNE_INTERVAL_BLOCKS); + EngineHandle::spawn_with_thresholds( + self.evm_config.clone(), + self.provider.clone(), + store, + pruner, + persistence_threshold, + backpressure_threshold, + ) + } + pub(super) fn locate(&self, block: BlockNumHash) -> Location { let canonical = block.number <= self.head().number && self.num_hash(block.number) == block; Location { @@ -185,7 +217,7 @@ impl SimulatedReth { self.head().number < limit, "the persistence rule did not reach the requested parent state within one cycle" ); - self.commit(&BlockSpec::new(vec![]))?; + self.commit(&BlockSpec::empty())?; } Ok(()) } @@ -195,7 +227,7 @@ impl SimulatedReth { pub(super) 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![]))?; + let fork = self.build_block(&BlockSpec::empty())?; assert_ne!( fork.recovered_block().hash(), old[0].recovered_block().hash(), @@ -204,7 +236,7 @@ impl SimulatedReth { let depth = old.len(); self.make_canonical(NewCanonicalChain::Reorg { new: vec![fork], old }); for _ in 1..depth { - self.commit(&BlockSpec::new(vec![]))?; + self.commit(&BlockSpec::empty())?; } Ok(()) } diff --git a/rust/op-reth/crates/trie/tests/live/store_view.rs b/rust/op-reth/crates/trie/tests/live/store_view.rs new file mode 100644 index 00000000000..795ac337f00 --- /dev/null +++ b/rust/op-reth/crates/trie/tests/live/store_view.rs @@ -0,0 +1,151 @@ +//! What a proofs store serves at one block, read through its public cursors and proof builder, +//! and the comparison of two such views that names the first differing table and key. + +use alloy_primitives::{Address, B256, U256, keccak256}; +use reth_optimism_trie::{OpProofsProviderRO, OpProofsStore, proof::DatabaseProof}; +use reth_primitives_traits::Account; +use reth_trie::{ + hashed_cursor::HashedCursor, + proof::Proof, + trie_cursor::{TrieCursor, TrieCursorIter}, +}; +use reth_trie_common::{AccountProof, BranchNodeCompact, Nibbles, TrieInput}; +use std::{ + collections::{BTreeMap, BTreeSet}, + fmt, +}; + +/// An address whose proof, with these slots, every store must serve identically. +pub(super) struct ProofTarget { + pub(super) address: Address, + pub(super) slots: Vec, +} + +/// One account's storage: its slots and its storage trie nodes. +pub(super) struct AccountStorage { + pub(super) slots: BTreeMap, + pub(super) trie: BTreeMap, +} + +/// Everything a store serves at one block. +pub(super) struct BlockView { + pub(super) account_trie: BTreeMap, + pub(super) accounts: BTreeMap, + pub(super) proofs: BTreeMap, + /// The storage of every account the store holds and of every proof target, by hashed address. + pub(super) storages: BTreeMap, +} + +impl BlockView { + pub(super) fn read( + store: &S, + number: u64, + targets: &[ProofTarget], + ) -> eyre::Result { + let provider = store.provider_ro()?; + let accounts = collect_hashed(provider.account_hashed_cursor(number)?)?; + let hashed_addresses: BTreeSet = accounts + .keys() + .copied() + .chain(targets.iter().map(|target| keccak256(target.address))) + .collect(); + let storages = hashed_addresses + .into_iter() + .map(|hashed_address| { + let slots = + collect_hashed(provider.storage_hashed_cursor(hashed_address, number)?)?; + let trie = collect_trie(provider.storage_trie_cursor(hashed_address, number)?)?; + Ok((hashed_address, AccountStorage { slots, trie })) + }) + .collect::>()?; + let proofs = targets + .iter() + .map(|target| { + let proof = Proof::overlay_account_proof( + provider.clone(), + number, + TrieInput::default(), + target.address, + &target.slots, + )?; + Ok((target.address, proof)) + }) + .collect::>()?; + Ok(Self { + account_trie: collect_trie(provider.account_trie_cursor(number)?)?, + accounts, + proofs, + storages, + }) + } + + /// Every proof must hash to `state_root`, the header's, which ties the store to reth. + pub(super) fn verify_proofs(&self, number: u64, state_root: B256) -> eyre::Result<()> { + for (address, proof) in &self.proofs { + proof.verify(state_root).map_err(|err| { + eyre::eyre!( + "the proof of {address} at block {number} does not verify against the \ + header state root: {err}" + ) + })?; + } + Ok(()) + } +} + +/// Every entry of a hashed cursor, from the lowest key up. +fn collect_hashed(mut cursor: C) -> eyre::Result> { + let mut entries = BTreeMap::new(); + let mut entry = cursor.seek(B256::ZERO)?; + while let Some((key, value)) = entry { + entries.insert(key, value); + entry = cursor.next()?; + } + Ok(entries) +} + +/// Every node of a trie cursor, from the root path up. +fn collect_trie( + mut cursor: C, +) -> eyre::Result> { + Ok(TrieCursorIter::new(&mut cursor).collect::>()?) +} + +/// Fails on the first table and key at which the `store` store's `actual` view differs from the +/// indexed store's `expected` view. +pub(super) fn assert_same_view( + number: u64, + store: &str, + expected: &BlockView, + actual: &BlockView, +) -> eyre::Result<()> { + assert_same_table(number, store, "HashedAccounts", &expected.accounts, &actual.accounts)?; + assert_same_table(number, store, "AccountsTrie", &expected.account_trie, &actual.account_trie)?; + // Equal account tables give both views the same storage keys. + for (hashed_address, storage) in &expected.storages { + let actual_storage = &actual.storages[hashed_address]; + let slots_table = format!("HashedStorages[{hashed_address}]"); + assert_same_table(number, store, &slots_table, &storage.slots, &actual_storage.slots)?; + let trie_table = format!("StoragesTrie[{hashed_address}]"); + assert_same_table(number, store, &trie_table, &storage.trie, &actual_storage.trie)?; + } + assert_same_table(number, store, "AccountProof", &expected.proofs, &actual.proofs) +} + +fn assert_same_table( + number: u64, + store: &str, + table: &str, + expected: &BTreeMap, + actual: &BTreeMap, +) -> eyre::Result<()> { + for key in expected.keys().chain(actual.keys()).collect::>() { + let (want, got) = (expected.get(key), actual.get(key)); + eyre::ensure!( + want == got, + "the {store} store differs from the indexed store at block {number} in {table} at \ + key {key:?}: indexed {want:?}, {store} {got:?}" + ); + } + Ok(()) +} From c20b7c2406a42e2852fe682e10f9d727739dbb12 Mon Sep 17 00:00:00 2001 From: Einar Rasmussen Date: Tue, 10 Nov 2026 00:07:25 +0100 Subject: [PATCH 3/3] op-reth: return the parity test's persistence error as a value Co-Authored-By: Claude Fable 5.1 --- rust/op-reth/crates/trie/tests/live/index_execute_parity.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs b/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs index 1957750fd3f..0698943b148 100644 --- a/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs +++ b/rust/op-reth/crates/trie/tests/live/index_execute_parity.rs @@ -384,7 +384,7 @@ fn flush_until_persisted( return Ok(()); } } - eyre::bail!("the engine did not persist through {tip:?}") + Err(eyre::eyre!("the engine did not persist through {tip:?}")) } /// Commits the chain to reth while feeding every block to an indexing engine over one store and