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

refactor: simplify trie_walker get_proof code #1384

Merged
merged 1 commit into from
Aug 17, 2024

Conversation

KolbyML
Copy link
Member

@KolbyML KolbyML commented Aug 16, 2024

What was wrong?

the reverse logic for path was a little overly complex

How was it fixed?

by making the logic less complex

@KolbyML KolbyML requested a review from morph-dev August 16, 2024 05:53
@KolbyML KolbyML self-assigned this Aug 16, 2024
@@ -140,24 +140,22 @@ impl TrieWalker {

pub fn get_proof(&self, node_hash: B256) -> TrieProof {
// Build path and proof from the node to the root and reverse it at the end.
let mut reverse_path = vec![];
let mut reverse_path_parts = vec![];
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about using VecDeque. Something like this:

pub fn get_proof(&self, node_hash: B256) -> TrieProof {
    let mut path_parts = VecDeque::new();
    let mut proof = VecDeque::new();
    let mut next_node: Option<B256> = Some(node_hash);
    while let Some(current_node) = next_node {
        let Some(node) = self.nodes.get(&current_node) else {
            panic!("Node not found in trie walker nodes. This should never happen.");
        };
        path_parts.push_front(node.path_nibbles.clone());
        proof.push_front(node.encoded_node.clone().into());
        next_node = node.parent_hash;
    }

    TrieProof {
        path: Vec::from(path_parts).concat(),
        proof: Vec::from(proof),
    }
}

In my opinion, it's easier to follow logic without tracking what is reversed and what not. It's up to you (i'm fine with merging as is as well).

@KolbyML KolbyML merged commit 3a05fd4 into ethereum:master Aug 17, 2024
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants