0g-chain/app/app.go

321 lines
10 KiB
Go
Raw Normal View History

2019-06-20 13:37:57 +00:00
package app
import (
"io"
"os"
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"
bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
2019-07-18 17:36:31 +00:00
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/version"
2019-06-20 13:37:57 +00:00
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/crisis"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
2019-07-18 17:36:31 +00:00
distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client"
"github.com/cosmos/cosmos-sdk/x/genaccounts"
"github.com/cosmos/cosmos-sdk/x/genutil"
2019-06-20 13:37:57 +00:00
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/mint"
"github.com/cosmos/cosmos-sdk/x/params"
2019-07-18 17:36:31 +00:00
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
2019-06-20 13:37:57 +00:00
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/staking"
2019-07-18 17:36:31 +00:00
"github.com/cosmos/cosmos-sdk/x/supply"
2019-06-20 13:37:57 +00:00
)
2019-07-18 17:36:31 +00:00
// TODO investigate monkey patch import
2019-06-20 13:37:57 +00:00
const (
2019-07-18 17:36:31 +00:00
appName = "kava"
2019-07-02 20:06:16 +00:00
Bech32MainPrefix = "kava"
2019-06-20 13:37:57 +00:00
)
var (
2019-07-18 17:36:31 +00:00
// default home directories for expected binaries
2019-06-20 17:08:58 +00:00
DefaultCLIHome = os.ExpandEnv("$HOME/.kvcli")
DefaultNodeHome = os.ExpandEnv("$HOME/.kvd")
2019-07-18 17:36:31 +00:00
// _ manages simple versions of full app modules. It's used for things such as codec registration and genesis file verification
ModuleBasics module.BasicManager // TODO rename
// module account permissions
mAccPerms = map[string][]string{
auth.FeeCollectorName: nil,
distr.ModuleName: nil,
mint.ModuleName: {supply.Minter},
staking.BondedPoolName: {supply.Burner, supply.Staking},
staking.NotBondedPoolName: {supply.Burner, supply.Staking},
gov.ModuleName: {supply.Burner},
}
2019-06-20 13:37:57 +00:00
)
2019-07-18 17:36:31 +00:00
func init() {
ModuleBasics = module.NewBasicManager(
genaccounts.AppModuleBasic{},
genutil.AppModuleBasic{},
auth.AppModuleBasic{},
bank.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
gov.NewAppModuleBasic(paramsclient.ProposalHandler, distrclient.ProposalHandler),
params.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
supply.AppModuleBasic{},
)
}
2019-06-20 13:37:57 +00:00
// Extended ABCI application
2019-06-20 17:02:29 +00:00
type App struct {
2019-06-20 13:37:57 +00:00
*bam.BaseApp
cdc *codec.Codec
invCheckPeriod uint
// keys to access the substores
2019-07-18 17:36:31 +00:00
keyMain *sdk.KVStoreKey
keyAccount *sdk.KVStoreKey
keySupply *sdk.KVStoreKey
keyStaking *sdk.KVStoreKey
tkeyStaking *sdk.TransientStoreKey
keySlashing *sdk.KVStoreKey
keyMint *sdk.KVStoreKey
keyDistr *sdk.KVStoreKey
tkeyDistr *sdk.TransientStoreKey
keyGov *sdk.KVStoreKey
keyParams *sdk.KVStoreKey
tkeyParams *sdk.TransientStoreKey
2019-06-20 13:37:57 +00:00
// Manage getting and setting accounts
2019-07-18 17:36:31 +00:00
accountKeeper auth.AccountKeeper
bankKeeper bank.Keeper
supplyKeeper supply.Keeper
stakingKeeper staking.Keeper
slashingKeeper slashing.Keeper
mintKeeper mint.Keeper
distrKeeper distr.Keeper
govKeeper gov.Keeper
crisisKeeper crisis.Keeper
paramsKeeper params.Keeper
// the module manager
mm *module.Manager
2019-06-20 13:37:57 +00:00
}
2019-06-20 17:02:29 +00:00
// NewApp returns a reference to an initialized App.
func NewApp(logger log.Logger, db dbm.DB, traceStore io.Writer, loadLatest bool,
2019-06-20 13:37:57 +00:00
invCheckPeriod uint,
2019-06-20 17:02:29 +00:00
baseAppOptions ...func(*bam.BaseApp)) *App {
2019-06-20 13:37:57 +00:00
cdc := MakeCodec()
bApp := bam.NewBaseApp(appName, logger, db, auth.DefaultTxDecoder(cdc), baseAppOptions...)
bApp.SetCommitMultiStoreTracer(traceStore)
2019-07-18 17:36:31 +00:00
bApp.SetAppVersion(version.Version)
2019-06-20 13:37:57 +00:00
2019-06-20 17:02:29 +00:00
var app = &App{
2019-07-18 17:36:31 +00:00
BaseApp: bApp,
cdc: cdc,
invCheckPeriod: invCheckPeriod,
keyMain: sdk.NewKVStoreKey(bam.MainStoreKey),
keyAccount: sdk.NewKVStoreKey(auth.StoreKey),
keySupply: sdk.NewKVStoreKey(supply.StoreKey),
keyStaking: sdk.NewKVStoreKey(staking.StoreKey),
tkeyStaking: sdk.NewTransientStoreKey(staking.TStoreKey),
keyMint: sdk.NewKVStoreKey(mint.StoreKey),
keyDistr: sdk.NewKVStoreKey(distr.StoreKey),
tkeyDistr: sdk.NewTransientStoreKey(distr.TStoreKey),
keySlashing: sdk.NewKVStoreKey(slashing.StoreKey),
keyGov: sdk.NewKVStoreKey(gov.StoreKey),
keyParams: sdk.NewKVStoreKey(params.StoreKey),
tkeyParams: sdk.NewTransientStoreKey(params.TStoreKey),
2019-06-20 13:37:57 +00:00
}
2019-07-18 17:36:31 +00:00
// init params keeper and subspaces
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams, params.DefaultCodespace)
authSubspace := app.paramsKeeper.Subspace(auth.DefaultParamspace)
bankSubspace := app.paramsKeeper.Subspace(bank.DefaultParamspace)
stakingSubspace := app.paramsKeeper.Subspace(staking.DefaultParamspace)
mintSubspace := app.paramsKeeper.Subspace(mint.DefaultParamspace)
distrSubspace := app.paramsKeeper.Subspace(distr.DefaultParamspace)
slashingSubspace := app.paramsKeeper.Subspace(slashing.DefaultParamspace)
govSubspace := app.paramsKeeper.Subspace(gov.DefaultParamspace)
crisisSubspace := app.paramsKeeper.Subspace(crisis.DefaultParamspace)
// add keepers
2019-06-20 13:37:57 +00:00
app.accountKeeper = auth.NewAccountKeeper(
app.cdc,
app.keyAccount,
2019-07-18 17:36:31 +00:00
authSubspace,
auth.ProtoBaseAccount)
2019-06-20 13:37:57 +00:00
app.bankKeeper = bank.NewBaseKeeper(
app.accountKeeper,
2019-07-18 17:36:31 +00:00
bankSubspace,
bank.DefaultCodespace)
app.supplyKeeper = supply.NewKeeper(
2019-06-20 13:37:57 +00:00
app.cdc,
2019-07-18 17:36:31 +00:00
app.keySupply,
app.accountKeeper,
app.bankKeeper,
supply.DefaultCodespace,
mAccPerms)
2019-06-20 13:37:57 +00:00
stakingKeeper := staking.NewKeeper(
app.cdc,
2019-07-18 17:36:31 +00:00
app.keyStaking,
app.tkeyStaking,
app.supplyKeeper,
stakingSubspace,
staking.DefaultCodespace)
app.mintKeeper = mint.NewKeeper(
app.cdc,
app.keyMint,
mintSubspace,
&stakingKeeper,
app.supplyKeeper,
auth.FeeCollectorName)
2019-06-20 13:37:57 +00:00
app.distrKeeper = distr.NewKeeper(
app.cdc,
app.keyDistr,
2019-07-18 17:36:31 +00:00
distrSubspace,
&stakingKeeper,
app.supplyKeeper,
2019-06-20 13:37:57 +00:00
distr.DefaultCodespace,
2019-07-18 17:36:31 +00:00
auth.FeeCollectorName)
2019-06-20 13:37:57 +00:00
app.slashingKeeper = slashing.NewKeeper(
app.cdc,
app.keySlashing,
2019-07-18 17:36:31 +00:00
&stakingKeeper,
slashingSubspace,
slashing.DefaultCodespace)
app.crisisKeeper = crisis.NewKeeper(
crisisSubspace,
invCheckPeriod,
app.supplyKeeper,
auth.FeeCollectorName)
govRouter := gov.NewRouter()
govRouter.
AddRoute(gov.RouterKey, gov.ProposalHandler).
AddRoute(params.RouterKey, params.NewParamChangeProposalHandler(app.paramsKeeper)).
AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.distrKeeper))
2019-06-20 13:37:57 +00:00
app.govKeeper = gov.NewKeeper(
app.cdc,
app.keyGov,
2019-07-18 17:36:31 +00:00
app.paramsKeeper,
govSubspace,
app.supplyKeeper,
&stakingKeeper,
2019-06-20 13:37:57 +00:00
gov.DefaultCodespace,
2019-07-18 17:36:31 +00:00
govRouter)
2019-06-20 13:37:57 +00:00
// register the staking hooks
2019-07-18 17:36:31 +00:00
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
2019-06-20 13:37:57 +00:00
app.stakingKeeper = *stakingKeeper.SetHooks(
2019-07-18 17:36:31 +00:00
staking.NewMultiStakingHooks(app.distrKeeper.Hooks(), app.slashingKeeper.Hooks()))
// create the module manager
app.mm = module.NewManager(
genaccounts.NewAppModule(app.accountKeeper),
genutil.NewAppModule(app.accountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx),
auth.NewAppModule(app.accountKeeper),
bank.NewAppModule(app.bankKeeper, app.accountKeeper),
crisis.NewAppModule(app.crisisKeeper),
supply.NewAppModule(app.supplyKeeper, app.accountKeeper),
distr.NewAppModule(app.distrKeeper, app.supplyKeeper),
gov.NewAppModule(app.govKeeper, app.supplyKeeper),
mint.NewAppModule(app.mintKeeper),
slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper),
staking.NewAppModule(app.stakingKeeper, app.distrKeeper, app.accountKeeper, app.supplyKeeper),
2019-06-20 13:37:57 +00:00
)
2019-07-18 17:36:31 +00:00
// During begin block slashing happens after distr.BeginBlocker so that
// there is nothing left over in the validator fee pool, so as to keep the
// CanWithdrawInvariant invariant.
app.mm.SetOrderBeginBlockers(mint.ModuleName, distr.ModuleName, slashing.ModuleName)
app.mm.SetOrderEndBlockers(gov.ModuleName, staking.ModuleName)
// genutils must occur after staking so that pools are properly
// initialized with tokens from genesis accounts.
app.mm.SetOrderInitGenesis(genaccounts.ModuleName, distr.ModuleName,
staking.ModuleName, auth.ModuleName, bank.ModuleName, slashing.ModuleName,
gov.ModuleName, mint.ModuleName, supply.ModuleName, crisis.ModuleName, genutil.ModuleName)
app.mm.RegisterInvariants(&app.crisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())
// initialize the app
app.MountStores(app.keyMain, app.keyAccount, app.keySupply, app.keyStaking,
app.keyMint, app.keyDistr, app.keySlashing, app.keyGov, app.keyParams,
app.tkeyParams, app.tkeyStaking, app.tkeyDistr)
app.SetInitChainer(app.InitChainer)
2019-06-20 13:37:57 +00:00
app.SetBeginBlocker(app.BeginBlocker)
2019-07-18 17:36:31 +00:00
app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.supplyKeeper, auth.DefaultSigVerificationGasConsumer))
2019-06-20 13:37:57 +00:00
app.SetEndBlocker(app.EndBlocker)
2019-07-18 17:36:31 +00:00
// load store
2019-06-20 13:37:57 +00:00
if loadLatest {
err := app.LoadLatestVersion(app.keyMain)
if err != nil {
cmn.Exit(err.Error())
}
}
return app
}
// custom tx codec
func MakeCodec() *codec.Codec {
var cdc = codec.New()
2019-07-18 17:36:31 +00:00
ModuleBasics.RegisterCodec(cdc)
2019-06-20 13:37:57 +00:00
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
return cdc
}
2019-06-25 13:29:56 +00:00
func SetBech32AddressPrefixes(config *sdk.Config) {
config.SetBech32PrefixForAccount(Bech32MainPrefix, Bech32MainPrefix+sdk.PrefixPublic)
config.SetBech32PrefixForValidator(Bech32MainPrefix+sdk.PrefixValidator+sdk.PrefixOperator, Bech32MainPrefix+sdk.PrefixValidator+sdk.PrefixOperator+sdk.PrefixPublic)
config.SetBech32PrefixForConsensusNode(Bech32MainPrefix+sdk.PrefixValidator+sdk.PrefixConsensus, Bech32MainPrefix+sdk.PrefixValidator+sdk.PrefixConsensus+sdk.PrefixPublic)
}
2019-06-20 13:37:57 +00:00
// application updates every end block
2019-06-20 17:02:29 +00:00
func (app *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
2019-07-18 17:36:31 +00:00
return app.mm.BeginBlock(ctx, req)
2019-06-20 13:37:57 +00:00
}
// application updates every end block
2019-06-20 17:02:29 +00:00
func (app *App) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
2019-07-18 17:36:31 +00:00
return app.mm.EndBlock(ctx, req)
2019-06-20 13:37:57 +00:00
}
2019-06-20 17:28:21 +00:00
// custom logic for app initialization
2019-07-18 17:36:31 +00:00
func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
2019-06-20 13:37:57 +00:00
var genesisState GenesisState
2019-07-18 17:36:31 +00:00
app.cdc.MustUnmarshalJSON(req.AppStateBytes, &genesisState)
return app.mm.InitGenesis(ctx, genesisState)
2019-06-20 13:37:57 +00:00
}
// load a particular height
2019-06-20 17:02:29 +00:00
func (app *App) LoadHeight(height int64) error {
2019-06-20 13:37:57 +00:00
return app.LoadVersion(height, app.keyMain)
}
2019-07-18 17:36:31 +00:00
// TODO only used in sim test, needed?
// ModuleAccountAddrs returns all the app's module account addresses.
func (app *App) ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
for acc := range mAccPerms {
modAccAddrs[app.supplyKeeper.GetModuleAddress(acc).String()] = true
}
2019-06-20 13:37:57 +00:00
2019-07-18 17:36:31 +00:00
return modAccAddrs
2019-06-20 13:37:57 +00:00
}