2021-04-17 10:00:07 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-09 09:04:46 +00:00
|
|
|
"encoding/json"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/store/prefix"
|
2021-05-10 16:34:00 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-07-12 10:25:15 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-05-31 14:54:59 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/query"
|
2021-05-10 16:34:00 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
ethcmn "github.com/ethereum/go-ethereum/common"
|
2021-07-12 10:25:15 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
ethermint "github.com/tharsis/ethermint/types"
|
|
|
|
"github.com/tharsis/ethermint/x/evm/types"
|
2021-04-17 10:00:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ types.QueryServer = Keeper{}
|
|
|
|
|
|
|
|
// Account implements the Query/Account gRPC method
|
2021-04-18 15:54:18 +00:00
|
|
|
func (k Keeper) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
|
2021-04-17 10:00:07 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
if err := ethermint.ValidateAddress(req.Address); err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return nil, status.Error(
|
2021-05-17 10:13:08 +00:00
|
|
|
codes.InvalidArgument, err.Error(),
|
2021-04-17 10:00:07 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
addr := ethcmn.HexToAddress(req.Address)
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
k.WithContext(ctx)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return &types.QueryAccountResponse{
|
2021-07-02 09:29:47 +00:00
|
|
|
Balance: k.GetBalance(addr).String(),
|
2021-06-08 17:10:29 +00:00
|
|
|
CodeHash: k.GetCodeHash(addr).Hex(),
|
|
|
|
Nonce: k.GetNonce(addr),
|
2021-04-17 10:00:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
func (k Keeper) CosmosAccount(c context.Context, req *types.QueryCosmosAccountRequest) (*types.QueryCosmosAccountResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
if err := ethermint.ValidateAddress(req.Address); err != nil {
|
2021-04-18 15:54:18 +00:00
|
|
|
return nil, status.Error(
|
2021-05-17 10:13:08 +00:00
|
|
|
codes.InvalidArgument, err.Error(),
|
2021-04-18 15:54:18 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2021-06-08 17:10:29 +00:00
|
|
|
k.WithContext(ctx)
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
ethAddr := ethcmn.HexToAddress(req.Address)
|
|
|
|
cosmosAddr := sdk.AccAddress(ethAddr.Bytes())
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
account := k.accountKeeper.GetAccount(ctx, cosmosAddr)
|
2021-04-18 15:54:18 +00:00
|
|
|
res := types.QueryCosmosAccountResponse{
|
2021-05-17 10:13:08 +00:00
|
|
|
CosmosAddress: cosmosAddr.String(),
|
2021-04-18 15:54:18 +00:00
|
|
|
}
|
2021-05-17 10:13:08 +00:00
|
|
|
|
|
|
|
if account != nil {
|
|
|
|
res.Sequence = account.GetSequence()
|
|
|
|
res.AccountNumber = account.GetAccountNumber()
|
2021-04-18 15:54:18 +00:00
|
|
|
}
|
2021-05-17 10:13:08 +00:00
|
|
|
|
2021-04-18 15:54:18 +00:00
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
2021-06-22 10:14:40 +00:00
|
|
|
func (k Keeper) ValidatorAccount(c context.Context, req *types.QueryValidatorAccountRequest) (*types.QueryValidatorAccountResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
consAddr, err := sdk.ConsAddressFromBech32(req.ConsAddress)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(
|
|
|
|
codes.InvalidArgument, err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
k.WithContext(ctx)
|
|
|
|
|
|
|
|
validator, found := k.stakingKeeper.GetValidatorByConsAddr(ctx, consAddr)
|
|
|
|
if !found {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
accAddr := sdk.AccAddress(validator.GetOperator())
|
|
|
|
|
|
|
|
res := types.QueryValidatorAccountResponse{
|
|
|
|
AccountAddress: accAddr.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
account := k.accountKeeper.GetAccount(ctx, accAddr)
|
|
|
|
if account != nil {
|
|
|
|
res.Sequence = account.GetSequence()
|
|
|
|
res.AccountNumber = account.GetAccountNumber()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &res, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// Balance implements the Query/Balance gRPC method
|
2021-04-18 15:54:18 +00:00
|
|
|
func (k Keeper) Balance(c context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) {
|
2021-04-17 10:00:07 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
if err := ethermint.ValidateAddress(req.Address); err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return nil, status.Error(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
types.ErrZeroAddress.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2021-06-08 17:10:29 +00:00
|
|
|
k.WithContext(ctx)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
balanceInt := k.GetBalance(ethcmn.HexToAddress(req.Address))
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return &types.QueryBalanceResponse{
|
2021-07-02 09:29:47 +00:00
|
|
|
Balance: balanceInt.String(),
|
2021-04-17 10:00:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storage implements the Query/Storage gRPC method
|
2021-04-18 15:54:18 +00:00
|
|
|
func (k Keeper) Storage(c context.Context, req *types.QueryStorageRequest) (*types.QueryStorageResponse, error) {
|
2021-04-17 10:00:07 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
if err := ethermint.ValidateAddress(req.Address); err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return nil, status.Error(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
types.ErrZeroAddress.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
if ethermint.IsEmptyHash(req.Key) {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
types.ErrEmptyHash.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2021-06-08 17:10:29 +00:00
|
|
|
k.WithContext(ctx)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
address := ethcmn.HexToAddress(req.Address)
|
|
|
|
key := ethcmn.HexToHash(req.Key)
|
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
state := k.GetState(address, key)
|
2021-05-31 14:54:59 +00:00
|
|
|
stateHex := state.Hex()
|
|
|
|
|
|
|
|
if ethermint.IsEmptyHash(stateHex) {
|
|
|
|
return nil, status.Error(
|
|
|
|
codes.NotFound, "contract code not found for given address",
|
|
|
|
)
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return &types.QueryStorageResponse{
|
2021-05-31 14:54:59 +00:00
|
|
|
Value: stateHex,
|
2021-04-17 10:00:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Code implements the Query/Code gRPC method
|
2021-04-18 15:54:18 +00:00
|
|
|
func (k Keeper) Code(c context.Context, req *types.QueryCodeRequest) (*types.QueryCodeResponse, error) {
|
2021-04-17 10:00:07 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
if err := ethermint.ValidateAddress(req.Address); err != nil {
|
2021-04-17 10:00:07 +00:00
|
|
|
return nil, status.Error(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
types.ErrZeroAddress.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2021-06-08 17:10:29 +00:00
|
|
|
k.WithContext(ctx)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
address := ethcmn.HexToAddress(req.Address)
|
2021-06-08 17:10:29 +00:00
|
|
|
code := k.GetCode(address)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return &types.QueryCodeResponse{
|
|
|
|
Code: code,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxLogs implements the Query/TxLogs gRPC method
|
2021-04-18 15:54:18 +00:00
|
|
|
func (k Keeper) TxLogs(c context.Context, req *types.QueryTxLogsRequest) (*types.QueryTxLogsResponse, error) {
|
2021-04-17 10:00:07 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
if ethermint.IsEmptyHash(req.Hash) {
|
2021-04-17 10:00:07 +00:00
|
|
|
return nil, status.Error(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
types.ErrEmptyHash.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2021-06-08 17:10:29 +00:00
|
|
|
k.WithContext(ctx)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
hash := ethcmn.HexToHash(req.Hash)
|
2021-06-08 17:10:29 +00:00
|
|
|
logs := k.GetTxLogs(hash)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return &types.QueryTxLogsResponse{
|
2021-06-08 17:10:29 +00:00
|
|
|
Logs: types.NewLogsFromEth(logs),
|
2021-04-17 10:00:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
// BlockLogs implements the Query/BlockLogs gRPC method
|
|
|
|
func (k Keeper) BlockLogs(c context.Context, req *types.QueryBlockLogsRequest) (*types.QueryBlockLogsResponse, error) {
|
2021-04-18 15:54:18 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
if ethermint.IsEmptyHash(req.Hash) {
|
2021-04-18 15:54:18 +00:00
|
|
|
return nil, status.Error(
|
|
|
|
codes.InvalidArgument,
|
|
|
|
types.ErrEmptyHash.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixLogs)
|
|
|
|
txLogs := []types.TransactionLogs{}
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
pageRes, err := query.FilteredPaginate(store, req.Pagination, func(_, value []byte, accumulate bool) (bool, error) {
|
|
|
|
var txLog types.TransactionLogs
|
2021-06-29 17:02:21 +00:00
|
|
|
k.cdc.MustUnmarshal(value, &txLog)
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
if len(txLog.Logs) > 0 && txLog.Logs[0].BlockHash == req.Hash {
|
|
|
|
if accumulate {
|
|
|
|
txLogs = append(txLogs, txLog)
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
return false, nil
|
|
|
|
})
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return &types.QueryBlockLogsResponse{
|
2021-05-31 14:54:59 +00:00
|
|
|
TxLogs: txLogs,
|
|
|
|
Pagination: pageRes,
|
2021-04-17 10:00:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockBloom implements the Query/BlockBloom gRPC method
|
2021-05-17 10:13:08 +00:00
|
|
|
func (k Keeper) BlockBloom(c context.Context, _ *types.QueryBlockBloomRequest) (*types.QueryBlockBloomResponse, error) {
|
2021-04-17 10:00:07 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
2021-05-17 10:13:08 +00:00
|
|
|
bloom, found := k.GetBlockBloom(ctx, ctx.BlockHeight())
|
2021-04-17 10:00:07 +00:00
|
|
|
if !found {
|
2021-07-12 10:25:15 +00:00
|
|
|
// if the bloom is not found, query the transient store at the current height
|
|
|
|
k.ctx = ctx
|
|
|
|
bloomInt := k.GetBlockBloomTransient()
|
|
|
|
|
|
|
|
if bloomInt.Sign() == 0 {
|
|
|
|
return nil, status.Error(
|
|
|
|
codes.NotFound, sdkerrors.Wrapf(types.ErrBloomNotFound, "height: %d", ctx.BlockHeight()).Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
bloom = ethtypes.BytesToBloom(bloomInt.Bytes())
|
2021-04-17 10:00:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryBlockBloomResponse{
|
|
|
|
Bloom: bloom.Bytes(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Params implements the Query/Params gRPC method
|
2021-05-17 10:13:08 +00:00
|
|
|
func (k Keeper) Params(c context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
|
2021-04-17 10:00:07 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2021-04-18 15:54:18 +00:00
|
|
|
params := k.GetParams(ctx)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return &types.QueryParamsResponse{
|
|
|
|
Params: params,
|
|
|
|
}, nil
|
|
|
|
}
|
2021-04-18 15:54:18 +00:00
|
|
|
|
|
|
|
// StaticCall implements Query/StaticCall gRPCP method
|
|
|
|
func (k Keeper) StaticCall(c context.Context, req *types.QueryStaticCallRequest) (*types.QueryStaticCallResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
// ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
// k.WithContext(ctx)
|
|
|
|
|
|
|
|
// // parse the chainID from a string to a base-10 integer
|
|
|
|
// chainIDEpoch, err := ethermint.ParseChainID(ctx.ChainID())
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
// }
|
|
|
|
|
|
|
|
// txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
|
|
|
|
// ethHash := ethcmn.BytesToHash(txHash)
|
|
|
|
|
|
|
|
// var recipient *ethcmn.Address
|
|
|
|
// if len(req.Address) > 0 {
|
|
|
|
// addr := ethcmn.HexToAddress(req.Address)
|
|
|
|
// recipient = &addr
|
|
|
|
// }
|
|
|
|
|
|
|
|
// so := k.GetOrNewStateObject(*recipient)
|
|
|
|
// sender := ethcmn.HexToAddress("0xaDd00275E3d9d213654Ce5223f0FADE8b106b707")
|
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
// msg := types.NewTx(
|
2021-06-08 17:10:29 +00:00
|
|
|
// chainIDEpoch, so.Nonce(), recipient, big.NewInt(0), 100000000, big.NewInt(0), req.Input, nil,
|
|
|
|
// )
|
|
|
|
// msg.From = sender.Hex()
|
|
|
|
|
|
|
|
// if err := msg.ValidateBasic(); err != nil {
|
|
|
|
// return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ethMsg, err := msg.AsMessage()
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
// }
|
|
|
|
|
|
|
|
// st := &types.StateTransition{
|
|
|
|
// Message: ethMsg,
|
|
|
|
// Csdb: k.WithContext(ctx),
|
|
|
|
// ChainID: chainIDEpoch,
|
|
|
|
// TxHash: ðHash,
|
|
|
|
// Simulate: ctx.IsCheckTx(),
|
|
|
|
// Debug: false,
|
|
|
|
// }
|
|
|
|
|
|
|
|
// config, found := k.GetChainConfig(ctx)
|
|
|
|
// if !found {
|
|
|
|
// return nil, status.Error(codes.Internal, types.ErrChainConfigNotFound.Error())
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ret, err := st.StaticCall(ctx, config)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return &types.QueryStaticCallResponse{Data: ret}, nil
|
|
|
|
|
|
|
|
return nil, nil
|
2021-04-18 15:54:18 +00:00
|
|
|
}
|
2021-07-09 09:04:46 +00:00
|
|
|
|
|
|
|
// EthCall implements eth_call rpc api.
|
|
|
|
func (k Keeper) EthCall(c context.Context, req *types.EthCallRequest) (*types.MsgEthereumTxResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
k.WithContext(ctx)
|
|
|
|
|
|
|
|
var args types.CallArgs
|
|
|
|
err := json.Unmarshal(req.Args, &args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := args.ToMessage(uint64(ethermint.DefaultRPCGasLimit))
|
|
|
|
|
2021-07-14 09:13:55 +00:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(k.eip155ChainID)
|
|
|
|
|
|
|
|
evm := k.NewEVM(msg, ethCfg, params)
|
2021-07-09 09:04:46 +00:00
|
|
|
res, err := k.ApplyMessage(evm, msg, ethCfg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ApplyMessage don't handle gas refund, let's do it here
|
|
|
|
refund := k.GasToRefund(res.GasUsed)
|
|
|
|
res.GasUsed -= refund
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|