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

refactor(tm2/pkg/db): Avoid panicking when creating DBs. #726

Merged
merged 2 commits into from
Apr 11, 2023
Merged
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
5 changes: 4 additions & 1 deletion gno.land/pkg/gnoland/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import (
// NewApp creates the GnoLand application.
func NewApp(rootDir string, skipFailingGenesisTxs bool, logger log.Logger) (abci.Application, error) {
// Get main DB.
db := dbm.NewDB("gnolang", dbm.GoLevelDBBackend, filepath.Join(rootDir, "data"))
db, err := dbm.NewDB("gnolang", dbm.GoLevelDBBackend, filepath.Join(rootDir, "data"))
if err != nil {
return nil, fmt.Errorf("error initializing database %q using path %q: %w", dbm.GoLevelDBBackend, rootDir, err)
}

// Capabilities keys.
mainKey := store.NewStoreKey("main")
Expand Down
12 changes: 10 additions & 2 deletions tm2/pkg/bft/consensus/replay_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,19 @@ func (pb *playback) replayConsoleLoop() int {
func newConsensusStateForReplay(config cfg.BaseConfig, csConfig *cnscfg.ConsensusConfig) *ConsensusState {
dbType := dbm.BackendType(config.DBBackend)
// Get BlockStore
blockStoreDB := dbm.NewDB("blockstore", dbType, config.DBDir())
blockStoreDB, err := dbm.NewDB("blockstore", dbType, config.DBDir())
if err != nil {
osm.Exit(err.Error())
}

blockStore := store.NewBlockStore(blockStoreDB)

// Get State
stateDB := dbm.NewDB("state", dbType, config.DBDir())
stateDB, err := dbm.NewDB("state", dbType, config.DBDir())
if err != nil {
osm.Exit(err.Error())
}

gdoc, err := sm.MakeGenesisDocFromFile(config.GenesisFile())
if err != nil {
osm.Exit(err.Error())
Expand Down
6 changes: 2 additions & 4 deletions tm2/pkg/bft/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ import (
rpcserver "github.com/gnolang/gno/tm2/pkg/bft/rpc/lib/server"
sm "github.com/gnolang/gno/tm2/pkg/bft/state"
"github.com/gnolang/gno/tm2/pkg/bft/state/txindex"
"github.com/gnolang/gno/tm2/pkg/events"

//"github.com/gnolang/gno/tm2/pkg/bft/state/txindex/kv"
"github.com/gnolang/gno/tm2/pkg/bft/state/txindex/null"
"github.com/gnolang/gno/tm2/pkg/bft/store"
"github.com/gnolang/gno/tm2/pkg/bft/types"
Expand All @@ -37,6 +34,7 @@ import (
"github.com/gnolang/gno/tm2/pkg/crypto"
dbm "github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/events"
"github.com/gnolang/gno/tm2/pkg/log"
"github.com/gnolang/gno/tm2/pkg/p2p"
"github.com/gnolang/gno/tm2/pkg/service"
Expand All @@ -58,7 +56,7 @@ type DBProvider func(*DBContext) (dbm.DB, error)
// specified in the ctx.Config.
func DefaultDBProvider(ctx *DBContext) (dbm.DB, error) {
dbType := dbm.BackendType(ctx.Config.DBBackend)
return dbm.NewDB(ctx.ID, dbType, ctx.Config.DBDir()), nil
return dbm.NewDB(ctx.ID, dbType, ctx.Config.DBDir())
}

// GenesisDocProvider returns a GenesisDoc.
Expand Down
4 changes: 3 additions & 1 deletion tm2/pkg/bft/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ func setupTestCase(t *testing.T) (func(t *testing.T), dbm.DB, sm.State) {

config := cfg.ResetTestRoot("state_")
dbType := dbm.BackendType(config.DBBackend)
stateDB := dbm.NewDB("state", dbType, config.DBDir())
stateDB, err := dbm.NewDB("state", dbType, config.DBDir())
require.NoError(t, err)

state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile())
assert.NoError(t, err, "expected no error on LoadStateFromDBOrGenesisFile")

Expand Down
8 changes: 4 additions & 4 deletions tm2/pkg/bft/state/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ func BenchmarkLoadValidators(b *testing.B) {
config := cfg.ResetTestRoot("state_")
defer os.RemoveAll(config.RootDir)
dbType := dbm.BackendType(config.DBBackend)
stateDB := dbm.NewDB("state", dbType, config.DBDir())
stateDB, err := dbm.NewDB("state", dbType, config.DBDir())
require.NoError(b, err)

state, err := sm.LoadStateFromDBOrGenesisFile(stateDB, config.GenesisFile())
if err != nil {
b.Fatal(err)
}
require.NoError(b, err)
state.Validators = genValSet(valSetSize)
state.NextValidators = state.Validators.CopyIncrementProposerPriority(1)
sm.SaveState(stateDB, state)
Expand Down
3 changes: 1 addition & 2 deletions tm2/pkg/crypto/keys/armor/armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ func UnarmorDecryptPrivKey(armorStr string, passphrase string) (crypto.PrivKey,
}
saltBytes, err := hex.DecodeString(header["salt"])
if err != nil {
//nolint:errorlint
return privKey, fmt.Errorf("error decoding salt: %v", err.Error())
return privKey, fmt.Errorf("error decoding salt: %w", err)
}
privKey, err = decryptPrivKey(saltBytes, encBytes, passphrase)
return privKey, err
Expand Down
9 changes: 6 additions & 3 deletions tm2/pkg/db/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func testBackendGetSetDelete(t *testing.T, backend BackendType) {
t.Helper()

// Default
db := NewDB("testdb", backend, t.TempDir())
db, err := NewDB("testdb", backend, t.TempDir())
require.NoError(t, err)

// A nonexistent key should return nil, even if the key is empty
require.Nil(t, db.Get([]byte("")))
Expand Down Expand Up @@ -137,7 +138,8 @@ func TestBackendsNilKeys(t *testing.T) {

func TestGoLevelDBBackend(t *testing.T) {
name := fmt.Sprintf("test_%x", randStr(12))
db := NewDB(name, GoLevelDBBackend, t.TempDir())
db, err := NewDB(name, GoLevelDBBackend, t.TempDir())
require.NoError(t, err)

_, ok := db.(*GoLevelDB)
assert.True(t, ok)
Expand All @@ -157,7 +159,8 @@ func testDBIterator(t *testing.T, backend BackendType) {
t.Helper()

name := fmt.Sprintf("test_%x", randStr(12))
db := NewDB(name, backend, t.TempDir())
db, err := NewDB(name, backend, t.TempDir())
require.NoError(t, err)

for i := 0; i < 10; i++ {
if i != 6 { // but skip 6.
Expand Down
5 changes: 4 additions & 1 deletion tm2/pkg/db/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func checkValuePanics(t *testing.T, itr Iterator) {
func newTempDB(t *testing.T, backend BackendType) (db DB) {
t.Helper()

return NewDB("testdb", backend, t.TempDir())
db, err := NewDB("testdb", backend, t.TempDir())
require.NoError(t, err)

return db
}

// ----------------------------------------
Expand Down
14 changes: 6 additions & 8 deletions tm2/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,19 @@ func registerDBCreator(backend BackendType, creator dbCreator, force bool) {
// NOTE: function panics if:
// - backend is unknown (not registered)
// - creator function, provided during registration, returns error
func NewDB(name string, backend BackendType, dir string) DB {
func NewDB(name string, backend BackendType, dir string) (DB, error) {
dbCreator, ok := backends[backend]
if !ok {
keys := make([]string, len(backends))
i := 0
var keys []string
for k := range backends {
keys[i] = string(k)
i++
keys = append(keys, string(k))
}
panic(fmt.Sprintf("Unknown db_backend %s, expected either %s", backend, strings.Join(keys, " or ")))
return nil, fmt.Errorf("unknown db_backend %s. Expected either %s", backend, strings.Join(keys, " or "))
}

db, err := dbCreator(name, dir)
if err != nil {
panic(fmt.Sprintf("Error initializing DB: %v", err))
return nil, fmt.Errorf("error initializing DB: %w", err)
}
return db
return db, nil
}
4 changes: 3 additions & 1 deletion tm2/pkg/iavl/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ func TestRemove(t *testing.T) {
size := 10000
keyLen, dataLen := 16, 40

d := db.NewDB("test", "memdb", "")
d, err := db.NewDB("test", "memdb", "")
require.NoError(t, err)

defer d.Close()
t1 := NewMutableTree(d, size)

Expand Down
5 changes: 4 additions & 1 deletion tm2/pkg/iavl/benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"runtime"
"testing"

"github.com/jaekwon/testify/require"

"github.com/gnolang/gno/tm2/pkg/db"
"github.com/gnolang/gno/tm2/pkg/iavl"
)
Expand Down Expand Up @@ -253,7 +255,8 @@ func runBenchmarks(b *testing.B, benchmarks []benchmark) {
// note that "" leads to nil backing db!
var d db.DB
if bb.dbType != "nodb" {
d = db.NewDB("test", bb.dbType, dirName)
d, err := db.NewDB("test", bb.dbType, dirName)
require.NoError(b, err)
defer d.Close()
}
b.Run(prefix, func(sub *testing.B) {
Expand Down
15 changes: 12 additions & 3 deletions tm2/pkg/iavl/testutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package iavl
import (
"bytes"
"fmt"
mrand "math/rand"
"runtime"
"testing"

mrand "math/rand"
"github.com/jaekwon/testify/require"

"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/db"
Expand Down Expand Up @@ -54,7 +55,11 @@ func N(l, r interface{}) *Node {

// Setup a deep node
func T(n *Node) *MutableTree {
d := db.NewDB("test", db.MemDBBackend, "")
d, err := db.NewDB("test", db.MemDBBackend, "")
if err != nil {
panic(err)
}

t := NewMutableTree(d, 0)

n.hashWithCount()
Expand Down Expand Up @@ -108,7 +113,11 @@ func expectTraverse(t *testing.T, trav traverser, start, end string, count int)
}

func BenchmarkImmutableAvlTreeMemDB(b *testing.B) {
db := db.NewDB("test", db.MemDBBackend, "")
db, err := db.NewDB("test", db.MemDBBackend, "")
require.NoError(b, err)

b.ResetTimer()

benchmarkImmutableAvlTreeWithDB(b, db)
}

Expand Down