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

Implement trie.put #3473

Merged
merged 14 commits into from
Jun 28, 2024
Merged

Implement trie.put #3473

merged 14 commits into from
Jun 28, 2024

Conversation

acolytec3
Copy link
Contributor

This builds out trie.put for the VerkleTrie class

Copy link

codecov bot commented Jun 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.11%. Comparing base (d24ca11) to head (c130d5a).
Report is 35 commits behind head on master.

Current head c130d5a differs from pull request most recent head 6607f74

Please upload reports for the commit 6607f74 to get more accurate results.

Additional details and impacted files

Impacted file tree graph

Flag Coverage Δ
blockchain 90.97% <ø> (ø)
common 94.38% <ø> (+0.09%) ⬆️
devp2p 81.83% <ø> (?)
statemanager 74.81% <ø> (+0.51%) ⬆️
trie 60.85% <ø> (?)
util 81.46% <ø> (+0.03%) ⬆️

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

@acolytec3 acolytec3 changed the title Implement trie.put [WIP] Implement trie.put Jun 25, 2024
// TODO: Rewrite following logic in verkle.spec.ts "findPath validation" test
async put(_key: Uint8Array, _value: Uint8Array): Promise<void> {
throw new Error('not implemented')
async put(key: Uint8Array, value: Uint8Array): Promise<void> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is very long. I find functions more difficult to read and debug when I have to scroll in order to see different parts of it.

I appreciate the way you use the comments to outline how things are happening step by step. I wonder if these different steps could be externalized into separate functions, so that the put function itself can fit on a screen?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a 100% valid criticism. Lemme look at the logic and see if there's a way to break it down. I think you're right that it can be done in smaller chunks.

@acolytec3 acolytec3 mentioned this pull request Jun 26, 2024
18 tasks
Comment on lines 238 to 243
if (foundPath.node.type !== VerkleNodeType.Leaf) {
throw new Error(
`expected leaf node found at ${bytesToHex(stem)}. Got internal node instead`
)
}
leafNode = foundPath.node as LeafNode
Copy link
Contributor

Choose a reason for hiding this comment

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

I can create some type guards for those, I think this will be useful in qute a few places.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just did: 9b00803 (#3473)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good!

let lastPath = leafNode.stem

// Step 2) Determine if a new internal node is needed
if (foundPath.stack.length > 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just to make sure I am getting this correctly, could you confirm that this scenario is correct:

  • Let's say we start with a root node
  • We put a key/value pair where the key starts with 0x01.
  • findPath returns the rootNode.
  • We create a leafNode, and add a pointer to the newly created leaf node at index 2 of the root node.
  • We put another key/value pair that also starts with 0x01.
  • findPath now returns the leafNode that was created in the previous put operation, along with the remaining values where the paths branch off.
  • We then create an internal node where the paths branch off, commit to the two children to it, and then commit the internal node to the root node (at index 2).
  • rootHash gets updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That should be how it works. Not guaranteeing I got it exactly right but the tests seem to indicate so

Copy link
Contributor

Choose a reason for hiding this comment

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

Cool, I didn't infer the above from how the function/tests were structured, but wrote it based on my understanding of verkle trees, good to hear it matches

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at your comment again, let me confirm something. What you call "index 2" here would be rootNode.children[1] right? These are zero indexed arrays and so inserting a node that starts with 0x01 means you check rootNode.children[1] to see if there's an existing reference to some child node and if not, create a new leaf node with the value for the key that starts with 0x01 and then put the commitment from that new leaf node in rootNode.children[1]

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yes that's what I meant, sorry for the confusing indexing

}

/**
* Helper method for updating or creating the parent internal node for a given leaf node
* @param leafNode the reference leaf node
Copy link
Contributor

Choose a reason for hiding this comment

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

By reference leaf node I assume you mean the "new leaf node". Do you think that "newLeafNode/new leaf node" would be a clearer naming?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, it could be a new leaf node but it could also just be an existing leaf node where we've updated a value. In this instance, we wouldn't be creating a new internal node above but would still update the parent. Maybe I can call it "child leaf node"?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah that's fair. I'm not actually sure I find child leaf node clearer. I think the comment makes it clear enough, thanks for addressing.

gabrocheleau
gabrocheleau previously approved these changes Jun 28, 2024
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.

Excellent work! Pushed and implemented some typeguards, and left some minor nitpicks that you might want to consider. Othewise this can be merged imo!

@gabrocheleau gabrocheleau merged commit 81cd86d into master Jun 28, 2024
33 of 34 checks passed
@holgerd77 holgerd77 deleted the put-put-put branch July 11, 2024 08:12
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