Skip to content

Commit

Permalink
fix data mismatch while calling eth_call (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilovers authored Mar 28, 2023
1 parent 72b429c commit 152d063
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
11 changes: 2 additions & 9 deletions app/rpc/namespaces/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

// ----------------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions x/evm/types/state_object_mpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions x/evm/types/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 152d063

Please sign in to comment.