diff --git a/x/evm/keeper/msg_server_test.go b/x/evm/keeper/msg_server_test.go index 4a8c86cefa..e9062d4605 100644 --- a/x/evm/keeper/msg_server_test.go +++ b/x/evm/keeper/msg_server_test.go @@ -3,8 +3,10 @@ package keeper_test import ( "math/big" + sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/params" @@ -12,6 +14,10 @@ import ( "github.com/evmos/ethermint/x/evm/types" ) +const PrecompileNonce uint64 = 1 + +var PrecompileCode = []byte{0x1} + func (suite *KeeperTestSuite) TestEthereumTx() { var ( err error @@ -115,3 +121,73 @@ func (suite *KeeperTestSuite) TestUpdateParams() { }) } } + +func (suite *KeeperTestSuite) TestInitPrecompiles() { + addr1 := "0x1000000000000000000000000000000000000000" + addr2 := "0x2000000000000000000000000000000000000000" + addr3 := "0x3000000000000000000000000000000000000000" + + testCases := []struct { + name string + enabledPrecompiles []string + // precompiles which must be uninitialized after corresponding test case + uninitialized []string + }{ + { + name: "enable addr1 and addr2", + enabledPrecompiles: []string{addr1, addr2}, + uninitialized: []string{addr3}, + }, + { + name: "enable addr3, and disable the rest", + enabledPrecompiles: []string{addr3}, + uninitialized: []string{addr1, addr2}, + }, + { + name: "no changes", + enabledPrecompiles: []string{addr3}, + uninitialized: []string{addr1, addr2}, + }, + { + name: "enable all precompiles", + enabledPrecompiles: []string{addr1, addr2, addr3}, + uninitialized: []string{}, + }, + { + name: "disable all precompiles", + enabledPrecompiles: []string{}, + uninitialized: []string{addr1, addr2, addr3}, + }, + } + + for _, tc := range testCases { + suite.Run(tc.name, func() { + params := suite.app.EvmKeeper.GetParams(suite.ctx) + params.EnabledPrecompiles = tc.enabledPrecompiles + + _, err := suite.app.EvmKeeper.UpdateParams(sdk.WrapSDKContext(suite.ctx), &types.MsgUpdateParams{ + Authority: authtypes.NewModuleAddress(govtypes.ModuleName).String(), + Params: params, + }) + suite.Require().NoError(err) + + vmdb := suite.StateDB() + + // check that precompiles are initialized + for _, hexAddr := range tc.enabledPrecompiles { + addr := common.HexToAddress(hexAddr) + + suite.Require().Equal(PrecompileNonce, vmdb.GetNonce(addr)) + suite.Require().Equal(PrecompileCode, vmdb.GetCode(addr)) + } + + // check that precompiles are uninitialized + for _, hexAddr := range tc.uninitialized { + addr := common.HexToAddress(hexAddr) + + suite.Require().Equal(uint64(0), vmdb.GetNonce(addr)) + suite.Require().Equal([]byte(nil), vmdb.GetCode(addr)) + } + }) + } +}