wip refactoring basecoin
This commit is contained in:
parent
f446b94ac7
commit
44536faf08
@ -16,6 +16,7 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/store"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||
)
|
||||
|
||||
var mainHeaderKey = []byte("header")
|
||||
@ -34,10 +35,11 @@ type BaseApp struct {
|
||||
//--------------------
|
||||
// Volatile
|
||||
|
||||
msCheck sdk.CacheMultiStore // CheckTx state, a cache-wrap of `.cms`
|
||||
msDeliver sdk.CacheMultiStore // DeliverTx state, a cache-wrap of `.cms`
|
||||
header *abci.Header // current block header
|
||||
valUpdates []abci.Validator // cached validator changes from DeliverTx
|
||||
accountMapper sdk.AccountMapper // Manage getting and setting accounts
|
||||
msCheck sdk.CacheMultiStore // CheckTx state, a cache-wrap of `.cms`
|
||||
msDeliver sdk.CacheMultiStore // DeliverTx state, a cache-wrap of `.cms`
|
||||
header *abci.Header // current block header
|
||||
valUpdates []abci.Validator // cached validator changes from DeliverTx
|
||||
}
|
||||
|
||||
var _ abci.Application = &BaseApp{}
|
||||
@ -53,9 +55,44 @@ func NewBaseApp(name string) *BaseApp {
|
||||
}
|
||||
baseapp.initDB()
|
||||
baseapp.initMultiStore()
|
||||
|
||||
return baseapp
|
||||
}
|
||||
|
||||
// Create and name new BaseApp
|
||||
func NewBaseAppExpanded(name string, accountMapper sdk.AccountMapper, keys []*sdk.KVStoreKey) *BaseApp {
|
||||
var baseapp = &BaseApp{
|
||||
logger: makeDefaultLogger(),
|
||||
name: name,
|
||||
db: nil,
|
||||
cms: nil,
|
||||
defaultAnteHandler: auth.NewAnteHandler(app.AccountMapper()),
|
||||
router: NewRouter(),
|
||||
accountMapper: accountMapper,
|
||||
}
|
||||
baseapp.initDB()
|
||||
baseapp.initMultiStore()
|
||||
baseapp.initAccountMapper()
|
||||
|
||||
for _, key := range keys {
|
||||
baseApp.MountStore(key, sdk.StoreTypeIAVL)
|
||||
}
|
||||
|
||||
return baseapp
|
||||
}
|
||||
|
||||
// Initialize the AccountMapper.
|
||||
func (app *BaseApp) initAccountMapper() {
|
||||
|
||||
// Register all interfaces and concrete types that
|
||||
// implement those interfaces, here.
|
||||
cdc := accountMapper.WireCodec()
|
||||
auth.RegisterWireBaseAccount(cdc)
|
||||
|
||||
// Make accountMapper's WireCodec() inaccessible.
|
||||
app.accountMapper = accountMapper.Seal()
|
||||
}
|
||||
|
||||
// Create the underlying leveldb datastore which will
|
||||
// persist the Merkle tree inner & leaf nodes.
|
||||
func (app *BaseApp) initDB() {
|
||||
@ -82,7 +119,7 @@ func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
|
||||
app.cms.MountStoreWithDB(key, typ, app.db)
|
||||
}
|
||||
|
||||
// nolint
|
||||
// nolint - Set functions
|
||||
func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
|
||||
app.txDecoder = txDecoder
|
||||
}
|
||||
@ -90,11 +127,13 @@ func (app *BaseApp) SetInitStater(initStater sdk.InitStater) {
|
||||
app.InitStater = initStater
|
||||
}
|
||||
func (app *BaseApp) SetDefaultAnteHandler(ah sdk.AnteHandler) {
|
||||
// deducts fee from payer, verifies signatures and nonces, sets Signers to ctx.
|
||||
app.defaultAnteHandler = ah
|
||||
}
|
||||
func (app *BaseApp) Router() Router {
|
||||
return app.router
|
||||
}
|
||||
|
||||
// nolint - Get functions
|
||||
func (app *BaseApp) Router() Router { return app.router }
|
||||
func (app *BaseApp) AccountMapper() sdk.AccountMapper { return app.accountMapper }
|
||||
|
||||
/* TODO consider:
|
||||
func (app *BaseApp) SetBeginBlocker(...) {}
|
||||
|
||||
@ -12,6 +12,7 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/x/sketchy"
|
||||
|
||||
abci "github.com/tendermint/abci/types"
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
"github.com/tendermint/go-wire"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
)
|
||||
@ -21,42 +22,63 @@ const appName = "BasecoinApp"
|
||||
// Extended ABCI application
|
||||
type BasecoinApp struct {
|
||||
*bam.BaseApp
|
||||
router bam.Router
|
||||
cdc *wire.Codec
|
||||
cdc *wire.Codec
|
||||
|
||||
// keys to access the substores
|
||||
capKeyMainStore *sdk.KVStoreKey
|
||||
capKeyIBCStore *sdk.KVStoreKey
|
||||
|
||||
// object mappers
|
||||
accountMapper sdk.AccountMapper
|
||||
}
|
||||
|
||||
// construct top level keys
|
||||
func (app *BasecoinApp) initCapKeys() {
|
||||
app.capKeyMainStore = sdk.NewKVStoreKey("main")
|
||||
app.capKeyIBCStore = sdk.NewKVStoreKey("ibc")
|
||||
func NewBasecoinApp(genesisPath string) *BasecoinApp {
|
||||
|
||||
var app = &BasecoinApp{
|
||||
cdc: makeCodex(),
|
||||
capKeyMainStore: sdk.NewKVStoreKey("main"),
|
||||
capKeyIBCStore: sdk.NewKVStoreKey("ibc"),
|
||||
}
|
||||
|
||||
var accMapper = auth.NewAccountMapper(
|
||||
app.capKeyMainStore, // target store
|
||||
&types.AppAccount{}, // prototype
|
||||
)
|
||||
|
||||
app.BaseApp = bam.NewBaseAppExpanded(appName, accMapper)
|
||||
app.initBaseAppTxDecoder()
|
||||
app.initBaseAppInitStater(genesisPath)
|
||||
|
||||
// Add the handlers
|
||||
app.Router().AddRoute("bank", bank.NewHandler(bank.NewCoinKeeper(app.AccountMapper())))
|
||||
app.Router().AddRoute("sketchy", sketchy.NewHandler())
|
||||
|
||||
// load the stores
|
||||
if err := app.LoadLatestVersion(app.capKeyMainStore); err != nil {
|
||||
cmn.Exit(err.Error())
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
func (app *BasecoinApp) initDefaultAnteHandler() {
|
||||
// deducts fee from payer, verifies signatures and nonces, sets Signers to ctx.
|
||||
app.BaseApp.SetDefaultAnteHandler(auth.NewAnteHandler(app.accountMapper))
|
||||
}
|
||||
// Wire requires registration of interfaces & concrete types. All
|
||||
// interfaces to be encoded/decoded in a Msg must be registered
|
||||
// here, along with all the concrete types that implement them.
|
||||
func makeTxCodec() (cdc *wire.Codec) {
|
||||
cdc = wire.NewCodec()
|
||||
|
||||
func (app *BasecoinApp) initRouterHandlers() {
|
||||
// Register crypto.[PubKey,PrivKey,Signature] types.
|
||||
crypto.RegisterWire(cdc)
|
||||
|
||||
// All handlers must be added here, the order matters
|
||||
app.router.AddRoute("bank", bank.NewHandler(bank.NewCoinKeeper(app.accountMapper)))
|
||||
app.router.AddRoute("sketchy", sketchy.NewHandler())
|
||||
// Register bank.[SendMsg,IssueMsg] types.
|
||||
bank.RegisterWire(cdc)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (app *BasecoinApp) initBaseAppTxDecoder() {
|
||||
cdc := makeTxCodec()
|
||||
app.BaseApp.SetTxDecoder(func(txBytes []byte) (sdk.Tx, sdk.Error) {
|
||||
var tx = sdk.StdTx{}
|
||||
// StdTx.Msg is an interface whose concrete
|
||||
// types are registered in app/msgs.go.
|
||||
err := cdc.UnmarshalBinary(txBytes, &tx)
|
||||
err := app.cdc.UnmarshalBinary(txBytes, &tx)
|
||||
if err != nil {
|
||||
return nil, sdk.ErrTxParse("").TraceCause(err, "")
|
||||
}
|
||||
@ -65,8 +87,21 @@ func (app *BasecoinApp) initBaseAppTxDecoder() {
|
||||
}
|
||||
|
||||
// define the custom logic for basecoin initialization
|
||||
func (app *BasecoinApp) initBaseAppInitStater() {
|
||||
accountMapper := app.accountMapper
|
||||
func (app *BasecoinApp) initBaseAppInitStater(genesisPath string) {
|
||||
|
||||
genesisAppState, err := bam.ReadGenesisAppState(genesisPath)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("error loading genesis state: %v", err))
|
||||
}
|
||||
|
||||
// set up the cache store for ctx, get ctx
|
||||
// TODO: combine with InitChain and let tendermint invoke it.
|
||||
app.BaseApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{}})
|
||||
ctx := app.BaseApp.NewContext(false, nil) // context for DeliverTx
|
||||
err = app.BaseApp.InitStater(ctx, genesisAppState)
|
||||
if err != nil {
|
||||
cmn.Exit(fmt.Sprintf("error initializing application genesis state: %v", err))
|
||||
}
|
||||
|
||||
app.BaseApp.SetInitStater(func(ctx sdk.Context, state json.RawMessage) sdk.Error {
|
||||
if state == nil {
|
||||
@ -84,78 +119,8 @@ func (app *BasecoinApp) initBaseAppInitStater() {
|
||||
if err != nil {
|
||||
return sdk.ErrGenesisParse("").TraceCause(err, "")
|
||||
}
|
||||
accountMapper.SetAccount(ctx, acc)
|
||||
app.AccountMapper().SetAccount(ctx, acc)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Initialize root stores.
|
||||
func (app *BasecoinApp) mountStores() {
|
||||
|
||||
// Create MultiStore mounts.
|
||||
app.BaseApp.MountStore(app.capKeyMainStore, sdk.StoreTypeIAVL)
|
||||
app.BaseApp.MountStore(app.capKeyIBCStore, sdk.StoreTypeIAVL)
|
||||
}
|
||||
|
||||
// Initialize the AccountMapper.
|
||||
func (app *BasecoinApp) initAccountMapper() {
|
||||
|
||||
var accountMapper = auth.NewAccountMapper(
|
||||
app.capKeyMainStore, // target store
|
||||
&types.AppAccount{}, // prototype
|
||||
)
|
||||
|
||||
// Register all interfaces and concrete types that
|
||||
// implement those interfaces, here.
|
||||
cdc := accountMapper.WireCodec()
|
||||
auth.RegisterWireBaseAccount(cdc)
|
||||
|
||||
// Make accountMapper's WireCodec() inaccessible.
|
||||
app.accountMapper = accountMapper.Seal()
|
||||
}
|
||||
|
||||
func NewBasecoinApp(genesisPath string) *BasecoinApp {
|
||||
|
||||
// create and configure app
|
||||
var app = &BasecoinApp{}
|
||||
bapp := bam.NewBaseApp(appName)
|
||||
app.BaseApp = bapp
|
||||
app.router = bapp.Router()
|
||||
app.initBaseAppTxDecoder()
|
||||
|
||||
// add keys
|
||||
app.initCapKeys()
|
||||
|
||||
// initialize the stores
|
||||
app.mountStores()
|
||||
app.initAccountMapper()
|
||||
|
||||
// initialize the genesis function
|
||||
app.initBaseAppInitStater()
|
||||
|
||||
// initialize the handler
|
||||
app.initDefaultAnteHandler()
|
||||
app.initRouterHandlers()
|
||||
|
||||
genesisAppState, err := bam.ReadGenesisAppState(genesisPath)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("error loading genesis state: %v", err))
|
||||
}
|
||||
|
||||
// set up the cache store for ctx, get ctx
|
||||
// TODO: combine with InitChain and let tendermint invoke it.
|
||||
app.BaseApp.BeginBlock(abci.RequestBeginBlock{Header: abci.Header{}})
|
||||
ctx := app.BaseApp.NewContext(false, nil) // context for DeliverTx
|
||||
err = app.BaseApp.InitStater(ctx, genesisAppState)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("error initializing application genesis state: %v", err))
|
||||
}
|
||||
|
||||
// load the stores
|
||||
if err := app.LoadLatestVersion(app.capKeyMainStore); err != nil {
|
||||
cmn.Exit(err.Error())
|
||||
}
|
||||
|
||||
return app
|
||||
}
|
||||
|
||||
@ -1,22 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||
crypto "github.com/tendermint/go-crypto"
|
||||
wire "github.com/tendermint/go-wire"
|
||||
)
|
||||
|
||||
// Wire requires registration of interfaces & concrete types. All
|
||||
// interfaces to be encoded/decoded in a Msg must be registered
|
||||
// here, along with all the concrete types that implement them.
|
||||
func makeTxCodec() (cdc *wire.Codec) {
|
||||
cdc = wire.NewCodec()
|
||||
|
||||
// Register crypto.[PubKey,PrivKey,Signature] types.
|
||||
crypto.RegisterWire(cdc)
|
||||
|
||||
// Register bank.[SendMsg,IssueMsg] types.
|
||||
bank.RegisterWire(cdc)
|
||||
|
||||
return
|
||||
}
|
||||
@ -36,6 +36,19 @@ func NewAccountMapper(key sdk.StoreKey, proto sdk.Account) accountMapper {
|
||||
}
|
||||
}
|
||||
|
||||
// XXX add comment
|
||||
func NewAccountMapperSealed(key sdk.StoreKey, proto sdk.Account) accountMapper {
|
||||
cdc := wire.NewCodec()
|
||||
am := accountMapper{
|
||||
key: key,
|
||||
proto: proto,
|
||||
cdc: cdc,
|
||||
}
|
||||
RegisterWireBaseAccount(cdc)
|
||||
am.Seal()
|
||||
return am
|
||||
}
|
||||
|
||||
// Returns the go-wire codec. You may need to register interfaces
|
||||
// and concrete types here, if your app's sdk.Account
|
||||
// implementation includes interface fields.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user