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): do not reveal same node twice in sparse trie #14370

Merged
merged 6 commits into from
Feb 10, 2025

Conversation

shekhirin
Copy link
Collaborator

@shekhirin shekhirin commented Feb 10, 2025

Problem

The issue is that it's possible to reveal the same leaf twice if we request two proofs with two leaves that have the same prefix.

For example, in DB we have a leaf at path 0x0d07. First, we request a proof for leaf 0xd7b6990105719101dabeb77144f2a3385c8033acd3af97e9423a695e81ad1eb5. Then, we request another proof for leaf 0xd7999c7eedf8fe42767a51577f99fda631d84616191df1557bdaf61266d16a0f.

In both cases, the multiproof will have a leaf at path 0xd07, and we will try to reveal it. Current prevention for not revealing it second time is this check

// If the node is already revealed and it's not a hash node, do nothing.
if self.nodes.get(&path).is_some_and(|node| !node.is_hash()) {
return Ok(())
}
, but it will not pass if the first leaf 0xd7b6990105719101dabeb77144f2a3385c8033acd3af97e9423a695e81ad1eb5 was deleted after it was revealed, so we will reveal the leaf node agian.

It's problematic because we could have already deleted a leaf node, and we should not insert it again on proof revealing.

Solution

Maintain a list of revealed paths per account trie and each of storage tries.

@shekhirin shekhirin added C-bug An unexpected or incorrect behavior A-trie Related to Merkle Patricia Trie implementation labels Feb 10, 2025
@shekhirin shekhirin force-pushed the alexey/sparse-trie-revealed-nodes branch from 496c81d to 557b344 Compare February 10, 2025 13:52
@shekhirin shekhirin force-pushed the alexey/sparse-trie-revealed-nodes branch from 557b344 to 50bd2cb Compare February 10, 2025 13:54
@shekhirin shekhirin force-pushed the alexey/sparse-trie-revealed-nodes branch from 9fd7871 to 7f341a0 Compare February 10, 2025 14:57
@shekhirin shekhirin requested a review from rkrasiuk February 10, 2025 14:59
Comment on lines +29 to +32
/// Collection of revealed account trie paths.
revealed_account_paths: HashSet<Nibbles>,
/// Collection of revealed storage trie paths, per account.
revealed_storage_paths: B256HashMap<HashSet<Nibbles>>,
Copy link
Member

Choose a reason for hiding this comment

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

should we keep these on trie level instead?

@shekhirin shekhirin added this pull request to the merge queue Feb 10, 2025
Merged via the queue into main with commit 30488a1 Feb 10, 2025
44 checks passed
@shekhirin shekhirin deleted the alexey/sparse-trie-revealed-nodes branch February 10, 2025 17:24
@mattsse mattsse added this to the Release 1.2.0 milestone Feb 11, 2025
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