wip refactoring basecoin
This commit is contained in:
committed by
Ethan Buchman
parent
f446b94ac7
commit
44536faf08
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user