diff --git a/CHANGELOG.md b/CHANGELOG.md index 54e994bdf..a366f96fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ BREAKING CHANGES * Remove go-wire, use go-amino * [store] Add `SubspaceIterator` and `ReverseSubspaceIterator` to `KVStore` interface +* [basecoin] NewBasecoinApp takes a `dbm.DB` and uses namespaced DBs for substores BUG FIXES diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 74b23bfb0..884528876 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -71,21 +71,20 @@ func (app *BaseApp) Name() string { } // Mount a store to the provided key in the BaseApp multistore -// Broken until #532 is implemented. func (app *BaseApp) MountStoresIAVL(keys ...*sdk.KVStoreKey) { for _, key := range keys { app.MountStore(key, sdk.StoreTypeIAVL) } } -// Mount a store to the provided key in the BaseApp multistore +// Mount a store to the provided key in the BaseApp multistore, using a specified DB func (app *BaseApp) MountStoreWithDB(key sdk.StoreKey, typ sdk.StoreType, db dbm.DB) { app.cms.MountStoreWithDB(key, typ, db) } -// Mount a store to the provided key in the BaseApp multistore +// Mount a store to the provided key in the BaseApp multistore, using the default DB func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) { - app.cms.MountStoreWithDB(key, typ, app.db) + app.cms.MountStoreWithDB(key, typ, nil) } // nolint - Set functions diff --git a/baseapp/baseapp_test.go b/baseapp/baseapp_test.go index 29b224c46..78fd50464 100644 --- a/baseapp/baseapp_test.go +++ b/baseapp/baseapp_test.go @@ -35,15 +35,12 @@ func TestMountStores(t *testing.T) { // make some cap keys capKey1 := sdk.NewKVStoreKey("key1") - db1 := dbm.NewMemDB() capKey2 := sdk.NewKVStoreKey("key2") - db2 := dbm.NewMemDB() // no stores are mounted assert.Panics(t, func() { app.LoadLatestVersion(capKey1) }) - app.MountStoreWithDB(capKey1, sdk.StoreTypeIAVL, db1) - app.MountStoreWithDB(capKey2, sdk.StoreTypeIAVL, db2) + app.MountStoresIAVL(capKey1, capKey2) // stores are mounted err := app.LoadLatestVersion(capKey1) @@ -155,11 +152,8 @@ func TestInitChainer(t *testing.T) { // NOTE/TODO: mounting multiple stores is broken // see https://github.com/cosmos/cosmos-sdk/issues/532 capKey := sdk.NewKVStoreKey("main") - db1 := dbm.NewMemDB() capKey2 := sdk.NewKVStoreKey("key2") - db2 := dbm.NewMemDB() - app.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db1) - app.MountStoreWithDB(capKey2, sdk.StoreTypeIAVL, db2) + app.MountStoresIAVL(capKey, capKey2) err := app.LoadLatestVersion(capKey) // needed to make stores non-nil assert.Nil(t, err) @@ -191,8 +185,7 @@ func TestInitChainer(t *testing.T) { // reload app app = NewBaseApp(name, logger, db) - app.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db1) - app.MountStoreWithDB(capKey2, sdk.StoreTypeIAVL, db2) + app.MountStoresIAVL(capKey, capKey2) err = app.LoadLatestVersion(capKey) // needed to make stores non-nil assert.Nil(t, err) app.SetInitChainer(initChainer) diff --git a/client/lcd/lcd_test.go b/client/lcd/lcd_test.go index fe38b18d8..0e7ce7b7b 100644 --- a/client/lcd/lcd_test.go +++ b/client/lcd/lcd_test.go @@ -333,13 +333,8 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) { // logger = log.NewFilter(logger, log.AllowError()) privValidatorFile := config.PrivValidatorFile() privVal := pvm.LoadOrGenFilePV(privValidatorFile) - dbs := map[string]dbm.DB{ - "main": dbm.NewMemDB(), - "acc": dbm.NewMemDB(), - "ibc": dbm.NewMemDB(), - "staking": dbm.NewMemDB(), - } - app := bapp.NewBasecoinApp(logger, dbs) + db := dbm.NewMemDB() + app := bapp.NewBasecoinApp(logger, db) cdc = bapp.MakeCodec() // XXX genesisFile := config.GenesisFile() diff --git a/cmd/gaiad/main.go b/cmd/gaiad/main.go index e44bc73ea..9ebf196db 100644 --- a/cmd/gaiad/main.go +++ b/cmd/gaiad/main.go @@ -28,29 +28,11 @@ var ( // TODO: distinguish from basecoin func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { dataDir := filepath.Join(rootDir, "data") - dbMain, err := dbm.NewGoLevelDB("gaia", dataDir) + db, err := dbm.NewGoLevelDB("gaia", dataDir) if err != nil { return nil, err } - dbAcc, err := dbm.NewGoLevelDB("gaia-acc", dataDir) - if err != nil { - return nil, err - } - dbIBC, err := dbm.NewGoLevelDB("gaia-ibc", dataDir) - if err != nil { - return nil, err - } - dbStaking, err := dbm.NewGoLevelDB("gaia-staking", dataDir) - if err != nil { - return nil, err - } - dbs := map[string]dbm.DB{ - "main": dbMain, - "acc": dbAcc, - "ibc": dbIBC, - "staking": dbStaking, - } - bapp := app.NewBasecoinApp(logger, dbs) + bapp := app.NewBasecoinApp(logger, db) return bapp, nil } diff --git a/examples/basecoin/app/app.go b/examples/basecoin/app/app.go index 15a4f1f19..a1b94318a 100644 --- a/examples/basecoin/app/app.go +++ b/examples/basecoin/app/app.go @@ -38,14 +38,14 @@ type BasecoinApp struct { accountMapper sdk.AccountMapper } -func NewBasecoinApp(logger log.Logger, dbs map[string]dbm.DB) *BasecoinApp { +func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp { // Create app-level codec for txs and accounts. var cdc = MakeCodec() // Create your application object. var app = &BasecoinApp{ - BaseApp: bam.NewBaseApp(appName, logger, dbs["main"]), + BaseApp: bam.NewBaseApp(appName, logger, db), cdc: cdc, capKeyMainStore: sdk.NewKVStoreKey("main"), capKeyAccountStore: sdk.NewKVStoreKey("acc"), @@ -72,12 +72,7 @@ func NewBasecoinApp(logger log.Logger, dbs map[string]dbm.DB) *BasecoinApp { // Initialize BaseApp. app.SetTxDecoder(app.txDecoder) app.SetInitChainer(app.initChainer) - app.MountStoreWithDB(app.capKeyMainStore, sdk.StoreTypeIAVL, dbs["main"]) - app.MountStoreWithDB(app.capKeyAccountStore, sdk.StoreTypeIAVL, dbs["acc"]) - app.MountStoreWithDB(app.capKeyIBCStore, sdk.StoreTypeIAVL, dbs["ibc"]) - app.MountStoreWithDB(app.capKeyStakingStore, sdk.StoreTypeIAVL, dbs["staking"]) - // NOTE: Broken until #532 lands - //app.MountStoresIAVL(app.capKeyMainStore, app.capKeyIBCStore, app.capKeyStakingStore) + app.MountStoresIAVL(app.capKeyMainStore, app.capKeyAccountStore, app.capKeyIBCStore, app.capKeyStakingStore) app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper)) err := app.LoadLatestVersion(app.capKeyMainStore) if err != nil { diff --git a/examples/basecoin/app/app_test.go b/examples/basecoin/app/app_test.go index 41c1a6f17..ead49adc2 100644 --- a/examples/basecoin/app/app_test.go +++ b/examples/basecoin/app/app_test.go @@ -85,20 +85,15 @@ var ( } ) -func loggerAndDBs() (log.Logger, map[string]dbm.DB) { +func loggerAndDB() (log.Logger, dbm.DB) { logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app") - dbs := map[string]dbm.DB{ - "main": dbm.NewMemDB(), - "acc": dbm.NewMemDB(), - "ibc": dbm.NewMemDB(), - "staking": dbm.NewMemDB(), - } - return logger, dbs + db := dbm.NewMemDB() + return logger, db } func newBasecoinApp() *BasecoinApp { - logger, dbs := loggerAndDBs() - return NewBasecoinApp(logger, dbs) + logger, db := loggerAndDB() + return NewBasecoinApp(logger, db) } func setGenesisAccounts(bapp *BasecoinApp, accs ...auth.BaseAccount) error { @@ -142,8 +137,8 @@ func TestMsgs(t *testing.T) { } func TestSortGenesis(t *testing.T) { - logger, dbs := loggerAndDBs() - bapp := NewBasecoinApp(logger, dbs) + logger, db := loggerAndDB() + bapp := NewBasecoinApp(logger, db) // Note the order: the coins are unsorted! coinDenom1, coinDenom2 := "foocoin", "barcoin" @@ -184,8 +179,8 @@ func TestSortGenesis(t *testing.T) { } func TestGenesis(t *testing.T) { - logger, dbs := loggerAndDBs() - bapp := NewBasecoinApp(logger, dbs) + logger, db := loggerAndDB() + bapp := NewBasecoinApp(logger, db) // Construct some genesis bytes to reflect basecoin/types/AppAccount pk := crypto.GenPrivKeyEd25519().PubKey() @@ -207,7 +202,7 @@ func TestGenesis(t *testing.T) { assert.Equal(t, acc, res1) // reload app and ensure the account is still there - bapp = NewBasecoinApp(logger, dbs) + bapp = NewBasecoinApp(logger, db) ctx = bapp.BaseApp.NewContext(true, abci.Header{}) res1 = bapp.accountMapper.GetAccount(ctx, baseAcc.Address) assert.Equal(t, acc, res1) diff --git a/examples/basecoin/cmd/basecoind/main.go b/examples/basecoin/cmd/basecoind/main.go index 34e45bf31..37eb7d58f 100644 --- a/examples/basecoin/cmd/basecoind/main.go +++ b/examples/basecoin/cmd/basecoind/main.go @@ -27,29 +27,11 @@ var ( func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { dataDir := filepath.Join(rootDir, "data") - dbMain, err := dbm.NewGoLevelDB("basecoin", dataDir) + db, err := dbm.NewGoLevelDB("basecoin", dataDir) if err != nil { return nil, err } - dbAcc, err := dbm.NewGoLevelDB("basecoin-acc", dataDir) - if err != nil { - return nil, err - } - dbIBC, err := dbm.NewGoLevelDB("basecoin-ibc", dataDir) - if err != nil { - return nil, err - } - dbStaking, err := dbm.NewGoLevelDB("basecoin-staking", dataDir) - if err != nil { - return nil, err - } - dbs := map[string]dbm.DB{ - "main": dbMain, - "acc": dbAcc, - "ibc": dbIBC, - "staking": dbStaking, - } - bapp := app.NewBasecoinApp(logger, dbs) + bapp := app.NewBasecoinApp(logger, db) return bapp, nil } diff --git a/examples/democoin/app/app.go b/examples/democoin/app/app.go index ad6fd7865..db2614bad 100644 --- a/examples/democoin/app/app.go +++ b/examples/democoin/app/app.go @@ -42,14 +42,14 @@ type DemocoinApp struct { accountMapper sdk.AccountMapper } -func NewDemocoinApp(logger log.Logger, dbs map[string]dbm.DB) *DemocoinApp { +func NewDemocoinApp(logger log.Logger, db dbm.DB) *DemocoinApp { // Create app-level codec for txs and accounts. var cdc = MakeCodec() // Create your application object. var app = &DemocoinApp{ - BaseApp: bam.NewBaseApp(appName, logger, dbs["main"]), + BaseApp: bam.NewBaseApp(appName, logger, db), cdc: cdc, capKeyMainStore: sdk.NewKVStoreKey("main"), capKeyAccountStore: sdk.NewKVStoreKey("acc"), @@ -82,13 +82,7 @@ func NewDemocoinApp(logger log.Logger, dbs map[string]dbm.DB) *DemocoinApp { // Initialize BaseApp. app.SetTxDecoder(app.txDecoder) app.SetInitChainer(app.initChainerFn(coolKeeper, powKeeper)) - app.MountStoreWithDB(app.capKeyMainStore, sdk.StoreTypeIAVL, dbs["main"]) - app.MountStoreWithDB(app.capKeyAccountStore, sdk.StoreTypeIAVL, dbs["acc"]) - app.MountStoreWithDB(app.capKeyPowStore, sdk.StoreTypeIAVL, dbs["pow"]) - app.MountStoreWithDB(app.capKeyIBCStore, sdk.StoreTypeIAVL, dbs["ibc"]) - app.MountStoreWithDB(app.capKeyStakingStore, sdk.StoreTypeIAVL, dbs["staking"]) - // NOTE: Broken until #532 lands - //app.MountStoresIAVL(app.capKeyMainStore, app.capKeyIBCStore, app.capKeyStakingStore) + app.MountStoresIAVL(app.capKeyMainStore, app.capKeyAccountStore, app.capKeyPowStore, app.capKeyIBCStore, app.capKeyStakingStore) app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper)) err := app.LoadLatestVersion(app.capKeyMainStore) if err != nil { diff --git a/examples/democoin/app/app_test.go b/examples/democoin/app/app_test.go index 7047f5d28..ca4e2509e 100644 --- a/examples/democoin/app/app_test.go +++ b/examples/democoin/app/app_test.go @@ -67,21 +67,15 @@ var ( } ) -func loggerAndDBs() (log.Logger, map[string]dbm.DB) { +func loggerAndDB() (log.Logger, dbm.DB) { logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app") - dbs := map[string]dbm.DB{ - "main": dbm.NewMemDB(), - "acc": dbm.NewMemDB(), - "pow": dbm.NewMemDB(), - "ibc": dbm.NewMemDB(), - "staking": dbm.NewMemDB(), - } - return logger, dbs + db := dbm.NewMemDB() + return logger, db } func newDemocoinApp() *DemocoinApp { - logger, dbs := loggerAndDBs() - return NewDemocoinApp(logger, dbs) + logger, db := loggerAndDB() + return NewDemocoinApp(logger, db) } //_______________________________________________________________________ @@ -123,8 +117,8 @@ func TestMsgs(t *testing.T) { } func TestGenesis(t *testing.T) { - logger, dbs := loggerAndDBs() - bapp := NewDemocoinApp(logger, dbs) + logger, db := loggerAndDB() + bapp := NewDemocoinApp(logger, db) // Construct some genesis bytes to reflect democoin/types/AppAccount pk := crypto.GenPrivKeyEd25519().PubKey() @@ -157,7 +151,7 @@ func TestGenesis(t *testing.T) { assert.Equal(t, acc, res1) // reload app and ensure the account is still there - bapp = NewDemocoinApp(logger, dbs) + bapp = NewDemocoinApp(logger, db) ctx = bapp.BaseApp.NewContext(true, abci.Header{}) res1 = bapp.accountMapper.GetAccount(ctx, baseAcc.Address) assert.Equal(t, acc, res1) diff --git a/examples/democoin/cmd/democoind/main.go b/examples/democoin/cmd/democoind/main.go index df94e2c32..8f8bb5a90 100644 --- a/examples/democoin/cmd/democoind/main.go +++ b/examples/democoin/cmd/democoind/main.go @@ -47,34 +47,11 @@ func defaultAppState(args []string, addr sdk.Address, coinDenom string) (json.Ra } func generateApp(rootDir string, logger log.Logger) (abci.Application, error) { - dbMain, err := dbm.NewGoLevelDB("democoin", filepath.Join(rootDir, "data")) + db, err := dbm.NewGoLevelDB("democoin", filepath.Join(rootDir, "data")) if err != nil { return nil, err } - dbAcc, err := dbm.NewGoLevelDB("democoin-acc", filepath.Join(rootDir, "data")) - if err != nil { - return nil, err - } - dbPow, err := dbm.NewGoLevelDB("democoin-pow", filepath.Join(rootDir, "data")) - if err != nil { - return nil, err - } - dbIBC, err := dbm.NewGoLevelDB("democoin-ibc", filepath.Join(rootDir, "data")) - if err != nil { - return nil, err - } - dbStaking, err := dbm.NewGoLevelDB("democoin-staking", filepath.Join(rootDir, "data")) - if err != nil { - return nil, err - } - dbs := map[string]dbm.DB{ - "main": dbMain, - "acc": dbAcc, - "pow": dbPow, - "ibc": dbIBC, - "staking": dbStaking, - } - bapp := app.NewDemocoinApp(logger, dbs) + bapp := app.NewDemocoinApp(logger, db) return bapp, nil }