Skip to content

Commit

Permalink
fix: Add Extra Check for Reformatted Root Node in GetNode (backport #…
Browse files Browse the repository at this point in the history
…1007) (#1009)

Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
Co-authored-by: Marko Baricevic <marbar3778@yahoo.com>
  • Loading branch information
3 people authored Nov 26, 2024
1 parent 594b181 commit 99821d4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog


### Bug Fixes

- [#1007](https://github.com/cosmos/iavl/pull/1007) Add the extra check for the reformatted root node in `GetNode`

### Improvements

- [#961](https://github.com/cosmos/iavl/pull/961) Add new `GetLatestVersion` API to get the latest version.
Expand Down
24 changes: 24 additions & 0 deletions mutable_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1453,3 +1453,27 @@ func TestMutableTreeClose(t *testing.T) {

require.NoError(t, tree.Close())
}

func TestReferenceRootPruning(t *testing.T) {
memDB := dbm.NewMemDB()
tree := NewMutableTree(memDB, 0, true, NewNopLogger())

_, err := tree.Set([]byte("foo"), []byte("bar"))
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)

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

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

err = tree.DeleteVersionsTo(1)
require.NoError(t, err)

_, err = tree.Set([]byte("foo"), []byte("bar*"))
require.NoError(t, err)
}
14 changes: 14 additions & 0 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,20 @@ func (ndb *nodeDB) GetNode(nk []byte) (*Node, error) {
if err != nil {
return nil, fmt.Errorf("can't get node %v: %v", nk, err)
}
if buf == nil && !isLegcyNode {
// if the node is reformatted by pruning, check against (version, 0)
nKey := GetNodeKey(nk)
if nKey.nonce == 1 {
nodeKey = ndb.nodeKey((&NodeKey{
version: nKey.version,
nonce: 0,
}).GetKey())
buf, err = ndb.db.Get(nodeKey)
if err != nil {
return nil, fmt.Errorf("can't get the reformatted node %v: %v", nk, err)
}
}
}
if buf == nil {
return nil, fmt.Errorf("Value missing for key %v corresponding to nodeKey %x", nk, nodeKey)
}
Expand Down

0 comments on commit 99821d4

Please sign in to comment.