diff --git a/crates/engine/primitives/src/config.rs b/crates/engine/primitives/src/config.rs index 0b72e1d6243..43af0c09bd0 100644 --- a/crates/engine/primitives/src/config.rs +++ b/crates/engine/primitives/src/config.rs @@ -100,8 +100,6 @@ pub struct TreeConfig { disable_state_cache: bool, /// Whether to disable parallel prewarming. disable_prewarming: bool, - /// Whether to disable the parallel sparse trie state root algorithm. - disable_parallel_sparse_trie: bool, /// Whether to enable state provider metrics. state_provider_metrics: bool, /// Cross-block cache size in bytes. @@ -158,7 +156,6 @@ impl Default for TreeConfig { always_compare_trie_updates: false, disable_state_cache: false, disable_prewarming: false, - disable_parallel_sparse_trie: false, state_provider_metrics: false, cross_block_cache_size: DEFAULT_CROSS_BLOCK_CACHE_SIZE, has_enough_parallelism: has_enough_parallelism(), @@ -191,7 +188,6 @@ impl TreeConfig { always_compare_trie_updates: bool, disable_state_cache: bool, disable_prewarming: bool, - disable_parallel_sparse_trie: bool, state_provider_metrics: bool, cross_block_cache_size: u64, has_enough_parallelism: bool, @@ -218,7 +214,6 @@ impl TreeConfig { always_compare_trie_updates, disable_state_cache, disable_prewarming, - disable_parallel_sparse_trie, state_provider_metrics, cross_block_cache_size, has_enough_parallelism, @@ -299,11 +294,6 @@ impl TreeConfig { self.state_provider_metrics } - /// Returns whether or not the parallel sparse trie is disabled. - pub const fn disable_parallel_sparse_trie(&self) -> bool { - self.disable_parallel_sparse_trie - } - /// Returns whether or not state cache is disabled. pub const fn disable_state_cache(&self) -> bool { self.disable_state_cache @@ -441,15 +431,6 @@ impl TreeConfig { self } - /// Setter for whether to disable the parallel sparse trie - pub const fn with_disable_parallel_sparse_trie( - mut self, - disable_parallel_sparse_trie: bool, - ) -> Self { - self.disable_parallel_sparse_trie = disable_parallel_sparse_trie; - self - } - /// Setter for whether multiproof task should chunk proof targets. pub const fn with_multiproof_chunking_enabled( mut self, diff --git a/crates/engine/tree/src/tree/payload_processor/configured_sparse_trie.rs b/crates/engine/tree/src/tree/payload_processor/configured_sparse_trie.rs deleted file mode 100644 index c4f4e0ec366..00000000000 --- a/crates/engine/tree/src/tree/payload_processor/configured_sparse_trie.rs +++ /dev/null @@ -1,188 +0,0 @@ -//! Configured sparse trie enum for switching between serial and parallel implementations. - -use alloy_primitives::B256; -use reth_trie::{BranchNodeMasks, Nibbles, ProofTrieNode, TrieNode}; -use reth_trie_sparse::{ - errors::SparseTrieResult, provider::TrieNodeProvider, LeafLookup, LeafLookupError, - SerialSparseTrie, SparseTrieInterface, SparseTrieUpdates, -}; -use reth_trie_sparse_parallel::ParallelSparseTrie; -use std::borrow::Cow; - -/// Enum for switching between serial and parallel sparse trie implementations. -/// -/// This type allows runtime selection between different sparse trie implementations, -/// providing flexibility in choosing the appropriate implementation based on workload -/// characteristics. -#[derive(Debug, Clone)] -pub(crate) enum ConfiguredSparseTrie { - /// Serial implementation of the sparse trie. - Serial(Box), - /// Parallel implementation of the sparse trie. - Parallel(Box), -} - -impl From for ConfiguredSparseTrie { - fn from(trie: SerialSparseTrie) -> Self { - Self::Serial(Box::new(trie)) - } -} - -impl From for ConfiguredSparseTrie { - fn from(trie: ParallelSparseTrie) -> Self { - Self::Parallel(Box::new(trie)) - } -} - -impl Default for ConfiguredSparseTrie { - fn default() -> Self { - Self::Serial(Default::default()) - } -} - -impl SparseTrieInterface for ConfiguredSparseTrie { - fn with_root( - self, - root: TrieNode, - masks: Option, - retain_updates: bool, - ) -> SparseTrieResult { - match self { - Self::Serial(trie) => { - trie.with_root(root, masks, retain_updates).map(|t| Self::Serial(Box::new(t))) - } - Self::Parallel(trie) => { - trie.with_root(root, masks, retain_updates).map(|t| Self::Parallel(Box::new(t))) - } - } - } - - fn with_updates(self, retain_updates: bool) -> Self { - match self { - Self::Serial(trie) => Self::Serial(Box::new(trie.with_updates(retain_updates))), - Self::Parallel(trie) => Self::Parallel(Box::new(trie.with_updates(retain_updates))), - } - } - - fn reserve_nodes(&mut self, additional: usize) { - match self { - Self::Serial(trie) => trie.reserve_nodes(additional), - Self::Parallel(trie) => trie.reserve_nodes(additional), - } - } - - fn reveal_node( - &mut self, - path: Nibbles, - node: TrieNode, - masks: Option, - ) -> SparseTrieResult<()> { - match self { - Self::Serial(trie) => trie.reveal_node(path, node, masks), - Self::Parallel(trie) => trie.reveal_node(path, node, masks), - } - } - - fn reveal_nodes(&mut self, nodes: Vec) -> SparseTrieResult<()> { - match self { - Self::Serial(trie) => trie.reveal_nodes(nodes), - Self::Parallel(trie) => trie.reveal_nodes(nodes), - } - } - - fn update_leaf( - &mut self, - full_path: Nibbles, - value: Vec, - provider: P, - ) -> SparseTrieResult<()> { - match self { - Self::Serial(trie) => trie.update_leaf(full_path, value, provider), - Self::Parallel(trie) => trie.update_leaf(full_path, value, provider), - } - } - - fn remove_leaf( - &mut self, - full_path: &Nibbles, - provider: P, - ) -> SparseTrieResult<()> { - match self { - Self::Serial(trie) => trie.remove_leaf(full_path, provider), - Self::Parallel(trie) => trie.remove_leaf(full_path, provider), - } - } - - fn root(&mut self) -> B256 { - match self { - Self::Serial(trie) => trie.root(), - Self::Parallel(trie) => trie.root(), - } - } - - fn update_subtrie_hashes(&mut self) { - match self { - Self::Serial(trie) => trie.update_subtrie_hashes(), - Self::Parallel(trie) => trie.update_subtrie_hashes(), - } - } - - fn get_leaf_value(&self, full_path: &Nibbles) -> Option<&Vec> { - match self { - Self::Serial(trie) => trie.get_leaf_value(full_path), - Self::Parallel(trie) => trie.get_leaf_value(full_path), - } - } - - fn find_leaf( - &self, - full_path: &Nibbles, - expected_value: Option<&Vec>, - ) -> Result { - match self { - Self::Serial(trie) => trie.find_leaf(full_path, expected_value), - Self::Parallel(trie) => trie.find_leaf(full_path, expected_value), - } - } - - fn take_updates(&mut self) -> SparseTrieUpdates { - match self { - Self::Serial(trie) => trie.take_updates(), - Self::Parallel(trie) => trie.take_updates(), - } - } - - fn wipe(&mut self) { - match self { - Self::Serial(trie) => trie.wipe(), - Self::Parallel(trie) => trie.wipe(), - } - } - - fn clear(&mut self) { - match self { - Self::Serial(trie) => trie.clear(), - Self::Parallel(trie) => trie.clear(), - } - } - - fn updates_ref(&self) -> Cow<'_, SparseTrieUpdates> { - match self { - Self::Serial(trie) => trie.updates_ref(), - Self::Parallel(trie) => trie.updates_ref(), - } - } - fn shrink_nodes_to(&mut self, size: usize) { - match self { - Self::Serial(trie) => trie.shrink_nodes_to(size), - Self::Parallel(trie) => trie.shrink_nodes_to(size), - } - } - - fn shrink_values_to(&mut self, size: usize) { - match self { - Self::Serial(trie) => trie.shrink_values_to(size), - Self::Parallel(trie) => trie.shrink_values_to(size), - } - } -} diff --git a/crates/engine/tree/src/tree/payload_processor/mod.rs b/crates/engine/tree/src/tree/payload_processor/mod.rs index 23da7c23cc4..47bbe0e5a16 100644 --- a/crates/engine/tree/src/tree/payload_processor/mod.rs +++ b/crates/engine/tree/src/tree/payload_processor/mod.rs @@ -44,7 +44,7 @@ use reth_trie_parallel::{ }; use reth_trie_sparse::{ provider::{TrieNodeProvider, TrieNodeProviderFactory}, - ClearedSparseStateTrie, SparseStateTrie, SparseTrie, + ClearedSparseStateTrie, RevealableSparseTrie, SparseStateTrie, }; use reth_trie_sparse_parallel::{ParallelSparseTrie, ParallelismThresholds}; use std::{ @@ -60,15 +60,12 @@ use std::{ use tracing::{debug, debug_span, instrument, warn, Span}; pub mod bal; -mod configured_sparse_trie; pub mod executor; pub mod multiproof; pub mod prewarm; pub mod receipt_root_task; pub mod sparse_trie; -use configured_sparse_trie::ConfiguredSparseTrie; - /// Default parallelism thresholds to use with the [`ParallelSparseTrie`]. /// /// These values were determined by performing benchmarks using gradually increasing values to judge @@ -134,12 +131,8 @@ where /// A cleared `SparseStateTrie`, kept around to be reused for the state root computation so /// that allocations can be minimized. sparse_state_trie: Arc< - parking_lot::Mutex< - Option>, - >, + parking_lot::Mutex>>, >, - /// Whether to disable the parallel sparse trie. - disable_parallel_sparse_trie: bool, /// Maximum concurrency for prewarm task. prewarm_max_concurrency: usize, /// Whether to disable cache metrics recording. @@ -174,7 +167,6 @@ where precompile_cache_disabled: config.precompile_cache_disabled(), precompile_cache_map, sparse_state_trie: Arc::default(), - disable_parallel_sparse_trie: config.disable_parallel_sparse_trie(), prewarm_max_concurrency: config.prewarm_max_concurrency(), disable_cache_metrics: config.disable_cache_metrics(), } @@ -514,7 +506,6 @@ where BPF::StorageNodeProvider: TrieNodeProvider + Send + Sync, { let cleared_sparse_trie = Arc::clone(&self.sparse_state_trie); - let disable_parallel_sparse_trie = self.disable_parallel_sparse_trie; let trie_metrics = self.trie_metrics.clone(); let span = Span::current(); @@ -524,14 +515,10 @@ where // Reuse a stored SparseStateTrie, or create a new one using the desired configuration // if there's none to reuse. let sparse_state_trie = cleared_sparse_trie.lock().take().unwrap_or_else(|| { - let default_trie = SparseTrie::blind_from(if disable_parallel_sparse_trie { - ConfiguredSparseTrie::Serial(Default::default()) - } else { - ConfiguredSparseTrie::Parallel(Box::new( - ParallelSparseTrie::default() - .with_parallelism_thresholds(PARALLEL_SPARSE_TRIE_PARALLELISM_THRESHOLDS), - )) - }); + let default_trie = RevealableSparseTrie::blind_from( + ParallelSparseTrie::default() + .with_parallelism_thresholds(PARALLEL_SPARSE_TRIE_PARALLELISM_THRESHOLDS), + ); ClearedSparseStateTrie::from_state_trie( SparseStateTrie::new() .with_accounts_trie(default_trie.clone()) @@ -540,12 +527,13 @@ where ) }); - let task = SparseTrieTask::<_, ConfiguredSparseTrie, ConfiguredSparseTrie>::new_with_cleared_trie( - sparse_trie_rx, - proof_worker_handle, - trie_metrics, - sparse_state_trie, - ); + let task = + SparseTrieTask::<_, ParallelSparseTrie, ParallelSparseTrie>::new_with_cleared_trie( + sparse_trie_rx, + proof_worker_handle, + trie_metrics, + sparse_state_trie, + ); let (result, trie) = task.run(); // Send state root computation result diff --git a/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs b/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs index 052fd8672b2..a1df41ee12f 100644 --- a/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs +++ b/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs @@ -8,7 +8,7 @@ use reth_trie_parallel::{proof_task::ProofResult, root::ParallelStateRootError}; use reth_trie_sparse::{ errors::{SparseStateTrieResult, SparseTrieErrorKind}, provider::{TrieNodeProvider, TrieNodeProviderFactory}, - ClearedSparseStateTrie, SerialSparseTrie, SparseStateTrie, SparseTrieInterface, + ClearedSparseStateTrie, SerialSparseTrie, SparseStateTrie, SparseTrie, }; use smallvec::SmallVec; use std::{ @@ -38,8 +38,8 @@ where BPF: TrieNodeProviderFactory + Send + Sync + Clone, BPF::AccountNodeProvider: TrieNodeProvider + Send + Sync, BPF::StorageNodeProvider: TrieNodeProvider + Send + Sync, - A: SparseTrieInterface + Send + Sync + Default, - S: SparseTrieInterface + Send + Sync + Default + Clone, + A: SparseTrie + Send + Sync + Default, + S: SparseTrie + Send + Sync + Default + Clone, { /// Creates a new sparse trie, pre-populating with a [`ClearedSparseStateTrie`]. pub(super) fn new_with_cleared_trie( @@ -150,8 +150,8 @@ where BPF: TrieNodeProviderFactory + Send + Sync, BPF::AccountNodeProvider: TrieNodeProvider + Send + Sync, BPF::StorageNodeProvider: TrieNodeProvider + Send + Sync, - A: SparseTrieInterface + Send + Sync + Default, - S: SparseTrieInterface + Send + Sync + Default + Clone, + A: SparseTrie + Send + Sync + Default, + S: SparseTrie + Send + Sync + Default + Clone, { trace!(target: "engine::root::sparse", "Updating sparse trie"); let started_at = Instant::now(); diff --git a/crates/node/core/src/args/engine.rs b/crates/node/core/src/args/engine.rs index d7c320fc52d..389b709b8f6 100644 --- a/crates/node/core/src/args/engine.rs +++ b/crates/node/core/src/args/engine.rs @@ -22,7 +22,6 @@ pub struct DefaultEngineValues { legacy_state_root_task_enabled: bool, state_cache_disabled: bool, prewarming_disabled: bool, - parallel_sparse_trie_disabled: bool, state_provider_metrics: bool, cross_block_cache_size: u64, state_root_task_compare_updates: bool, @@ -81,12 +80,6 @@ impl DefaultEngineValues { self } - /// Set whether to disable parallel sparse trie by default - pub const fn with_parallel_sparse_trie_disabled(mut self, v: bool) -> Self { - self.parallel_sparse_trie_disabled = v; - self - } - /// Set whether to enable state provider metrics by default pub const fn with_state_provider_metrics(mut self, v: bool) -> Self { self.state_provider_metrics = v; @@ -189,7 +182,6 @@ impl Default for DefaultEngineValues { legacy_state_root_task_enabled: false, state_cache_disabled: false, prewarming_disabled: false, - parallel_sparse_trie_disabled: false, state_provider_metrics: false, cross_block_cache_size: DEFAULT_CROSS_BLOCK_CACHE_SIZE_MB, state_root_task_compare_updates: false, @@ -244,14 +236,14 @@ pub struct EngineArgs { #[arg(long = "engine.disable-prewarming", alias = "engine.disable-caching-and-prewarming", default_value_t = DefaultEngineValues::get_global().prewarming_disabled)] pub prewarming_disabled: bool, - /// CAUTION: This CLI flag has no effect anymore, use --engine.disable-parallel-sparse-trie - /// if you want to disable usage of the `ParallelSparseTrie`. + /// CAUTION: This CLI flag has no effect anymore. The parallel sparse trie is always enabled. #[deprecated] #[arg(long = "engine.parallel-sparse-trie", default_value = "true", hide = true)] pub parallel_sparse_trie_enabled: bool, - /// Disable the parallel sparse trie in the engine. - #[arg(long = "engine.disable-parallel-sparse-trie", default_value_t = DefaultEngineValues::get_global().parallel_sparse_trie_disabled)] + /// CAUTION: This CLI flag has no effect anymore. The parallel sparse trie is always enabled. + #[deprecated] + #[arg(long = "engine.disable-parallel-sparse-trie", default_value = "false", hide = true)] pub parallel_sparse_trie_disabled: bool, /// Enable state provider latency metrics. This allows the engine to collect and report stats @@ -343,7 +335,6 @@ impl Default for EngineArgs { legacy_state_root_task_enabled, state_cache_disabled, prewarming_disabled, - parallel_sparse_trie_disabled, state_provider_metrics, cross_block_cache_size, state_root_task_compare_updates, @@ -369,7 +360,7 @@ impl Default for EngineArgs { state_cache_disabled, prewarming_disabled, parallel_sparse_trie_enabled: true, - parallel_sparse_trie_disabled, + parallel_sparse_trie_disabled: false, state_provider_metrics, cross_block_cache_size, accept_execution_requests_hash, @@ -398,7 +389,6 @@ impl EngineArgs { .with_legacy_state_root(self.legacy_state_root_task_enabled) .without_state_cache(self.state_cache_disabled) .without_prewarming(self.prewarming_disabled) - .with_disable_parallel_sparse_trie(self.parallel_sparse_trie_disabled) .with_state_provider_metrics(self.state_provider_metrics) .with_always_compare_trie_updates(self.state_root_task_compare_updates) .with_cross_block_cache_size(self.cross_block_cache_size * 1024 * 1024) @@ -457,7 +447,7 @@ mod tests { state_cache_disabled: true, prewarming_disabled: true, parallel_sparse_trie_enabled: true, - parallel_sparse_trie_disabled: true, + parallel_sparse_trie_disabled: false, state_provider_metrics: true, cross_block_cache_size: 256, state_root_task_compare_updates: true, @@ -485,7 +475,6 @@ mod tests { "--engine.legacy-state-root", "--engine.disable-state-cache", "--engine.disable-prewarming", - "--engine.disable-parallel-sparse-trie", "--engine.state-provider-metrics", "--engine.cross-block-cache-size", "256", diff --git a/crates/stateless/src/trie.rs b/crates/stateless/src/trie.rs index 49d1f6cf0fd..c4bfc762afb 100644 --- a/crates/stateless/src/trie.rs +++ b/crates/stateless/src/trie.rs @@ -11,7 +11,7 @@ use reth_trie_common::{HashedPostState, Nibbles, TRIE_ACCOUNT_RLP_MAX_SIZE}; use reth_trie_sparse::{ errors::SparseStateTrieResult, provider::{DefaultTrieNodeProvider, DefaultTrieNodeProviderFactory}, - SparseStateTrie, SparseTrie, SparseTrieInterface, + RevealableSparseTrie, SparseStateTrie, SparseTrie, }; /// Trait for stateless trie implementations that can be used for stateless validation. @@ -245,7 +245,7 @@ fn calculate_state_root( for (address, storage) in state.storages.into_iter().sorted_unstable_by_key(|(addr, _)| *addr) { // Take the existing storage trie (or create an empty, “revealed” one) let mut storage_trie = - trie.take_storage_trie(&address).unwrap_or_else(SparseTrie::revealed_empty); + trie.take_storage_trie(&address).unwrap_or_else(RevealableSparseTrie::revealed_empty); if storage.wiped { storage_trie.wipe()?; diff --git a/crates/trie/sparse-parallel/src/trie.rs b/crates/trie/sparse-parallel/src/trie.rs index af24f510b3d..7b55b66fd40 100644 --- a/crates/trie/sparse-parallel/src/trie.rs +++ b/crates/trie/sparse-parallel/src/trie.rs @@ -14,7 +14,7 @@ use reth_trie_common::{ }; use reth_trie_sparse::{ provider::{RevealedNode, TrieNodeProvider}, - LeafLookup, LeafLookupError, RlpNodeStackItem, SparseNode, SparseNodeType, SparseTrieInterface, + LeafLookup, LeafLookupError, RlpNodeStackItem, SparseNode, SparseNodeType, SparseTrie, SparseTrieUpdates, }; use smallvec::SmallVec; @@ -147,7 +147,7 @@ impl Default for ParallelSparseTrie { } } -impl SparseTrieInterface for ParallelSparseTrie { +impl SparseTrie for ParallelSparseTrie { fn with_root( mut self, root: TrieNode, @@ -2704,8 +2704,7 @@ mod tests { use reth_trie_db::DatabaseTrieCursorFactory; use reth_trie_sparse::{ provider::{DefaultTrieNodeProvider, RevealedNode, TrieNodeProvider}, - LeafLookup, LeafLookupError, SerialSparseTrie, SparseNode, SparseTrieInterface, - SparseTrieUpdates, + LeafLookup, LeafLookupError, SerialSparseTrie, SparseNode, SparseTrie, SparseTrieUpdates, }; use std::collections::{BTreeMap, BTreeSet}; diff --git a/crates/trie/sparse/benches/rlp_node.rs b/crates/trie/sparse/benches/rlp_node.rs index 9f2337f31b8..8054d30a2d8 100644 --- a/crates/trie/sparse/benches/rlp_node.rs +++ b/crates/trie/sparse/benches/rlp_node.rs @@ -7,7 +7,7 @@ use proptest::{prelude::*, test_runner::TestRunner}; use rand::{seq::IteratorRandom, Rng}; use reth_testing_utils::generators; use reth_trie::Nibbles; -use reth_trie_sparse::{provider::DefaultTrieNodeProvider, SerialSparseTrie, SparseTrieInterface}; +use reth_trie_sparse::{provider::DefaultTrieNodeProvider, SerialSparseTrie, SparseTrie}; fn update_rlp_node_level(c: &mut Criterion) { let mut rng = generators::rng(); diff --git a/crates/trie/sparse/benches/root.rs b/crates/trie/sparse/benches/root.rs index ece0aa5313d..1725f529359 100644 --- a/crates/trie/sparse/benches/root.rs +++ b/crates/trie/sparse/benches/root.rs @@ -13,7 +13,7 @@ use reth_trie::{ HashedStorage, }; use reth_trie_common::{updates::TrieUpdatesSorted, HashBuilder, Nibbles}; -use reth_trie_sparse::{provider::DefaultTrieNodeProvider, SerialSparseTrie, SparseTrie}; +use reth_trie_sparse::{provider::DefaultTrieNodeProvider, RevealableSparseTrie, SerialSparseTrie}; fn calculate_root_from_leaves(c: &mut Criterion) { let mut group = c.benchmark_group("calculate root from leaves"); @@ -42,19 +42,22 @@ fn calculate_root_from_leaves(c: &mut Criterion) { // sparse trie let provider = DefaultTrieNodeProvider; group.bench_function(BenchmarkId::new("sparse trie", size), |b| { - b.iter_with_setup(SparseTrie::::revealed_empty, |mut sparse| { - for (key, value) in &state { + b.iter_with_setup( + RevealableSparseTrie::::revealed_empty, + |mut sparse| { + for (key, value) in &state { + sparse + .update_leaf( + Nibbles::unpack(key), + alloy_rlp::encode_fixed_size(value).to_vec(), + &provider, + ) + .unwrap(); + } + sparse.root().unwrap(); sparse - .update_leaf( - Nibbles::unpack(key), - alloy_rlp::encode_fixed_size(value).to_vec(), - &provider, - ) - .unwrap(); - } - sparse.root().unwrap(); - sparse - }) + }, + ) }); } } @@ -206,7 +209,8 @@ fn calculate_root_from_leaves_repeated(c: &mut Criterion) { group.bench_function(benchmark_id, |b| { b.iter_with_setup( || { - let mut sparse = SparseTrie::::revealed_empty(); + let mut sparse = + RevealableSparseTrie::::revealed_empty(); for (key, value) in &init_state { sparse .update_leaf( diff --git a/crates/trie/sparse/src/state.rs b/crates/trie/sparse/src/state.rs index 415938915ad..84f57cde78e 100644 --- a/crates/trie/sparse/src/state.rs +++ b/crates/trie/sparse/src/state.rs @@ -1,7 +1,7 @@ use crate::{ provider::{TrieNodeProvider, TrieNodeProviderFactory}, - traits::SparseTrieInterface, - SerialSparseTrie, SparseTrie, + traits::SparseTrie as SparseTrieTrait, + RevealableSparseTrie, SerialSparseTrie, }; use alloc::{collections::VecDeque, vec::Vec}; use alloy_primitives::{ @@ -31,8 +31,8 @@ pub struct ClearedSparseStateTrie< impl ClearedSparseStateTrie where - A: SparseTrieInterface, - S: SparseTrieInterface, + A: SparseTrieTrait, + S: SparseTrieTrait, { /// Creates a [`ClearedSparseStateTrie`] by clearing all the existing internal state of a /// [`SparseStateTrie`] and then storing that instance for later re-use. @@ -83,7 +83,7 @@ pub struct SparseStateTrie< S = SerialSparseTrie, // Storage trie implementation > { /// Sparse account trie. - state: SparseTrie, + state: RevealableSparseTrie, /// Collection of revealed account trie paths. revealed_account_paths: HashSet, /// State related to storage tries. @@ -118,7 +118,7 @@ where #[cfg(test)] impl SparseStateTrie { /// Create state trie from state trie. - pub fn from_state(state: SparseTrie) -> Self { + pub fn from_state(state: RevealableSparseTrie) -> Self { Self { state, ..Default::default() } } } @@ -130,14 +130,15 @@ impl SparseStateTrie { self } - /// Set the accounts trie to the given `SparseTrie`. - pub fn with_accounts_trie(mut self, trie: SparseTrie) -> Self { + /// Set the accounts trie to the given `RevealableSparseTrie`. + pub fn with_accounts_trie(mut self, trie: RevealableSparseTrie) -> Self { self.state = trie; self } - /// Set the default trie which will be cloned when creating new storage [`SparseTrie`]s. - pub fn with_default_storage_trie(mut self, trie: SparseTrie) -> Self { + /// Set the default trie which will be cloned when creating new storage + /// [`RevealableSparseTrie`]s. + pub fn with_default_storage_trie(mut self, trie: RevealableSparseTrie) -> Self { self.storage.default_trie = trie; self } @@ -145,8 +146,8 @@ impl SparseStateTrie { impl SparseStateTrie where - A: SparseTrieInterface + Default, - S: SparseTrieInterface + Default + Clone, + A: SparseTrieTrait + Default, + S: SparseTrieTrait + Default + Clone, { /// Create new [`SparseStateTrie`] pub fn new() -> Self { @@ -214,12 +215,12 @@ where } /// Takes the storage trie for the provided address. - pub fn take_storage_trie(&mut self, address: &B256) -> Option> { + pub fn take_storage_trie(&mut self, address: &B256) -> Option> { self.storage.tries.remove(address) } /// Inserts storage trie for the provided address. - pub fn insert_storage_trie(&mut self, address: B256, storage_trie: SparseTrie) { + pub fn insert_storage_trie(&mut self, address: B256, storage_trie: RevealableSparseTrie) { self.storage.tries.insert(address, storage_trie); } @@ -270,8 +271,8 @@ where let retain_updates = self.retain_updates; // Process all storage trie revealings in parallel, having first removed the - // `reveal_nodes` tracking and `SparseTrie`s for each account from their HashMaps. - // These will be returned after processing. + // `reveal_nodes` tracking and `RevealableSparseTrie`s for each account from their + // HashMaps. These will be returned after processing. let results: Vec<_> = storages .into_iter() .map(|(account, storage_subtree)| { @@ -293,8 +294,8 @@ where }) .collect(); - // Return `revealed_nodes` and `SparseTrie` for each account, incrementing metrics and - // returning the last error seen if any. + // Return `revealed_nodes` and `RevealableSparseTrie` for each account, incrementing + // metrics and returning the last error seen if any. let mut any_err = Ok(()); for (account, revealed_nodes, trie, result) in results { self.storage.revealed_paths.insert(account, revealed_nodes); @@ -352,8 +353,8 @@ where let retain_updates = self.retain_updates; // Process all storage trie revealings in parallel, having first removed the - // `reveal_nodes` tracking and `SparseTrie`s for each account from their HashMaps. - // These will be returned after processing. + // `reveal_nodes` tracking and `RevealableSparseTrie`s for each account from their + // HashMaps. These will be returned after processing. let results: Vec<_> = multiproof .storage_proofs .into_iter() @@ -506,7 +507,7 @@ where account: B256, nodes: Vec, revealed_nodes: &mut HashSet, - trie: &mut SparseTrie, + trie: &mut RevealableSparseTrie, retain_updates: bool, ) -> SparseStateTrieResult { let FilteredV2ProofNodes { root_node, nodes, new_nodes, metric_values } = @@ -566,7 +567,7 @@ where account: B256, storage_subtree: DecodedStorageMultiProof, revealed_nodes: &mut HashSet, - trie: &mut SparseTrie, + trie: &mut RevealableSparseTrie, retain_updates: bool, ) -> SparseStateTrieResult { let FilterMappedProofNodes { root_node, nodes, new_nodes, metric_values } = @@ -707,7 +708,7 @@ where /// If the trie has not been revealed, this function does nothing. #[instrument(target = "trie::sparse", skip_all)] pub fn calculate_subtries(&mut self) { - if let SparseTrie::Revealed(trie) = &mut self.state { + if let RevealableSparseTrie::Revealed(trie) = &mut self.state { trie.update_subtrie_hashes(); } } @@ -725,7 +726,7 @@ where provider_factory: impl TrieNodeProviderFactory, ) -> SparseStateTrieResult<&mut A> { match self.state { - SparseTrie::Blind(_) => { + RevealableSparseTrie::Blind(_) => { let (root_node, hash_mask, tree_mask) = provider_factory .account_node_provider() .trie_node(&Nibbles::default())? @@ -738,7 +739,7 @@ where let masks = BranchNodeMasks::from_optional(hash_mask, tree_mask); self.state.reveal_root(root_node, masks, self.retain_updates).map_err(Into::into) } - SparseTrie::Revealed(ref mut trie) => Ok(trie), + RevealableSparseTrie::Revealed(ref mut trie) => Ok(trie), } } @@ -977,18 +978,18 @@ where #[derive(Debug, Default)] struct StorageTries { /// Sparse storage tries. - tries: B256Map>, + tries: B256Map>, /// Cleared storage tries, kept for re-use. - cleared_tries: Vec>, + cleared_tries: Vec>, /// Collection of revealed storage trie paths, per account. revealed_paths: B256Map>, /// Cleared revealed storage trie path collections, kept for re-use. cleared_revealed_paths: Vec>, /// A default cleared trie instance, which will be cloned when creating new tries. - default_trie: SparseTrie, + default_trie: RevealableSparseTrie, } -impl StorageTries { +impl StorageTries { /// Returns all fields to a cleared state, equivalent to the default state, keeping cleared /// collections for re-use later when possible. fn clear(&mut self) { @@ -1025,7 +1026,7 @@ impl StorageTries { } } -impl StorageTries { +impl StorageTries { /// Returns the set of already revealed trie node paths for an account's storage, creating the /// set if it didn't previously exist. fn get_revealed_paths_mut(&mut self, account: B256) -> &mut HashSet { @@ -1034,12 +1035,12 @@ impl StorageTries { .or_insert_with(|| self.cleared_revealed_paths.pop().unwrap_or_default()) } - /// Returns the `SparseTrie` and the set of already revealed trie node paths for an account's - /// storage, creating them if they didn't previously exist. + /// Returns the `RevealableSparseTrie` and the set of already revealed trie node paths for an + /// account's storage, creating them if they didn't previously exist. fn get_trie_and_revealed_paths_mut( &mut self, account: B256, - ) -> (&mut SparseTrie, &mut HashSet) { + ) -> (&mut RevealableSparseTrie, &mut HashSet) { let trie = self.tries.entry(account).or_insert_with(|| { self.cleared_tries.pop().unwrap_or_else(|| self.default_trie.clone()) }); @@ -1055,7 +1056,7 @@ impl StorageTries { /// Takes the storage trie for the account from the internal `HashMap`, creating it if it /// doesn't already exist. #[cfg(feature = "std")] - fn take_or_create_trie(&mut self, account: &B256) -> SparseTrie { + fn take_or_create_trie(&mut self, account: &B256) -> RevealableSparseTrie { self.tries.remove(account).unwrap_or_else(|| { self.cleared_tries.pop().unwrap_or_else(|| self.default_trie.clone()) }) diff --git a/crates/trie/sparse/src/traits.rs b/crates/trie/sparse/src/traits.rs index 1d652284a40..15f474c6a2c 100644 --- a/crates/trie/sparse/src/traits.rs +++ b/crates/trie/sparse/src/traits.rs @@ -17,8 +17,8 @@ use crate::provider::TrieNodeProvider; /// /// This trait abstracts over different sparse trie implementations (serial vs parallel) /// while providing a unified interface for the core trie operations needed by the -/// [`crate::SparseTrie`] enum. -pub trait SparseTrieInterface: Sized + Debug + Send + Sync { +/// [`crate::RevealableSparseTrie`] enum. +pub trait SparseTrie: Sized + Debug + Send + Sync { /// Configures the trie to have the given root node revealed. /// /// # Arguments diff --git a/crates/trie/sparse/src/trie.rs b/crates/trie/sparse/src/trie.rs index 17809bb4cf1..0ca4a20cb7d 100644 --- a/crates/trie/sparse/src/trie.rs +++ b/crates/trie/sparse/src/trie.rs @@ -1,6 +1,6 @@ use crate::{ provider::{RevealedNode, TrieNodeProvider}, - LeafLookup, LeafLookupError, SparseTrieInterface, SparseTrieUpdates, + LeafLookup, LeafLookupError, SparseTrie as SparseTrieTrait, SparseTrieUpdates, }; use alloc::{ borrow::Cow, @@ -43,14 +43,15 @@ const SPARSE_TRIE_SUBTRIE_HASHES_LEVEL: usize = 2; /// 3. Incremental operations - nodes can be revealed as needed without loading the entire trie. /// This is what gives rise to the notion of a "sparse" trie. #[derive(PartialEq, Eq, Debug, Clone)] -pub enum SparseTrie { +pub enum RevealableSparseTrie { /// The trie is blind -- no nodes have been revealed /// /// This is the default state. In this state, the trie cannot be directly queried or modified /// until nodes are revealed. /// - /// In this state the `SparseTrie` can optionally carry with it a cleared `SerialSparseTrie`. - /// This allows for reusing the trie's allocations between payload executions. + /// In this state the `RevealableSparseTrie` can optionally carry with it a cleared + /// `SerialSparseTrie`. This allows for reusing the trie's allocations between payload + /// executions. Blind(Option>), /// Some nodes in the Trie have been revealed. /// @@ -60,21 +61,23 @@ pub enum SparseTrie { Revealed(Box), } -impl Default for SparseTrie { +impl Default for RevealableSparseTrie { fn default() -> Self { Self::Blind(None) } } -impl SparseTrie { +impl RevealableSparseTrie { /// Creates a new revealed but empty sparse trie with `SparseNode::Empty` as root node. /// /// # Examples /// /// ``` - /// use reth_trie_sparse::{provider::DefaultTrieNodeProvider, SerialSparseTrie, SparseTrie}; + /// use reth_trie_sparse::{ + /// provider::DefaultTrieNodeProvider, RevealableSparseTrie, SerialSparseTrie, + /// }; /// - /// let trie = SparseTrie::::revealed_empty(); + /// let trie = RevealableSparseTrie::::revealed_empty(); /// assert!(!trie.is_blind()); /// ``` pub fn revealed_empty() -> Self { @@ -91,7 +94,7 @@ impl SparseTrie { /// /// # Returns /// - /// A mutable reference to the underlying [`SparseTrieInterface`]. + /// A mutable reference to the underlying [`RevealableSparseTrie`](SparseTrieTrait). pub fn reveal_root( &mut self, root: TrieNode, @@ -115,17 +118,19 @@ impl SparseTrie { } } -impl SparseTrie { +impl RevealableSparseTrie { /// Creates a new blind sparse trie. /// /// # Examples /// /// ``` - /// use reth_trie_sparse::{provider::DefaultTrieNodeProvider, SerialSparseTrie, SparseTrie}; + /// use reth_trie_sparse::{ + /// provider::DefaultTrieNodeProvider, RevealableSparseTrie, SerialSparseTrie, + /// }; /// - /// let trie = SparseTrie::::blind(); + /// let trie = RevealableSparseTrie::::blind(); /// assert!(trie.is_blind()); - /// let trie = SparseTrie::::default(); + /// let trie = RevealableSparseTrie::::default(); /// assert!(trie.is_blind()); /// ``` pub const fn blind() -> Self { @@ -133,7 +138,7 @@ impl SparseTrie { } /// Creates a new blind sparse trie, clearing and later reusing the given - /// [`SparseTrieInterface`]. + /// [`RevealableSparseTrie`](SparseTrieTrait). pub fn blind_from(mut trie: T) -> Self { trie.clear(); Self::Blind(Some(Box::new(trie))) @@ -212,9 +217,10 @@ impl SparseTrie { Some((revealed.root(), revealed.take_updates())) } - /// Returns a [`SparseTrie::Blind`] based on this one. If this instance was revealed, or was - /// itself a `Blind` with a pre-allocated [`SparseTrieInterface`], this will return - /// a `Blind` carrying a cleared pre-allocated [`SparseTrieInterface`]. + /// Returns a [`RevealableSparseTrie::Blind`] based on this one. If this instance was revealed, + /// or was itself a `Blind` with a pre-allocated [`RevealableSparseTrie`](SparseTrieTrait), + /// this will return a `Blind` carrying a cleared pre-allocated + /// [`RevealableSparseTrie`](SparseTrieTrait). pub fn clear(self) -> Self { match self { Self::Blind(_) => self, @@ -415,7 +421,7 @@ impl Default for SerialSparseTrie { } } -impl SparseTrieInterface for SerialSparseTrie { +impl SparseTrieTrait for SerialSparseTrie { fn with_root( mut self, root: TrieNode, @@ -2486,8 +2492,8 @@ mod tests { #[test] fn sparse_trie_is_blind() { - assert!(SparseTrie::::blind().is_blind()); - assert!(!SparseTrie::::revealed_empty().is_blind()); + assert!(RevealableSparseTrie::::blind().is_blind()); + assert!(!RevealableSparseTrie::::revealed_empty().is_blind()); } #[test] diff --git a/crates/trie/trie/src/witness.rs b/crates/trie/trie/src/witness.rs index a740b698fa3..de444815fee 100644 --- a/crates/trie/trie/src/witness.rs +++ b/crates/trie/trie/src/witness.rs @@ -7,7 +7,7 @@ use crate::{ use alloy_rlp::EMPTY_STRING_CODE; use alloy_trie::EMPTY_ROOT_HASH; use reth_trie_common::HashedPostState; -use reth_trie_sparse::SparseTrieInterface; +use reth_trie_sparse::SparseTrie; use alloy_primitives::{ keccak256, diff --git a/docs/vocs/docs/pages/cli/op-reth/node.mdx b/docs/vocs/docs/pages/cli/op-reth/node.mdx index 98205ad008e..2ab4267a8b9 100644 --- a/docs/vocs/docs/pages/cli/op-reth/node.mdx +++ b/docs/vocs/docs/pages/cli/op-reth/node.mdx @@ -949,9 +949,6 @@ Engine: --engine.disable-prewarming Disable parallel prewarming - --engine.disable-parallel-sparse-trie - Disable the parallel sparse trie in the engine - --engine.state-provider-metrics Enable state provider latency metrics. This allows the engine to collect and report stats about how long state provider calls took during execution, but this does introduce slight overhead to state provider calls diff --git a/docs/vocs/docs/pages/cli/reth/node.mdx b/docs/vocs/docs/pages/cli/reth/node.mdx index 75ee10f7c17..072ca841edb 100644 --- a/docs/vocs/docs/pages/cli/reth/node.mdx +++ b/docs/vocs/docs/pages/cli/reth/node.mdx @@ -949,9 +949,6 @@ Engine: --engine.disable-prewarming Disable parallel prewarming - --engine.disable-parallel-sparse-trie - Disable the parallel sparse trie in the engine - --engine.state-provider-metrics Enable state provider latency metrics. This allows the engine to collect and report stats about how long state provider calls took during execution, but this does introduce slight overhead to state provider calls