cosmos-sdk/cmd/gaia/app/app.go

232 lines
7.5 KiB
Go
Raw Normal View History

2018-04-06 22:12:00 -07:00
package app
import (
2018-04-27 17:00:33 -07:00
"encoding/json"
"io"
2018-04-25 21:27:40 -07:00
"os"
2018-04-06 22:12:00 -07:00
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
2018-07-02 21:08:00 -07:00
tmtypes "github.com/tendermint/tendermint/types"
2018-04-06 22:12:00 -07:00
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-21 17:19:14 -07:00
"github.com/cosmos/cosmos-sdk/x/gov"
2018-04-06 22:12:00 -07:00
"github.com/cosmos/cosmos-sdk/x/ibc"
"github.com/cosmos/cosmos-sdk/x/params"
2018-05-23 13:25:56 -07:00
"github.com/cosmos/cosmos-sdk/x/slashing"
2018-04-06 22:12:00 -07:00
"github.com/cosmos/cosmos-sdk/x/stake"
)
const (
appName = "GaiaApp"
)
2018-04-25 21:27:40 -07:00
// default home directories for expected binaries
var (
DefaultCLIHome = os.ExpandEnv("$HOME/.gaiacli")
DefaultNodeHome = os.ExpandEnv("$HOME/.gaiad")
)
2018-04-06 22:12:00 -07:00
// Extended ABCI application
type GaiaApp struct {
*bam.BaseApp
cdc *wire.Codec
// keys to access the substores
2018-06-29 06:37:14 -07:00
keyMain *sdk.KVStoreKey
keyAccount *sdk.KVStoreKey
keyIBC *sdk.KVStoreKey
keyStake *sdk.KVStoreKey
tkeyStake *sdk.TransientStoreKey
2018-06-29 06:37:14 -07:00
keySlashing *sdk.KVStoreKey
keyGov *sdk.KVStoreKey
keyFeeCollection *sdk.KVStoreKey
keyParams *sdk.KVStoreKey
2018-07-26 18:24:18 -07:00
tkeyParams *sdk.TransientStoreKey
2018-04-06 22:12:00 -07:00
// Manage getting and setting accounts
2018-05-25 20:29:40 -07:00
accountMapper auth.AccountMapper
feeCollectionKeeper auth.FeeCollectionKeeper
bankKeeper bank.Keeper
2018-05-25 20:29:40 -07:00
ibcMapper ibc.Mapper
stakeKeeper stake.Keeper
2018-05-23 13:25:56 -07:00
slashingKeeper slashing.Keeper
2018-06-21 17:19:14 -07:00
govKeeper gov.Keeper
paramsKeeper params.Keeper
2018-04-06 22:12:00 -07:00
}
// NewGaiaApp returns a reference to an initialized GaiaApp.
2018-07-12 18:20:26 -07:00
func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptions ...func(*bam.BaseApp)) *GaiaApp {
cdc := MakeCodec()
2018-07-18 16:24:16 -07:00
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
2018-04-06 22:12:00 -07:00
var app = &GaiaApp{
BaseApp: bApp,
2018-06-29 06:37:14 -07:00
cdc: cdc,
keyMain: sdk.NewKVStoreKey("main"),
keyAccount: sdk.NewKVStoreKey("acc"),
keyIBC: sdk.NewKVStoreKey("ibc"),
keyStake: sdk.NewKVStoreKey("stake"),
tkeyStake: sdk.NewTransientStoreKey("transient_stake"),
2018-06-29 06:37:14 -07:00
keySlashing: sdk.NewKVStoreKey("slashing"),
keyGov: sdk.NewKVStoreKey("gov"),
2018-07-02 21:08:00 -07:00
keyFeeCollection: sdk.NewKVStoreKey("fee"),
keyParams: sdk.NewKVStoreKey("params"),
tkeyParams: sdk.NewTransientStoreKey("transient_params"),
2018-04-06 22:12:00 -07:00
}
2018-04-27 08:06:55 -07:00
// define the accountMapper
app.accountMapper = auth.NewAccountMapper(
app.cdc,
app.keyAccount, // target store
auth.ProtoBaseAccount, // prototype
2018-04-27 08:06:55 -07:00
)
// add handlers
app.bankKeeper = bank.NewBaseKeeper(app.accountMapper)
2018-04-18 21:49:24 -07:00
app.ibcMapper = ibc.NewMapper(app.cdc, app.keyIBC, app.RegisterCodespace(ibc.DefaultCodespace))
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.tkeyStake, app.bankKeeper, app.RegisterCodespace(stake.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper, app.paramsKeeper.Getter(), app.RegisterCodespace(slashing.DefaultCodespace))
app.stakeKeeper = app.stakeKeeper.WithValidatorHooks(app.slashingKeeper.ValidatorHooks())
app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper.Setter(), app.bankKeeper, app.stakeKeeper, app.RegisterCodespace(gov.DefaultCodespace))
2018-06-29 06:37:14 -07:00
app.feeCollectionKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.keyFeeCollection)
2018-04-18 09:36:55 -07:00
// register message routes
2018-04-06 22:12:00 -07:00
app.Router().
AddRoute("bank", bank.NewHandler(app.bankKeeper)).
AddRoute("ibc", ibc.NewHandler(app.ibcMapper, app.bankKeeper)).
AddRoute("stake", stake.NewHandler(app.stakeKeeper)).
2018-06-21 17:19:14 -07:00
AddRoute("slashing", slashing.NewHandler(app.slashingKeeper)).
AddRoute("gov", gov.NewHandler(app.govKeeper))
2018-04-06 22:12:00 -07:00
2018-08-04 22:56:48 -07:00
app.QueryRouter().
AddRoute("gov", gov.NewQuerier(app.govKeeper))
2018-04-06 22:12:00 -07:00
// initialize BaseApp
app.SetInitChainer(app.initChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
2018-05-25 20:29:40 -07:00
app.SetAnteHandler(auth.NewAnteHandler(app.accountMapper, app.feeCollectionKeeper))
app.MountStoresIAVL(app.keyMain, app.keyAccount, app.keyIBC, app.keyStake,
app.keySlashing, app.keyGov, app.keyFeeCollection, app.keyParams)
app.MountStoresTransient(app.tkeyParams, app.tkeyStake)
2018-04-18 21:49:24 -07:00
err := app.LoadLatestVersion(app.keyMain)
2018-04-06 22:12:00 -07:00
if err != nil {
cmn.Exit(err.Error())
}
return app
}
// custom tx codec
func MakeCodec() *wire.Codec {
2018-04-09 20:08:00 -07:00
var cdc = wire.NewCodec()
ibc.RegisterWire(cdc)
bank.RegisterWire(cdc)
stake.RegisterWire(cdc)
2018-05-28 12:38:02 -07:00
slashing.RegisterWire(cdc)
2018-06-21 17:19:14 -07:00
gov.RegisterWire(cdc)
2018-05-23 22:09:01 -07:00
auth.RegisterWire(cdc)
sdk.RegisterWire(cdc)
2018-04-09 20:08:00 -07:00
wire.RegisterCrypto(cdc)
2018-04-06 22:12:00 -07:00
return cdc
}
// application updates every end block
func (app *GaiaApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
tags := slashing.BeginBlocker(ctx, req, app.slashingKeeper)
return abci.ResponseBeginBlock{
Tags: tags.ToKVPairs(),
}
}
// application updates every end block
// nolint: unparam
func (app *GaiaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
tags := gov.EndBlocker(ctx, app.govKeeper)
2018-07-20 08:35:13 -07:00
validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
// Add these new validators to the addr -> pubkey map.
app.slashingKeeper.AddValidators(ctx, validatorUpdates)
return abci.ResponseEndBlock{
ValidatorUpdates: validatorUpdates,
2018-06-21 17:19:14 -07:00
Tags: tags,
}
}
2018-04-06 22:12:00 -07:00
// custom logic for gaia initialization
func (app *GaiaApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
2018-06-04 16:42:01 -07:00
stateJSON := req.AppStateBytes
2018-05-23 13:25:56 -07:00
// TODO is this now the whole genesis file?
2018-04-06 22:12:00 -07:00
2018-04-30 14:21:14 -07:00
var genesisState GenesisState
err := app.cdc.UnmarshalJSON(stateJSON, &genesisState)
2018-04-06 22:12:00 -07:00
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}
2018-04-06 22:50:46 -07:00
// load the accounts
2018-04-06 22:12:00 -07:00
for _, gacc := range genesisState.Accounts {
acc := gacc.ToAccount()
acc.AccountNumber = app.accountMapper.GetNextAccountNumber(ctx)
2018-04-06 22:50:46 -07:00
app.accountMapper.SetAccount(ctx, acc)
2018-04-06 22:12:00 -07:00
}
2018-04-06 22:50:46 -07:00
// load the initial stake information
validators, err := stake.InitGenesis(ctx, app.stakeKeeper, genesisState.StakeData)
2018-07-09 19:51:13 -07:00
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}
2018-04-06 22:50:46 -07:00
// load the address to pubkey map
slashing.InitGenesis(ctx, app.slashingKeeper, genesisState.StakeData)
2018-08-20 03:47:04 -07:00
gov.InitGenesis(ctx, app.govKeeper, genesisState.GovData)
err = GaiaValidateGenesisState(genesisState)
if err != nil {
// TODO find a way to do this w/o panics
panic(err)
}
2018-06-21 17:19:14 -07:00
return abci.ResponseInitChain{
Validators: validators,
}
2018-04-06 22:12:00 -07:00
}
2018-04-24 06:46:39 -07:00
// export the state of gaia for a genesis file
func (app *GaiaApp) ExportAppStateAndValidators() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
2018-04-27 07:59:47 -07:00
ctx := app.NewContext(true, abci.Header{})
2018-04-27 17:00:33 -07:00
// iterate to get the accounts
2018-04-27 07:59:47 -07:00
accounts := []GenesisAccount{}
2018-05-23 19:26:54 -07:00
appendAccount := func(acc auth.Account) (stop bool) {
2018-04-27 17:00:33 -07:00
account := NewGenesisAccountI(acc)
accounts = append(accounts, account)
2018-04-27 07:59:47 -07:00
return false
2018-04-27 17:00:33 -07:00
}
app.accountMapper.IterateAccounts(ctx, appendAccount)
genState := GenesisState{
2018-04-27 07:59:47 -07:00
Accounts: accounts,
StakeData: stake.WriteGenesis(ctx, app.stakeKeeper),
2018-08-20 03:47:04 -07:00
GovData: gov.WriteGenesis(ctx, app.govKeeper),
2018-04-26 08:53:07 -07:00
}
appState, err = wire.MarshalJSONIndent(app.cdc, genState)
if err != nil {
return nil, nil, err
}
validators = stake.WriteValidators(ctx, app.stakeKeeper)
return appState, validators, nil
2018-04-24 06:46:39 -07:00
}