From 038ededfbfa0198d95182a703cba1763b7eb6061 Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Sat, 4 Apr 2020 21:26:31 +0200 Subject: [PATCH 1/5] import: set correct version for empty import --- CHANGELOG.md | 2 ++ import.go | 2 ++ import_test.go | 13 +++++++++++++ 3 files changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 520a7294e..d4ec6a99e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ Special thanks to external contributors on this release: ### Bug Fixes +- [import] 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..8da9e2f86 100644 --- a/import.go +++ b/import.go @@ -158,6 +158,8 @@ func (i *Importer) Commit() error { } switch { + case len(i.stack) == 0: + i.batch.Set(i.tree.ndb.rootKey(i.version), []byte{}) case len(i.stack) == 1: i.batch.Set(i.tree.ndb.rootKey(i.version), i.stack[0].hash) case len(i.stack) > 2: 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) From f3c4d1e9911a65441fdfd5dd5e3006bfaa3ef82e Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Sat, 4 Apr 2020 21:32:40 +0200 Subject: [PATCH 2/5] import: fix commit sanity check --- import.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/import.go b/import.go index 8da9e2f86..56df48b64 100644 --- a/import.go +++ b/import.go @@ -162,7 +162,7 @@ func (i *Importer) Commit() error { i.batch.Set(i.tree.ndb.rootKey(i.version), []byte{}) case len(i.stack) == 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)) } From 2dfb99e10c4bfce6eb2771d236cc05ed005554e2 Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Sat, 4 Apr 2020 21:33:24 +0200 Subject: [PATCH 3/5] style tweak --- import.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/import.go b/import.go index 56df48b64..171fa60db 100644 --- a/import.go +++ b/import.go @@ -157,10 +157,10 @@ func (i *Importer) Commit() error { return ErrNoImport } - switch { - case len(i.stack) == 0: + switch len(i.stack) { + case 0: i.batch.Set(i.tree.ndb.rootKey(i.version), []byte{}) - case len(i.stack) == 1: + case 1: i.batch.Set(i.tree.ndb.rootKey(i.version), i.stack[0].hash) default: return errors.Errorf("invalid node structure, found stack size %v when committing", From 433ea00b95620a871b4103a870e5e7eb364b2046 Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Sat, 4 Apr 2020 21:39:05 +0200 Subject: [PATCH 4/5] changelog: update with PR link --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4ec6a99e..f6485633c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ Special thanks to external contributors on this release: ### Bug Fixes -- [import] Set correct version when committing an empty import. +- [import] [\#230](https://github.com/tendermint/iavl/pull/230/files) Set correct version when committing an empty import. ## 0.13.2 (March 18, 2020) From a30e96b9f5728f2806ba3cf9f09e750e80563ad1 Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Sun, 5 Apr 2020 13:43:12 +0200 Subject: [PATCH 5/5] changelog link fix --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6485633c..fec6b9a87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ Special thanks to external contributors on this release: ### Bug Fixes -- [import] [\#230](https://github.com/tendermint/iavl/pull/230/files) Set correct version when committing an empty import. +- [import] [\#230](https://github.com/tendermint/iavl/pull/230) Set correct version when committing an empty import. ## 0.13.2 (March 18, 2020)