Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(trie): update sparse trie hashes below level #11969

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading