From d6a64a275a4fd7a847e77537b3402b09f2e09d24 Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 27 Sep 2021 18:26:45 +0800 Subject: [PATCH] evm: fix import/export genesis for contract storage (#590) * Problem: import/export roundtrip test fail contract storage Closes: #589 - don't hash the key again in InitGenesis * changelog * try to fix estimate-gas undeterministics Closes #536 --- CHANGELOG.md | 1 + x/evm/keeper/grpc_query_test.go | 3 +-- x/evm/keeper/keeper.go | 1 - x/evm/keeper/statedb.go | 4 +--- x/evm/types/key.go | 15 --------------- 5 files changed, 3 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c4ed353..47c319b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (app) [tharsis#476](https://github.com/tharsis/ethermint/pull/476) Update Bech32 HRP to `ethm`. * (evm) [tharsis#556](https://github.com/tharsis/ethermint/pull/556) Remove tx logs and block bloom from chain state +* (evm) [tharsis#590](https://github.com/tharsis/ethermint/pull/590) Contract storage key is not hashed anymore ### API Breaking diff --git a/x/evm/keeper/grpc_query_test.go b/x/evm/keeper/grpc_query_test.go index 18468c0c..2e7a68cd 100644 --- a/x/evm/keeper/grpc_query_test.go +++ b/x/evm/keeper/grpc_query_test.go @@ -478,7 +478,6 @@ func (suite *KeeperTestSuite) TestQueryValidatorAccount() { } func (suite *KeeperTestSuite) TestEstimateGas() { - ctx := sdk.WrapSDKContext(suite.ctx) gasHelper := hexutil.Uint64(20000) var ( @@ -545,7 +544,7 @@ func (suite *KeeperTestSuite) TestEstimateGas() { GasCap: gasCap, } - rsp, err := suite.queryClient.EstimateGas(ctx, &req) + rsp, err := suite.queryClient.EstimateGas(sdk.WrapSDKContext(suite.ctx), &req) if tc.expPass { suite.Require().NoError(err) suite.Require().Equal(tc.expGas, rsp.Gas) diff --git a/x/evm/keeper/keeper.go b/x/evm/keeper/keeper.go index 4a308e29..cbe5d94b 100644 --- a/x/evm/keeper/keeper.go +++ b/x/evm/keeper/keeper.go @@ -307,7 +307,6 @@ func (k Keeper) GetAccountStorage(ctx sdk.Context, address common.Address) (type func (k Keeper) DeleteState(addr common.Address, key common.Hash) { store := prefix.NewStore(k.Ctx().KVStore(k.storeKey), types.AddressStoragePrefix(addr)) - key = types.KeyAddressStorage(addr, key) store.Delete(key.Bytes()) } diff --git a/x/evm/keeper/statedb.go b/x/evm/keeper/statedb.go index 7b3ad724..76a42116 100644 --- a/x/evm/keeper/statedb.go +++ b/x/evm/keeper/statedb.go @@ -454,10 +454,9 @@ func (k *Keeper) GetRefund() uint64 { // State // ---------------------------------------------------------------------------- -func doGetState(ctx sdk.Context, storeKey sdk.StoreKey, addr common.Address, hash common.Hash) common.Hash { +func doGetState(ctx sdk.Context, storeKey sdk.StoreKey, addr common.Address, key common.Hash) common.Hash { store := prefix.NewStore(ctx.KVStore(storeKey), types.AddressStoragePrefix(addr)) - key := types.KeyAddressStorage(addr, hash) value := store.Get(key.Bytes()) if len(value) == 0 { return common.Hash{} @@ -496,7 +495,6 @@ func (k *Keeper) SetState(addr common.Address, key, value common.Hash) { ctx := k.Ctx() store := prefix.NewStore(ctx.KVStore(k.storeKey), types.AddressStoragePrefix(addr)) - key = types.KeyAddressStorage(addr, key) action := "updated" if ethermint.IsEmptyHash(value.Hex()) { diff --git a/x/evm/types/key.go b/x/evm/types/key.go index 16a2b392..1b89c260 100644 --- a/x/evm/types/key.go +++ b/x/evm/types/key.go @@ -2,7 +2,6 @@ package types import ( "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/crypto" ) const ( @@ -69,17 +68,3 @@ func AddressStoragePrefix(address common.Address) []byte { func StateKey(address common.Address, key []byte) []byte { return append(AddressStoragePrefix(address), key...) } - -// KeyAddressStorage returns the key hash to access a given account state. The composite key -// (address + hash) is hashed using Keccak256. -func KeyAddressStorage(address common.Address, hash common.Hash) common.Hash { - prefix := address.Bytes() - key := hash.Bytes() - - compositeKey := make([]byte, len(prefix)+len(key)) - - copy(compositeKey, prefix) - copy(compositeKey[len(prefix):], key) - - return crypto.Keccak256Hash(compositeKey) -}