diff --git a/x/evm/genesis.go b/x/evm/genesis.go index 5df8066d8d..86b0075ea2 100644 --- a/x/evm/genesis.go +++ b/x/evm/genesis.go @@ -24,7 +24,7 @@ import ( authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" - + precompile_modules "github.com/ethereum/go-ethereum/precompile/modules" ethermint "github.com/evmos/ethermint/types" "github.com/evmos/ethermint/x/evm/keeper" "github.com/evmos/ethermint/x/evm/types" @@ -39,7 +39,15 @@ func InitGenesis( ) []abci.ValidatorUpdate { k.WithChainID(ctx) - err := k.SetParams(ctx, data.Params) + err := types.CheckIfEnabledPrecompilesAreRegistered( + precompile_modules.RegisteredModules(), + data.Params.GetEnabledPrecompiles(), + ) + if err != nil { + panic(err) + } + + err = k.SetParams(ctx, data.Params) if err != nil { panic(fmt.Errorf("error setting params %s", err)) } diff --git a/x/evm/types/params.go b/x/evm/types/params.go index c931075d7a..d5940b8f5c 100644 --- a/x/evm/types/params.go +++ b/x/evm/types/params.go @@ -19,11 +19,12 @@ import ( "fmt" "math/big" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/params" + precompile_modules "github.com/ethereum/go-ethereum/precompile/modules" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/ethereum/go-ethereum/core/vm" "github.com/evmos/ethermint/types" ) @@ -209,3 +210,19 @@ func validateEnabledPrecompiles(enabledPrecompiles []string) error { func IsLondon(ethConfig *params.ChainConfig, height int64) bool { return ethConfig.IsLondon(big.NewInt(height)) } + +func CheckIfEnabledPrecompilesAreRegistered(registeredModules []precompile_modules.Module, enabledPrecompiles []string) error { + registeredAddrs := make(map[string]struct{}, len(registeredModules)) + + for _, module := range registeredModules { + registeredAddrs[module.Address.String()] = struct{}{} + } + + for _, enabledPrecompile := range enabledPrecompiles { + if _, ok := registeredAddrs[enabledPrecompile]; !ok { + return fmt.Errorf("precompile %v is enabled but not registered", enabledPrecompile) + } + } + + return nil +} diff --git a/x/evm/types/params_test.go b/x/evm/types/params_test.go index b9947c4634..f7fbdb16ce 100644 --- a/x/evm/types/params_test.go +++ b/x/evm/types/params_test.go @@ -3,8 +3,9 @@ package types import ( "testing" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/params" - + precompile_modules "github.com/ethereum/go-ethereum/precompile/modules" "github.com/stretchr/testify/require" ) @@ -169,3 +170,62 @@ func TestIsLondon(t *testing.T) { require.Equal(t, IsLondon(ethConfig, tc.height), tc.result) } } + +func TestCheckIfEnabledPrecompilesAreRegistered(t *testing.T) { + m := func(addr string) precompile_modules.Module { + return precompile_modules.Module{ + Address: common.HexToAddress(addr), + } + } + a := func(addr string) string { + return common.HexToAddress(addr).String() + } + + testCases := []struct { + name string + registeredModules []precompile_modules.Module + enabledPrecompiles []string + expError bool + }{ + { + name: "test-case #1", + registeredModules: []precompile_modules.Module{m("0x1"), m("0x2"), m("0x3")}, + enabledPrecompiles: []string{a("0x1"), a("0x2"), a("0x3")}, + expError: false, + }, + { + name: "test-case #2", + registeredModules: []precompile_modules.Module{m("0x1"), m("0x2"), m("0x3")}, + enabledPrecompiles: []string{a("0x1"), a("0x3")}, + expError: false, + }, + { + name: "test-case #3", + registeredModules: []precompile_modules.Module{m("0x1"), m("0x2"), m("0x3")}, + enabledPrecompiles: []string{}, + expError: false, + }, + { + name: "test-case #4", + registeredModules: []precompile_modules.Module{}, + enabledPrecompiles: []string{}, + expError: false, + }, + { + name: "test-case #5", + registeredModules: []precompile_modules.Module{m("0x1"), m("0x2"), m("0x3")}, + enabledPrecompiles: []string{"0x4"}, + expError: true, + }, + } + + for _, tc := range testCases { + err := CheckIfEnabledPrecompilesAreRegistered(tc.registeredModules, tc.enabledPrecompiles) + + if tc.expError { + require.Error(t, err, tc.name) + } else { + require.NoError(t, err, tc.name) + } + } +}