BasecoinApp init refactor

This commit is contained in:
Jae Kwon
2018-01-20 19:06:29 -08:00
parent 05f0c96089
commit bd8bbf9d98
8 changed files with 103 additions and 80 deletions
+5 -31
View File
@@ -31,18 +31,15 @@ func NewBasecoinApp() *BasecoinApp {
// Create and configure app.
var app = &BasecoinApp{}
app.initKeys()
app.initMultiStore()
app.initAppStore()
app.initSDKApp()
app.initCodec()
app.initTxDecoder()
app.initAnteHandler()
app.initRoutes()
app.initCapKeys() // ./capkeys.go
app.initStores() // ./stores.go
app.initSDKApp() // ./sdkapp.go
app.initRoutes() // ./routes.go
// TODO: Load genesis
// TODO: InitChain with validators
// TODO: Set the genesis accounts
app.loadStores()
return app
@@ -66,29 +63,6 @@ func (app *BasecoinApp) RunForever() {
}
func (app *BasecoinApp) initKeys() {
app.mainStoreKey = sdk.NewKVStoreKey("main")
app.ibcStoreKey = sdk.NewKVStoreKey("ibc")
}
// depends on initMultiStore()
func (app *BasecoinApp) initSDKApp() {
app.App = apm.NewApp(appName, app.multiStore)
}
func (app *BasecoinApp) initCodec() {
app.cdc = wire.NewCodec()
app.registerMsgs()
}
// depends on initSDKApp()
func (app *BasecoinApp) initTxDecoder() {
app.App.SetTxDecoder(app.decodeTx)
}
// initAnteHandler defined in app/routes.go
// initRoutes defined in app/routes.go
// Load the stores.
func (app *BasecoinApp) loadStores() {
if err := app.LoadLatestVersion(app.mainStoreKey); err != nil {
+16
View File
@@ -0,0 +1,16 @@
package app
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// initCapKeys() happens before initStores(), initSDKApp(), and 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")
}
+10 -20
View File
@@ -1,32 +1,22 @@
package app
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank"
crypto "github.com/tendermint/go-crypto"
wire "github.com/tendermint/go-wire"
)
// Set via `app.App.SetTxDecoder(app.decodeTx)`
func (app *BasecoinApp) decodeTx(txBytes []byte) (sdk.Tx, error) {
var tx = sdk.StdTx{}
err := app.cdc.UnmarshalBinary(txBytes, &tx)
return tx, err
}
// 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()
// Wire requires registration of interfaces & concrete types.
func (app *BasecoinApp) registerMsgs() {
cdc := app.cdc
// Register the crypto
// Register crypto.[PubKey,PrivKey,Signature] types.
crypto.RegisterWire(cdc)
// Register the Msg interface.
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
cdc.RegisterConcrete(bank.SendMsg{}, "cosmos-sdk/SendMsg", nil) // XXX refactor out
cdc.RegisterConcrete(bank.IssueMsg{}, "cosmos-sdk/IssueMsg", nil) // XXX refactor out to bank/msgs.go
// more msgs here...
// Register bank.[SendMsg,IssueMsg] types.
bank.RegisterWire(cdc)
// All interfaces to be encoded/decoded in a Msg must be
// registered here, along with all the concrete types that
// implement them.
return
}
+3 -10
View File
@@ -1,22 +1,15 @@
package app
import (
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
)
// Handle charging tx fees and checking signatures.
func (app *BasecoinApp) initAnteHandler() {
var authAnteHandler = auth.NewAnteHandler(app.accStore)
app.App.SetDefaultAnteHandler(authAnteHandler)
}
// Constructs router to route handling of msgs.
// initRoutes() happens after initCapKeys(), initStores(), and initSDKApp().
func (app *BasecoinApp) initRoutes() {
var router = app.App.Router()
// var multiStore = app.multiStore
var accStore = app.accStore
// All handlers must be added here.
// The order matters.
router.AddRoute("bank", bank.NewHandler(accStore))
// more routes here... (order matters)
}
+29
View File
@@ -0,0 +1,29 @@
package app
import (
apm "github.com/cosmos/cosmos-sdk/app"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)
// initSDKApp() happens after initCapKeys() and initStores().
// initSDKApp() happens before initRoutes().
func (app *BasecoinApp) initSDKApp() {
app.App = apm.NewApp(appName, app.multiStore)
app.initSDKAppTxDecoder()
app.initSDKAppAnteHandler()
}
func (app *BasecoinApp) initSDKAppTxDecoder() {
cdc := makeTxCodec()
app.App.SetTxDecoder(func(txBytes []byte) (sdk.Tx, error) {
var tx = sdk.StdTx{}
err := cdc.UnmarshalBinary(txBytes, &tx)
return tx, err
})
}
func (app *BasecoinApp) initSDKAppAnteHandler() {
var authAnteHandler = auth.NewAnteHandler(app.accStore)
app.App.SetDefaultAnteHandler(authAnteHandler)
}
+16 -8
View File
@@ -10,7 +10,13 @@ import (
dbm "github.com/tendermint/tmlibs/db"
)
// depends on initKeys()
// initStores() happens after initCapKeys(), but before initSDKApp() and initRoutes().
func (app *BasecoinApp) initStores() {
app.initMultiStore()
app.initAccountStore()
}
// Initialize root MultiStore.
func (app *BasecoinApp) initMultiStore() {
// Create the underlying leveldb datastore which will
@@ -36,16 +42,18 @@ func (app *BasecoinApp) initMultiStore() {
app.multiStore = multiStore
}
// depends on initKeys()
func (app *BasecoinApp) initAppStore() {
// Initialize the AccountStore, which accesses the MultiStore.
func (app *BasecoinApp) initAccountStore() {
accStore := auth.NewAccountStore(
app.mainStoreKey, // where accounts are persisted.
&types.AppAccount{}, // prototype sdk.Account.
// where accounts are persisted in the MultiStore.
app.mainStoreKey,
// prototype sdk.Account.
&types.AppAccount{},
)
// 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.
// 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()
// cdc.RegisterInterface(...)