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

Add trie.del #3486

Merged
merged 11 commits into from
Jul 9, 2024
Merged

Add trie.del #3486

merged 11 commits into from
Jul 9, 2024

Conversation

acolytec3
Copy link
Contributor

Adds trie.del as a simple extension of trie.put

  • Adds a new trie.del function which wraps trie.put
  • Add special handling in trie.put for the deleted leaf value
    1. If no leaf node exists at the specified key (i.e. findPath returns no leaf node), return early (since you can't delete a value in a leaf node that doesn't exist)
    2. Writes the special VerkleLeafNodeValues.Deleted when inserting the value into the leaf node.

@@ -250,11 +250,28 @@ export class VerkleTree {
)
}
} else {
if (equalsBytes(value, createDeletedLeafValue())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this not throw an error instead of silently returning?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now that I've re-read the spec, I'm not sure what the expected behavior is. If it's just writing 0 to the trie, then this behavior is actually invalid and we should just let it create a new leaf node and write what I'm calling the deleted leaf value here. Will ask kev

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My instinct here is that we should actually just create a new leaf node and write zeroes to it. There is no "deletion" in verkle tries so writing zeroes to a leaf value is the same as "deleting" a value in the trie. Will update this section of code accordingly (i.e. just remove this special handling and allow `trie.del to write zeroes)

@@ -211,7 +211,7 @@ describe('Verkle tree', () => {
assert.deepEqual(val2, hexToBytes(values[2]), 'confirm values[2] can be retrieved from trie')
})

Copy link
Contributor

Choose a reason for hiding this comment

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

We could also add a test for the expected behavior when attempting to delete a value that does not exist? Left a related comment in the other file (i.e. should this throw or return?)

Copy link
Contributor

@gabrocheleau gabrocheleau left a comment

Choose a reason for hiding this comment

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

Nice work, cool to see so much progress on the verkle tree front lately!
Left some comments, can be merged once addressed.

Copy link

codecov bot commented Jul 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (d24ca11) to head (380beec).
Report is 46 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (d24ca11) and HEAD (380beec). Click for more details.

HEAD has 4 uploads less than BASE
Flag BASE (d24ca11) HEAD (380beec)
common 1 0
blockchain 1 0
util 1 0
statemanager 1 0
Additional details and impacted files

Impacted file tree graph

Flag Coverage Δ
blockchain ?
common ?
statemanager ?
util ?
wallet 0.00% <ø> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

['DEL']
)
leafNode.setValue(suffix, VerkleLeafNodeValue.Deleted)
}
leafNode.setValue(suffix, value)
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't line 268 overwriting what has been done in line 266 within the if block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indeed. Will address in the next commit.

@gabrocheleau
Copy link
Contributor

One general Q. We currently return undefined for both VerkleNodeLeafValues (Untouched and Deleted) when calling getValue on a leaf node. Is this what we want? Are there ever cases where blurring the distinction between Ontouched & Deleted would be problematic (in which case we might want to return undefined & null respectively, although I know you're not a big fan of treating those differently?)

@acolytec3
Copy link
Contributor Author

One general Q. We currently return undefined for both VerkleNodeLeafValues (Untouched and Deleted) when calling getValue on a leaf node. Is this what we want? Are there ever cases where blurring the distinction between Ontouched & Deleted would be problematic (in which case we might want to return undefined & null respectively, although I know you're not a big fan of treating those differently?)

Good point. We should returns an array of zeroes for the "deleted" case. This comes back to the underlying point that there really is no concept of "deleted" in a verkle trie. There's just untouched, and then touched (with maybe a special flag for zeroes). For the moment, I think it makes the most sense to update trie.get to return an array of 0s unless the leaf value requested is untouched.

@gabrocheleau
Copy link
Contributor

One general Q. We currently return undefined for both VerkleNodeLeafValues (Untouched and Deleted) when calling getValue on a leaf node. Is this what we want? Are there ever cases where blurring the distinction between Ontouched & Deleted would be problematic (in which case we might want to return undefined & null respectively, although I know you're not a big fan of treating those differently?)

Good point. We should returns an array of zeroes for the "deleted" case. This comes back to the underlying point that there really is no concept of "deleted" in a verkle trie. There's just untouched, and then touched (with maybe a special flag for zeroes). For the moment, I think it makes the most sense to update trie.get to return an array of 0s unless the leaf value requested is untouched.

Makes sense! Thanks for addressing.

gabrocheleau
gabrocheleau previously approved these changes Jul 9, 2024
@gabrocheleau gabrocheleau merged commit c6ff99a into master Jul 9, 2024
34 checks passed
@holgerd77 holgerd77 deleted the add-trie.del branch July 9, 2024 20:09
@ethereumjs ethereumjs deleted a comment from Mikhai56 Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants