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): reveal extension child in sparse trie when updating a leaf #13183

Merged
merged 2 commits into from
Dec 9, 2024

Conversation

shekhirin
Copy link
Collaborator

@shekhirin shekhirin commented Dec 6, 2024

Consider the following scenario:

  1. We need to insert a leaf with the path 0x77853ecb3c80cc9dd0543d964574eefd6e89131a8e875c3606451dabd99ae6f4
  2. We query the proof, and reveal nodes down to 0x77853ec which is an extension node Extension(ExtensionNode { key: Nibbles("0d"), child: "a04d1721cf2c8ae2649fffbf4c5bc6e71ac1646e592ff4bfeb3054aa4d56b69456" })
  3. We get to inserting the leaf 0x77853ecb3c80cc9dd0543d964574eefd6e89131a8e875c3606451dabd99ae6f4 and turn the extension node into a branch node, setting the old nibble d from the extension node in the state mask of the new branch node.

The problem here is that the node under the nibble d may be a branch node that will need to appear in the hash mask of the newly created branch node, but we don't know if that's a branch node unless we reveal the extension node child.


This PR reveals the original extension node child when we turn an extension node into a branch node with two children.

@shekhirin shekhirin added C-bug An unexpected or incorrect behavior A-trie Related to Merkle Patricia Trie implementation labels Dec 6, 2024
@shekhirin shekhirin marked this pull request as ready for review December 6, 2024 18:07
*key = current.slice(current.len() - key.len()..common);

// Check if the extension node child is a hash that needs to be revealed
if self.nodes.get(&current).unwrap().is_hash() {
Copy link
Member

Choose a reason for hiding this comment

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

only if updates are enabled

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@shekhirin shekhirin force-pushed the alexey/update-leaf-extension branch from 2d8c899 to 522273f Compare December 9, 2024 13:00
@shekhirin shekhirin requested a review from rkrasiuk December 9, 2024 14:45
Copy link
Member

@rkrasiuk rkrasiuk left a comment

Choose a reason for hiding this comment

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

lgtm

Comment on lines +838 to +840
if self.updates.is_some() {
// Check if the extension node child is a hash that needs to be revealed
if self.nodes.get(&current).unwrap().is_hash() {
Copy link
Member

Choose a reason for hiding this comment

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

surprised clippy does not complain

@shekhirin shekhirin added this pull request to the merge queue Dec 9, 2024
Merged via the queue into main with commit da5ffc2 Dec 9, 2024
41 checks passed
@shekhirin shekhirin deleted the alexey/update-leaf-extension branch December 9, 2024 15:25
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.

2 participants