Skip to content

Commit

Permalink
Merge pull request #51 from provenance-io/dwedul/10948-remove-dbbacke…
Browse files Browse the repository at this point in the history
…nd-var

feat(types): Deprecate the DBBackend variable in favor of the Tendermint Config's db_backend value.
  • Loading branch information
dwedul-figure authored Feb 15, 2022
2 parents 2174fef + 824ad49 commit b529968
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Improvements

* (types) [\#10948](https://github.com/cosmos/cosmos-sdk/issues/10948) Use Tendermint `db_backend` config value instead of compile-time `types.DBBackend` value.

# Provenance Specific releases

## [v0.45-pio-1](https://github.com/provenance-io/cosmos-sdk/releases/tag/v0.45-pio-1.8.rc1)
Expand Down
9 changes: 1 addition & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ ifeq ($(LEDGER_ENABLED),true)
endif
endif

ifeq (cleveldb,$(findstring cleveldb,$(COSMOS_BUILD_OPTIONS)))
build_tags += gcc
endif

whitespace :=
whitespace += $(whitespace)
comma := ,
Expand All @@ -62,22 +58,19 @@ ldflags = -X github.com/cosmos/cosmos-sdk/version.Name=sim \

# DB backend selection
ifeq (cleveldb,$(findstring cleveldb,$(COSMOS_BUILD_OPTIONS)))
ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb
build_tags += gcc
endif
ifeq (badgerdb,$(findstring badgerdb,$(COSMOS_BUILD_OPTIONS)))
ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=badgerdb
BUILD_TAGS += badgerdb
endif
# handle rocksdb
ifeq (rocksdb,$(findstring rocksdb,$(COSMOS_BUILD_OPTIONS)))
CGO_ENABLED=1
BUILD_TAGS += rocksdb
ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=rocksdb
endif
# handle boltdb
ifeq (boltdb,$(findstring boltdb,$(COSMOS_BUILD_OPTIONS)))
BUILD_TAGS += boltdb
ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=boltdb
endif

ifeq (,$(findstring nostrip,$(COSMOS_BUILD_OPTIONS)))
Expand Down
2 changes: 1 addition & 1 deletion server/constructors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func Test_openDB(t *testing.T) {
t.Parallel()
_, err := openDB(t.TempDir())
_, err := openDB(t.TempDir(), "")
require.NoError(t, err)
}

Expand Down
2 changes: 1 addition & 1 deletion server/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ExportCmd(appExporter types.AppExporter, defaultNodeHome string) *cobra.Com
return err
}

db, err := openDB(config.RootDir)
db, err := openDB(config.RootDir, config.DBBackend)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion server/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
// similar to a real app. Make sure rootDir is empty before running the test,
// in order to guarantee consistent results
func NewApp(rootDir string, logger log.Logger) (abci.Application, error) {
db, err := sdk.NewLevelDB("mock", filepath.Join(rootDir, "data"))
db, err := sdk.NewDB("mock", "", filepath.Join(rootDir, "data"))
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func startStandAlone(ctx *Context, appCreator types.AppCreator) error {
transport := ctx.Viper.GetString(flagTransport)
home := ctx.Viper.GetString(flags.FlagHome)

db, err := openDB(home)
db, err := openDB(home, ctx.Viper.GetString("db_backend"))
if err != nil {
return err
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
}

traceWriterFile := ctx.Viper.GetString(flagTraceStore)
db, err := openDB(home)
db, err := openDB(home, cfg.DBBackend)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,9 @@ func addrToIP(addr net.Addr) net.IP {
return ip
}

func openDB(rootDir string) (dbm.DB, error) {
func openDB(rootDir, backendType string) (dbm.DB, error) {
dataDir := filepath.Join(rootDir, "data")
return sdk.NewLevelDB("application", dataDir)
return sdk.NewDB("application", backendType, dataDir)
}

func openTraceWriter(traceWriterFile string) (w io.Writer, err error) {
Expand Down
3 changes: 3 additions & 0 deletions simapp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
FlagCommitValue bool
FlagOnOperationValue bool // TODO: Remove in favor of binary search for invariant violation
FlagAllInvariantsValue bool
FlagDBBackendValue string

FlagEnabledValue bool
FlagVerboseValue bool
Expand All @@ -46,6 +47,7 @@ func GetSimulatorFlags() {
flag.BoolVar(&FlagCommitValue, "Commit", false, "have the simulation commit")
flag.BoolVar(&FlagOnOperationValue, "SimulateEveryOperation", false, "run slow invariants every operation")
flag.BoolVar(&FlagAllInvariantsValue, "PrintAllInvariants", false, "print all invariants if a broken invariant is found")
flag.StringVar(&FlagDBBackendValue, "DBBackend", "goleveldb", "custom db backend type")

// simulation flags
flag.BoolVar(&FlagEnabledValue, "Enabled", false, "enable the simulation")
Expand All @@ -71,5 +73,6 @@ func NewConfigFromFlags() simulation.Config {
Commit: FlagCommitValue,
OnOperation: FlagOnOperationValue,
AllInvariants: FlagAllInvariantsValue,
DBBackend: FlagDBBackendValue,
}
}
2 changes: 1 addition & 1 deletion simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a
}

snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots")
snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDir)
snapshotDB, err := sdk.NewDB("metadata", cast.ToString(appOpts.Get("db_backend")), snapshotDir)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion simapp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func SetupSimulation(dirPrefix, dbName string) (simtypes.Config, dbm.DB, string,
return simtypes.Config{}, nil, "", nil, false, err
}

db, err := sdk.NewLevelDB(dbName, dir)
db, err := sdk.NewDB(dbName, config.DBBackend, dir)
if err != nil {
return simtypes.Config{}, nil, "", nil, false, err
}
Expand Down
2 changes: 2 additions & 0 deletions types/simulation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ type Config struct {

OnOperation bool // run slow invariants every operation
AllInvariants bool // print all failed invariants if a broken invariant is found

DBBackend string // custom db backend type
}
27 changes: 26 additions & 1 deletion types/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (

var (
// This is set at compile time. Could be cleveldb, defaults is goleveldb.
DBBackend = ""
DBBackend = "" // Deprecated: Use tendermint config's DBBackend value instead.
backend = dbm.GoLevelDBBackend
)

func init() {
if len(DBBackend) != 0 {
backend = dbm.BackendType(DBBackend)
} else {
DBBackend = string(backend)
}
}

Expand Down Expand Up @@ -85,6 +87,8 @@ func ParseTimeBytes(bz []byte) (time.Time, error) {
}

// NewLevelDB instantiate a new LevelDB instance according to DBBackend.
//
// Deprecated: Use NewDB instead. Suggested backendType is tendermint config's DBBackend value.
func NewLevelDB(name, dir string) (db dbm.DB, err error) {
defer func() {
if r := recover(); r != nil {
Expand All @@ -95,6 +99,27 @@ func NewLevelDB(name, dir string) (db dbm.DB, err error) {
return dbm.NewDB(name, backend, dir)
}

// NewDB instantiate a new DB instance.
// This differs from the tendermint/tm-db.NewDB function in three ways:
// 1) Returns an error instead of panicking.
// 2) Takes in a string for the backend type.
// 3) If the backendType is an empty string, "goleveldb" is used.
func NewDB(name, backendType, dir string) (db dbm.DB, err error) {
defer func() {
r := recover()
switch {
case r != nil:
err = fmt.Errorf("could not create %q db in %s with name %q: %v", backendType, dir, name, r)
case err != nil:
err = fmt.Errorf("could not create %q db in %s with name %q: %w", backendType, dir, name, err)
}
}()
if len(backendType) == 0 {
backendType = string(dbm.GoLevelDBBackend)
}
return dbm.NewDB(name, dbm.BackendType(backendType), dir)
}

// copy bytes
func CopyBytes(bz []byte) (ret []byte) {
if bz == nil {
Expand Down
67 changes: 67 additions & 0 deletions types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import (
"testing"
"time"

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

sdk "github.com/cosmos/cosmos-sdk/types"

dbm "github.com/tendermint/tm-db"
)

type utilsTestSuite struct {
Expand Down Expand Up @@ -109,3 +113,66 @@ func (s *utilsTestSuite) TestParseTimeBytes() {
_, err = sdk.ParseTimeBytes([]byte{})
s.Require().Error(err)
}

func (s *utilsTestSuite) TestNewDB() {
tests := []struct {
testName string

name string
backendType string

isGoLevelDB bool
isMemDB bool
errContains []string
}{
{
testName: "unknown backendType gives error",
name: "test-unknown",
backendType: "baddbtype",
errContains: []string{"could not create", "test-unknown", "unknown", "baddbtype", "goleveldb"},
},
{
testName: "empty backendType defaults to goleveldb",
name: "test-empty",
backendType: "",
isGoLevelDB: true,
},
{
testName: "goleveldb returns a GoLevelDB",
name: "test-goleveldb",
backendType: "goleveldb",
isGoLevelDB: true,
},
{
testName: "memdb returns a MemDB",
name: "test-memdb",
backendType: "memdb",
isMemDB: true,
},
}

for _, tc := range tests {
s.T().Run(tc.testName, func(t *testing.T) {
dir := t.TempDir()
var db dbm.DB
var err error
require.NotPanics(t, func() {
db, err = sdk.NewDB(tc.name, tc.backendType, dir)
}, "calling NewDB")
if len(tc.errContains) != 0 {
require.Error(t, err, "err")
for _, exp := range tc.errContains {
assert.Contains(t, err.Error(), exp, "err.Error()")
}
} else {
require.NoError(t, err, "err")
require.NotNil(t, db, "db")
assert.NoError(t, db.Close(), "db.Close()")
_, isGoLevelDB := db.(*dbm.GoLevelDB)
assert.Equal(t, tc.isGoLevelDB, isGoLevelDB, "isGoLevelDB")
_, isMemDB := db.(*dbm.MemDB)
assert.Equal(t, tc.isMemDB, isMemDB, "isMemDB")
}
})
}
}

0 comments on commit b529968

Please sign in to comment.