diff --git a/crates/chain-state/src/lib.rs b/crates/chain-state/src/lib.rs index 519469d67f60..ea52aab8f081 100644 --- a/crates/chain-state/src/lib.rs +++ b/crates/chain-state/src/lib.rs @@ -11,6 +11,8 @@ mod in_memory; pub use in_memory::*; +mod noop; + mod chain_info; pub use chain_info::ChainInfoTracker; diff --git a/crates/chain-state/src/noop.rs b/crates/chain-state/src/noop.rs new file mode 100644 index 000000000000..fac32b925b1e --- /dev/null +++ b/crates/chain-state/src/noop.rs @@ -0,0 +1,29 @@ +//! Noop impls for testing. + +use crate::{ + CanonStateNotifications, CanonStateSubscriptions, ForkChoiceNotifications, + ForkChoiceSubscriptions, +}; +use reth_primitives_traits::NodePrimitives; +use reth_storage_api::noop::NoopProvider; +use tokio::sync::{broadcast, watch}; + +impl CanonStateSubscriptions for NoopProvider { + fn subscribe_to_canonical_state(&self) -> CanonStateNotifications { + broadcast::channel(1).1 + } +} + +impl ForkChoiceSubscriptions for NoopProvider { + type Header = N::BlockHeader; + + fn subscribe_safe_block(&self) -> ForkChoiceNotifications { + let (_, rx) = watch::channel(None); + ForkChoiceNotifications(rx) + } + + fn subscribe_finalized_block(&self) -> ForkChoiceNotifications { + let (_, rx) = watch::channel(None); + ForkChoiceNotifications(rx) + } +} diff --git a/crates/rpc/rpc/src/eth/helpers/transaction.rs b/crates/rpc/rpc/src/eth/helpers/transaction.rs index 3fcdfb3f0681..33c8849be1b2 100644 --- a/crates/rpc/rpc/src/eth/helpers/transaction.rs +++ b/crates/rpc/rpc/src/eth/helpers/transaction.rs @@ -55,6 +55,7 @@ where #[cfg(test)] mod tests { + use super::*; use alloy_eips::eip1559::ETHEREUM_BLOCK_GAS_LIMIT; use alloy_primitives::{hex_literal::hex, Bytes}; use reth_chainspec::ChainSpecProvider; @@ -71,8 +72,6 @@ mod tests { use reth_tasks::pool::BlockingTaskPool; use reth_transaction_pool::{test_utils::testing_pool, TransactionPool}; - use super::*; - #[tokio::test] async fn send_raw_transaction() { let noop_provider = NoopProvider::default(); @@ -81,10 +80,10 @@ mod tests { let pool = testing_pool(); let evm_config = EthEvmConfig::new(noop_provider.chain_spec()); - let cache = EthStateCache::spawn(noop_provider, Default::default()); + let cache = EthStateCache::spawn(noop_provider.clone(), Default::default()); let fee_history_cache = FeeHistoryCache::new(FeeHistoryCacheConfig::default()); let eth_api = EthApi::new( - noop_provider, + noop_provider.clone(), pool.clone(), noop_network_provider, cache.clone(), diff --git a/crates/storage/provider/src/test_utils/noop.rs b/crates/storage/provider/src/test_utils/noop.rs index 5fdc37be64a9..b1ec5d47391b 100644 --- a/crates/storage/provider/src/test_utils/noop.rs +++ b/crates/storage/provider/src/test_utils/noop.rs @@ -1,575 +1,14 @@ -use std::{ - ops::{RangeBounds, RangeInclusive}, - path::PathBuf, - sync::Arc, -}; +//! Additional testing support for `NoopProvider`. -use alloy_consensus::Header; -use alloy_eips::{ - eip4895::{Withdrawal, Withdrawals}, - BlockHashOrNumber, BlockId, BlockNumberOrTag, -}; -use alloy_primitives::{ - map::{B256HashMap, HashMap}, - Address, BlockHash, BlockNumber, Bytes, StorageKey, StorageValue, TxHash, TxNumber, B256, U256, -}; -use reth_chain_state::{ - CanonStateNotifications, CanonStateSubscriptions, ForkChoiceNotifications, - ForkChoiceSubscriptions, -}; -use reth_chainspec::{ChainInfo, ChainSpec, MAINNET}; -use reth_db_api::models::{AccountBeforeTx, StoredBlockBodyIndices}; -use reth_errors::ProviderError; -use reth_evm::ConfigureEvmEnv; -use reth_primitives::{ - Account, Block, BlockWithSenders, Bytecode, EthPrimitives, Receipt, SealedBlock, - SealedBlockWithSenders, SealedHeader, TransactionMeta, TransactionSigned, -}; -use reth_prune_types::{PruneCheckpoint, PruneSegment}; -use reth_stages_types::{StageCheckpoint, StageId}; -use reth_storage_api::{ - HashedPostStateProvider, NodePrimitivesProvider, OmmersProvider, StateProofProvider, - StorageRootProvider, -}; -use reth_storage_errors::provider::ProviderResult; -use reth_trie::{ - updates::TrieUpdates, AccountProof, HashedPostState, HashedStorage, MultiProof, - MultiProofTargets, TrieInput, -}; -use revm::primitives::{BlockEnv, CfgEnvWithHandlerCfg}; -use tokio::sync::{broadcast, watch}; +use crate::{providers::StaticFileProvider, StaticFileProviderFactory}; +use reth_primitives_traits::NodePrimitives; +use std::path::PathBuf; -use crate::{ - providers::StaticFileProvider, - traits::{BlockSource, ReceiptProvider}, - AccountReader, BlockHashReader, BlockIdReader, BlockNumReader, BlockReader, BlockReaderIdExt, - ChainSpecProvider, ChangeSetReader, EvmEnvProvider, HeaderProvider, PruneCheckpointReader, - ReceiptProviderIdExt, StageCheckpointReader, StateProvider, StateProviderBox, - StateProviderFactory, StateRootProvider, StaticFileProviderFactory, TransactionVariant, - TransactionsProvider, WithdrawalsProvider, -}; +/// Re-exported for convenience +pub use reth_storage_api::noop::NoopProvider; -/// Supports various api interfaces for testing purposes. -#[derive(Debug, Clone, Default, Copy)] -#[non_exhaustive] -pub struct NoopProvider; - -impl ChainSpecProvider for NoopProvider { - type ChainSpec = ChainSpec; - - fn chain_spec(&self) -> Arc { - MAINNET.clone() - } -} - -/// Noop implementation for testing purposes -impl BlockHashReader for NoopProvider { - fn block_hash(&self, _number: u64) -> ProviderResult> { - Ok(None) - } - - fn canonical_hashes_range( - &self, - _start: BlockNumber, - _end: BlockNumber, - ) -> ProviderResult> { - Ok(vec![]) - } -} - -impl BlockNumReader for NoopProvider { - fn chain_info(&self) -> ProviderResult { - Ok(ChainInfo::default()) - } - - fn best_block_number(&self) -> ProviderResult { - Ok(0) - } - - fn last_block_number(&self) -> ProviderResult { - Ok(0) - } - - fn block_number(&self, _hash: B256) -> ProviderResult> { - Ok(None) - } -} - -impl BlockReader for NoopProvider { - type Block = Block; - - fn find_block_by_hash( - &self, - hash: B256, - _source: BlockSource, - ) -> ProviderResult> { - self.block(hash.into()) - } - - fn block(&self, _id: BlockHashOrNumber) -> ProviderResult> { - Ok(None) - } - - fn pending_block(&self) -> ProviderResult> { - Ok(None) - } - - fn pending_block_with_senders(&self) -> ProviderResult> { - Ok(None) - } - - fn pending_block_and_receipts(&self) -> ProviderResult)>> { - Ok(None) - } - - fn block_body_indices(&self, _num: u64) -> ProviderResult> { - Ok(None) - } - - fn block_with_senders( - &self, - _id: BlockHashOrNumber, - _transaction_kind: TransactionVariant, - ) -> ProviderResult> { - Ok(None) - } - - fn sealed_block_with_senders( - &self, - _id: BlockHashOrNumber, - _transaction_kind: TransactionVariant, - ) -> ProviderResult> { - Ok(None) - } - - fn block_range(&self, _range: RangeInclusive) -> ProviderResult> { - Ok(vec![]) - } - - fn block_with_senders_range( - &self, - _range: RangeInclusive, - ) -> ProviderResult> { - Ok(vec![]) - } - - fn sealed_block_with_senders_range( - &self, - _range: RangeInclusive, - ) -> ProviderResult> { - Ok(vec![]) - } -} - -impl BlockReaderIdExt for NoopProvider { - fn block_by_id(&self, _id: BlockId) -> ProviderResult> { - Ok(None) - } - - fn sealed_header_by_id(&self, _id: BlockId) -> ProviderResult> { - Ok(None) - } - - fn header_by_id(&self, _id: BlockId) -> ProviderResult> { - Ok(None) - } - - fn ommers_by_id(&self, _id: BlockId) -> ProviderResult>> { - Ok(None) - } -} - -impl BlockIdReader for NoopProvider { - fn pending_block_num_hash(&self) -> ProviderResult> { - Ok(None) - } - - fn safe_block_num_hash(&self) -> ProviderResult> { - Ok(None) - } - - fn finalized_block_num_hash(&self) -> ProviderResult> { - Ok(None) - } -} - -impl TransactionsProvider for NoopProvider { - type Transaction = TransactionSigned; - - fn transaction_id(&self, _tx_hash: TxHash) -> ProviderResult> { - Ok(None) - } - - fn transaction_by_id(&self, _id: TxNumber) -> ProviderResult> { - Ok(None) - } - - fn transaction_by_id_unhashed( - &self, - _id: TxNumber, - ) -> ProviderResult> { - Ok(None) - } - - fn transaction_by_hash(&self, _hash: TxHash) -> ProviderResult> { - Ok(None) - } - - fn transaction_by_hash_with_meta( - &self, - _hash: TxHash, - ) -> ProviderResult> { - Ok(None) - } - - fn transaction_block(&self, _id: TxNumber) -> ProviderResult> { - todo!() - } - - fn transactions_by_block( - &self, - _block_id: BlockHashOrNumber, - ) -> ProviderResult>> { - Ok(None) - } - - fn transactions_by_block_range( - &self, - _range: impl RangeBounds, - ) -> ProviderResult>> { - Ok(Vec::default()) - } - - fn transactions_by_tx_range( - &self, - _range: impl RangeBounds, - ) -> ProviderResult> { - Ok(Vec::default()) - } - - fn senders_by_tx_range( - &self, - _range: impl RangeBounds, - ) -> ProviderResult> { - Ok(Vec::default()) - } - - fn transaction_sender(&self, _id: TxNumber) -> ProviderResult> { - Ok(None) - } -} - -impl ReceiptProvider for NoopProvider { - type Receipt = Receipt; - fn receipt(&self, _id: TxNumber) -> ProviderResult> { - Ok(None) - } - - fn receipt_by_hash(&self, _hash: TxHash) -> ProviderResult> { - Ok(None) - } - - fn receipts_by_block(&self, _block: BlockHashOrNumber) -> ProviderResult>> { - Ok(None) - } - - fn receipts_by_tx_range( - &self, - _range: impl RangeBounds, - ) -> ProviderResult> { - Ok(vec![]) - } -} - -impl ReceiptProviderIdExt for NoopProvider {} - -impl HeaderProvider for NoopProvider { - type Header = Header; - - fn header(&self, _block_hash: &BlockHash) -> ProviderResult> { - Ok(None) - } - - fn header_by_number(&self, _num: u64) -> ProviderResult> { - Ok(None) - } - - fn header_td(&self, _hash: &BlockHash) -> ProviderResult> { - Ok(None) - } - - fn header_td_by_number(&self, _number: BlockNumber) -> ProviderResult> { - Ok(None) - } - - fn headers_range(&self, _range: impl RangeBounds) -> ProviderResult> { - Ok(vec![]) - } - - fn sealed_header(&self, _number: BlockNumber) -> ProviderResult> { - Ok(None) - } - - fn sealed_headers_while( - &self, - _range: impl RangeBounds, - _predicate: impl FnMut(&SealedHeader) -> bool, - ) -> ProviderResult> { - Ok(vec![]) - } -} - -impl AccountReader for NoopProvider { - fn basic_account(&self, _address: Address) -> ProviderResult> { - Ok(None) - } -} - -impl ChangeSetReader for NoopProvider { - fn account_block_changeset( - &self, - _block_number: BlockNumber, - ) -> ProviderResult> { - Ok(Vec::default()) - } -} - -impl StateRootProvider for NoopProvider { - fn state_root(&self, _state: HashedPostState) -> ProviderResult { - Ok(B256::default()) - } - - fn state_root_from_nodes(&self, _input: TrieInput) -> ProviderResult { - Ok(B256::default()) - } - - fn state_root_with_updates( - &self, - _state: HashedPostState, - ) -> ProviderResult<(B256, TrieUpdates)> { - Ok((B256::default(), TrieUpdates::default())) - } - - fn state_root_from_nodes_with_updates( - &self, - _input: TrieInput, - ) -> ProviderResult<(B256, TrieUpdates)> { - Ok((B256::default(), TrieUpdates::default())) - } -} - -impl StorageRootProvider for NoopProvider { - fn storage_root( - &self, - _address: Address, - _hashed_storage: HashedStorage, - ) -> ProviderResult { - Ok(B256::default()) - } - - fn storage_proof( - &self, - _address: Address, - slot: B256, - _hashed_storage: HashedStorage, - ) -> ProviderResult { - Ok(reth_trie::StorageProof::new(slot)) - } - - fn storage_multiproof( - &self, - _address: Address, - _slots: &[B256], - _hashed_storage: HashedStorage, - ) -> ProviderResult { - Ok(reth_trie::StorageMultiProof::empty()) - } -} - -impl StateProofProvider for NoopProvider { - fn proof( - &self, - _input: TrieInput, - address: Address, - _slots: &[B256], - ) -> ProviderResult { - Ok(AccountProof::new(address)) - } - - fn multiproof( - &self, - _input: TrieInput, - _targets: MultiProofTargets, - ) -> ProviderResult { - Ok(MultiProof::default()) - } - - fn witness( - &self, - _input: TrieInput, - _target: HashedPostState, - ) -> ProviderResult> { - Ok(HashMap::default()) - } -} - -impl HashedPostStateProvider for NoopProvider { - fn hashed_post_state(&self, _bundle_state: &revm::db::BundleState) -> HashedPostState { - HashedPostState::default() - } -} - -impl StateProvider for NoopProvider { - fn storage( - &self, - _account: Address, - _storage_key: StorageKey, - ) -> ProviderResult> { - Ok(None) - } - - fn bytecode_by_hash(&self, _code_hash: B256) -> ProviderResult> { - Ok(None) - } -} - -impl EvmEnvProvider for NoopProvider { - fn env_with_header( - &self, - header: &Header, - evm_config: EvmConfig, - ) -> ProviderResult<(CfgEnvWithHandlerCfg, BlockEnv)> - where - EvmConfig: ConfigureEvmEnv
, - { - Ok(evm_config.cfg_and_block_env(header, U256::MAX)) - } -} - -impl StateProviderFactory for NoopProvider { - fn latest(&self) -> ProviderResult { - Ok(Box::new(*self)) - } - - fn state_by_block_number_or_tag( - &self, - number_or_tag: BlockNumberOrTag, - ) -> ProviderResult { - match number_or_tag { - BlockNumberOrTag::Latest => self.latest(), - BlockNumberOrTag::Finalized => { - // we can only get the finalized state by hash, not by num - let hash = - self.finalized_block_hash()?.ok_or(ProviderError::FinalizedBlockNotFound)?; - - // only look at historical state - self.history_by_block_hash(hash) - } - BlockNumberOrTag::Safe => { - // we can only get the safe state by hash, not by num - let hash = self.safe_block_hash()?.ok_or(ProviderError::SafeBlockNotFound)?; - - self.history_by_block_hash(hash) - } - BlockNumberOrTag::Earliest => self.history_by_block_number(0), - BlockNumberOrTag::Pending => self.pending(), - BlockNumberOrTag::Number(num) => self.history_by_block_number(num), - } - } - - fn history_by_block_number(&self, _block: BlockNumber) -> ProviderResult { - Ok(Box::new(*self)) - } - - fn history_by_block_hash(&self, _block: BlockHash) -> ProviderResult { - Ok(Box::new(*self)) - } - - fn state_by_block_hash(&self, _block: BlockHash) -> ProviderResult { - Ok(Box::new(*self)) - } - - fn pending(&self) -> ProviderResult { - Ok(Box::new(*self)) - } - - fn pending_state_by_hash(&self, _block_hash: B256) -> ProviderResult> { - Ok(Some(Box::new(*self))) - } -} - -impl StageCheckpointReader for NoopProvider { - fn get_stage_checkpoint(&self, _id: StageId) -> ProviderResult> { - Ok(None) - } - - fn get_stage_checkpoint_progress(&self, _id: StageId) -> ProviderResult>> { - Ok(None) - } - - fn get_all_checkpoints(&self) -> ProviderResult> { - Ok(Vec::new()) - } -} - -impl WithdrawalsProvider for NoopProvider { - fn withdrawals_by_block( - &self, - _id: BlockHashOrNumber, - _timestamp: u64, - ) -> ProviderResult> { - Ok(None) - } - fn latest_withdrawal(&self) -> ProviderResult> { - Ok(None) - } -} - -impl OmmersProvider for NoopProvider { - fn ommers(&self, _id: BlockHashOrNumber) -> ProviderResult>> { - Ok(None) - } -} - -impl PruneCheckpointReader for NoopProvider { - fn get_prune_checkpoint( - &self, - _segment: PruneSegment, - ) -> ProviderResult> { - Ok(None) - } - - fn get_prune_checkpoints(&self) -> ProviderResult> { - Ok(Vec::new()) - } -} - -impl NodePrimitivesProvider for NoopProvider { - type Primitives = EthPrimitives; -} - -impl StaticFileProviderFactory for NoopProvider { +impl StaticFileProviderFactory for NoopProvider { fn static_file_provider(&self) -> StaticFileProvider { StaticFileProvider::read_only(PathBuf::default(), false).unwrap() } } - -impl CanonStateSubscriptions for NoopProvider { - fn subscribe_to_canonical_state(&self) -> CanonStateNotifications { - broadcast::channel(1).1 - } -} - -impl ForkChoiceSubscriptions for NoopProvider { - type Header = Header; - - fn subscribe_safe_block(&self) -> ForkChoiceNotifications { - let (_, rx) = watch::channel(None); - ForkChoiceNotifications(rx) - } - - fn subscribe_finalized_block(&self) -> ForkChoiceNotifications { - let (_, rx) = watch::channel(None); - ForkChoiceNotifications(rx) - } -}