-
Notifications
You must be signed in to change notification settings - Fork 265
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
feat: Avoid inserting an empty leaf in indexed trees on update #10281
Merged
+267
−191
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
07c34b5
feat: Avoid inserting in indexed trees on update
sirasistant bac1a18
fix is_empty for public data tree leaf
sirasistant fa55e7f
Merge branch 'master' into arv/no_insert_on_update
sirasistant aaea0f5
Merge branch 'arv/no_insert_on_update' of github.com:AztecProtocol/az…
sirasistant 6d7355f
cpp improvements
sirasistant daa898a
docs
sirasistant 3634530
Merge branch 'master' into arv/no_insert_on_update
sirasistant 4325962
Merge branch 'master' into arv/no_insert_on_update
sirasistant 9f33898
remove zero leaf insertion from witgen
sirasistant 8895dfb
Merge branch 'arv/no_insert_on_update' of github.com:AztecProtocol/az…
sirasistant 1748df6
Merge branch 'master' into arv/no_insert_on_update
sirasistant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ pub mod check_valid_low_leaf; | |
use crate::{ | ||
abis::append_only_tree_snapshot::AppendOnlyTreeSnapshot, | ||
merkle_tree::{ | ||
leaf_preimage::{IndexedTreeLeafPreimage, IndexedTreeLeafValue}, | ||
membership::{assert_check_membership, MembershipWitness}, | ||
root::{calculate_empty_tree_root, calculate_subtree_root, root_from_sibling_path}, | ||
}, | ||
|
@@ -112,14 +113,14 @@ pub fn insert<Value, Leaf, let TreeHeight: u32>( | |
build_insertion_leaf: fn(Value, Leaf) -> Leaf, | ||
) -> AppendOnlyTreeSnapshot | ||
where | ||
Value: Eq + Empty, | ||
Leaf: Hash + Empty, | ||
Value: IndexedTreeLeafValue, | ||
Leaf: IndexedTreeLeafPreimage, | ||
{ | ||
Comment on lines
+116
to
118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to refactor in a followup the indexed tree insertions code using the leaf and preimage traits. the original code was done when traits weren't a thing so we were passing callbacks to the functions, which is uglier! |
||
assert(is_valid_low_leaf(low_leaf_preimage, value), "Invalid low leaf"); | ||
|
||
// perform membership check for the low leaf against the original root | ||
assert_check_membership( | ||
low_leaf_preimage.hash(), | ||
low_leaf_preimage.as_leaf(), | ||
low_leaf_membership_witness.leaf_index, | ||
low_leaf_membership_witness.sibling_path, | ||
snapshot.root, | ||
|
@@ -129,29 +130,34 @@ where | |
let updated_low_leaf = | ||
update_low_leaf(low_leaf_preimage, value, snapshot.next_available_leaf_index); | ||
|
||
// Update low leaf | ||
snapshot.root = root_from_sibling_path( | ||
updated_low_leaf.hash(), | ||
updated_low_leaf.as_leaf(), | ||
low_leaf_membership_witness.leaf_index, | ||
low_leaf_membership_witness.sibling_path, | ||
); | ||
|
||
let insertion_leaf = build_insertion_leaf(value, low_leaf_preimage); | ||
|
||
assert_check_membership( | ||
0, | ||
snapshot.next_available_leaf_index as Field, | ||
insertion_sibling_path, | ||
snapshot.root, | ||
); | ||
|
||
// Calculate the new root | ||
snapshot.root = root_from_sibling_path( | ||
insertion_leaf.hash(), | ||
snapshot.next_available_leaf_index as Field, | ||
insertion_sibling_path, | ||
); | ||
|
||
snapshot.next_available_leaf_index += 1; | ||
|
||
snapshot | ||
if low_leaf_preimage.get_key() == value.get_key() { | ||
// If it's an update, we don't need to insert the new leaf and advance the tree | ||
snapshot | ||
} else { | ||
let insertion_leaf = build_insertion_leaf(value, low_leaf_preimage); | ||
assert_check_membership( | ||
0, | ||
snapshot.next_available_leaf_index as Field, | ||
insertion_sibling_path, | ||
snapshot.root, | ||
); | ||
|
||
// Calculate the new root | ||
snapshot.root = root_from_sibling_path( | ||
insertion_leaf.as_leaf(), | ||
snapshot.next_available_leaf_index as Field, | ||
insertion_sibling_path, | ||
); | ||
|
||
snapshot.next_available_leaf_index += 1; | ||
|
||
snapshot | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was triggering a bug: slot 0 was being updated to the value
128
in the avm_tree.test.ts and the insertion code thought that the write was empty, since it was only checking the slot.