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

fix(trie): sparse trie walk should be done in a sorted manner #12087

Merged
merged 4 commits into from
Oct 28, 2024

Conversation

shekhirin
Copy link
Collaborator

@shekhirin shekhirin commented Oct 25, 2024

Two fixes:

  1. Iterate children of a branch node in reverse, because we push them to the stack and then pop from it. So pushing a..=f means popping f..=a. We need to push f..=a and pop a..=f to make it a DFS search.
  2. Return paths in the same order as they were popped from the trie, and it's a sorted order. It's crucial for the prefix set to work efficiently.

@shekhirin shekhirin added C-bug An unexpected or incorrect behavior A-trie Related to Merkle Patricia Trie implementation labels Oct 25, 2024
@shekhirin shekhirin changed the title fix(trie): get nodes at depth should return sorted paths fix(trie): sparse trie walk should be done in a sorted manner Oct 25, 2024
Comment on lines 669 to 688
for bit in CHILD_INDEX_RANGE.rev() {
if state_mask.is_bit_set(bit) {
let mut child = path.clone();
child.push_unchecked(bit);
branch_child_buf.push(child);
}
}

branch_value_stack_buf.clear();
for child_path in &branch_child_buf {
branch_value_stack_buf.resize(branch_child_buf.len(), Default::default());
let mut added_children = false;
for (i, child_path) in branch_child_buf.iter().enumerate() {
if rlp_node_stack.last().map_or(false, |e| &e.0 == child_path) {
let (_, child) = rlp_node_stack.pop().unwrap();
branch_value_stack_buf.push(child);
branch_value_stack_buf[branch_child_buf.len() - i - 1] = child;
added_children = true;
} else {
debug_assert!(branch_value_stack_buf.is_empty());
debug_assert!(!added_children);
path_stack.push(path);
path_stack.extend(branch_child_buf.drain(..));
continue 'main
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this reverse?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if children are pushed from 0 to f, then the first one to pop will be f and we will do lookups in a non-sorted order

SparseNode::Leaf { key, hash } => {
self.rlp_buf.clear();
let mut path = path.clone();
path.extend_from_slice_unchecked(key);
if let Some(hash) = hash.filter(|_| !prefix_set.contains(&path)) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didn't see ur reply 😅 , ye, makes sense

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a comment

@rkrasiuk rkrasiuk added this pull request to the merge queue Oct 28, 2024
Merged via the queue into main with commit 0f86287 Oct 28, 2024
40 checks passed
@rkrasiuk rkrasiuk deleted the alexey/sparse-trie-get-nodes-at-depth branch October 28, 2024 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trie Related to Merkle Patricia Trie implementation C-bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants