forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
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: Add refnode at InitialVersion pointing at version 1 root #2
Merged
drklee3
merged 17 commits into
release/v1.2.0-kava-v0.26-x
from
dl-v1.2.0-initial-node-ver
Aug 21, 2024
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
2d4c81a
test: Update test for loading patched initial version
drklee3 628affc
fix: Add refnode to initial version to point at version 1
drklee3 5cf60dd
docs: Update comments on patch
drklee3 33f1680
fix: Update WorkingHash() to also use tree.version + 1
drklee3 882046b
ci: Run test workflow on release branches
drklee3 2d28d39
fix: Prevent nil panic on reading tree.root.nodeKey on empty tree
drklee3 f73700b
test: Add test for empty tree, pruning
drklee3 6c6dbcf
test: Temp skip ndb.hasVersion(1)
drklee3 178156c
test: Add cases for node db versions
drklee3 2644f53
fix: Hide root node at version 1
drklee3 01ca777
chore: remove todo comment
drklee3 a6d56f9
refactor: Alternative way to set the initialversion root node
drklee3 0e0bd3e
Revert "refactor: Alternative way to set the initialversion root node"
drklee3 5237676
doc: Clarify patch comments
drklee3 c1d6db0
test: Ensure apphash stays the same for different InitialVersions
drklee3 cdfc587
test: Update skipped (1, 0) root node test
drklee3 d5920a1
test: Add test for rollbacks on new tree and existing ndb
drklee3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ on: | |
push: | ||
branches: | ||
- master | ||
- release/* | ||
pull_request: | ||
|
||
jobs: | ||
|
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 |
---|---|---|
|
@@ -137,7 +137,9 @@ func (tree *MutableTree) Hash() []byte { | |
|
||
// WorkingHash returns the hash of the current working tree. | ||
func (tree *MutableTree) WorkingHash() []byte { | ||
return tree.root.hashWithCount(tree.WorkingVersion()) | ||
// Use tree.version instead of tree.WorkingVersion() because tree.version | ||
// is always the latest version | ||
return tree.root.hashWithCount(tree.version + 1) | ||
} | ||
|
||
func (tree *MutableTree) WorkingVersion() int64 { | ||
|
@@ -746,6 +748,7 @@ func (tree *MutableTree) SaveVersion() ([]byte, int64, error) { | |
return nil, 0, err | ||
} | ||
} else { | ||
// nodeKey already set, no changes to tree | ||
if tree.root.nodeKey != nil { | ||
// it means there are no updated nodes | ||
if err := tree.ndb.SaveRoot(version, tree.root.nodeKey); err != nil { | ||
|
@@ -761,10 +764,43 @@ func (tree *MutableTree) SaveVersion() ([]byte, int64, error) { | |
} | ||
} | ||
} else { | ||
// No nodeKey, assign new nodeKeys and save nodes. This happens when | ||
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. Well done explaining the logic branches here 👍 |
||
// any node in the tree has been updated as setting a key will | ||
// reset all nodeKeys to nil | ||
if err := tree.saveNewNodes(); err != nil { | ||
return nil, 0, err | ||
} | ||
} | ||
|
||
// Addition to node version patch for backwards compatibility: | ||
// This is to resolve queries at InitialVersion AND at version 1. | ||
// Only applies when InitialVersion > 1 and on the first save. | ||
// The root node on a new tree is always at version 1 to maintain app | ||
// hash backwards compatibility: | ||
// InitialVersion != nodekey.Version | ||
if tree.ndb.opts.InitialVersion > 1 && version == int64(tree.ndb.opts.InitialVersion) { | ||
// SaveRoot is meant for saving a tree when there are no updates, which | ||
// simply creates a reference node to the root node. We reuse this to | ||
// create a reference node at InitialVersion with value of the root | ||
// node at (1, 1). | ||
if err := tree.ndb.SaveRoot(version, tree.root.nodeKey); err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
// Delete the root node at (1, 1) to be re-saved at (1, 0) below. | ||
if err := tree.ndb.deleteFromPruning(tree.ndb.nodeKey(tree.root.nodeKey.GetKey())); err != nil { | ||
return nil, 0, err | ||
} | ||
|
||
// Use a nonce of 0 to match pruning behavior that hides it from | ||
// the version list. | ||
// ndb.GetRoot will check for (version, 0) if (version, 1) does not | ||
// exist. | ||
tree.root.nodeKey.nonce = 0 | ||
if err := tree.ndb.SaveNode(tree.root); err != nil { | ||
return nil, 0, err | ||
} | ||
} | ||
} | ||
|
||
if err := tree.ndb.Commit(); err != nil { | ||
|
Oops, something went wrong.
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.
just saying out loud here that this must've been included in the live data we have or we would have gotten app hash mismatches with mainnet?
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.
Cosmos v0.47.x doesn't use the WorkingHash/WorkingVersion afaik, they are just defined on the iavl interface and unused by the rootmultistore
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.
right - this mostly updated to ensure the tree tests pass and keep the behavior consistent.
I thought it could be a potential issue with rollbacks via
LoadVersionForOverwriting
- two cases:InitialVersion-1
to writeInitialVersion
againSaveVersion()
that callsWorkingHash()
that produces an incorrect mismatchAdded a test for the second case to make sure we aren't depending on cosmos handling it properly and WorkingHash() is correct in that case
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.
Right, I forgot about the
WorkingHash
within those methods 🤦. I read through theLoadVersionForOverwriting
test, nice comments there too -- some great thinking into how the iavl library is used 💯