From 5c1427667ecf7c956b807f58bc1b862f18fcf9ac Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 24 May 2023 14:59:47 -0600 Subject: [PATCH] refactor: don't return an error from NewImmutableTree[WithOpts] Tree construction never fails, so don't return an error. Fixes #756 --- basic_test.go | 27 ++--- benchmarks/bench_test.go | 5 +- benchmarks/cosmos-exim/main.go | 15 +-- cmd/iaviewer/main.go | 5 +- diff_test.go | 5 +- export_test.go | 20 ++-- import_test.go | 59 ++++------- iterator_test.go | 24 ++--- migrate_test.go | 9 +- mutable_tree.go | 6 +- mutable_tree_test.go | 82 ++++++--------- nodedb_test.go | 9 +- proof_iavl_test.go | 5 +- proof_ics23_test.go | 5 +- proof_test.go | 8 +- testutils_test.go | 10 +- tree_dotgraph_test.go | 5 +- tree_fuzz_test.go | 5 +- tree_random_test.go | 3 +- tree_test.go | 176 ++++++++++++--------------------- 20 files changed, 171 insertions(+), 312 deletions(-) diff --git a/basic_test.go b/basic_test.go index b2ab183ea..b344a3212 100644 --- a/basic_test.go +++ b/basic_test.go @@ -15,8 +15,7 @@ import ( ) func TestBasic(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) up, err := tree.Set([]byte("1"), []byte("one")) require.NoError(t, err) if up { @@ -241,8 +240,7 @@ func TestRemove(t *testing.T) { keyLen, dataLen := 16, 40 size := 10000 - t1, err := getTestTree(size) - require.NoError(t, err) + t1 := getTestTree(size) // insert a bunch of random nodes keys := make([][]byte, size) @@ -271,8 +269,7 @@ func TestIntegration(t *testing.T) { } records := make([]*record, 400) - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) randomRecord := func() *record { return &record{randstr(20), randstr(20)} @@ -324,7 +321,6 @@ func TestIntegration(t *testing.T) { } else if string(val) != x.value { t.Error("Wrong value") } - require.NoError(t, err) for _, r := range records[i+1:] { has, err := tree.Has([]byte(r.key)) require.NoError(t, err) @@ -374,8 +370,7 @@ func TestIterateRange(t *testing.T) { } sort.Strings(keys) - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) // insert all the data for _, r := range records { @@ -446,16 +441,14 @@ func TestPersistence(t *testing.T) { } // Construct some tree and save it - t1, err := NewMutableTree(db, 0, false, log.NewNopLogger()) - require.NoError(t, err) + t1 := NewMutableTree(db, 0, false, log.NewNopLogger()) for key, value := range records { t1.Set([]byte(key), []byte(value)) } t1.SaveVersion() // Load a tree - t2, err := NewMutableTree(db, 0, false, log.NewNopLogger()) - require.NoError(t, err) + t2 := NewMutableTree(db, 0, false, log.NewNopLogger()) t2.Load() for key, value := range records { t2value, err := t2.Get([]byte(key)) @@ -468,8 +461,7 @@ func TestPersistence(t *testing.T) { func TestProof(t *testing.T) { // Construct some random tree - tree, err := getTestTree(100) - require.NoError(t, err) + tree := getTestTree(100) for i := 0; i < 10; i++ { key, value := randstr(20), randstr(20) tree.Set([]byte(key), []byte(value)) @@ -504,13 +496,12 @@ func TestProof(t *testing.T) { func TestTreeProof(t *testing.T) { db := db.NewMemDB() - tree, err := NewMutableTree(db, 100, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db, 100, false, log.NewNopLogger()) hash := tree.Hash() assert.Equal(t, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(hash)) // should get false for proof with nil root - _, err = tree.GetProof([]byte("foo")) + _, err := tree.GetProof([]byte("foo")) require.Error(t, err) // insert lots of info and store the bytes diff --git a/benchmarks/bench_test.go b/benchmarks/bench_test.go index cca09fdc6..df0557ece 100644 --- a/benchmarks/bench_test.go +++ b/benchmarks/bench_test.go @@ -27,13 +27,12 @@ func randBytes(length int) []byte { } func prepareTree(b *testing.B, db db.DB, size, keyLen, dataLen int) (*iavl.MutableTree, [][]byte) { - t, err := iavl.NewMutableTreeWithOpts(db, size, nil, false, log.NewNopLogger()) - require.NoError(b, err) + t := iavl.NewMutableTreeWithOpts(db, size, nil, false, log.NewNopLogger()) keys := make([][]byte, size) for i := 0; i < size; i++ { key := randBytes(keyLen) - _, err = t.Set(key, randBytes(dataLen)) + _, err := t.Set(key, randBytes(dataLen)) require.NoError(b, err) keys[i] = key } diff --git a/benchmarks/cosmos-exim/main.go b/benchmarks/cosmos-exim/main.go index 956956da6..98d629923 100644 --- a/benchmarks/cosmos-exim/main.go +++ b/benchmarks/cosmos-exim/main.go @@ -91,10 +91,7 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) { if err != nil { return 0, nil, err } - tree, err := iavl.NewMutableTree(tmdb.NewPrefixDB(ldb, []byte("s/k:main/")), 0, false, log.NewNopLogger()) - if err != nil { - return 0, nil, err - } + tree := iavl.NewMutableTree(tmdb.NewPrefixDB(ldb, []byte("s/k:main/")), 0, false, log.NewNopLogger()) version, err := tree.LoadVersion(0) if err != nil { return 0, nil, err @@ -106,10 +103,7 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) { totalStats := Stats{} for _, name := range stores { db := tmdb.NewPrefixDB(ldb, []byte("s/k:"+name+"/")) - tree, err := iavl.NewMutableTree(db, 0, false, log.NewNopLogger()) - if err != nil { - return 0, nil, err - } + tree := iavl.NewMutableTree(db, 0, false, log.NewNopLogger()) stats := Stats{} export := make([]*iavl.ExportNode, 0, 100000) @@ -174,10 +168,7 @@ func runImport(version int64, exports map[string][]*iavl.ExportNode) error { if err != nil { return err } - newTree, err := iavl.NewMutableTree(newDB, 0, false, log.NewNopLogger()) - if err != nil { - return err - } + newTree := iavl.NewMutableTree(newDB, 0, false, log.NewNopLogger()) importer, err := newTree.Import(version) if err != nil { return err diff --git a/cmd/iaviewer/main.go b/cmd/iaviewer/main.go index c808ce276..6a8ba2982 100644 --- a/cmd/iaviewer/main.go +++ b/cmd/iaviewer/main.go @@ -122,10 +122,7 @@ func ReadTree(dir string, version int, prefix []byte) (*iavl.MutableTree, error) db = dbm.NewPrefixDB(db, prefix) } - tree, err := iavl.NewMutableTree(db, DefaultCacheSize, false, log.NewLogger(os.Stdout)) - if err != nil { - return nil, err - } + tree := iavl.NewMutableTree(db, DefaultCacheSize, false, log.NewLogger(os.Stdout)) ver, err := tree.LoadVersion(int64(version)) fmt.Printf("Got version: %d\n", ver) return tree, err diff --git a/diff_test.go b/diff_test.go index 1b8ae2634..92f8f39e5 100644 --- a/diff_test.go +++ b/diff_test.go @@ -20,8 +20,7 @@ func TestDiffRoundTrip(t *testing.T) { // apply changeSets to tree db := db.NewMemDB() - tree, err := NewMutableTree(db, 0, true, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db, 0, true, log.NewNopLogger()) for i := range changeSets { v, err := tree.SaveChangeSet(changeSets[i]) require.NoError(t, err) @@ -31,7 +30,7 @@ func TestDiffRoundTrip(t *testing.T) { // extract change sets from db var extractChangeSets []*ChangeSet tree2 := NewImmutableTree(db, 0, true, log.NewNopLogger()) - err = tree2.TraverseStateChanges(0, math.MaxInt64, func(version int64, changeSet *ChangeSet) error { + err := tree2.TraverseStateChanges(0, math.MaxInt64, func(version int64, changeSet *ChangeSet) error { extractChangeSets = append(extractChangeSets, changeSet) return nil }) diff --git a/export_test.go b/export_test.go index 9cef686f0..12deb780a 100644 --- a/export_test.go +++ b/export_test.go @@ -15,10 +15,9 @@ import ( // setupExportTreeBasic sets up a basic tree with a handful of // create/update/delete operations over a few versions. func setupExportTreeBasic(t require.TestingT) *ImmutableTree { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - _, err = tree.Set([]byte("x"), []byte{255}) + _, err := tree.Set([]byte("x"), []byte{255}) require.NoError(t, err) _, err = tree.Set([]byte("z"), []byte{255}) require.NoError(t, err) @@ -75,8 +74,7 @@ func setupExportTreeRandom(t *testing.T) *ImmutableTree { ) r := rand.New(rand.NewSource(randSeed)) - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) var version int64 keys := make([][]byte, 0, versionOps) @@ -113,6 +111,7 @@ func setupExportTreeRandom(t *testing.T) *ImmutableTree { keys = append(keys, key) } } + var err error _, version, err = tree.SaveVersion() require.NoError(t, err) } @@ -135,8 +134,7 @@ func setupExportTreeSized(t require.TestingT, treeSize int) *ImmutableTree { //n ) r := rand.New(rand.NewSource(randSeed)) - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) for i := 0; i < treeSize; i++ { key := make([]byte, keySize) @@ -257,8 +255,7 @@ func TestExporter_Import(t *testing.T) { exporter = NewCompressExporter(innerExporter) } - newTree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + newTree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) innerImporter, err := newTree.Import(tree.Version()) require.NoError(t, err) defer innerImporter.Close() @@ -326,10 +323,9 @@ func TestExporter_Close(t *testing.T) { } func TestExporter_DeleteVersionErrors(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - _, err = tree.Set([]byte("a"), []byte{1}) + _, err := tree.Set([]byte("a"), []byte{1}) require.NoError(t, err) _, _, err = tree.SaveVersion() require.NoError(t, err) diff --git a/import_test.go b/import_test.go index 7c1fb1102..a3dc4071f 100644 --- a/import_test.go +++ b/import_test.go @@ -11,12 +11,9 @@ import ( ) func ExampleImporter() { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - if err != nil { - panic(err) - } + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - _, err = tree.Set([]byte("a"), []byte{1}) + _, err := tree.Set([]byte("a"), []byte{1}) if err != nil { panic(err) } @@ -55,10 +52,7 @@ func ExampleImporter() { exported = append(exported, node) } - newTree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - if err != nil { - panic(err) - } + newTree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) importer, err := newTree.Import(version) if err != nil { panic(err) @@ -77,16 +71,14 @@ func ExampleImporter() { } func TestImporter_NegativeVersion(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) - _, err = tree.Import(-1) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) + _, err := tree.Import(-1) require.Error(t, err) } func TestImporter_NotEmpty(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) - _, err = tree.Set([]byte("a"), []byte{1}) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) + _, err := tree.Set([]byte("a"), []byte{1}) require.NoError(t, err) _, _, err = tree.SaveVersion() require.NoError(t, err) @@ -98,15 +90,13 @@ func TestImporter_NotEmpty(t *testing.T) { func TestImporter_NotEmptyDatabase(t *testing.T) { db := db.NewMemDB() - tree, err := NewMutableTree(db, 0, false, log.NewNopLogger()) - require.NoError(t, err) - _, err = tree.Set([]byte("a"), []byte{1}) + tree := NewMutableTree(db, 0, false, log.NewNopLogger()) + _, err := tree.Set([]byte("a"), []byte{1}) require.NoError(t, err) _, _, err = tree.SaveVersion() require.NoError(t, err) - tree, err = NewMutableTree(db, 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree = NewMutableTree(db, 0, false, log.NewNopLogger()) _, err = tree.Load() require.NoError(t, err) @@ -115,9 +105,8 @@ func TestImporter_NotEmptyDatabase(t *testing.T) { } func TestImporter_NotEmptyUnsaved(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) - _, err = tree.Set([]byte("a"), []byte{1}) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) + _, err := tree.Set([]byte("a"), []byte{1}) require.NoError(t, err) _, err = tree.Import(1) @@ -143,8 +132,7 @@ func TestImporter_Add(t *testing.T) { for desc, tc := range testcases { tc := tc // appease scopelint t.Run(desc, func(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) importer, err := tree.Import(1) require.NoError(t, err) defer importer.Close() @@ -163,8 +151,7 @@ func TestImporter_Add(t *testing.T) { } func TestImporter_Add_Closed(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) importer, err := tree.Import(1) require.NoError(t, err) @@ -175,8 +162,7 @@ func TestImporter_Add_Closed(t *testing.T) { } func TestImporter_Close(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) importer, err := tree.Import(1) require.NoError(t, err) @@ -192,8 +178,7 @@ func TestImporter_Close(t *testing.T) { } func TestImporter_Commit(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) importer, err := tree.Import(1) require.NoError(t, err) @@ -208,8 +193,7 @@ func TestImporter_Commit(t *testing.T) { } func TestImporter_Commit_ForwardVersion(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) importer, err := tree.Import(2) require.NoError(t, err) @@ -224,8 +208,7 @@ func TestImporter_Commit_ForwardVersion(t *testing.T) { } func TestImporter_Commit_Closed(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) importer, err := tree.Import(1) require.NoError(t, err) @@ -239,8 +222,7 @@ func TestImporter_Commit_Closed(t *testing.T) { } func TestImporter_Commit_Empty(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) importer, err := tree.Import(3) require.NoError(t, err) defer importer.Close() @@ -269,8 +251,7 @@ func BenchmarkImport(b *testing.B) { b.StartTimer() for n := 0; n < b.N; n++ { - newTree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(b, err) + newTree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) importer, err := newTree.Import(tree.Version()) require.NoError(b, err) for _, item := range exported { diff --git a/iterator_test.go b/iterator_test.go index 7491cbc21..8d27a7232 100644 --- a/iterator_test.go +++ b/iterator_test.go @@ -57,16 +57,14 @@ func TestUnsavedFastIterator_NewIterator_NilAdditions_Failure(t *testing.T) { } t.Run("Nil additions given", func(t *testing.T) { - tree, err := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) itr := NewUnsavedFastIterator(start, end, ascending, tree.ndb, nil, tree.unsavedFastNodeRemovals) performTest(t, itr) require.ErrorIs(t, errUnsavedFastIteratorNilAdditionsGiven, itr.Error()) }) t.Run("Nil removals given", func(t *testing.T) { - tree, err := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) itr := NewUnsavedFastIterator(start, end, ascending, tree.ndb, tree.unsavedFastNodeAdditions, nil) performTest(t, itr) require.ErrorIs(t, errUnsavedFastIteratorNilRemovalsGiven, itr.Error()) @@ -79,8 +77,7 @@ func TestUnsavedFastIterator_NewIterator_NilAdditions_Failure(t *testing.T) { }) t.Run("Additions and removals are nil", func(t *testing.T) { - tree, err := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) itr := NewUnsavedFastIterator(start, end, ascending, tree.ndb, nil, nil) performTest(t, itr) require.ErrorIs(t, errUnsavedFastIteratorNilAdditionsGiven, itr.Error()) @@ -249,11 +246,10 @@ func iteratorSuccessTest(t *testing.T, config *iteratorTestConfig) { } func setupIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (dbm.Iterator, [][]string) { - tree, err := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) mirror := setupMirrorForIterator(t, config, tree) - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(t, err) latestVersion, err := tree.ndb.getLatestVersion() @@ -266,11 +262,10 @@ func setupIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (dbm.Itera } func setupFastIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (dbm.Iterator, [][]string) { - tree, err := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) mirror := setupMirrorForIterator(t, config, tree) - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(t, err) itr := NewFastIterator(config.startIterate, config.endIterate, config.ascending, tree.ndb) @@ -278,8 +273,7 @@ func setupFastIteratorAndMirror(t *testing.T, config *iteratorTestConfig) (dbm.I } func setupUnsavedFastIterator(t *testing.T, config *iteratorTestConfig) (dbm.Iterator, [][]string) { - tree, err := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(dbm.NewMemDB(), 0, false, log.NewNopLogger()) // For unsaved fast iterator, we would like to test the state where // there are saved fast nodes as well as some unsaved additions and removals. @@ -294,7 +288,7 @@ func setupUnsavedFastIterator(t *testing.T, config *iteratorTestConfig) (dbm.Ite // First half of the mirror mirror := setupMirrorForIterator(t, &firstHalfConfig, tree) - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(t, err) // No unsaved additions or removals should be present after saving diff --git a/migrate_test.go b/migrate_test.go index 24906051e..59c75246a 100644 --- a/migrate_test.go +++ b/migrate_test.go @@ -57,8 +57,7 @@ func TestLazySet(t *testing.T) { db, err := dbm.NewDB("test", dbm.GoLevelDBBackend, relateDir) require.NoError(t, err) - tree, err := NewMutableTree(db, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db, 1000, false, log.NewNopLogger()) // Load the latest legacy version _, err = tree.LoadVersion(int64(legacyVersion)) @@ -76,8 +75,7 @@ func TestLazySet(t *testing.T) { require.NoError(t, err) } - tree, err = NewMutableTree(db, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + tree = NewMutableTree(db, 1000, false, log.NewNopLogger()) // Verify that the latest legacy version can still be loaded _, err = tree.LoadVersion(int64(legacyVersion)) @@ -101,8 +99,7 @@ func TestDeleteVersions(t *testing.T) { db, err := dbm.NewDB("test", dbm.GoLevelDBBackend, relateDir) require.NoError(t, err) - tree, err := NewMutableTree(db, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db, 1000, false, log.NewNopLogger()) // Load the latest legacy version _, err = tree.LoadVersion(int64(legacyVersion)) diff --git a/mutable_tree.go b/mutable_tree.go index eb6afbf48..306081771 100644 --- a/mutable_tree.go +++ b/mutable_tree.go @@ -45,12 +45,12 @@ type MutableTree struct { } // NewMutableTree returns a new tree with the specified cache size and datastore. -func NewMutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool, lg log.Logger) (*MutableTree, error) { +func NewMutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool, lg log.Logger) *MutableTree { return NewMutableTreeWithOpts(db, cacheSize, nil, skipFastStorageUpgrade, lg) } // NewMutableTreeWithOpts returns a new tree with the specified options. -func NewMutableTreeWithOpts(db dbm.DB, cacheSize int, opts *Options, skipFastStorageUpgrade bool, lg log.Logger) (*MutableTree, error) { +func NewMutableTreeWithOpts(db dbm.DB, cacheSize int, opts *Options, skipFastStorageUpgrade bool, lg log.Logger) *MutableTree { ndb := newNodeDB(db, cacheSize, opts, lg) head := &ImmutableTree{ndb: ndb, skipFastStorageUpgrade: skipFastStorageUpgrade} @@ -62,7 +62,7 @@ func NewMutableTreeWithOpts(db dbm.DB, cacheSize int, opts *Options, skipFastSto unsavedFastNodeRemovals: make(map[string]interface{}), ndb: ndb, skipFastStorageUpgrade: skipFastStorageUpgrade, - }, nil + } } // IsEmpty returns whether or not the tree has any keys. Only trees that are diff --git a/mutable_tree_test.go b/mutable_tree_test.go index 77fb28639..81a9658f9 100644 --- a/mutable_tree_test.go +++ b/mutable_tree_test.go @@ -35,8 +35,7 @@ var ( func setupMutableTree(t *testing.T, skipFastStorageUpgrade bool) *MutableTree { memDB := db.NewMemDB() - tree, err := NewMutableTree(memDB, 0, skipFastStorageUpgrade, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(memDB, 0, skipFastStorageUpgrade, log.NewNopLogger()) return tree } @@ -258,10 +257,9 @@ func TestMutableTree_LoadVersion_Empty(t *testing.T) { func TestMutableTree_InitialVersion(t *testing.T) { memDB := db.NewMemDB() - tree, err := NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 9}, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 9}, false, log.NewNopLogger()) - _, err = tree.Set([]byte("a"), []byte{0x01}) + _, err := tree.Set([]byte("a"), []byte{0x01}) require.NoError(t, err) _, version, err := tree.SaveVersion() require.NoError(t, err) @@ -274,21 +272,18 @@ func TestMutableTree_InitialVersion(t *testing.T) { assert.EqualValues(t, 10, version) // Reloading the tree with the same initial version is fine - tree, err = NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 9}, false, log.NewNopLogger()) - require.NoError(t, err) + tree = NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 9}, false, log.NewNopLogger()) version, err = tree.Load() require.NoError(t, err) assert.EqualValues(t, 10, version) // Reloading the tree with an initial version beyond the lowest should error - tree, err = NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 10}, false, log.NewNopLogger()) - require.NoError(t, err) + tree = NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 10}, false, log.NewNopLogger()) _, err = tree.Load() require.Error(t, err) // Reloading the tree with a lower initial version is fine, and new versions can be produced - tree, err = NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 3}, false, log.NewNopLogger()) - require.NoError(t, err) + tree = NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 3}, false, log.NewNopLogger()) version, err = tree.Load() require.NoError(t, err) assert.EqualValues(t, 10, version) @@ -314,8 +309,7 @@ func TestMutableTree_SetInitialVersion(t *testing.T) { func BenchmarkMutableTree_Set(b *testing.B) { db, err := db.NewDB("test", db.MemDBBackend, "") require.NoError(b, err) - t, err := NewMutableTree(db, 100000, false, log.NewNopLogger()) - require.NoError(b, err) + t := NewMutableTree(db, 100000, false, log.NewNopLogger()) for i := 0; i < 1000000; i++ { _, err = t.Set(iavlrand.RandBytes(10), []byte{}) require.NoError(b, err) @@ -333,10 +327,9 @@ func BenchmarkMutableTree_Set(b *testing.B) { func prepareTree(t *testing.T) *MutableTree { mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) for i := 0; i < 100; i++ { - _, err = tree.Set([]byte{byte(i)}, []byte("a")) + _, err := tree.Set([]byte{byte(i)}, []byte("a")) require.NoError(t, err) } _, ver, err := tree.SaveVersion() @@ -350,8 +343,7 @@ func prepareTree(t *testing.T) *MutableTree { require.True(t, ver == 2) require.NoError(t, err) - newTree, err := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + newTree := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) return newTree } @@ -406,19 +398,16 @@ func TestMutableTree_DeleteVersion(t *testing.T) { func TestMutableTree_LazyLoadVersionWithEmptyTree(t *testing.T) { mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) _, v1, err := tree.SaveVersion() require.NoError(t, err) - newTree1, err := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + newTree1 := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) v2, err := newTree1.LoadVersion(1) require.NoError(t, err) require.True(t, v1 == v2) - newTree2, err := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + newTree2 := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) v2, err = newTree1.LoadVersion(1) require.NoError(t, err) require.True(t, v1 == v2) @@ -428,8 +417,7 @@ func TestMutableTree_LazyLoadVersionWithEmptyTree(t *testing.T) { func TestMutableTree_SetSimple(t *testing.T) { mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(mdb, 0, false, log.NewNopLogger()) const testKey1 = "a" const testVal1 = "test" @@ -600,8 +588,7 @@ func TestMutableTree_SetRemoveSet(t *testing.T) { func TestMutableTree_FastNodeIntegration(t *testing.T) { mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(mdb, 1000, false, log.NewNopLogger()) const key1 = "a" const key2 = "b" @@ -665,8 +652,7 @@ func TestMutableTree_FastNodeIntegration(t *testing.T) { require.Equal(t, len(unsavedNodeRemovals), 0) // Load - t2, err := NewMutableTree(mdb, 0, false, log.NewNopLogger()) - require.NoError(t, err) + t2 := NewMutableTree(mdb, 0, false, log.NewNopLogger()) _, err = t2.Load() require.NoError(t, err) @@ -722,8 +708,7 @@ func TestIterate_MutableTree_Unsaved_NextVersion(t *testing.T) { } func TestIterator_MutableTree_Invalid(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) itr, err := tree.Iterator([]byte("a"), []byte("b"), true) require.NoError(t, err) @@ -734,8 +719,7 @@ func TestIterator_MutableTree_Invalid(t *testing.T) { func TestUpgradeStorageToFast_LatestVersion_Success(t *testing.T) { // Setup db := db.NewMemDB() - tree, err := NewMutableTree(db, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db, 1000, false, log.NewNopLogger()) // Default version when storage key does not exist in the db isFastCacheEnabled, err := tree.IsFastCacheEnabled() @@ -765,8 +749,7 @@ func TestUpgradeStorageToFast_LatestVersion_Success(t *testing.T) { func TestUpgradeStorageToFast_AlreadyUpgraded_Success(t *testing.T) { // Setup db := db.NewMemDB() - tree, err := NewMutableTree(db, 1000, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db, 1000, false, log.NewNopLogger()) // Default version when storage key does not exist in the db isFastCacheEnabled, err := tree.IsFastCacheEnabled() @@ -816,8 +799,7 @@ func TestUpgradeStorageToFast_DbErrorConstructor_Failure(t *testing.T) { dbMock.EXPECT().NewBatch().Return(nil).Times(1) dbMock.EXPECT().ReverseIterator(gomock.Any(), gomock.Any()).Return(rIterMock, nil).Times(1) - tree, err := NewMutableTree(dbMock, 0, false, log.NewNopLogger()) - require.Nil(t, err) + tree := NewMutableTree(dbMock, 0, false, log.NewNopLogger()) require.NotNil(t, tree) isFastCacheEnabled, err := tree.IsFastCacheEnabled() @@ -851,8 +833,7 @@ func TestUpgradeStorageToFast_DbErrorEnableFastStorage_Failure(t *testing.T) { batchMock.EXPECT().Set(gomock.Any(), gomock.Any()).Return(expectedError).Times(1) - tree, err := NewMutableTree(dbMock, 0, false, log.NewNopLogger()) - require.Nil(t, err) + tree := NewMutableTree(dbMock, 0, false, log.NewNopLogger()) require.NotNil(t, tree) isFastCacheEnabled, err := tree.IsFastCacheEnabled() @@ -892,8 +873,7 @@ func TestFastStorageReUpgradeProtection_NoForceUpgrade_Success(t *testing.T) { dbMock.EXPECT().NewBatch().Return(batchMock).Times(1) dbMock.EXPECT().ReverseIterator(gomock.Any(), gomock.Any()).Return(rIterMock, nil).Times(1) // called to get latest version - tree, err := NewMutableTree(dbMock, 0, false, log.NewNopLogger()) - require.Nil(t, err) + tree := NewMutableTree(dbMock, 0, false, log.NewNopLogger()) require.NotNil(t, tree) // Pretend that we called Load and have the latest state in the tree @@ -985,8 +965,7 @@ func TestFastStorageReUpgradeProtection_ForceUpgradeFirstTime_NoForceSecondTime_ iterMock.EXPECT().Valid().Return(false).Times(1) iterMock.EXPECT().Close().Return(nil).Times(1) - tree, err := NewMutableTree(dbMock, 0, false, log.NewNopLogger()) - require.Nil(t, err) + tree := NewMutableTree(dbMock, 0, false, log.NewNopLogger()) require.NotNil(t, tree) // Pretend that we called Load and have the latest state in the tree @@ -1036,7 +1015,7 @@ func TestUpgradeStorageToFast_Integration_Upgraded_FastIterator_Success(t *testi require.False(t, isUpgradeable) require.NoError(t, err) - sut, _ := NewMutableTree(tree.ndb.db, 1000, false, log.NewNopLogger()) + sut := NewMutableTree(tree.ndb.db, 1000, false, log.NewNopLogger()) isFastCacheEnabled, err = sut.IsFastCacheEnabled() require.NoError(t, err) @@ -1103,7 +1082,7 @@ func TestUpgradeStorageToFast_Integration_Upgraded_GetFast_Success(t *testing.T) require.False(t, isUpgradeable) require.NoError(t, err) - sut, _ := NewMutableTree(tree.ndb.db, 1000, false, log.NewNopLogger()) + sut := NewMutableTree(tree.ndb.db, 1000, false, log.NewNopLogger()) isFastCacheEnabled, err = sut.IsFastCacheEnabled() require.NoError(t, err) @@ -1242,7 +1221,7 @@ func TestUpgradeStorageToFast_Delete_Stale_Success(t *testing.T) { func setupTreeAndMirror(t *testing.T, numEntries int, skipFastStorageUpgrade bool) (*MutableTree, [][]string) { db := db.NewMemDB() - tree, _ := NewMutableTree(db, 0, skipFastStorageUpgrade, log.NewNopLogger()) + tree := NewMutableTree(db, 0, skipFastStorageUpgrade, log.NewNopLogger()) keyPrefix, valPrefix := "key", "val" @@ -1290,7 +1269,7 @@ func TestNoFastStorageUpgrade_Integration_SaveVersion_Load_Get_Success(t *testin require.False(t, isUpgradeable) require.NoError(t, err) - sut, _ := NewMutableTree(tree.ndb.db, 1000, true, log.NewNopLogger()) + sut := NewMutableTree(tree.ndb.db, 1000, true, log.NewNopLogger()) isFastCacheEnabled, err = sut.IsFastCacheEnabled() require.NoError(t, err) @@ -1377,7 +1356,7 @@ func TestNoFastStorageUpgrade_Integration_SaveVersion_Load_Iterate_Success(t *te require.False(t, isUpgradeable) require.NoError(t, err) - sut, _ := NewMutableTree(tree.ndb.db, 1000, true, log.NewNopLogger()) + sut := NewMutableTree(tree.ndb.db, 1000, true, log.NewNopLogger()) isFastCacheEnabled, err = sut.IsFastCacheEnabled() require.NoError(t, err) @@ -1436,12 +1415,11 @@ func TestMutableTree_InitialVersion_FirstVersion(t *testing.T) { db := db.NewMemDB() initialVersion := int64(1000) - tree, err := NewMutableTreeWithOpts(db, 0, &Options{ + tree := NewMutableTreeWithOpts(db, 0, &Options{ InitialVersion: uint64(initialVersion), }, true, log.NewNopLogger()) - require.NoError(t, err) - _, err = tree.Set([]byte("hello"), []byte("world")) + _, err := tree.Set([]byte("hello"), []byte("world")) require.NoError(t, err) _, version, err := tree.SaveVersion() diff --git a/nodedb_test.go b/nodedb_test.go index 613e4e605..80f5aa17c 100644 --- a/nodedb_test.go +++ b/nodedb_test.go @@ -258,7 +258,7 @@ func TestIsFastStorageEnabled_False(t *testing.T) { } func TestTraverseNodes(t *testing.T) { - tree, _ := getTestTree(0) + tree := getTestTree(0) // version 1 for i := 0; i < 20; i++ { _, err := tree.Set([]byte{byte(i)}, []byte{byte(i)}) @@ -310,7 +310,7 @@ func assertOrphansAndBranches(t *testing.T, ndb *nodeDB, version int64, branches } func TestNodeDB_traverseOrphans(t *testing.T) { - tree, _ := getTestTree(0) + tree := getTestTree(0) var up bool var err error @@ -385,8 +385,7 @@ func TestNodeDB_traverseOrphans(t *testing.T) { func makeAndPopulateMutableTree(tb testing.TB) *MutableTree { memDB := db.NewMemDB() - tree, err := NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 9}, false, log.NewNopLogger()) - require.NoError(tb, err) + tree := NewMutableTreeWithOpts(memDB, 0, &Options{InitialVersion: 9}, false, log.NewNopLogger()) for i := 0; i < 1e4; i++ { buf := make([]byte, 0, (i/255)+1) @@ -395,7 +394,7 @@ func makeAndPopulateMutableTree(tb testing.TB) *MutableTree { } tree.Set(buf, buf) //nolint:errcheck } - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.Nil(tb, err, "Expected .SaveVersion to succeed") return tree } diff --git a/proof_iavl_test.go b/proof_iavl_test.go index 8c77097ee..722603c83 100644 --- a/proof_iavl_test.go +++ b/proof_iavl_test.go @@ -10,12 +10,11 @@ import ( ) func TestProofOp(t *testing.T) { - tree, err := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil, false, log.NewNopLogger()) keys := []byte{0x0a, 0x11, 0x2e, 0x32, 0x50, 0x72, 0x99, 0xa1, 0xe4, 0xf7} // 10 total. for _, ikey := range keys { key := []byte{ikey} - _, err = tree.Set(key, key) + _, err := tree.Set(key, key) require.NoError(t, err) } diff --git a/proof_ics23_test.go b/proof_ics23_test.go index a89316399..530a674bf 100644 --- a/proof_ics23_test.go +++ b/proof_ics23_test.go @@ -204,10 +204,7 @@ func GetNonKey(allkeys [][]byte, loc Where) []byte { // BuildTree creates random key/values and stores in tree // returns a list of all keys in sorted order func BuildTree(size int, cacheSize int) (itree *MutableTree, keys [][]byte, err error) { - tree, err := NewMutableTree(db.NewMemDB(), cacheSize, false, log.NewNopLogger()) - if err != nil { - return nil, nil, err - } + tree := NewMutableTree(db.NewMemDB(), cacheSize, false, log.NewNopLogger()) // insert lots of info and store the bytes keys = make([][]byte, size) diff --git a/proof_test.go b/proof_test.go index c0d711d63..0c475c95b 100644 --- a/proof_test.go +++ b/proof_test.go @@ -14,8 +14,7 @@ import ( func TestTreeGetProof(t *testing.T) { require := require.New(t) - tree, err := getTestTree(0) - require.NoError(err) + tree := getTestTree(0) for _, ikey := range []byte{0x11, 0x32, 0x50, 0x72, 0x99} { key := []byte{ikey} tree.Set(key, []byte(iavlrand.RandStr(8))) @@ -41,11 +40,10 @@ func TestTreeGetProof(t *testing.T) { } func TestTreeKeyExistsProof(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) // should get error - _, err = tree.GetProof([]byte("foo")) + _, err := tree.GetProof([]byte("foo")) assert.Error(t, err) // insert lots of info and store the bytes diff --git a/testutils_test.go b/testutils_test.go index 0a05f6aed..3673ab0c8 100644 --- a/testutils_test.go +++ b/testutils_test.go @@ -42,7 +42,7 @@ func b2i(bz []byte) int { } // Construct a MutableTree -func getTestTree(cacheSize int) (*MutableTree, error) { +func getTestTree(cacheSize int) *MutableTree { return NewMutableTreeWithOpts(db.NewMemDB(), cacheSize, nil, false, log.NewNopLogger()) } @@ -72,7 +72,7 @@ func N(l, r interface{}) *Node { // Setup a deep node func T(n *Node) (*MutableTree, error) { - t, _ := getTestTree(0) + t := getTestTree(0) n.hashWithCount(t.version + 1) t.root = n @@ -164,8 +164,7 @@ func getSortedMirrorKeys(mirror map[string]string) []string { func getRandomizedTreeAndMirror(t *testing.T) (*MutableTree, map[string]string) { const cacheSize = 100 - tree, err := getTestTree(cacheSize) - require.NoError(t, err) + tree := getTestTree(cacheSize) mirror := make(map[string]string) @@ -323,8 +322,7 @@ func benchmarkImmutableAvlTreeWithDB(b *testing.B, db db.DB) { b.StopTimer() - t, err := NewMutableTree(db, 100000, false, log.NewNopLogger()) - require.NoError(b, err) + t := NewMutableTree(db, 100000, false, log.NewNopLogger()) value := []byte{} for i := 0; i < 1000000; i++ { diff --git a/tree_dotgraph_test.go b/tree_dotgraph_test.go index 404918273..41029307d 100644 --- a/tree_dotgraph_test.go +++ b/tree_dotgraph_test.go @@ -3,13 +3,10 @@ package iavl import ( "io" "testing" - - "github.com/stretchr/testify/require" ) func TestWriteDOTGraph(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) for _, ikey := range []byte{ 0x0a, 0x11, 0x2e, 0x32, 0x50, 0x72, 0x99, 0xa1, 0xe4, 0xf7, } { diff --git a/tree_fuzz_test.go b/tree_fuzz_test.go index ce71892ca..4abe43162 100644 --- a/tree_fuzz_test.go +++ b/tree_fuzz_test.go @@ -113,10 +113,9 @@ func TestMutableTreeFuzz(t *testing.T) { for size := 5; iterations < maxIterations; size++ { for i := 0; i < progsPerIteration/size; i++ { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) program := genRandomProgram(size) - err = program.Execute(tree) + err := program.Execute(tree) if err != nil { str, err := tree.String() require.Nil(t, err) diff --git a/tree_random_test.go b/tree_random_test.go index 22c0d237f..09c0e4278 100644 --- a/tree_random_test.go +++ b/tree_random_test.go @@ -80,8 +80,7 @@ func testRandomOperations(t *testing.T, randSeed int64) { if !(r.Float64() < cacheChance) { cacheSize = 0 } - tree, err = NewMutableTreeWithOpts(levelDB, cacheSize, options, false, log.NewNopLogger()) - require.NoError(t, err) + tree = NewMutableTreeWithOpts(levelDB, cacheSize, options, false, log.NewNopLogger()) version, err = tree.Load() require.NoError(t, err) t.Logf("Loaded version %v (sync=%v cache=%v)", version, options.Sync, cacheSize) diff --git a/tree_test.go b/tree_test.go index 1c8b3afe8..4c0b3ca7a 100644 --- a/tree_test.go +++ b/tree_test.go @@ -54,8 +54,7 @@ func TestVersionedRandomTree(t *testing.T) { d, closeDB := getTestDB() defer closeDB() - tree, err := NewMutableTree(d, 100, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(d, 100, false, log.NewNopLogger()) versions := 50 keysPerVersion := 30 @@ -131,8 +130,7 @@ func TestTreeHash(t *testing.T) { require.Len(t, expectHashes, versions, "must have expected hashes for all versions") r := rand.New(rand.NewSource(randSeed)) - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) keys := make([][]byte, 0, versionOps) for i := 0; i < versions; i++ { @@ -182,10 +180,8 @@ func TestVersionedRandomTreeSmallKeys(t *testing.T) { d, closeDB := getTestDB() defer closeDB() - tree, err := NewMutableTree(d, 100, false, log.NewNopLogger()) - require.NoError(err) - singleVersionTree, err := getTestTree(0) - require.NoError(err) + tree := NewMutableTree(d, 100, false, log.NewNopLogger()) + singleVersionTree := getTestTree(0) versions := 20 keysPerVersion := 50 @@ -232,10 +228,8 @@ func TestVersionedRandomTreeSmallKeysRandomDeletes(t *testing.T) { d, closeDB := getTestDB() defer closeDB() - tree, err := NewMutableTree(d, 100, false, log.NewNopLogger()) - require.NoError(err) - singleVersionTree, err := getTestTree(0) - require.NoError(err) + tree := NewMutableTree(d, 100, false, log.NewNopLogger()) + singleVersionTree := getTestTree(0) versions := 30 keysPerVersion := 50 @@ -278,8 +272,7 @@ func TestVersionedRandomTreeSmallKeysRandomDeletes(t *testing.T) { } func TestVersionedTreeSpecial1(t *testing.T) { - tree, err := getTestTree(100) - require.NoError(t, err) + tree := getTestTree(100) tree.Set([]byte("C"), []byte("so43QQFN")) tree.SaveVersion() @@ -304,8 +297,7 @@ func TestVersionedTreeSpecial1(t *testing.T) { func TestVersionedRandomTreeSpecial2(t *testing.T) { require := require.New(t) - tree, err := getTestTree(100) - require.NoError(err) + tree := getTestTree(100) tree.Set([]byte("OFMe2Yvm"), []byte("ez2OtQtE")) tree.Set([]byte("WEN4iN7Y"), []byte("kQNyUalI")) @@ -327,8 +319,7 @@ func TestVersionedEmptyTree(t *testing.T) { d, closeDB := getTestDB() defer closeDB() - tree, err := NewMutableTree(d, 0, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(d, 0, false, log.NewNopLogger()) hash, v, err := tree.SaveVersion() require.NoError(err) @@ -368,8 +359,7 @@ func TestVersionedEmptyTree(t *testing.T) { tree.Set([]byte("k"), []byte("v")) // Now reload the tree. - tree, err = NewMutableTree(d, 0, false, log.NewNopLogger()) - require.NoError(err) + tree = NewMutableTree(d, 0, false, log.NewNopLogger()) tree.Load() require.False(tree.VersionExists(1)) @@ -385,8 +375,7 @@ func TestVersionedTree(t *testing.T) { d, closeDB := getTestDB() defer closeDB() - tree, err := NewMutableTree(d, 0, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(d, 0, false, log.NewNopLogger()) // We start with empty database. require.Equal(0, tree.ndb.size()) @@ -436,8 +425,7 @@ func TestVersionedTree(t *testing.T) { // Recreate a new tree and load it, to make sure it works in this // scenario. - tree, err = NewMutableTree(d, 100, false, log.NewNopLogger()) - require.NoError(err) + tree = NewMutableTree(d, 100, false, log.NewNopLogger()) _, err = tree.Load() require.NoError(err) @@ -490,8 +478,7 @@ func TestVersionedTree(t *testing.T) { require.EqualValues(hash3, hash4) require.NotNil(hash4) - tree, err = NewMutableTree(d, 100, false, log.NewNopLogger()) - require.NoError(err) + tree = NewMutableTree(d, 100, false, log.NewNopLogger()) _, err = tree.Load() require.NoError(err) @@ -606,8 +593,7 @@ func TestVersionedTreeVersionDeletingEfficiency(t *testing.T) { d, closeDB := getTestDB() defer closeDB() - tree, err := NewMutableTree(d, 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(d, 0, false, log.NewNopLogger()) tree.Set([]byte("key0"), []byte("val0")) tree.Set([]byte("key1"), []byte("val0")) @@ -642,8 +628,7 @@ func TestVersionedTreeVersionDeletingEfficiency(t *testing.T) { require.Nil(t, err) require.Len(t, leafNodes, 3) - tree2, err := getTestTree(0) - require.NoError(t, err) + tree2 := getTestTree(0) tree2.Set([]byte("key0"), []byte("val2")) tree2.Set([]byte("key2"), []byte("val2")) tree2.Set([]byte("key3"), []byte("val1")) @@ -653,8 +638,7 @@ func TestVersionedTreeVersionDeletingEfficiency(t *testing.T) { } func TestVersionedTreeOrphanDeleting(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) tree.Set([]byte("key0"), []byte("val0")) tree.Set([]byte("key1"), []byte("val0")) @@ -701,8 +685,7 @@ func TestVersionedTreeSpecialCase(t *testing.T) { d, closeDB := getTestDB() defer closeDB() - tree, err := NewMutableTree(d, 0, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(d, 0, false, log.NewNopLogger()) tree.Set([]byte("key1"), []byte("val0")) tree.Set([]byte("key2"), []byte("val0")) @@ -726,8 +709,7 @@ func TestVersionedTreeSpecialCase2(t *testing.T) { require := require.New(t) d := db.NewMemDB() - tree, err := NewMutableTree(d, 100, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(d, 100, false, log.NewNopLogger()) tree.Set([]byte("key1"), []byte("val0")) tree.Set([]byte("key2"), []byte("val0")) @@ -740,9 +722,8 @@ func TestVersionedTreeSpecialCase2(t *testing.T) { tree.Set([]byte("key2"), []byte("val2")) tree.SaveVersion() - tree, err = NewMutableTree(d, 100, false, log.NewNopLogger()) - require.NoError(err) - _, err = tree.Load() + tree = NewMutableTree(d, 100, false, log.NewNopLogger()) + _, err := tree.Load() require.NoError(err) require.NoError(tree.DeleteVersionsTo(2)) @@ -754,8 +735,7 @@ func TestVersionedTreeSpecialCase2(t *testing.T) { func TestVersionedTreeSpecialCase3(t *testing.T) { require := require.New(t) - tree, err := getTestTree(0) - require.NoError(err) + tree := getTestTree(0) tree.Set([]byte("m"), []byte("liWT0U6G")) tree.Set([]byte("G"), []byte("7PxRXwUA")) @@ -786,8 +766,7 @@ func TestVersionedTreeSpecialCase3(t *testing.T) { func TestVersionedTreeSaveAndLoad(t *testing.T) { require := require.New(t) d := db.NewMemDB() - tree, err := NewMutableTree(d, 0, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(d, 0, false, log.NewNopLogger()) // Loading with an empty root is a no-op. tree.Load() @@ -811,8 +790,7 @@ func TestVersionedTreeSaveAndLoad(t *testing.T) { require.Equal(int64(6), tree.Version()) // Reload the tree, to test that roots and orphans are properly loaded. - ntree, err := NewMutableTree(d, 0, false, log.NewNopLogger()) - require.NoError(err) + ntree := NewMutableTree(d, 0, false, log.NewNopLogger()) ntree.Load() require.False(ntree.IsEmpty()) @@ -837,8 +815,7 @@ func TestVersionedTreeSaveAndLoad(t *testing.T) { func TestVersionedTreeErrors(t *testing.T) { require := require.New(t) - tree, err := getTestTree(100) - require.NoError(err) + tree := getTestTree(100) // Can't delete non-existent versions. require.Error(tree.DeleteVersionsTo(1)) @@ -847,7 +824,7 @@ func TestVersionedTreeErrors(t *testing.T) { tree.Set([]byte("key"), []byte("val")) // Saving with content is ok. - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(err) // Can't delete current version. @@ -866,8 +843,7 @@ func TestVersionedTreeErrors(t *testing.T) { func TestVersionedCheckpointsSpecialCase(t *testing.T) { require := require.New(t) - tree, err := getTestTree(0) - require.NoError(err) + tree := getTestTree(0) key := []byte("k") tree.Set(key, []byte("val1")) @@ -893,8 +869,7 @@ func TestVersionedCheckpointsSpecialCase(t *testing.T) { } func TestVersionedCheckpointsSpecialCase2(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) tree.Set([]byte("U"), []byte("XamDUtiJ")) tree.Set([]byte("A"), []byte("UkZBuYIU")) @@ -914,8 +889,7 @@ func TestVersionedCheckpointsSpecialCase2(t *testing.T) { } func TestVersionedCheckpointsSpecialCase3(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) tree.Set([]byte("n"), []byte("2wUCUs8q")) tree.Set([]byte("l"), []byte("WQ7mvMbc")) @@ -935,8 +909,7 @@ func TestVersionedCheckpointsSpecialCase3(t *testing.T) { } func TestVersionedCheckpointsSpecialCase4(t *testing.T) { - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(t, err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) tree.Set([]byte("U"), []byte("XamDUtiJ")) tree.Set([]byte("A"), []byte("UkZBuYIU")) @@ -972,8 +945,7 @@ func TestVersionedCheckpointsSpecialCase4(t *testing.T) { } func TestVersionedCheckpointsSpecialCase5(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) tree.Set([]byte("R"), []byte("ygZlIzeW")) tree.SaveVersion() @@ -990,8 +962,7 @@ func TestVersionedCheckpointsSpecialCase5(t *testing.T) { } func TestVersionedCheckpointsSpecialCase6(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) tree.Set([]byte("Y"), []byte("MW79JQeV")) tree.Set([]byte("7"), []byte("Kp0ToUJB")) @@ -1023,8 +994,7 @@ func TestVersionedCheckpointsSpecialCase6(t *testing.T) { } func TestVersionedCheckpointsSpecialCase7(t *testing.T) { - tree, err := getTestTree(100) - require.NoError(t, err) + tree := getTestTree(100) tree.Set([]byte("n"), []byte("OtqD3nyn")) tree.Set([]byte("W"), []byte("kMdhJjF5")) @@ -1058,8 +1028,7 @@ func TestVersionedCheckpointsSpecialCase7(t *testing.T) { func TestVersionedTreeEfficiency(t *testing.T) { require := require.New(t) - tree, err := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(db.NewMemDB(), 0, false, log.NewNopLogger()) versions := 20 keysPerVersion := 100 keysAddedPerVersion := map[int]int{} @@ -1106,13 +1075,12 @@ func TestVersionedTreeEfficiency(t *testing.T) { func TestVersionedTreeProofs(t *testing.T) { require := require.New(t) - tree, err := getTestTree(0) - require.NoError(err) + tree := getTestTree(0) tree.Set([]byte("k1"), []byte("v1")) tree.Set([]byte("k2"), []byte("v1")) tree.Set([]byte("k3"), []byte("v1")) - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(err) // fmt.Println("TREE VERSION 1") @@ -1188,8 +1156,7 @@ func TestOrphans(t *testing.T) { // Then randomly delete versions other than the first and last until only those two remain // Any remaining orphan nodes should either have fromVersion == firstVersion || toVersion == lastVersion require := require.New(t) - tree, err := NewMutableTree(db.NewMemDB(), 100, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(db.NewMemDB(), 100, false, log.NewNopLogger()) NUMVERSIONS := 100 NUMUPDATES := 100 @@ -1198,20 +1165,19 @@ func TestOrphans(t *testing.T) { for j := 1; j < NUMUPDATES; j++ { tree.Set(iavlrand.RandBytes(2), iavlrand.RandBytes(2)) } - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(err, "SaveVersion should not error") } for v := 1; v < NUMVERSIONS; v++ { - err = tree.DeleteVersionsTo(int64(v)) + err := tree.DeleteVersionsTo(int64(v)) require.NoError(err, "DeleteVersion should not error") } } func TestVersionedTreeHash(t *testing.T) { require := require.New(t) - tree, err := getTestTree(0) - require.NoError(err) + tree := getTestTree(0) hash := tree.Hash() require.Equal("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", hex.EncodeToString(hash)) @@ -1241,18 +1207,16 @@ func TestVersionedTreeHash(t *testing.T) { func TestNilValueSemantics(t *testing.T) { require := require.New(t) - tree, err := getTestTree(0) - require.NoError(err) + tree := getTestTree(0) - _, err = tree.Set([]byte("k"), nil) + _, err := tree.Set([]byte("k"), nil) require.Error(err) } func TestCopyValueSemantics(t *testing.T) { require := require.New(t) - tree, err := getTestTree(0) - require.NoError(err) + tree := getTestTree(0) val := []byte("v1") @@ -1271,8 +1235,7 @@ func TestCopyValueSemantics(t *testing.T) { func TestRollback(t *testing.T) { require := require.New(t) - tree, err := getTestTree(0) - require.NoError(err) + tree := getTestTree(0) tree.Set([]byte("k"), []byte("v")) tree.SaveVersion() @@ -1302,8 +1265,7 @@ func TestRollback(t *testing.T) { } func TestLoadVersion(t *testing.T) { - tree, err := getTestTree(0) - require.NoError(t, err) + tree := getTestTree(0) maxVersions := 10 version, err := tree.LoadVersion(0) @@ -1345,12 +1307,11 @@ func TestOverwrite(t *testing.T) { require := require.New(t) mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 0, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(mdb, 0, false, log.NewNopLogger()) // Set one kv pair and save version 1 tree.Set([]byte("key1"), []byte("value1")) - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(err, "SaveVersion should not fail") // Set another kv pair and save version 2 @@ -1359,8 +1320,7 @@ func TestOverwrite(t *testing.T) { require.NoError(err, "SaveVersion should not fail") // Reload tree at version 1 - tree, err = NewMutableTree(mdb, 0, false, log.NewNopLogger()) - require.NoError(err) + tree = NewMutableTree(mdb, 0, false, log.NewNopLogger()) _, err = tree.LoadVersion(int64(1)) require.NoError(err, "LoadVersion should not fail") @@ -1379,11 +1339,10 @@ func TestOverwriteEmpty(t *testing.T) { require := require.New(t) mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 0, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(mdb, 0, false, log.NewNopLogger()) // Save empty version 1 - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(err) // Save empty version 2 @@ -1414,25 +1373,22 @@ func TestLoadVersionForOverwriting(t *testing.T) { require := require.New(t) mdb := db.NewMemDB() - tree, err := NewMutableTree(mdb, 0, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTree(mdb, 0, false, log.NewNopLogger()) maxLength := 100 for count := 1; count <= maxLength; count++ { countStr := strconv.Itoa(count) // Set one kv pair and save version tree.Set([]byte("key"+countStr), []byte("value"+countStr)) - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(err, "SaveVersion should not fail") } - tree, err = NewMutableTree(mdb, 0, false, log.NewNopLogger()) - require.NoError(err) + tree = NewMutableTree(mdb, 0, false, log.NewNopLogger()) require.Error(tree.LoadVersionForOverwriting(int64(maxLength * 2))) - tree, err = NewMutableTree(mdb, 0, false, log.NewNopLogger()) - require.NoError(err) - err = tree.LoadVersionForOverwriting(int64(maxLength / 2)) + tree = NewMutableTree(mdb, 0, false, log.NewNopLogger()) + err := tree.LoadVersionForOverwriting(int64(maxLength / 2)) require.NoError(err, "LoadVersion should not fail") for version := 1; version <= maxLength/2; version++ { @@ -1454,8 +1410,7 @@ func TestLoadVersionForOverwriting(t *testing.T) { require.NoError(err, "SaveVersion should not fail, overwrite was allowed") // Reload tree at version 50, the latest tree version is 52 - tree, err = NewMutableTree(mdb, 0, false, log.NewNopLogger()) - require.NoError(err) + tree = NewMutableTree(mdb, 0, false, log.NewNopLogger()) _, err = tree.LoadVersion(int64(maxLength / 2)) require.NoError(err, "LoadVersion should not fail") @@ -1491,8 +1446,7 @@ func BenchmarkTreeLoadAndDelete(b *testing.B) { defer d.Close() defer os.RemoveAll("./bench.db") - tree, err := NewMutableTree(d, 0, false, log.NewNopLogger()) - require.NoError(b, err) + tree := NewMutableTree(d, 0, false, log.NewNopLogger()) for v := 1; v < numVersions; v++ { for i := 0; i < numKeysPerVersion; i++ { tree.Set([]byte(iavlrand.RandStr(16)), iavlrand.RandBytes(32)) @@ -1503,7 +1457,7 @@ func BenchmarkTreeLoadAndDelete(b *testing.B) { b.Run("LoadAndDelete", func(b *testing.B) { for n := 0; n < b.N; n++ { b.StopTimer() - tree, err = NewMutableTree(d, 0, false, log.NewNopLogger()) + tree = NewMutableTree(d, 0, false, log.NewNopLogger()) require.NoError(b, err) runtime.GC() b.StartTimer() @@ -1527,7 +1481,7 @@ func BenchmarkTreeLoadAndDelete(b *testing.B) { func TestLoadVersionForOverwritingCase2(t *testing.T) { require := require.New(t) - tree, _ := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil, false, log.NewNopLogger()) + tree := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil, false, log.NewNopLogger()) for i := byte(0); i < 20; i++ { tree.Set([]byte{i}, []byte{i}) @@ -1589,13 +1543,12 @@ func TestLoadVersionForOverwritingCase2(t *testing.T) { func TestLoadVersionForOverwritingCase3(t *testing.T) { require := require.New(t) - tree, err := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil, false, log.NewNopLogger()) - require.NoError(err) + tree := NewMutableTreeWithOpts(db.NewMemDB(), 0, nil, false, log.NewNopLogger()) for i := byte(0); i < 20; i++ { tree.Set([]byte{i}, []byte{i}) } - _, _, err = tree.SaveVersion() + _, _, err := tree.SaveVersion() require.NoError(err) for i := byte(0); i < 20; i++ { @@ -1720,8 +1673,7 @@ func Benchmark_GetWithIndex(b *testing.B) { const numKeyVals = 100000 - t, err := NewMutableTree(db, numKeyVals, false, log.NewNopLogger()) - require.NoError(b, err) + t := NewMutableTree(db, numKeyVals, false, log.NewNopLogger()) keys := make([][]byte, 0, numKeyVals) @@ -1772,8 +1724,7 @@ func Benchmark_GetByIndex(b *testing.B) { const numKeyVals = 100000 - t, err := NewMutableTree(db, numKeyVals, false, log.NewNopLogger()) - require.NoError(b, err) + t := NewMutableTree(db, numKeyVals, false, log.NewNopLogger()) for i := 0; i < numKeyVals; i++ { key := iavlrand.RandBytes(10) @@ -1848,8 +1799,7 @@ func TestNodeCacheStatisic(t *testing.T) { opts := &Options{Stat: stat} db, err := db.NewDB("test", db.MemDBBackend, "") require.NoError(t, err) - mt, err := NewMutableTreeWithOpts(db, tc.cacheSize, opts, false, log.NewNopLogger()) - require.NoError(t, err) + mt := NewMutableTreeWithOpts(db, tc.cacheSize, opts, false, log.NewNopLogger()) for i := 0; i < numKeyVals; i++ { key := []byte(strconv.Itoa(i))