From 732643fff31564de8e4e36a97c097e5b74d92b38 Mon Sep 17 00:00:00 2001 From: Arun Dhyani Date: Tue, 27 Jan 2026 20:44:43 +0530 Subject: [PATCH 1/2] chore: separate initial state store trait --- crates/optimism/trie/src/api.rs | 54 +++---- crates/optimism/trie/src/db/store.rs | 168 ++++++++++---------- crates/optimism/trie/src/in_memory.rs | 210 ++++++++++++++++++------- crates/optimism/trie/src/initialize.rs | 10 +- crates/optimism/trie/src/lib.rs | 2 +- crates/optimism/trie/src/metrics.rs | 180 ++++++++++----------- crates/optimism/trie/tests/lib.rs | 136 ++++++++++------ 7 files changed, 451 insertions(+), 309 deletions(-) diff --git a/crates/optimism/trie/src/api.rs b/crates/optimism/trie/src/api.rs index bf5868c57c0..d68bce13ed3 100644 --- a/crates/optimism/trie/src/api.rs +++ b/crates/optimism/trie/src/api.rs @@ -87,33 +87,6 @@ pub trait OpProofsStore: Send + Sync + Debug { where Self: 'tx; - /// Store a batch of account trie branches. Used for saving existing state. For live state - /// capture, use [store_trie_updates](OpProofsStore::store_trie_updates). - fn store_account_branches( - &self, - account_nodes: Vec<(Nibbles, Option)>, - ) -> OpProofsStorageResult<()>; - - /// Store a batch of storage trie branches. Used for saving existing state. - fn store_storage_branches( - &self, - hashed_address: B256, - storage_nodes: Vec<(Nibbles, Option)>, - ) -> OpProofsStorageResult<()>; - - /// Store a batch of account trie leaf nodes. Used for saving existing state. - fn store_hashed_accounts( - &self, - accounts: Vec<(B256, Option)>, - ) -> OpProofsStorageResult<()>; - - /// Store a batch of storage trie leaf nodes. Used for saving existing state. - fn store_hashed_storages( - &self, - hashed_address: B256, - storages: Vec<(B256, U256)>, - ) -> OpProofsStorageResult<()>; - /// Get the earliest block number and hash that has been stored /// /// This is used to determine the block number of trie nodes with block number 0. @@ -225,6 +198,33 @@ pub trait OpProofsInitialStateStore: Send + Sync + Debug { /// Returns `Err` if an anchor already exists (prevents accidental overwrite). fn set_initial_state_anchor(&self, anchor: BlockNumHash) -> OpProofsStorageResult<()>; + /// Store a batch of account trie branches. Used for saving existing state. For live state + /// capture, use [store_trie_updates](OpProofsStore::store_trie_updates). + fn store_account_branches( + &self, + account_nodes: Vec<(Nibbles, Option)>, + ) -> OpProofsStorageResult<()>; + + /// Store a batch of storage trie branches. Used for saving existing state. + fn store_storage_branches( + &self, + hashed_address: B256, + storage_nodes: Vec<(Nibbles, Option)>, + ) -> OpProofsStorageResult<()>; + + /// Store a batch of account trie leaf nodes. Used for saving existing state. + fn store_hashed_accounts( + &self, + accounts: Vec<(B256, Option)>, + ) -> OpProofsStorageResult<()>; + + /// Store a batch of storage trie leaf nodes. Used for saving existing state. + fn store_hashed_storages( + &self, + hashed_address: B256, + storages: Vec<(B256, U256)>, + ) -> OpProofsStorageResult<()>; + /// Commit the initial state - mark the anchor as completed and also set the earliest block /// number to anchor. fn commit_initial_state(&self) -> OpProofsStorageResult; diff --git a/crates/optimism/trie/src/db/store.rs b/crates/optimism/trie/src/db/store.rs index da3e364f73f..a1b727dc43e 100644 --- a/crates/optimism/trie/src/db/store.rs +++ b/crates/optimism/trie/src/db/store.rs @@ -636,90 +636,6 @@ impl OpProofsStore for MdbxProofsStorage { where Self: 'tx; - fn store_account_branches( - &self, - account_nodes: Vec<(Nibbles, Option)>, - ) -> OpProofsStorageResult<()> { - let mut account_nodes = account_nodes; - if account_nodes.is_empty() { - return Ok(()); - } - - account_nodes.sort_by_key(|(key, _)| *key); - - self.env.update(|tx| { - self.persist_history_batch(tx, 0, account_nodes.into_iter(), true)?; - Ok(()) - })? - } - - fn store_storage_branches( - &self, - hashed_address: B256, - storage_nodes: Vec<(Nibbles, Option)>, - ) -> OpProofsStorageResult<()> { - let mut storage_nodes = storage_nodes; - if storage_nodes.is_empty() { - return Ok(()); - } - - storage_nodes.sort_by_key(|(key, _)| *key); - - self.env.update(|tx| { - self.persist_history_batch( - tx, - 0, - storage_nodes.into_iter().map(|(path, node)| (hashed_address, path, node)), - true, - )?; - Ok(()) - })? - } - - fn store_hashed_accounts( - &self, - accounts: Vec<(B256, Option)>, - ) -> OpProofsStorageResult<()> { - let mut accounts = accounts; - if accounts.is_empty() { - return Ok(()); - } - - // sort the accounts by key to ensure insertion is efficient - accounts.sort_by_key(|(key, _)| *key); - - self.env.update(|tx| { - self.persist_history_batch(tx, 0, accounts.into_iter(), true)?; - Ok(()) - })? - } - - fn store_hashed_storages( - &self, - hashed_address: B256, - storages: Vec<(B256, U256)>, - ) -> OpProofsStorageResult<()> { - let mut storages = storages; - if storages.is_empty() { - return Ok(()); - } - - // sort the storages by key to ensure insertion is efficient - storages.sort_by_key(|(key, _)| *key); - - self.env.update(|tx| { - self.persist_history_batch( - tx, - 0, - storages - .into_iter() - .map(|(key, val)| (hashed_address, key, Some(StorageValue(val)))), - true, - )?; - Ok(()) - })? - } - fn get_earliest_block_number(&self) -> OpProofsStorageResult> { self.env.view(|tx| self.inner_get_block_number_hash(tx, ProofWindowKey::EarliestBlock))? } @@ -1064,6 +980,90 @@ impl OpProofsInitialStateStore for MdbxProofsStorage { })? } + fn store_account_branches( + &self, + account_nodes: Vec<(Nibbles, Option)>, + ) -> OpProofsStorageResult<()> { + let mut account_nodes = account_nodes; + if account_nodes.is_empty() { + return Ok(()); + } + + account_nodes.sort_by_key(|(key, _)| *key); + + self.env.update(|tx| { + self.persist_history_batch(tx, 0, account_nodes.into_iter(), true)?; + Ok(()) + })? + } + + fn store_storage_branches( + &self, + hashed_address: B256, + storage_nodes: Vec<(Nibbles, Option)>, + ) -> OpProofsStorageResult<()> { + let mut storage_nodes = storage_nodes; + if storage_nodes.is_empty() { + return Ok(()); + } + + storage_nodes.sort_by_key(|(key, _)| *key); + + self.env.update(|tx| { + self.persist_history_batch( + tx, + 0, + storage_nodes.into_iter().map(|(path, node)| (hashed_address, path, node)), + true, + )?; + Ok(()) + })? + } + + fn store_hashed_accounts( + &self, + accounts: Vec<(B256, Option)>, + ) -> OpProofsStorageResult<()> { + let mut accounts = accounts; + if accounts.is_empty() { + return Ok(()); + } + + // sort the accounts by key to ensure insertion is efficient + accounts.sort_by_key(|(key, _)| *key); + + self.env.update(|tx| { + self.persist_history_batch(tx, 0, accounts.into_iter(), true)?; + Ok(()) + })? + } + + fn store_hashed_storages( + &self, + hashed_address: B256, + storages: Vec<(B256, U256)>, + ) -> OpProofsStorageResult<()> { + let mut storages = storages; + if storages.is_empty() { + return Ok(()); + } + + // sort the storages by key to ensure insertion is efficient + storages.sort_by_key(|(key, _)| *key); + + self.env.update(|tx| { + self.persist_history_batch( + tx, + 0, + storages + .into_iter() + .map(|(key, val)| (hashed_address, key, Some(StorageValue(val)))), + true, + )?; + Ok(()) + })? + } + fn commit_initial_state(&self) -> OpProofsStorageResult { let anchor = self.get_initial_state_anchor()?.ok_or(NoBlocksFound)?; self.set_earliest_block_number(anchor.number, anchor.hash)?; diff --git a/crates/optimism/trie/src/in_memory.rs b/crates/optimism/trie/src/in_memory.rs index 8a2b650460d..937ee4688a8 100644 --- a/crates/optimism/trie/src/in_memory.rs +++ b/crates/optimism/trie/src/in_memory.rs @@ -1,9 +1,11 @@ //! In-memory implementation of [`OpProofsStore`] for testing purposes use crate::{ - api::WriteCounts, BlockStateDiff, OpProofsStorageError, OpProofsStorageResult, OpProofsStore, + api::{InitialStateAnchor, InitialStateStatus, OpProofsInitialStateStore, WriteCounts}, + db::{HashedStorageKey, StorageTrieKey}, + BlockStateDiff, OpProofsStorageError, OpProofsStorageResult, OpProofsStore, }; -use alloy_eips::{eip1898::BlockWithParent, BlockNumHash}; +use alloy_eips::{eip1898::BlockWithParent, BlockNumHash, NumHash}; use alloy_primitives::{B256, U256}; use parking_lot::RwLock; use reth_db::DatabaseError; @@ -13,7 +15,7 @@ use reth_trie::{ trie_cursor::{TrieCursor, TrieStorageCursor}, }; use reth_trie_common::{ - updates::TrieUpdatesSorted, BranchNodeCompact, HashedPostStateSorted, Nibbles, + updates::TrieUpdatesSorted, BranchNodeCompact, HashedPostStateSorted, Nibbles, StoredNibbles, }; use std::{collections::BTreeMap, sync::Arc}; @@ -46,6 +48,9 @@ struct InMemoryStorageInner { /// Earliest block number and hash earliest_block: Option<(u64, B256)>, + + /// The anchor block (initial state) of the store. + anchor_block: Option<(u64, B256)>, } impl InMemoryStorageInner { @@ -132,6 +137,56 @@ impl InMemoryProofsStorage { pub fn new() -> Self { Self { inner: Arc::new(RwLock::new(InMemoryStorageInner::default())) } } + + fn get_latest_account_trie_key(&self) -> OpProofsStorageResult> { + let inner = self.inner.read(); + Ok(inner + .account_branches + .range(( + std::ops::Bound::Included((0, Nibbles::default())), + std::ops::Bound::Excluded((1, Nibbles::default())), + )) + .next_back() + .map(|((_, nibbles), _)| StoredNibbles::from(*nibbles))) + } + + fn get_latest_storage_trie_key(&self) -> OpProofsStorageResult> { + let inner = self.inner.read(); + Ok(inner + .storage_branches + .range(( + std::ops::Bound::Included((0, B256::ZERO, Nibbles::default())), + std::ops::Bound::Excluded((1, B256::ZERO, Nibbles::default())), + )) + .next_back() + .map(|((_, address, nibbles), _)| { + StorageTrieKey::new(*address, StoredNibbles::from(*nibbles)) + })) + } + + fn get_latest_hashed_account_key(&self) -> OpProofsStorageResult> { + let inner = self.inner.read(); + Ok(inner + .hashed_accounts + .range(( + std::ops::Bound::Included((0, B256::ZERO)), + std::ops::Bound::Excluded((1, B256::ZERO)), + )) + .next_back() + .map(|((_, address), _)| *address)) + } + + fn get_latest_hashed_storage_key(&self) -> OpProofsStorageResult> { + let inner = self.inner.read(); + Ok(inner + .hashed_storages + .range(( + std::ops::Bound::Included((0, B256::ZERO, B256::ZERO)), + std::ops::Bound::Excluded((1, B256::ZERO, B256::ZERO)), + )) + .next_back() + .map(|((_, address, slot), _)| HashedStorageKey::new(*address, *slot))) + } } /// In-memory implementation of [`TrieCursor`]. @@ -485,60 +540,6 @@ impl OpProofsStore for InMemoryProofsStorage { type StorageCursor<'tx> = InMemoryStorageCursor; type AccountHashedCursor<'tx> = InMemoryAccountCursor; - fn store_account_branches( - &self, - updates: Vec<(Nibbles, Option)>, - ) -> OpProofsStorageResult<()> { - let mut inner = self.inner.write(); - - for (path, branch) in updates { - inner.account_branches.insert((0, path), branch); - } - - Ok(()) - } - - fn store_storage_branches( - &self, - hashed_address: B256, - items: Vec<(Nibbles, Option)>, - ) -> OpProofsStorageResult<()> { - let mut inner = self.inner.write(); - - for (path, branch) in items { - inner.storage_branches.insert((0, hashed_address, path), branch); - } - - Ok(()) - } - - fn store_hashed_accounts( - &self, - accounts: Vec<(B256, Option)>, - ) -> OpProofsStorageResult<()> { - let mut inner = self.inner.write(); - - for (address, account) in accounts { - inner.hashed_accounts.insert((0, address), account); - } - - Ok(()) - } - - fn store_hashed_storages( - &self, - hashed_address: B256, - storages: Vec<(B256, U256)>, - ) -> OpProofsStorageResult<()> { - let mut inner = self.inner.write(); - - for (slot, value) in storages { - inner.hashed_storages.insert((0, hashed_address, slot), value); - } - - Ok(()) - } - fn get_earliest_block_number(&self) -> OpProofsStorageResult> { let inner = self.inner.read(); Ok(inner.earliest_block) @@ -743,6 +744,101 @@ impl OpProofsStore for InMemoryProofsStorage { } } +impl OpProofsInitialStateStore for InMemoryProofsStorage { + fn initial_state_anchor(&self) -> OpProofsStorageResult { + let inner = self.inner.read(); + + let Some((block_num, block_hash)) = inner.anchor_block else { + return Ok(InitialStateAnchor::default()); + }; + + let completed = inner.earliest_block.is_some(); + + Ok(InitialStateAnchor { + block: Some(NumHash::new(block_num, block_hash)), + status: if completed { + InitialStateStatus::Completed + } else { + InitialStateStatus::InProgress + }, + latest_account_trie_key: self.get_latest_account_trie_key()?, + latest_storage_trie_key: self.get_latest_storage_trie_key()?, + latest_hashed_account_key: self.get_latest_hashed_account_key()?, + latest_hashed_storage_key: self.get_latest_hashed_storage_key()?, + }) + } + + fn set_initial_state_anchor(&self, anchor: BlockNumHash) -> OpProofsStorageResult<()> { + let mut inner = self.inner.write(); + inner.anchor_block = Some((anchor.number, anchor.hash)); + Ok(()) + } + + fn store_account_branches( + &self, + updates: Vec<(Nibbles, Option)>, + ) -> OpProofsStorageResult<()> { + let mut inner = self.inner.write(); + + for (path, branch) in updates { + inner.account_branches.insert((0, path), branch); + } + + Ok(()) + } + + fn store_storage_branches( + &self, + hashed_address: B256, + items: Vec<(Nibbles, Option)>, + ) -> OpProofsStorageResult<()> { + let mut inner = self.inner.write(); + + for (path, branch) in items { + inner.storage_branches.insert((0, hashed_address, path), branch); + } + + Ok(()) + } + + fn store_hashed_accounts( + &self, + accounts: Vec<(B256, Option)>, + ) -> OpProofsStorageResult<()> { + let mut inner = self.inner.write(); + + for (address, account) in accounts { + inner.hashed_accounts.insert((0, address), account); + } + + Ok(()) + } + + fn store_hashed_storages( + &self, + hashed_address: B256, + storages: Vec<(B256, U256)>, + ) -> OpProofsStorageResult<()> { + let mut inner = self.inner.write(); + + for (slot, value) in storages { + inner.hashed_storages.insert((0, hashed_address, slot), value); + } + + Ok(()) + } + + fn commit_initial_state(&self) -> OpProofsStorageResult { + let mut inner = self.inner.write(); + if let Some((number, hash)) = inner.anchor_block { + inner.earliest_block = Some((number, hash)); + Ok(BlockNumHash::new(number, hash)) + } else { + Err(OpProofsStorageError::NoBlocksFound) + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/optimism/trie/src/initialize.rs b/crates/optimism/trie/src/initialize.rs index 4339acc05a4..3eb55515071 100644 --- a/crates/optimism/trie/src/initialize.rs +++ b/crates/optimism/trie/src/initialize.rs @@ -367,7 +367,7 @@ trait InitTable { /// Writes given entries to given storage. fn store_entries( - store: &impl OpProofsStore, + store: &impl OpProofsInitialStateStore, entries: impl IntoIterator, ) -> Result<(), OpProofsStorageError>; } @@ -378,7 +378,7 @@ impl InitTable for HashedAccountsInit { /// Save mapping of hashed addresses to accounts to storage. fn store_entries( - store: &impl OpProofsStore, + store: &impl OpProofsInitialStateStore, entries: impl IntoIterator, ) -> Result<(), OpProofsStorageError> { store.store_hashed_accounts( @@ -394,7 +394,7 @@ impl InitTable for HashedStoragesInit { /// Save mapping of hashed addresses to storage entries to storage. fn store_entries( - store: &impl OpProofsStore, + store: &impl OpProofsInitialStateStore, entries: impl IntoIterator, ) -> Result<(), OpProofsStorageError> { let entries_iter = entries.into_iter(); @@ -420,7 +420,7 @@ impl InitTable for AccountsTrieInit { /// Save mapping of account trie paths to branch nodes to storage. fn store_entries( - store: &impl OpProofsStore, + store: &impl OpProofsInitialStateStore, entries: impl IntoIterator, ) -> Result<(), OpProofsStorageError> { store.store_account_branches( @@ -437,7 +437,7 @@ impl InitTable for StoragesTrieInit { /// Save mapping of hashed addresses to storage trie entries to storage. fn store_entries( - store: &impl OpProofsStore, + store: &impl OpProofsInitialStateStore, entries: impl IntoIterator, ) -> Result<(), OpProofsStorageError> { let entries_iter = entries.into_iter(); diff --git a/crates/optimism/trie/src/lib.rs b/crates/optimism/trie/src/lib.rs index 1de99b3ed1d..667952f069f 100644 --- a/crates/optimism/trie/src/lib.rs +++ b/crates/optimism/trie/src/lib.rs @@ -13,7 +13,7 @@ #![cfg_attr(not(test), warn(unused_crate_dependencies))] pub mod api; -pub use api::{BlockStateDiff, OpProofsStore}; +pub use api::{BlockStateDiff, OpProofsInitialStateStore, OpProofsStore}; pub mod initialize; pub use initialize::InitializationJob; diff --git a/crates/optimism/trie/src/metrics.rs b/crates/optimism/trie/src/metrics.rs index 03e9470a0cd..b9e2dba8d92 100644 --- a/crates/optimism/trie/src/metrics.rs +++ b/crates/optimism/trie/src/metrics.rs @@ -379,96 +379,6 @@ where where Self: 'tx; - #[inline] - fn store_account_branches( - &self, - account_nodes: Vec<(Nibbles, Option)>, - ) -> OpProofsStorageResult<()> { - let count = account_nodes.len(); - let start = Instant::now(); - let result = self.storage.store_account_branches(account_nodes); - let duration = start.elapsed(); - - // Record per-item duration - if count > 0 { - self.metrics.record_duration_per_item( - StorageOperation::StoreAccountBranch, - duration, - count, - ); - } - - result - } - - #[inline] - fn store_storage_branches( - &self, - hashed_address: B256, - storage_nodes: Vec<(Nibbles, Option)>, - ) -> OpProofsStorageResult<()> { - let count = storage_nodes.len(); - let start = Instant::now(); - let result = self.storage.store_storage_branches(hashed_address, storage_nodes); - let duration = start.elapsed(); - - // Record per-item duration - if count > 0 { - self.metrics.record_duration_per_item( - StorageOperation::StoreStorageBranch, - duration, - count, - ); - } - - result - } - - #[inline] - fn store_hashed_accounts( - &self, - accounts: Vec<(B256, Option)>, - ) -> OpProofsStorageResult<()> { - let count = accounts.len(); - let start = Instant::now(); - let result = self.storage.store_hashed_accounts(accounts); - let duration = start.elapsed(); - - // Record per-item duration - if count > 0 { - self.metrics.record_duration_per_item( - StorageOperation::StoreHashedAccount, - duration, - count, - ); - } - - result - } - - #[inline] - fn store_hashed_storages( - &self, - hashed_address: B256, - storages: Vec<(B256, U256)>, - ) -> OpProofsStorageResult<()> { - let count = storages.len(); - let start = Instant::now(); - let result = self.storage.store_hashed_storages(hashed_address, storages); - let duration = start.elapsed(); - - // Record per-item duration - if count > 0 { - self.metrics.record_duration_per_item( - StorageOperation::StoreHashedStorage, - duration, - count, - ); - } - - result - } - #[inline] fn get_earliest_block_number(&self) -> OpProofsStorageResult> { self.storage.get_earliest_block_number() @@ -582,6 +492,96 @@ where self.storage.set_initial_state_anchor(anchor) } + #[inline] + fn store_account_branches( + &self, + account_nodes: Vec<(Nibbles, Option)>, + ) -> OpProofsStorageResult<()> { + let count = account_nodes.len(); + let start = Instant::now(); + let result = self.storage.store_account_branches(account_nodes); + let duration = start.elapsed(); + + // Record per-item duration + if count > 0 { + self.metrics.record_duration_per_item( + StorageOperation::StoreAccountBranch, + duration, + count, + ); + } + + result + } + + #[inline] + fn store_storage_branches( + &self, + hashed_address: B256, + storage_nodes: Vec<(Nibbles, Option)>, + ) -> OpProofsStorageResult<()> { + let count = storage_nodes.len(); + let start = Instant::now(); + let result = self.storage.store_storage_branches(hashed_address, storage_nodes); + let duration = start.elapsed(); + + // Record per-item duration + if count > 0 { + self.metrics.record_duration_per_item( + StorageOperation::StoreStorageBranch, + duration, + count, + ); + } + + result + } + + #[inline] + fn store_hashed_accounts( + &self, + accounts: Vec<(B256, Option)>, + ) -> OpProofsStorageResult<()> { + let count = accounts.len(); + let start = Instant::now(); + let result = self.storage.store_hashed_accounts(accounts); + let duration = start.elapsed(); + + // Record per-item duration + if count > 0 { + self.metrics.record_duration_per_item( + StorageOperation::StoreHashedAccount, + duration, + count, + ); + } + + result + } + + #[inline] + fn store_hashed_storages( + &self, + hashed_address: B256, + storages: Vec<(B256, U256)>, + ) -> OpProofsStorageResult<()> { + let count = storages.len(); + let start = Instant::now(); + let result = self.storage.store_hashed_storages(hashed_address, storages); + let duration = start.elapsed(); + + // Record per-item duration + if count > 0 { + self.metrics.record_duration_per_item( + StorageOperation::StoreHashedStorage, + duration, + count, + ); + } + + result + } + #[inline] fn commit_initial_state(&self) -> OpProofsStorageResult { let block = self.storage.commit_initial_state()?; diff --git a/crates/optimism/trie/tests/lib.rs b/crates/optimism/trie/tests/lib.rs index 73f39d6d032..59ed6fd47c8 100644 --- a/crates/optimism/trie/tests/lib.rs +++ b/crates/optimism/trie/tests/lib.rs @@ -3,8 +3,8 @@ use alloy_eips::{eip1898::BlockWithParent, BlockNumHash, NumHash}; use alloy_primitives::{B256, U256}; use reth_optimism_trie::{ - db::MdbxProofsStorage, BlockStateDiff, InMemoryProofsStorage, OpProofsStorageError, - OpProofsStore, + db::MdbxProofsStorage, BlockStateDiff, InMemoryProofsStorage, OpProofsInitialStateStore, + OpProofsStorageError, OpProofsStore, }; use reth_primitives_traits::Account; use reth_trie::{ @@ -80,7 +80,7 @@ fn create_mdbx_proofs_storage() -> MdbxProofsStorage { #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_earliest_block_operations( +fn test_earliest_block_operations( storage: S, ) -> Result<(), OpProofsStorageError> { // Initially should be None @@ -102,7 +102,9 @@ fn test_earliest_block_operations( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_trie_updates_operations(storage: S) -> Result<(), OpProofsStorageError> { +fn test_trie_updates_operations( + storage: S, +) -> Result<(), OpProofsStorageError> { let block_ref = BlockWithParent::new(B256::ZERO, NumHash::new(50, B256::repeat_byte(0x96))); let sorted_trie_updates = TrieUpdatesSorted::default(); let sorted_post_state = HashedPostStateSorted::default(); @@ -130,7 +132,9 @@ fn test_trie_updates_operations(storage: S) -> Result<(), OpPr #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_cursor_empty_trie(storage: S) -> Result<(), OpProofsStorageError> { +fn test_cursor_empty_trie( + storage: S, +) -> Result<(), OpProofsStorageError> { let mut cursor = storage.account_trie_cursor(100)?; // All operations should return None on empty trie @@ -146,7 +150,9 @@ fn test_cursor_empty_trie(storage: S) -> Result<(), OpProofsSt #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_cursor_single_entry(storage: S) -> Result<(), OpProofsStorageError> { +fn test_cursor_single_entry( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1, 2, 3]); let branch = create_test_branch(); @@ -172,7 +178,9 @@ fn test_cursor_single_entry(storage: S) -> Result<(), OpProofs #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_cursor_multiple_entries(storage: S) -> Result<(), OpProofsStorageError> { +fn test_cursor_multiple_entries( + storage: S, +) -> Result<(), OpProofsStorageError> { let paths = vec![ nibbles_from(vec![1]), nibbles_from(vec![1, 2]), @@ -211,7 +219,9 @@ fn test_cursor_multiple_entries(storage: S) -> Result<(), OpPr #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_seek_exact_existing_path(storage: S) -> Result<(), OpProofsStorageError> { +fn test_seek_exact_existing_path( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1, 2, 3]); let branch = create_test_branch(); @@ -228,7 +238,7 @@ fn test_seek_exact_existing_path(storage: S) -> Result<(), OpP #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_seek_exact_non_existing_path( +fn test_seek_exact_non_existing_path( storage: S, ) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1, 2, 3]); @@ -247,7 +257,9 @@ fn test_seek_exact_non_existing_path( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_seek_exact_empty_path(storage: S) -> Result<(), OpProofsStorageError> { +fn test_seek_exact_empty_path( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![]); let branch = create_test_branch(); @@ -264,7 +276,9 @@ fn test_seek_exact_empty_path(storage: S) -> Result<(), OpProo #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_seek_to_existing_path(storage: S) -> Result<(), OpProofsStorageError> { +fn test_seek_to_existing_path( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1, 2, 3]); let branch = create_test_branch(); @@ -281,7 +295,7 @@ fn test_seek_to_existing_path(storage: S) -> Result<(), OpProo #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_seek_between_existing_nodes( +fn test_seek_between_existing_nodes( storage: S, ) -> Result<(), OpProofsStorageError> { let path1 = nibbles_from(vec![1]); @@ -304,7 +318,9 @@ fn test_seek_between_existing_nodes( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_seek_after_all_nodes(storage: S) -> Result<(), OpProofsStorageError> { +fn test_seek_after_all_nodes( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1]); let branch = create_test_branch(); @@ -322,7 +338,9 @@ fn test_seek_after_all_nodes(storage: S) -> Result<(), OpProof #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_seek_before_all_nodes(storage: S) -> Result<(), OpProofsStorageError> { +fn test_seek_before_all_nodes( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![5]); let branch = create_test_branch(); @@ -345,7 +363,9 @@ fn test_seek_before_all_nodes(storage: S) -> Result<(), OpProo #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_next_without_prior_seek(storage: S) -> Result<(), OpProofsStorageError> { +fn test_next_without_prior_seek( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1, 2]); let branch = create_test_branch(); @@ -363,7 +383,9 @@ fn test_next_without_prior_seek(storage: S) -> Result<(), OpPr #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_next_after_seek(storage: S) -> Result<(), OpProofsStorageError> { +fn test_next_after_seek( + storage: S, +) -> Result<(), OpProofsStorageError> { let path1 = nibbles_from(vec![1]); let path2 = nibbles_from(vec![2]); let branch = create_test_branch(); @@ -385,7 +407,9 @@ fn test_next_after_seek(storage: S) -> Result<(), OpProofsStor #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_next_at_end_of_trie(storage: S) -> Result<(), OpProofsStorageError> { +fn test_next_at_end_of_trie( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1]); let branch = create_test_branch(); @@ -404,7 +428,7 @@ fn test_next_at_end_of_trie(storage: S) -> Result<(), OpProofs #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_multiple_consecutive_next( +fn test_multiple_consecutive_next( storage: S, ) -> Result<(), OpProofsStorageError> { let paths = vec![nibbles_from(vec![1]), nibbles_from(vec![2]), nibbles_from(vec![3])]; @@ -432,7 +456,9 @@ fn test_multiple_consecutive_next( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_current_after_operations(storage: S) -> Result<(), OpProofsStorageError> { +fn test_current_after_operations( + storage: S, +) -> Result<(), OpProofsStorageError> { let path1 = nibbles_from(vec![1]); let path2 = nibbles_from(vec![2]); let branch = create_test_branch(); @@ -460,7 +486,7 @@ fn test_current_after_operations(storage: S) -> Result<(), OpP #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_current_no_prior_operations( +fn test_current_no_prior_operations( storage: S, ) -> Result<(), OpProofsStorageError> { let mut cursor = storage.account_trie_cursor(100)?; @@ -479,7 +505,7 @@ fn test_current_no_prior_operations( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_same_path_different_blocks( +fn test_same_path_different_blocks( storage: S, ) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1, 2]); @@ -507,7 +533,9 @@ fn test_same_path_different_blocks( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_deleted_branch_nodes(storage: S) -> Result<(), OpProofsStorageError> { +fn test_deleted_branch_nodes( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1, 2]); let branch = create_test_branch(); let block_ref = BlockWithParent::new(B256::ZERO, NumHash::new(100, B256::repeat_byte(0x96))); @@ -542,7 +570,9 @@ fn test_deleted_branch_nodes(storage: S) -> Result<(), OpProof #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_account_specific_cursor(storage: S) -> Result<(), OpProofsStorageError> { +fn test_account_specific_cursor( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1, 2]); let addr1 = B256::repeat_byte(0x01); let addr2 = B256::repeat_byte(0x02); @@ -577,7 +607,9 @@ fn test_account_specific_cursor(storage: S) -> Result<(), OpPr #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_state_trie_cursor(storage: S) -> Result<(), OpProofsStorageError> { +fn test_state_trie_cursor( + storage: S, +) -> Result<(), OpProofsStorageError> { let path = nibbles_from(vec![1, 2]); let addr = B256::repeat_byte(0x01); let branch = create_test_branch(); @@ -607,7 +639,9 @@ fn test_state_trie_cursor(storage: S) -> Result<(), OpProofsSt #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_mixed_account_state_data(storage: S) -> Result<(), OpProofsStorageError> { +fn test_mixed_account_state_data( + storage: S, +) -> Result<(), OpProofsStorageError> { let path1 = nibbles_from(vec![1]); let path2 = nibbles_from(vec![2]); let addr = B256::repeat_byte(0x01); @@ -646,7 +680,9 @@ fn test_mixed_account_state_data(storage: S) -> Result<(), OpP #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_lexicographic_ordering(storage: S) -> Result<(), OpProofsStorageError> { +fn test_lexicographic_ordering( + storage: S, +) -> Result<(), OpProofsStorageError> { let paths = vec![ nibbles_from(vec![3, 1]), nibbles_from(vec![1, 2]), @@ -683,7 +719,9 @@ fn test_lexicographic_ordering(storage: S) -> Result<(), OpPro #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_path_prefix_scenarios(storage: S) -> Result<(), OpProofsStorageError> { +fn test_path_prefix_scenarios( + storage: S, +) -> Result<(), OpProofsStorageError> { let paths = vec![ nibbles_from(vec![1]), // Prefix of next nibbles_from(vec![1, 2]), // Extends first @@ -715,7 +753,7 @@ fn test_path_prefix_scenarios(storage: S) -> Result<(), OpProo #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_complex_nibble_combinations( +fn test_complex_nibble_combinations( storage: S, ) -> Result<(), OpProofsStorageError> { // Test various nibble patterns including edge values @@ -757,7 +795,7 @@ fn test_complex_nibble_combinations( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_store_and_retrieve_single_account( +fn test_store_and_retrieve_single_account( storage: S, ) -> Result<(), OpProofsStorageError> { let account_key = B256::repeat_byte(0x01); @@ -782,7 +820,7 @@ fn test_store_and_retrieve_single_account( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_account_cursor_navigation( +fn test_account_cursor_navigation( storage: S, ) -> Result<(), OpProofsStorageError> { let accounts = [ @@ -820,7 +858,9 @@ fn test_account_cursor_navigation( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_account_block_versioning(storage: S) -> Result<(), OpProofsStorageError> { +fn test_account_block_versioning( + storage: S, +) -> Result<(), OpProofsStorageError> { let account_key = B256::repeat_byte(0x01); let account_v1 = create_test_account_with_values(1, 100, 0xBB); let account_v2 = create_test_account_with_values(2, 200, 0xDD); @@ -849,7 +889,7 @@ fn test_account_block_versioning(storage: S) -> Result<(), OpP #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] -fn test_store_and_retrieve_storage( +fn test_store_and_retrieve_storage( storage: S, ) -> Result<(), OpProofsStorageError> { let hashed_address = B256::repeat_byte(0x01); @@ -879,7 +919,7 @@ fn test_store_and_retrieve_storage( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_storage_cursor_navigation( +fn test_storage_cursor_navigation( storage: S, ) -> Result<(), OpProofsStorageError> { let hashed_address = B256::repeat_byte(0x01); @@ -911,7 +951,7 @@ fn test_storage_cursor_navigation( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_storage_account_isolation( +fn test_storage_account_isolation( storage: S, ) -> Result<(), OpProofsStorageError> { let address1 = B256::repeat_byte(0x01); @@ -946,7 +986,9 @@ fn test_storage_account_isolation( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_storage_block_versioning(storage: S) -> Result<(), OpProofsStorageError> { +fn test_storage_block_versioning( + storage: S, +) -> Result<(), OpProofsStorageError> { let hashed_address = B256::repeat_byte(0x01); let storage_key = B256::repeat_byte(0x10); @@ -971,7 +1013,7 @@ fn test_storage_block_versioning(storage: S) -> Result<(), OpP #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_storage_zero_value_deletion( +fn test_storage_zero_value_deletion( storage: S, ) -> Result<(), OpProofsStorageError> { let hashed_address = B256::repeat_byte(0x01); @@ -1011,7 +1053,7 @@ fn test_storage_zero_value_deletion( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_storage_cursor_skips_zero_values( +fn test_storage_cursor_skips_zero_values( storage: S, ) -> Result<(), OpProofsStorageError> { let hashed_address = B256::repeat_byte(0x01); @@ -1060,7 +1102,9 @@ fn test_storage_cursor_skips_zero_values( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_empty_cursors(storage: S) -> Result<(), OpProofsStorageError> { +fn test_empty_cursors( + storage: S, +) -> Result<(), OpProofsStorageError> { // Test empty account cursor let mut account_cursor = storage.account_hashed_cursor(100)?; assert!(account_cursor.seek(B256::repeat_byte(0x01))?.is_none()); @@ -1078,7 +1122,7 @@ fn test_empty_cursors(storage: S) -> Result<(), OpProofsStorag #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_cursor_boundary_conditions( +fn test_cursor_boundary_conditions( storage: S, ) -> Result<(), OpProofsStorageError> { let account_key = B256::repeat_byte(0x80); // Middle value @@ -1107,7 +1151,9 @@ fn test_cursor_boundary_conditions( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_large_batch_operations(storage: S) -> Result<(), OpProofsStorageError> { +fn test_large_batch_operations( + storage: S, +) -> Result<(), OpProofsStorageError> { // Create large batch of accounts let mut accounts = Vec::new(); for i in 0..100 { @@ -1143,7 +1189,7 @@ fn test_large_batch_operations(storage: S) -> Result<(), OpPro #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_store_trie_updates_with_wiped_storage( +fn test_store_trie_updates_with_wiped_storage( storage: S, ) -> Result<(), OpProofsStorageError> { use reth_trie::HashedStorage; @@ -1234,7 +1280,7 @@ fn test_store_trie_updates_with_wiped_storage( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_store_trie_updates_comprehensive( +fn test_store_trie_updates_comprehensive( storage: S, ) -> Result<(), OpProofsStorageError> { use reth_trie::{updates::StorageTrieUpdates, HashedStorage}; @@ -1408,7 +1454,7 @@ fn test_store_trie_updates_comprehensive( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_replace_updates_applies_all_updates( +fn test_replace_updates_applies_all_updates( storage: S, ) -> Result<(), OpProofsStorageError> { use reth_trie::{updates::StorageTrieUpdates, HashedStorage}; @@ -1675,7 +1721,7 @@ fn test_replace_updates_applies_all_updates( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_pure_deletions_stored_correctly( +fn test_pure_deletions_stored_correctly( storage: S, ) -> Result<(), OpProofsStorageError> { use reth_trie::updates::StorageTrieUpdates; @@ -1809,7 +1855,7 @@ fn test_pure_deletions_stored_correctly( #[test_case(InMemoryProofsStorage::new(); "InMemory")] #[test_case(create_mdbx_proofs_storage(); "Mdbx")] #[serial] -fn test_updates_take_precedence_over_removals( +fn test_updates_take_precedence_over_removals( storage: S, ) -> Result<(), OpProofsStorageError> { use reth_trie::updates::StorageTrieUpdates; From 3f0fb866758e2038edc99b5179e4210bd2a55166 Mon Sep 17 00:00:00 2001 From: Arun Dhyani Date: Wed, 28 Jan 2026 16:37:13 +0530 Subject: [PATCH 2/2] fix test --- crates/optimism/trie/src/provider.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/optimism/trie/src/provider.rs b/crates/optimism/trie/src/provider.rs index 8477a3af466..c2c68b07a57 100644 --- a/crates/optimism/trie/src/provider.rs +++ b/crates/optimism/trie/src/provider.rs @@ -229,7 +229,7 @@ mod tests { assert_eq!( format!("{:?}", provider), - "OpProofsStateProviderRef { storage: InMemoryProofsStorage { inner: RwLock { data: InMemoryStorageInner { account_branches: {}, storage_branches: {}, hashed_accounts: {}, hashed_storages: {}, trie_updates: {}, post_states: {}, earliest_block: None } } }, block_number: 42 }" + "OpProofsStateProviderRef { storage: InMemoryProofsStorage { inner: RwLock { data: InMemoryStorageInner { account_branches: {}, storage_branches: {}, hashed_accounts: {}, hashed_storages: {}, trie_updates: {}, post_states: {}, earliest_block: None, anchor_block: None } } }, block_number: 42 }" ); } }