Skip to content

Commit

Permalink
Validate code hash in GenesisAccount
Browse files Browse the repository at this point in the history
Closes: evmos#872
  • Loading branch information
yihuang committed Jan 4, 2022
1 parent 7c53e32 commit 1960ea6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
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

0 comments on commit 1960ea6

Please sign in to comment.