2021-04-17 10:00:07 +00:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-09 09:04:46 +00:00
|
|
|
"encoding/json"
|
2021-07-19 15:19:23 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-09-04 20:33:06 +00:00
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-07-19 15:19:23 +00:00
|
|
|
"github.com/palantir/stacktrace"
|
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-09-03 18:06:36 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-07-19 15:19:23 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
2021-07-12 10:25:15 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2021-07-19 15:19:23 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
ethparams "github.com/ethereum/go-ethereum/params"
|
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{}
|
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
const (
|
|
|
|
defaultTraceTimeout = 5 * time.Second
|
|
|
|
)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// 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-09-03 18:06:36 +00:00
|
|
|
addr := common.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-09-03 18:06:36 +00:00
|
|
|
ethAddr := common.HexToAddress(req.Address)
|
2021-05-17 10:13:08 +00:00
|
|
|
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-09-03 18:06:36 +00:00
|
|
|
balanceInt := k.GetBalance(common.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(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2021-06-08 17:10:29 +00:00
|
|
|
k.WithContext(ctx)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-09-03 18:06:36 +00:00
|
|
|
address := common.HexToAddress(req.Address)
|
|
|
|
key := common.HexToHash(req.Key)
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-06-08 17:10:29 +00:00
|
|
|
state := k.GetState(address, key)
|
2021-05-31 14:54:59 +00:00
|
|
|
stateHex := state.Hex()
|
|
|
|
|
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
|
|
|
|
2021-09-03 18:06:36 +00:00
|
|
|
address := common.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
|
|
|
|
2021-09-03 18:06:36 +00:00
|
|
|
hash := common.HexToHash(req.Hash)
|
2021-09-09 14:26:30 +00:00
|
|
|
|
|
|
|
store := prefix.NewStore(k.Ctx().KVStore(k.storeKey), append(types.KeyPrefixLogs, hash.Bytes()...))
|
|
|
|
|
|
|
|
var logs []*types.Log
|
|
|
|
|
|
|
|
pageRes, err := query.Paginate(store, req.Pagination, func(_, value []byte) error {
|
|
|
|
var log types.Log
|
|
|
|
if err := k.cdc.Unmarshal(value, &log); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logs = append(logs, &log)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return &types.QueryTxLogsResponse{
|
2021-09-09 14:26:30 +00:00
|
|
|
Logs: logs,
|
|
|
|
Pagination: pageRes,
|
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)
|
2021-08-31 08:32:11 +00:00
|
|
|
|
|
|
|
mapOrder := []string{}
|
|
|
|
logs := make(map[string][]*types.Log)
|
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) {
|
2021-08-31 08:32:11 +00:00
|
|
|
var txLog types.Log
|
2021-09-09 14:26:30 +00:00
|
|
|
if err := k.cdc.Unmarshal(value, &txLog); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2021-04-18 15:54:18 +00:00
|
|
|
|
2021-08-31 08:32:11 +00:00
|
|
|
if txLog.BlockHash == req.Hash {
|
2021-05-31 14:54:59 +00:00
|
|
|
if accumulate {
|
2021-08-31 08:32:11 +00:00
|
|
|
if len(logs[txLog.TxHash]) == 0 {
|
|
|
|
mapOrder = append(mapOrder, txLog.TxHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
logs[txLog.TxHash] = append(logs[txLog.TxHash], &txLog)
|
2021-05-31 14:54:59 +00:00
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-05-31 14:54:59 +00:00
|
|
|
return false, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-09-05 11:03:06 +00:00
|
|
|
txsLogs := []types.TransactionLogs{}
|
2021-08-31 08:32:11 +00:00
|
|
|
for _, txHash := range mapOrder {
|
|
|
|
if len(logs[txHash]) > 0 {
|
|
|
|
txsLogs = append(txsLogs, types.TransactionLogs{Hash: txHash, Logs: logs[txHash]})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
return &types.QueryBlockLogsResponse{
|
2021-08-31 08:32:11 +00:00
|
|
|
TxLogs: txsLogs,
|
2021-05-31 14:54:59 +00:00
|
|
|
Pagination: pageRes,
|
2021-04-17 10:00:07 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlockBloom implements the Query/BlockBloom gRPC method
|
2021-07-20 15:16:02 +00:00
|
|
|
func (k Keeper) BlockBloom(c context.Context, req *types.QueryBlockBloomRequest) (*types.QueryBlockBloomResponse, error) {
|
2021-04-17 10:00:07 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
2021-07-20 15:16:02 +00:00
|
|
|
bloom, found := k.GetBlockBloom(ctx, req.Height)
|
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
|
2021-08-10 07:22:46 +00:00
|
|
|
k.WithContext(ctx)
|
2021-07-12 10:25:15 +00:00
|
|
|
bloomInt := k.GetBlockBloomTransient()
|
|
|
|
|
|
|
|
if bloomInt.Sign() == 0 {
|
|
|
|
return nil, status.Error(
|
2021-07-20 15:16:02 +00:00
|
|
|
codes.NotFound, sdkerrors.Wrapf(types.ErrBloomNotFound, "height: %d", req.Height).Error(),
|
2021-07-12 10:25:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2021-07-19 15:19:23 +00:00
|
|
|
msg := args.ToMessage(req.GasCap)
|
2021-07-09 09:04:46 +00:00
|
|
|
|
2021-07-14 09:13:55 +00:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(k.eip155ChainID)
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
coinbase, err := k.GetCoinbaseAddress(ctx)
|
2021-07-14 20:44:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-08-19 08:18:20 +00:00
|
|
|
tracer := types.NewTracer(k.tracer, msg, ethCfg, k.Ctx().BlockHeight(), k.debug)
|
|
|
|
evm := k.NewEVM(msg, ethCfg, params, coinbase, tracer)
|
2021-08-16 09:45:10 +00:00
|
|
|
|
2021-07-15 06:01:05 +00:00
|
|
|
// pass true means execute in query mode, which don't do actual gas refund.
|
|
|
|
res, err := k.ApplyMessage(evm, msg, ethCfg, true)
|
2021-08-10 07:22:46 +00:00
|
|
|
k.ctxStack.RevertAll()
|
2021-07-09 09:04:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2021-07-19 15:19:23 +00:00
|
|
|
|
|
|
|
// EstimateGas implements eth_estimateGas rpc api.
|
|
|
|
func (k Keeper) EstimateGas(c context.Context, req *types.EthCallRequest) (*types.EstimateGasResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
k.WithContext(ctx)
|
|
|
|
|
2021-07-23 14:24:36 +00:00
|
|
|
if req.GasCap < ethparams.TxGas {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "gas cap cannot be lower than 21,000")
|
|
|
|
}
|
|
|
|
|
2021-07-19 15:19:23 +00:00
|
|
|
var args types.CallArgs
|
|
|
|
err := json.Unmarshal(req.Args, &args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Binary search the gas requirement, as it may be higher than the amount used
|
|
|
|
var (
|
2021-07-28 15:50:05 +00:00
|
|
|
lo = ethparams.TxGas - 1
|
2021-07-19 15:19:23 +00:00
|
|
|
hi uint64
|
|
|
|
cap uint64
|
|
|
|
)
|
|
|
|
|
|
|
|
// Determine the highest gas limit can be used during the estimation.
|
|
|
|
if args.Gas != nil && uint64(*args.Gas) >= ethparams.TxGas {
|
|
|
|
hi = uint64(*args.Gas)
|
|
|
|
} else {
|
|
|
|
// Query block gas limit
|
|
|
|
params := ctx.ConsensusParams()
|
|
|
|
if params != nil && params.Block != nil && params.Block.MaxGas > 0 {
|
|
|
|
hi = uint64(params.Block.MaxGas)
|
|
|
|
} else {
|
|
|
|
hi = req.GasCap
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO Recap the highest gas limit with account's available balance.
|
|
|
|
|
|
|
|
// Recap the highest gas allowance with specified gascap.
|
|
|
|
if req.GasCap != 0 && hi > req.GasCap {
|
|
|
|
hi = req.GasCap
|
|
|
|
}
|
|
|
|
cap = hi
|
|
|
|
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(k.eip155ChainID)
|
|
|
|
|
2021-08-31 12:50:31 +00:00
|
|
|
coinbase, err := k.GetCoinbaseAddress(ctx)
|
2021-07-19 15:19:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a helper to check if a gas allowance results in an executable transaction
|
|
|
|
executable := func(gas uint64) (bool, *types.MsgEthereumTxResponse, error) {
|
|
|
|
args.Gas = (*hexutil.Uint64)(&gas)
|
|
|
|
|
2021-08-10 07:22:46 +00:00
|
|
|
// Reset to the initial context
|
|
|
|
k.WithContext(ctx)
|
2021-07-19 15:19:23 +00:00
|
|
|
|
|
|
|
msg := args.ToMessage(req.GasCap)
|
2021-08-19 08:18:20 +00:00
|
|
|
|
|
|
|
tracer := types.NewTracer(k.tracer, msg, ethCfg, k.Ctx().BlockHeight(), k.debug)
|
|
|
|
evm := k.NewEVM(msg, ethCfg, params, coinbase, tracer)
|
2021-07-19 15:19:23 +00:00
|
|
|
// pass true means execute in query mode, which don't do actual gas refund.
|
|
|
|
rsp, err := k.ApplyMessage(evm, msg, ethCfg, true)
|
|
|
|
|
2021-08-10 07:22:46 +00:00
|
|
|
k.ctxStack.RevertAll()
|
2021-07-19 15:19:23 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(stacktrace.RootCause(err), core.ErrIntrinsicGas) {
|
|
|
|
return true, nil, nil // Special case, raise gas limit
|
|
|
|
}
|
|
|
|
return true, nil, err // Bail out
|
|
|
|
}
|
|
|
|
return len(rsp.VmError) > 0, rsp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the binary search and hone in on an executable gas limit
|
|
|
|
hi, err = types.BinSearch(lo, hi, executable)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reject the transaction as invalid if it still fails at the highest allowance
|
|
|
|
if hi == cap {
|
|
|
|
failed, result, err := executable(hi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
if failed {
|
|
|
|
if result != nil && result.VmError != vm.ErrOutOfGas.Error() {
|
|
|
|
if result.VmError == vm.ErrExecutionReverted.Error() {
|
2021-07-28 15:50:05 +00:00
|
|
|
return nil, types.NewExecErrorWithReason(result.Ret)
|
2021-07-19 15:19:23 +00:00
|
|
|
}
|
|
|
|
return nil, status.Error(codes.Internal, result.VmError)
|
|
|
|
}
|
|
|
|
// Otherwise, the specified gas cap is too low
|
|
|
|
return nil, status.Error(codes.Internal, fmt.Sprintf("gas required exceeds allowance (%d)", cap))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &types.EstimateGasResponse{Gas: hi}, nil
|
|
|
|
}
|
2021-09-04 20:33:06 +00:00
|
|
|
|
|
|
|
// TraceTx configures a new tracer according to the provided configuration, and
|
|
|
|
// executes the given message in the provided environment. The return value will
|
|
|
|
// be tracer dependent.
|
|
|
|
func (k Keeper) TraceTx(c context.Context, req *types.QueryTraceTxRequest) (*types.QueryTraceTxResponse, error) {
|
|
|
|
// Assemble the structured logger or the JavaScript tracer
|
|
|
|
var (
|
|
|
|
tracer vm.Tracer
|
|
|
|
err error
|
|
|
|
resultData []byte
|
|
|
|
)
|
|
|
|
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
k.WithContext(ctx)
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
|
|
|
coinbase, err := k.GetCoinbaseAddress(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(k.eip155ChainID)
|
|
|
|
signer := ethtypes.MakeSigner(ethCfg, big.NewInt(ctx.BlockHeight()))
|
|
|
|
coreMessage, err := req.Msg.AsMessage(signer)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case req.TraceConfig != nil && req.TraceConfig.Tracer != "":
|
|
|
|
timeout := defaultTraceTimeout
|
2021-09-05 11:03:06 +00:00
|
|
|
// TODO change timeout to time.duration
|
|
|
|
// Used string to comply with go ethereum
|
2021-09-04 20:33:06 +00:00
|
|
|
if req.TraceConfig.Timeout != "" {
|
|
|
|
if timeout, err = time.ParseDuration(req.TraceConfig.Timeout); err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "timeout value: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
txContext := core.NewEVMTxContext(coreMessage)
|
|
|
|
// Constuct the JavaScript tracer to execute with
|
|
|
|
if tracer, err = tracers.New(req.TraceConfig.Tracer, txContext); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle timeouts and RPC cancellations
|
|
|
|
deadlineCtx, cancel := context.WithTimeout(c, timeout)
|
|
|
|
go func() {
|
|
|
|
<-deadlineCtx.Done()
|
|
|
|
if deadlineCtx.Err() == context.DeadlineExceeded {
|
|
|
|
tracer.(*tracers.Tracer).Stop(errors.New("execution timeout"))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
defer cancel()
|
|
|
|
case req.TraceConfig != nil && req.TraceConfig.LogConfig != nil:
|
|
|
|
logConfig := vm.LogConfig{
|
|
|
|
DisableMemory: req.TraceConfig.LogConfig.DisableMemory,
|
|
|
|
Debug: req.TraceConfig.LogConfig.Debug,
|
|
|
|
DisableStorage: req.TraceConfig.LogConfig.DisableStorage,
|
|
|
|
DisableStack: req.TraceConfig.LogConfig.DisableStack,
|
|
|
|
}
|
|
|
|
tracer = vm.NewStructLogger(&logConfig)
|
|
|
|
default:
|
|
|
|
tracer = types.NewTracer(types.TracerStruct, coreMessage, ethCfg, ctx.BlockHeight(), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
evm := k.NewEVM(coreMessage, ethCfg, params, coinbase, tracer)
|
|
|
|
|
|
|
|
k.SetTxHashTransient(common.HexToHash(req.Msg.Hash))
|
|
|
|
k.SetTxIndexTransient(uint64(req.TxIndex))
|
|
|
|
|
|
|
|
res, err := k.ApplyMessage(evm, coreMessage, ethCfg, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Depending on the tracer type, format and return the trace result data.
|
|
|
|
switch tracer := tracer.(type) {
|
|
|
|
case *vm.StructLogger:
|
2021-09-05 11:03:06 +00:00
|
|
|
// TODO Return proper returnValue
|
2021-09-04 20:33:06 +00:00
|
|
|
result := types.ExecutionResult{
|
|
|
|
Gas: res.GasUsed,
|
|
|
|
Failed: res.Failed(),
|
|
|
|
ReturnValue: "",
|
|
|
|
StructLogs: types.FormatLogs(tracer.StructLogs()),
|
|
|
|
}
|
|
|
|
|
|
|
|
resultData, err = json.Marshal(result)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
case *tracers.Tracer:
|
|
|
|
result, err := tracer.GetResult()
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
resultData, err = json.Marshal(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "invalid tracer type")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryTraceTxResponse{
|
|
|
|
Data: resultData,
|
|
|
|
}, nil
|
|
|
|
}
|