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

Remove empty root when possible Version 2 #2160

Closed

Conversation

dboehm-avalabs
Copy link
Contributor

Currently the MerkleDB always has the node with the nil key as its root. This isn't required for the trie structure whenever the root has no value and only a single child. In those cases, switch to using that child as the root of the trie.

Alternative to #2106

x/merkledb/db.go Show resolved Hide resolved
x/merkledb/db.go Outdated Show resolved Hide resolved
Comment on lines +56 to +57
rootNode *node
rootID ids.ID

Choose a reason for hiding this comment

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

Can we add comments for these fields?

rootNode *node
rootID ids.ID
nodes map[Path]*change[*node]
values map[Path]*change[maybe.Maybe[[]byte]]
}

func newChangeSummary(estimatedSize int) *changeSummary {

Choose a reason for hiding this comment

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

It feels kind of weird that we don't always populate rootNode and rootID in changeSummary. It makes it hard to reason about when these are/aren't needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what you mean. They are always populated once the changes are finalized.

x/merkledb/trie_test.go Show resolved Hide resolved
Comment on lines 253 to 254
// If the root has no value and only a single child, that child can become the root.
// Use valueDigest instead of value because proof verification only sets the digest

Choose a reason for hiding this comment

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

If the root has no value and a single child, why is it t.root here? Shouldn't its child become the root in remove/insert?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a tricky case where it was hard to detect in insert/remove. It was easier to catch it here.

}

// the node passed in was the old root so update the parent to be the new root
if parent == nil {

Choose a reason for hiding this comment

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

Can we document what it means when parent == nil in a comment on this method?

)

if !key.HasPrefix(t.root.key) {

Choose a reason for hiding this comment

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

// Always returns at least the root node.

this is no longer true

@dhrubabasu dhrubabasu deleted the RemoveEmptyRootWhenPossible branch January 9, 2024 02:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants