Skip to content

Commit

Permalink
fix: legacy node key (#928)
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-develope authored Apr 8, 2024
1 parent ff86011 commit 0d64869
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

### Bug Fixes

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

## v1.1.1 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 @@ -1010,10 +1010,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)
}

0 comments on commit 0d64869

Please sign in to comment.