0g-chain/internal/app/app.go

189 lines
5.6 KiB
Go
Raw Normal View History

2018-05-25 13:46:33 +00:00
package app
import (
"encoding/json"
abci "github.com/tendermint/abci/types"
2018-06-16 21:34:07 +00:00
tmtypes "github.com/tendermint/tendermint/types"
2018-05-25 13:46:33 +00:00
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"
"github.com/cosmos/cosmos-sdk/wire"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
2018-06-18 14:49:09 +00:00
//"github.com/cosmos/cosmos-sdk/x/ibc"
//"github.com/cosmos/cosmos-sdk/x/slashing"
//"github.com/cosmos/cosmos-sdk/x/stake"
2018-06-16 16:21:13 +00:00
"github.com/kava-labs/kava/internal/types"
2018-05-25 13:46:33 +00:00
)
const (
2018-06-16 21:34:07 +00:00
appName = "KavaApp"
2018-05-25 13:46:33 +00:00
)
// Extended ABCI application
2018-06-16 21:34:07 +00:00
type KavaApp struct {
2018-05-25 13:46:33 +00:00
*bam.BaseApp
cdc *wire.Codec
// keys to access the substores
2018-06-18 14:49:09 +00:00
keyMain *sdk.KVStoreKey
keyAccount *sdk.KVStoreKey
//keyIBC *sdk.KVStoreKey
//keyStake *sdk.KVStoreKey
//keySlashing *sdk.KVStoreKey
2018-05-25 13:46:33 +00:00
// Manage getting and setting accounts
2018-06-16 21:34:07 +00:00
accountMapper auth.AccountMapper
feeCollectionKeeper auth.FeeCollectionKeeper
coinKeeper bank.Keeper
2018-06-18 14:49:09 +00:00
//ibcMapper ibc.Mapper
//stakeKeeper stake.Keeper
//slashingKeeper slashing.Keeper
2018-05-25 13:46:33 +00:00
}
2018-06-16 21:34:07 +00:00
func NewKavaApp(logger log.Logger, db dbm.DB) *KavaApp {
2018-05-25 13:46:33 +00:00
// Create app-level codec for txs and accounts.
var cdc = MakeCodec()
// Create your application object.
2018-06-16 21:34:07 +00:00
var app = &KavaApp{
2018-06-18 14:49:09 +00:00
BaseApp: bam.NewBaseApp(appName, cdc, logger, db),
cdc: cdc,
keyMain: sdk.NewKVStoreKey("main"),
keyAccount: sdk.NewKVStoreKey("acc"),
//keyIBC: sdk.NewKVStoreKey("ibc"),
//keyStake: sdk.NewKVStoreKey("stake"),
//keySlashing: sdk.NewKVStoreKey("slashing"),
2018-05-25 13:46:33 +00:00
}
// Define the accountMapper.
app.accountMapper = auth.NewAccountMapper(
cdc,
2018-06-18 14:49:09 +00:00
app.keyAccount, // target store
&auth.BaseAccount{},
2018-05-25 13:46:33 +00:00
)
// add accountMapper/handlers
app.coinKeeper = bank.NewKeeper(app.accountMapper)
2018-06-18 14:49:09 +00:00
//app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
//app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.coinKeeper, app.RegisterCodespace(stake.DefaultCodespace))
//app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.RegisterCodespace(slashing.DefaultCodespace))
2018-05-25 13:46:33 +00:00
// register message routes
app.Router().
2018-06-16 21:34:07 +00:00
AddRoute("auth", auth.NewHandler(app.accountMapper)).
2018-06-18 14:49:09 +00:00
AddRoute("bank", bank.NewHandler(app.coinKeeper))
//AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.coinKeeper)).
//AddRoute("stake", stake.NewHandler(app.stakeKeeper))
2018-05-25 13:46:33 +00:00
// Initialize BaseApp.
app.SetInitChainer(app.initChainer)
2018-06-16 21:34:07 +00:00
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
2018-06-18 14:49:09 +00:00
app.MountStoresIAVL(app.keyMain, app.keyAccount) //, app.keyIBC, app.keyStake, app.keySlashing)
2018-05-25 13:46:33 +00:00
err := app.LoadLatestVersion(app.keyMain)
if err != nil {
cmn.Exit(err.Error())
}
return app
}
// Custom tx codec
func MakeCodec() *wire.Codec {
var cdc = wire.NewCodec()
wire.RegisterCrypto(cdc) // Register crypto.
sdk.RegisterWire(cdc) // Register Msgs
bank.RegisterWire(cdc)
2018-06-18 14:49:09 +00:00
//stake.RegisterWire(cdc)
//slashing.RegisterWire(cdc)
//ibc.RegisterWire(cdc)
auth.RegisterWire(cdc)
2018-05-25 13:46:33 +00:00
// register custom AppAccount
2018-06-18 14:49:09 +00:00
//cdc.RegisterInterface((*auth.Account)(nil), nil)
//cdc.RegisterConcrete(&types.BaseAccount{}, "kava/Account", nil)
2018-05-25 13:46:33 +00:00
return cdc
}
2018-06-16 21:34:07 +00:00
// application updates every end block
func (app *KavaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
2018-06-18 14:49:09 +00:00
//tags := slashing.BeginBlocker(ctx, req, app.slashingKeeper)
2018-06-16 21:34:07 +00:00
2018-06-18 14:49:09 +00:00
//return abci.ResponseBeginBlock{
// Tags: tags.ToKVPairs(),
//}
return abci.ResponseBeginBlock{}
2018-06-16 21:34:07 +00:00
}
// application updates every end block
func (app *KavaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
2018-06-18 14:49:09 +00:00
//validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
2018-06-16 21:34:07 +00:00
2018-06-18 14:49:09 +00:00
//return abci.ResponseEndBlock{
// ValidatorUpdates: validatorUpdates,
//}
return abci.ResponseEndBlock{}
2018-06-16 21:34:07 +00:00
}
2018-06-18 14:49:09 +00:00
// Custom logic for initialization
2018-06-16 21:34:07 +00:00
func (app *KavaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
2018-05-25 13:46:33 +00:00
stateJSON := req.AppStateBytes
genesisState := new(types.GenesisState)
err := app.cdc.UnmarshalJSON(stateJSON, genesisState)
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}
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-06-16 21:34:07 +00:00
acc.AccountNumber = app.accountMapper.GetNextAccountNumber(ctx)
2018-05-25 13:46:33 +00:00
app.accountMapper.SetAccount(ctx, acc)
}
2018-06-16 21:34:07 +00:00
// load the initial stake information
2018-06-18 14:49:09 +00:00
//stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
2018-06-16 21:34:07 +00:00
2018-05-25 13:46:33 +00:00
return abci.ResponseInitChain{}
}
// Custom logic for state export
2018-06-16 21:34:07 +00:00
func (app *KavaApp) ExportAppStateAndValidators() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
2018-05-25 13:46:33 +00:00
ctx := app.NewContext(true, abci.Header{})
// iterate to get the accounts
2018-06-18 14:49:09 +00:00
accounts := []types.GenesisAccount{}
2018-06-16 21:34:07 +00:00
appendAccount := func(acc auth.Account) (stop bool) {
2018-06-18 14:49:09 +00:00
account := types.GenesisAccount{
2018-05-25 13:46:33 +00:00
Address: acc.GetAddress(),
Coins: acc.GetCoins(),
}
accounts = append(accounts, account)
return false
}
app.accountMapper.IterateAccounts(ctx, appendAccount)
genState := types.GenesisState{
Accounts: accounts,
}
2018-06-16 21:34:07 +00:00
appState, err = wire.MarshalJSONIndent(app.cdc, genState)
if err != nil {
return nil, nil, err
}
2018-06-18 14:49:09 +00:00
validators = make([]tmtypes.GenesisValidator, 0) // TODO export the actual validators
2018-06-16 21:34:07 +00:00
return appState, validators, err
2018-05-25 13:46:33 +00:00
}