cosmos-sdk/examples/basecoin/app/app.go

159 lines
4.9 KiB
Go
Raw Normal View History

package app
import (
2018-02-14 08:16:06 -08:00
"encoding/json"
abci "github.com/tendermint/abci/types"
2018-03-02 01:24:07 -08:00
oldwire "github.com/tendermint/go-wire"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
bam "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
2018-03-02 01:24:07 -08:00
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
2018-03-15 07:07:01 -07:00
"github.com/cosmos/cosmos-sdk/x/ibc"
2018-03-28 21:40:06 -07:00
"github.com/cosmos/cosmos-sdk/x/simplestake"
"github.com/cosmos/cosmos-sdk/examples/basecoin/types"
)
const (
appName = "BasecoinApp"
)
2018-02-13 05:36:08 -08:00
// Extended ABCI application
type BasecoinApp struct {
*bam.BaseApp
2018-02-14 11:14:22 -08:00
cdc *wire.Codec
2018-02-13 05:36:08 -08:00
// keys to access the substores
capKeyMainStore *sdk.KVStoreKey
capKeyAccountStore *sdk.KVStoreKey
capKeyIBCStore *sdk.KVStoreKey
capKeyStakingStore *sdk.KVStoreKey
2018-02-14 17:09:00 -08:00
// Manage getting and setting accounts
accountMapper sdk.AccountMapper
}
func NewBasecoinApp(logger log.Logger, dbs map[string]dbm.DB) *BasecoinApp {
2018-02-14 17:09:00 -08:00
// create your application object
var app = &BasecoinApp{
BaseApp: bam.NewBaseApp(appName, logger, dbs["main"]),
cdc: MakeCodec(),
capKeyMainStore: sdk.NewKVStoreKey("main"),
capKeyAccountStore: sdk.NewKVStoreKey("acc"),
capKeyIBCStore: sdk.NewKVStoreKey("ibc"),
2018-03-28 21:33:45 -07:00
capKeyStakingStore: sdk.NewKVStoreKey("stake"),
2018-02-14 17:09:00 -08:00
}
2018-02-14 11:14:22 -08:00
2018-02-15 08:00:57 -08:00
// define the accountMapper
app.accountMapper = auth.NewAccountMapperSealed(
app.capKeyMainStore, // target store
&types.AppAccount{}, // prototype
)
2018-02-14 17:09:00 -08:00
// add handlers
2018-02-17 13:19:34 -08:00
coinKeeper := bank.NewCoinKeeper(app.accountMapper)
2018-03-19 16:22:49 -07:00
ibcMapper := ibc.NewIBCMapper(app.cdc, app.capKeyIBCStore)
2018-03-28 21:33:45 -07:00
stakeKeeper := simplestake.NewKeeper(app.capKeyStakingStore, coinKeeper)
app.Router().
2018-04-02 08:13:37 -07:00
AddRoute("bank", bank.NewHandler(coinKeeper)).
AddRoute("ibc", ibc.NewHandler(ibcMapper, coinKeeper)).
AddRoute("simplestake", simplestake.NewHandler(stakeKeeper))
2018-02-14 11:14:22 -08:00
2018-02-15 08:00:57 -08:00
// initialize BaseApp
app.SetTxDecoder(app.txDecoder)
app.SetInitChainer(app.initChainer)
app.MountStoreWithDB(app.capKeyMainStore, sdk.StoreTypeIAVL, dbs["main"])
app.MountStoreWithDB(app.capKeyAccountStore, sdk.StoreTypeIAVL, dbs["acc"])
app.MountStoreWithDB(app.capKeyIBCStore, sdk.StoreTypeIAVL, dbs["ibc"])
app.MountStoreWithDB(app.capKeyStakingStore, sdk.StoreTypeIAVL, dbs["staking"])
// NOTE: Broken until #532 lands
//app.MountStoresIAVL(app.capKeyMainStore, app.capKeyIBCStore, app.capKeyStakingStore)
2018-02-15 08:00:57 -08:00
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper))
2018-02-17 13:19:34 -08:00
err := app.LoadLatestVersion(app.capKeyMainStore)
2018-02-15 08:00:57 -08:00
if err != nil {
2018-02-14 11:14:22 -08:00
cmn.Exit(err.Error())
}
return app
2018-02-14 08:16:06 -08:00
}
2018-02-15 08:00:57 -08:00
// custom tx codec
2018-03-03 09:30:08 -08:00
// TODO: use new go-wire
2018-02-28 17:57:38 -08:00
func MakeCodec() *wire.Codec {
2018-03-03 09:30:08 -08:00
const msgTypeSend = 0x1
const msgTypeIssue = 0x2
2018-03-12 17:37:50 -07:00
const msgTypeQuiz = 0x3
const msgTypeSetTrend = 0x4
2018-03-15 07:07:01 -07:00
const msgTypeIBCTransferMsg = 0x5
const msgTypeIBCReceiveMsg = 0x6
const msgTypeBondMsg = 0x7
const msgTypeUnbondMsg = 0x8
2018-03-02 01:24:07 -08:00
var _ = oldwire.RegisterInterface(
struct{ sdk.Msg }{},
oldwire.ConcreteType{bank.SendMsg{}, msgTypeSend},
oldwire.ConcreteType{bank.IssueMsg{}, msgTypeIssue},
2018-03-15 07:07:01 -07:00
oldwire.ConcreteType{ibc.IBCTransferMsg{}, msgTypeIBCTransferMsg},
oldwire.ConcreteType{ibc.IBCReceiveMsg{}, msgTypeIBCReceiveMsg},
2018-03-28 21:33:45 -07:00
oldwire.ConcreteType{simplestake.BondMsg{}, msgTypeBondMsg},
oldwire.ConcreteType{simplestake.UnbondMsg{}, msgTypeUnbondMsg},
2018-03-02 01:24:07 -08:00
)
2018-03-03 09:30:08 -08:00
const accTypeApp = 0x1
2018-03-02 01:24:07 -08:00
var _ = oldwire.RegisterInterface(
struct{ sdk.Account }{},
oldwire.ConcreteType{&types.AppAccount{}, accTypeApp},
)
2018-02-15 08:00:57 -08:00
cdc := wire.NewCodec()
2018-03-03 09:30:08 -08:00
2018-03-02 01:24:07 -08:00
// cdc.RegisterInterface((*sdk.Msg)(nil), nil)
// bank.RegisterWire(cdc) // Register bank.[SendMsg,IssueMsg] types.
// crypto.RegisterWire(cdc) // Register crypto.[PubKey,PrivKey,Signature] types.
2018-03-18 07:47:17 -07:00
// ibc.RegisterWire(cdc) // Register ibc.[IBCTransferMsg, IBCReceiveMsg] types.
2018-02-15 08:00:57 -08:00
return cdc
2018-02-14 08:16:06 -08:00
}
2018-02-15 08:00:57 -08:00
// custom logic for transaction decoding
func (app *BasecoinApp) txDecoder(txBytes []byte) (sdk.Tx, sdk.Error) {
var tx = sdk.StdTx{}
2018-03-03 09:30:08 -08:00
if len(txBytes) == 0 {
return nil, sdk.ErrTxDecode("txBytes are empty")
}
// StdTx.Msg is an interface. The concrete types
// are registered by MakeTxCodec in bank.RegisterWire.
err := app.cdc.UnmarshalBinary(txBytes, &tx)
if err != nil {
return nil, sdk.ErrTxDecode("").TraceCause(err, "")
}
return tx, nil
2018-02-14 08:16:06 -08:00
}
2018-02-15 08:00:57 -08:00
// custom logic for basecoin initialization
func (app *BasecoinApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
stateJSON := req.AppStateBytes
2018-02-14 08:16:06 -08:00
genesisState := new(types.GenesisState)
err := json.Unmarshal(stateJSON, genesisState)
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}
2018-02-14 08:16:06 -08:00
for _, gacc := range genesisState.Accounts {
acc, err := gacc.ToAppAccount()
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
2018-02-14 08:16:06 -08:00
}
app.accountMapper.SetAccount(ctx, acc)
}
return abci.ResponseInitChain{}
2018-02-14 08:16:06 -08:00
}