Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
fix(evm): skip hash check when the code has been deleted (#1320)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Federico Kunze Küllmer <[email protected]>
  • Loading branch information
3 people authored Oct 19, 2022
1 parent 236ca33 commit 5879750
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions x/evm/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions x/evm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 := &etherminttypes.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 {
Expand Down

0 comments on commit 5879750

Please sign in to comment.