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

Validate code hash in GenesisAccount #873

Merged
merged 2 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 9 additions & 2 deletions x/evm/genesis.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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))
Expand Down
17 changes: 17 additions & 0 deletions x/evm/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down