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: rootKey empty check by len equals 0 (backport #801) #810

Closed
wants to merge 1 commit into from
Closed
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
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,31 @@

- [#654](https://github.com/cosmos/iavl/pull/654) Add API `TraverseStateChanges` to extract state changes from iavl versions.
- [#726](https://github.com/cosmos/iavl/pull/726) Make `KVPair` and `ChangeSet` serializable with protobuf.
<<<<<<< HEAD
- [#795](https://github.com/cosmos/iavl/pull/795) Use gogofaster buf plugin.
=======
- [#718](https://github.com/cosmos/iavl/pull/718) Fix `traverseNodes` unexpected behaviour
- [#770](https://github.com/cosmos/iavl/pull/770) Add `WorkingVersion()int64` API.

### Bug Fixes

- [#773](https://github.com/cosmos/iavl/pull/773) Fix memory leak in `Import`.
- [#795](https://github.com/cosmos/iavl/pull/795) Fix plugin used for buf generate.
- [#801](https://github.com/cosmos/iavl/pull/801) Fix rootKey empty check by len equals 0.

### Breaking Changes

- [#735](https://github.com/cosmos/iavl/pull/735) Pass logger to `NodeDB`, `MutableTree` and `ImmutableTree`

- [#646](https://github.com/cosmos/iavl/pull/646) Remove the `orphans` from the storage

- [#777](https://github.com/cosmos/iavl/pull/777) Don't return errors from ImmutableTree.Hash, NewImmutableTree, NewImmutableTreeWIthOpts

### API Changes

- [#646](https://github.com/cosmos/iavl/pull/646) Remove the `DeleteVersion`, `DeleteVersions`, `DeleteVersionsRange` and introduce a new endpoint of `DeleteVersionsTo` instead
- [#695](https://github.com/cosmos/iavl/pull/695) Add API `SaveChangeSet` to save the changeset as a new version.
>>>>>>> 06f5be1 (fix: rootKey empty check by len equals 0 (#801))

## 0.20.0 (March 14, 2023)

Expand Down
2 changes: 1 addition & 1 deletion iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ type NodeIterator struct {

// NewNodeIterator returns a new NodeIterator to traverse the tree of the root node.
func NewNodeIterator(rootKey []byte, ndb *nodeDB) (*NodeIterator, error) {
if rootKey == nil {
if len(rootKey) == 0 {
return &NodeIterator{
nodesToVisit: []*Node{},
ndb: ndb,
Expand Down
49 changes: 49 additions & 0 deletions iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,3 +330,52 @@
itr := NewUnsavedFastIterator(config.startIterate, config.endIterate, config.ascending, tree.ndb, tree.unsavedFastNodeAdditions, tree.unsavedFastNodeRemovals)
return itr, mirror
}
<<<<<<< HEAD

Check failure on line 333 in iterator_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected declaration, found '<<' (typecheck)

Check failure on line 333 in iterator_test.go

View workflow job for this annotation

GitHub Actions / Test

expected declaration, found '<<'
=======

func TestNodeIterator_Success(t *testing.T) {
tree, mirror := getRandomizedTreeAndMirror(t)

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

randomizeTreeAndMirror(t, tree, mirror)

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

// check if the iterating count is same with the entire node count of the tree
itr, err := NewNodeIterator(tree.root.GetKey(), tree.ndb)
require.NoError(t, err)
nodeCount := 0
for ; itr.Valid(); itr.Next(false) {
nodeCount++
}
require.Equal(t, int64(nodeCount), tree.Size()*2-1)

// check if the skipped node count is right
itr, err = NewNodeIterator(tree.root.GetKey(), tree.ndb)
require.NoError(t, err)
updateCount := 0
skipCount := 0
for itr.Valid() {
node := itr.GetNode()
updateCount++
if node.nodeKey.version < tree.Version() {
skipCount += int(node.size*2 - 2) // the size of the subtree without the root
}
itr.Next(node.nodeKey.version < tree.Version())
}
require.Equal(t, nodeCount, updateCount+skipCount)
}

func TestNodeIterator_WithEmptyRoot(t *testing.T) {
itr, err := NewNodeIterator(nil, newNodeDB(dbm.NewMemDB(), 0, nil, log.NewNopLogger()))
require.NoError(t, err)
require.False(t, itr.Valid())

itr, err = NewNodeIterator([]byte{}, newNodeDB(dbm.NewMemDB(), 0, nil, log.NewNopLogger()))
require.NoError(t, err)
require.False(t, itr.Valid())
}
>>>>>>> 06f5be1 (fix: rootKey empty check by len equals 0 (#801))

Check failure on line 381 in iterator_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

illegal character U+0023 '#' (typecheck)
Loading