Substore->Store; BaseApp has db; Mapper

This commit is contained in:
Jae Kwon
2018-01-22 06:20:46 -08:00
parent b4e4881261
commit be665d53fe
31 changed files with 1012 additions and 566 deletions
+6 -6
View File
@@ -19,11 +19,11 @@ type BasecoinApp struct {
multiStore sdk.CommitMultiStore
// The key to access the substores.
mainStoreKey *sdk.KVStoreKey
ibcStoreKey *sdk.KVStoreKey
capKeyMainStore *sdk.KVStoreKey
capKeyIBCStore *sdk.KVStoreKey
// Additional stores:
accStore sdk.AccountStore
// Object mappers :
accountMapper sdk.AccountMapper
}
// TODO: This should take in more configuration options.
@@ -32,8 +32,8 @@ func NewBasecoinApp() *BasecoinApp {
// Create and configure app.
var app = &BasecoinApp{}
app.initCapKeys() // ./init_capkeys.go
app.initStores() // ./init_stores.go
app.initBaseApp() // ./init_baseapp.go
app.initStores() // ./init_stores.go
app.initRoutes() // ./init_routes.go
// TODO: Load genesis
@@ -65,7 +65,7 @@ func (app *BasecoinApp) RunForever() {
// Load the stores.
func (app *BasecoinApp) loadStores() {
if err := app.LoadLatestVersion(app.mainStoreKey); err != nil {
if err := app.LoadLatestVersion(app.capKeyMainStore); err != nil {
fmt.Println(err)
os.Exit(1)
}
+3 -4
View File
@@ -6,10 +6,9 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth"
)
// initBaseApp() happens after initCapKeys() and initStores().
// initBaseApp() happens before initRoutes().
// initCapKeys, initBaseApp, initStores, initRoutes.
func (app *BasecoinApp) initBaseApp() {
app.BaseApp = baseapp.NewBaseApp(appName, app.multiStore)
app.BaseApp = baseapp.NewBaseApp(appName)
app.initBaseAppTxDecoder()
app.initBaseAppAnteHandler()
}
@@ -24,6 +23,6 @@ func (app *BasecoinApp) initBaseAppTxDecoder() {
}
func (app *BasecoinApp) initBaseAppAnteHandler() {
var authAnteHandler = auth.NewAnteHandler(app.accStore)
var authAnteHandler = auth.NewAnteHandler(app.accountMapper)
app.BaseApp.SetDefaultAnteHandler(authAnteHandler)
}
+3 -3
View File
@@ -4,13 +4,13 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// initCapKeys() happens before initStores(), initSDKApp(), and initRoutes().
// initCapKeys, initBaseApp, initStores, initRoutes.
func (app *BasecoinApp) initCapKeys() {
// All top-level capabilities keys
// should be constructed here.
// For more information, see http://www.erights.org/elib/capability/ode/ode.pdf.
app.mainStoreKey = sdk.NewKVStoreKey("main")
app.ibcStoreKey = sdk.NewKVStoreKey("ibc")
app.capKeyMainStore = sdk.NewKVStoreKey("main")
app.capKeyIBCStore = sdk.NewKVStoreKey("ibc")
}
+3 -3
View File
@@ -4,12 +4,12 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank"
)
// initRoutes() happens after initCapKeys(), initStores(), and initSDKApp().
// initCapKeys, initBaseApp, initStores, initRoutes.
func (app *BasecoinApp) initRoutes() {
var router = app.BaseApp.Router()
var accStore = app.accStore
var accountMapper = app.accountMapper
// All handlers must be added here.
// The order matters.
router.AddRoute("bank", bank.NewHandler(accStore))
router.AddRoute("bank", bank.NewHandler(accountMapper))
}
+20 -43
View File
@@ -1,61 +1,38 @@
package app
import (
"fmt"
"os"
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
dbm "github.com/tendermint/tmlibs/db"
)
// initStores() happens after initCapKeys(), but before initSDKApp() and initRoutes().
// initCapKeys, initBaseApp, initStores, initRoutes.
func (app *BasecoinApp) initStores() {
app.initMultiStore()
app.initAccountStore()
app.mountStores()
app.initAccountMapper()
}
// Initialize root MultiStore.
func (app *BasecoinApp) initMultiStore() {
// Initialize root stores.
func (app *BasecoinApp) mountStores() {
// Create the underlying leveldb datastore which will
// persist the Merkle tree inner & leaf nodes.
db, err := dbm.NewGoLevelDB("basecoin", "basecoin-data")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Create CommitStoreLoader.
cacheSize := 10000
numHistory := int64(100)
mainLoader := store.NewIAVLStoreLoader(db, cacheSize, numHistory)
ibcLoader := store.NewIAVLStoreLoader(db, cacheSize, numHistory)
// Create MultiStore
multiStore := store.NewCommitMultiStore(db)
multiStore.SetSubstoreLoader(app.mainStoreKey, mainLoader)
multiStore.SetSubstoreLoader(app.ibcStoreKey, ibcLoader)
// Finally,
app.multiStore = multiStore
// Create MultiStore mounts.
app.BaseApp.MountStore(app.capKeyMainStore, sdk.StoreTypeIAVL)
app.BaseApp.MountStore(app.capKeyIBCStore, sdk.StoreTypeIAVL)
}
// Initialize the AccountStore, which accesses the MultiStore.
func (app *BasecoinApp) initAccountStore() {
accStore := auth.NewAccountStore(
// where accounts are persisted in the MultiStore.
app.mainStoreKey,
// prototype sdk.Account.
&types.AppAccount{},
// Initialize the AccountMapper.
func (app *BasecoinApp) initAccountMapper() {
var accountMapper = auth.NewAccountMapper(
app.capKeyMainStore, // target store
&types.AppAccount{}, // prototype
)
// If there are additional interfaces & concrete types that
// need to be registered w/ wire.Codec, they can be registered
// here before the accStore is sealed.
cdc := accStore.WireCodec()
// Register all interfaces and concrete types that
// implement those interfaces, here.
cdc := accountMapper.WireCodec()
auth.RegisterWireBaseAccount(cdc)
app.accStore = accStore.Seal()
// Make accountMapper's WireCodec() inaccessible.
app.accountMapper = accountMapper.Seal()
}
+11 -23
View File
@@ -8,42 +8,30 @@ import (
"github.com/tendermint/abci/server"
crypto "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func main() {
db, err := dbm.NewGoLevelDB("dummy", "dummy-data")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// Capabilities key to access the main KVStore.
var capKeyMainStore = sdk.NewKVStoreKey("main")
// create CommitStoreLoader
cacheSize := 10000
numHistory := int64(100)
loader := store.NewIAVLStoreLoader(db, cacheSize, numHistory)
// Create BaseApp.
var baseApp = bam.NewBaseApp("dummy")
// key to access the main KVStore
var mainStoreKey = sdk.NewKVStoreKey("main")
// Create MultiStore
multiStore := store.NewCommitMultiStore(db)
multiStore.SetSubstoreLoader(mainStoreKey, loader)
// Set everything on the baseApp and load latest
baseApp := bam.NewBaseApp("dummy", multiStore)
// Set mounts for BaseApp's MultiStore.
baseApp.MountStore(capKeyMainStore, sdk.StoreTypeIAVL)
// Set Tx decoder
baseApp.SetTxDecoder(decodeTx)
baseApp.Router().AddRoute("dummy", DummyHandler(mainStoreKey))
// Set a handler Route.
baseApp.Router().AddRoute("dummy", DummyHandler(capKeyMainStore))
if err := baseApp.LoadLatestVersion(mainStoreKey); err != nil {
// Load latest version.
if err := baseApp.LoadLatestVersion(capKeyMainStore); err != nil {
fmt.Println(err)
os.Exit(1)
}
@@ -126,7 +114,7 @@ func decodeTx(txBytes []byte) (sdk.Tx, error) {
return tx, nil
}
func DummyHandler(storeKey sdk.SubstoreKey) sdk.Handler {
func DummyHandler(storeKey sdk.StoreKey) sdk.Handler {
return func(ctx sdk.Context, tx sdk.Tx) sdk.Result {
// tx is already unmarshalled
key := tx.Get("key").([]byte)