0g-chain/app/app.go

401 lines
13 KiB
Go
Raw Normal View History

2019-06-20 13:37:57 +00:00
package app
import (
"io"
"os"
2019-11-29 10:55:10 +00:00
"github.com/kava-labs/kava/x/auction"
"github.com/kava-labs/kava/x/cdp"
"github.com/kava-labs/kava/x/liquidator"
"github.com/kava-labs/kava/x/pricefeed"
2019-10-02 13:10:28 +00:00
validatorvesting "github.com/kava-labs/kava/x/validator-vesting"
2019-06-20 13:37:57 +00:00
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
"github.com/tendermint/tendermint/libs/log"
2019-09-11 22:33:20 +00:00
dbm "github.com/tendermint/tm-db"
2019-06-20 13:37:57 +00:00
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"
2019-10-04 17:55:49 +00:00
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
2019-06-20 13:37:57 +00:00
"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
"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
)
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
2019-07-19 14:06:33 +00:00
// ModuleBasics manages simple versions of full app modules. It's used for things such as codec registration and genesis file verification.
2019-07-18 17:36:31 +00:00
ModuleBasics = module.NewBasicManager(
genutil.AppModuleBasic{},
auth.AppModuleBasic{},
2019-10-02 13:10:28 +00:00
validatorvesting.AppModuleBasic{},
2019-07-18 17:36:31 +00:00
bank.AppModuleBasic{},
staking.AppModuleBasic{},
mint.AppModuleBasic{},
distr.AppModuleBasic{},
2019-09-11 22:33:20 +00:00
gov.NewAppModuleBasic(paramsclient.ProposalHandler, distr.ProposalHandler),
2019-07-18 17:36:31 +00:00
params.AppModuleBasic{},
crisis.AppModuleBasic{},
slashing.AppModuleBasic{},
supply.AppModuleBasic{},
2019-11-29 10:55:10 +00:00
auction.AppModuleBasic{},
cdp.AppModuleBasic{},
liquidator.AppModuleBasic{},
pricefeed.AppModuleBasic{},
2019-07-18 17:36:31 +00:00
)
2019-09-11 22:33:20 +00:00
// module account permissions
mAccPerms = map[string][]string{
2019-10-02 13:10:28 +00:00
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},
validatorvesting.ModuleName: {supply.Burner},
2019-09-11 22:33:20 +00:00
}
)
2019-07-18 17:36:31 +00:00
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-09-11 22:33:20 +00:00
keys map[string]*sdk.KVStoreKey
tkeys map[string]*sdk.TransientStoreKey
2019-06-20 13:37:57 +00:00
2019-07-26 12:11:23 +00:00
// keepers from all the modules
2019-11-29 10:55:10 +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
vvKeeper validatorvesting.Keeper
auctionKeeper auction.Keeper
cdpKeeper cdp.Keeper
liquidatorKeeper liquidator.Keeper
pricefeedKeeper pricefeed.Keeper
2019-07-18 17:36:31 +00:00
// the module manager
mm *module.Manager
2019-09-11 22:33:20 +00:00
// simulation manager
sm *module.SimulationManager
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-09-11 22:33:20 +00:00
keys := sdk.NewKVStoreKeys(
bam.MainStoreKey, auth.StoreKey, staking.StoreKey,
supply.StoreKey, mint.StoreKey, distr.StoreKey, slashing.StoreKey,
2019-10-02 13:10:28 +00:00
gov.StoreKey, params.StoreKey, validatorvesting.StoreKey,
2019-11-29 10:55:10 +00:00
auction.StoreKey, cdp.StoreKey, liquidator.StoreKey, pricefeed.StoreKey,
2019-09-11 22:33:20 +00:00
)
2019-09-25 19:00:44 +00:00
tkeys := sdk.NewTransientStoreKeys(params.TStoreKey)
2019-09-11 22:33:20 +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,
2019-09-11 22:33:20 +00:00
keys: keys,
tkeys: tkeys,
2019-06-20 13:37:57 +00:00
}
2019-07-18 17:36:31 +00:00
// init params keeper and subspaces
2019-09-11 22:33:20 +00:00
app.paramsKeeper = params.NewKeeper(app.cdc, keys[params.StoreKey], tkeys[params.TStoreKey], params.DefaultCodespace)
2019-07-18 17:36:31 +00:00
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)
2019-09-11 22:33:20 +00:00
govSubspace := app.paramsKeeper.Subspace(gov.DefaultParamspace).WithKeyTable(gov.ParamKeyTable())
2019-07-18 17:36:31 +00:00
crisisSubspace := app.paramsKeeper.Subspace(crisis.DefaultParamspace)
2019-11-29 10:55:10 +00:00
auctionSubspace := app.paramsKeeper.Subspace(auction.DefaultParamspace)
cdpSubspace := app.paramsKeeper.Subspace(cdp.DefaultParamspace)
liquidatorSubspace := app.paramsKeeper.Subspace(liquidator.DefaultParamspace)
pricefeedSubspace := app.paramsKeeper.Subspace(pricefeed.DefaultParamspace)
2019-07-18 17:36:31 +00:00
// add keepers
2019-06-20 13:37:57 +00:00
app.accountKeeper = auth.NewAccountKeeper(
app.cdc,
2019-09-11 22:33:20 +00:00
keys[auth.StoreKey],
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,
2019-09-11 22:33:20 +00:00
bank.DefaultCodespace,
app.ModuleAccountAddrs())
2019-07-18 17:36:31 +00:00
app.supplyKeeper = supply.NewKeeper(
2019-06-20 13:37:57 +00:00
app.cdc,
2019-09-11 22:33:20 +00:00
keys[supply.StoreKey],
2019-07-18 17:36:31 +00:00
app.accountKeeper,
app.bankKeeper,
mAccPerms)
2019-06-20 13:37:57 +00:00
stakingKeeper := staking.NewKeeper(
app.cdc,
2019-09-11 22:33:20 +00:00
keys[staking.StoreKey],
2019-07-18 17:36:31 +00:00
app.supplyKeeper,
stakingSubspace,
staking.DefaultCodespace)
app.mintKeeper = mint.NewKeeper(
app.cdc,
2019-09-11 22:33:20 +00:00
keys[mint.StoreKey],
2019-07-18 17:36:31 +00:00
mintSubspace,
&stakingKeeper,
app.supplyKeeper,
auth.FeeCollectorName)
2019-06-20 13:37:57 +00:00
app.distrKeeper = distr.NewKeeper(
app.cdc,
2019-09-11 22:33:20 +00:00
keys[distr.StoreKey],
2019-07-18 17:36:31 +00:00
distrSubspace,
&stakingKeeper,
app.supplyKeeper,
2019-06-20 13:37:57 +00:00
distr.DefaultCodespace,
2019-09-11 22:33:20 +00:00
auth.FeeCollectorName,
app.ModuleAccountAddrs())
2019-06-20 13:37:57 +00:00
app.slashingKeeper = slashing.NewKeeper(
app.cdc,
2019-09-11 22:33:20 +00:00
keys[slashing.StoreKey],
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,
2019-09-11 22:33:20 +00:00
keys[gov.StoreKey],
2019-07-18 17:36:31 +00:00
govSubspace,
app.supplyKeeper,
&stakingKeeper,
2019-06-20 13:37:57 +00:00
gov.DefaultCodespace,
2019-07-18 17:36:31 +00:00
govRouter)
2019-10-02 13:10:28 +00:00
app.vvKeeper = validatorvesting.NewKeeper(
app.cdc,
keys[validatorvesting.StoreKey],
app.accountKeeper,
app.bankKeeper,
app.supplyKeeper,
&stakingKeeper)
2019-11-29 10:55:10 +00:00
app.pricefeedKeeper = pricefeed.NewKeeper(
app.cdc,
keys[pricefeed.StoreKey],
pricefeedSubspace,
pricefeed.DefaultCodespace)
app.cdpKeeper = cdp.NewKeeper(
app.cdc,
keys[cdp.StoreKey],
cdpSubspace,
app.pricefeedKeeper,
app.bankKeeper)
app.auctionKeeper = auction.NewKeeper(
app.cdc,
app.cdpKeeper, // CDP keeper standing in for bank
keys[auction.StoreKey],
auctionSubspace)
app.liquidatorKeeper = liquidator.NewKeeper(
app.cdc,
keys[liquidator.StoreKey],
liquidatorSubspace,
app.cdpKeeper,
app.auctionKeeper,
app.cdpKeeper) // CDP keeper standing in for bank
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()))
2019-09-11 22:33:20 +00:00
// create the module manager (Note: Any module instantiated in the module manager that is later modified
// must be passed by reference here.)
2019-07-18 17:36:31 +00:00
app.mm = module.NewManager(
genutil.NewAppModule(app.accountKeeper, app.stakingKeeper, app.BaseApp.DeliverTx),
auth.NewAppModule(app.accountKeeper),
bank.NewAppModule(app.bankKeeper, app.accountKeeper),
2019-09-11 22:33:20 +00:00
crisis.NewAppModule(&app.crisisKeeper),
2019-07-18 17:36:31 +00:00
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),
2019-09-11 22:33:20 +00:00
staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper),
2019-10-02 13:10:28 +00:00
validatorvesting.NewAppModule(app.vvKeeper, app.accountKeeper),
2019-11-29 10:55:10 +00:00
auction.NewAppModule(app.auctionKeeper),
cdp.NewAppModule(app.cdpKeeper, app.pricefeedKeeper),
liquidator.NewAppModule(app.liquidatorKeeper),
pricefeed.NewAppModule(app.pricefeedKeeper),
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.
2019-10-02 13:10:28 +00:00
app.mm.SetOrderBeginBlockers(mint.ModuleName, distr.ModuleName, slashing.ModuleName, validatorvesting.ModuleName)
2019-07-18 17:36:31 +00:00
2019-11-29 10:55:10 +00:00
app.mm.SetOrderEndBlockers(crisis.ModuleName, gov.ModuleName, staking.ModuleName, pricefeed.ModuleName, auction.ModuleName) // TODO is this correct order?
2019-07-18 17:36:31 +00:00
2019-09-27 18:05:21 +00:00
// Note: genutils must occur after staking so that pools are properly
2019-07-18 17:36:31 +00:00
// initialized with tokens from genesis accounts.
2019-09-27 18:05:21 +00:00
//
// Note: Changing the order of the auth module and modules that use module accounts
// results in subtle changes to the way accounts are loaded from genesis.
2019-09-25 19:50:03 +00:00
app.mm.SetOrderInitGenesis(
2019-10-02 13:10:28 +00:00
auth.ModuleName, validatorvesting.ModuleName, distr.ModuleName,
2019-09-25 19:50:03 +00:00
staking.ModuleName, bank.ModuleName, slashing.ModuleName,
gov.ModuleName, mint.ModuleName, supply.ModuleName, crisis.ModuleName, genutil.ModuleName,
2019-11-29 10:55:10 +00:00
pricefeed.ModuleName, cdp.ModuleName, auction.ModuleName, liquidator.ModuleName, // TODO is this order ok?
2019-09-25 19:50:03 +00:00
)
2019-07-18 17:36:31 +00:00
app.mm.RegisterInvariants(&app.crisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())
2019-09-11 22:33:20 +00:00
// create the simulation manager and define the order of the modules for deterministic simulations
//
// NOTE: This is not required for apps that don't use the simulator for fuzz testing
// transactions.
app.sm = module.NewSimulationManager(
auth.NewAppModule(app.accountKeeper),
2019-10-02 13:10:28 +00:00
validatorvesting.NewAppModule(app.vvKeeper, app.accountKeeper),
2019-09-11 22:33:20 +00:00
bank.NewAppModule(app.bankKeeper, app.accountKeeper),
supply.NewAppModule(app.supplyKeeper, app.accountKeeper),
gov.NewAppModule(app.govKeeper, app.supplyKeeper),
mint.NewAppModule(app.mintKeeper),
distr.NewAppModule(app.distrKeeper, app.supplyKeeper),
staking.NewAppModule(app.stakingKeeper, app.accountKeeper, app.supplyKeeper),
slashing.NewAppModule(app.slashingKeeper, app.stakingKeeper),
)
app.sm.RegisterStoreDecoders()
// initialize stores
app.MountKVStores(keys)
app.MountTransientStores(tkeys)
2019-07-18 17:36:31 +00:00
// initialize the app
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 {
2019-09-11 22:33:20 +00:00
err := app.LoadLatestVersion(app.keys[bam.MainStoreKey])
2019-06-20 13:37:57 +00:00
if err != nil {
cmn.Exit(err.Error())
}
}
return app
}
// custom tx codec
func MakeCodec() *codec.Codec {
var cdc = codec.New()
2019-09-11 22:33:20 +00:00
2019-07-18 17:36:31 +00:00
ModuleBasics.RegisterCodec(cdc)
2019-10-04 17:55:49 +00:00
vesting.RegisterCodec(cdc)
2019-06-20 13:37:57 +00:00
sdk.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
2019-09-11 22:33:20 +00:00
codec.RegisterEvidences(cdc)
return cdc.Seal()
2019-06-20 13:37:57 +00:00
}
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-09-11 22:33:20 +00:00
return app.LoadVersion(height, app.keys[bam.MainStoreKey])
2019-06-20 13:37:57 +00:00
}
2019-07-18 17:36:31 +00:00
// 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 {
2019-09-11 22:33:20 +00:00
modAccAddrs[supply.NewModuleAddress(acc).String()] = true
2019-07-18 17:36:31 +00:00
}
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
}
2019-09-11 22:33:20 +00:00
// Codec returns the application's sealed codec.
func (app *App) Codec() *codec.Codec {
return app.cdc
}
// GetMaccPerms returns a mapping of the application's module account permissions.
func GetMaccPerms() map[string][]string {
perms := make(map[string][]string)
for k, v := range mAccPerms {
perms[k] = v
}
return perms
}