Skip to content

Commit

Permalink
Added enabled-precompiles param validation in InitGenesis
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniy-scherbina committed May 1, 2024
1 parent f385a8d commit 6e25d37
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
12 changes: 10 additions & 2 deletions x/evm/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
}
Expand Down
21 changes: 19 additions & 2 deletions x/evm/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
62 changes: 61 additions & 1 deletion x/evm/types/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
}
}

0 comments on commit 6e25d37

Please sign in to comment.