use reth_primitives::{ keccak256, trie::AccountProof, Account, Address, BlockNumber, Bytecode, Bytes, StorageKey, B256, U256, }; use reth_storage_api::{AccountReader, BlockHashReader, StateProvider, StateRootProvider}; use reth_storage_errors::provider::ProviderResult; use reth_trie::updates::TrieUpdates; use revm::db::BundleState; use std::collections::HashMap; /// Mock state for testing #[derive(Debug, Default, Clone, Eq, PartialEq)] pub struct StateProviderTest { accounts: HashMap, Account)>, contracts: HashMap, block_hash: HashMap, } impl StateProviderTest { /// Insert account. pub fn insert_account( &mut self, address: Address, mut account: Account, bytecode: Option, storage: HashMap, ) { if let Some(bytecode) = bytecode { let hash = keccak256(&bytecode); account.bytecode_hash = Some(hash); self.contracts.insert(hash, Bytecode::new_raw(bytecode)); } self.accounts.insert(address, (storage, account)); } /// Insert a block hash. pub fn insert_block_hash(&mut self, block_number: u64, block_hash: B256) { self.block_hash.insert(block_number, block_hash); } } impl AccountReader for StateProviderTest { fn basic_account(&self, address: Address) -> ProviderResult> { Ok(self.accounts.get(&address).map(|(_, acc)| *acc)) } } impl BlockHashReader for StateProviderTest { fn block_hash(&self, number: u64) -> ProviderResult> { Ok(self.block_hash.get(&number).cloned()) } fn canonical_hashes_range( &self, start: BlockNumber, end: BlockNumber, ) -> ProviderResult> { let range = start..end; Ok(self .block_hash .iter() .filter_map(|(block, hash)| range.contains(block).then_some(*hash)) .collect()) } } impl StateRootProvider for StateProviderTest { fn state_root(&self, _bundle_state: &BundleState) -> ProviderResult { unimplemented!("state root computation is not supported") } fn state_root_with_updates( &self, _bundle_state: &BundleState, ) -> ProviderResult<(B256, TrieUpdates)> { unimplemented!("state root computation is not supported") } } impl StateProvider for StateProviderTest { fn storage( &self, account: Address, storage_key: StorageKey, ) -> ProviderResult> { Ok(self.accounts.get(&account).and_then(|(storage, _)| storage.get(&storage_key).cloned())) } fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult> { Ok(self.contracts.get(&code_hash).cloned()) } fn proof(&self, _address: Address, _keys: &[B256]) -> ProviderResult { unimplemented!("proof generation is not supported") } }