Skip to content

Commit

Permalink
Substore->Store; BaseApp has db; Mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Jan 22, 2018
1 parent b4e4881 commit be665d5
Show file tree
Hide file tree
Showing 31 changed files with 1,013 additions and 567 deletions.
41 changes: 35 additions & 6 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"github.com/pkg/errors"
abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"

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

Expand All @@ -23,6 +25,9 @@ type BaseApp struct {
// Application name from abci.Info
name string

// Common DB backend
db dbm.DB

// Main (uncached) state
ms sdk.CommitMultiStore

Expand Down Expand Up @@ -53,19 +58,43 @@ type BaseApp struct {

var _ abci.Application = &BaseApp{}

func NewBaseApp(name string, ms sdk.CommitMultiStore) *BaseApp {
return &BaseApp{
func NewBaseApp(name string) *BaseApp {
var baseapp = &BaseApp{
logger: makeDefaultLogger(),
name: name,
ms: ms,
db: nil,
ms: nil,
router: NewRouter(),
}
baseapp.initDB()
baseapp.initMultiStore()
return baseapp
}

// Create the underlying leveldb datastore which will
// persist the Merkle tree inner & leaf nodes.
func (app *BaseApp) initDB() {
db, err := dbm.NewGoLevelDB(app.name, "data")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
app.db = db
}

func (app *BaseApp) initMultiStore() {
ms := store.NewCommitMultiStore(app.db)
app.ms = ms
}

func (app *BaseApp) Name() string {
return app.name
}

func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
app.ms.MountStoreWithDB(key, typ, app.db)
}

func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
app.txDecoder = txDecoder
}
Expand All @@ -84,12 +113,12 @@ func (app *BaseApp) SetEndBlocker(...) {}
func (app *BaseApp) SetInitStater(...) {}
*/

func (app *BaseApp) LoadLatestVersion(mainKey sdk.SubstoreKey) error {
func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error {
app.ms.LoadLatestVersion()
return app.initFromStore(mainKey)
}

func (app *BaseApp) LoadVersion(version int64, mainKey sdk.SubstoreKey) error {
func (app *BaseApp) LoadVersion(version int64, mainKey sdk.StoreKey) error {
app.ms.LoadVersion(version)
return app.initFromStore(mainKey)
}
Expand All @@ -105,7 +134,7 @@ func (app *BaseApp) LastBlockHeight() int64 {
}

// Initializes the remaining logic from app.ms.
func (app *BaseApp) initFromStore(mainKey sdk.SubstoreKey) error {
func (app *BaseApp) initFromStore(mainKey sdk.StoreKey) error {
lastCommitID := app.ms.LastCommitID()
main := app.ms.GetKVStore(mainKey)
header := abci.Header{}
Expand Down
16 changes: 7 additions & 9 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"

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

Expand All @@ -34,10 +33,10 @@ func (tx testTx) GetFeePayer() crypto.Address { return nil }
func (tx testTx) GetSignatures() []sdk.StdSignature { return nil }

func TestBasic(t *testing.T) {
store, storeKeys := newCommitMultiStore()

// Create app.
app := NewBaseApp(t.Name(), store)
app := NewBaseApp(t.Name())
storeKeys := createMounts(app.ms)
app.SetTxDecoder(func(txBytes []byte) (sdk.Tx, error) {
var ttx testTx
fromJSON(txBytes, &ttx)
Expand Down Expand Up @@ -161,16 +160,15 @@ func fromJSON(bz []byte, ptr interface{}) {
}
}

// Creates a sample CommitMultiStore
func newCommitMultiStore() (sdk.CommitMultiStore, map[string]sdk.SubstoreKey) {
// Mounts stores to CommitMultiStore and returns a map of keys.
func createMounts(ms sdk.CommitMultiStore) map[string]sdk.StoreKey {
dbMain := dbm.NewMemDB()
dbXtra := dbm.NewMemDB()
keyMain := sdk.NewKVStoreKey("main")
keyXtra := sdk.NewKVStoreKey("xtra")
ms := store.NewCommitMultiStore(dbMain) // Also store rootMultiStore metadata here (it shouldn't clash)
ms.SetSubstoreLoader(keyMain, store.NewIAVLStoreLoader(dbMain, 0, 0))
ms.SetSubstoreLoader(keyXtra, store.NewIAVLStoreLoader(dbXtra, 0, 0))
return ms, map[string]sdk.SubstoreKey{
ms.MountStoreWithDB(keyMain, sdk.StoreTypeIAVL, dbMain)
ms.MountStoreWithDB(keyXtra, sdk.StoreTypeIAVL, dbXtra)
return map[string]sdk.StoreKey{
"main": keyMain,
"xtra": keyXtra,
}
Expand Down
1 change: 1 addition & 0 deletions baseapp/data/TestBasic.db/CURRENT
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MANIFEST-000093
Empty file added baseapp/data/TestBasic.db/LOCK
Empty file.
Loading

0 comments on commit be665d5

Please sign in to comment.