Skip to content

Commit

Permalink
clarify the doc
Browse files Browse the repository at this point in the history
  • Loading branch information
shekhirin committed Oct 26, 2024
1 parent cac80c0 commit b7f601c
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions crates/trie/sparse/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,16 +571,20 @@ impl RevealedSparseTrie {
pub fn update_rlp_node_level(&mut self, depth: usize) {
let mut prefix_set = self.prefix_set.clone().freeze();

let targets = self.get_nodes_at_depth(&mut prefix_set, depth);
let targets = self.get_changed_nodes_at_depth(&mut prefix_set, depth);
for target in targets {
self.rlp_node(target, &mut prefix_set);
}
}

/// Returns a list of paths to the nodes that are located at the provided depth when counting
/// from the root node. If there's a leaf at a depth less than the provided depth, it will be
/// included in the result.
fn get_nodes_at_depth(&self, prefix_set: &mut PrefixSet, depth: usize) -> HashSet<Nibbles> {
/// Returns a list of paths to the nodes that were changed according to the prefix set and are
/// located at the provided depth when counting from the root node. If there's a leaf at a
/// depth less than the provided depth, it will be included in the result.
fn get_changed_nodes_at_depth(
&self,
prefix_set: &mut PrefixSet,
depth: usize,
) -> HashSet<Nibbles> {
let mut paths = Vec::from([(Nibbles::default(), 0)]);
let mut targets = HashSet::<Nibbles>::default();

Expand Down Expand Up @@ -1547,7 +1551,7 @@ mod tests {
}

#[test]
fn sparse_trie_get_nodes_at_depth() {
fn sparse_trie_get_changed_nodes_at_depth() {
let mut sparse = RevealedSparseTrie::default();

let value = alloy_rlp::encode_fixed_size(&U256::ZERO).to_vec();
Expand Down Expand Up @@ -1582,23 +1586,23 @@ mod tests {
sparse.update_leaf(Nibbles::from_nibbles([0x5, 0x3, 0x3, 0x2, 0x0]), value).unwrap();

assert_eq!(
sparse.get_nodes_at_depth(&mut PrefixSet::default(), 0),
sparse.get_changed_nodes_at_depth(&mut PrefixSet::default(), 0),
HashSet::from([Nibbles::default()])
);
assert_eq!(
sparse.get_nodes_at_depth(&mut PrefixSet::default(), 1),
sparse.get_changed_nodes_at_depth(&mut PrefixSet::default(), 1),
HashSet::from([Nibbles::from_nibbles_unchecked([0x5])])
);
assert_eq!(
sparse.get_nodes_at_depth(&mut PrefixSet::default(), 2),
sparse.get_changed_nodes_at_depth(&mut PrefixSet::default(), 2),
HashSet::from([
Nibbles::from_nibbles_unchecked([0x5, 0x0]),
Nibbles::from_nibbles_unchecked([0x5, 0x2]),
Nibbles::from_nibbles_unchecked([0x5, 0x3])
])
);
assert_eq!(
sparse.get_nodes_at_depth(&mut PrefixSet::default(), 3),
sparse.get_changed_nodes_at_depth(&mut PrefixSet::default(), 3),
HashSet::from([
Nibbles::from_nibbles_unchecked([0x5, 0x0, 0x2, 0x3]),
Nibbles::from_nibbles_unchecked([0x5, 0x2]),
Expand All @@ -1607,7 +1611,7 @@ mod tests {
])
);
assert_eq!(
sparse.get_nodes_at_depth(&mut PrefixSet::default(), 4),
sparse.get_changed_nodes_at_depth(&mut PrefixSet::default(), 4),
HashSet::from([
Nibbles::from_nibbles_unchecked([0x5, 0x0, 0x2, 0x3, 0x1]),
Nibbles::from_nibbles_unchecked([0x5, 0x0, 0x2, 0x3, 0x3]),
Expand Down

0 comments on commit b7f601c

Please sign in to comment.