From e70b112420b1b06fc0d451ec79d2c01a561dd8af Mon Sep 17 00:00:00 2001 From: Alexey Shekhirin Date: Tue, 22 Oct 2024 19:04:58 +0100 Subject: [PATCH] feat(trie): update sparse trie hashes below level (#11969) --- crates/trie/sparse/src/trie.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/crates/trie/sparse/src/trie.rs b/crates/trie/sparse/src/trie.rs index a0f7a0c305..39deb50e7a 100644 --- a/crates/trie/sparse/src/trie.rs +++ b/crates/trie/sparse/src/trie.rs @@ -518,34 +518,35 @@ impl RevealedSparseTrie { } } - /// Update node hashes only if their path exceeds the provided level. - pub fn update_rlp_node_level(&mut self, min_len: usize) { - let mut paths = Vec::from([Nibbles::default()]); + /// Update hashes of the nodes that are located at a level deeper than or equal to the provided + /// depth. Root node has a level of 0. + pub fn update_rlp_node_level(&mut self, depth: usize) { + let mut paths = Vec::from([(Nibbles::default(), 0)]); let mut targets = HashSet::::default(); - while let Some(mut path) = paths.pop() { + while let Some((mut path, level)) = paths.pop() { match self.nodes.get(&path).unwrap() { SparseNode::Empty | SparseNode::Hash(_) => {} SparseNode::Leaf { .. } => { targets.insert(path); } SparseNode::Extension { key, .. } => { - if path.len() >= min_len { + if level >= depth { targets.insert(path); } else { path.extend_from_slice_unchecked(key); - paths.push(path); + paths.push((path, level + 1)); } } SparseNode::Branch { state_mask, .. } => { - if path.len() >= min_len { + if level >= depth { targets.insert(path); } else { for bit in CHILD_INDEX_RANGE { if state_mask.is_bit_set(bit) { let mut child_path = path.clone(); child_path.push_unchecked(bit); - paths.push(child_path); + paths.push((child_path, level + 1)); } } }