From 152d063f97a872df81d55f007994b8775960a2c0 Mon Sep 17 00:00:00 2001 From: Evan Han Date: Tue, 28 Mar 2023 09:51:47 +0800 Subject: [PATCH] fix data mismatch while calling eth_call (#81) --- app/rpc/namespaces/eth/api.go | 11 ++--------- x/evm/keeper/querier.go | 2 +- x/evm/keeper/statedb.go | 4 ++-- x/evm/types/state_object_mpt.go | 8 ++++++++ x/evm/types/statedb.go | 9 +++++++++ 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/rpc/namespaces/eth/api.go b/app/rpc/namespaces/eth/api.go index f9830fdbc..9be781af4 100644 --- a/app/rpc/namespaces/eth/api.go +++ b/app/rpc/namespaces/eth/api.go @@ -490,17 +490,10 @@ func (api *PublicEthereumAPI) getStorageAt(address common.Address, key []byte, b var out evmtypes.QueryResStorage api.clientCtx.Codec.MustUnmarshalJSON(res, &out) - realValue := out.Value if useWatchBackend { - realValue = bytes.TrimLeftFunc(out.Value, func(r rune) bool { - if r == 0 { - return true - } - return false - }) - api.watcherBackend.CommitStateToRpcDb(address, key, realValue) + api.watcherBackend.CommitStateToRpcDb(address, key, out.Value) } - return realValue, nil + return out.Value, nil } // GetStorageAt returns the contract storage at the given address, block number, and key. diff --git a/x/evm/keeper/querier.go b/x/evm/keeper/querier.go index 194d00a8d..d47faf244 100644 --- a/x/evm/keeper/querier.go +++ b/x/evm/keeper/querier.go @@ -183,7 +183,7 @@ func queryStorageByKey(ctx sdk.Context, path []string, keeper Keeper) ([]byte, e addr := ethcmn.HexToAddress(path[1]) key := ethcmn.HexToHash(path[2]) val := keeper.GetStateByKey(ctx, addr, key) - res := types.QueryResStorage{Value: val.Bytes()} + res := types.QueryResStorage{Value: val} bz, err := codec.MarshalJSONIndent(keeper.cdc, res) if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error()) diff --git a/x/evm/keeper/statedb.go b/x/evm/keeper/statedb.go index cf5206fb9..fb68133a2 100644 --- a/x/evm/keeper/statedb.go +++ b/x/evm/keeper/statedb.go @@ -58,8 +58,8 @@ func (k *Keeper) GetStorageProof(ctx sdk.Context, addr ethcmn.Address, key ethcm } // GetStateByKey calls CommitStateDB.GetState using the passed in context -func (k *Keeper) GetStateByKey(ctx sdk.Context, addr ethcmn.Address, key ethcmn.Hash) ethcmn.Hash { - return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).GetCommittedState(addr, key) +func (k *Keeper) GetStateByKey(ctx sdk.Context, addr ethcmn.Address, key ethcmn.Hash) []byte { + return types.CreateEmptyCommitStateDB(k.GenerateCSDBParams(), ctx).GetCommittedStateForQuery(addr, key) } // ---------------------------------------------------------------------------- diff --git a/x/evm/types/state_object_mpt.go b/x/evm/types/state_object_mpt.go index 23d836890..8dadd4f38 100644 --- a/x/evm/types/state_object_mpt.go +++ b/x/evm/types/state_object_mpt.go @@ -3,6 +3,7 @@ package types import ( "bytes" "fmt" + "github.com/okx/okbchain/libs/cosmos-sdk/store/mpt" ethcmn "github.com/ethereum/go-ethereum/common" @@ -70,6 +71,13 @@ func (so *stateObject) GetCommittedStateMpt(db ethstate.Database, key ethcmn.Has return value } +func (so *stateObject) GetCommittedStateMptForQuery(db ethstate.Database, key ethcmn.Hash) []byte { + ctx := &so.stateDB.ctx + store := so.stateDB.dbAdapter.NewStore(ctx.KVStore(so.stateDB.storeKey), mpt.AddressStoragePrefixMpt(so.address, so.account.StateRoot)) + enc := store.Get(key.Bytes()) + return enc +} + func (so *stateObject) CodeInRawDB(db ethstate.Database) []byte { if so.code != nil { return so.code diff --git a/x/evm/types/statedb.go b/x/evm/types/statedb.go index 72b2bbf29..3d313dc2d 100644 --- a/x/evm/types/statedb.go +++ b/x/evm/types/statedb.go @@ -786,6 +786,15 @@ func (csdb *CommitStateDB) GetCommittedState(addr ethcmn.Address, hash ethcmn.Ha return ethcmn.Hash{} } +func (csdb *CommitStateDB) GetCommittedStateForQuery(addr ethcmn.Address, hash ethcmn.Hash) []byte { + so := csdb.getStateObject(addr) + if so != nil { + return so.GetCommittedStateMptForQuery(csdb.db, hash) + } + + return nil +} + // GetLogs returns the current logs for a given transaction hash from the KVStore. func (csdb *CommitStateDB) GetLogs(hash ethcmn.Hash) ([]*ethtypes.Log, error) { return csdb.logs[hash], nil