diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index b7d9fc3a27ac..0c458e8eee28 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -39,6 +39,7 @@ jobs: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@nightly with: + toolchain: nightly-2024-09-25 components: clippy - uses: Swatinem/rust-cache@v2 with: diff --git a/Cargo.lock b/Cargo.lock index 79533465f4e8..443d73c461b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2712,6 +2712,7 @@ name = "example-custom-engine-types" version = "0.0.0" dependencies = [ "alloy-genesis", + "alloy-primitives", "alloy-rpc-types", "eyre", "reth", @@ -2734,6 +2735,7 @@ name = "example-custom-evm" version = "0.0.0" dependencies = [ "alloy-genesis", + "alloy-primitives", "eyre", "reth", "reth-chainspec", @@ -2750,6 +2752,7 @@ dependencies = [ name = "example-custom-inspector" version = "0.0.0" dependencies = [ + "alloy-primitives", "alloy-rpc-types", "clap", "futures-util", @@ -2810,6 +2813,7 @@ dependencies = [ name = "example-db-access" version = "0.0.0" dependencies = [ + "alloy-primitives", "alloy-rpc-types", "eyre", "reth-chainspec", @@ -2916,6 +2920,7 @@ name = "example-stateful-precompile" version = "0.0.0" dependencies = [ "alloy-genesis", + "alloy-primitives", "eyre", "parking_lot 0.12.3", "reth", @@ -2933,6 +2938,7 @@ dependencies = [ name = "example-txpool-tracing" version = "0.0.0" dependencies = [ + "alloy-primitives", "alloy-rpc-types-trace", "clap", "futures-util", @@ -6174,6 +6180,7 @@ name = "reth" version = "1.0.7" dependencies = [ "alloy-consensus", + "alloy-primitives", "alloy-rlp", "alloy-rpc-types", "aquamarine", @@ -6255,6 +6262,7 @@ dependencies = [ "reth-execution-types", "reth-network-p2p", "reth-network-peers", + "reth-optimism-consensus", "reth-primitives", "reth-provider", "reth-revm", @@ -6609,11 +6617,11 @@ dependencies = [ name = "reth-config" version = "1.0.7" dependencies = [ + "alloy-primitives", "eyre", "humantime-serde", "reth-network-peers", "reth-network-types", - "reth-primitives", "reth-prune-types", "reth-stages-types", "serde", @@ -7283,6 +7291,7 @@ name = "reth-evm" version = "1.0.7" dependencies = [ "alloy-eips", + "alloy-primitives", "auto_impl", "futures-util", "metrics", @@ -7367,7 +7376,6 @@ version = "1.0.7" dependencies = [ "alloy-eips", "alloy-primitives", - "reth-chainspec", "reth-execution-errors", "reth-primitives", "reth-trie", @@ -8117,6 +8125,7 @@ dependencies = [ "reth-evm", "reth-evm-optimism", "reth-execution-types", + "reth-optimism-consensus", "reth-optimism-forks", "reth-payload-builder", "reth-payload-primitives", @@ -8163,6 +8172,7 @@ dependencies = [ "reth-node-api", "reth-node-builder", "reth-optimism-chainspec", + "reth-optimism-consensus", "reth-optimism-forks", "reth-primitives", "reth-provider", @@ -8262,7 +8272,6 @@ dependencies = [ "reth-codecs", "reth-ethereum-forks", "reth-optimism-chainspec", - "reth-optimism-forks", "reth-primitives-traits", "reth-static-file-types", "reth-trie-common", @@ -8402,6 +8411,7 @@ dependencies = [ name = "reth-revm" version = "1.0.7" dependencies = [ + "alloy-primitives", "reth-chainspec", "reth-consensus-common", "reth-ethereum-forks", @@ -8756,6 +8766,7 @@ dependencies = [ name = "reth-stages" version = "1.0.7" dependencies = [ + "alloy-primitives", "alloy-rlp", "assert_matches", "criterion", diff --git a/bin/reth/Cargo.toml b/bin/reth/Cargo.toml index 19d7d18f1c08..e456871facb6 100644 --- a/bin/reth/Cargo.toml +++ b/bin/reth/Cargo.toml @@ -69,6 +69,7 @@ reth-prune.workspace = true alloy-rlp.workspace = true alloy-rpc-types = { workspace = true, features = ["engine"] } alloy-consensus.workspace = true +alloy-primitives.workspace = true # tracing tracing.workspace = true diff --git a/bin/reth/src/commands/debug_cmd/build_block.rs b/bin/reth/src/commands/debug_cmd/build_block.rs index a8589a74ec8d..57a8ee8a6b8d 100644 --- a/bin/reth/src/commands/debug_cmd/build_block.rs +++ b/bin/reth/src/commands/debug_cmd/build_block.rs @@ -1,5 +1,6 @@ //! Command for debugging block building. use alloy_consensus::TxEip4844; +use alloy_primitives::Address; use alloy_rlp::Decodable; use alloy_rpc_types::engine::{BlobsBundleV1, PayloadAttributes}; use clap::Parser; @@ -24,7 +25,7 @@ use reth_node_api::{NodeTypesWithDB, NodeTypesWithEngine, PayloadBuilderAttribut use reth_node_ethereum::{EthEvmConfig, EthExecutorProvider}; use reth_payload_builder::database::CachedReads; use reth_primitives::{ - revm_primitives::KzgSettings, Address, BlobTransaction, BlobTransactionSidecar, Bytes, + revm_primitives::KzgSettings, BlobTransaction, BlobTransactionSidecar, Bytes, PooledTransactionsElement, SealedBlock, SealedBlockWithSenders, Transaction, TransactionSigned, B256, U256, }; diff --git a/bin/reth/src/commands/debug_cmd/execution.rs b/bin/reth/src/commands/debug_cmd/execution.rs index 64ca4dc2dc84..46cc6064d25e 100644 --- a/bin/reth/src/commands/debug_cmd/execution.rs +++ b/bin/reth/src/commands/debug_cmd/execution.rs @@ -1,6 +1,7 @@ //! Command for debugging execution. use crate::{args::NetworkArgs, utils::get_single_header}; +use alloy_primitives::BlockNumber; use clap::Parser; use futures::{stream::select as stream_select, StreamExt}; use reth_beacon_consensus::EthBeaconConsensus; @@ -22,7 +23,7 @@ use reth_network_api::NetworkInfo; use reth_network_p2p::{headers::client::HeadersClient, BlockClient}; use reth_node_api::{NodeTypesWithDB, NodeTypesWithDBAdapter, NodeTypesWithEngine}; use reth_node_ethereum::EthExecutorProvider; -use reth_primitives::{BlockHashOrNumber, BlockNumber, B256}; +use reth_primitives::{BlockHashOrNumber, B256}; use reth_provider::{ BlockExecutionWriter, ChainSpecProvider, ProviderFactory, StageCheckpointReader, }; diff --git a/crates/blockchain-tree/Cargo.toml b/crates/blockchain-tree/Cargo.toml index bc7b340baaba..d0718c97b8cc 100644 --- a/crates/blockchain-tree/Cargo.toml +++ b/crates/blockchain-tree/Cargo.toml @@ -25,7 +25,7 @@ reth-execution-types.workspace = true reth-stages-api.workspace = true reth-trie = { workspace = true, features = ["metrics"] } reth-trie-db = { workspace = true, features = ["metrics"] } -reth-trie-parallel = { workspace = true, features = ["parallel"] } +reth-trie-parallel.workspace = true reth-network.workspace = true reth-consensus.workspace = true reth-node-types.workspace = true diff --git a/crates/blockchain-tree/src/metrics.rs b/crates/blockchain-tree/src/metrics.rs index 5d44a6391178..121d0a69786f 100644 --- a/crates/blockchain-tree/src/metrics.rs +++ b/crates/blockchain-tree/src/metrics.rs @@ -5,26 +5,6 @@ use reth_metrics::{ }; use std::time::{Duration, Instant}; -/// Metrics for the entire blockchain tree -#[derive(Metrics)] -#[metrics(scope = "blockchain_tree")] -pub struct TreeMetrics { - /// Total number of sidechains (not including the canonical chain) - pub sidechains: Gauge, - /// The highest block number in the canonical chain - pub canonical_chain_height: Gauge, - /// The number of reorgs - pub reorgs: Counter, - /// The latest reorg depth - pub latest_reorg_depth: Gauge, - /// Longest sidechain height - pub longest_sidechain_height: Gauge, - /// The number of times cached trie updates were used for insert. - pub trie_updates_insert_cached: Counter, - /// The number of times trie updates were recomputed for insert. - pub trie_updates_insert_recomputed: Counter, -} - /// Metrics for the blockchain tree block buffer #[derive(Metrics)] #[metrics(scope = "blockchain_tree.block_buffer")] @@ -65,6 +45,26 @@ impl MakeCanonicalDurationsRecorder { } } +/// Metrics for the entire blockchain tree +#[derive(Metrics)] +#[metrics(scope = "blockchain_tree")] +pub struct TreeMetrics { + /// Total number of sidechains (not including the canonical chain) + pub sidechains: Gauge, + /// The highest block number in the canonical chain + pub canonical_chain_height: Gauge, + /// The number of reorgs + pub reorgs: Counter, + /// The latest reorg depth + pub latest_reorg_depth: Gauge, + /// Longest sidechain height + pub longest_sidechain_height: Gauge, + /// The number of times cached trie updates were used for insert. + pub trie_updates_insert_cached: Counter, + /// The number of times trie updates were recomputed for insert. + pub trie_updates_insert_recomputed: Counter, +} + /// Represents actions for making a canonical chain. #[derive(Debug, Copy, Clone)] pub(crate) enum MakeCanonicalAction { diff --git a/crates/chainspec/src/api.rs b/crates/chainspec/src/api.rs index 15a3a024494c..fb9744a53164 100644 --- a/crates/chainspec/src/api.rs +++ b/crates/chainspec/src/api.rs @@ -8,7 +8,7 @@ use reth_primitives_traits::Header; /// Trait representing type configuring a chain spec. #[auto_impl::auto_impl(&, Arc)] -pub trait EthChainSpec: Send + Sync + Unpin + Debug + 'static { +pub trait EthChainSpec: Send + Sync + Unpin + Debug { // todo: make chain spec type generic over hardfork //type Hardfork: Clone + Copy + 'static; diff --git a/crates/chainspec/src/spec.rs b/crates/chainspec/src/spec.rs index 463501ee4df6..c62ed4f67223 100644 --- a/crates/chainspec/src/spec.rs +++ b/crates/chainspec/src/spec.rs @@ -411,10 +411,7 @@ impl ChainSpec { /// Returns the hardfork display helper. pub fn display_hardforks(&self) -> DisplayHardforks { - DisplayHardforks::new( - &self.hardforks, - self.paris_block_and_final_difficulty.map(|(block, _)| block), - ) + DisplayHardforks::new(&self, self.paris_block_and_final_difficulty.map(|(block, _)| block)) } /// Get the fork id for the given hardfork. @@ -613,6 +610,18 @@ impl Hardforks for ChainSpec { fn forks_iter(&self) -> impl Iterator { self.hardforks.forks_iter() } + + fn fork_id(&self, head: &Head) -> ForkId { + self.fork_id(head) + } + + fn latest_fork_id(&self) -> ForkId { + self.latest_fork_id() + } + + fn fork_filter(&self, head: Head) -> ForkFilter { + self.fork_filter(head) + } } impl EthereumHardforks for ChainSpec { @@ -820,7 +829,7 @@ fn into_optimism_chain_spec(genesis: Genesis) -> ChainSpec { #[auto_impl::auto_impl(&, Arc)] pub trait ChainSpecProvider: Send + Sync { /// The chain spec type. - type ChainSpec: EthChainSpec; + type ChainSpec: EthChainSpec + 'static; /// Get an [`Arc`] to the [`ChainSpec`]. fn chain_spec(&self) -> Arc; diff --git a/crates/cli/commands/src/p2p/mod.rs b/crates/cli/commands/src/p2p/mod.rs index e8eadddbd906..6d40e414dd33 100644 --- a/crates/cli/commands/src/p2p/mod.rs +++ b/crates/cli/commands/src/p2p/mod.rs @@ -100,13 +100,12 @@ impl> Command { let net = NetworkConfigBuilder::new(p2p_secret_key) .peer_config(config.peers_config_with_basic_nodes_from_file(None)) .external_ip_resolver(self.network.nat) - .chain_spec(self.chain.clone()) .disable_discv4_discovery_if(self.chain.chain.is_optimism()) .boot_nodes(boot_nodes.clone()) .apply(|builder| { self.network.discovery.apply_to_builder(builder, rlpx_socket, boot_nodes) }) - .build_with_noop_provider() + .build_with_noop_provider(self.chain) .manager() .await?; let network = net.handle().clone(); diff --git a/crates/config/Cargo.toml b/crates/config/Cargo.toml index d8224a3d62ed..a186b8407a8a 100644 --- a/crates/config/Cargo.toml +++ b/crates/config/Cargo.toml @@ -27,4 +27,4 @@ eyre.workspace = true [dev-dependencies] tempfile.workspace = true reth-network-peers.workspace = true -reth-primitives.workspace = true +alloy-primitives.workspace = true diff --git a/crates/config/src/config.rs b/crates/config/src/config.rs index 24c992fddd82..e4a7fc9677aa 100644 --- a/crates/config/src/config.rs +++ b/crates/config/src/config.rs @@ -444,8 +444,8 @@ where mod tests { use super::{Config, EXTENSION}; use crate::PruneConfig; + use alloy_primitives::Address; use reth_network_peers::TrustedPeer; - use reth_primitives::Address; use reth_prune_types::{PruneMode, PruneModes, ReceiptsLogPruneConfig}; use std::{collections::BTreeMap, path::Path, str::FromStr, time::Duration}; diff --git a/crates/consensus/auto-seal/Cargo.toml b/crates/consensus/auto-seal/Cargo.toml index d712071a1927..b4b281230336 100644 --- a/crates/consensus/auto-seal/Cargo.toml +++ b/crates/consensus/auto-seal/Cargo.toml @@ -35,6 +35,9 @@ alloy-primitives.workspace = true revm-primitives.workspace = true alloy-rpc-types-engine.workspace = true +# optimism +reth-optimism-consensus = { workspace = true, optional = true } + # async futures-util.workspace = true tokio = { workspace = true, features = ["sync", "time"] } @@ -42,4 +45,4 @@ tokio-stream.workspace = true tracing.workspace = true [features] -optimism = ["reth-provider/optimism"] +optimism = ["reth-provider/optimism", "reth-optimism-consensus"] diff --git a/crates/consensus/auto-seal/src/lib.rs b/crates/consensus/auto-seal/src/lib.rs index fea15f84d510..db418285cf3e 100644 --- a/crates/consensus/auto-seal/src/lib.rs +++ b/crates/consensus/auto-seal/src/lib.rs @@ -419,7 +419,13 @@ impl StorageInner { header.receipts_root = { #[cfg(feature = "optimism")] let receipts_root = execution_outcome - .optimism_receipts_root_slow(header.number, &chain_spec, header.timestamp) + .generic_receipts_root_slow(header.number, |receipts| { + reth_optimism_consensus::calculate_receipt_root_no_memo_optimism( + receipts, + &chain_spec, + header.timestamp, + ) + }) .expect("Receipts is present"); #[cfg(not(feature = "optimism"))] diff --git a/crates/consensus/auto-seal/src/task.rs b/crates/consensus/auto-seal/src/task.rs index 8979428bed7c..e4873615f1d0 100644 --- a/crates/consensus/auto-seal/src/task.rs +++ b/crates/consensus/auto-seal/src/task.rs @@ -5,7 +5,6 @@ use reth_beacon_consensus::{BeaconEngineMessage, ForkchoiceStatus}; use reth_chainspec::{EthChainSpec, EthereumHardforks}; use reth_engine_primitives::EngineTypes; use reth_evm::execute::BlockExecutorProvider; -use reth_primitives::IntoRecoveredTransaction; use reth_provider::{CanonChainTracker, StateProviderFactory}; use reth_stages_api::PipelineEvent; use reth_tokio_util::EventStream; @@ -87,7 +86,7 @@ where Pool: TransactionPool + Unpin + 'static, Engine: EngineTypes, Executor: BlockExecutorProvider, - ChainSpec: EthChainSpec + EthereumHardforks, + ChainSpec: EthChainSpec + EthereumHardforks + 'static, { type Output = (); diff --git a/crates/engine/tree/src/tree/metrics.rs b/crates/engine/tree/src/tree/metrics.rs index 2df1fbdac700..922041ae7151 100644 --- a/crates/engine/tree/src/tree/metrics.rs +++ b/crates/engine/tree/src/tree/metrics.rs @@ -1,3 +1,4 @@ +use reth_blockchain_tree::metrics::TreeMetrics; use reth_evm::metrics::ExecutorMetrics; use reth_metrics::{ metrics::{Counter, Gauge, Histogram}, @@ -13,6 +14,8 @@ pub(crate) struct EngineApiMetrics { pub(crate) executor: ExecutorMetrics, /// Metrics for block validation pub(crate) block_validation: BlockValidationMetrics, + /// A copy of legacy blockchain tree metrics, to be replaced when we replace the old tree + pub(crate) tree: TreeMetrics, } /// Metrics for the `EngineApi`. diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index 3dd736cdc10f..30c72254552d 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -40,7 +40,7 @@ use reth_provider::{ use reth_revm::database::StateProviderDatabase; use reth_stages_api::ControlFlow; use reth_trie::{updates::TrieUpdates, HashedPostState, TrieInput}; -use reth_trie_parallel::async_root::{AsyncStateRoot, AsyncStateRootError}; +use reth_trie_parallel::parallel_root::{ParallelStateRoot, ParallelStateRootError}; use std::{ cmp::Ordering, collections::{btree_map, hash_map, BTreeMap, HashMap, HashSet, VecDeque}, @@ -1214,7 +1214,7 @@ where if let Err(err) = tx.send(output.map(|o| o.outcome).map_err(Into::into)) { - error!("Failed to send event: {err:?}"); + error!(target: "engine::tree", "Failed to send event: {err:?}"); } } BeaconEngineMessage::NewPayload { payload, cancun_fields, tx } => { @@ -1224,7 +1224,7 @@ where Box::new(e), ) })) { - error!("Failed to send event: {err:?}"); + error!(target: "engine::tree", "Failed to send event: {err:?}"); } } BeaconEngineMessage::TransitionConfigurationExchanged => { @@ -1291,6 +1291,7 @@ where backfill_num_hash, ); self.metrics.engine.executed_blocks.set(self.state.tree_state.block_count() as f64); + self.metrics.tree.canonical_chain_height.set(backfill_height as f64); // remove all buffered blocks below the backfill height self.state.buffer.remove_old_blocks(backfill_height); @@ -1410,10 +1411,9 @@ where debug!(target: "engine::tree", "emitting backfill action event"); } - let _ = self - .outgoing - .send(event) - .inspect_err(|err| error!("Failed to send internal event: {err:?}")); + let _ = self.outgoing.send(event).inspect_err( + |err| error!(target: "engine::tree", "Failed to send internal event: {err:?}"), + ); } /// Returns true if the canonical chain length minus the last persisted @@ -1700,6 +1700,7 @@ where fn validate_block(&self, block: &SealedBlockWithSenders) -> Result<(), ConsensusError> { if let Err(e) = self.consensus.validate_header_with_total_difficulty(block, U256::MAX) { error!( + target: "engine::tree", ?block, "Failed to validate total difficulty for block {}: {e}", block.header.hash() @@ -1708,12 +1709,12 @@ where } if let Err(e) = self.consensus.validate_header(block) { - error!(?block, "Failed to validate header {}: {e}", block.header.hash()); + error!(target: "engine::tree", ?block, "Failed to validate header {}: {e}", block.header.hash()); return Err(e) } if let Err(e) = self.consensus.validate_block_pre_execution(block) { - error!(?block, "Failed to validate block {}: {e}", block.header.hash()); + error!(target: "engine::tree", ?block, "Failed to validate block {}: {e}", block.header.hash()); return Err(e) } @@ -1956,6 +1957,9 @@ where self.canonical_in_memory_state.update_chain(chain_update); self.canonical_in_memory_state.set_canonical_head(tip.clone()); + // Update metrics based on new tip + self.metrics.tree.canonical_chain_height.set(tip.number as f64); + // sends an event to all active listeners about the new canonical chain self.canonical_in_memory_state.notify_canon_state(notification); @@ -2144,7 +2148,7 @@ where )) })?; if let Err(e) = self.consensus.validate_header_against_parent(&block, &parent_block) { - warn!(?block, "Failed to validate header {} against parent: {e}", block.header.hash()); + warn!(target: "engine::tree", ?block, "Failed to validate header {} against parent: {e}", block.header.hash()); return Err(e.into()) } @@ -2191,11 +2195,11 @@ where let persistence_in_progress = self.persistence_state.in_progress(); if !persistence_in_progress { state_root_result = match self - .compute_state_root_async(block.parent_hash, &hashed_state) + .compute_state_root_parallel(block.parent_hash, &hashed_state) { Ok((state_root, trie_output)) => Some((state_root, trie_output)), - Err(AsyncStateRootError::Provider(ProviderError::ConsistentView(error))) => { - debug!(target: "engine", %error, "Async state root computation failed consistency check, falling back"); + Err(ParallelStateRootError::Provider(ProviderError::ConsistentView(error))) => { + debug!(target: "engine", %error, "Parallel state root computation failed consistency check, falling back"); None } Err(error) => return Err(InsertBlockErrorKindTwo::Other(Box::new(error))), @@ -2261,20 +2265,19 @@ where Ok(InsertPayloadOk2::Inserted(BlockStatus2::Valid)) } - /// Compute state root for the given hashed post state asynchronously. + /// Compute state root for the given hashed post state in parallel. /// /// # Returns /// /// Returns `Ok(_)` if computed successfully. /// Returns `Err(_)` if error was encountered during computation. /// `Err(ProviderError::ConsistentView(_))` can be safely ignored and fallback computation - /// should be used instead. - fn compute_state_root_async( + fn compute_state_root_parallel( &self, parent_hash: B256, hashed_state: &HashedPostState, - ) -> Result<(B256, TrieUpdates), AsyncStateRootError> { + ) -> Result<(B256, TrieUpdates), ParallelStateRootError> { let consistent_view = ConsistentDbView::new_with_latest_tip(self.provider.clone())?; let mut input = TrieInput::default(); @@ -2296,7 +2299,7 @@ where // Extend with block we are validating root for. input.append_ref(hashed_state); - AsyncStateRoot::new(consistent_view, input).incremental_root_with_updates() + ParallelStateRoot::new(consistent_view, input).incremental_root_with_updates() } /// Handles an error that occurred while inserting a block. diff --git a/crates/ethereum-forks/src/hardforks/mod.rs b/crates/ethereum-forks/src/hardforks/mod.rs index 11851c738962..78db5464cb2b 100644 --- a/crates/ethereum-forks/src/hardforks/mod.rs +++ b/crates/ethereum-forks/src/hardforks/mod.rs @@ -2,7 +2,7 @@ mod ethereum; pub use ethereum::EthereumHardforks; -use crate::{ForkCondition, Hardfork}; +use crate::{ForkCondition, ForkFilter, ForkId, Hardfork, Head}; #[cfg(feature = "std")] use rustc_hash::FxHashMap; #[cfg(feature = "std")] @@ -31,6 +31,15 @@ pub trait Hardforks: Clone { fn is_fork_active_at_block(&self, fork: H, block_number: u64) -> bool { self.fork(fork).active_at_block(block_number) } + + /// Compute the [`ForkId`] for the given [`Head`] following eip-6122 spec + fn fork_id(&self, head: &Head) -> ForkId; + + /// Returns the [`ForkId`] for the last fork. + fn latest_fork_id(&self) -> ForkId; + + /// Creates a [`ForkFilter`] for the block described by [Head]. + fn fork_filter(&self, head: Head) -> ForkFilter; } /// Ordered list of a chain hardforks that implement [`Hardfork`]. @@ -129,16 +138,6 @@ impl ChainHardforks { } } -impl Hardforks for ChainHardforks { - fn fork(&self, fork: H) -> ForkCondition { - self.fork(fork) - } - - fn forks_iter(&self) -> impl Iterator { - self.forks_iter() - } -} - impl core::fmt::Debug for ChainHardforks { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.debug_struct("ChainHardforks") diff --git a/crates/ethereum/payload/src/lib.rs b/crates/ethereum/payload/src/lib.rs index ecce558f1a6f..2593e8336611 100644 --- a/crates/ethereum/payload/src/lib.rs +++ b/crates/ethereum/payload/src/lib.rs @@ -33,8 +33,7 @@ use reth_primitives::{ constants::{eip4844::MAX_DATA_GAS_PER_BLOCK, BEACON_NONCE}, proofs::{self, calculate_requests_root}, revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg}, - Block, BlockBody, EthereumHardforks, Header, IntoRecoveredTransaction, Receipt, - EMPTY_OMMER_ROOT_HASH, + Block, BlockBody, EthereumHardforks, Header, Receipt, EMPTY_OMMER_ROOT_HASH, }; use reth_provider::{ChainSpecProvider, StateProviderFactory}; use reth_revm::database::StateProviderDatabase; diff --git a/crates/evm/Cargo.toml b/crates/evm/Cargo.toml index f520c75eeaa9..20070d421e97 100644 --- a/crates/evm/Cargo.toml +++ b/crates/evm/Cargo.toml @@ -23,7 +23,11 @@ reth-storage-errors.workspace = true reth-execution-types.workspace = true revm.workspace = true + +# alloy +alloy-primitives.workspace = true alloy-eips.workspace = true + auto_impl.workspace = true futures-util.workspace = true metrics = { workspace = true, optional = true } diff --git a/crates/evm/execution-types/Cargo.toml b/crates/evm/execution-types/Cargo.toml index cf50b47a03b2..65426e3083b4 100644 --- a/crates/evm/execution-types/Cargo.toml +++ b/crates/evm/execution-types/Cargo.toml @@ -12,21 +12,23 @@ workspace = true [dependencies] reth-primitives.workspace = true -reth-chainspec = { workspace = true, optional = true } reth-execution-errors.workspace = true reth-trie.workspace = true revm.workspace = true +# alloy +alloy-primitives.workspace = true +alloy-eips.workspace = true + serde = { workspace = true, optional = true } [dev-dependencies] reth-primitives = { workspace = true, features = ["test-utils"] } -alloy-primitives.workspace = true alloy-eips.workspace = true [features] default = ["std"] -optimism = ["dep:reth-chainspec"] +optimism = [] serde = ["dep:serde", "reth-trie/serde", "revm/serde"] std = [] diff --git a/crates/evm/execution-types/src/chain.rs b/crates/evm/execution-types/src/chain.rs index 4d0dc694a05f..588bc832e688 100644 --- a/crates/evm/execution-types/src/chain.rs +++ b/crates/evm/execution-types/src/chain.rs @@ -2,11 +2,13 @@ use crate::ExecutionOutcome; use alloc::{borrow::Cow, collections::BTreeMap}; +use alloy_eips::{eip1898::ForkBlock, BlockNumHash}; +use alloy_primitives::{Address, BlockHash, BlockNumber, TxHash}; use core::{fmt, ops::RangeInclusive}; use reth_execution_errors::{BlockExecutionError, InternalBlockExecutionError}; use reth_primitives::{ - Address, BlockHash, BlockNumHash, BlockNumber, ForkBlock, Receipt, SealedBlock, - SealedBlockWithSenders, SealedHeader, TransactionSigned, TransactionSignedEcRecovered, TxHash, + Receipt, SealedBlock, SealedBlockWithSenders, SealedHeader, TransactionSigned, + TransactionSignedEcRecovered, }; use reth_trie::updates::TrieUpdates; use revm::db::BundleState; @@ -508,7 +510,8 @@ pub enum ChainSplit { #[cfg(test)] mod tests { use super::*; - use reth_primitives::{Receipt, Receipts, TxType, B256}; + use alloy_primitives::B256; + use reth_primitives::{Receipt, Receipts, TxType}; use revm::primitives::{AccountInfo, HashMap}; #[test] diff --git a/crates/evm/execution-types/src/execute.rs b/crates/evm/execution-types/src/execute.rs index 2933fd59815f..0cf5d7050793 100644 --- a/crates/evm/execution-types/src/execute.rs +++ b/crates/evm/execution-types/src/execute.rs @@ -1,4 +1,5 @@ -use reth_primitives::{Request, U256}; +use alloy_primitives::U256; +use reth_primitives::Request; use revm::db::BundleState; /// A helper type for ethereum block inputs that consists of a block and the total difficulty. diff --git a/crates/evm/execution-types/src/execution_outcome.rs b/crates/evm/execution-types/src/execution_outcome.rs index 8996ac9959e3..3b21f36dd13c 100644 --- a/crates/evm/execution-types/src/execution_outcome.rs +++ b/crates/evm/execution-types/src/execution_outcome.rs @@ -1,8 +1,6 @@ use crate::BlockExecutionOutput; -use reth_primitives::{ - logs_bloom, Account, Address, BlockNumber, Bloom, Bytecode, Log, Receipt, Receipts, Requests, - StorageEntry, B256, U256, -}; +use alloy_primitives::{Address, BlockNumber, Bloom, Log, B256, U256}; +use reth_primitives::{logs_bloom, Account, Bytecode, Receipt, Receipts, Requests, StorageEntry}; use reth_trie::HashedPostState; use revm::{ db::{states::BundleState, BundleAccount}, @@ -198,24 +196,21 @@ impl ExecutionOutcome { #[cfg(feature = "optimism")] panic!("This should not be called in optimism mode. Use `optimism_receipts_root_slow` instead."); #[cfg(not(feature = "optimism"))] - self.receipts.root_slow(self.block_number_to_index(_block_number)?) + self.receipts.root_slow( + self.block_number_to_index(_block_number)?, + reth_primitives::proofs::calculate_receipt_root_no_memo, + ) } /// Returns the receipt root for all recorded receipts. /// Note: this function calculated Bloom filters for every receipt and created merkle trees /// of receipt. This is a expensive operation. - #[cfg(feature = "optimism")] - pub fn optimism_receipts_root_slow( + pub fn generic_receipts_root_slow( &self, block_number: BlockNumber, - chain_spec: impl reth_chainspec::Hardforks, - timestamp: u64, + f: impl FnOnce(&[&Receipt]) -> B256, ) -> Option { - self.receipts.optimism_root_slow( - self.block_number_to_index(block_number)?, - chain_spec, - timestamp, - ) + self.receipts.root_slow(self.block_number_to_index(block_number)?, f) } /// Returns reference to receipts. @@ -371,8 +366,8 @@ impl From<(BlockExecutionOutput, BlockNumber)> for ExecutionOutcome { mod tests { use super::*; use alloy_eips::{eip6110::DepositRequest, eip7002::WithdrawalRequest}; - use alloy_primitives::{FixedBytes, LogData}; - use reth_primitives::{Address, Receipts, Request, Requests, TxType, B256}; + use alloy_primitives::{Address, FixedBytes, LogData, B256}; + use reth_primitives::{Receipts, Request, Requests, TxType}; use std::collections::HashMap; #[test] diff --git a/crates/evm/src/either.rs b/crates/evm/src/either.rs index fde316da9f5b..a3fca50ec7ee 100644 --- a/crates/evm/src/either.rs +++ b/crates/evm/src/either.rs @@ -3,9 +3,10 @@ use core::fmt::Display; use crate::execute::{BatchExecutor, BlockExecutorProvider, Executor}; +use alloy_primitives::BlockNumber; use reth_execution_errors::BlockExecutionError; use reth_execution_types::{BlockExecutionInput, BlockExecutionOutput, ExecutionOutcome}; -use reth_primitives::{BlockNumber, BlockWithSenders, Receipt}; +use reth_primitives::{BlockWithSenders, Receipt}; use reth_prune_types::PruneModes; use reth_storage_errors::provider::ProviderError; use revm_primitives::db::Database; diff --git a/crates/evm/src/execute.rs b/crates/evm/src/execute.rs index 60a29d4de9fc..ffc08469dc8d 100644 --- a/crates/evm/src/execute.rs +++ b/crates/evm/src/execute.rs @@ -5,8 +5,9 @@ pub use reth_execution_errors::{BlockExecutionError, BlockValidationError}; pub use reth_execution_types::{BlockExecutionInput, BlockExecutionOutput, ExecutionOutcome}; pub use reth_storage_errors::provider::ProviderError; +use alloy_primitives::BlockNumber; use core::fmt::Display; -use reth_primitives::{BlockNumber, BlockWithSenders, Receipt}; +use reth_primitives::{BlockWithSenders, Receipt}; use reth_prune_types::PruneModes; use revm::State; use revm_primitives::db::Database; @@ -151,8 +152,8 @@ pub trait BlockExecutorProvider: Send + Sync + Clone + Unpin + 'static { #[cfg(test)] mod tests { use super::*; + use alloy_primitives::U256; use revm::db::{CacheDB, EmptyDBTyped}; - use revm_primitives::U256; use std::marker::PhantomData; #[derive(Clone, Default)] diff --git a/crates/evm/src/lib.rs b/crates/evm/src/lib.rs index 569491a1b02c..6fcb3d9f8c3d 100644 --- a/crates/evm/src/lib.rs +++ b/crates/evm/src/lib.rs @@ -14,12 +14,11 @@ extern crate alloc; use core::ops::Deref; use crate::builder::RethEvmBuilder; -use reth_primitives::{Address, TransactionSigned, TransactionSignedEcRecovered, B256, U256}; +use alloy_primitives::{Address, Bytes, B256, U256}; +use reth_primitives::{TransactionSigned, TransactionSignedEcRecovered}; use reth_primitives_traits::BlockHeader; use revm::{Database, Evm, GetInspector}; -use revm_primitives::{ - BlockEnv, Bytes, CfgEnvWithHandlerCfg, Env, EnvWithHandlerCfg, SpecId, TxEnv, -}; +use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, Env, EnvWithHandlerCfg, SpecId, TxEnv}; pub mod builder; pub mod either; diff --git a/crates/evm/src/noop.rs b/crates/evm/src/noop.rs index ae6171a506a9..392bfd0bd722 100644 --- a/crates/evm/src/noop.rs +++ b/crates/evm/src/noop.rs @@ -1,9 +1,10 @@ //! A no operation block executor implementation. +use alloy_primitives::BlockNumber; use core::fmt::Display; use reth_execution_errors::BlockExecutionError; use reth_execution_types::{BlockExecutionInput, BlockExecutionOutput, ExecutionOutcome}; -use reth_primitives::{BlockNumber, BlockWithSenders, Receipt}; +use reth_primitives::{BlockWithSenders, Receipt}; use reth_prune_types::PruneModes; use reth_storage_errors::provider::ProviderError; use revm::State; diff --git a/crates/evm/src/provider.rs b/crates/evm/src/provider.rs index fc3d4ff94f79..8db828ec4a00 100644 --- a/crates/evm/src/provider.rs +++ b/crates/evm/src/provider.rs @@ -1,7 +1,8 @@ //! Provider trait for populating the EVM environment. use crate::ConfigureEvmEnv; -use reth_primitives::{BlockHashOrNumber, Header}; +use alloy_eips::eip1898::BlockHashOrNumber; +use reth_primitives::Header; use reth_storage_errors::provider::ProviderResult; use revm::primitives::{BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, SpecId}; diff --git a/crates/evm/src/system_calls/eip2935.rs b/crates/evm/src/system_calls/eip2935.rs index 4a1ec14a467e..7b09a4813079 100644 --- a/crates/evm/src/system_calls/eip2935.rs +++ b/crates/evm/src/system_calls/eip2935.rs @@ -4,12 +4,13 @@ use alloc::{boxed::Box, string::ToString}; use alloy_eips::eip2935::HISTORY_STORAGE_ADDRESS; use crate::ConfigureEvm; +use alloy_primitives::B256; use core::fmt::Display; use reth_chainspec::EthereumHardforks; use reth_execution_errors::{BlockExecutionError, BlockValidationError}; use reth_primitives::Header; use revm::{interpreter::Host, Database, DatabaseCommit, Evm}; -use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState, B256}; +use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState}; /// Apply the [EIP-2935](https://eips.ethereum.org/EIPS/eip-2935) pre block contract call. /// diff --git a/crates/evm/src/system_calls/eip4788.rs b/crates/evm/src/system_calls/eip4788.rs index d1148f6cdc74..a1f97bf5e908 100644 --- a/crates/evm/src/system_calls/eip4788.rs +++ b/crates/evm/src/system_calls/eip4788.rs @@ -3,11 +3,12 @@ use alloc::{boxed::Box, string::ToString}; use crate::ConfigureEvm; use alloy_eips::eip4788::BEACON_ROOTS_ADDRESS; +use alloy_primitives::B256; use reth_chainspec::EthereumHardforks; use reth_execution_errors::{BlockExecutionError, BlockValidationError}; use reth_primitives::Header; use revm::{interpreter::Host, Database, DatabaseCommit, Evm}; -use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState, B256}; +use revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ResultAndState}; /// Apply the [EIP-4788](https://eips.ethereum.org/EIPS/eip-4788) pre block contract call. /// diff --git a/crates/evm/src/system_calls/eip7002.rs b/crates/evm/src/system_calls/eip7002.rs index 9b770cfceb55..d03268254f43 100644 --- a/crates/evm/src/system_calls/eip7002.rs +++ b/crates/evm/src/system_calls/eip7002.rs @@ -4,12 +4,12 @@ use core::fmt::Display; use crate::ConfigureEvm; use alloy_eips::eip7002::{WithdrawalRequest, WITHDRAWAL_REQUEST_PREDEPLOY_ADDRESS}; +use alloy_primitives::{Address, Bytes, FixedBytes}; use reth_execution_errors::{BlockExecutionError, BlockValidationError}; use reth_primitives::{Buf, Header, Request}; use revm::{interpreter::Host, Database, DatabaseCommit, Evm}; use revm_primitives::{ - Address, BlockEnv, Bytes, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ExecutionResult, FixedBytes, - ResultAndState, + BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ExecutionResult, ResultAndState, }; /// Apply the [EIP-7002](https://eips.ethereum.org/EIPS/eip-7002) post block contract call. diff --git a/crates/evm/src/system_calls/eip7251.rs b/crates/evm/src/system_calls/eip7251.rs index 8a7049671028..8247f06b1841 100644 --- a/crates/evm/src/system_calls/eip7251.rs +++ b/crates/evm/src/system_calls/eip7251.rs @@ -4,12 +4,12 @@ use core::fmt::Display; use crate::ConfigureEvm; use alloy_eips::eip7251::{ConsolidationRequest, CONSOLIDATION_REQUEST_PREDEPLOY_ADDRESS}; +use alloy_primitives::{Address, Bytes, FixedBytes}; use reth_execution_errors::{BlockExecutionError, BlockValidationError}; use reth_primitives::{Buf, Header, Request}; use revm::{interpreter::Host, Database, DatabaseCommit, Evm}; use revm_primitives::{ - Address, BlockEnv, Bytes, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ExecutionResult, FixedBytes, - ResultAndState, + BlockEnv, CfgEnvWithHandlerCfg, EnvWithHandlerCfg, ExecutionResult, ResultAndState, }; /// Apply the [EIP-7251](https://eips.ethereum.org/EIPS/eip-7251) post block contract call. diff --git a/crates/evm/src/test_utils.rs b/crates/evm/src/test_utils.rs index f30262c281e0..cf45930aece9 100644 --- a/crates/evm/src/test_utils.rs +++ b/crates/evm/src/test_utils.rs @@ -3,10 +3,11 @@ use crate::execute::{ BatchExecutor, BlockExecutionInput, BlockExecutionOutput, BlockExecutorProvider, Executor, }; +use alloy_primitives::BlockNumber; use parking_lot::Mutex; use reth_execution_errors::BlockExecutionError; use reth_execution_types::ExecutionOutcome; -use reth_primitives::{BlockNumber, BlockWithSenders, Receipt}; +use reth_primitives::{BlockWithSenders, Receipt}; use reth_prune_types::PruneModes; use reth_storage_errors::provider::ProviderError; use revm::State; diff --git a/crates/net/discv5/src/network_stack_id.rs b/crates/net/discv5/src/network_stack_id.rs index 9943b38c87b6..f707c7de7b7f 100644 --- a/crates/net/discv5/src/network_stack_id.rs +++ b/crates/net/discv5/src/network_stack_id.rs @@ -1,7 +1,7 @@ //! Keys of ENR [`ForkId`](reth_ethereum_forks::ForkId) kv-pair. Identifies which network stack a //! node belongs to. -use reth_chainspec::ChainSpec; +use reth_chainspec::EthChainSpec; /// Identifies which Ethereum network stack a node belongs to, on the discovery network. #[derive(Debug)] @@ -21,11 +21,11 @@ impl NetworkStackId { pub const OPSTACK: &'static [u8] = b"opstack"; #[allow(clippy::missing_const_for_fn)] - /// Returns the [`NetworkStackId`] that matches the given [`ChainSpec`]. - pub fn id(chain: &ChainSpec) -> Option<&'static [u8]> { - if chain.is_optimism() { + /// Returns the [`NetworkStackId`] that matches the given chain spec. + pub fn id(chain: impl EthChainSpec) -> Option<&'static [u8]> { + if chain.chain().is_optimism() { return Some(Self::OPEL) - } else if chain.is_ethereum() { + } else if chain.chain().is_ethereum() { return Some(Self::ETH) } diff --git a/crates/net/downloaders/src/bodies/test_utils.rs b/crates/net/downloaders/src/bodies/test_utils.rs index d61e1f7fb6c8..af4bf8145af4 100644 --- a/crates/net/downloaders/src/bodies/test_utils.rs +++ b/crates/net/downloaders/src/bodies/test_utils.rs @@ -34,7 +34,7 @@ pub(crate) fn create_raw_bodies( .into_iter() .map(|header| { let body = bodies.remove(&header.hash()).expect("body exists"); - body.create_block(header.unseal()) + body.into_block(header.unseal()) }) .collect() } diff --git a/crates/net/eth-wire-types/src/status.rs b/crates/net/eth-wire-types/src/status.rs index 91e4223a35a8..baf1e2991522 100644 --- a/crates/net/eth-wire-types/src/status.rs +++ b/crates/net/eth-wire-types/src/status.rs @@ -3,7 +3,7 @@ use alloy_chains::{Chain, NamedChain}; use alloy_genesis::Genesis; use alloy_primitives::{hex, B256, U256}; use alloy_rlp::{RlpDecodable, RlpEncodable}; -use reth_chainspec::{ChainSpec, MAINNET}; +use reth_chainspec::{ChainSpec, EthChainSpec, Hardforks, MAINNET}; use reth_codecs_derive::add_arbitrary_tests; use reth_primitives::{EthereumHardfork, ForkId, Head}; use std::fmt::{Debug, Display}; @@ -75,9 +75,12 @@ impl Status { /// /// Sets the `chain` and `genesis`, `blockhash`, and `forkid` fields based on the [`ChainSpec`] /// and head. - pub fn spec_builder(spec: &ChainSpec, head: &Head) -> StatusBuilder { + pub fn spec_builder(spec: Spec, head: &Head) -> StatusBuilder + where + Spec: EthChainSpec + Hardforks, + { Self::builder() - .chain(spec.chain) + .chain(spec.chain()) .genesis(spec.genesis_hash()) .blockhash(head.hash) .total_difficulty(head.total_difficulty) diff --git a/crates/net/network/src/config.rs b/crates/net/network/src/config.rs index 546d9f182133..8217a02a1bab 100644 --- a/crates/net/network/src/config.rs +++ b/crates/net/network/src/config.rs @@ -2,7 +2,7 @@ use std::{collections::HashSet, net::SocketAddr, sync::Arc}; -use reth_chainspec::{ChainSpec, MAINNET}; +use reth_chainspec::{ChainSpecProvider, EthChainSpec, Hardforks}; use reth_discv4::{Discv4Config, Discv4ConfigBuilder, NatResolver, DEFAULT_DISCOVERY_ADDRESS}; use reth_discv5::NetworkStackId; use reth_dns_discovery::DnsDiscoveryConfig; @@ -10,7 +10,7 @@ use reth_eth_wire::{HelloMessage, HelloMessageWithProtocols, Status}; use reth_network_peers::{mainnet_nodes, pk2id, sepolia_nodes, PeerId, TrustedPeer}; use reth_network_types::{PeersConfig, SessionsConfig}; use reth_primitives::{ForkFilter, Head}; -use reth_storage_api::{BlockNumReader, BlockReader, HeaderProvider}; +use reth_storage_api::{noop::NoopBlockReader, BlockNumReader, BlockReader, HeaderProvider}; use reth_tasks::{TaskSpawner, TokioTaskExecutor}; use secp256k1::SECP256K1; @@ -56,8 +56,8 @@ pub struct NetworkConfig { pub peers_config: PeersConfig, /// How to configure the [`SessionManager`](crate::session::SessionManager). pub sessions_config: SessionsConfig, - /// The chain spec - pub chain_spec: Arc, + /// The chain id + pub chain_id: u64, /// The [`ForkFilter`] to use at launch for authenticating sessions. /// /// See also @@ -99,7 +99,10 @@ impl NetworkConfig<()> { impl NetworkConfig { /// Create a new instance with all mandatory fields set, rest is field with defaults. - pub fn new(client: C, secret_key: SecretKey) -> Self { + pub fn new(client: C, secret_key: SecretKey) -> Self + where + C: ChainSpecProvider, + { NetworkConfig::builder(secret_key).build(client) } @@ -170,8 +173,6 @@ pub struct NetworkConfigBuilder { peers_config: Option, /// How to configure the sessions manager sessions_config: Option, - /// The network's chain spec - chain_spec: Arc, /// The default mode of the network. network_mode: NetworkMode, /// The executor to use for spawning tasks. @@ -211,7 +212,6 @@ impl NetworkConfigBuilder { listener_addr: None, peers_config: None, sessions_config: None, - chain_spec: MAINNET.clone(), network_mode: Default::default(), executor: None, hello_message: None, @@ -241,12 +241,6 @@ impl NetworkConfigBuilder { &self.secret_key } - /// Sets the chain spec. - pub fn chain_spec(mut self, chain_spec: Arc) -> Self { - self.chain_spec = chain_spec; - self - } - /// Sets the [`NetworkMode`]. pub const fn network_mode(mut self, network_mode: NetworkMode) -> Self { self.network_mode = network_mode; @@ -461,10 +455,14 @@ impl NetworkConfigBuilder { /// Convenience function for creating a [`NetworkConfig`] with a noop provider that does /// nothing. - pub fn build_with_noop_provider( + pub fn build_with_noop_provider( self, - ) -> NetworkConfig { - self.build(Default::default()) + chain_spec: Arc, + ) -> NetworkConfig> + where + ChainSpec: EthChainSpec + Hardforks + 'static, + { + self.build(NoopBlockReader::new(chain_spec)) } /// Consumes the type and creates the actual [`NetworkConfig`] @@ -473,8 +471,12 @@ impl NetworkConfigBuilder { /// The given client is to be used for interacting with the chain, for example fetching the /// corresponding block for a given block hash we receive from a peer in the status message when /// establishing a connection. - pub fn build(self, client: C) -> NetworkConfig { + pub fn build(self, client: C) -> NetworkConfig + where + C: ChainSpecProvider, + { let peer_id = self.get_peer_id(); + let chain_spec = client.chain_spec(); let Self { secret_key, mut dns_discovery_config, @@ -485,7 +487,6 @@ impl NetworkConfigBuilder { listener_addr, peers_config, sessions_config, - chain_spec, network_mode, executor, hello_message, @@ -514,9 +515,9 @@ impl NetworkConfigBuilder { let head = head.unwrap_or_else(|| Head { hash: chain_spec.genesis_hash(), number: 0, - timestamp: chain_spec.genesis.timestamp, - difficulty: chain_spec.genesis.difficulty, - total_difficulty: chain_spec.genesis.difficulty, + timestamp: chain_spec.genesis().timestamp, + difficulty: chain_spec.genesis().difficulty, + total_difficulty: chain_spec.genesis().difficulty, }); // set the status @@ -525,6 +526,9 @@ impl NetworkConfigBuilder { // set a fork filter based on the chain spec and head let fork_filter = chain_spec.fork_filter(head); + // get the chain id + let chain_id = chain_spec.chain().id(); + // If default DNS config is used then we add the known dns network to bootstrap from if let Some(dns_networks) = dns_discovery_config.as_mut().and_then(|c| c.bootstrap_dns_networks.as_mut()) @@ -547,7 +551,7 @@ impl NetworkConfigBuilder { listener_addr, peers_config: peers_config.unwrap_or_default(), sessions_config: sessions_config.unwrap_or_default(), - chain_spec, + chain_id, block_import: block_import.unwrap_or_else(|| Box::::default()), network_mode, executor: executor.unwrap_or_else(|| Box::::default()), @@ -587,9 +591,11 @@ impl NetworkMode { #[cfg(test)] mod tests { + use std::sync::Arc; + use super::*; use rand::thread_rng; - use reth_chainspec::Chain; + use reth_chainspec::{Chain, MAINNET}; use reth_dns_discovery::tree::LinkEntry; use reth_primitives::ForkHash; use reth_provider::test_utils::NoopProvider; @@ -622,7 +628,7 @@ mod tests { let genesis_fork_hash = ForkHash::from(chain_spec.genesis_hash()); // enforce that the fork_id set in the status is consistent with the generated fork filter - let config = builder().chain_spec(chain_spec).build(NoopProvider::default()); + let config = builder().build_with_noop_provider(chain_spec); let status = config.status; let fork_filter = config.fork_filter; diff --git a/crates/net/network/src/manager.rs b/crates/net/network/src/manager.rs index 864b6791f67a..3e86ae244284 100644 --- a/crates/net/network/src/manager.rs +++ b/crates/net/network/src/manager.rs @@ -178,7 +178,7 @@ impl NetworkManager { listener_addr, peers_config, sessions_config, - chain_spec, + chain_id, block_import, network_mode, boot_nodes, @@ -264,7 +264,7 @@ impl NetworkManager { local_peer_id, peers_handle, network_mode, - Arc::new(AtomicU64::new(chain_spec.chain.id())), + Arc::new(AtomicU64::new(chain_id)), tx_gossip_disabled, discv4, discv5, diff --git a/crates/net/network/src/test_utils/testnet.rs b/crates/net/network/src/test_utils/testnet.rs index 913fe55ae6fa..d92272a871e0 100644 --- a/crates/net/network/src/test_utils/testnet.rs +++ b/crates/net/network/src/test_utils/testnet.rs @@ -10,14 +10,14 @@ use std::{ use futures::{FutureExt, StreamExt}; use pin_project::pin_project; -use reth_chainspec::MAINNET; +use reth_chainspec::{Hardforks, MAINNET}; use reth_eth_wire::{protocol::Protocol, DisconnectReason, HelloMessageWithProtocols}; use reth_network_api::{ test_utils::{PeersHandle, PeersHandleProvider}, NetworkEvent, NetworkEventListenerProvider, NetworkInfo, Peers, }; use reth_network_peers::PeerId; -use reth_provider::test_utils::NoopProvider; +use reth_provider::{test_utils::NoopProvider, ChainSpecProvider}; use reth_storage_api::{BlockReader, BlockReaderIdExt, HeaderProvider, StateProviderFactory}; use reth_tasks::TokioTaskExecutor; use reth_tokio_util::EventStream; @@ -54,7 +54,7 @@ pub struct Testnet { impl Testnet where - C: BlockReader + HeaderProvider + Clone + 'static, + C: BlockReader + HeaderProvider + Clone + 'static + ChainSpecProvider, { /// Same as [`Self::try_create_with`] but panics on error pub async fn create_with(num_peers: usize, provider: C) -> Self { @@ -548,7 +548,10 @@ where /// Initialize the network with a random secret key, allowing the devp2p and discovery to bind /// to any available IP and port. - pub fn new(client: C) -> Self { + pub fn new(client: C) -> Self + where + C: ChainSpecProvider, + { let secret_key = SecretKey::new(&mut rand::thread_rng()); let config = Self::network_config_builder(secret_key).build(client.clone()); Self { config, client, secret_key } @@ -556,13 +559,19 @@ where /// Initialize the network with a given secret key, allowing devp2p and discovery to bind any /// available IP and port. - pub fn with_secret_key(client: C, secret_key: SecretKey) -> Self { + pub fn with_secret_key(client: C, secret_key: SecretKey) -> Self + where + C: ChainSpecProvider, + { let config = Self::network_config_builder(secret_key).build(client.clone()); Self { config, client, secret_key } } /// Initialize the network with a given capabilities. - pub fn with_protocols(client: C, protocols: impl IntoIterator) -> Self { + pub fn with_protocols(client: C, protocols: impl IntoIterator) -> Self + where + C: ChainSpecProvider, + { let secret_key = SecretKey::new(&mut rand::thread_rng()); let builder = Self::network_config_builder(secret_key); diff --git a/crates/net/network/tests/it/connect.rs b/crates/net/network/tests/it/connect.rs index 8201b3280bcf..5d91049b92af 100644 --- a/crates/net/network/tests/it/connect.rs +++ b/crates/net/network/tests/it/connect.rs @@ -5,6 +5,7 @@ use std::{collections::HashSet, net::SocketAddr, time::Duration}; use alloy_node_bindings::Geth; use alloy_provider::{ext::AdminApi, ProviderBuilder}; use futures::StreamExt; +use reth_chainspec::MAINNET; use reth_discv4::Discv4Config; use reth_eth_wire::{DisconnectReason, HeadersDirection}; use reth_net_banlist::BanList; @@ -688,7 +689,7 @@ async fn new_random_peer(max_in_bound: usize, trusted_nodes: Vec) - .listener_port(0) .disable_discovery() .peer_config(peers_config) - .build_with_noop_provider(); + .build_with_noop_provider(MAINNET.clone()); NetworkManager::new(config).await.unwrap() } diff --git a/crates/net/network/tests/it/startup.rs b/crates/net/network/tests/it/startup.rs index 269b352e4b10..8f7e4ff8c78c 100644 --- a/crates/net/network/tests/it/startup.rs +++ b/crates/net/network/tests/it/startup.rs @@ -3,6 +3,7 @@ use std::{ net::{Ipv4Addr, SocketAddr, SocketAddrV4}, }; +use reth_chainspec::MAINNET; use reth_discv4::Discv4Config; use reth_network::{ error::{NetworkError, ServiceKind}, @@ -74,7 +75,7 @@ async fn test_tcp_port_node_record_no_discovery() { let config = NetworkConfigBuilder::new(secret_key) .listener_port(0) .disable_discovery() - .build_with_noop_provider(); + .build_with_noop_provider(MAINNET.clone()); let network = NetworkManager::new(config).await.unwrap(); let local_addr = network.local_addr(); @@ -93,7 +94,7 @@ async fn test_tcp_port_node_record_discovery() { .listener_port(0) .discovery_port(0) .disable_dns_discovery() - .build_with_noop_provider(); + .build_with_noop_provider(MAINNET.clone()); let network = NetworkManager::new(config).await.unwrap(); let local_addr = network.local_addr(); diff --git a/crates/node/builder/src/builder/mod.rs b/crates/node/builder/src/builder/mod.rs index d7cec56cab91..2bd43d3c6ac7 100644 --- a/crates/node/builder/src/builder/mod.rs +++ b/crates/node/builder/src/builder/mod.rs @@ -10,7 +10,7 @@ pub use states::*; use std::sync::Arc; use futures::Future; -use reth_chainspec::{ChainSpec, EthChainSpec, EthereumHardforks}; +use reth_chainspec::{ChainSpec, EthChainSpec, EthereumHardforks, Hardforks}; use reth_cli_util::get_secret_key; use reth_db_api::{ database::Database, @@ -633,7 +633,10 @@ impl BuilderContext { pub fn build_network_config( &self, network_builder: NetworkConfigBuilder, - ) -> NetworkConfig { + ) -> NetworkConfig + where + Node::Types: NodeTypes, + { network_builder.build(self.provider.clone()) } } diff --git a/crates/node/builder/src/launch/common.rs b/crates/node/builder/src/launch/common.rs index d6575d000abb..720f69c18464 100644 --- a/crates/node/builder/src/launch/common.rs +++ b/crates/node/builder/src/launch/common.rs @@ -400,7 +400,7 @@ impl LaunchContextWith LaunchContextWith, DB>> where DB: Database + Clone + 'static, - ChainSpec: EthChainSpec + EthereumHardforks, + ChainSpec: EthChainSpec + EthereumHardforks + 'static, { /// Returns the [`ProviderFactory`] for the attached storage after executing a consistent check /// between the database and static files. **It may execute a pipeline unwind if it fails this diff --git a/crates/node/builder/src/setup.rs b/crates/node/builder/src/setup.rs index 10144d8699de..44c9f19e84b3 100644 --- a/crates/node/builder/src/setup.rs +++ b/crates/node/builder/src/setup.rs @@ -2,6 +2,7 @@ use std::sync::Arc; +use alloy_primitives::BlockNumber; use reth_config::{config::StageConfig, PruneConfig}; use reth_consensus::Consensus; use reth_downloaders::{ @@ -13,7 +14,7 @@ use reth_exex::ExExManagerHandle; use reth_network_p2p::{ bodies::downloader::BodyDownloader, headers::downloader::HeaderDownloader, BlockClient, }; -use reth_node_core::primitives::{BlockNumber, B256}; +use reth_node_core::primitives::B256; use reth_provider::{providers::ProviderNodeTypes, ProviderFactory}; use reth_stages::{prelude::DefaultStages, stages::ExecutionStage, Pipeline, StageSet}; use reth_static_file::StaticFileProducer; diff --git a/crates/node/core/src/args/network.rs b/crates/node/core/src/args/network.rs index 650a3dcb05e5..d25ebd8ea157 100644 --- a/crates/node/core/src/args/network.rs +++ b/crates/node/core/src/args/network.rs @@ -240,7 +240,6 @@ impl NetworkArgs { ) .peer_config(peers_config) .boot_nodes(chain_bootnodes.clone()) - .chain_spec(chain_spec) .transactions_manager_config(transactions_manager_config) // Configure node identity .apply(|builder| { diff --git a/crates/node/types/src/lib.rs b/crates/node/types/src/lib.rs index 2ad2f8abd873..2c72e02d3edc 100644 --- a/crates/node/types/src/lib.rs +++ b/crates/node/types/src/lib.rs @@ -121,7 +121,7 @@ impl AnyNodeTypes { impl NodeTypes for AnyNodeTypes where P: NodePrimitives + Send + Sync + Unpin + 'static, - C: EthChainSpec, + C: EthChainSpec + 'static, { type Primitives = P; type ChainSpec = C; @@ -157,7 +157,7 @@ impl NodeTypes for AnyNodeTypesWithEngine where P: NodePrimitives + Send + Sync + Unpin + 'static, E: EngineTypes + Send + Sync + Unpin, - C: EthChainSpec, + C: EthChainSpec + 'static, { type Primitives = P; type ChainSpec = C; @@ -167,7 +167,7 @@ impl NodeTypesWithEngine for AnyNodeTypesWithEngine where P: NodePrimitives + Send + Sync + Unpin + 'static, E: EngineTypes + Send + Sync + Unpin, - C: EthChainSpec, + C: EthChainSpec + 'static, { type Engine = E; } diff --git a/crates/optimism/cli/src/commands/init_state/bedrock.rs b/crates/optimism/cli/src/commands/init_state/bedrock.rs index 41cd467b2f22..2426a195a8b6 100644 --- a/crates/optimism/cli/src/commands/init_state/bedrock.rs +++ b/crates/optimism/cli/src/commands/init_state/bedrock.rs @@ -1,8 +1,7 @@ -use alloy_primitives::B256; +use alloy_primitives::{BlockNumber, B256}; use reth_optimism_primitives::bedrock::{BEDROCK_HEADER, BEDROCK_HEADER_HASH, BEDROCK_HEADER_TTD}; use reth_primitives::{ - BlockBody, BlockNumber, Header, SealedBlock, SealedBlockWithSenders, SealedHeader, - StaticFileSegment, U256, + BlockBody, Header, SealedBlock, SealedBlockWithSenders, SealedHeader, StaticFileSegment, U256, }; use reth_provider::{ providers::StaticFileProvider, BlockWriter, StageCheckpointWriter, StaticFileWriter, diff --git a/crates/optimism/cli/src/receipt_file_codec.rs b/crates/optimism/cli/src/receipt_file_codec.rs index c86bfa12a6bd..c0416fa5f072 100644 --- a/crates/optimism/cli/src/receipt_file_codec.rs +++ b/crates/optimism/cli/src/receipt_file_codec.rs @@ -1,10 +1,10 @@ //! Codec for reading raw receipts from a file. -use alloy_primitives::B256; +use alloy_primitives::{Address, Bloom, B256}; use alloy_rlp::{Decodable, RlpDecodable}; use reth_primitives::{ bytes::{Buf, BytesMut}, - Address, Bloom, Bytes, Log, Receipt, TxType, + Bytes, Log, Receipt, TxType, }; use tokio_util::codec::Decoder; diff --git a/crates/optimism/consensus/src/lib.rs b/crates/optimism/consensus/src/lib.rs index 1f529a9a927f..d040d32e04d1 100644 --- a/crates/optimism/consensus/src/lib.rs +++ b/crates/optimism/consensus/src/lib.rs @@ -25,6 +25,8 @@ use reth_primitives::{ use std::{sync::Arc, time::SystemTime}; mod proof; +pub use proof::calculate_receipt_root_no_memo_optimism; + mod validation; pub use validation::validate_block_post_execution; diff --git a/crates/optimism/consensus/src/proof.rs b/crates/optimism/consensus/src/proof.rs index 383902659989..b283356016c2 100644 --- a/crates/optimism/consensus/src/proof.rs +++ b/crates/optimism/consensus/src/proof.rs @@ -3,7 +3,7 @@ use alloy_primitives::B256; use reth_chainspec::ChainSpec; use reth_optimism_forks::OptimismHardfork; -use reth_primitives::ReceiptWithBloom; +use reth_primitives::{Receipt, ReceiptWithBloom, ReceiptWithBloomRef}; use reth_trie_common::root::ordered_trie_root_with_encoder; /// Calculates the receipt root for a header. @@ -37,6 +37,41 @@ pub(crate) fn calculate_receipt_root_optimism( ordered_trie_root_with_encoder(receipts, |r, buf| r.encode_inner(buf, false)) } +/// Calculates the receipt root for a header for the reference type of [Receipt]. +/// +/// NOTE: Prefer calculate receipt root optimism if you have log blooms memoized. +pub fn calculate_receipt_root_no_memo_optimism( + receipts: &[&Receipt], + chain_spec: impl reth_chainspec::Hardforks, + timestamp: u64, +) -> B256 { + // There is a minor bug in op-geth and op-erigon where in the Regolith hardfork, + // the receipt root calculation does not include the deposit nonce in the receipt + // encoding. In the Regolith Hardfork, we must strip the deposit nonce from the + // receipts before calculating the receipt root. This was corrected in the Canyon + // hardfork. + if chain_spec.is_fork_active_at_timestamp(OptimismHardfork::Regolith, timestamp) && + !chain_spec.is_fork_active_at_timestamp(OptimismHardfork::Canyon, timestamp) + { + let receipts = receipts + .iter() + .map(|r| { + let mut r = (*r).clone(); + r.deposit_nonce = None; + r + }) + .collect::>(); + + return ordered_trie_root_with_encoder(&receipts, |r, buf| { + ReceiptWithBloomRef::from(r).encode_inner(buf, false) + }) + } + + ordered_trie_root_with_encoder(receipts, |r, buf| { + ReceiptWithBloomRef::from(*r).encode_inner(buf, false) + }) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/optimism/payload/Cargo.toml b/crates/optimism/payload/Cargo.toml index e64b72610d99..e58b26ee83df 100644 --- a/crates/optimism/payload/Cargo.toml +++ b/crates/optimism/payload/Cargo.toml @@ -29,6 +29,7 @@ reth-chain-state.workspace = true # op-reth reth-evm-optimism.workspace = true +reth-optimism-consensus.workspace = true reth-optimism-forks.workspace = true # ethereum diff --git a/crates/optimism/payload/src/builder.rs b/crates/optimism/payload/src/builder.rs index 5e443a4f5a08..84e69cb92e73 100644 --- a/crates/optimism/payload/src/builder.rs +++ b/crates/optimism/payload/src/builder.rs @@ -11,13 +11,14 @@ use reth_evm::{ NextBlockEnvAttributes, }; use reth_execution_types::ExecutionOutcome; +use reth_optimism_consensus::calculate_receipt_root_no_memo_optimism; use reth_optimism_forks::OptimismHardfork; use reth_payload_primitives::{PayloadBuilderAttributes, PayloadBuilderError}; use reth_primitives::{ constants::BEACON_NONCE, proofs, revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg}, - Block, BlockBody, Header, IntoRecoveredTransaction, Receipt, TxType, EMPTY_OMMER_ROOT_HASH, + Block, BlockBody, Header, Receipt, TxType, EMPTY_OMMER_ROOT_HASH, }; use reth_provider::StateProviderFactory; use reth_revm::database::StateProviderDatabase; @@ -443,11 +444,9 @@ where Vec::new(), ); let receipts_root = execution_outcome - .optimism_receipts_root_slow( - block_number, - &chain_spec, - attributes.payload_attributes.timestamp, - ) + .generic_receipts_root_slow(block_number, |receipts| { + calculate_receipt_root_no_memo_optimism(receipts, &chain_spec, attributes.timestamp()) + }) .expect("Number is in range"); let logs_bloom = execution_outcome.block_logs_bloom(block_number).expect("Number is in range"); diff --git a/crates/optimism/rpc/Cargo.toml b/crates/optimism/rpc/Cargo.toml index 443830cc5321..e82ff1827652 100644 --- a/crates/optimism/rpc/Cargo.toml +++ b/crates/optimism/rpc/Cargo.toml @@ -29,6 +29,7 @@ reth-chainspec.workspace = true # op-reth reth-evm-optimism.workspace = true +reth-optimism-consensus.workspace = true reth-optimism-forks.workspace = true # ethereum diff --git a/crates/optimism/rpc/src/eth/pending_block.rs b/crates/optimism/rpc/src/eth/pending_block.rs index c96dff40b091..5b716f39320a 100644 --- a/crates/optimism/rpc/src/eth/pending_block.rs +++ b/crates/optimism/rpc/src/eth/pending_block.rs @@ -4,6 +4,7 @@ use alloy_primitives::{BlockNumber, B256}; use reth_chainspec::EthereumHardforks; use reth_evm::ConfigureEvm; use reth_node_api::{FullNodeComponents, NodeTypes}; +use reth_optimism_consensus::calculate_receipt_root_no_memo_optimism; use reth_primitives::{ revm_primitives::BlockEnv, BlockNumberOrTag, Header, Receipt, SealedBlockWithSenders, }; @@ -79,16 +80,18 @@ where fn receipts_root( &self, - _block_env: &BlockEnv, + block_env: &BlockEnv, execution_outcome: &ExecutionOutcome, block_number: BlockNumber, ) -> B256 { execution_outcome - .optimism_receipts_root_slow( - block_number, - self.provider().chain_spec().as_ref(), - _block_env.timestamp.to::(), - ) + .generic_receipts_root_slow(block_number, |receipts| { + calculate_receipt_root_no_memo_optimism( + receipts, + self.provider().chain_spec().as_ref(), + block_env.timestamp.to::(), + ) + }) .expect("Block is present") } } diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index 72de34e3f38e..9745159e8f52 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -18,12 +18,10 @@ reth-ethereum-forks.workspace = true reth-static-file-types.workspace = true reth-trie-common.workspace = true revm-primitives = { workspace = true, features = ["serde"] } -reth-chainspec = { workspace = true, optional = true } reth-codecs = { workspace = true, optional = true } # op-reth reth-optimism-chainspec = { workspace = true, optional = true } -reth-optimism-forks = { workspace = true, optional = true } # ethereum alloy-consensus.workspace = true @@ -96,7 +94,6 @@ asm-keccak = ["alloy-primitives/asm-keccak"] arbitrary = [ "reth-primitives-traits/arbitrary", "revm-primitives/arbitrary", - "reth-chainspec?/arbitrary", "reth-ethereum-forks/arbitrary", "alloy-eips/arbitrary", "dep:arbitrary", @@ -111,12 +108,10 @@ c-kzg = [ "alloy-consensus/kzg", ] optimism = [ - "reth-chainspec/optimism", "revm-primitives/optimism", "reth-codecs?/optimism", "dep:reth-optimism-chainspec", "dep:op-alloy-consensus", - "reth-optimism-forks", ] alloy-compat = [ "dep:alloy-rpc-types", diff --git a/crates/primitives/src/block.rs b/crates/primitives/src/block.rs index 5914602127e5..a5357fcec993 100644 --- a/crates/primitives/src/block.rs +++ b/crates/primitives/src/block.rs @@ -1,12 +1,12 @@ use crate::{ - Address, Bytes, GotExpected, Header, SealedHeader, TransactionSigned, - TransactionSignedEcRecovered, Withdrawals, B256, + Bytes, GotExpected, Header, SealedHeader, TransactionSigned, TransactionSignedEcRecovered, + Withdrawals, B256, }; use alloc::vec::Vec; pub use alloy_eips::eip1898::{ BlockHashOrNumber, BlockId, BlockNumHash, BlockNumberOrTag, ForkBlock, RpcBlockHash, }; -use alloy_primitives::Sealable; +use alloy_primitives::{Address, Sealable}; use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable}; use derive_more::{Deref, DerefMut}; #[cfg(any(test, feature = "arbitrary"))] @@ -576,17 +576,8 @@ pub struct BlockBody { impl BlockBody { /// Create a [`Block`] from the body and its header. - // todo(onbjerg): should this not just take `self`? its used in one place - pub fn create_block(&self, header: Header) -> Block { - Block { - header, - body: Self { - transactions: self.transactions.clone(), - ommers: self.ommers.clone(), - withdrawals: self.withdrawals.clone(), - requests: self.requests.clone(), - }, - } + pub const fn into_block(self, header: Header) -> Block { + Block { header, body: self } } /// Calculate the transaction root for the block body. diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index cdcf642c05ae..b0564ed07d5a 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -62,9 +62,9 @@ pub use transaction::BlobTransactionValidationError; pub use transaction::{ util::secp256k1::{public_key_to_address, recover_signer_unchecked, sign_message}, - IntoRecoveredTransaction, InvalidTransactionError, Signature, Transaction, TransactionMeta, - TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash, TxHashOrNumber, - TxType, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, EIP7702_TX_TYPE_ID, + InvalidTransactionError, Signature, Transaction, TransactionMeta, TransactionSigned, + TransactionSignedEcRecovered, TransactionSignedNoHash, TxHashOrNumber, TxType, + EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, EIP7702_TX_TYPE_ID, LEGACY_TX_TYPE_ID, }; @@ -72,30 +72,13 @@ pub use transaction::{ pub use alloy_primitives::{ self, address, b256, bloom, bytes, bytes::{Buf, BufMut, BytesMut}, - eip191_hash_message, hex, hex_literal, keccak256, ruint, + hex, hex_literal, ruint, utils::format_ether, - Address, BlockHash, BlockNumber, Bloom, BloomInput, Bytes, ChainId, Selector, StorageKey, - StorageValue, TxHash, TxIndex, TxNumber, B128, B256, B512, B64, U128, U256, U64, U8, + Bytes, StorageValue, TxHash, TxIndex, TxNumber, B128, B256, B512, B64, U128, U256, U64, }; pub use reth_ethereum_forks::*; pub use revm_primitives::{self, JumpTable}; -#[doc(hidden)] -#[deprecated = "use B64 instead"] -pub type H64 = B64; -#[doc(hidden)] -#[deprecated = "use B128 instead"] -pub type H128 = B128; -#[doc(hidden)] -#[deprecated = "use Address instead"] -pub type H160 = Address; -#[doc(hidden)] -#[deprecated = "use B256 instead"] -pub type H256 = B256; -#[doc(hidden)] -#[deprecated = "use B512 instead"] -pub type H512 = B512; - #[cfg(any(test, feature = "arbitrary"))] pub use arbitrary; diff --git a/crates/primitives/src/proofs.rs b/crates/primitives/src/proofs.rs index 7f41fa9bd20f..040ec365503e 100644 --- a/crates/primitives/src/proofs.rs +++ b/crates/primitives/src/proofs.rs @@ -1,11 +1,12 @@ //! Helper function for calculating Merkle proofs and hashes. use crate::{ - constants::EMPTY_OMMER_ROOT_HASH, keccak256, Header, Receipt, ReceiptWithBloom, - ReceiptWithBloomRef, Request, TransactionSigned, Withdrawal, B256, + constants::EMPTY_OMMER_ROOT_HASH, Header, Receipt, ReceiptWithBloom, ReceiptWithBloomRef, + Request, TransactionSigned, Withdrawal, B256, }; use alloc::vec::Vec; use alloy_eips::eip7685::Encodable7685; +use alloy_primitives::keccak256; use reth_trie_common::root::{ordered_trie_root, ordered_trie_root_with_encoder}; /// Calculate a transaction root. @@ -49,47 +50,6 @@ pub fn calculate_receipt_root_no_memo(receipts: &[&Receipt]) -> B256 { }) } -/// Calculates the receipt root for a header for the reference type of [Receipt]. -/// -/// NOTE: Prefer calculate receipt root optimism if you have log blooms memoized. -#[cfg(feature = "optimism")] -pub fn calculate_receipt_root_no_memo_optimism( - receipts: &[&Receipt], - chain_spec: impl reth_chainspec::Hardforks, - timestamp: u64, -) -> B256 { - // There is a minor bug in op-geth and op-erigon where in the Regolith hardfork, - // the receipt root calculation does not include the deposit nonce in the receipt - // encoding. In the Regolith Hardfork, we must strip the deposit nonce from the - // receipts before calculating the receipt root. This was corrected in the Canyon - // hardfork. - - if chain_spec - .is_fork_active_at_timestamp(reth_optimism_forks::OptimismHardfork::Regolith, timestamp) && - !chain_spec.is_fork_active_at_timestamp( - reth_optimism_forks::OptimismHardfork::Canyon, - timestamp, - ) - { - let receipts = receipts - .iter() - .map(|r| { - let mut r = (*r).clone(); - r.deposit_nonce = None; - r - }) - .collect::>(); - - return ordered_trie_root_with_encoder(&receipts, |r, buf| { - ReceiptWithBloomRef::from(r).encode_inner(buf, false) - }) - } - - ordered_trie_root_with_encoder(receipts, |r, buf| { - ReceiptWithBloomRef::from(*r).encode_inner(buf, false) - }) -} - /// Calculates the root hash for ommer/uncle headers. pub fn calculate_ommers_root(ommers: &[Header]) -> B256 { // Check if `ommers` list is empty diff --git a/crates/primitives/src/receipt.rs b/crates/primitives/src/receipt.rs index 6b1b62ba6ffb..6b43e77e913a 100644 --- a/crates/primitives/src/receipt.rs +++ b/crates/primitives/src/receipt.rs @@ -1,11 +1,11 @@ #[cfg(feature = "reth-codec")] use crate::compression::{RECEIPT_COMPRESSOR, RECEIPT_DECOMPRESSOR}; use crate::{ - logs_bloom, Bloom, Bytes, TxType, B256, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, - EIP4844_TX_TYPE_ID, EIP7702_TX_TYPE_ID, + logs_bloom, Bytes, TxType, B256, EIP1559_TX_TYPE_ID, EIP2930_TX_TYPE_ID, EIP4844_TX_TYPE_ID, + EIP7702_TX_TYPE_ID, }; use alloc::{vec, vec::Vec}; -use alloy_primitives::Log; +use alloy_primitives::{Bloom, Log}; use alloy_rlp::{length_of_length, Decodable, Encodable, RlpDecodable, RlpEncodable}; use bytes::{Buf, BufMut}; use core::{cmp::Ordering, ops::Deref}; @@ -100,26 +100,11 @@ impl Receipts { self.receipt_vec.push(receipts); } - /// Retrieves the receipt root for all recorded receipts from index. - pub fn root_slow(&self, index: usize) -> Option { - Some(crate::proofs::calculate_receipt_root_no_memo( - &self.receipt_vec[index].iter().map(Option::as_ref).collect::>>()?, - )) - } - - /// Retrieves the receipt root for all recorded receipts from index. - #[cfg(feature = "optimism")] - pub fn optimism_root_slow( - &self, - index: usize, - chain_spec: impl reth_chainspec::Hardforks, - timestamp: u64, - ) -> Option { - Some(crate::proofs::calculate_receipt_root_no_memo_optimism( - &self.receipt_vec[index].iter().map(Option::as_ref).collect::>>()?, - chain_spec, - timestamp, - )) + /// Retrieves all recorded receipts from index and calculates the root using the given closure. + pub fn root_slow(&self, index: usize, f: impl FnOnce(&[&Receipt]) -> B256) -> Option { + let receipts = + self.receipt_vec[index].iter().map(Option::as_ref).collect::>>()?; + Some(f(receipts.as_slice())) } } diff --git a/crates/primitives/src/transaction/access_list.rs b/crates/primitives/src/transaction/access_list.rs index 7a0782bef761..32beb293f9c1 100644 --- a/crates/primitives/src/transaction/access_list.rs +++ b/crates/primitives/src/transaction/access_list.rs @@ -2,8 +2,9 @@ #[cfg(test)] mod tests { - use crate::{Address, B256}; + use crate::B256; use alloy_eips::eip2930::{AccessList, AccessListItem}; + use alloy_primitives::Address; use alloy_rlp::{RlpDecodable, RlpDecodableWrapper, RlpEncodable, RlpEncodableWrapper}; use proptest::proptest; use proptest_arbitrary_interop::arb; diff --git a/crates/primitives/src/transaction/compat.rs b/crates/primitives/src/transaction/compat.rs index 319ae55b1af1..ac39671f34a8 100644 --- a/crates/primitives/src/transaction/compat.rs +++ b/crates/primitives/src/transaction/compat.rs @@ -1,5 +1,5 @@ -use crate::{Address, Transaction, TransactionSigned, U256}; -use alloy_primitives::TxKind; +use crate::{Transaction, TransactionSigned, U256}; +use alloy_primitives::{Address, TxKind}; use revm_primitives::{AuthorizationList, TxEnv}; /// Implements behaviour to fill a [`TxEnv`] from another transaction. diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index f1dae00fece2..ec1ddaa87810 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -1,8 +1,8 @@ //! Transaction types. -use crate::{keccak256, Address, BlockHashOrNumber, Bytes, TxHash, B256, U256}; +use crate::{BlockHashOrNumber, Bytes, TxHash, B256, U256}; use alloy_eips::eip7702::SignedAuthorization; -use alloy_primitives::TxKind; +use alloy_primitives::{keccak256, Address, TxKind}; use alloy_consensus::{SignableTransaction, TxEip1559, TxEip2930, TxEip4844, TxEip7702, TxLegacy}; use alloy_eips::eip2930::AccessList; @@ -1634,15 +1634,6 @@ impl Decodable for TransactionSignedEcRecovered { } } -/// Ensures the transaction can be sent over the -/// network -pub trait IntoRecoveredTransaction { - /// Converts to this type into a [`TransactionSignedEcRecovered`]. - /// - /// Note: this takes `&self` since indented usage is via `Arc`. - fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered; -} - /// Generic wrapper with encoded Bytes, such as transaction data. #[derive(Debug, Clone, PartialEq, Eq)] pub struct WithEncoded(Bytes, pub T); @@ -1702,10 +1693,10 @@ mod tests { use crate::{ hex, transaction::{signature::Signature, TxEip1559, TxKind, TxLegacy}, - Address, Bytes, Transaction, TransactionSigned, TransactionSignedEcRecovered, + Bytes, Transaction, TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash, B256, U256, }; - use alloy_primitives::{address, b256, bytes, Parity}; + use alloy_primitives::{address, b256, bytes, Address, Parity}; use alloy_rlp::{Decodable, Encodable, Error as RlpError}; use reth_chainspec::MIN_TRANSACTION_GAS; use reth_codecs::Compact; diff --git a/crates/primitives/src/transaction/pooled.rs b/crates/primitives/src/transaction/pooled.rs index 78ad756923c0..3f19b29b36c1 100644 --- a/crates/primitives/src/transaction/pooled.rs +++ b/crates/primitives/src/transaction/pooled.rs @@ -7,14 +7,15 @@ use super::{ TxEip7702, }; use crate::{ - Address, BlobTransaction, BlobTransactionSidecar, Bytes, Signature, Transaction, - TransactionSigned, TransactionSignedEcRecovered, TxHash, B256, EIP4844_TX_TYPE_ID, + BlobTransaction, BlobTransactionSidecar, Bytes, Signature, Transaction, TransactionSigned, + TransactionSignedEcRecovered, TxHash, B256, EIP4844_TX_TYPE_ID, }; use alloc::vec::Vec; use alloy_consensus::{ transaction::{TxEip1559, TxEip2930, TxEip4844, TxLegacy}, SignableTransaction, TxEip4844WithSidecar, }; +use alloy_primitives::Address; use alloy_rlp::{Decodable, Encodable, Error as RlpError, Header, EMPTY_LIST_CODE}; use bytes::Buf; use derive_more::{AsRef, Deref}; diff --git a/crates/primitives/src/transaction/sidecar.rs b/crates/primitives/src/transaction/sidecar.rs index e486fa670b6d..94dfbe6da4b6 100644 --- a/crates/primitives/src/transaction/sidecar.rs +++ b/crates/primitives/src/transaction/sidecar.rs @@ -1,7 +1,8 @@ #![cfg_attr(docsrs, doc(cfg(feature = "c-kzg")))] -use crate::{keccak256, Signature, Transaction, TransactionSigned, TxHash, EIP4844_TX_TYPE_ID}; +use crate::{Signature, Transaction, TransactionSigned, TxHash, EIP4844_TX_TYPE_ID}; use alloy_consensus::{transaction::TxEip4844, TxEip4844WithSidecar}; +use alloy_primitives::keccak256; use alloy_rlp::{Decodable, Error as RlpError, Header}; use serde::{Deserialize, Serialize}; diff --git a/crates/primitives/src/transaction/signature.rs b/crates/primitives/src/transaction/signature.rs index 4b4b5bb4547e..07a2d6195795 100644 --- a/crates/primitives/src/transaction/signature.rs +++ b/crates/primitives/src/transaction/signature.rs @@ -1,6 +1,5 @@ -use crate::{transaction::util::secp256k1, Address, B256, U256}; - -use alloy_primitives::Parity; +use crate::{transaction::util::secp256k1, B256, U256}; +use alloy_primitives::{Address, Parity}; use alloy_rlp::{Decodable, Error as RlpError}; pub use alloy_primitives::Signature; @@ -120,9 +119,9 @@ mod tests { transaction::signature::{ legacy_parity, recover_signer, recover_signer_unchecked, SECP256K1N_HALF, }, - Address, Signature, B256, U256, + Signature, B256, U256, }; - use alloy_primitives::Parity; + use alloy_primitives::{Address, Parity}; use std::str::FromStr; #[test] diff --git a/crates/primitives/src/transaction/tx_type.rs b/crates/primitives/src/transaction/tx_type.rs index 7331ba1ed93f..4017d8d4b9c4 100644 --- a/crates/primitives/src/transaction/tx_type.rs +++ b/crates/primitives/src/transaction/tx_type.rs @@ -1,4 +1,5 @@ -use crate::{U64, U8}; +use crate::U64; +use alloy_primitives::U8; use alloy_rlp::{Decodable, Encodable}; use serde::{Deserialize, Serialize}; diff --git a/crates/primitives/src/transaction/util.rs b/crates/primitives/src/transaction/util.rs index f3f49cb316c1..6205ec886ca0 100644 --- a/crates/primitives/src/transaction/util.rs +++ b/crates/primitives/src/transaction/util.rs @@ -1,4 +1,5 @@ -use crate::{Address, Signature}; +use crate::Signature; +use alloy_primitives::Address; use revm_primitives::B256; #[cfg(feature = "secp256k1")] @@ -14,13 +15,12 @@ pub(crate) mod secp256k1 { #[cfg(feature = "secp256k1")] mod impl_secp256k1 { use super::*; - use crate::keccak256; pub(crate) use ::secp256k1::Error; use ::secp256k1::{ ecdsa::{RecoverableSignature, RecoveryId}, Message, PublicKey, SecretKey, SECP256K1, }; - use alloy_primitives::Parity; + use alloy_primitives::{keccak256, Parity}; use revm_primitives::U256; /// Recovers the address of the sender using secp256k1 pubkey recovery. @@ -65,8 +65,7 @@ mod impl_secp256k1 { #[cfg_attr(feature = "secp256k1", allow(unused, unreachable_pub))] mod impl_k256 { use super::*; - use crate::keccak256; - use alloy_primitives::Parity; + use alloy_primitives::{keccak256, Parity}; pub(crate) use k256::ecdsa::Error; use k256::ecdsa::{RecoveryId, SigningKey, VerifyingKey}; use revm_primitives::U256; diff --git a/crates/primitives/src/transaction/variant.rs b/crates/primitives/src/transaction/variant.rs index 3e96b6dda899..c558e7ccb9ba 100644 --- a/crates/primitives/src/transaction/variant.rs +++ b/crates/primitives/src/transaction/variant.rs @@ -2,9 +2,9 @@ //! `TransactionSignedEcRecovered` use crate::{ - Address, Transaction, TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash, - B256, + Transaction, TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash, B256, }; +use alloy_primitives::Address; use core::ops::Deref; /// Represents various different transaction formats used in reth. diff --git a/crates/revm/Cargo.toml b/crates/revm/Cargo.toml index b4f169249444..9e4501f62770 100644 --- a/crates/revm/Cargo.toml +++ b/crates/revm/Cargo.toml @@ -21,6 +21,7 @@ reth-consensus-common.workspace = true reth-prune-types.workspace = true reth-storage-api.workspace = true reth-trie = { workspace = true, optional = true } +alloy-primitives.workspace = true # revm revm.workspace = true @@ -28,6 +29,7 @@ revm.workspace = true [dev-dependencies] reth-trie.workspace = true reth-ethereum-forks.workspace = true +alloy-primitives.workspace = true [features] default = ["std", "c-kzg"] diff --git a/crates/revm/src/batch.rs b/crates/revm/src/batch.rs index 4502732a429d..7c8e0f29b5d7 100644 --- a/crates/revm/src/batch.rs +++ b/crates/revm/src/batch.rs @@ -182,7 +182,8 @@ impl BlockBatchRecord { mod tests { use super::*; use alloc::collections::BTreeMap; - use reth_primitives::{Address, Log, Receipt}; + use alloy_primitives::Address; + use reth_primitives::{Log, Receipt}; use reth_prune_types::{PruneMode, ReceiptsLogPruneConfig}; #[test] diff --git a/crates/revm/src/database.rs b/crates/revm/src/database.rs index fb5f71045ea1..e277d63e4bcc 100644 --- a/crates/revm/src/database.rs +++ b/crates/revm/src/database.rs @@ -1,6 +1,7 @@ use crate::primitives::alloy_primitives::{BlockNumber, StorageKey, StorageValue}; +use alloy_primitives::Address; use core::ops::{Deref, DerefMut}; -use reth_primitives::{Account, Address, B256, U256}; +use reth_primitives::{Account, B256, U256}; use reth_storage_errors::provider::{ProviderError, ProviderResult}; use revm::{ db::DatabaseRef, diff --git a/crates/revm/src/state_change.rs b/crates/revm/src/state_change.rs index 6376957f3111..8799b8c70eb2 100644 --- a/crates/revm/src/state_change.rs +++ b/crates/revm/src/state_change.rs @@ -1,7 +1,8 @@ use crate::precompile::HashMap; +use alloy_primitives::Address; use reth_chainspec::{ChainSpec, EthereumHardforks}; use reth_consensus_common::calc; -use reth_primitives::{Address, Block, Withdrawal, Withdrawals, U256}; +use reth_primitives::{Block, Withdrawal, Withdrawals, U256}; /// Collect all balance changes at the end of the block. /// diff --git a/crates/revm/src/test_utils.rs b/crates/revm/src/test_utils.rs index fe377bc5fc3d..bbdc196cd528 100644 --- a/crates/revm/src/test_utils.rs +++ b/crates/revm/src/test_utils.rs @@ -2,9 +2,8 @@ use std::collections::HashSet; use crate::precompile::HashMap; use alloc::vec::Vec; -use reth_primitives::{ - keccak256, Account, Address, BlockNumber, Bytecode, Bytes, StorageKey, B256, U256, -}; +use alloy_primitives::{keccak256, Address, BlockNumber, StorageKey}; +use reth_primitives::{Account, Bytecode, Bytes, B256, U256}; use reth_storage_api::{ AccountReader, BlockHashReader, StateProofProvider, StateProvider, StateRootProvider, StorageRootProvider, diff --git a/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs b/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs index d4d5fd23d458..3f5d540723cb 100644 --- a/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs +++ b/crates/rpc/rpc-eth-api/src/helpers/pending_block.rs @@ -20,8 +20,8 @@ use reth_primitives::{ BlockEnv, CfgEnv, CfgEnvWithHandlerCfg, EVMError, Env, ExecutionResult, InvalidTransaction, ResultAndState, SpecId, }, - Block, BlockBody, Header, IntoRecoveredTransaction, Receipt, Requests, SealedBlockWithSenders, - SealedHeader, TransactionSignedEcRecovered, EMPTY_OMMER_ROOT_HASH, + Block, BlockBody, Header, Receipt, Requests, SealedBlockWithSenders, SealedHeader, + TransactionSignedEcRecovered, EMPTY_OMMER_ROOT_HASH, }; use reth_provider::{ BlockReader, BlockReaderIdExt, ChainSpecProvider, EvmEnvProvider, ProviderError, diff --git a/crates/rpc/rpc/src/eth/filter.rs b/crates/rpc/rpc/src/eth/filter.rs index c4caa7300ee2..23bf0b58fc55 100644 --- a/crates/rpc/rpc/src/eth/filter.rs +++ b/crates/rpc/rpc/src/eth/filter.rs @@ -19,7 +19,7 @@ use async_trait::async_trait; use jsonrpsee::{core::RpcResult, server::IdProvider}; use reth_chainspec::ChainInfo; use reth_node_api::EthApiTypes; -use reth_primitives::{IntoRecoveredTransaction, TransactionSignedEcRecovered}; +use reth_primitives::TransactionSignedEcRecovered; use reth_provider::{BlockIdReader, BlockReader, EvmEnvProvider, ProviderError}; use reth_rpc_eth_api::{EthFilterApiServer, FullEthApiTypes, RpcTransaction, TransactionCompat}; use reth_rpc_eth_types::{ diff --git a/crates/rpc/rpc/src/eth/helpers/types.rs b/crates/rpc/rpc/src/eth/helpers/types.rs index 8cd659bb81d2..05da8bd91563 100644 --- a/crates/rpc/rpc/src/eth/helpers/types.rs +++ b/crates/rpc/rpc/src/eth/helpers/types.rs @@ -1,10 +1,10 @@ //! L1 `eth` API types. use alloy_network::{AnyNetwork, Network}; -use alloy_primitives::TxKind; +use alloy_primitives::{Address, TxKind}; use alloy_rpc_types::{Transaction, TransactionInfo}; use alloy_serde::WithOtherFields; -use reth_primitives::{Address, TransactionSignedEcRecovered}; +use reth_primitives::TransactionSignedEcRecovered; use reth_rpc_types_compat::{ transaction::{from_primitive_signature, GasPrice}, TransactionCompat, diff --git a/crates/rpc/rpc/src/eth/pubsub.rs b/crates/rpc/rpc/src/eth/pubsub.rs index 17c4f7b74b04..7bd1fd03d3b9 100644 --- a/crates/rpc/rpc/src/eth/pubsub.rs +++ b/crates/rpc/rpc/src/eth/pubsub.rs @@ -16,7 +16,6 @@ use jsonrpsee::{ server::SubscriptionMessage, types::ErrorObject, PendingSubscriptionSink, SubscriptionSink, }; use reth_network_api::NetworkInfo; -use reth_primitives::IntoRecoveredTransaction; use reth_provider::{BlockReader, CanonStateSubscriptions, EvmEnvProvider}; use reth_rpc_eth_api::{pubsub::EthPubSubApiServer, FullEthApiTypes, RpcTransaction}; use reth_rpc_eth_types::logs_utils; diff --git a/crates/stages/stages/Cargo.toml b/crates/stages/stages/Cargo.toml index 934e9b109053..9ed84160477f 100644 --- a/crates/stages/stages/Cargo.toml +++ b/crates/stages/stages/Cargo.toml @@ -37,6 +37,8 @@ reth-trie-db = { workspace = true, features = ["metrics"] } reth-testing-utils = { workspace = true, optional = true } +alloy-primitives.workspace = true + # async tokio = { workspace = true, features = ["sync"] } futures-util.workspace = true diff --git a/crates/stages/stages/benches/criterion.rs b/crates/stages/stages/benches/criterion.rs index 3957ceac4d70..7519d81a3622 100644 --- a/crates/stages/stages/benches/criterion.rs +++ b/crates/stages/stages/benches/criterion.rs @@ -6,7 +6,7 @@ use reth_chainspec::ChainSpec; use reth_config::config::{EtlConfig, TransactionLookupConfig}; use reth_db::{test_utils::TempDatabase, Database, DatabaseEnv}; -use reth_primitives::BlockNumber; +use alloy_primitives::BlockNumber; use reth_provider::{DatabaseProvider, DatabaseProviderFactory}; use reth_stages::{ stages::{MerkleStage, SenderRecoveryStage, TransactionLookupStage}, diff --git a/crates/stages/stages/benches/setup/account_hashing.rs b/crates/stages/stages/benches/setup/account_hashing.rs index 86831418e53f..9926c1f3d378 100644 --- a/crates/stages/stages/benches/setup/account_hashing.rs +++ b/crates/stages/stages/benches/setup/account_hashing.rs @@ -1,11 +1,11 @@ #![allow(unreachable_pub)] use super::constants; +use alloy_primitives::BlockNumber; use reth_db::tables; use reth_db_api::{ cursor::DbCursorRO, database::Database, transaction::DbTx, DatabaseError as DbError, }; -use reth_primitives::BlockNumber; use reth_stages::{ stages::{AccountHashingStage, SeedOpts}, test_utils::TestStageDB, diff --git a/crates/stages/stages/benches/setup/mod.rs b/crates/stages/stages/benches/setup/mod.rs index 41570c057f8e..85e4e3e5a318 100644 --- a/crates/stages/stages/benches/setup/mod.rs +++ b/crates/stages/stages/benches/setup/mod.rs @@ -1,4 +1,5 @@ #![allow(unreachable_pub)] +use alloy_primitives::Address; use itertools::concat; use reth_chainspec::ChainSpec; use reth_db::{tables, test_utils::TempDatabase, Database, DatabaseEnv}; @@ -6,9 +7,7 @@ use reth_db_api::{ cursor::DbCursorRO, transaction::{DbTx, DbTxMut}, }; -use reth_primitives::{ - alloy_primitives::Sealable, Account, Address, SealedBlock, SealedHeader, B256, U256, -}; +use reth_primitives::{alloy_primitives::Sealable, Account, SealedBlock, SealedHeader, B256, U256}; use reth_provider::{DatabaseProvider, DatabaseProviderFactory, TrieWriter}; use reth_stages::{ stages::{AccountHashingStage, StorageHashingStage}, diff --git a/crates/stages/stages/src/stages/bodies.rs b/crates/stages/stages/src/stages/bodies.rs index 7f6d2d6a066f..cc30fc90a11d 100644 --- a/crates/stages/stages/src/stages/bodies.rs +++ b/crates/stages/stages/src/stages/bodies.rs @@ -621,6 +621,7 @@ mod tests { UnwindStageTestRunner, }, }; + use alloy_primitives::{BlockHash, BlockNumber}; use futures_util::Stream; use reth_db::{static_file::HeaderMask, tables}; use reth_db_api::{ @@ -636,8 +637,7 @@ mod tests { error::DownloadResult, }; use reth_primitives::{ - BlockBody, BlockHash, BlockNumber, Header, SealedBlock, SealedHeader, - StaticFileSegment, TxNumber, B256, + BlockBody, Header, SealedBlock, SealedHeader, StaticFileSegment, TxNumber, B256, }; use reth_provider::{ providers::StaticFileWriter, test_utils::MockNodeTypesWithDB, HeaderProvider, diff --git a/crates/stages/stages/src/stages/execution.rs b/crates/stages/stages/src/stages/execution.rs index 57a9cdaab7ef..04cc1dd65923 100644 --- a/crates/stages/stages/src/stages/execution.rs +++ b/crates/stages/stages/src/stages/execution.rs @@ -1,4 +1,5 @@ use crate::stages::MERKLE_STAGE_DEFAULT_CLEAN_THRESHOLD; +use alloy_primitives::BlockNumber; use num_traits::Zero; use reth_config::config::ExecutionConfig; use reth_db::{static_file::HeaderMask, tables}; @@ -9,9 +10,7 @@ use reth_evm::{ }; use reth_execution_types::{Chain, ExecutionOutcome}; use reth_exex::{ExExManagerHandle, ExExNotification}; -use reth_primitives::{ - alloy_primitives::Sealable, BlockNumber, Header, SealedHeader, StaticFileSegment, -}; +use reth_primitives::{alloy_primitives::Sealable, Header, SealedHeader, StaticFileSegment}; use reth_primitives_traits::format_gas_throughput; use reth_provider::{ providers::{StaticFileProvider, StaticFileProviderRWRefMut, StaticFileWriter}, @@ -659,6 +658,7 @@ where mod tests { use super::*; use crate::test_utils::TestStageDB; + use alloy_primitives::{keccak256, Address}; use alloy_rlp::Decodable; use assert_matches::assert_matches; use reth_chainspec::ChainSpecBuilder; @@ -666,8 +666,7 @@ mod tests { use reth_evm_ethereum::execute::EthExecutorProvider; use reth_execution_errors::BlockValidationError; use reth_primitives::{ - address, hex_literal::hex, keccak256, Account, Address, Bytecode, SealedBlock, - StorageEntry, B256, U256, + address, hex_literal::hex, Account, Bytecode, SealedBlock, StorageEntry, B256, U256, }; use reth_provider::{ test_utils::create_test_provider_factory, AccountReader, DatabaseProviderFactory, diff --git a/crates/stages/stages/src/stages/hashing_account.rs b/crates/stages/stages/src/stages/hashing_account.rs index 2e109f7557f1..bbcb9b8da270 100644 --- a/crates/stages/stages/src/stages/hashing_account.rs +++ b/crates/stages/stages/src/stages/hashing_account.rs @@ -1,3 +1,4 @@ +use alloy_primitives::keccak256; use itertools::Itertools; use reth_config::config::{EtlConfig, HashingConfig}; use reth_db::{tables, RawKey, RawTable, RawValue}; @@ -6,7 +7,7 @@ use reth_db_api::{ transaction::{DbTx, DbTxMut}, }; use reth_etl::Collector; -use reth_primitives::{keccak256, Account, B256}; +use reth_primitives::{Account, B256}; use reth_provider::{AccountExtReader, DBProvider, HashingWriter, StatsReader}; use reth_stages_api::{ AccountHashingCheckpoint, EntitiesCheckpoint, ExecInput, ExecOutput, Stage, StageCheckpoint, @@ -63,7 +64,7 @@ impl AccountHashingStage { >( provider: &reth_provider::DatabaseProvider, opts: SeedOpts, - ) -> Result, StageError> { + ) -> Result, StageError> { use reth_db_api::models::AccountBeforeTx; use reth_primitives::U256; use reth_provider::{StaticFileProviderFactory, StaticFileWriter}; @@ -348,7 +349,7 @@ mod tests { mod test_utils { use super::*; use crate::test_utils::TestStageDB; - use reth_primitives::Address; + use alloy_primitives::Address; use reth_provider::DatabaseProviderFactory; pub(crate) struct AccountHashingTestRunner { diff --git a/crates/stages/stages/src/stages/hashing_storage.rs b/crates/stages/stages/src/stages/hashing_storage.rs index 54a9921dd521..ba1e03c1a296 100644 --- a/crates/stages/stages/src/stages/hashing_storage.rs +++ b/crates/stages/stages/src/stages/hashing_storage.rs @@ -1,3 +1,4 @@ +use alloy_primitives::keccak256; use itertools::Itertools; use reth_config::config::{EtlConfig, HashingConfig}; use reth_db::tables; @@ -8,7 +9,7 @@ use reth_db_api::{ transaction::{DbTx, DbTxMut}, }; use reth_etl::Collector; -use reth_primitives::{keccak256, BufMut, StorageEntry, B256}; +use reth_primitives::{BufMut, StorageEntry, B256}; use reth_provider::{DBProvider, HashingWriter, StatsReader, StorageReader}; use reth_stages_api::{ EntitiesCheckpoint, ExecInput, ExecOutput, Stage, StageCheckpoint, StageError, StageId, @@ -211,13 +212,14 @@ mod tests { stage_test_suite_ext, ExecuteStageTestRunner, StageTestRunner, TestRunnerError, TestStageDB, UnwindStageTestRunner, }; + use alloy_primitives::Address; use assert_matches::assert_matches; use rand::Rng; use reth_db_api::{ cursor::{DbCursorRW, DbDupCursorRO}, models::StoredBlockBodyIndices, }; - use reth_primitives::{Address, SealedBlock, U256}; + use reth_primitives::{SealedBlock, U256}; use reth_provider::providers::StaticFileWriter; use reth_testing_utils::generators::{ self, random_block_range, random_contract_account_range, BlockRangeParams, diff --git a/crates/stages/stages/src/stages/headers.rs b/crates/stages/stages/src/stages/headers.rs index f2ff1c666e26..25c7abaee48a 100644 --- a/crates/stages/stages/src/stages/headers.rs +++ b/crates/stages/stages/src/stages/headers.rs @@ -1,3 +1,4 @@ +use alloy_primitives::{BlockHash, BlockNumber}; use futures_util::StreamExt; use reth_codecs::Compact; use reth_config::config::EtlConfig; @@ -10,7 +11,7 @@ use reth_db_api::{ }; use reth_etl::Collector; use reth_network_p2p::headers::{downloader::HeaderDownloader, error::HeadersDownloaderError}; -use reth_primitives::{BlockHash, BlockNumber, SealedHeader, StaticFileSegment, B256}; +use reth_primitives::{SealedHeader, StaticFileSegment, B256}; use reth_provider::{ providers::{StaticFileProvider, StaticFileWriter}, BlockHashReader, DBProvider, HeaderProvider, HeaderSyncGap, HeaderSyncGapProvider, diff --git a/crates/stages/stages/src/stages/index_account_history.rs b/crates/stages/stages/src/stages/index_account_history.rs index 8ca8d173fd85..e0fcde2b194f 100644 --- a/crates/stages/stages/src/stages/index_account_history.rs +++ b/crates/stages/stages/src/stages/index_account_history.rs @@ -1,8 +1,8 @@ use super::{collect_history_indices, load_history_indices}; +use alloy_primitives::Address; use reth_config::config::{EtlConfig, IndexHistoryConfig}; use reth_db::tables; use reth_db_api::{models::ShardedKey, table::Decode, transaction::DbTxMut}; -use reth_primitives::Address; use reth_provider::{DBProvider, HistoryWriter, PruneCheckpointReader, PruneCheckpointWriter}; use reth_prune_types::{PruneCheckpoint, PruneMode, PrunePurpose, PruneSegment}; use reth_stages_api::{ @@ -148,6 +148,7 @@ mod tests { stage_test_suite_ext, ExecuteStageTestRunner, StageTestRunner, TestRunnerError, TestStageDB, UnwindStageTestRunner, }; + use alloy_primitives::BlockNumber; use itertools::Itertools; use reth_db::BlockNumberList; use reth_db_api::{ @@ -158,7 +159,7 @@ mod tests { }, transaction::DbTx, }; - use reth_primitives::{address, BlockNumber, B256}; + use reth_primitives::{address, B256}; use reth_provider::{providers::StaticFileWriter, DatabaseProviderFactory}; use reth_testing_utils::generators::{ self, random_block_range, random_changeset_range, random_contract_account_range, diff --git a/crates/stages/stages/src/stages/index_storage_history.rs b/crates/stages/stages/src/stages/index_storage_history.rs index 00646da2fd22..4af2cb3efea2 100644 --- a/crates/stages/stages/src/stages/index_storage_history.rs +++ b/crates/stages/stages/src/stages/index_storage_history.rs @@ -153,6 +153,7 @@ mod tests { stage_test_suite_ext, ExecuteStageTestRunner, StageTestRunner, TestRunnerError, TestStageDB, UnwindStageTestRunner, }; + use alloy_primitives::{Address, BlockNumber}; use itertools::Itertools; use reth_db::BlockNumberList; use reth_db_api::{ @@ -163,7 +164,7 @@ mod tests { }, transaction::DbTx, }; - use reth_primitives::{address, b256, Address, BlockNumber, StorageEntry, B256, U256}; + use reth_primitives::{address, b256, StorageEntry, B256, U256}; use reth_provider::{providers::StaticFileWriter, DatabaseProviderFactory}; use reth_testing_utils::generators::{ self, random_block_range, random_changeset_range, random_contract_account_range, diff --git a/crates/stages/stages/src/stages/merkle.rs b/crates/stages/stages/src/stages/merkle.rs index 83c8c52ce561..7b94067e9926 100644 --- a/crates/stages/stages/src/stages/merkle.rs +++ b/crates/stages/stages/src/stages/merkle.rs @@ -1,8 +1,9 @@ +use alloy_primitives::BlockNumber; use reth_codecs::Compact; use reth_consensus::ConsensusError; use reth_db::tables; use reth_db_api::transaction::{DbTx, DbTxMut}; -use reth_primitives::{alloy_primitives::Sealable, BlockNumber, GotExpected, SealedHeader, B256}; +use reth_primitives::{alloy_primitives::Sealable, GotExpected, SealedHeader, B256}; use reth_provider::{ DBProvider, HeaderProvider, ProviderError, StageCheckpointReader, StageCheckpointWriter, StatsReader, TrieWriter, @@ -373,9 +374,10 @@ mod tests { stage_test_suite_ext, ExecuteStageTestRunner, StageTestRunner, StorageKind, TestRunnerError, TestStageDB, UnwindStageTestRunner, }; + use alloy_primitives::keccak256; use assert_matches::assert_matches; use reth_db_api::cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO}; - use reth_primitives::{keccak256, SealedBlock, StaticFileSegment, StorageEntry, U256}; + use reth_primitives::{SealedBlock, StaticFileSegment, StorageEntry, U256}; use reth_provider::{providers::StaticFileWriter, StaticFileProviderFactory}; use reth_stages_api::StageUnitCheckpoint; use reth_testing_utils::generators::{ diff --git a/crates/stages/stages/src/stages/mod.rs b/crates/stages/stages/src/stages/mod.rs index 4eb3c6f141d4..d3c5b7099d21 100644 --- a/crates/stages/stages/src/stages/mod.rs +++ b/crates/stages/stages/src/stages/mod.rs @@ -42,6 +42,7 @@ use utils::*; mod tests { use super::*; use crate::test_utils::{StorageKind, TestStageDB}; + use alloy_primitives::{keccak256, BlockNumber}; use alloy_rlp::Decodable; use reth_chainspec::ChainSpecBuilder; use reth_db::{ @@ -56,8 +57,7 @@ mod tests { use reth_evm_ethereum::execute::EthExecutorProvider; use reth_exex::ExExManagerHandle; use reth_primitives::{ - address, hex_literal::hex, keccak256, Account, BlockNumber, Bytecode, SealedBlock, - StaticFileSegment, B256, U256, + address, hex_literal::hex, Account, Bytecode, SealedBlock, StaticFileSegment, B256, U256, }; use reth_provider::{ providers::{StaticFileProvider, StaticFileWriter}, diff --git a/crates/stages/stages/src/stages/sender_recovery.rs b/crates/stages/stages/src/stages/sender_recovery.rs index bf7df6147ff8..344a8ea848ee 100644 --- a/crates/stages/stages/src/stages/sender_recovery.rs +++ b/crates/stages/stages/src/stages/sender_recovery.rs @@ -1,3 +1,4 @@ +use alloy_primitives::Address; use reth_config::config::SenderRecoveryConfig; use reth_consensus::ConsensusError; use reth_db::{static_file::TransactionMask, tables, RawValue}; @@ -6,7 +7,7 @@ use reth_db_api::{ transaction::{DbTx, DbTxMut}, DbTxUnwindExt, }; -use reth_primitives::{Address, GotExpected, StaticFileSegment, TransactionSignedNoHash, TxNumber}; +use reth_primitives::{GotExpected, StaticFileSegment, TransactionSignedNoHash, TxNumber}; use reth_provider::{ BlockReader, DBProvider, HeaderProvider, ProviderError, PruneCheckpointReader, StaticFileProviderFactory, StatsReader, @@ -333,9 +334,10 @@ struct FailedSenderRecoveryError { #[cfg(test)] mod tests { + use alloy_primitives::BlockNumber; use assert_matches::assert_matches; use reth_db_api::cursor::DbCursorRO; - use reth_primitives::{BlockNumber, SealedBlock, TransactionSigned, B256}; + use reth_primitives::{SealedBlock, TransactionSigned, B256}; use reth_provider::{ providers::StaticFileWriter, DatabaseProviderFactory, PruneCheckpointWriter, StaticFileProviderFactory, TransactionsProvider, diff --git a/crates/stages/stages/src/stages/tx_lookup.rs b/crates/stages/stages/src/stages/tx_lookup.rs index e636c281829d..4e68cfe90b76 100644 --- a/crates/stages/stages/src/stages/tx_lookup.rs +++ b/crates/stages/stages/src/stages/tx_lookup.rs @@ -250,8 +250,9 @@ mod tests { stage_test_suite_ext, ExecuteStageTestRunner, StageTestRunner, StorageKind, TestRunnerError, TestStageDB, UnwindStageTestRunner, }; + use alloy_primitives::BlockNumber; use assert_matches::assert_matches; - use reth_primitives::{BlockNumber, SealedBlock, B256}; + use reth_primitives::{SealedBlock, B256}; use reth_provider::{ providers::StaticFileWriter, DatabaseProviderFactory, StaticFileProviderFactory, }; diff --git a/crates/stages/stages/src/stages/utils.rs b/crates/stages/stages/src/stages/utils.rs index cb9c729aa234..c16886e0bdca 100644 --- a/crates/stages/stages/src/stages/utils.rs +++ b/crates/stages/stages/src/stages/utils.rs @@ -1,4 +1,5 @@ //! Utils for `stages`. +use alloy_primitives::BlockNumber; use reth_config::config::EtlConfig; use reth_db::BlockNumberList; use reth_db_api::{ @@ -9,7 +10,6 @@ use reth_db_api::{ DatabaseError, }; use reth_etl::Collector; -use reth_primitives::BlockNumber; use reth_provider::DBProvider; use reth_stages_api::StageError; use std::{collections::HashMap, hash::Hash, ops::RangeBounds}; diff --git a/crates/stages/stages/src/test_utils/test_db.rs b/crates/stages/stages/src/test_utils/test_db.rs index 5fef9d6a2be2..89d74d9d4092 100644 --- a/crates/stages/stages/src/test_utils/test_db.rs +++ b/crates/stages/stages/src/test_utils/test_db.rs @@ -1,3 +1,4 @@ +use alloy_primitives::{keccak256, Address, BlockNumber}; use reth_chainspec::MAINNET; use reth_db::{ tables, @@ -14,8 +15,8 @@ use reth_db_api::{ DatabaseError as DbError, }; use reth_primitives::{ - keccak256, Account, Address, BlockNumber, Receipt, SealedBlock, SealedHeader, - StaticFileSegment, StorageEntry, TxHash, TxNumber, B256, U256, + Account, Receipt, SealedBlock, SealedHeader, StaticFileSegment, StorageEntry, TxHash, TxNumber, + B256, U256, }; use reth_provider::{ providers::{StaticFileProvider, StaticFileProviderRWRefMut, StaticFileWriter}, diff --git a/crates/static-file/types/src/segment.rs b/crates/static-file/types/src/segment.rs index cb742b3f3184..94f09b64a51a 100644 --- a/crates/static-file/types/src/segment.rs +++ b/crates/static-file/types/src/segment.rs @@ -122,6 +122,12 @@ impl StaticFileSegment { pub const fn is_receipts(&self) -> bool { matches!(self, Self::Receipts) } + + /// Returns `true` if the segment is `StaticFileSegment::Receipts` or + /// `StaticFileSegment::Transactions`. + pub const fn is_tx_based(&self) -> bool { + matches!(self, Self::Receipts | Self::Transactions) + } } /// A segment header that contains information common to all segments. Used for storage. @@ -239,7 +245,7 @@ impl SegmentHeader { match self.segment { StaticFileSegment::Headers => { if let Some(range) = &mut self.block_range { - if num > range.end { + if num > range.end - range.start { self.block_range = None; } else { range.end = range.end.saturating_sub(num); @@ -248,7 +254,7 @@ impl SegmentHeader { } StaticFileSegment::Transactions | StaticFileSegment::Receipts => { if let Some(range) = &mut self.tx_range { - if num > range.end { + if num > range.end - range.start { self.tx_range = None; } else { range.end = range.end.saturating_sub(num); diff --git a/crates/storage/codecs/Cargo.toml b/crates/storage/codecs/Cargo.toml index 7a999d35f73a..640ec8c9561f 100644 --- a/crates/storage/codecs/Cargo.toml +++ b/crates/storage/codecs/Cargo.toml @@ -27,7 +27,6 @@ op-alloy-consensus = { workspace = true, optional = true } # misc bytes.workspace = true modular-bitfield = { workspace = true, optional = true } -serde = { workspace = true, optional = true } [dev-dependencies] alloy-eips = { workspace = true, default-features = false, features = [ @@ -48,17 +47,17 @@ serde_json.workspace = true arbitrary = { workspace = true, features = ["derive"] } proptest.workspace = true proptest-arbitrary-interop.workspace = true +serde.workspace = true [features] default = ["std", "alloy"] -std = ["alloy-primitives/std", "bytes/std", "serde?/std"] +std = ["alloy-primitives/std", "bytes/std"] alloy = [ "dep:alloy-consensus", "dep:alloy-eips", "dep:alloy-genesis", "dep:modular-bitfield", "dep:alloy-trie", - "dep:serde" ] optimism = ["alloy", "dep:op-alloy-consensus"] test-utils = [] diff --git a/crates/storage/codecs/src/alloy/authorization_list.rs b/crates/storage/codecs/src/alloy/authorization_list.rs index 2c1495abf716..3efe13590622 100644 --- a/crates/storage/codecs/src/alloy/authorization_list.rs +++ b/crates/storage/codecs/src/alloy/authorization_list.rs @@ -5,13 +5,12 @@ use alloy_eips::eip7702::{Authorization as AlloyAuthorization, SignedAuthorizati use alloy_primitives::{Address, U256}; use bytes::Buf; use reth_codecs_derive::add_arbitrary_tests; -use serde::{Deserialize, Serialize}; /// Authorization acts as bridge which simplifies Compact implementation for AlloyAuthorization. /// /// Notice: Make sure this struct is 1:1 with `alloy_eips::eip7702::Authorization` -#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, Compact)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[add_arbitrary_tests(compact)] pub(crate) struct Authorization { chain_id: U256, diff --git a/crates/storage/codecs/src/alloy/genesis_account.rs b/crates/storage/codecs/src/alloy/genesis_account.rs index a94f4e2ef906..938ad1375b15 100644 --- a/crates/storage/codecs/src/alloy/genesis_account.rs +++ b/crates/storage/codecs/src/alloy/genesis_account.rs @@ -3,7 +3,6 @@ use alloc::vec::Vec; use alloy_genesis::GenesisAccount as AlloyGenesisAccount; use alloy_primitives::{Bytes, B256, U256}; use reth_codecs_derive::add_arbitrary_tests; -use serde::{Deserialize, Serialize}; /// `GenesisAccount` acts as bridge which simplifies Compact implementation for /// `AlloyGenesisAccount`. @@ -23,8 +22,8 @@ pub(crate) struct GenesisAccountRef<'a> { private_key: Option<&'a B256>, } -#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, Compact)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[add_arbitrary_tests(compact)] pub(crate) struct GenesisAccount { /// The nonce of the account at genesis. @@ -39,15 +38,15 @@ pub(crate) struct GenesisAccount { private_key: Option, } -#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, Compact)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[add_arbitrary_tests(compact)] pub(crate) struct StorageEntries { entries: Vec, } -#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, Compact)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[add_arbitrary_tests(compact)] pub(crate) struct StorageEntry { key: B256, diff --git a/crates/storage/codecs/src/alloy/header.rs b/crates/storage/codecs/src/alloy/header.rs index b4fc90e390a5..c6ef2affccad 100644 --- a/crates/storage/codecs/src/alloy/header.rs +++ b/crates/storage/codecs/src/alloy/header.rs @@ -1,7 +1,6 @@ use crate::Compact; use alloy_consensus::Header as AlloyHeader; use alloy_primitives::{Address, BlockNumber, Bloom, Bytes, B256, U256}; -use serde::{Deserialize, Serialize}; /// Block header /// @@ -11,7 +10,8 @@ use serde::{Deserialize, Serialize}; /// will automatically apply to this type. /// /// Notice: Make sure this struct is 1:1 with [`alloy_consensus::Header`] -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize, Compact)] +#[cfg_attr(test, derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)] pub(crate) struct Header { parent_hash: B256, ommers_hash: B256, @@ -42,7 +42,8 @@ pub(crate) struct Header { /// used as a field of [`Header`] for backwards compatibility. /// /// More information: & [`reth_codecs_derive::Compact`]. -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize, Compact)] +#[cfg_attr(test, derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)] pub(crate) struct HeaderExt { requests_root: Option, } diff --git a/crates/storage/codecs/src/alloy/transaction/eip1559.rs b/crates/storage/codecs/src/alloy/transaction/eip1559.rs index d2113a736ed2..9c98bf300e69 100644 --- a/crates/storage/codecs/src/alloy/transaction/eip1559.rs +++ b/crates/storage/codecs/src/alloy/transaction/eip1559.rs @@ -2,8 +2,6 @@ use crate::Compact; use alloy_consensus::TxEip1559 as AlloyTxEip1559; use alloy_eips::eip2930::AccessList; use alloy_primitives::{Bytes, ChainId, TxKind, U256}; -use serde::{Deserialize, Serialize}; - /// [EIP-1559 Transaction](https://eips.ethereum.org/EIPS/eip-1559) /// /// This is a helper type to use derive on it instead of manually managing `bitfield`. @@ -12,8 +10,8 @@ use serde::{Deserialize, Serialize}; /// will automatically apply to this type. /// /// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip1559`] -#[derive(Debug, Clone, PartialEq, Eq, Hash, Compact, Default, Serialize, Deserialize)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Compact, Default)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[cfg_attr(test, crate::add_arbitrary_tests(compact))] pub(crate) struct TxEip1559 { chain_id: ChainId, diff --git a/crates/storage/codecs/src/alloy/transaction/eip2930.rs b/crates/storage/codecs/src/alloy/transaction/eip2930.rs index b8f24db747e7..4717884d9252 100644 --- a/crates/storage/codecs/src/alloy/transaction/eip2930.rs +++ b/crates/storage/codecs/src/alloy/transaction/eip2930.rs @@ -3,7 +3,6 @@ use alloy_consensus::TxEip2930 as AlloyTxEip2930; use alloy_eips::eip2930::AccessList; use alloy_primitives::{Bytes, ChainId, TxKind, U256}; use reth_codecs_derive::add_arbitrary_tests; -use serde::{Deserialize, Serialize}; /// Transaction with an [`AccessList`] ([EIP-2930](https://eips.ethereum.org/EIPS/eip-2930)). /// @@ -13,8 +12,8 @@ use serde::{Deserialize, Serialize}; /// will automatically apply to this type. /// /// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip2930`] -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize, Compact)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[add_arbitrary_tests(compact)] pub(crate) struct TxEip2930 { chain_id: ChainId, diff --git a/crates/storage/codecs/src/alloy/transaction/eip4844.rs b/crates/storage/codecs/src/alloy/transaction/eip4844.rs index e82b9affff47..9f58001fd371 100644 --- a/crates/storage/codecs/src/alloy/transaction/eip4844.rs +++ b/crates/storage/codecs/src/alloy/transaction/eip4844.rs @@ -4,7 +4,6 @@ use alloy_consensus::TxEip4844 as AlloyTxEip4844; use alloy_eips::eip2930::AccessList; use alloy_primitives::{Address, Bytes, ChainId, B256, U256}; use reth_codecs_derive::add_arbitrary_tests; -use serde::{Deserialize, Serialize}; /// [EIP-4844 Blob Transaction](https://eips.ethereum.org/EIPS/eip-4844#blob-transaction) /// @@ -14,8 +13,8 @@ use serde::{Deserialize, Serialize}; /// will automatically apply to this type. /// /// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip4844`] -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize, Compact)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[add_arbitrary_tests(compact)] pub(crate) struct TxEip4844 { chain_id: ChainId, diff --git a/crates/storage/codecs/src/alloy/transaction/eip7702.rs b/crates/storage/codecs/src/alloy/transaction/eip7702.rs index 5f34ac1c253e..2cc0786b1bda 100644 --- a/crates/storage/codecs/src/alloy/transaction/eip7702.rs +++ b/crates/storage/codecs/src/alloy/transaction/eip7702.rs @@ -4,7 +4,6 @@ use alloy_consensus::TxEip7702 as AlloyTxEip7702; use alloy_eips::{eip2930::AccessList, eip7702::SignedAuthorization}; use alloy_primitives::{Address, Bytes, ChainId, U256}; use reth_codecs_derive::add_arbitrary_tests; -use serde::{Deserialize, Serialize}; /// [EIP-7702 Set Code Transaction](https://eips.ethereum.org/EIPS/eip-7702) /// @@ -14,8 +13,8 @@ use serde::{Deserialize, Serialize}; /// will automatically apply to this type. /// /// Notice: Make sure this struct is 1:1 with [`alloy_consensus::TxEip7702`] -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize, Compact)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[add_arbitrary_tests(compact)] pub(crate) struct TxEip7702 { chain_id: ChainId, diff --git a/crates/storage/codecs/src/alloy/transaction/legacy.rs b/crates/storage/codecs/src/alloy/transaction/legacy.rs index 641b27bf53b2..6be6204297c7 100644 --- a/crates/storage/codecs/src/alloy/transaction/legacy.rs +++ b/crates/storage/codecs/src/alloy/transaction/legacy.rs @@ -1,11 +1,10 @@ use crate::Compact; use alloy_consensus::TxLegacy as AlloyTxLegacy; use alloy_primitives::{Bytes, ChainId, TxKind, U256}; -use serde::{Deserialize, Serialize}; /// Legacy transaction. -#[derive(Debug, Clone, PartialEq, Eq, Default, Compact, Serialize, Deserialize)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[cfg_attr(test, crate::add_arbitrary_tests(compact))] pub(crate) struct TxLegacy { /// Added as EIP-155: Simple replay attack protection diff --git a/crates/storage/codecs/src/alloy/transaction/optimism.rs b/crates/storage/codecs/src/alloy/transaction/optimism.rs index c84b19559fd5..0332c1a125cb 100644 --- a/crates/storage/codecs/src/alloy/transaction/optimism.rs +++ b/crates/storage/codecs/src/alloy/transaction/optimism.rs @@ -2,7 +2,6 @@ use crate::Compact; use alloy_primitives::{Address, Bytes, TxKind, B256, U256}; use op_alloy_consensus::TxDeposit as AlloyTxDeposit; use reth_codecs_derive::add_arbitrary_tests; -use serde::{Deserialize, Serialize}; /// Deposit transactions, also known as deposits are initiated on L1, and executed on L2. /// @@ -12,8 +11,8 @@ use serde::{Deserialize, Serialize}; /// will automatically apply to this type. /// /// Notice: Make sure this struct is 1:1 with [`op_alloy_consensus::TxDeposit`] -#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Serialize, Deserialize, Compact)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[add_arbitrary_tests(compact)] pub(crate) struct TxDeposit { source_hash: B256, diff --git a/crates/storage/codecs/src/alloy/withdrawal.rs b/crates/storage/codecs/src/alloy/withdrawal.rs index 0ec1693210c0..16324c280cc2 100644 --- a/crates/storage/codecs/src/alloy/withdrawal.rs +++ b/crates/storage/codecs/src/alloy/withdrawal.rs @@ -2,13 +2,12 @@ use crate::Compact; use alloy_eips::eip4895::Withdrawal as AlloyWithdrawal; use alloy_primitives::Address; use reth_codecs_derive::add_arbitrary_tests; -use serde::{Deserialize, Serialize}; /// Withdrawal acts as bridge which simplifies Compact implementation for AlloyWithdrawal. /// /// Notice: Make sure this struct is 1:1 with `alloy_eips::eip4895::Withdrawal` -#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, Compact)] -#[cfg_attr(test, derive(arbitrary::Arbitrary))] +#[derive(Debug, Clone, PartialEq, Eq, Default, Compact)] +#[cfg_attr(test, derive(arbitrary::Arbitrary, serde::Serialize, serde::Deserialize))] #[add_arbitrary_tests(compact)] pub(crate) struct Withdrawal { /// Monotonically increasing identifier issued by consensus layer. diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index e326ad6e1d13..9bdd1a4600a9 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -151,7 +151,9 @@ impl StaticFileProviderFactory for DatabaseProvider { } } -impl ChainSpecProvider for DatabaseProvider { +impl ChainSpecProvider + for DatabaseProvider +{ type ChainSpec = Spec; fn chain_spec(&self) -> Arc { diff --git a/crates/storage/provider/src/providers/static_file/manager.rs b/crates/storage/provider/src/providers/static_file/manager.rs index 77c87664b72c..9e6e925993d4 100644 --- a/crates/storage/provider/src/providers/static_file/manager.rs +++ b/crates/storage/provider/src/providers/static_file/manager.rs @@ -533,15 +533,16 @@ impl StaticFileProvider { }) .or_insert_with(|| BTreeMap::from([(tx_end, current_block_range)])); } - } else if tx_index.get(&segment).map(|index| index.len()) == Some(1) { - // Only happens if we unwind all the txs/receipts from the first static file. - // Should only happen in test scenarios. - if jar.user_header().expected_block_start() == 0 && - matches!( - segment, - StaticFileSegment::Receipts | StaticFileSegment::Transactions - ) - { + } else if segment.is_tx_based() { + // The unwinded file has no more transactions/receipts. However, the highest + // block is within this files' block range. We only retain + // entries with block ranges before the current one. + tx_index.entry(segment).and_modify(|index| { + index.retain(|_, block_range| block_range.start() < fixed_range.start()); + }); + + // If the index is empty, just remove it. + if tx_index.get(&segment).is_some_and(|index| index.is_empty()) { tx_index.remove(&segment); } } @@ -1145,11 +1146,17 @@ impl StaticFileProvider { Ok(data) } - #[cfg(any(test, feature = "test-utils"))] /// Returns `static_files` directory + #[cfg(any(test, feature = "test-utils"))] pub fn path(&self) -> &Path { &self.path } + + /// Returns `static_files` transaction index + #[cfg(any(test, feature = "test-utils"))] + pub fn tx_index(&self) -> &RwLock { + &self.static_files_tx_index + } } /// Helper trait to manage different [`StaticFileProviderRW`] of an `Arc 0 { if segment.is_receipts() { - writer.append_receipt(*next_tx_num, &Receipt::default()).unwrap(); + // Used as ID for validation + receipt.cumulative_gas_used = *next_tx_num; + writer.append_receipt(*next_tx_num, &receipt).unwrap(); } else { - writer - .append_transaction(*next_tx_num, &TransactionSignedNoHash::default()) - .unwrap(); + // Used as ID for validation + tx.transaction.set_nonce(*next_tx_num); + writer.append_transaction(*next_tx_num, &tx).unwrap(); } *next_tx_num += 1; tx_count -= 1; @@ -376,10 +390,19 @@ mod tests { expected_tx_range.as_ref() ); }); + + // Ensure transaction index + let tx_index = sf_rw.tx_index().read(); + let expected_tx_index = + vec![(8, SegmentRangeInclusive::new(0, 9)), (9, SegmentRangeInclusive::new(20, 29))]; + assert_eq!( + tx_index.get(&segment).map(|index| index.iter().map(|(k, v)| (*k, *v)).collect()), + (!expected_tx_index.is_empty()).then_some(expected_tx_index), + "tx index mismatch", + ); } #[test] - #[ignore] fn test_tx_based_truncation() { let segments = [StaticFileSegment::Transactions, StaticFileSegment::Receipts]; let blocks_per_file = 10; // Number of blocks per file @@ -387,14 +410,16 @@ mod tests { let file_set_count = 3; // Number of sets of files to create let initial_file_count = files_per_range * file_set_count + 1; // Includes lockfile + #[allow(clippy::too_many_arguments)] fn prune_and_validate( sf_rw: &StaticFileProvider, static_dir: impl AsRef, segment: StaticFileSegment, prune_count: u64, last_block: u64, - expected_tx_tip: u64, + expected_tx_tip: Option, expected_file_count: i32, + expected_tx_index: Vec<(TxNumber, SegmentRangeInclusive)>, ) -> eyre::Result<()> { let mut writer = sf_rw.latest_writer(segment)?; @@ -412,11 +437,25 @@ mod tests { Some(last_block), "block mismatch", )?; - assert_eyre( - sf_rw.get_highest_static_file_tx(segment), - Some(expected_tx_tip), - "tx mismatch", - )?; + assert_eyre(sf_rw.get_highest_static_file_tx(segment), expected_tx_tip, "tx mismatch")?; + + // Verify that transactions and receipts are returned correctly. Uses + // cumulative_gas_used & nonce as ids. + if let Some(id) = expected_tx_tip { + if segment.is_receipts() { + assert_eyre( + expected_tx_tip, + sf_rw.receipt(id)?.map(|r| r.cumulative_gas_used), + "tx mismatch", + )?; + } else { + assert_eyre( + expected_tx_tip, + sf_rw.transaction_by_id(id)?.map(|t| t.nonce()), + "tx mismatch", + )?; + } + } // Ensure the file count has reduced as expected assert_eyre( @@ -424,6 +463,15 @@ mod tests { expected_file_count as usize, "file count mismatch", )?; + + // Ensure that the inner tx index (max_tx -> block range) is as expected + let tx_index = sf_rw.tx_index().read(); + assert_eyre( + tx_index.get(&segment).map(|index| index.iter().map(|(k, v)| (*k, *v)).collect()), + (!expected_tx_index.is_empty()).then_some(expected_tx_index), + "tx index mismatch", + )?; + Ok(()) } @@ -442,26 +490,46 @@ mod tests { let highest_tx = sf_rw.get_highest_static_file_tx(segment).unwrap(); // Test cases - // [prune_count, last_block, expected_tx_tip, expected_file_count) + // [prune_count, last_block, expected_tx_tip, expected_file_count, expected_tx_index) let test_cases = vec![ // Case 0: 20..=29 has only one tx. Prune the only tx of the block range. // It ensures that the file is not deleted even though there are no rows, since the // `last_block` which is passed to the prune method is the first // block of the range. - (1, blocks_per_file * 2, highest_tx - 1, initial_file_count), + ( + 1, + blocks_per_file * 2, + Some(highest_tx - 1), + initial_file_count, + vec![(highest_tx - 1, SegmentRangeInclusive::new(0, 9))], + ), // Case 1: 10..=19 has no txs. There are no txes in the whole block range, but want // to unwind to block 9. Ensures that the 20..=29 and 10..=19 files // are deleted. - (0, blocks_per_file - 1, highest_tx - 1, files_per_range + 1), // includes lockfile + ( + 0, + blocks_per_file - 1, + Some(highest_tx - 1), + files_per_range + 1, // includes lockfile + vec![(highest_tx - 1, SegmentRangeInclusive::new(0, 9))], + ), // Case 2: Prune most txs up to block 1. - (7, 1, 1, files_per_range + 1), + ( + highest_tx - 1, + 1, + Some(0), + files_per_range + 1, + vec![(0, SegmentRangeInclusive::new(0, 1))], + ), // Case 3: Prune remaining tx and ensure that file is not deleted. - (1, 0, 0, files_per_range + 1), + (1, 0, None, files_per_range + 1, vec![]), ]; // Loop through test cases - for (case, (prune_count, last_block, expected_tx_tip, expected_file_count)) in - test_cases.into_iter().enumerate() + for ( + case, + (prune_count, last_block, expected_tx_tip, expected_file_count, expected_tx_index), + ) in test_cases.into_iter().enumerate() { prune_and_validate( &sf_rw, @@ -471,6 +539,7 @@ mod tests { last_block, expected_tx_tip, expected_file_count, + expected_tx_index, ) .map_err(|err| eyre::eyre!("Test case {case}: {err}")) .unwrap(); diff --git a/crates/storage/provider/src/providers/static_file/writer.rs b/crates/storage/provider/src/providers/static_file/writer.rs index b62fc22c730a..d086c5693ca5 100644 --- a/crates/storage/provider/src/providers/static_file/writer.rs +++ b/crates/storage/provider/src/providers/static_file/writer.rs @@ -381,8 +381,9 @@ impl StaticFileProviderRW { /// Commits to the configuration file at the end. fn truncate(&mut self, num_rows: u64, last_block: Option) -> ProviderResult<()> { let mut remaining_rows = num_rows; + let segment = self.writer.user_header().segment(); while remaining_rows > 0 { - let len = match self.writer.user_header().segment() { + let len = match segment { StaticFileSegment::Headers => { self.writer.user_header().block_len().unwrap_or_default() } @@ -396,7 +397,14 @@ impl StaticFileProviderRW { // delete the whole file and go to the next static file let block_start = self.writer.user_header().expected_block_start(); - if block_start != 0 { + // We only delete the file if it's NOT the first static file AND: + // * it's a Header segment OR + // * it's a tx-based segment AND `last_block` is lower than the first block of this + // file's block range. Otherwise, having no rows simply means that this block + // range has no transactions, but the file should remain. + if block_start != 0 && + (segment.is_headers() || last_block.is_some_and(|b| b < block_start)) + { self.delete_current_and_open_previous()?; } else { // Update `SegmentHeader` diff --git a/crates/storage/provider/src/test_utils/blocks.rs b/crates/storage/provider/src/test_utils/blocks.rs index 352f5314af9f..6f6aae992446 100644 --- a/crates/storage/provider/src/test_utils/blocks.rs +++ b/crates/storage/provider/src/test_utils/blocks.rs @@ -1,14 +1,14 @@ //! Dummy blocks and data for tests use crate::{DatabaseProviderRW, ExecutionOutcome}; use alloy_consensus::TxLegacy; -use alloy_primitives::{Log, Parity, Sealable, TxKind}; +use alloy_primitives::{Address, BlockNumber, Log, Parity, Sealable, TxKind}; use once_cell::sync::Lazy; use reth_db::tables; use reth_db_api::{database::Database, models::StoredBlockBodyIndices}; use reth_primitives::{ - alloy_primitives, b256, hex_literal::hex, Account, Address, BlockBody, BlockNumber, Bytes, - Header, Receipt, SealedBlock, SealedBlockWithSenders, SealedHeader, Signature, Transaction, - TransactionSigned, TxType, Withdrawal, Withdrawals, B256, U256, + alloy_primitives, b256, hex_literal::hex, Account, BlockBody, Bytes, Header, Receipt, + SealedBlock, SealedBlockWithSenders, SealedHeader, Signature, Transaction, TransactionSigned, + TxType, Withdrawal, Withdrawals, B256, U256, }; use reth_trie::root::{state_root_unhashed, storage_root_unhashed}; use revm::{ diff --git a/crates/storage/provider/src/writer/mod.rs b/crates/storage/provider/src/writer/mod.rs index 17bf9db81bd4..d96e722d7b40 100644 --- a/crates/storage/provider/src/writer/mod.rs +++ b/crates/storage/provider/src/writer/mod.rs @@ -544,14 +544,14 @@ mod tests { use crate::{ test_utils::create_test_provider_factory, AccountReader, StorageTrieWriter, TrieWriter, }; - use alloy_primitives::{keccak256, B256, U256}; + use alloy_primitives::{keccak256, Address, B256, U256}; use reth_db::tables; use reth_db_api::{ cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO}, models::{AccountBeforeTx, BlockNumberAddress}, transaction::{DbTx, DbTxMut}, }; - use reth_primitives::{Account, Address, Receipt, Receipts, StorageEntry}; + use reth_primitives::{Account, Receipt, Receipts, StorageEntry}; use reth_storage_api::DatabaseProviderFactory; use reth_trie::{ test_utils::{state_root, storage_root_prehashed}, diff --git a/crates/storage/storage-api/src/noop.rs b/crates/storage/storage-api/src/noop.rs index c3c33ac37908..7325e2b74360 100644 --- a/crates/storage/storage-api/src/noop.rs +++ b/crates/storage/storage-api/src/noop.rs @@ -1,17 +1,28 @@ //! Various noop implementations for traits. +use std::sync::Arc; + use crate::{BlockHashReader, BlockNumReader}; use alloy_primitives::{BlockNumber, B256}; -use reth_chainspec::ChainInfo; +use reth_chainspec::{ChainInfo, ChainSpecProvider, EthChainSpec}; use reth_storage_errors::provider::ProviderResult; /// Supports various api interfaces for testing purposes. -#[derive(Debug, Clone, Default, Copy)] +#[derive(Debug, Clone)] #[non_exhaustive] -pub struct NoopBlockReader; +pub struct NoopBlockReader { + chain_spec: Arc, +} + +impl NoopBlockReader { + /// Create a new instance of the `NoopBlockReader`. + pub const fn new(chain_spec: Arc) -> Self { + Self { chain_spec } + } +} /// Noop implementation for testing purposes -impl BlockHashReader for NoopBlockReader { +impl BlockHashReader for NoopBlockReader { fn block_hash(&self, _number: u64) -> ProviderResult> { Ok(None) } @@ -25,7 +36,7 @@ impl BlockHashReader for NoopBlockReader { } } -impl BlockNumReader for NoopBlockReader { +impl BlockNumReader for NoopBlockReader { fn chain_info(&self) -> ProviderResult { Ok(ChainInfo::default()) } @@ -42,3 +53,11 @@ impl BlockNumReader for NoopBlockReader { Ok(None) } } + +impl ChainSpecProvider for NoopBlockReader { + type ChainSpec = ChainSpec; + + fn chain_spec(&self) -> Arc { + self.chain_spec.clone() + } +} diff --git a/crates/transaction-pool/src/maintain.rs b/crates/transaction-pool/src/maintain.rs index 5a68b16607c8..e961d03703bc 100644 --- a/crates/transaction-pool/src/maintain.rs +++ b/crates/transaction-pool/src/maintain.rs @@ -17,8 +17,8 @@ use reth_chainspec::{ChainSpec, ChainSpecProvider}; use reth_execution_types::ChangedAccount; use reth_fs_util::FsPathError; use reth_primitives::{ - alloy_primitives::Sealable, BlockNumberOrTag, IntoRecoveredTransaction, - PooledTransactionsElementEcRecovered, SealedHeader, TransactionSigned, + alloy_primitives::Sealable, BlockNumberOrTag, PooledTransactionsElementEcRecovered, + SealedHeader, TransactionSigned, }; use reth_storage_api::{errors::provider::ProviderError, BlockReaderIdExt, StateProviderFactory}; use reth_tasks::TaskSpawner; diff --git a/crates/transaction-pool/src/pool/mod.rs b/crates/transaction-pool/src/pool/mod.rs index cfe38ea31da8..2ba101721b08 100644 --- a/crates/transaction-pool/src/pool/mod.rs +++ b/crates/transaction-pool/src/pool/mod.rs @@ -87,8 +87,7 @@ use reth_eth_wire_types::HandleMempoolData; use reth_execution_types::ChangedAccount; use reth_primitives::{ - BlobTransaction, BlobTransactionSidecar, IntoRecoveredTransaction, PooledTransactionsElement, - TransactionSigned, + BlobTransaction, BlobTransactionSidecar, PooledTransactionsElement, TransactionSigned, }; use std::{ collections::{HashMap, HashSet}, diff --git a/crates/transaction-pool/src/validate/mod.rs b/crates/transaction-pool/src/validate/mod.rs index 7baa5e3f335e..80aee0afe9eb 100644 --- a/crates/transaction-pool/src/validate/mod.rs +++ b/crates/transaction-pool/src/validate/mod.rs @@ -8,8 +8,8 @@ use crate::{ use alloy_primitives::{Address, TxHash, B256, U256}; use futures_util::future::Either; use reth_primitives::{ - BlobTransactionSidecar, IntoRecoveredTransaction, PooledTransactionsElementEcRecovered, - SealedBlock, TransactionSignedEcRecovered, + BlobTransactionSidecar, PooledTransactionsElementEcRecovered, SealedBlock, + TransactionSignedEcRecovered, }; use std::{fmt, future::Future, time::Instant}; @@ -380,10 +380,11 @@ impl ValidPoolTransaction { } } -impl> IntoRecoveredTransaction - for ValidPoolTransaction -{ - fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered { +impl> ValidPoolTransaction { + /// Converts to this type into a [`TransactionSignedEcRecovered`]. + /// + /// Note: this takes `&self` since indented usage is via `Arc`. + pub fn to_recovered_transaction(&self) -> TransactionSignedEcRecovered { self.transaction.clone().into_consensus() } } diff --git a/crates/trie/db/src/state.rs b/crates/trie/db/src/state.rs index 4f27679ddad9..9cefc6b9fa83 100644 --- a/crates/trie/db/src/state.rs +++ b/crates/trie/db/src/state.rs @@ -267,9 +267,10 @@ impl DatabaseHashedPostState for HashedPostState { #[cfg(test)] mod tests { use super::*; + use alloy_primitives::Address; use reth_db::test_utils::create_test_rw_db; use reth_db_api::database::Database; - use reth_primitives::{hex, revm_primitives::AccountInfo, Address, U256}; + use reth_primitives::{hex, revm_primitives::AccountInfo, U256}; use revm::db::BundleState; use std::collections::HashMap; diff --git a/crates/trie/parallel/Cargo.toml b/crates/trie/parallel/Cargo.toml index 80fa0a70d0e1..64a4644bdce4 100644 --- a/crates/trie/parallel/Cargo.toml +++ b/crates/trie/parallel/Cargo.toml @@ -31,13 +31,8 @@ tracing.workspace = true # misc thiserror.workspace = true derive_more.workspace = true - -# `async` feature -tokio = { workspace = true, optional = true, default-features = false } -itertools = { workspace = true, optional = true } - -# `parallel` feature -rayon = { workspace = true, optional = true } +rayon.workspace = true +itertools.workspace = true # `metrics` feature reth-metrics = { workspace = true, optional = true } @@ -58,12 +53,9 @@ proptest.workspace = true proptest-arbitrary-interop.workspace = true [features] -default = ["metrics", "async", "parallel"] +default = ["metrics"] metrics = ["reth-metrics", "dep:metrics", "reth-trie/metrics"] -async = ["tokio/sync", "itertools"] -parallel = ["rayon"] [[bench]] name = "root" -required-features = ["async", "parallel"] harness = false diff --git a/crates/trie/parallel/benches/root.rs b/crates/trie/parallel/benches/root.rs index e09fb9341389..d1ffe49dd0ad 100644 --- a/crates/trie/parallel/benches/root.rs +++ b/crates/trie/parallel/benches/root.rs @@ -13,7 +13,7 @@ use reth_trie::{ TrieInput, }; use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseStateRoot}; -use reth_trie_parallel::{async_root::AsyncStateRoot, parallel_root::ParallelStateRoot}; +use reth_trie_parallel::parallel_root::ParallelStateRoot; use std::collections::HashMap; pub fn calculate_state_root(c: &mut Criterion) { @@ -70,14 +70,6 @@ pub fn calculate_state_root(c: &mut Criterion) { |calculator| async { calculator.incremental_root() }, ); }); - - // async root - group.bench_function(BenchmarkId::new("async root", size), |b| { - b.iter_with_setup( - || AsyncStateRoot::new(view.clone(), TrieInput::from_state(updated_state.clone())), - |calculator| calculator.incremental_root(), - ); - }); } } diff --git a/crates/trie/parallel/src/async_root.rs b/crates/trie/parallel/src/async_root.rs deleted file mode 100644 index b6b57725cb70..000000000000 --- a/crates/trie/parallel/src/async_root.rs +++ /dev/null @@ -1,328 +0,0 @@ -#[cfg(feature = "metrics")] -use crate::metrics::ParallelStateRootMetrics; -use crate::{stats::ParallelTrieTracker, storage_root_targets::StorageRootTargets}; -use alloy_primitives::B256; -use alloy_rlp::{BufMut, Encodable}; -use itertools::Itertools; -use reth_execution_errors::StorageRootError; -use reth_provider::{ - providers::ConsistentDbView, BlockReader, DBProvider, DatabaseProviderFactory, ProviderError, -}; -use reth_trie::{ - hashed_cursor::{HashedCursorFactory, HashedPostStateCursorFactory}, - node_iter::{TrieElement, TrieNodeIter}, - trie_cursor::{InMemoryTrieCursorFactory, TrieCursorFactory}, - updates::TrieUpdates, - walker::TrieWalker, - HashBuilder, Nibbles, StorageRoot, TrieAccount, TrieInput, -}; -use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory}; -use std::{collections::HashMap, sync::Arc}; -use thiserror::Error; -use tracing::*; - -/// Async state root calculator. -/// -/// The calculator starts off by launching tasks to compute storage roots. -/// Then, it immediately starts walking the state trie updating the necessary trie -/// nodes in the process. Upon encountering a leaf node, it will poll the storage root -/// task for the corresponding hashed address. -/// -/// Internally, the calculator uses [`ConsistentDbView`] since -/// it needs to rely on database state saying the same until -/// the last transaction is open. -/// See docs of using [`ConsistentDbView`] for caveats. -/// -/// For sync usage, take a look at `ParallelStateRoot`. -#[derive(Debug)] -pub struct AsyncStateRoot { - /// Consistent view of the database. - view: ConsistentDbView, - /// Trie input. - input: TrieInput, - /// Parallel state root metrics. - #[cfg(feature = "metrics")] - metrics: ParallelStateRootMetrics, -} - -impl AsyncStateRoot { - /// Create new async state root calculator. - pub fn new(view: ConsistentDbView, input: TrieInput) -> Self { - Self { - view, - input, - #[cfg(feature = "metrics")] - metrics: ParallelStateRootMetrics::default(), - } - } -} - -impl AsyncStateRoot -where - Factory: DatabaseProviderFactory + Clone + Send + Sync + 'static, -{ - /// Calculate incremental state root asynchronously. - pub fn incremental_root(self) -> Result { - self.calculate(false).map(|(root, _)| root) - } - - /// Calculate incremental state root with updates asynchronously. - pub fn incremental_root_with_updates(self) -> Result<(B256, TrieUpdates), AsyncStateRootError> { - self.calculate(true) - } - - fn calculate(self, retain_updates: bool) -> Result<(B256, TrieUpdates), AsyncStateRootError> { - let mut tracker = ParallelTrieTracker::default(); - let trie_nodes_sorted = Arc::new(self.input.nodes.into_sorted()); - let hashed_state_sorted = Arc::new(self.input.state.into_sorted()); - let prefix_sets = self.input.prefix_sets.freeze(); - let storage_root_targets = StorageRootTargets::new( - prefix_sets.account_prefix_set.iter().map(|nibbles| B256::from_slice(&nibbles.pack())), - prefix_sets.storage_prefix_sets, - ); - - // Pre-calculate storage roots async for accounts which were changed. - tracker.set_precomputed_storage_roots(storage_root_targets.len() as u64); - debug!(target: "trie::async_state_root", len = storage_root_targets.len(), "pre-calculating storage roots"); - let mut storage_roots = HashMap::with_capacity(storage_root_targets.len()); - for (hashed_address, prefix_set) in - storage_root_targets.into_iter().sorted_unstable_by_key(|(address, _)| *address) - { - let view = self.view.clone(); - let hashed_state_sorted = hashed_state_sorted.clone(); - let trie_nodes_sorted = trie_nodes_sorted.clone(); - #[cfg(feature = "metrics")] - let metrics = self.metrics.storage_trie.clone(); - - let (tx, rx) = std::sync::mpsc::sync_channel(1); - - rayon::spawn_fifo(move || { - let result = (|| -> Result<_, AsyncStateRootError> { - let provider_ro = view.provider_ro()?; - let trie_cursor_factory = InMemoryTrieCursorFactory::new( - DatabaseTrieCursorFactory::new(provider_ro.tx_ref()), - &trie_nodes_sorted, - ); - let hashed_state = HashedPostStateCursorFactory::new( - DatabaseHashedCursorFactory::new(provider_ro.tx_ref()), - &hashed_state_sorted, - ); - Ok(StorageRoot::new_hashed( - trie_cursor_factory, - hashed_state, - hashed_address, - #[cfg(feature = "metrics")] - metrics, - ) - .with_prefix_set(prefix_set) - .calculate(retain_updates)?) - })(); - let _ = tx.send(result); - }); - storage_roots.insert(hashed_address, rx); - } - - trace!(target: "trie::async_state_root", "calculating state root"); - let mut trie_updates = TrieUpdates::default(); - - let provider_ro = self.view.provider_ro()?; - let trie_cursor_factory = InMemoryTrieCursorFactory::new( - DatabaseTrieCursorFactory::new(provider_ro.tx_ref()), - &trie_nodes_sorted, - ); - let hashed_cursor_factory = HashedPostStateCursorFactory::new( - DatabaseHashedCursorFactory::new(provider_ro.tx_ref()), - &hashed_state_sorted, - ); - - let walker = TrieWalker::new( - trie_cursor_factory.account_trie_cursor().map_err(ProviderError::Database)?, - prefix_sets.account_prefix_set, - ) - .with_deletions_retained(retain_updates); - let mut account_node_iter = TrieNodeIter::new( - walker, - hashed_cursor_factory.hashed_account_cursor().map_err(ProviderError::Database)?, - ); - - let mut hash_builder = HashBuilder::default().with_updates(retain_updates); - let mut account_rlp = Vec::with_capacity(128); - while let Some(node) = account_node_iter.try_next().map_err(ProviderError::Database)? { - match node { - TrieElement::Branch(node) => { - hash_builder.add_branch(node.key, node.value, node.children_are_in_trie); - } - TrieElement::Leaf(hashed_address, account) => { - let (storage_root, _, updates) = match storage_roots.remove(&hashed_address) { - Some(rx) => rx.recv().map_err(|_| { - AsyncStateRootError::StorageRootChannelClosed { hashed_address } - })??, - // Since we do not store all intermediate nodes in the database, there might - // be a possibility of re-adding a non-modified leaf to the hash builder. - None => { - tracker.inc_missed_leaves(); - StorageRoot::new_hashed( - trie_cursor_factory.clone(), - hashed_cursor_factory.clone(), - hashed_address, - #[cfg(feature = "metrics")] - self.metrics.storage_trie.clone(), - ) - .calculate(retain_updates)? - } - }; - - if retain_updates { - trie_updates.insert_storage_updates(hashed_address, updates); - } - - account_rlp.clear(); - let account = TrieAccount::from((account, storage_root)); - account.encode(&mut account_rlp as &mut dyn BufMut); - hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp); - } - } - } - - let root = hash_builder.root(); - - trie_updates.finalize( - account_node_iter.walker, - hash_builder, - prefix_sets.destroyed_accounts, - ); - - let stats = tracker.finish(); - - #[cfg(feature = "metrics")] - self.metrics.record_state_trie(stats); - - trace!( - target: "trie::async_state_root", - %root, - duration = ?stats.duration(), - branches_added = stats.branches_added(), - leaves_added = stats.leaves_added(), - missed_leaves = stats.missed_leaves(), - precomputed_storage_roots = stats.precomputed_storage_roots(), - "calculated state root" - ); - - Ok((root, trie_updates)) - } -} - -/// Error during async state root calculation. -#[derive(Error, Debug)] -pub enum AsyncStateRootError { - /// Storage root channel for a given address was closed. - #[error("storage root channel for {hashed_address} got closed")] - StorageRootChannelClosed { - /// The hashed address for which channel was closed. - hashed_address: B256, - }, - /// Receive error - #[error(transparent)] - Receive(#[from] std::sync::mpsc::RecvError), - /// Error while calculating storage root. - #[error(transparent)] - StorageRoot(#[from] StorageRootError), - /// Provider error. - #[error(transparent)] - Provider(#[from] ProviderError), -} - -#[cfg(test)] -mod tests { - use super::*; - use alloy_primitives::{keccak256, Address, U256}; - use rand::Rng; - use reth_primitives::{Account, StorageEntry}; - use reth_provider::{test_utils::create_test_provider_factory, HashingWriter}; - use reth_trie::{test_utils, HashedPostState, HashedStorage}; - - #[tokio::test] - async fn random_async_root() { - let factory = create_test_provider_factory(); - let consistent_view = ConsistentDbView::new(factory.clone(), None); - - let mut rng = rand::thread_rng(); - let mut state = (0..100) - .map(|_| { - let address = Address::random(); - let account = - Account { balance: U256::from(rng.gen::()), ..Default::default() }; - let mut storage = HashMap::::default(); - let has_storage = rng.gen_bool(0.7); - if has_storage { - for _ in 0..100 { - storage.insert( - B256::from(U256::from(rng.gen::())), - U256::from(rng.gen::()), - ); - } - } - (address, (account, storage)) - }) - .collect::>(); - - { - let provider_rw = factory.provider_rw().unwrap(); - provider_rw - .insert_account_for_hashing( - state.iter().map(|(address, (account, _))| (*address, Some(*account))), - ) - .unwrap(); - provider_rw - .insert_storage_for_hashing(state.iter().map(|(address, (_, storage))| { - ( - *address, - storage - .iter() - .map(|(slot, value)| StorageEntry { key: *slot, value: *value }), - ) - })) - .unwrap(); - provider_rw.commit().unwrap(); - } - - assert_eq!( - AsyncStateRoot::new(consistent_view.clone(), Default::default(),) - .incremental_root() - .unwrap(), - test_utils::state_root(state.clone()) - ); - - let mut hashed_state = HashedPostState::default(); - for (address, (account, storage)) in &mut state { - let hashed_address = keccak256(address); - - let should_update_account = rng.gen_bool(0.5); - if should_update_account { - *account = Account { balance: U256::from(rng.gen::()), ..*account }; - hashed_state.accounts.insert(hashed_address, Some(*account)); - } - - let should_update_storage = rng.gen_bool(0.3); - if should_update_storage { - for (slot, value) in storage.iter_mut() { - let hashed_slot = keccak256(slot); - *value = U256::from(rng.gen::()); - hashed_state - .storages - .entry(hashed_address) - .or_insert_with(|| HashedStorage::new(false)) - .storage - .insert(hashed_slot, *value); - } - } - } - - assert_eq!( - AsyncStateRoot::new(consistent_view, TrieInput::from_state(hashed_state)) - .incremental_root() - .unwrap(), - test_utils::state_root(state) - ); - } -} diff --git a/crates/trie/parallel/src/lib.rs b/crates/trie/parallel/src/lib.rs index ff130b2187e7..40a6af347580 100644 --- a/crates/trie/parallel/src/lib.rs +++ b/crates/trie/parallel/src/lib.rs @@ -13,12 +13,7 @@ pub use storage_root_targets::StorageRootTargets; /// Parallel trie calculation stats. pub mod stats; -/// Implementation of async state root computation. -#[cfg(feature = "async")] -pub mod async_root; - /// Implementation of parallel state root computation. -#[cfg(feature = "parallel")] pub mod parallel_root; /// Parallel state root metrics. diff --git a/crates/trie/parallel/src/parallel_root.rs b/crates/trie/parallel/src/parallel_root.rs index e63c3f1a17b3..a64b8351446e 100644 --- a/crates/trie/parallel/src/parallel_root.rs +++ b/crates/trie/parallel/src/parallel_root.rs @@ -1,10 +1,10 @@ #[cfg(feature = "metrics")] use crate::metrics::ParallelStateRootMetrics; use crate::{stats::ParallelTrieTracker, storage_root_targets::StorageRootTargets}; +use alloy_primitives::B256; use alloy_rlp::{BufMut, Encodable}; -use rayon::prelude::*; +use itertools::Itertools; use reth_execution_errors::StorageRootError; -use reth_primitives::B256; use reth_provider::{ providers::ConsistentDbView, BlockReader, DBProvider, DatabaseProviderFactory, ProviderError, }; @@ -17,22 +17,21 @@ use reth_trie::{ HashBuilder, Nibbles, StorageRoot, TrieAccount, TrieInput, }; use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory}; -use std::collections::HashMap; +use std::{collections::HashMap, sync::Arc}; use thiserror::Error; use tracing::*; /// Parallel incremental state root calculator. /// -/// The calculator starts off by pre-computing storage roots of changed -/// accounts in parallel. Once that's done, it proceeds to walking the state -/// trie retrieving the pre-computed storage roots when needed. +/// The calculator starts off by launching tasks to compute storage roots. +/// Then, it immediately starts walking the state trie updating the necessary trie +/// nodes in the process. Upon encountering a leaf node, it will poll the storage root +/// task for the corresponding hashed address. /// /// Internally, the calculator uses [`ConsistentDbView`] since /// it needs to rely on database state saying the same until /// the last transaction is open. /// See docs of using [`ConsistentDbView`] for caveats. -/// -/// If possible, use more optimized `AsyncStateRoot` instead. #[derive(Debug)] pub struct ParallelStateRoot { /// Consistent view of the database. @@ -58,7 +57,7 @@ impl ParallelStateRoot { impl ParallelStateRoot where - Factory: DatabaseProviderFactory + Send + Sync, + Factory: DatabaseProviderFactory + Clone + Send + Sync + 'static, { /// Calculate incremental state root in parallel. pub fn incremental_root(self) -> Result { @@ -77,8 +76,8 @@ where retain_updates: bool, ) -> Result<(B256, TrieUpdates), ParallelStateRootError> { let mut tracker = ParallelTrieTracker::default(); - let trie_nodes_sorted = self.input.nodes.into_sorted(); - let hashed_state_sorted = self.input.state.into_sorted(); + let trie_nodes_sorted = Arc::new(self.input.nodes.into_sorted()); + let hashed_state_sorted = Arc::new(self.input.state.into_sorted()); let prefix_sets = self.input.prefix_sets.freeze(); let storage_root_targets = StorageRootTargets::new( prefix_sets.account_prefix_set.iter().map(|nibbles| B256::from_slice(&nibbles.pack())), @@ -88,30 +87,43 @@ where // Pre-calculate storage roots in parallel for accounts which were changed. tracker.set_precomputed_storage_roots(storage_root_targets.len() as u64); debug!(target: "trie::parallel_state_root", len = storage_root_targets.len(), "pre-calculating storage roots"); - let mut storage_roots = storage_root_targets - .into_par_iter() - .map(|(hashed_address, prefix_set)| { - let provider_ro = self.view.provider_ro()?; - let trie_cursor_factory = InMemoryTrieCursorFactory::new( - DatabaseTrieCursorFactory::new(provider_ro.tx_ref()), - &trie_nodes_sorted, - ); - let hashed_cursor_factory = HashedPostStateCursorFactory::new( - DatabaseHashedCursorFactory::new(provider_ro.tx_ref()), - &hashed_state_sorted, - ); - let storage_root_result = StorageRoot::new_hashed( - trie_cursor_factory, - hashed_cursor_factory, - hashed_address, - #[cfg(feature = "metrics")] - self.metrics.storage_trie.clone(), - ) - .with_prefix_set(prefix_set) - .calculate(retain_updates); - Ok((hashed_address, storage_root_result?)) - }) - .collect::, ParallelStateRootError>>()?; + let mut storage_roots = HashMap::with_capacity(storage_root_targets.len()); + for (hashed_address, prefix_set) in + storage_root_targets.into_iter().sorted_unstable_by_key(|(address, _)| *address) + { + let view = self.view.clone(); + let hashed_state_sorted = hashed_state_sorted.clone(); + let trie_nodes_sorted = trie_nodes_sorted.clone(); + #[cfg(feature = "metrics")] + let metrics = self.metrics.storage_trie.clone(); + + let (tx, rx) = std::sync::mpsc::sync_channel(1); + + rayon::spawn_fifo(move || { + let result = (|| -> Result<_, ParallelStateRootError> { + let provider_ro = view.provider_ro()?; + let trie_cursor_factory = InMemoryTrieCursorFactory::new( + DatabaseTrieCursorFactory::new(provider_ro.tx_ref()), + &trie_nodes_sorted, + ); + let hashed_state = HashedPostStateCursorFactory::new( + DatabaseHashedCursorFactory::new(provider_ro.tx_ref()), + &hashed_state_sorted, + ); + Ok(StorageRoot::new_hashed( + trie_cursor_factory, + hashed_state, + hashed_address, + #[cfg(feature = "metrics")] + metrics, + ) + .with_prefix_set(prefix_set) + .calculate(retain_updates)?) + })(); + let _ = tx.send(result); + }); + storage_roots.insert(hashed_address, rx); + } trace!(target: "trie::parallel_state_root", "calculating state root"); let mut trie_updates = TrieUpdates::default(); @@ -145,7 +157,13 @@ where } TrieElement::Leaf(hashed_address, account) => { let (storage_root, _, updates) = match storage_roots.remove(&hashed_address) { - Some(result) => result, + Some(rx) => rx.recv().map_err(|_| { + ParallelStateRootError::StorageRoot(StorageRootError::Database( + reth_db::DatabaseError::Other(format!( + "channel closed for {hashed_address}" + )), + )) + })??, // Since we do not store all intermediate nodes in the database, there might // be a possibility of re-adding a non-modified leaf to the hash builder. None => { diff --git a/crates/trie/parallel/src/storage_root_targets.rs b/crates/trie/parallel/src/storage_root_targets.rs index 8325fbcf7205..9b52d49afc80 100644 --- a/crates/trie/parallel/src/storage_root_targets.rs +++ b/crates/trie/parallel/src/storage_root_targets.rs @@ -36,7 +36,6 @@ impl IntoIterator for StorageRootTargets { } } -#[cfg(feature = "parallel")] impl rayon::iter::IntoParallelIterator for StorageRootTargets { type Iter = rayon::collections::hash_map::IntoIter; type Item = (B256, PrefixSet); diff --git a/examples/bsc-p2p/src/main.rs b/examples/bsc-p2p/src/main.rs index 7756728aa9df..e46ea4bec357 100644 --- a/examples/bsc-p2p/src/main.rs +++ b/examples/bsc-p2p/src/main.rs @@ -49,9 +49,8 @@ async fn main() { // The network configuration let mut net_cfg = NetworkConfig::builder(secret_key) - .chain_spec(bsc_chain_spec()) .listener_addr(local_addr) - .build_with_noop_provider() + .build_with_noop_provider(bsc_chain_spec()) .set_discovery_v4( Discv4ConfigBuilder::default() .add_boot_nodes(boot_nodes()) diff --git a/examples/custom-engine-types/Cargo.toml b/examples/custom-engine-types/Cargo.toml index 9a949b8367fd..f826451d2038 100644 --- a/examples/custom-engine-types/Cargo.toml +++ b/examples/custom-engine-types/Cargo.toml @@ -18,6 +18,7 @@ reth-node-ethereum = { workspace = true, features = ["test-utils"] } reth-tracing.workspace = true alloy-genesis.workspace = true alloy-rpc-types = { workspace = true, features = ["engine"] } +alloy-primitives.workspace = true eyre.workspace = true tokio.workspace = true diff --git a/examples/custom-engine-types/src/main.rs b/examples/custom-engine-types/src/main.rs index 5e0503701ab9..c4f640e9b87f 100644 --- a/examples/custom-engine-types/src/main.rs +++ b/examples/custom-engine-types/src/main.rs @@ -23,6 +23,7 @@ use serde::{Deserialize, Serialize}; use thiserror::Error; use alloy_genesis::Genesis; +use alloy_primitives::Address; use alloy_rpc_types::{ engine::{ ExecutionPayloadEnvelopeV2, ExecutionPayloadEnvelopeV3, ExecutionPayloadEnvelopeV4, @@ -63,7 +64,7 @@ use reth_payload_builder::{ EthBuiltPayload, EthPayloadBuilderAttributes, PayloadBuilderError, PayloadBuilderHandle, PayloadBuilderService, }; -use reth_primitives::{Address, Withdrawals, B256}; +use reth_primitives::{Withdrawals, B256}; use reth_tracing::{RethTracer, Tracer}; /// A custom payload attributes type. diff --git a/examples/custom-evm/Cargo.toml b/examples/custom-evm/Cargo.toml index 7642dc80cf2f..53563ab9575b 100644 --- a/examples/custom-evm/Cargo.toml +++ b/examples/custom-evm/Cargo.toml @@ -15,6 +15,7 @@ reth-primitives.workspace = true reth-node-ethereum = { workspace = true, features = ["test-utils"] } reth-tracing.workspace = true alloy-genesis.workspace = true +alloy-primitives.workspace = true eyre.workspace = true tokio.workspace = true diff --git a/examples/custom-evm/src/main.rs b/examples/custom-evm/src/main.rs index 3a93d85ad6fb..4f1c0c7eb299 100644 --- a/examples/custom-evm/src/main.rs +++ b/examples/custom-evm/src/main.rs @@ -3,6 +3,7 @@ #![cfg_attr(not(test), warn(unused_crate_dependencies))] use alloy_genesis::Genesis; +use alloy_primitives::Address; use reth::{ builder::{ components::{ExecutorBuilder, PayloadServiceBuilder}, @@ -38,7 +39,7 @@ use reth_node_ethereum::{ }; use reth_primitives::{ revm_primitives::{CfgEnvWithHandlerCfg, TxEnv}, - Address, Header, TransactionSigned, U256, + Header, TransactionSigned, U256, }; use reth_tracing::{RethTracer, Tracer}; use std::sync::Arc; diff --git a/examples/custom-inspector/Cargo.toml b/examples/custom-inspector/Cargo.toml index 101806276099..a94980951627 100644 --- a/examples/custom-inspector/Cargo.toml +++ b/examples/custom-inspector/Cargo.toml @@ -12,3 +12,4 @@ reth-rpc-types.workspace = true alloy-rpc-types.workspace = true clap = { workspace = true, features = ["derive"] } futures-util.workspace = true +alloy-primitives.workspace = true diff --git a/examples/custom-inspector/src/main.rs b/examples/custom-inspector/src/main.rs index 87f8dd7e95bf..42271dcabe97 100644 --- a/examples/custom-inspector/src/main.rs +++ b/examples/custom-inspector/src/main.rs @@ -10,6 +10,7 @@ #![cfg_attr(not(test), warn(unused_crate_dependencies))] +use alloy_primitives::Address; use alloy_rpc_types::state::EvmOverrides; use clap::Parser; use futures_util::StreamExt; @@ -17,7 +18,7 @@ use reth::{ args::utils::DefaultChainSpecParser, builder::NodeHandle, cli::Cli, - primitives::{Address, BlockNumberOrTag, IntoRecoveredTransaction}, + primitives::BlockNumberOrTag, revm::{ inspector_handle_register, interpreter::{Interpreter, OpCode}, diff --git a/examples/custom-rlpx-subprotocol/src/main.rs b/examples/custom-rlpx-subprotocol/src/main.rs index 8dc95641e201..e16f71071c8c 100644 --- a/examples/custom-rlpx-subprotocol/src/main.rs +++ b/examples/custom-rlpx-subprotocol/src/main.rs @@ -19,7 +19,6 @@ use reth_network::{ }; use reth_network_api::{test_utils::PeersHandleProvider, NetworkInfo}; use reth_node_ethereum::EthereumNode; -use reth_provider::test_utils::NoopProvider; use subprotocol::{ connection::CustomCommand, protocol::{ @@ -51,7 +50,7 @@ fn main() -> eyre::Result<()> { .listener_addr(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, 0))) .disable_discovery() .add_rlpx_sub_protocol(custom_rlpx_handler_2.into_rlpx_sub_protocol()) - .build(NoopProvider::default()); + .build_with_noop_provider(node.chain_spec()); // spawn the second network instance let subnetwork = NetworkManager::new(net_cfg).await?; diff --git a/examples/db-access/Cargo.toml b/examples/db-access/Cargo.toml index c0fbe74e1874..0180f4f90922 100644 --- a/examples/db-access/Cargo.toml +++ b/examples/db-access/Cargo.toml @@ -16,6 +16,7 @@ reth-node-ethereum.workspace = true reth-node-types.workspace = true alloy-rpc-types.workspace = true +alloy-primitives.workspace = true eyre.workspace = true diff --git a/examples/db-access/src/main.rs b/examples/db-access/src/main.rs index 53b56b738c0a..5730e6753522 100644 --- a/examples/db-access/src/main.rs +++ b/examples/db-access/src/main.rs @@ -1,8 +1,9 @@ +use alloy_primitives::Address; use alloy_rpc_types::{Filter, FilteredParams}; use reth_chainspec::ChainSpecBuilder; use reth_node_ethereum::EthereumNode; use reth_node_types::NodeTypesWithDBAdapter; -use reth_primitives::{alloy_primitives::Sealable, Address, SealedHeader, B256}; +use reth_primitives::{alloy_primitives::Sealable, SealedHeader, B256}; use reth_provider::{ providers::StaticFileProvider, AccountReader, BlockReader, BlockSource, HeaderProvider, ProviderFactory, ReceiptProvider, StateProvider, TransactionsProvider, diff --git a/examples/polygon-p2p/src/main.rs b/examples/polygon-p2p/src/main.rs index d93c92cebb02..6078ae14cb85 100644 --- a/examples/polygon-p2p/src/main.rs +++ b/examples/polygon-p2p/src/main.rs @@ -14,7 +14,6 @@ use reth_discv4::Discv4ConfigBuilder; use reth_network::{ config::NetworkMode, NetworkConfig, NetworkEvent, NetworkEventListenerProvider, NetworkManager, }; -use reth_provider::test_utils::NoopProvider; use reth_tracing::{ tracing::info, tracing_subscriber::filter::LevelFilter, LayerInfo, LogFormat, RethTracer, Tracer, @@ -47,11 +46,10 @@ async fn main() { // The network configuration let net_cfg = NetworkConfig::builder(secret_key) - .chain_spec(polygon_chain_spec()) .set_head(head()) .network_mode(NetworkMode::Work) .listener_addr(local_addr) - .build(NoopProvider::default()); + .build_with_noop_provider(polygon_chain_spec()); // Set Discv4 lookup interval to 1 second let mut discv4_cfg = Discv4ConfigBuilder::default(); diff --git a/examples/stateful-precompile/Cargo.toml b/examples/stateful-precompile/Cargo.toml index 2ae4656eee86..47a784c36e14 100644 --- a/examples/stateful-precompile/Cargo.toml +++ b/examples/stateful-precompile/Cargo.toml @@ -14,6 +14,7 @@ reth-primitives.workspace = true reth-node-ethereum = { workspace = true, features = ["test-utils"] } reth-tracing.workspace = true alloy-genesis.workspace = true +alloy-primitives.workspace = true eyre.workspace = true parking_lot.workspace = true diff --git a/examples/stateful-precompile/src/main.rs b/examples/stateful-precompile/src/main.rs index 88ca2ac1a7bb..effece640d26 100644 --- a/examples/stateful-precompile/src/main.rs +++ b/examples/stateful-precompile/src/main.rs @@ -3,13 +3,14 @@ #![cfg_attr(not(test), warn(unused_crate_dependencies))] use alloy_genesis::Genesis; +use alloy_primitives::Address; use parking_lot::RwLock; use reth::{ api::NextBlockEnvAttributes, builder::{components::ExecutorBuilder, BuilderContext, NodeBuilder}, primitives::{ revm_primitives::{BlockEnv, CfgEnvWithHandlerCfg, Env, PrecompileResult, TxEnv}, - Address, Bytes, U256, + Bytes, U256, }, revm::{ handler::register::EvmHandler, diff --git a/examples/txpool-tracing/Cargo.toml b/examples/txpool-tracing/Cargo.toml index 219292ee0686..38d0ad9409b6 100644 --- a/examples/txpool-tracing/Cargo.toml +++ b/examples/txpool-tracing/Cargo.toml @@ -11,3 +11,4 @@ reth-node-ethereum.workspace = true alloy-rpc-types-trace.workspace = true clap = { workspace = true, features = ["derive"] } futures-util.workspace = true +alloy-primitives.workspace = true diff --git a/examples/txpool-tracing/src/main.rs b/examples/txpool-tracing/src/main.rs index c9d27089d9ce..6b916dcb5092 100644 --- a/examples/txpool-tracing/src/main.rs +++ b/examples/txpool-tracing/src/main.rs @@ -10,16 +10,13 @@ #![cfg_attr(not(test), warn(unused_crate_dependencies))] +use alloy_primitives::Address; use alloy_rpc_types_trace::{parity::TraceType, tracerequest::TraceCallRequest}; use clap::Parser; use futures_util::StreamExt; use reth::{ - args::utils::DefaultChainSpecParser, - builder::NodeHandle, - cli::Cli, - primitives::{Address, IntoRecoveredTransaction}, - rpc::compat::transaction::transaction_to_call_request, - transaction_pool::TransactionPool, + args::utils::DefaultChainSpecParser, builder::NodeHandle, cli::Cli, + rpc::compat::transaction::transaction_to_call_request, transaction_pool::TransactionPool, }; use reth_node_ethereum::node::EthereumNode; diff --git a/testing/ef-tests/src/models.rs b/testing/ef-tests/src/models.rs index 8c8c3189a5d3..47d0a388a3ef 100644 --- a/testing/ef-tests/src/models.rs +++ b/testing/ef-tests/src/models.rs @@ -1,7 +1,7 @@ //! Shared models for use crate::{assert::assert_equal, Error}; -use alloy_primitives::{Address, Bloom, Bytes, B256, B64, U256}; +use alloy_primitives::{keccak256, Address, Bloom, Bytes, B256, B64, U256}; use reth_chainspec::{ChainSpec, ChainSpecBuilder}; use reth_db::tables; use reth_db_api::{ @@ -9,8 +9,7 @@ use reth_db_api::{ transaction::{DbTx, DbTxMut}, }; use reth_primitives::{ - keccak256, Account as RethAccount, Bytecode, Header as RethHeader, SealedHeader, StorageEntry, - Withdrawals, + Account as RethAccount, Bytecode, Header as RethHeader, SealedHeader, StorageEntry, Withdrawals, }; use serde::Deserialize; use std::{collections::BTreeMap, ops::Deref}; diff --git a/testing/testing-utils/src/genesis_allocator.rs b/testing/testing-utils/src/genesis_allocator.rs index 8a5adb300240..d2da3bbabd28 100644 --- a/testing/testing-utils/src/genesis_allocator.rs +++ b/testing/testing-utils/src/genesis_allocator.rs @@ -18,7 +18,8 @@ use std::{ /// /// # Example /// ``` -/// # use reth_primitives::{Address, U256, hex, Bytes}; +/// # use alloy_primitives::Address; +/// # use reth_primitives::{U256, hex, Bytes}; /// # use reth_testing_utils::GenesisAllocator; /// # use std::str::FromStr; /// let mut allocator = GenesisAllocator::default();