Skip to content

Commit

Permalink
Merge pull request cosmos#726 from cosmos/adrian/mountmultipledbs
Browse files Browse the repository at this point in the history
Mount every single store with its own DB
  • Loading branch information
ebuchman authored Mar 30, 2018
2 parents 66a1130 + 55e7278 commit d25593a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 30 deletions.
7 changes: 7 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type BaseApp struct {
var _ abci.Application = (*BaseApp)(nil)

// Create and name new BaseApp
// NOTE: The db is used to store the version number for now.
func NewBaseApp(name string, logger log.Logger, db dbm.DB) *BaseApp {
return &BaseApp{
Logger: logger,
Expand All @@ -71,12 +72,18 @@ 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
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
func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
app.cms.MountStoreWithDB(key, typ, app.db)
Expand Down
23 changes: 13 additions & 10 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,15 @@ 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.MountStoresIAVL(capKey1, capKey2)
app.MountStoreWithDB(capKey1, sdk.StoreTypeIAVL, db1)
app.MountStoreWithDB(capKey2, sdk.StoreTypeIAVL, db2)

// stores are mounted
err := app.LoadLatestVersion(capKey1)
Expand Down Expand Up @@ -126,7 +129,6 @@ func TestTxDecoder(t *testing.T) {

// Test that Info returns the latest committed state.
func TestInfo(t *testing.T) {

app := newBaseApp(t.Name())

// ----- test an empty response -------
Expand All @@ -145,17 +147,19 @@ func TestInfo(t *testing.T) {
}

func TestInitChainer(t *testing.T) {
logger := defaultLogger()
db := dbm.NewMemDB()
name := t.Name()
db := dbm.NewMemDB()
logger := defaultLogger()
app := NewBaseApp(name, logger, db)

// make cap keys and mount the stores
// NOTE/TODO: mounting multiple stores is broken
// see https://github.com/cosmos/cosmos-sdk/issues/532
capKey := sdk.NewKVStoreKey("main")
// capKey2 := sdk.NewKVStoreKey("key2")
app.MountStoresIAVL(capKey) // , capKey2)
db1 := dbm.NewMemDB()
capKey2 := sdk.NewKVStoreKey("key2")
db2 := dbm.NewMemDB()
app.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db1)
app.MountStoreWithDB(capKey2, sdk.StoreTypeIAVL, db2)
err := app.LoadLatestVersion(capKey) // needed to make stores non-nil
assert.Nil(t, err)

Expand Down Expand Up @@ -187,9 +191,8 @@ func TestInitChainer(t *testing.T) {

// reload app
app = NewBaseApp(name, logger, db)
capKey = sdk.NewKVStoreKey("main")
// capKey2 = sdk.NewKVStoreKey("key2") // TODO
app.MountStoresIAVL(capKey) //, capKey2)
app.MountStoreWithDB(capKey, sdk.StoreTypeIAVL, db1)
app.MountStoreWithDB(capKey2, sdk.StoreTypeIAVL, db2)
err = app.LoadLatestVersion(capKey) // needed to make stores non-nil
assert.Nil(t, err)
app.SetInitChainer(initChainer)
Expand Down
8 changes: 7 additions & 1 deletion client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,13 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) {
logger = log.NewFilter(logger, log.AllowError())
privValidatorFile := config.PrivValidatorFile()
privVal := tmtypes.LoadOrGenPrivValidatorFS(privValidatorFile)
app := bapp.NewBasecoinApp(logger, dbm.NewMemDB())
dbs := map[string]dbm.DB{
"main": dbm.NewMemDB(),
"acc": dbm.NewMemDB(),
"ibc": dbm.NewMemDB(),
"staking": dbm.NewMemDB(),
}
app := bapp.NewBasecoinApp(logger, dbs)

genesisFile := config.GenesisFile()
genDoc, err := tmtypes.GenesisDocFromFile(genesisFile)
Expand Down
13 changes: 10 additions & 3 deletions examples/basecoin/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ type BasecoinApp struct {

// keys to access the substores
capKeyMainStore *sdk.KVStoreKey
capKeyAccountStore *sdk.KVStoreKey
capKeyIBCStore *sdk.KVStoreKey
capKeyStakingStore *sdk.KVStoreKey

// Manage getting and setting accounts
accountMapper sdk.AccountMapper
}

func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
func NewBasecoinApp(logger log.Logger, dbs map[string]dbm.DB) *BasecoinApp {
// create your application object
var app = &BasecoinApp{
BaseApp: bam.NewBaseApp(appName, logger, db),
BaseApp: bam.NewBaseApp(appName, logger, dbs["main"]),
cdc: MakeCodec(),
capKeyMainStore: sdk.NewKVStoreKey("main"),
capKeyAccountStore: sdk.NewKVStoreKey("acc"),
capKeyIBCStore: sdk.NewKVStoreKey("ibc"),
capKeyStakingStore: sdk.NewKVStoreKey("stake"),
}
Expand All @@ -71,7 +73,12 @@ func NewBasecoinApp(logger log.Logger, db dbm.DB) *BasecoinApp {
// initialize BaseApp
app.SetTxDecoder(app.txDecoder)
app.SetInitChainer(app.initChainer)
app.MountStoresIAVL(app.capKeyMainStore, app.capKeyIBCStore, app.capKeyStakingStore)
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.SetAnteHandler(auth.NewAnteHandler(app.accountMapper))
err := app.LoadLatestVersion(app.capKeyMainStore)
if err != nil {
Expand Down
34 changes: 21 additions & 13 deletions examples/basecoin/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,20 @@ var (
}
)

func newBasecoinApp() *BasecoinApp {
func loggerAndDBs() (log.Logger, map[string]dbm.DB) {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
db := dbm.NewMemDB()
return NewBasecoinApp(logger, db)
dbs := map[string]dbm.DB{
"main": dbm.NewMemDB(),
"acc": dbm.NewMemDB(),
"ibc": dbm.NewMemDB(),
"staking": dbm.NewMemDB(),
}
return logger, dbs
}

func newBasecoinApp() *BasecoinApp {
logger, dbs := loggerAndDBs()
return NewBasecoinApp(logger, dbs)
}

//_______________________________________________________________________
Expand Down Expand Up @@ -112,9 +122,8 @@ func TestMsgs(t *testing.T) {
}

func TestGenesis(t *testing.T) {
logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "sdk/app")
db := dbm.NewMemDB()
bapp := NewBasecoinApp(logger, db)
logger, dbs := loggerAndDBs()
bapp := NewBasecoinApp(logger, dbs)

// Construct some genesis bytes to reflect basecoin/types/AppAccount
pk := crypto.GenPrivKeyEd25519().PubKey()
Expand Down Expand Up @@ -145,13 +154,12 @@ func TestGenesis(t *testing.T) {
ctx := bapp.BaseApp.NewContext(true, abci.Header{})
res1 := bapp.accountMapper.GetAccount(ctx, baseAcc.Address)
assert.Equal(t, acc, res1)
/*
// reload app and ensure the account is still there
bapp = NewBasecoinApp(logger, db)
ctx = bapp.BaseApp.NewContext(true, abci.Header{})
res1 = bapp.accountMapper.GetAccount(ctx, baseAcc.Address)
assert.Equal(t, acc, res1)
*/

// reload app and ensure the account is still there
bapp = NewBasecoinApp(logger, dbs)
ctx = bapp.BaseApp.NewContext(true, abci.Header{})
res1 = bapp.accountMapper.GetAccount(ctx, baseAcc.Address)
assert.Equal(t, acc, res1)
}

func TestSendMsgWithAccounts(t *testing.T) {
Expand Down
22 changes: 20 additions & 2 deletions examples/basecoin/cmd/basecoind/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,29 @@ func defaultOptions(args []string) (json.RawMessage, error) {
}

func generateApp(rootDir string, logger log.Logger) (abci.Application, error) {
db, err := dbm.NewGoLevelDB("basecoin", filepath.Join(rootDir, "data"))
dbMain, err := dbm.NewGoLevelDB("basecoin", filepath.Join(rootDir, "data"))
if err != nil {
return nil, err
}
bapp := app.NewBasecoinApp(logger, db)
dbAcc, err := dbm.NewGoLevelDB("basecoin-acc", filepath.Join(rootDir, "data"))
if err != nil {
return nil, err
}
dbIBC, err := dbm.NewGoLevelDB("basecoin-ibc", filepath.Join(rootDir, "data"))
if err != nil {
return nil, err
}
dbStaking, err := dbm.NewGoLevelDB("basecoin-staking", filepath.Join(rootDir, "data"))
if err != nil {
return nil, err
}
dbs := map[string]dbm.DB{
"main": dbMain,
"acc": dbAcc,
"ibc": dbIBC,
"staking": dbStaking,
}
bapp := app.NewBasecoinApp(logger, dbs)
return bapp, nil
}

Expand Down
2 changes: 1 addition & 1 deletion examples/kvstore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
var baseApp = bam.NewBaseApp("kvstore", logger, db)

// Set mounts for BaseApp's MultiStore.
baseApp.MountStore(capKeyMainStore, sdk.StoreTypeIAVL)
baseApp.MountStoresIAVL(capKeyMainStore)

// Set Tx decoder
baseApp.SetTxDecoder(decodeTx)
Expand Down

0 comments on commit d25593a

Please sign in to comment.