diff --git a/CHANGELOG.md b/CHANGELOG.md index 520a7294e..fec6b9a87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/import.go b/import.go index a119693da..171fa60db 100644 --- a/import.go +++ b/import.go @@ -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)) } diff --git a/import_test.go b/import_test.go index 43d7b2895..26035c858 100644 --- a/import_test.go +++ b/import_test.go @@ -3,6 +3,7 @@ package iavl import ( "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" db "github.com/tendermint/tm-db" @@ -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)