evm: validate code hash in GenesisAccount (#873)

* Validate code hash in GenesisAccount

Closes: #872

* changelog
This commit is contained in:
yihuang 2022-01-04 22:21:32 +08:00 committed by GitHub
parent 7c53e32c78
commit 8e4ff5aae2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View File

@ -59,6 +59,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (evm) [tharsis#826](https://github.com/tharsis/ethermint/issues/826) Improve allocation of bytes of `tx.To` address.
* (evm) [tharsis#827](https://github.com/tharsis/ethermint/issues/827) Speed up creation of event logs by using the slice insertion idiom with indices.
* (ante) [tharsis#819](https://github.com/tharsis/ethermint/pull/819) remove redundant ante handlers
* (app) [tharsis#873](https://github.com/tharsis/ethermint/pull/873) Validate code hash in GenesisAccount
### Bug Fixes

View File

@ -1,11 +1,13 @@
package evm
import (
"bytes"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
abci "github.com/tendermint/tendermint/abci/types"
ethermint "github.com/tharsis/ethermint/types"
@ -39,7 +41,7 @@ func InitGenesis(
panic(fmt.Errorf("account not found for address %s", account.Address))
}
_, ok := acc.(*ethermint.EthAccount)
ethAcct, ok := acc.(*ethermint.EthAccount)
if !ok {
panic(
fmt.Errorf("account %s must be an %T type, got %T",
@ -48,7 +50,12 @@ func InitGenesis(
)
}
k.SetCode(address, common.Hex2Bytes(account.Code))
code := common.Hex2Bytes(account.Code)
codeHash := crypto.Keccak256Hash(code)
if !bytes.Equal(common.HexToHash(ethAcct.CodeHash).Bytes(), codeHash.Bytes()) {
panic("code don't match codeHash")
}
k.SetCode(address, code)
for _, storage := range account.Storage {
k.SetState(address, common.HexToHash(storage.Key), common.HexToHash(storage.Value))

View File

@ -80,6 +80,23 @@ func (suite *EvmTestSuite) TestInitGenesis() {
},
true,
},
{
"invalid code hash",
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: "ffffffff",
},
},
},
true,
},
}
for _, tc := range testCases {