From 3e82a8bbb81a1ece51661946c5f56b1f40356ea4 Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Mon, 4 Mar 2019 20:31:55 +0000 Subject: [PATCH] Merge PR #3792: Use DBBackend set at compile time * use DBBackend set at compile time Closes: #3719 * Refresh changelog * Tweak Makefile --- Makefile | 76 ++++++++++++++++++++------------- PENDING.md | 3 ++ cmd/gaia/app/sim_test.go | 12 +++--- cmd/gaia/cmd/gaiadebug/hack.go | 2 +- cmd/gaia/cmd/gaiareplay/main.go | 8 ++-- crypto/keys/lazy_keybase.go | 39 +++++++++-------- server/constructors.go | 3 +- server/mock/app.go | 3 +- types/utils.go | 22 ++++++++++ 9 files changed, 105 insertions(+), 63 deletions(-) diff --git a/Makefile b/Makefile index 0b8c041a9..d939fde1c 100644 --- a/Makefile +++ b/Makefile @@ -2,13 +2,7 @@ PACKAGES_NOSIMULATION=$(shell go list ./... | grep -v '/simulation') PACKAGES_SIMTEST=$(shell go list ./... | grep '/simulation') VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//') COMMIT := $(shell git log -1 --format='%H') -BUILD_TAGS = netgo CAT := $(if $(filter $(OS),Windows_NT),type,cat) -BUILD_FLAGS = -tags "$(BUILD_TAGS)" -ldflags \ - '-X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \ - -X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \ - -X github.com/cosmos/cosmos-sdk/version.VendorDirHash=$(shell $(CAT) vendor-deps) \ - -X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(BUILD_TAGS)"' LEDGER_ENABLED ?= true GOTOOLS = \ github.com/golang/dep/cmd/dep \ @@ -16,6 +10,53 @@ GOTOOLS = \ github.com/rakyll/statik GOBIN ?= $(GOPATH)/bin +# process build tags + +build_tags = netgo +ifeq ($(LEDGER_ENABLED),true) + ifeq ($(OS),Windows_NT) + GCCEXE = $(shell where gcc.exe 2> NUL) + ifeq ($(GCCEXE),) + $(error gcc.exe not installed for ledger support, please install or set LEDGER_ENABLED=false) + else + build_tags += ledger + endif + else + UNAME_S = $(shell uname -s) + ifeq ($(UNAME_S),OpenBSD) + $(warning OpenBSD detected, disabling ledger support (https://github.com/cosmos/cosmos-sdk/issues/1988)) + else + GCC = $(shell command -v gcc 2> /dev/null) + ifeq ($(GCC),) + $(error gcc not installed for ledger support, please install or set LEDGER_ENABLED=false) + else + build_tags += ledger + endif + endif + endif +endif + +ifeq ($(WITH_CLEVELDB),yes) + build_tags += gcc +endif +build_tags += $(BUILD_TAGS) +build_tags := $(strip $(build_tags)) + +# process linker flags + +ldflags = -X github.com/cosmos/cosmos-sdk/version.Version=$(VERSION) \ + -X github.com/cosmos/cosmos-sdk/version.Commit=$(COMMIT) \ + -X github.com/cosmos/cosmos-sdk/version.VendorDirHash=$(shell $(CAT) vendor-deps) \ + -X "github.com/cosmos/cosmos-sdk/version.BuildTags=$(build_tags)" + +ifeq ($(WITH_CLEVELDB),yes) + ldflags += -X github.com/cosmos/cosmos-sdk/types.DBBackend=cleveldb +endif +ldflags += $(LDFLAGS) +ldflags := $(strip $(ldflags)) + +BUILD_FLAGS := -tags "$(build_tags)" -ldflags '$(ldflags)' + all: devtools vendor-deps install test_lint test # The below include contains the tools target. @@ -29,29 +70,6 @@ ci: devtools vendor-deps install test_cover test_lint test ######################################## ### Build/Install -ifeq ($(LEDGER_ENABLED),true) - ifeq ($(OS),Windows_NT) - GCCEXE = $(shell where gcc.exe 2> NUL) - ifeq ($(GCCEXE),) - $(error gcc.exe not installed for ledger support, please install or set LEDGER_ENABLED=false) - else - BUILD_TAGS += ledger - endif - else - UNAME_S = $(shell uname -s) - ifeq ($(UNAME_S),OpenBSD) - $(warning OpenBSD detected, disabling ledger support (https://github.com/cosmos/cosmos-sdk/issues/1988)) - else - GCC = $(shell command -v gcc 2> /dev/null) - ifeq ($(GCC),) - $(error gcc not installed for ledger support, please install or set LEDGER_ENABLED=false) - else - BUILD_TAGS += ledger - endif - endif - endif -endif - build: ifeq ($(OS),Windows_NT) go build $(BUILD_FLAGS) -o build/gaiad.exe ./cmd/gaia/cmd/gaiad diff --git a/PENDING.md b/PENDING.md index 49b2eeb8b..daffd5ad4 100644 --- a/PENDING.md +++ b/PENDING.md @@ -40,6 +40,9 @@ tags. ### SDK +* [\#3719](https://github.com/cosmos/cosmos-sdk/issues/3719) DBBackend can now be set at compile time. + Defaults: goleveldb. Supported: cleveldb. + ### Tendermint diff --git a/cmd/gaia/app/sim_test.go b/cmd/gaia/app/sim_test.go index c6f1f046a..2cba35458 100644 --- a/cmd/gaia/app/sim_test.go +++ b/cmd/gaia/app/sim_test.go @@ -305,7 +305,7 @@ func BenchmarkFullGaiaSimulation(b *testing.B) { var db dbm.DB dir, _ := ioutil.TempDir("", "goleveldb-gaia-sim") - db, _ = dbm.NewGoLevelDB("Simulation", dir) + db, _ = sdk.NewLevelDB("Simulation", dir) defer func() { db.Close() os.RemoveAll(dir) @@ -347,7 +347,7 @@ func TestFullGaiaSimulation(t *testing.T) { } var db dbm.DB dir, _ := ioutil.TempDir("", "goleveldb-gaia-sim") - db, _ = dbm.NewGoLevelDB("Simulation", dir) + db, _ = sdk.NewLevelDB("Simulation", dir) defer func() { db.Close() os.RemoveAll(dir) @@ -388,7 +388,7 @@ func TestGaiaImportExport(t *testing.T) { } var db dbm.DB dir, _ := ioutil.TempDir("", "goleveldb-gaia-sim") - db, _ = dbm.NewGoLevelDB("Simulation", dir) + db, _ = sdk.NewLevelDB("Simulation", dir) defer func() { db.Close() os.RemoveAll(dir) @@ -421,7 +421,7 @@ func TestGaiaImportExport(t *testing.T) { fmt.Printf("Importing genesis...\n") newDir, _ := ioutil.TempDir("", "goleveldb-gaia-sim-2") - newDB, _ := dbm.NewGoLevelDB("Simulation-2", dir) + newDB, _ := sdk.NewLevelDB("Simulation-2", dir) defer func() { newDB.Close() os.RemoveAll(newDir) @@ -483,7 +483,7 @@ func TestGaiaSimulationAfterImport(t *testing.T) { logger = log.NewNopLogger() } dir, _ := ioutil.TempDir("", "goleveldb-gaia-sim") - db, _ := dbm.NewGoLevelDB("Simulation", dir) + db, _ := sdk.NewLevelDB("Simulation", dir) defer func() { db.Close() os.RemoveAll(dir) @@ -525,7 +525,7 @@ func TestGaiaSimulationAfterImport(t *testing.T) { fmt.Printf("Importing genesis...\n") newDir, _ := ioutil.TempDir("", "goleveldb-gaia-sim-2") - newDB, _ := dbm.NewGoLevelDB("Simulation-2", dir) + newDB, _ := sdk.NewLevelDB("Simulation-2", dir) defer func() { newDB.Close() os.RemoveAll(newDir) diff --git a/cmd/gaia/cmd/gaiadebug/hack.go b/cmd/gaia/cmd/gaiadebug/hack.go index e7a89cea7..51927592a 100644 --- a/cmd/gaia/cmd/gaiadebug/hack.go +++ b/cmd/gaia/cmd/gaiadebug/hack.go @@ -45,7 +45,7 @@ func runHackCmd(cmd *cobra.Command, args []string) error { // load the app logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)) - db, err := dbm.NewGoLevelDB("gaia", dataDir) + db, err := sdk.NewLevelDB("gaia", dataDir) if err != nil { fmt.Println(err) os.Exit(1) diff --git a/cmd/gaia/cmd/gaiareplay/main.go b/cmd/gaia/cmd/gaiareplay/main.go index ef6bcb139..78cacec36 100644 --- a/cmd/gaia/cmd/gaiareplay/main.go +++ b/cmd/gaia/cmd/gaiareplay/main.go @@ -15,7 +15,6 @@ import ( abci "github.com/tendermint/tendermint/abci/types" bcm "github.com/tendermint/tendermint/blockchain" cmn "github.com/tendermint/tendermint/libs/common" - dbm "github.com/tendermint/tendermint/libs/db" "github.com/tendermint/tendermint/proxy" tmsm "github.com/tendermint/tendermint/state" tm "github.com/tendermint/tendermint/types" @@ -23,6 +22,7 @@ import ( "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/cmd/gaia/app" "github.com/cosmos/cosmos-sdk/server" + sdk "github.com/cosmos/cosmos-sdk/types" ) var ( @@ -72,7 +72,7 @@ func run(rootDir string) { // App DB // appDB := dbm.NewMemDB() fmt.Println("Opening app database") - appDB, err := dbm.NewGoLevelDB("application", dataDir) + appDB, err := sdk.NewLevelDB("application", dataDir) if err != nil { panic(err) } @@ -80,14 +80,14 @@ func run(rootDir string) { // TM DB // tmDB := dbm.NewMemDB() fmt.Println("Opening tendermint state database") - tmDB, err := dbm.NewGoLevelDB("state", dataDir) + tmDB, err := sdk.NewLevelDB("state", dataDir) if err != nil { panic(err) } // Blockchain DB fmt.Println("Opening blockstore database") - bcDB, err := dbm.NewGoLevelDB("blockstore", dataDir) + bcDB, err := sdk.NewLevelDB("blockstore", dataDir) if err != nil { panic(err) } diff --git a/crypto/keys/lazy_keybase.go b/crypto/keys/lazy_keybase.go index 00c13a817..6f0b68523 100644 --- a/crypto/keys/lazy_keybase.go +++ b/crypto/keys/lazy_keybase.go @@ -5,10 +5,9 @@ import ( "github.com/tendermint/tendermint/crypto" cmn "github.com/tendermint/tendermint/libs/common" - dbm "github.com/tendermint/tendermint/libs/db" "github.com/cosmos/cosmos-sdk/crypto/keys/hd" - "github.com/cosmos/cosmos-sdk/types" + sdk "github.com/cosmos/cosmos-sdk/types" ) var _ Keybase = lazyKeybase{} @@ -28,7 +27,7 @@ func New(name, dir string) Keybase { } func (lkb lazyKeybase) List() ([]Info, error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, err } @@ -38,7 +37,7 @@ func (lkb lazyKeybase) List() ([]Info, error) { } func (lkb lazyKeybase) Get(name string) (Info, error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, err } @@ -47,8 +46,8 @@ func (lkb lazyKeybase) Get(name string) (Info, error) { return newDbKeybase(db).Get(name) } -func (lkb lazyKeybase) GetByAddress(address types.AccAddress) (Info, error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) +func (lkb lazyKeybase) GetByAddress(address sdk.AccAddress) (Info, error) { + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, err } @@ -58,7 +57,7 @@ func (lkb lazyKeybase) GetByAddress(address types.AccAddress) (Info, error) { } func (lkb lazyKeybase) Delete(name, passphrase string, skipPass bool) error { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return err } @@ -68,7 +67,7 @@ func (lkb lazyKeybase) Delete(name, passphrase string, skipPass bool) error { } func (lkb lazyKeybase) Sign(name, passphrase string, msg []byte) ([]byte, crypto.PubKey, error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, nil, err } @@ -78,7 +77,7 @@ func (lkb lazyKeybase) Sign(name, passphrase string, msg []byte) ([]byte, crypto } func (lkb lazyKeybase) CreateMnemonic(name string, language Language, passwd string, algo SigningAlgo) (info Info, seed string, err error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, "", err } @@ -88,7 +87,7 @@ func (lkb lazyKeybase) CreateMnemonic(name string, language Language, passwd str } func (lkb lazyKeybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd string, account uint32, index uint32) (Info, error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, err } @@ -98,7 +97,7 @@ func (lkb lazyKeybase) CreateAccount(name, mnemonic, bip39Passwd, encryptPasswd } func (lkb lazyKeybase) Derive(name, mnemonic, bip39Passwd, encryptPasswd string, params hd.BIP44Params) (Info, error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, err } @@ -108,7 +107,7 @@ func (lkb lazyKeybase) Derive(name, mnemonic, bip39Passwd, encryptPasswd string, } func (lkb lazyKeybase) CreateLedger(name string, algo SigningAlgo, account uint32, index uint32) (info Info, err error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, err } @@ -118,7 +117,7 @@ func (lkb lazyKeybase) CreateLedger(name string, algo SigningAlgo, account uint3 } func (lkb lazyKeybase) CreateOffline(name string, pubkey crypto.PubKey) (info Info, err error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, err } @@ -128,7 +127,7 @@ func (lkb lazyKeybase) CreateOffline(name string, pubkey crypto.PubKey) (info In } func (lkb lazyKeybase) CreateMulti(name string, pubkey crypto.PubKey) (info Info, err error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, err } @@ -138,7 +137,7 @@ func (lkb lazyKeybase) CreateMulti(name string, pubkey crypto.PubKey) (info Info } func (lkb lazyKeybase) Update(name, oldpass string, getNewpass func() (string, error)) error { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return err } @@ -148,7 +147,7 @@ func (lkb lazyKeybase) Update(name, oldpass string, getNewpass func() (string, e } func (lkb lazyKeybase) Import(name string, armor string) (err error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return err } @@ -158,7 +157,7 @@ func (lkb lazyKeybase) Import(name string, armor string) (err error) { } func (lkb lazyKeybase) ImportPubKey(name string, armor string) (err error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return err } @@ -168,7 +167,7 @@ func (lkb lazyKeybase) ImportPubKey(name string, armor string) (err error) { } func (lkb lazyKeybase) Export(name string) (armor string, err error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return "", err } @@ -178,7 +177,7 @@ func (lkb lazyKeybase) Export(name string) (armor string, err error) { } func (lkb lazyKeybase) ExportPubKey(name string) (armor string, err error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return "", err } @@ -188,7 +187,7 @@ func (lkb lazyKeybase) ExportPubKey(name string) (armor string, err error) { } func (lkb lazyKeybase) ExportPrivateKeyObject(name string, passphrase string) (crypto.PrivKey, error) { - db, err := dbm.NewGoLevelDB(lkb.name, lkb.dir) + db, err := sdk.NewLevelDB(lkb.name, lkb.dir) if err != nil { return nil, err } diff --git a/server/constructors.go b/server/constructors.go index 5a51937a9..ac087d657 100644 --- a/server/constructors.go +++ b/server/constructors.go @@ -6,6 +6,7 @@ import ( "os" "path/filepath" + sdk "github.com/cosmos/cosmos-sdk/types" abci "github.com/tendermint/tendermint/abci/types" dbm "github.com/tendermint/tendermint/libs/db" "github.com/tendermint/tendermint/libs/log" @@ -24,7 +25,7 @@ type ( func openDB(rootDir string) (dbm.DB, error) { dataDir := filepath.Join(rootDir, "data") - db, err := dbm.NewGoLevelDB("application", dataDir) + db, err := sdk.NewLevelDB("application", dataDir) return db, err } diff --git a/server/mock/app.go b/server/mock/app.go index e2f94c1f1..9063bb7c5 100644 --- a/server/mock/app.go +++ b/server/mock/app.go @@ -8,7 +8,6 @@ import ( "github.com/tendermint/tendermint/types" abci "github.com/tendermint/tendermint/abci/types" - dbm "github.com/tendermint/tendermint/libs/db" "github.com/tendermint/tendermint/libs/log" bam "github.com/cosmos/cosmos-sdk/baseapp" @@ -20,7 +19,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 := dbm.NewGoLevelDB("mock", filepath.Join(rootDir, "data")) + db, err := sdk.NewLevelDB("mock", filepath.Join(rootDir, "data")) if err != nil { return nil, err } diff --git a/types/utils.go b/types/utils.go index f5b244d74..09bce6b76 100644 --- a/types/utils.go +++ b/types/utils.go @@ -3,7 +3,15 @@ package types import ( "encoding/binary" "encoding/json" + "fmt" "time" + + dbm "github.com/tendermint/tendermint/libs/db" +) + +var ( + // This is set at compile time. Could be cleveldb, defaults is goleveldb. + DBBackend = "" ) // SortedJSON takes any JSON and returns it sorted by keys. Also, all white-spaces @@ -58,3 +66,17 @@ func ParseTimeBytes(bz []byte) (time.Time, error) { } return t.UTC().Round(0), nil } + +// NewLevelDB instantiate a new LevelDB instance according to DBBackend. +func NewLevelDB(name, dir string) (db dbm.DB, err error) { + backend := dbm.GoLevelDBBackend + if DBBackend == string(dbm.CLevelDBBackend) { + backend = dbm.CLevelDBBackend + } + defer func() { + if r := recover(); r != nil { + err = fmt.Errorf("couldn't create db: %v", r) + } + }() + return dbm.NewDB(name, backend, dir), err +}