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

fix: legacy node key (backport #928) #929

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## v1.0.3 April 8, 2024

### Bug Fixes

- [#928](https://github.com/cosmos/iavl/pull/928) Fix the reformatted root node issue.

## v1.0.2 March 16, 2024

### Bug Fixes
Expand Down
5 changes: 1 addition & 4 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1035,10 +1035,7 @@ func (tree *MutableTree) saveNewNodes(version int64) error {
var recursiveAssignKey func(*Node) ([]byte, error)
recursiveAssignKey = func(node *Node) ([]byte, error) {
if node.nodeKey != nil {
if node.nodeKey.nonce != 0 {
return node.nodeKey.GetKey(), nil
}
return node.hash, nil
return node.GetKey(), nil
}
nonce++
node.nodeKey = &NodeKey{
Expand Down
37 changes: 36 additions & 1 deletion tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1876,12 +1876,47 @@ func TestReferenceRoot(t *testing.T) {
_, _, err = tree.SaveVersion()
require.NoError(t, err)

// Load the tree from disk.
// load the tree from disk
tree = NewMutableTree(db, 0, false, log.NewNopLogger())
_, err = tree.Load()
require.NoError(t, err)
require.Equal(t, int64(2), tree.Version())
// check the root of version 2 is the leaf node of key2
require.Equal(t, tree.root.GetKey(), (&NodeKey{version: 1, nonce: 3}).GetKey())
require.Equal(t, tree.root.key, []byte("key2"))

// test the reference root when pruning
db, err = dbm.NewDB("test", "memdb", "")
require.NoError(t, err)
tree = NewMutableTree(db, 0, false, log.NewNopLogger())

_, err = tree.Set([]byte("key1"), []byte("value1"))
require.NoError(t, err)

_, _, err = tree.SaveVersion()
require.NoError(t, err)

_, _, err = tree.SaveVersion() // empty version
require.NoError(t, err)

require.NoError(t, tree.DeleteVersionsTo(1))
_, _, err = tree.SaveVersion() // empty version
require.NoError(t, err)

// load the tree from disk
tree = NewMutableTree(db, 0, false, log.NewNopLogger())
_, err = tree.Load()
require.NoError(t, err)

_, err = tree.Set([]byte("key2"), []byte("value2"))
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)

// load the tree from disk to check if the reference root is loaded correctly
tree = NewMutableTree(db, 0, false, log.NewNopLogger())
_, err = tree.Load()
require.NoError(t, err)
_, err = tree.Set([]byte("key1"), []byte("value2"))
require.NoError(t, err)
}
Loading