Skip to content

Commit

Permalink
feat(trie): update sparse trie hashes below level (paradigmxyz#11969)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin authored Oct 22, 2024
1 parent 468ac0d commit e70b112
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions crates/trie/sparse/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Nibbles>::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));
}
}
}
Expand Down

0 comments on commit e70b112

Please sign in to comment.