2019-07-25 20:38:55 +00:00
|
|
|
package evm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Supported endpoints
|
|
|
|
const (
|
|
|
|
QueryProtocolVersion = "protocolVersion"
|
|
|
|
QueryBalance = "balance"
|
|
|
|
QueryBlockNumber = "blockNumber"
|
|
|
|
QueryStorage = "storage"
|
|
|
|
QueryCode = "code"
|
2019-09-06 14:46:26 +00:00
|
|
|
QueryNonce = "nonce"
|
2019-07-25 20:38:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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) (res []byte, err sdk.Error) {
|
|
|
|
switch path[0] {
|
|
|
|
case QueryProtocolVersion:
|
|
|
|
return queryProtocolVersion(keeper)
|
|
|
|
case QueryBalance:
|
|
|
|
return queryBalance(ctx, path, keeper)
|
|
|
|
case QueryBlockNumber:
|
|
|
|
return queryBlockNumber(ctx, keeper)
|
|
|
|
case QueryStorage:
|
|
|
|
return queryStorage(ctx, path, keeper)
|
|
|
|
case QueryCode:
|
|
|
|
return queryCode(ctx, path, keeper)
|
2019-09-06 14:46:26 +00:00
|
|
|
case QueryNonce:
|
|
|
|
return queryNonce(ctx, path, keeper)
|
2019-07-25 20:38:55 +00:00
|
|
|
default:
|
|
|
|
return nil, sdk.ErrUnknownRequest("unknown query endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryProtocolVersion(keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
vers := version.ProtocolVersion
|
|
|
|
|
2019-09-26 15:36:23 +00:00
|
|
|
res, err := codec.MarshalJSONIndent(keeper.cdc, hexutil.Uint(vers))
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("could not marshal result to JSON")
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryBalance(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.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
|
|
|
|
|
|
|
bRes := types.QueryResBalance{Balance: utils.MarshalBigInt(balance)}
|
|
|
|
res, err := codec.MarshalJSONIndent(keeper.cdc, bRes)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2019-09-26 15:36:23 +00:00
|
|
|
panic("could not marshal result to JSON: " + err.Error())
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryBlockNumber(ctx sdk.Context, keeper Keeper) ([]byte, sdk.Error) {
|
|
|
|
num := ctx.BlockHeight()
|
2019-09-26 15:36:23 +00:00
|
|
|
bnRes := types.QueryResBlockNumber{Number: num}
|
|
|
|
res, err := codec.MarshalJSONIndent(keeper.cdc, bnRes)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2019-09-24 14:49:40 +00:00
|
|
|
panic("could not marshal result to JSON: " + err.Error())
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryStorage(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.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)
|
2019-09-26 15:36:23 +00:00
|
|
|
bRes := types.QueryResStorage{Value: val.Bytes()}
|
|
|
|
res, err := codec.MarshalJSONIndent(keeper.cdc, bRes)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2019-09-24 14:49:40 +00:00
|
|
|
panic("could not marshal result to JSON: " + err.Error())
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryCode(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.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)
|
2019-09-26 15:36:23 +00:00
|
|
|
cRes := types.QueryResCode{Code: code}
|
|
|
|
res, err := codec.MarshalJSONIndent(keeper.cdc, cRes)
|
2019-07-25 20:38:55 +00:00
|
|
|
if err != nil {
|
2019-09-24 14:49:40 +00:00
|
|
|
panic("could not marshal result to JSON: " + err.Error())
|
2019-07-25 20:38:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2019-09-06 14:46:26 +00:00
|
|
|
|
|
|
|
func queryNonce(ctx sdk.Context, path []string, keeper Keeper) ([]byte, sdk.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}
|
2019-09-06 14:46:26 +00:00
|
|
|
res, err := codec.MarshalJSONIndent(keeper.cdc, nRes)
|
|
|
|
if err != nil {
|
2019-09-24 14:49:40 +00:00
|
|
|
panic("could not marshal result to JSON: " + err.Error())
|
2019-09-06 14:46:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|