Skip to content

Commit b6a2a7e

Browse files
committedApr 9, 2025
create a separate function
1 parent d81a508 commit b6a2a7e

File tree

3 files changed

+62
-72
lines changed

3 files changed

+62
-72
lines changed
 

‎crates/trie/sparse/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ alloy-rlp.workspace = true
2424

2525
# misc
2626
auto_impl.workspace = true
27-
itertools.workspace = true
2827
smallvec = { workspace = true, features = ["const_new"] }
2928

3029
# metrics

‎crates/trie/sparse/src/metrics.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -36,23 +36,23 @@ impl SparseStateTrieMetrics {
3636
.record(self.multiproof_total_storage_nodes as f64);
3737
}
3838

39-
/// Increment the skipped account nodes counter
40-
pub(crate) fn increment_skipped_account_nodes(&mut self) {
39+
/// Increment the skipped account nodes counter by the given count
40+
pub(crate) fn increment_skipped_account_nodes(&mut self, count: u64) {
4141
self.multiproof_skipped_account_nodes += 1;
4242
}
4343

44-
/// Increment the total account nodes counter
45-
pub(crate) fn increment_total_account_nodes(&mut self) {
44+
/// Increment the total account nodes counter by the given count
45+
pub(crate) fn increment_total_account_nodes(&mut self, count: u64) {
4646
self.multiproof_total_account_nodes += 1;
4747
}
4848

49-
/// Increment the skipped storage nodes counter
50-
pub(crate) fn increment_skipped_storage_nodes(&mut self) {
49+
/// Increment the skipped storage nodes counter by the given count
50+
pub(crate) fn increment_skipped_storage_nodes(&mut self, count: u64) {
5151
self.multiproof_skipped_storage_nodes += 1;
5252
}
5353

54-
/// Increment the total storage nodes counter
55-
pub(crate) fn increment_total_storage_nodes(&mut self) {
54+
/// Increment the total storage nodes counter by the given count
55+
pub(crate) fn increment_total_storage_nodes(&mut self, count: u64) {
5656
self.multiproof_total_storage_nodes += 1;
5757
}
5858
}

‎crates/trie/sparse/src/state.rs

+54-63
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use alloy_primitives::{
1010
};
1111
use alloy_rlp::{Decodable, Encodable};
1212
use core::fmt;
13-
use itertools::Itertools;
1413
use reth_execution_errors::{SparseStateTrieErrorKind, SparseStateTrieResult, SparseTrieErrorKind};
1514
use reth_primitives_traits::Account;
1615
use reth_tracing::tracing::trace;
@@ -282,34 +281,11 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
282281
branch_node_hash_masks: HashMap<Nibbles, TrieMask>,
283282
branch_node_tree_masks: HashMap<Nibbles, TrieMask>,
284283
) -> SparseStateTrieResult<()> {
285-
let len = account_subtree.len();
286-
let (mut account_nodes, branch_children) = account_subtree
287-
.into_inner()
288-
.into_iter()
289-
.filter_map(|(path, bytes)| {
290-
self.metrics.increment_total_account_nodes();
291-
// If the node is already revealed, skip it.
292-
if self.revealed_account_paths.contains(&path) {
293-
self.metrics.increment_skipped_account_nodes();
294-
return None
295-
}
296-
297-
Some(TrieNode::decode(&mut &bytes[..]).map(|node| (path, node)))
298-
})
299-
.fold_ok(
300-
(Vec::with_capacity(len), 0usize),
301-
|(mut nodes, mut children), (path, node)| {
302-
if let TrieNode::Branch(branch) = &node {
303-
children += branch.state_mask.count_ones() as usize;
304-
}
305-
306-
nodes.push((path, node));
307-
308-
(nodes, children)
309-
},
310-
)?;
311-
account_nodes.sort_unstable_by(|a, b| a.0.cmp(&b.0));
312-
let mut account_nodes = account_nodes.into_iter().peekable();
284+
let DecodedProofNodes { nodes, total_nodes, skipped_nodes, branch_children } =
285+
DecodedProofNodes::new(account_subtree, &self.revealed_account_paths)?;
286+
self.metrics.increment_total_account_nodes(total_nodes);
287+
self.metrics.increment_skipped_account_nodes(skipped_nodes);
288+
let mut account_nodes = nodes.into_iter().peekable();
313289

314290
if let Some(root_node) = Self::validate_root_node_decoded(&mut account_nodes)? {
315291
// Reveal root node if it wasn't already.
@@ -355,35 +331,10 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
355331
) -> SparseStateTrieResult<()> {
356332
let revealed_nodes = self.revealed_storage_paths.entry(account).or_default();
357333

358-
let len = storage_subtree.subtree.len();
359-
let (mut nodes, branch_children) = storage_subtree
360-
.subtree
361-
.into_inner()
362-
.into_iter()
363-
.filter_map(|(path, bytes)| {
364-
self.metrics.increment_total_storage_nodes();
365-
// If the node is already revealed, skip it.
366-
if revealed_nodes.contains(&path) {
367-
self.metrics.increment_skipped_storage_nodes();
368-
return None
369-
}
370-
371-
Some(TrieNode::decode(&mut &bytes[..]).map(|node| (path, node)))
372-
})
373-
.fold_ok(
374-
(Vec::with_capacity(len), 0usize),
375-
|(mut nodes, mut children), (path, node)| {
376-
if let TrieNode::Branch(branch) = &node {
377-
children += branch.state_mask.count_ones() as usize;
378-
}
379-
380-
nodes.push((path, node));
381-
382-
(nodes, children)
383-
},
384-
)?;
385-
386-
nodes.sort_unstable_by(|a, b| a.0.cmp(&b.0));
334+
let DecodedProofNodes { nodes, total_nodes, skipped_nodes, branch_children } =
335+
DecodedProofNodes::new(storage_subtree.subtree, revealed_nodes)?;
336+
self.metrics.increment_total_storage_nodes(total_nodes);
337+
self.metrics.increment_skipped_storage_nodes(skipped_nodes);
387338
let mut nodes = nodes.into_iter().peekable();
388339

389340
if let Some(root_node) = Self::validate_root_node_decoded(&mut nodes)? {
@@ -542,8 +493,6 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
542493
&self,
543494
proof: &mut Peekable<I>,
544495
) -> SparseStateTrieResult<Option<TrieNode>> {
545-
let mut proof = proof.into_iter().peekable();
546-
547496
// Validate root node.
548497
let Some((path, node)) = proof.next() else { return Ok(None) };
549498
if !path.is_empty() {
@@ -559,11 +508,10 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
559508
Ok(Some(root_node))
560509
}
561510

511+
/// Validates the decoded root node of the proof and returns it if it exists and is valid.
562512
fn validate_root_node_decoded<I: Iterator<Item = (Nibbles, TrieNode)>>(
563513
proof: &mut Peekable<I>,
564514
) -> SparseStateTrieResult<Option<TrieNode>> {
565-
let mut proof = proof.into_iter().peekable();
566-
567515
// Validate root node.
568516
let Some((path, root_node)) = proof.next() else { return Ok(None) };
569517
if !path.is_empty() {
@@ -574,7 +522,7 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
574522
.into())
575523
}
576524

577-
// Decode root node and perform sanity check.
525+
// Perform sanity check.
578526
if matches!(root_node, TrieNode::EmptyRoot) && proof.peek().is_some() {
579527
return Err(SparseStateTrieErrorKind::InvalidRootNode {
580528
path,
@@ -833,6 +781,49 @@ impl<F: BlindedProviderFactory> SparseStateTrie<F> {
833781
}
834782
}
835783

784+
/// Decoded proof nodes returned by [`decode_proof_nodes`].
785+
#[derive(Debug)]
786+
struct DecodedProofNodes {
787+
/// Filtered, decoded and sorted proof nodes.
788+
nodes: Vec<(Nibbles, TrieNode)>,
789+
/// Number of nodes in the proof.
790+
total_nodes: u64,
791+
/// Number of nodes that were skipped because they were already revealed.
792+
skipped_nodes: u64,
793+
/// Number of children of all branch nodes in the proof.
794+
branch_children: usize,
795+
}
796+
797+
impl DecodedProofNodes {
798+
fn new(proof_nodes: ProofNodes, revealed_nodes: &HashSet<Nibbles>) -> alloy_rlp::Result<Self> {
799+
let mut result = Self {
800+
nodes: Vec::with_capacity(proof_nodes.len()),
801+
total_nodes: 0,
802+
skipped_nodes: 0,
803+
branch_children: 0,
804+
};
805+
806+
for (path, bytes) in proof_nodes.into_inner() {
807+
result.total_nodes += 1;
808+
// If the node is already revealed, skip it.
809+
if revealed_nodes.contains(&path) {
810+
result.skipped_nodes += 1;
811+
continue
812+
}
813+
814+
let node = TrieNode::decode(&mut &bytes[..])?;
815+
if let TrieNode::Branch(branch) = &node {
816+
result.branch_children += branch.state_mask.count_ones() as usize;
817+
}
818+
819+
result.nodes.push((path, node));
820+
}
821+
822+
result.nodes.sort_unstable_by(|a, b| a.0.cmp(&b.0));
823+
Ok(result)
824+
}
825+
}
826+
836827
#[cfg(test)]
837828
mod tests {
838829
use super::*;

0 commit comments

Comments
 (0)
Please sign in to comment.