laconicd/x/evm/keeper/querier.go
Federico Kunze da9157e406
upgrade changes cleanup (#236)
* changes from update version

* app changes

* cmd changes

* build and send tx

* fix tests

* eth_rpc fixes

* lint

* add WithEventManager to handler ctx

* changelog

* go mod verify and tidy
2020-04-01 15:49:21 -03:00

195 lines
5.4 KiB
Go

package keeper
import (
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/ethermint/utils"
"github.com/cosmos/ethermint/version"
"github.com/cosmos/ethermint/x/evm/types"
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 {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) {
switch path[0] {
case types.QueryProtocolVersion:
return queryProtocolVersion(keeper)
case types.QueryBalance:
return queryBalance(ctx, path, keeper)
case types.QueryBlockNumber:
return queryBlockNumber(ctx, keeper)
case types.QueryStorage:
return queryStorage(ctx, path, keeper)
case types.QueryCode:
return queryCode(ctx, path, keeper)
case types.QueryNonce:
return queryNonce(ctx, path, keeper)
case types.QueryHashToHeight:
return queryHashToHeight(ctx, path, keeper)
case types.QueryTxLogs:
return queryTxLogs(ctx, path, keeper)
case types.QueryLogsBloom:
return queryBlockLogsBloom(ctx, path, keeper)
case types.QueryLogs:
return queryLogs(ctx, keeper)
case types.QueryAccount:
return queryAccount(ctx, path, keeper)
default:
return nil, sdk.ErrUnknownRequest("unknown query endpoint")
}
}
}
func queryProtocolVersion(keeper Keeper) ([]byte, sdk.Error) {
vers := version.ProtocolVersion
bz, err := codec.MarshalJSONIndent(keeper.cdc, hexutil.Uint(vers))
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
addr := ethcmn.HexToAddress(path[1])
balance := keeper.GetBalance(ctx, addr)
res := types.QueryResBalance{Balance: utils.MarshalBigInt(balance)}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryBlockNumber(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) {
num := ctx.BlockHeight()
bnRes := types.QueryResBlockNumber{Number: num}
bz, err := codec.MarshalJSONIndent(keeper.cdc, bnRes)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
addr := ethcmn.HexToAddress(path[1])
key := ethcmn.HexToHash(path[2])
val := keeper.GetState(ctx, addr, key)
res := types.QueryResStorage{Value: val.Bytes()}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
addr := ethcmn.HexToAddress(path[1])
code := keeper.GetCode(ctx, addr)
res := types.QueryResCode{Code: code}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryNonce(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
addr := ethcmn.HexToAddress(path[1])
nonce := keeper.GetNonce(ctx, addr)
nRes := types.QueryResNonce{Nonce: nonce}
bz, err := codec.MarshalJSONIndent(keeper.cdc, nRes)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryHashToHeight(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
blockHash := ethcmn.FromHex(path[1])
blockNumber := keeper.GetBlockHashMapping(ctx, blockHash)
res := types.QueryResBlockNumber{Number: blockNumber}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryBlockLogsBloom(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
num, err := strconv.ParseInt(path[1], 10, 64)
if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("could not unmarshall block number: %s", err.Error()))
}
bloom, err := keeper.GetBlockBloomMapping(ctx, num)
if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to get block bloom mapping: %s", err.Error()))
}
res := types.QueryBloomFilter{Bloom: bloom}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryTxLogs(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
txHash := ethcmn.HexToHash(path[1])
logs, err := keeper.GetLogs(ctx, txHash)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
res := types.QueryETHLogs{Logs: logs}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryLogs(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) {
logs := keeper.AllLogs(ctx)
res := types.QueryETHLogs{Logs: logs}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}
func queryAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.Error) {
addr := ethcmn.HexToAddress(path[1])
so := keeper.GetOrNewStateObject(ctx, addr)
res := types.QueryResAccount{
Balance: utils.MarshalBigInt(so.Balance()),
CodeHash: so.CodeHash(),
Nonce: so.Nonce(),
}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdk.ErrInternal(err.Error())
}
return bz, nil
}