From e6984a63a9b8e15efd7606b5732a5a381377915f Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 20 Jul 2022 11:24:53 +0200 Subject: [PATCH 1/5] fix: MutableTree.Get --- mutable_tree.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mutable_tree.go b/mutable_tree.go index 654b1347f..d3eff68bb 100644 --- a/mutable_tree.go +++ b/mutable_tree.go @@ -150,6 +150,10 @@ func (tree *MutableTree) Get(key []byte) ([]byte, error) { if fastNode, ok := tree.unsavedFastNodeAdditions[string(key)]; ok { return fastNode.value, nil } + // check if node was deleted + if _, ok := tree.unsavedFastNodeRemovals[string(key)]; ok { + return nil, nil + } return tree.ImmutableTree.Get(key) } @@ -177,7 +181,6 @@ func (tree *MutableTree) Iterate(fn func(key []byte, value []byte) bool) (stoppe if err != nil { return false, err } - if !isFastCacheEnabled { return tree.ImmutableTree.Iterate(fn) } From 9070a02d29a0c2bbfa9ffb801e03c93a3bf39d95 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 20 Jul 2022 11:32:26 +0200 Subject: [PATCH 2/5] cleanup tests --- mutable_tree_test.go | 45 +++++++++++++++++--------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/mutable_tree_test.go b/mutable_tree_test.go index 4e1d6efd8..8abebc94d 100644 --- a/mutable_tree_test.go +++ b/mutable_tree_test.go @@ -18,10 +18,15 @@ import ( db "github.com/tendermint/tm-db" ) -func TestDelete(t *testing.T) { +func setupMutableTree(t *testing.T) *MutableTree { memDB := db.NewMemDB() tree, err := NewMutableTree(memDB, 0) require.NoError(t, err) + return tree +} + +func TestDelete(t *testing.T) { + tree := setupMutableTree(t) tree.set([]byte("k1"), []byte("Fred")) hash, version, err := tree.SaveVersion() @@ -35,7 +40,7 @@ func TestDelete(t *testing.T) { require.Nil(t, k1Value) key := tree.ndb.rootKey(version) - err = memDB.Set(key, hash) + err = tree.ndb.db.Set(key, hash) require.NoError(t, err) tree.versions[version] = true @@ -45,9 +50,7 @@ func TestDelete(t *testing.T) { } func TestTraverse(t *testing.T) { - memDB := db.NewMemDB() - tree, err := NewMutableTree(memDB, 0) - require.NoError(t, err) + tree := setupMutableTree(t) for i := 0; i < 6; i++ { tree.set([]byte(fmt.Sprintf("k%d", i)), []byte(fmt.Sprintf("v%d", i))) @@ -57,9 +60,7 @@ func TestTraverse(t *testing.T) { } func TestMutableTree_DeleteVersions(t *testing.T) { - memDB := db.NewMemDB() - tree, err := NewMutableTree(memDB, 0) - require.NoError(t, err) + tree := setupMutableTree(t) type entry struct { key []byte @@ -77,7 +78,7 @@ func TestMutableTree_DeleteVersions(t *testing.T) { v := randBytes(10) entries[j] = entry{k, v} - _, err = tree.Set(k, v) + _, err := tree.Set(k, v) require.NoError(t, err) } @@ -115,9 +116,7 @@ func TestMutableTree_DeleteVersions(t *testing.T) { } func TestMutableTree_LoadVersion_Empty(t *testing.T) { - memDB := db.NewMemDB() - tree, err := NewMutableTree(memDB, 0) - require.NoError(t, err) + tree := setupMutableTree(t) version, err := tree.LoadVersion(0) require.NoError(t, err) @@ -154,7 +153,6 @@ func TestMutableTree_DeleteVersionsRange(t *testing.T) { mdb := db.NewMemDB() tree, err := NewMutableTree(mdb, 0) require.NoError(err) - const maxLength = 100 const fromLength = 10 @@ -272,9 +270,7 @@ func TestMutableTree_InitialVersion(t *testing.T) { } func TestMutableTree_SetInitialVersion(t *testing.T) { - memDB := db.NewMemDB() - tree, err := NewMutableTree(memDB, 0) - require.NoError(t, err) + tree := setupMutableTree(t) tree.SetInitialVersion(9) tree.Set([]byte("a"), []byte{0x01}) @@ -423,9 +419,7 @@ func TestMutableTree_SetSimple(t *testing.T) { } func TestMutableTree_SetTwoKeys(t *testing.T) { - mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 0) - require.NoError(t, err) + tree := setupMutableTree(t) const testKey1 = "a" const testVal1 = "test" @@ -470,10 +464,7 @@ func TestMutableTree_SetTwoKeys(t *testing.T) { } func TestMutableTree_SetOverwrite(t *testing.T) { - mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 0) - require.NoError(t, err) - + tree := setupMutableTree(t) const testKey1 = "a" const testVal1 = "test" const testVal2 = "test2" @@ -503,10 +494,7 @@ func TestMutableTree_SetOverwrite(t *testing.T) { } func TestMutableTree_SetRemoveSet(t *testing.T) { - mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 0) - require.NoError(t, err) - + tree := setupMutableTree(t) const testKey1 = "a" const testVal1 = "test" @@ -709,6 +697,7 @@ func TestUpgradeStorageToFast_LatestVersion_Success(t *testing.T) { // Setup db := db.NewMemDB() tree, err := NewMutableTree(db, 1000) + require.NoError(t, err) // Default version when storage key does not exist in the db isFastCacheEnabled, err := tree.IsFastCacheEnabled() @@ -739,9 +728,9 @@ func TestUpgradeStorageToFast_AlreadyUpgraded_Success(t *testing.T) { // Setup db := db.NewMemDB() tree, err := NewMutableTree(db, 1000) + require.NoError(t, err) // Default version when storage key does not exist in the db - require.NoError(t, err) isFastCacheEnabled, err := tree.IsFastCacheEnabled() require.NoError(t, err) require.False(t, isFastCacheEnabled) From 96cdc8fd401f38a7cd32f1d8894856927c9baa02 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 20 Jul 2022 11:54:36 +0200 Subject: [PATCH 3/5] add test --- mutable_tree_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/mutable_tree_test.go b/mutable_tree_test.go index 8abebc94d..4f2b60478 100644 --- a/mutable_tree_test.go +++ b/mutable_tree_test.go @@ -18,6 +18,14 @@ import ( db "github.com/tendermint/tm-db" ) +var ( + tKey1 = []byte("k1") + tVal1 = []byte("v1") + + tKey2 = []byte("k2") + tVal2 = []byte("v2") +) + func setupMutableTree(t *testing.T) *MutableTree { memDB := db.NewMemDB() tree, err := NewMutableTree(memDB, 0) @@ -49,6 +57,47 @@ func TestDelete(t *testing.T) { require.Equal(t, 0, bytes.Compare([]byte("Fred"), k1Value)) } +func TestGetRemove(t *testing.T) { + require := require.New(t) + tree := setupMutableTree(t) + testGet := func(exists bool) { + v, err := tree.Get(tKey1) + require.NoError(err) + if exists { + require.Equal(tVal1, v, "key should exist") + } else { + require.Nil(v, "key should not exist") + } + } + + testGet(false) + + ok, err := tree.Set(tKey1, tVal1) + require.NoError(err) + require.False(ok, "new key set: nothing to update") + + // add second key to avoid tree.root removal + ok, err = tree.Set(tKey2, tVal2) + require.NoError(err) + require.False(ok, "new key set: nothing to update") + + testGet(true) + + // Save to tree.ImmutableTree + _, version, err := tree.SaveVersion() + require.NoError(err) + require.Equal(int64(1), version) + + testGet(true) + + v, ok, err := tree.Remove(tKey1) + require.NoError(err) + require.True(ok, "key should be removed") + require.Equal(tVal1, v, "key should exist") + + testGet(false) +} + func TestTraverse(t *testing.T) { tree := setupMutableTree(t) From 9c47f8a8922c8aeff027449d03d743ad95d2f40d Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 20 Jul 2022 13:07:50 +0200 Subject: [PATCH 4/5] update changelog --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21338251f..212b0b6c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,12 +2,16 @@ ## Unreleased +### Bug Fixes + +- [524](https://github.com/cosmos/iavl/pull/524) Fix: `MutableTree.Get`. + ## 0.19.0 (July 6, 2022) ### Breaking Changes - [\514](https://github.com/cosmos/iavl/pull/514) Downgrade Tendermint to 0.34.x -- [\500](https://github.com/cosmos/iavl/pull/500) Return errors instead of panicking. +- [\500](https://github.com/cosmos/iavl/pull/500) Return errors instead of panicking. ### Improvements From 111f0434dbea035cbf25429df9815e0774daba18 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 20 Jul 2022 13:08:45 +0200 Subject: [PATCH 5/5] fix changelog --- CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 212b0b6c8..c5847045f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,18 +4,18 @@ ### Bug Fixes -- [524](https://github.com/cosmos/iavl/pull/524) Fix: `MutableTree.Get`. +- [#524](https://github.com/cosmos/iavl/pull/524) Fix: `MutableTree.Get`. ## 0.19.0 (July 6, 2022) ### Breaking Changes -- [\514](https://github.com/cosmos/iavl/pull/514) Downgrade Tendermint to 0.34.x -- [\500](https://github.com/cosmos/iavl/pull/500) Return errors instead of panicking. +- [#514](https://github.com/cosmos/iavl/pull/514) Downgrade Tendermint to 0.34.x +- [#500](https://github.com/cosmos/iavl/pull/500) Return errors instead of panicking. ### Improvements -- [\514](https://github.com/cosmos/iavl/pull/514) Use Go v1.18 +- [#514](https://github.com/cosmos/iavl/pull/514) Use Go v1.18 ## 0.18.0 (March 10, 2022)