2019-07-08 16:02:20 +00:00
|
|
|
package evm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-10-04 19:32:56 +00:00
|
|
|
"math/big"
|
2019-08-14 23:52:45 +00:00
|
|
|
|
2019-07-08 16:02:20 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/context"
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2019-07-25 20:38:55 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
|
|
|
"github.com/cosmos/ethermint/x/evm/client/cli"
|
2020-03-09 13:17:23 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/keeper"
|
2019-07-08 16:02:20 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
2019-10-04 19:32:56 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2019-07-08 16:02:20 +00:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/spf13/cobra"
|
2019-07-25 20:38:55 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2019-07-08 16:02:20 +00:00
|
|
|
)
|
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
var _ module.AppModuleBasic = AppModuleBasic{}
|
|
|
|
var _ module.AppModule = AppModule{}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// AppModuleBasic struct
|
2019-07-08 16:02:20 +00:00
|
|
|
type AppModuleBasic struct{}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// Name for app module basic
|
2019-07-08 16:02:20 +00:00
|
|
|
func (AppModuleBasic) Name() string {
|
|
|
|
return types.ModuleName
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// RegisterCodec registers types for module
|
2019-07-08 16:02:20 +00:00
|
|
|
func (AppModuleBasic) RegisterCodec(cdc *codec.Codec) {
|
|
|
|
types.RegisterCodec(cdc)
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// DefaultGenesis is json default structure
|
2019-07-08 16:02:20 +00:00
|
|
|
func (AppModuleBasic) DefaultGenesis() json.RawMessage {
|
2020-03-09 13:17:23 +00:00
|
|
|
return types.ModuleCdc.MustMarshalJSON(types.DefaultGenesisState())
|
2019-07-08 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// ValidateGenesis is the validation check of the Genesis
|
2019-07-08 16:02:20 +00:00
|
|
|
func (AppModuleBasic) ValidateGenesis(bz json.RawMessage) error {
|
2020-03-09 13:17:23 +00:00
|
|
|
var data types.GenesisState
|
2019-07-08 16:02:20 +00:00
|
|
|
err := types.ModuleCdc.UnmarshalJSON(bz, &data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Once json successfully marshalled, passes along to genesis.go
|
2020-03-09 13:17:23 +00:00
|
|
|
return types.ValidateGenesis(data)
|
2019-07-08 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// RegisterRESTRoutes Registers rest routes
|
2019-07-08 16:02:20 +00:00
|
|
|
func (AppModuleBasic) RegisterRESTRoutes(ctx context.CLIContext, rtr *mux.Router) {
|
|
|
|
//rpc.RegisterRoutes(ctx, rtr, StoreKey)
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// GetQueryCmd Gets the root query command of this module
|
2019-07-08 16:02:20 +00:00
|
|
|
func (AppModuleBasic) GetQueryCmd(cdc *codec.Codec) *cobra.Command {
|
2019-07-25 20:38:55 +00:00
|
|
|
return cli.GetQueryCmd(types.ModuleName, cdc)
|
2019-07-08 16:02:20 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// GetTxCmd Gets the root tx command of this module
|
2019-07-08 16:02:20 +00:00
|
|
|
func (AppModuleBasic) GetTxCmd(cdc *codec.Codec) *cobra.Command {
|
2019-09-15 16:12:59 +00:00
|
|
|
return cli.GetTxCmd(types.ModuleName, cdc)
|
2019-07-08 16:02:20 +00:00
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// AppModule is struct that defines variables used within module
|
2019-07-25 20:38:55 +00:00
|
|
|
type AppModule struct {
|
|
|
|
AppModuleBasic
|
|
|
|
keeper Keeper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAppModule creates a new AppModule Object
|
2020-03-09 13:17:23 +00:00
|
|
|
func NewAppModule(k Keeper) AppModule {
|
2019-07-25 20:38:55 +00:00
|
|
|
return AppModule{
|
|
|
|
AppModuleBasic: AppModuleBasic{},
|
2020-03-09 13:17:23 +00:00
|
|
|
keeper: k,
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// Name is module name
|
2019-07-25 20:38:55 +00:00
|
|
|
func (AppModule) Name() string {
|
|
|
|
return types.ModuleName
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// RegisterInvariants interface for registering invariants
|
2019-07-25 20:38:55 +00:00
|
|
|
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// Route specifies path for transactions
|
2019-07-25 20:38:55 +00:00
|
|
|
func (am AppModule) Route() string {
|
|
|
|
return types.RouterKey
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// NewHandler sets up a new handler for module
|
2019-07-25 20:38:55 +00:00
|
|
|
func (am AppModule) NewHandler() sdk.Handler {
|
2019-08-14 23:52:45 +00:00
|
|
|
return NewHandler(am.keeper)
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2019-09-26 15:54:23 +00:00
|
|
|
|
|
|
|
// QuerierRoute sets up path for queries
|
2019-07-25 20:38:55 +00:00
|
|
|
func (am AppModule) QuerierRoute() string {
|
|
|
|
return types.ModuleName
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// NewQuerierHandler sets up new querier handler for module
|
2019-07-25 20:38:55 +00:00
|
|
|
func (am AppModule) NewQuerierHandler() sdk.Querier {
|
2020-03-09 13:17:23 +00:00
|
|
|
return keeper.NewQuerier(am.keeper)
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// BeginBlock function for module at start of each block
|
2019-09-27 14:08:45 +00:00
|
|
|
func (am AppModule) BeginBlock(ctx sdk.Context, bl abci.RequestBeginBlock) {
|
|
|
|
// Consider removing this when using evm as module without web3 API
|
2020-03-09 13:17:23 +00:00
|
|
|
bloom := ethtypes.BytesToBloom(am.keeper.Bloom.Bytes())
|
2019-10-04 19:32:56 +00:00
|
|
|
am.keeper.SetBlockBloomMapping(ctx, bloom, bl.Header.GetHeight()-1)
|
2019-09-27 14:08:45 +00:00
|
|
|
am.keeper.SetBlockHashMapping(ctx, bl.Header.LastBlockId.GetHash(), bl.Header.GetHeight()-1)
|
2020-03-09 13:17:23 +00:00
|
|
|
am.keeper.Bloom = big.NewInt(0)
|
|
|
|
am.keeper.TxCount.Reset()
|
2019-09-27 14:08:45 +00:00
|
|
|
}
|
2019-07-25 20:38:55 +00:00
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// EndBlock function for module at end of block
|
2019-09-18 13:51:18 +00:00
|
|
|
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
|
2019-10-18 23:14:38 +00:00
|
|
|
// Gas costs are handled within msg handler so costs should be ignored
|
|
|
|
ebCtx := ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
// Update account balances before committing other parts of state
|
2020-03-09 13:17:23 +00:00
|
|
|
am.keeper.CommitStateDB.UpdateAccounts()
|
2019-11-15 17:02:13 +00:00
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
// Commit state objects to KV store
|
2020-03-09 13:17:23 +00:00
|
|
|
_, err := am.keeper.CommitStateDB.WithContext(ebCtx).Commit(true)
|
2019-09-26 15:54:23 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-11-15 17:02:13 +00:00
|
|
|
// Clear accounts cache after account data has been committed
|
2020-03-09 13:17:23 +00:00
|
|
|
am.keeper.CommitStateDB.ClearStateObjects()
|
2019-11-15 17:02:13 +00:00
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
return []abci.ValidatorUpdate{}
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// InitGenesis instantiates the genesis state
|
2019-07-25 20:38:55 +00:00
|
|
|
func (am AppModule) InitGenesis(ctx sdk.Context, data json.RawMessage) []abci.ValidatorUpdate {
|
2020-03-09 13:17:23 +00:00
|
|
|
var genesisState types.GenesisState
|
2019-07-25 20:38:55 +00:00
|
|
|
types.ModuleCdc.MustUnmarshalJSON(data, &genesisState)
|
|
|
|
return InitGenesis(ctx, am.keeper, genesisState)
|
|
|
|
}
|
|
|
|
|
2019-09-26 15:54:23 +00:00
|
|
|
// ExportGenesis exports the genesis state to be used by daemon
|
2019-07-25 20:38:55 +00:00
|
|
|
func (am AppModule) ExportGenesis(ctx sdk.Context) json.RawMessage {
|
|
|
|
gs := ExportGenesis(ctx, am.keeper)
|
|
|
|
return types.ModuleCdc.MustMarshalJSON(gs)
|
|
|
|
}
|