laconicd/app/ethermint.go

157 lines
4.8 KiB
Go
Raw Normal View History

package app
import (
bam "github.com/cosmos/cosmos-sdk/baseapp"
2018-10-24 14:49:37 +00:00
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
2018-08-24 18:56:43 +00:00
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/gov"
"github.com/cosmos/cosmos-sdk/x/params"
"github.com/cosmos/cosmos-sdk/x/slashing"
"github.com/cosmos/cosmos-sdk/x/stake"
"github.com/pkg/errors"
"github.com/cosmos/ethermint/handlers"
"github.com/cosmos/ethermint/types"
ethcmn "github.com/ethereum/go-ethereum/common"
2018-08-24 18:56:43 +00:00
abci "github.com/tendermint/tendermint/abci/types"
tmcmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
tmlog "github.com/tendermint/tendermint/libs/log"
)
const (
appName = "Ethermint"
)
type (
// EthermintApp implements an extended ABCI application. It is an application
// that may process transactions through Ethereum's EVM running atop of
// Tendermint consensus.
EthermintApp struct {
*bam.BaseApp
2018-10-24 14:49:37 +00:00
cdc *codec.Codec
2018-08-24 18:56:43 +00:00
accountKey *sdk.KVStoreKey
storageKey *sdk.KVStoreKey
2018-08-24 18:56:43 +00:00
mainKey *sdk.KVStoreKey
stakeKey *sdk.KVStoreKey
slashingKey *sdk.KVStoreKey
govKey *sdk.KVStoreKey
feeCollKey *sdk.KVStoreKey
paramsKey *sdk.KVStoreKey
tParamsKey *sdk.TransientStoreKey
2018-10-24 14:49:37 +00:00
accountKeeper auth.AccountKeeper
2018-08-24 18:56:43 +00:00
feeCollKeeper auth.FeeCollectionKeeper
coinKeeper bank.Keeper
stakeKeeper stake.Keeper
slashingKeeper slashing.Keeper
govKeeper gov.Keeper
paramsKeeper params.Keeper
}
)
// NewEthermintApp returns a reference to a new initialized Ethermint
// application.
func NewEthermintApp(logger tmlog.Logger, db dbm.DB, sdkAddr ethcmn.Address) *EthermintApp {
2018-10-24 14:49:37 +00:00
cdc := CreateCodec()
cms := store.NewCommitMultiStore(db)
baseAppOpts := []func(*bam.BaseApp){
func(bApp *bam.BaseApp) { bApp.SetCMS(cms) },
}
2018-10-24 14:49:37 +00:00
baseApp := bam.NewBaseApp(appName, logger, db, types.TxDecoder(cdc, sdkAddr), baseAppOpts...)
app := &EthermintApp{
BaseApp: baseApp,
2018-10-24 14:49:37 +00:00
cdc: cdc,
accountKey: types.StoreKeyAccount,
storageKey: types.StoreKeyStorage,
mainKey: types.StoreKeyMain,
stakeKey: types.StoreKeyStake,
slashingKey: types.StoreKeySlashing,
govKey: types.StoreKeyGov,
feeCollKey: types.StoreKeyFeeColl,
paramsKey: types.StoreKeyParams,
tParamsKey: types.StoreKeyTransParams,
}
2018-08-24 18:56:43 +00:00
// set application keepers and mappers
2018-10-24 14:49:37 +00:00
app.accountKeeper = auth.NewAccountKeeper(app.cdc, app.accountKey, auth.ProtoBaseAccount)
app.paramsKeeper = params.NewKeeper(app.cdc, app.paramsKey, app.tParamsKey)
app.feeCollKeeper = auth.NewFeeCollectionKeeper(app.cdc, app.feeCollKey)
2018-08-24 18:56:43 +00:00
// register message handlers
app.Router().
// TODO: Do we need to mount bank and IBC handlers? Should be handled
// directly in the EVM.
AddRoute("stake", stake.NewHandler(app.stakeKeeper)).
AddRoute("slashing", slashing.NewHandler(app.slashingKeeper)).
AddRoute("gov", gov.NewHandler(app.govKeeper))
// initialize the underlying ABCI BaseApp
app.SetInitChainer(app.initChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetEndBlocker(app.EndBlocker)
2018-10-24 14:49:37 +00:00
app.SetAnteHandler(handlers.AnteHandler(app.accountKeeper, app.feeCollKeeper))
2018-08-24 18:56:43 +00:00
app.MountStoresIAVL(
app.mainKey, app.accountKey, app.stakeKey, app.slashingKey,
app.govKey, app.feeCollKey, app.paramsKey, app.storageKey,
2018-08-24 18:56:43 +00:00
)
app.MountStore(app.tParamsKey, sdk.StoreTypeTransient)
if err := app.LoadLatestVersion(app.accountKey); err != nil {
tmcmn.Exit(err.Error())
}
2018-08-24 18:56:43 +00:00
app.BaseApp.Seal()
return app
}
2018-08-24 18:56:43 +00:00
// BeginBlocker signals the beginning of a block. It performs application
// updates on the start of every block.
func (app *EthermintApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
2018-10-24 14:21:51 +00:00
return abci.ResponseBeginBlock{}
2018-08-24 18:56:43 +00:00
}
// EndBlocker signals the end of a block. It performs application updates on
// the end of every block.
func (app *EthermintApp) EndBlocker(ctx sdk.Context, _ abci.RequestEndBlock) abci.ResponseEndBlock {
2018-10-24 14:21:51 +00:00
return abci.ResponseEndBlock{}
2018-08-24 18:56:43 +00:00
}
// initChainer initializes the application blockchain with validators and other
// state data from TendermintCore.
func (app *EthermintApp) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
var genesisState GenesisState
stateJSON := req.AppStateBytes
2018-10-24 14:49:37 +00:00
err := app.cdc.UnmarshalJSON(stateJSON, &genesisState)
if err != nil {
2018-08-24 18:56:43 +00:00
panic(errors.Wrap(err, "failed to parse application genesis state"))
}
2018-10-24 14:21:51 +00:00
// TODO: load the genesis accounts
2018-10-24 14:21:51 +00:00
return abci.ResponseInitChain{}
}
// CreateCodec creates a new amino wire codec and registers all the necessary
// concrete types and interfaces needed for the application.
2018-10-24 14:49:37 +00:00
func CreateCodec() *codec.Codec {
cdc := codec.New()
2018-10-24 14:49:37 +00:00
types.RegisterCodec(cdc)
auth.RegisterCodec(cdc)
codec.RegisterCrypto(cdc)
2018-08-24 18:56:43 +00:00
2018-10-24 14:49:37 +00:00
return cdc
}