Skip to content

Commit

Permalink
import: set correct version on empty import (#230)
Browse files Browse the repository at this point in the history
* import: set correct version for empty import
  • Loading branch information
erikgrinaker authored Apr 5, 2020
1 parent 240b18a commit 99a603e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Special thanks to external contributors on this release:

### Bug Fixes

- [import] [\#230](https://github.com/tendermint/iavl/pull/230) Set correct version when committing an empty import.

## 0.13.2 (March 18, 2020)

### Improvements
Expand Down
8 changes: 5 additions & 3 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@ func (i *Importer) Commit() error {
return ErrNoImport
}

switch {
case len(i.stack) == 1:
switch len(i.stack) {
case 0:
i.batch.Set(i.tree.ndb.rootKey(i.version), []byte{})
case 1:
i.batch.Set(i.tree.ndb.rootKey(i.version), i.stack[0].hash)
case len(i.stack) > 2:
default:
return errors.Errorf("invalid node structure, found stack size %v when committing",
len(i.stack))
}
Expand Down
13 changes: 13 additions & 0 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package iavl
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

db "github.com/tendermint/tm-db"
Expand Down Expand Up @@ -199,6 +200,18 @@ func TestImporter_Commit_Closed(t *testing.T) {
require.Equal(t, ErrNoImport, err)
}

func TestImporter_Commit_Empty(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
importer, err := tree.Import(3)
require.NoError(t, err)
defer importer.Close()

err = importer.Commit()
require.NoError(t, err)
assert.EqualValues(t, 3, tree.Version())
}

func BenchmarkImport(b *testing.B) {
b.StopTimer()
tree := setupExportTreeSized(b, 4096)
Expand Down

0 comments on commit 99a603e

Please sign in to comment.