From 5879750b301526a94b13b9fd9ddf55ba4e224cc4 Mon Sep 17 00:00:00 2001 From: JayT106 Date: Wed, 19 Oct 2022 08:29:24 -0400 Subject: [PATCH] fix(evm): skip hash check when the code has been deleted (#1320) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * evm.InitGenesis skip codehash check when the code has been deleted in the evm state * fix lint * just ignore all the codehash check when the evm account code is empty * update changelog * nit * add test * add test Co-authored-by: Daniel Burckhardt Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- CHANGELOG.md | 1 + x/evm/genesis.go | 9 ++++++--- x/evm/genesis_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b60368fc..c75f7aaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (rpc) [#1340](https://github.com/evmos/ethermint/pull/1340) Fix error response when `eth_estimateGas` height provided is not found. * (rpc) [#1354](https://github.com/evmos/ethermint/pull/1354) Fix grpc query failure(`BaseFee` and `EthCall`) on legacy block states. * (cli) [#1362](https://github.com/evmos/ethermint/pull/1362) Fix `index-eth-tx` error when the indexer db is empty. +* (state) [#1320](https://github.com/evmos/ethermint/pull/1320) Fix codehash check mismatch when the code has been deleted in the evm state. ## [v0.19.2] - 2022-08-29 diff --git a/x/evm/genesis.go b/x/evm/genesis.go index a4440c61..b293cc12 100644 --- a/x/evm/genesis.go +++ b/x/evm/genesis.go @@ -48,11 +48,14 @@ func InitGenesis( ), ) } - code := common.Hex2Bytes(account.Code) codeHash := crypto.Keccak256Hash(code) - if !bytes.Equal(ethAcct.GetCodeHash().Bytes(), codeHash.Bytes()) { - panic("code don't match codeHash") + + // we ignore the empty Code hash checking, see ethermint PR#1234 + if len(account.Code) != 0 && !bytes.Equal(ethAcct.GetCodeHash().Bytes(), codeHash.Bytes()) { + s := "the evm state code doesn't match with the codehash\n" + panic(fmt.Sprintf("%s account: %s , evm state codehash: %v, ethAccount codehash: %v, evm state code: %s\n", + s, account.Address, codeHash, ethAcct.GetCodeHash(), account.Code)) } k.SetCode(ctx, codeHash.Bytes(), code) diff --git a/x/evm/genesis_test.go b/x/evm/genesis_test.go index 4be1c74c..e060c5d9 100644 --- a/x/evm/genesis_test.go +++ b/x/evm/genesis_test.go @@ -7,6 +7,7 @@ import ( 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" "github.com/evmos/ethermint/x/evm/statedb" "github.com/evmos/ethermint/x/evm/types" @@ -96,6 +97,45 @@ func (suite *EvmTestSuite) TestInitGenesis() { }, true, }, + { + "ignore empty account code checking", + 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: "", + }, + }, + }, + false, + }, + { + "ignore empty account code checking with non-empty codehash", + func() { + ethAcc := ðerminttypes.EthAccount{ + BaseAccount: authtypes.NewBaseAccount(address.Bytes(), nil, 0, 0), + CodeHash: common.BytesToHash([]byte{1, 2, 3}).Hex(), + } + + suite.app.AccountKeeper.SetAccount(suite.ctx, ethAcc) + }, + &types.GenesisState{ + Params: types.DefaultParams(), + Accounts: []types.GenesisAccount{ + { + Address: address.String(), + Code: "", + }, + }, + }, + false, + }, } for _, tc := range testCases {