2018-07-19 21:16:48 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-07-12 17:13:15 +00:00
|
|
|
"encoding/json"
|
2019-07-05 16:46:13 +00:00
|
|
|
"os"
|
|
|
|
|
2018-07-19 21:16:48 +00:00
|
|
|
bam "github.com/cosmos/cosmos-sdk/baseapp"
|
2018-10-24 14:49:37 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-08-09 01:06:34 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-07-08 19:26:33 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2019-07-12 17:13:15 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
2018-08-09 01:06:34 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2019-07-04 19:46:54 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
2019-07-12 17:13:15 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/crisis"
|
2019-07-04 19:46:54 +00:00
|
|
|
distr "github.com/cosmos/cosmos-sdk/x/distribution"
|
2019-07-12 17:13:15 +00:00
|
|
|
distrclient "github.com/cosmos/cosmos-sdk/x/distribution/client"
|
2019-07-08 19:26:33 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/genaccounts"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil"
|
2018-08-24 18:56:43 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov"
|
2019-07-04 19:46:54 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/mint"
|
2018-08-24 18:56:43 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/params"
|
2019-07-12 17:13:15 +00:00
|
|
|
paramsclient "github.com/cosmos/cosmos-sdk/x/params/client"
|
2018-08-24 18:56:43 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing"
|
2019-07-04 19:46:54 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/supply"
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
"github.com/cosmos/ethermint/crypto"
|
2018-11-28 22:19:22 +00:00
|
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
2018-08-09 01:06:34 +00:00
|
|
|
|
2018-08-24 18:56:43 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-08-09 01:06:34 +00:00
|
|
|
dbm "github.com/tendermint/tendermint/libs/db"
|
|
|
|
tmlog "github.com/tendermint/tendermint/libs/log"
|
2019-07-12 17:13:15 +00:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2018-07-19 21:16:48 +00:00
|
|
|
)
|
|
|
|
|
2018-11-28 22:19:22 +00:00
|
|
|
const appName = "Ethermint"
|
|
|
|
|
|
|
|
var (
|
2019-07-05 16:46:13 +00:00
|
|
|
// default home directories for the application CLI
|
|
|
|
DefaultCLIHome = os.ExpandEnv("$HOME/.emintcli")
|
|
|
|
|
2019-07-08 19:26:33 +00:00
|
|
|
// DefaultNodeHome sets the folder where the applcation data and configuration will be stored
|
|
|
|
DefaultNodeHome = os.ExpandEnv("$HOME/.emintd")
|
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// The module BasicManager is in charge of setting up basic,
|
|
|
|
// non-dependant module elements, such as codec registration
|
|
|
|
// and genesis verification.
|
2019-07-08 19:26:33 +00:00
|
|
|
ModuleBasics = module.NewBasicManager(
|
|
|
|
genaccounts.AppModuleBasic{},
|
|
|
|
genutil.AppModuleBasic{},
|
|
|
|
auth.AppModuleBasic{},
|
|
|
|
bank.AppModuleBasic{},
|
|
|
|
staking.AppModuleBasic{},
|
2019-07-12 17:13:15 +00:00
|
|
|
mint.AppModuleBasic{},
|
2019-07-08 19:26:33 +00:00
|
|
|
distr.AppModuleBasic{},
|
2019-07-12 17:13:15 +00:00
|
|
|
gov.NewAppModuleBasic(paramsclient.ProposalHandler, distrclient.ProposalHandler),
|
|
|
|
params.AppModuleBasic{},
|
|
|
|
crisis.AppModuleBasic{},
|
2019-07-08 19:26:33 +00:00
|
|
|
slashing.AppModuleBasic{},
|
2019-07-12 17:13:15 +00:00
|
|
|
supply.AppModuleBasic{},
|
2019-07-08 19:26:33 +00:00
|
|
|
)
|
2018-07-19 21:16:48 +00:00
|
|
|
)
|
|
|
|
|
2019-07-08 19:26:33 +00:00
|
|
|
// MakeCodec generates the necessary codecs for Amino
|
|
|
|
func MakeCodec() *codec.Codec {
|
|
|
|
var cdc = codec.New()
|
2019-07-12 17:13:15 +00:00
|
|
|
|
|
|
|
// TODO: Move this codec to module (Issue #12 https://github.com/ChainSafe/ethermint/issues/12)
|
|
|
|
crypto.RegisterCodec(cdc)
|
|
|
|
|
2019-07-08 19:26:33 +00:00
|
|
|
ModuleBasics.RegisterCodec(cdc)
|
|
|
|
sdk.RegisterCodec(cdc)
|
|
|
|
codec.RegisterCrypto(cdc)
|
|
|
|
return cdc
|
|
|
|
}
|
|
|
|
|
|
|
|
// EthermintApp implements an extended ABCI application. It is an application
|
|
|
|
// that may process transactions through Ethereum's EVM running atop of
|
|
|
|
// Tendermint consensus.
|
|
|
|
type EthermintApp struct {
|
|
|
|
*bam.BaseApp
|
|
|
|
cdc *codec.Codec
|
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
invCheckPeriod uint
|
|
|
|
|
|
|
|
// keys to access the substores
|
|
|
|
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
|
|
|
|
// TODO: Add evm module key
|
|
|
|
|
|
|
|
// keepers
|
2019-07-08 19:26:33 +00:00
|
|
|
accountKeeper auth.AccountKeeper
|
|
|
|
bankKeeper bank.Keeper
|
2019-07-12 17:13:15 +00:00
|
|
|
supplyKeeper supply.Keeper
|
|
|
|
stakingKeeper staking.Keeper
|
2019-07-08 19:26:33 +00:00
|
|
|
slashingKeeper slashing.Keeper
|
2019-07-12 17:13:15 +00:00
|
|
|
mintKeeper mint.Keeper
|
|
|
|
distrKeeper distr.Keeper
|
2019-07-08 19:26:33 +00:00
|
|
|
govKeeper gov.Keeper
|
2019-07-12 17:13:15 +00:00
|
|
|
crisisKeeper crisis.Keeper
|
2019-07-08 19:26:33 +00:00
|
|
|
paramsKeeper params.Keeper
|
2019-07-12 17:13:15 +00:00
|
|
|
// TODO: Include evm Keeper
|
|
|
|
|
|
|
|
// the module manager
|
|
|
|
mm *module.Manager
|
2019-07-08 19:26:33 +00:00
|
|
|
}
|
2018-07-19 21:16:48 +00:00
|
|
|
|
|
|
|
// NewEthermintApp returns a reference to a new initialized Ethermint
|
|
|
|
// application.
|
2018-11-28 22:19:22 +00:00
|
|
|
//
|
|
|
|
// TODO: Ethermint needs to support being bootstrapped as an application running
|
|
|
|
// in a sovereign zone and as an application running with a shared security model.
|
|
|
|
// For now, it will support only running as a sovereign application.
|
2019-07-12 17:13:15 +00:00
|
|
|
func NewEthermintApp(logger tmlog.Logger, db dbm.DB, loadLatest bool,
|
|
|
|
invCheckPeriod uint, baseAppOptions ...func(*bam.BaseApp)) *EthermintApp {
|
|
|
|
cdc := MakeCodec()
|
|
|
|
|
|
|
|
baseApp := bam.NewBaseApp(appName, logger, db, evmtypes.TxDecoder(cdc), baseAppOptions...)
|
|
|
|
baseApp.SetAppVersion(version.Version)
|
|
|
|
|
|
|
|
var app = &EthermintApp{
|
|
|
|
BaseApp: baseApp,
|
|
|
|
cdc: cdc,
|
|
|
|
invCheckPeriod: invCheckPeriod,
|
|
|
|
keyMain: sdk.NewKVStoreKey(bam.MainStoreKey),
|
|
|
|
keyAccount: sdk.NewKVStoreKey(auth.StoreKey),
|
|
|
|
keyStaking: sdk.NewKVStoreKey(staking.StoreKey),
|
|
|
|
keySupply: sdk.NewKVStoreKey(supply.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),
|
|
|
|
// TODO: Initialize evm module key
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// init params keeper and subspaces
|
|
|
|
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams, app.tkeyParams, params.DefaultCodespace)
|
2019-07-04 19:46:54 +00:00
|
|
|
authSubspace := app.paramsKeeper.Subspace(auth.DefaultParamspace)
|
|
|
|
bankSubspace := app.paramsKeeper.Subspace(bank.DefaultParamspace)
|
2019-07-12 17:13:15 +00:00
|
|
|
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)
|
2019-07-04 19:46:54 +00:00
|
|
|
|
|
|
|
// account permissions
|
2019-07-11 18:05:34 +00:00
|
|
|
maccPerms := map[string][]string{
|
|
|
|
auth.FeeCollectorName: []string{supply.Basic},
|
|
|
|
distr.ModuleName: []string{supply.Basic},
|
|
|
|
mint.ModuleName: []string{supply.Minter},
|
|
|
|
staking.BondedPoolName: []string{supply.Burner, supply.Staking},
|
|
|
|
staking.NotBondedPoolName: []string{supply.Burner, supply.Staking},
|
|
|
|
gov.ModuleName: []string{supply.Burner},
|
|
|
|
}
|
2019-07-04 19:46:54 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// add keepers
|
|
|
|
app.accountKeeper = auth.NewAccountKeeper(app.cdc, app.keyAccount, authSubspace, auth.ProtoBaseAccount)
|
2019-07-04 19:46:54 +00:00
|
|
|
app.bankKeeper = bank.NewBaseKeeper(app.accountKeeper, bankSubspace, bank.DefaultCodespace)
|
2019-07-12 17:13:15 +00:00
|
|
|
app.supplyKeeper = supply.NewKeeper(app.cdc, app.keySupply, app.accountKeeper, app.bankKeeper, supply.DefaultCodespace, maccPerms)
|
|
|
|
stakingKeeper := staking.NewKeeper(app.cdc, app.keyStaking, app.tkeyStaking,
|
|
|
|
app.supplyKeeper, stakingSubspace, staking.DefaultCodespace)
|
|
|
|
app.mintKeeper = mint.NewKeeper(app.cdc, app.keyMint, mintSubspace, &stakingKeeper, app.supplyKeeper, auth.FeeCollectorName)
|
|
|
|
app.distrKeeper = distr.NewKeeper(app.cdc, app.keyDistr, distrSubspace, &stakingKeeper,
|
|
|
|
app.supplyKeeper, distr.DefaultCodespace, auth.FeeCollectorName)
|
|
|
|
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, &stakingKeeper,
|
|
|
|
slashingSubspace, slashing.DefaultCodespace)
|
|
|
|
app.crisisKeeper = crisis.NewKeeper(crisisSubspace, invCheckPeriod, app.supplyKeeper, auth.FeeCollectorName)
|
|
|
|
// TODO: Instantiate evm Keeper
|
|
|
|
|
|
|
|
// register the proposal types
|
|
|
|
govRouter := gov.NewRouter()
|
|
|
|
govRouter.AddRoute(gov.RouterKey, gov.ProposalHandler).
|
|
|
|
AddRoute(params.RouterKey, params.NewParamChangeProposalHandler(app.paramsKeeper)).
|
|
|
|
AddRoute(distr.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.distrKeeper))
|
|
|
|
app.govKeeper = gov.NewKeeper(app.cdc, app.keyGov, app.paramsKeeper, govSubspace,
|
|
|
|
app.supplyKeeper, &stakingKeeper, gov.DefaultCodespace, govRouter)
|
|
|
|
|
|
|
|
// register the staking hooks
|
|
|
|
// NOTE: stakingKeeper above is passed by reference, so that it will contain these hooks
|
|
|
|
app.stakingKeeper = *stakingKeeper.SetHooks(
|
|
|
|
staking.NewMultiStakingHooks(app.distrKeeper.Hooks(), app.slashingKeeper.Hooks()))
|
|
|
|
|
|
|
|
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),
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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, supply.ModuleName, distr.ModuleName,
|
|
|
|
staking.ModuleName, auth.ModuleName, bank.ModuleName, slashing.ModuleName,
|
|
|
|
gov.ModuleName, mint.ModuleName, crisis.ModuleName, genutil.ModuleName)
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
app.mm.RegisterInvariants(&app.crisisKeeper)
|
|
|
|
app.mm.RegisterRoutes(app.Router(), app.QueryRouter())
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// initialize stores
|
|
|
|
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)
|
|
|
|
|
|
|
|
// initialize BaseApp
|
|
|
|
app.SetInitChainer(app.InitChainer)
|
2018-08-24 18:56:43 +00:00
|
|
|
app.SetBeginBlocker(app.BeginBlocker)
|
2019-07-12 17:13:15 +00:00
|
|
|
app.SetAnteHandler(auth.NewAnteHandler(app.accountKeeper, app.supplyKeeper, auth.DefaultSigVerificationGasConsumer))
|
2018-08-24 18:56:43 +00:00
|
|
|
app.SetEndBlocker(app.EndBlocker)
|
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
if loadLatest {
|
|
|
|
err := app.LoadLatestVersion(app.keyMain)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
|
|
|
return app
|
2019-07-12 17:13:15 +00:00
|
|
|
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
2018-07-19 21:16:48 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// The genesis state of the blockchain is represented here as a map of raw json
|
|
|
|
// messages key'd by a identifier string.
|
|
|
|
type GenesisState map[string]json.RawMessage
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// application updates every begin block
|
|
|
|
func (app *EthermintApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
|
|
|
|
return app.mm.BeginBlock(ctx, req)
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// application updates every end block
|
|
|
|
func (app *EthermintApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
|
|
|
|
return app.mm.EndBlock(ctx, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// application update at chain initialization
|
|
|
|
func (app *EthermintApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
|
|
|
|
var genesisState GenesisState
|
|
|
|
app.cdc.MustUnmarshalJSON(req.AppStateBytes, &genesisState)
|
|
|
|
return app.mm.InitGenesis(ctx, genesisState)
|
|
|
|
}
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// load a particular height
|
|
|
|
func (app *EthermintApp) LoadHeight(height int64) error {
|
|
|
|
return app.LoadVersion(height, app.keyMain)
|
2018-08-24 18:56:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// ExportAppStateAndValidators exports the state of the application for a genesis
|
|
|
|
// file.
|
|
|
|
func (app *EthermintApp) ExportAppStateAndValidators(forZeroHeight bool, jailWhiteList []string,
|
|
|
|
) (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// Creates context with current height and checks txs for ctx to be usable by start of next block
|
|
|
|
ctx := app.NewContext(true, abci.Header{Height: app.LastBlockHeight()})
|
2018-08-24 18:56:43 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// Export genesis to be used by SDK modules
|
|
|
|
genState := app.mm.ExportGenesis(ctx)
|
|
|
|
appState, err = codec.MarshalJSONIndent(app.cdc, genState)
|
2018-08-09 01:06:34 +00:00
|
|
|
if err != nil {
|
2019-07-12 17:13:15 +00:00
|
|
|
return nil, nil, err
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
// Write validators to staking module to be used by TM node
|
|
|
|
validators = staking.WriteValidators(ctx, app.stakingKeeper)
|
2018-07-19 21:16:48 +00:00
|
|
|
|
2019-07-12 17:13:15 +00:00
|
|
|
return appState, validators, nil
|
2018-08-09 01:06:34 +00:00
|
|
|
}
|