Skip to content

Commit 5e65e9b

Browse files
committed
chore: refactor to use dedicated SparseTrieState struct and new variant
1 parent b613648 commit 5e65e9b

File tree

4 files changed

+131
-101
lines changed

4 files changed

+131
-101
lines changed

crates/engine/tree/src/tree/payload_processor/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use reth_trie_parallel::{
2828
proof_task::{ProofTaskCtx, ProofTaskManager},
2929
root::ParallelStateRootError,
3030
};
31-
use reth_trie_sparse::{blinded::DefaultBlindedProvider, SparseTrie};
31+
use reth_trie_sparse::SparseTrieState;
3232
use std::{
3333
collections::VecDeque,
3434
sync::{
@@ -70,7 +70,7 @@ where
7070
precompile_cache_map: PrecompileCacheMap<SpecFor<Evm>>,
7171
/// A sparse trie, kept around to be used for the state root computation so that allocations
7272
/// can be minimized.
73-
sparse_trie: Option<SparseTrie<DefaultBlindedProvider>>,
73+
sparse_trie: Option<SparseTrieState>,
7474
_marker: std::marker::PhantomData<N>,
7575
}
7676

@@ -251,7 +251,7 @@ where
251251
}
252252

253253
/// Sets the sparse trie to be kept around for the state root computation.
254-
pub(super) fn set_sparse_trie(&mut self, sparse_trie: SparseTrie<DefaultBlindedProvider>) {
254+
pub(super) fn set_sparse_trie(&mut self, sparse_trie: SparseTrieState) {
255255
self.sparse_trie = Some(sparse_trie);
256256
}
257257

crates/engine/tree/src/tree/payload_processor/sparse_trie.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use rayon::iter::{ParallelBridge, ParallelIterator};
99
use reth_trie::{updates::TrieUpdates, Nibbles};
1010
use reth_trie_parallel::root::ParallelStateRootError;
1111
use reth_trie_sparse::{
12-
blinded::{BlindedProvider, BlindedProviderFactory, DefaultBlindedProvider},
12+
blinded::{BlindedProvider, BlindedProviderFactory},
1313
errors::{SparseStateTrieResult, SparseTrieErrorKind},
14-
SparseStateTrie, SparseTrie,
14+
SparseStateTrie, SparseTrieState,
1515
};
1616
use std::{
1717
sync::mpsc,
@@ -63,41 +63,39 @@ where
6363
}
6464
}
6565

66-
/// Creates a new sparse trie, populating the accounts trie with the given cleared `SparseTrie`
67-
/// if it exists.
66+
/// Creates a new sparse trie, populating the accounts trie with the given cleared
67+
/// `SparseTrieState` if it exists.
6868
pub(super) fn new_with_stored_trie(
6969
executor: WorkloadExecutor,
7070
updates: mpsc::Receiver<SparseTrieUpdate>,
7171
blinded_provider_factory: BPF,
7272
trie_metrics: MultiProofTaskMetrics,
73-
sparse_trie: Option<SparseTrie<DefaultBlindedProvider>>,
73+
sparse_trie_state: Option<SparseTrieState>,
7474
) -> Self {
75-
if let Some(sparse_trie) = sparse_trie {
75+
if let Some(sparse_trie_state) = sparse_trie_state {
7676
Self::with_accounts_trie(
7777
executor,
7878
updates,
7979
blinded_provider_factory,
8080
trie_metrics,
81-
sparse_trie,
81+
sparse_trie_state,
8282
)
8383
} else {
8484
Self::new(executor, updates, blinded_provider_factory, trie_metrics)
8585
}
8686
}
8787

88-
/// Creates a new sparse trie task, using the given cleared `SparseTrie` for the accounts trie.
88+
/// Creates a new sparse trie task, using the given cleared `SparseTrieState` for the accounts
89+
/// trie.
8990
pub(super) fn with_accounts_trie(
9091
executor: WorkloadExecutor,
9192
updates: mpsc::Receiver<SparseTrieUpdate>,
9293
blinded_provider_factory: BPF,
9394
metrics: MultiProofTaskMetrics,
94-
cleared_accounts_trie: SparseTrie<DefaultBlindedProvider>,
95+
sparse_trie_state: SparseTrieState,
9596
) -> Self {
9697
let mut trie = SparseStateTrie::new(blinded_provider_factory).with_updates(true);
97-
98-
if let SparseTrie::Revealed(cleared) = cleared_accounts_trie {
99-
trie.populate_from(cleared);
100-
}
98+
trie.populate_from(sparse_trie_state);
10199

102100
Self { executor, updates, metrics, trie }
103101
}
@@ -149,7 +147,7 @@ where
149147
self.metrics.sparse_trie_total_duration_histogram.record(now.elapsed());
150148

151149
// take the account trie
152-
let trie = self.trie.take_cleared_account_trie();
150+
let trie = self.trie.take_cleared_account_trie_state();
153151

154152
Ok(StateRootComputeOutcome { state_root, trie_updates, trie })
155153
}
@@ -164,7 +162,7 @@ pub struct StateRootComputeOutcome {
164162
/// The trie updates.
165163
pub trie_updates: TrieUpdates,
166164
/// The account state trie.
167-
pub trie: SparseTrie<DefaultBlindedProvider>,
165+
pub trie: SparseTrieState,
168166
}
169167

170168
/// Updates the sparse trie with the given proofs and state, and returns the elapsed time.

crates/trie/sparse/src/state.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
use crate::{
2-
blinded::{
3-
BlindedProvider, BlindedProviderFactory, DefaultBlindedProvider,
4-
DefaultBlindedProviderFactory,
5-
},
6-
LeafLookup, RevealedSparseTrie, SparseTrie, TrieMasks,
2+
blinded::{BlindedProvider, BlindedProviderFactory, DefaultBlindedProviderFactory},
3+
LeafLookup, RevealedSparseTrie, SparseTrie, SparseTrieState, TrieMasks,
74
};
8-
use alloc::{boxed::Box, collections::VecDeque, vec::Vec};
5+
use alloc::{collections::VecDeque, vec::Vec};
96
use alloy_primitives::{
107
hex,
118
map::{B256Map, HashMap, HashSet},
@@ -110,15 +107,16 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
110107
self.revealed_account_paths.contains(&Nibbles::unpack(account))
111108
}
112109

113-
/// Uses the input `SparseTrie` to populate the backing data structures in the `state` trie.
114-
pub fn populate_from<P>(&mut self, trie: Box<RevealedSparseTrie<P>>) {
110+
/// Uses the input `SparseTrieState` to populate the backing data structures in the `state`
111+
/// trie.
112+
pub fn populate_from(&mut self, trie: SparseTrieState) {
115113
if let Some(new_trie) = self.state.as_revealed_mut() {
116-
new_trie.populate_from_trie(trie);
114+
new_trie.use_allocated_state(trie);
117115
} else {
118116
self.state = SparseTrie::revealed_with_provider(
119117
self.provider_factory.account_node_provider(),
120118
trie,
121-
);
119+
)
122120
}
123121
}
124122

@@ -662,7 +660,7 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
662660
&mut self,
663661
) -> SparseStateTrieResult<&mut RevealedSparseTrie<F::AccountNodeProvider>> {
664662
match self.state {
665-
SparseTrie::Blind { allocated: _ } => {
663+
SparseTrie::Blind | SparseTrie::AllocatedEmpty { .. } => {
666664
let (root_node, hash_mask, tree_mask) = self
667665
.provider_factory
668666
.account_node_provider()
@@ -882,7 +880,7 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
882880
}
883881

884882
/// Clears and takes the account trie.
885-
pub fn take_cleared_account_trie(&mut self) -> SparseTrie<DefaultBlindedProvider> {
883+
pub fn take_cleared_account_trie_state(&mut self) -> SparseTrieState {
886884
let trie = core::mem::take(&mut self.state);
887885
trie.cleared()
888886
}
@@ -988,7 +986,7 @@ mod tests {
988986
assert_eq!(proofs.len(), 1);
989987

990988
let mut sparse = SparseStateTrie::default();
991-
assert_eq!(sparse.state, SparseTrie::Blind { allocated: None });
989+
assert_eq!(sparse.state, SparseTrie::Blind);
992990

993991
sparse.reveal_account(Default::default(), proofs.into_inner()).unwrap();
994992
assert_eq!(sparse.state, SparseTrie::revealed_empty());

0 commit comments

Comments
 (0)