diff --git a/x/evm/genesis.go b/x/evm/genesis.go index 86b0075ea2..67ef6c33cc 100644 --- a/x/evm/genesis.go +++ b/x/evm/genesis.go @@ -36,11 +36,12 @@ func InitGenesis( k *keeper.Keeper, accountKeeper types.AccountKeeper, data types.GenesisState, + registeredModules []precompile_modules.Module, ) []abci.ValidatorUpdate { k.WithChainID(ctx) err := types.CheckIfEnabledPrecompilesAreRegistered( - precompile_modules.RegisteredModules(), + registeredModules, data.Params.GetEnabledPrecompiles(), ) if err != nil { diff --git a/x/evm/genesis_test.go b/x/evm/genesis_test.go index e060c5d965..4fb5254b0f 100644 --- a/x/evm/genesis_test.go +++ b/x/evm/genesis_test.go @@ -3,9 +3,10 @@ package evm_test import ( "math/big" + authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/ethereum/go-ethereum/common" + precompile_modules "github.com/ethereum/go-ethereum/precompile/modules" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/evmos/ethermint/crypto/ethsecp256k1" etherminttypes "github.com/evmos/ethermint/types" "github.com/evmos/ethermint/x/evm" @@ -18,106 +19,120 @@ func (suite *EvmTestSuite) TestInitGenesis() { suite.Require().NoError(err) address := common.HexToAddress(privkey.PubKey().Address().String()) + hexAddr1 := "0x1000000000000000000000000000000000000000" var vmdb *statedb.StateDB testCases := []struct { - name string - malleate func() - genState *types.GenesisState - expPanic bool + name string + malleate func() + getGenState func() *types.GenesisState + registeredModules []precompile_modules.Module + expPanic bool }{ { - "default", - func() {}, - types.DefaultGenesisState(), - false, + name: "default", + malleate: func() {}, + getGenState: func() *types.GenesisState { + return types.DefaultGenesisState() + }, + expPanic: false, }, { - "valid account", - func() { + name: "valid account", + malleate: func() { vmdb.AddBalance(address, big.NewInt(1)) }, - &types.GenesisState{ - Params: types.DefaultParams(), - Accounts: []types.GenesisAccount{ - { - Address: address.String(), - Storage: types.Storage{ - {Key: common.BytesToHash([]byte("key")).String(), Value: common.BytesToHash([]byte("value")).String()}, + getGenState: func() *types.GenesisState { + return &types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + Storage: types.Storage{ + {Key: common.BytesToHash([]byte("key")).String(), Value: common.BytesToHash([]byte("value")).String()}, + }, }, }, - }, + } }, - false, + expPanic: false, }, { - "account not found", - func() {}, - &types.GenesisState{ - Params: types.DefaultParams(), - Accounts: []types.GenesisAccount{ - { - Address: address.String(), + name: "account not found", + malleate: func() {}, + getGenState: func() *types.GenesisState { + return &types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + }, }, - }, + } }, - true, + expPanic: true, }, { - "invalid account type", - func() { + name: "invalid account type", + malleate: func() { acc := authtypes.NewBaseAccountWithAddress(address.Bytes()) suite.app.AccountKeeper.SetAccount(suite.ctx, acc) }, - &types.GenesisState{ - Params: types.DefaultParams(), - Accounts: []types.GenesisAccount{ - { - Address: address.String(), + getGenState: func() *types.GenesisState { + return &types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + }, }, - }, + } }, - true, + expPanic: true, }, { - "invalid code hash", - func() { + name: "invalid code hash", + malleate: func() { acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes()) suite.app.AccountKeeper.SetAccount(suite.ctx, acc) }, - &types.GenesisState{ - Params: types.DefaultParams(), - Accounts: []types.GenesisAccount{ - { - Address: address.String(), - Code: "ffffffff", + getGenState: func() *types.GenesisState { + return &types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + Code: "ffffffff", + }, }, - }, + } }, - true, + expPanic: true, }, { - "ignore empty account code checking", - func() { + name: "ignore empty account code checking", + malleate: func() { acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, address.Bytes()) suite.app.AccountKeeper.SetAccount(suite.ctx, acc) }, - &types.GenesisState{ - Params: types.DefaultParams(), - Accounts: []types.GenesisAccount{ - { - Address: address.String(), - Code: "", + getGenState: func() *types.GenesisState { + return &types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + Code: "", + }, }, - }, + } }, - false, + expPanic: false, }, { - "ignore empty account code checking with non-empty codehash", - func() { + name: "ignore empty account code checking with non-empty codehash", + malleate: func() { ethAcc := ðerminttypes.EthAccount{ BaseAccount: authtypes.NewBaseAccount(address.Bytes(), nil, 0, 0), CodeHash: common.BytesToHash([]byte{1, 2, 3}).Hex(), @@ -125,16 +140,44 @@ func (suite *EvmTestSuite) TestInitGenesis() { suite.app.AccountKeeper.SetAccount(suite.ctx, ethAcc) }, - &types.GenesisState{ - Params: types.DefaultParams(), - Accounts: []types.GenesisAccount{ - { - Address: address.String(), - Code: "", + getGenState: func() *types.GenesisState { + return &types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + Code: "", + }, }, + } + }, + expPanic: false, + }, + { + name: "precompile is enabled and registered", + malleate: func() {}, + getGenState: func() *types.GenesisState { + defaultGen := types.DefaultGenesisState() + defaultGen.Params.EnabledPrecompiles = []string{hexAddr1} + return defaultGen + }, + registeredModules: []precompile_modules.Module{ + { + Address: common.HexToAddress(hexAddr1), }, }, - false, + expPanic: false, + }, + { + name: "precompile is enabled, but not registered", + malleate: func() {}, + getGenState: func() *types.GenesisState { + defaultGen := types.DefaultGenesisState() + defaultGen.Params.EnabledPrecompiles = []string{hexAddr1} + return defaultGen + }, + registeredModules: nil, + expPanic: true, }, } @@ -149,13 +192,13 @@ func (suite *EvmTestSuite) TestInitGenesis() { if tc.expPanic { suite.Require().Panics( func() { - _ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState) + _ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.getGenState(), tc.registeredModules) }, ) } else { suite.Require().NotPanics( func() { - _ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.genState) + _ = evm.InitGenesis(suite.ctx, suite.app.EvmKeeper, suite.app.AccountKeeper, *tc.getGenState(), tc.registeredModules) }, ) } diff --git a/x/evm/module.go b/x/evm/module.go index 80ac05269b..353f887a96 100644 --- a/x/evm/module.go +++ b/x/evm/module.go @@ -20,18 +20,17 @@ import ( "encoding/json" "fmt" - "github.com/gorilla/mux" - "github.com/grpc-ecosystem/grpc-gateway/runtime" - "github.com/spf13/cobra" - abci "github.com/cometbft/cometbft/abci/types" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/codec" codectypes "github.com/cosmos/cosmos-sdk/codec/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + precompile_modules "github.com/ethereum/go-ethereum/precompile/modules" + "github.com/gorilla/mux" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/spf13/cobra" "github.com/evmos/ethermint/x/evm/client/cli" "github.com/evmos/ethermint/x/evm/keeper" @@ -166,7 +165,7 @@ func (am AppModule) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) []abci.V func (am AppModule) InitGenesis(ctx sdk.Context, cdc codec.JSONCodec, data json.RawMessage) []abci.ValidatorUpdate { var genesisState types.GenesisState cdc.MustUnmarshalJSON(data, &genesisState) - InitGenesis(ctx, am.keeper, am.ak, genesisState) + InitGenesis(ctx, am.keeper, am.ak, genesisState, precompile_modules.RegisteredModules()) return []abci.ValidatorUpdate{} }