2020-03-09 13:17:23 +00:00
|
|
|
package keeper
|
2019-07-25 20:38:55 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-23 19:38:47 +00:00
|
|
|
"encoding/json"
|
2020-04-01 18:49:21 +00:00
|
|
|
"fmt"
|
2019-10-04 19:32:56 +00:00
|
|
|
"strconv"
|
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-04-16 15:47:39 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-04-23 15:49:25 +00:00
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
"github.com/cosmos/ethermint/utils"
|
2019-07-25 20:38:55 +00:00
|
|
|
"github.com/cosmos/ethermint/version"
|
2019-09-26 15:36:23 +00:00
|
|
|
"github.com/cosmos/ethermint/x/evm/types"
|
2020-04-23 15:49:25 +00:00
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2020-04-23 15:49:25 +00:00
|
|
|
|
2019-07-25 20:38:55 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier is the module level router for state queries
|
|
|
|
func NewQuerier(keeper Keeper) sdk.Querier {
|
2020-09-02 19:41:05 +00:00
|
|
|
return func(ctx sdk.Context, path []string, _ abci.RequestQuery) ([]byte, error) {
|
2019-07-25 20:38:55 +00:00
|
|
|
switch path[0] {
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryProtocolVersion:
|
2020-04-22 19:26:01 +00:00
|
|
|
return queryProtocolVersion(keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryBalance:
|
2020-04-22 19:26:01 +00:00
|
|
|
return queryBalance(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryBlockNumber:
|
2020-04-22 19:26:01 +00:00
|
|
|
return queryBlockNumber(ctx, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryStorage:
|
2020-04-22 19:26:01 +00:00
|
|
|
return queryStorage(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryCode:
|
2020-04-22 19:26:01 +00:00
|
|
|
return queryCode(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryHashToHeight:
|
2020-04-22 19:26:01 +00:00
|
|
|
return queryHashToHeight(ctx, path, keeper)
|
2020-05-04 22:02:26 +00:00
|
|
|
case types.QueryTransactionLogs:
|
|
|
|
return queryTransactionLogs(ctx, path, keeper)
|
2020-06-04 10:40:21 +00:00
|
|
|
case types.QueryBloom:
|
|
|
|
return queryBlockBloom(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryLogs:
|
2020-04-22 19:26:01 +00:00
|
|
|
return queryLogs(ctx, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryAccount:
|
2020-04-22 19:26:01 +00:00
|
|
|
return queryAccount(ctx, path, keeper)
|
2020-06-17 18:23:36 +00:00
|
|
|
case types.QueryExportAccount:
|
|
|
|
return queryExportAccount(ctx, path, keeper)
|
2019-07-25 20:38:55 +00:00
|
|
|
default:
|
2020-04-22 19:26:01 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query endpoint")
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
func queryProtocolVersion(keeper Keeper) ([]byte, error) {
|
2019-07-25 20:38:55 +00:00
|
|
|
vers := version.ProtocolVersion
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, hexutil.Uint(vers))
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2019-09-26 15:36:23 +00:00
|
|
|
addr := ethcmn.HexToAddress(path[1])
|
2019-07-25 20:38:55 +00:00
|
|
|
balance := keeper.GetBalance(ctx, addr)
|
2020-04-23 15:49:25 +00:00
|
|
|
balanceStr, err := utils.MarshalBigInt(balance)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-26 15:36:23 +00:00
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
res := types.QueryResBalance{Balance: balanceStr}
|
2020-04-01 18:49:21 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
func queryBlockNumber(ctx sdk.Context, keeper Keeper) ([]byte, error) {
|
2019-07-25 20:38:55 +00:00
|
|
|
num := ctx.BlockHeight()
|
2019-09-26 15:36:23 +00:00
|
|
|
bnRes := types.QueryResBlockNumber{Number: num}
|
2020-04-01 18:49:21 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, bnRes)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2019-09-26 15:36:23 +00:00
|
|
|
addr := ethcmn.HexToAddress(path[1])
|
|
|
|
key := ethcmn.HexToHash(path[2])
|
2019-07-25 20:38:55 +00:00
|
|
|
val := keeper.GetState(ctx, addr, key)
|
2020-04-01 18:49:21 +00:00
|
|
|
res := types.QueryResStorage{Value: val.Bytes()}
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2019-09-26 15:36:23 +00:00
|
|
|
addr := ethcmn.HexToAddress(path[1])
|
2019-07-25 20:38:55 +00:00
|
|
|
code := keeper.GetCode(ctx, addr)
|
2020-04-01 18:49:21 +00:00
|
|
|
res := types.QueryResCode{Code: code}
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2019-09-06 14:46:26 +00:00
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
func queryHashToHeight(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2019-09-27 14:08:45 +00:00
|
|
|
blockHash := ethcmn.FromHex(path[1])
|
2020-06-04 10:40:21 +00:00
|
|
|
blockNumber, found := keeper.GetBlockHash(ctx, blockHash)
|
|
|
|
if !found {
|
|
|
|
return []byte{}, fmt.Errorf("block height not found for hash %s", path[1])
|
2020-04-23 15:49:25 +00:00
|
|
|
}
|
2019-09-27 14:08:45 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
res := types.QueryResBlockNumber{Number: blockNumber}
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
|
2019-09-27 14:08:45 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-09-27 14:08:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-09-27 14:08:45 +00:00
|
|
|
}
|
2019-10-04 19:32:56 +00:00
|
|
|
|
2020-06-04 10:40:21 +00:00
|
|
|
func queryBlockBloom(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2019-10-04 19:32:56 +00:00
|
|
|
num, err := strconv.ParseInt(path[1], 10, 64)
|
|
|
|
if err != nil {
|
2020-06-04 10:40:21 +00:00
|
|
|
return nil, fmt.Errorf("could not unmarshal block height: %w", err)
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-23 19:38:47 +00:00
|
|
|
bloom, found := keeper.GetBlockBloom(ctx.WithBlockHeight(num), num)
|
2020-06-04 10:40:21 +00:00
|
|
|
if !found {
|
|
|
|
return nil, fmt.Errorf("block bloom not found for height %d", num)
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2019-10-04 19:32:56 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
res := types.QueryBloomFilter{Bloom: bloom}
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
|
2019-10-04 19:32:56 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 22:02:26 +00:00
|
|
|
func queryTransactionLogs(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2019-10-04 19:32:56 +00:00
|
|
|
txHash := ethcmn.HexToHash(path[1])
|
2020-05-04 22:02:26 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
logs, err := keeper.GetLogs(ctx, txHash)
|
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, err
|
2020-04-01 18:49:21 +00:00
|
|
|
}
|
2019-10-04 19:32:56 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
res := types.QueryETHLogs{Logs: logs}
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
|
2019-10-04 19:32:56 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
2019-10-08 03:32:28 +00:00
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
func queryLogs(ctx sdk.Context, keeper Keeper) ([]byte, error) {
|
2020-04-01 18:49:21 +00:00
|
|
|
logs := keeper.AllLogs(ctx)
|
2019-10-08 03:32:28 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
res := types.QueryETHLogs{Logs: logs}
|
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
|
2019-10-16 00:46:50 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-10-16 00:46:50 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-10-16 00:46:50 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 15:47:39 +00:00
|
|
|
func queryAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2019-10-16 00:46:50 +00:00
|
|
|
addr := ethcmn.HexToAddress(path[1])
|
|
|
|
so := keeper.GetOrNewStateObject(ctx, addr)
|
|
|
|
|
2020-04-23 15:49:25 +00:00
|
|
|
balance, err := utils.MarshalBigInt(so.Balance())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
res := types.QueryResAccount{
|
2020-04-23 15:49:25 +00:00
|
|
|
Balance: balance,
|
2019-10-16 00:46:50 +00:00
|
|
|
CodeHash: so.CodeHash(),
|
|
|
|
Nonce: so.Nonce(),
|
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
|
2019-10-08 03:32:28 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-10-08 03:32:28 +00:00
|
|
|
}
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-10-08 03:32:28 +00:00
|
|
|
}
|
2020-06-17 18:23:36 +00:00
|
|
|
|
|
|
|
func queryExportAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2020-11-27 18:42:04 +00:00
|
|
|
hexAddress := path[1]
|
|
|
|
addr := ethcmn.HexToAddress(hexAddress)
|
2020-06-17 18:23:36 +00:00
|
|
|
|
2020-07-13 20:01:45 +00:00
|
|
|
var storage types.Storage
|
2020-09-02 19:41:05 +00:00
|
|
|
err := keeper.ForEachStorage(ctx, addr, func(key, value ethcmn.Hash) bool {
|
2020-07-13 20:01:45 +00:00
|
|
|
storage = append(storage, types.NewState(key, value))
|
2020-06-17 18:23:36 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-27 18:42:04 +00:00
|
|
|
balanceInt := keeper.GetBalance(ctx, addr)
|
|
|
|
balance := sdk.NewIntFromBigInt(balanceInt)
|
|
|
|
|
2020-06-17 18:23:36 +00:00
|
|
|
res := types.GenesisAccount{
|
2020-11-27 18:42:04 +00:00
|
|
|
Address: hexAddress,
|
|
|
|
Balance: balance,
|
2020-06-17 18:23:36 +00:00
|
|
|
Code: keeper.GetCode(ctx, addr),
|
|
|
|
Storage: storage,
|
|
|
|
}
|
|
|
|
|
2020-07-23 19:38:47 +00:00
|
|
|
// TODO: codec.MarshalJSONIndent doesn't call the String() method of types properly
|
|
|
|
bz, err := json.MarshalIndent(res, "", "\t")
|
2020-06-17 18:23:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bz, nil
|
|
|
|
}
|