Skip to content

Commit 2b894c7

Browse files
test: add logs in state root
1 parent b0902d9 commit 2b894c7

2 files changed

Lines changed: 49 additions & 16 deletions

File tree

crates/trie/parallel/src/parallel_root.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,21 @@ where
131131

132132
let mut hash_builder = HashBuilder::default().with_updates(retain_updates);
133133
let mut account_rlp = Vec::with_capacity(128);
134+
let start = std::time::Instant::now();
135+
let mut hash_elapsed = 0;
134136
while let Some(node) = account_node_iter.try_next().map_err(ProviderError::Database)? {
137+
let start = std::time::Instant::now();
135138
match node {
136139
TrieElement::Branch(node) => {
140+
tracker.inc_branch();
137141
hash_builder.add_branch(node.key, node.value, node.children_are_in_trie);
138142
}
139143
TrieElement::Leaf(hashed_address, account) => {
140144
let (storage_root, _, updates) = match storage_roots.remove(&hashed_address) {
141-
Some(result) => result,
145+
Some(result) => {
146+
tracker.inc_leaf();
147+
result
148+
},
142149
// Since we do not store all intermediate nodes in the database, there might
143150
// be a possibility of re-adding a non-modified leaf to the hash builder.
144151
None => {
@@ -164,7 +171,10 @@ where
164171
hash_builder.add_leaf(Nibbles::unpack(hashed_address), &account_rlp);
165172
}
166173
}
174+
hash_elapsed += start.elapsed().as_micros();
167175
}
176+
debug!(target: "trie::parallel_state_root", "test info: total elapsed in account node {:?}", start.elapsed());
177+
debug!(target: "trie::parallel_state_root", "test info: hash elapsed in account node {:?}us", hash_elapsed);
168178

169179
let root = hash_builder.root();
170180

@@ -179,15 +189,15 @@ where
179189
#[cfg(feature = "metrics")]
180190
self.metrics.record_state_trie(stats);
181191

182-
trace!(
192+
debug!(
183193
target: "trie::parallel_state_root",
184194
%root,
185195
duration = ?stats.duration(),
186196
branches_added = stats.branches_added(),
187197
leaves_added = stats.leaves_added(),
188198
missed_leaves = stats.missed_leaves(),
189199
precomputed_storage_roots = stats.precomputed_storage_roots(),
190-
"calculated state root"
200+
"test info: calculated state root"
191201
);
192202

193203
Ok((root, trie_updates))

crates/trie/prefetch/src/prefetch.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use reth_trie_parallel::{parallel_root::ParallelStateRootError, StorageRootTarge
1717
use std::{collections::HashMap, sync::Arc};
1818
use thiserror::Error;
1919
use tokio::{
20-
sync::{mpsc::UnboundedReceiver, oneshot::Receiver},
20+
sync::{mpsc::UnboundedReceiver, oneshot::Receiver, Mutex},
2121
task::JoinSet,
2222
};
2323
use tracing::{debug, trace};
@@ -29,11 +29,19 @@ pub struct TriePrefetch {
2929
cached_accounts: HashMap<B256, bool>,
3030
/// Cached storages.
3131
cached_storages: HashMap<B256, HashMap<B256, bool>>,
32+
global_stats: Arc<Mutex<GlobalStats>>,
3233
/// State trie metrics.
3334
#[cfg(feature = "metrics")]
3435
metrics: TrieRootMetrics,
3536
}
3637

38+
#[derive(Default, Debug)]
39+
pub struct GlobalStats {
40+
pub branch_count: usize,
41+
pub leaf_count: usize,
42+
pub missing_count: usize,
43+
}
44+
3745
impl Default for TriePrefetch {
3846
fn default() -> Self {
3947
Self::new()
@@ -48,6 +56,7 @@ impl TriePrefetch {
4856
cached_storages: HashMap::new(),
4957
#[cfg(feature = "metrics")]
5058
metrics: TrieRootMetrics::default(),
59+
global_stats: Arc::new(Mutex::new(GlobalStats::default())),
5160
}
5261
}
5362

@@ -70,8 +79,9 @@ impl TriePrefetch {
7079
let hashed_state = self.deduplicate_and_update_cached(state);
7180

7281
let self_clone = Arc::new(self.clone());
82+
let global_stats = Arc::clone(&self.global_stats);
7383
join_set.spawn(async move {
74-
if let Err(e) = self_clone.prefetch_once::<DB>(consistent_view, hashed_state).await {
84+
if let Err(e) = self_clone.prefetch_once::<DB>(consistent_view, hashed_state, global_stats).await {
7585
debug!(target: "trie::trie_prefetch", ?e, "Error while prefetching trie storage");
7686
};
7787
});
@@ -80,6 +90,7 @@ impl TriePrefetch {
8090
_ = &mut interrupt_rx => {
8191
debug!(target: "trie::trie_prefetch", "Interrupted trie prefetch task. Unprocessed tx {:?}", prefetch_rx.len());
8292
join_set.abort_all();
93+
debug!(target: "trie::trie_prefetch", "test info: prefetch trie node count: {:?}", self.global_stats.lock().await);
8394
return
8495
}
8596
}
@@ -141,6 +152,7 @@ impl TriePrefetch {
141152
self: Arc<Self>,
142153
consistent_view: Arc<ConsistentDbView<DB, ProviderFactory<DB>>>,
143154
hashed_state: HashedPostState,
155+
global_stats: Arc<Mutex<GlobalStats>>,
144156
) -> Result<(), TriePrefetchError>
145157
where
146158
DB: Database,
@@ -201,22 +213,33 @@ impl TriePrefetch {
201213
match node {
202214
TrieElement::Branch(_) => {
203215
tracker.inc_branch();
216+
let mut stats = global_stats.lock().await;
217+
stats.branch_count += 1;
204218
}
205219
TrieElement::Leaf(hashed_address, _) => {
206220
match storage_roots.remove(&hashed_address) {
207-
Some(result) => result,
221+
Some(result) => {
222+
let mut stats = global_stats.lock().await;
223+
stats.leaf_count += 1;
224+
result
225+
}
208226
// Since we do not store all intermediate nodes in the database, there might
209227
// be a possibility of re-adding a non-modified leaf to the hash builder.
210-
None => StorageRoot::new_hashed(
211-
trie_cursor_factory.clone(),
212-
hashed_cursor_factory.clone(),
213-
hashed_address,
214-
#[cfg(feature = "metrics")]
215-
self.metrics.clone(),
216-
)
217-
.prefetch()
218-
.ok()
219-
.unwrap_or_default(),
228+
None => {
229+
let mut stats = global_stats.lock().await;
230+
stats.missing_count += 1;
231+
232+
StorageRoot::new_hashed(
233+
trie_cursor_factory.clone(),
234+
hashed_cursor_factory.clone(),
235+
hashed_address,
236+
#[cfg(feature = "metrics")]
237+
self.metrics.clone(),
238+
)
239+
.prefetch()
240+
.ok()
241+
.unwrap_or_default()
242+
}
220243
};
221244
tracker.inc_leaf();
222245
}

0 commit comments

Comments
 (0)