2020-03-09 13:17:23 +00:00
|
|
|
package keeper
|
2019-07-25 20:38:55 +00:00
|
|
|
|
|
|
|
import (
|
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"
|
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"
|
2019-07-25 20:38:55 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier is the module level router for state queries
|
|
|
|
func NewQuerier(keeper Keeper) sdk.Querier {
|
2020-04-01 18:49:21 +00:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) {
|
2020-04-16 15:47:39 +00:00
|
|
|
var (
|
|
|
|
bz []byte
|
|
|
|
err 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-16 15:47:39 +00:00
|
|
|
bz, err = queryProtocolVersion(keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryBalance:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryBalance(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryBlockNumber:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryBlockNumber(ctx, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryStorage:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryStorage(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryCode:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryCode(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryNonce:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryNonce(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryHashToHeight:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryHashToHeight(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryTxLogs:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryTxLogs(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryLogsBloom:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryBlockLogsBloom(ctx, path, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryLogs:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryLogs(ctx, keeper)
|
2020-03-09 13:17:23 +00:00
|
|
|
case types.QueryAccount:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = queryAccount(ctx, path, keeper)
|
2019-07-25 20:38:55 +00:00
|
|
|
default:
|
2020-04-16 15:47:39 +00:00
|
|
|
bz, err = nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query endpoint")
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
2020-04-16 15:47:39 +00:00
|
|
|
|
|
|
|
return bz, sdk.ConvertError(err)
|
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)
|
2019-09-26 15:36:23 +00:00
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
res := types.QueryResBalance{Balance: utils.MarshalBigInt(balance)}
|
|
|
|
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 queryNonce(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2019-09-26 15:36:23 +00:00
|
|
|
addr := ethcmn.HexToAddress(path[1])
|
2019-09-06 14:46:26 +00:00
|
|
|
nonce := keeper.GetNonce(ctx, addr)
|
2019-09-26 15:36:23 +00:00
|
|
|
nRes := types.QueryResNonce{Nonce: nonce}
|
2020-04-01 18:49:21 +00:00
|
|
|
bz, err := codec.MarshalJSONIndent(keeper.cdc, nRes)
|
2019-09-06 14:46:26 +00:00
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
2019-09-06 14:46:26 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
return bz, nil
|
2019-09-06 14:46:26 +00:00
|
|
|
}
|
2019-09-27 14:08:45 +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])
|
|
|
|
blockNumber := keeper.GetBlockHashMapping(ctx, blockHash)
|
|
|
|
|
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-04-16 15:47:39 +00:00
|
|
|
func queryBlockLogsBloom(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-04-16 15:47:39 +00:00
|
|
|
return nil, fmt.Errorf("could not unmarshal block number: %w", err)
|
2019-10-04 19:32:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-01 18:49:21 +00:00
|
|
|
bloom, err := keeper.GetBlockBloomMapping(ctx, num)
|
|
|
|
if err != nil {
|
2020-04-16 15:47:39 +00:00
|
|
|
return nil, fmt.Errorf("failed to get block bloom mapping: %w", 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.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-04-16 15:47:39 +00:00
|
|
|
func queryTxLogs(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
|
2019-10-04 19:32:56 +00:00
|
|
|
txHash := ethcmn.HexToHash(path[1])
|
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-01 18:49:21 +00:00
|
|
|
res := types.QueryResAccount{
|
2019-10-16 00:46:50 +00:00
|
|
|
Balance: utils.MarshalBigInt(so.Balance()),
|
|
|
|
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
|
|
|
}
|