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"
|
|
|
|
|
2022-02-14 23:08:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers/logger"
|
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/tracers"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2021-05-10 16:34:00 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
|
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
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
"github.com/evmos/ethermint/x/evm/statedb"
|
|
|
|
"github.com/evmos/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)
|
2022-01-05 18:18:02 +00:00
|
|
|
acct := k.GetAccountOrEmpty(ctx, addr)
|
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
return &types.QueryAccountResponse{
|
2022-01-05 07:28:27 +00:00
|
|
|
Balance: acct.Balance.String(),
|
|
|
|
CodeHash: common.BytesToHash(acct.CodeHash).Hex(),
|
|
|
|
Nonce: acct.Nonce,
|
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-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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
balanceInt := k.GetBalance(ctx, 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-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
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
state := k.GetState(ctx, 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-09-03 18:06:36 +00:00
|
|
|
address := common.HexToAddress(req.Address)
|
2022-01-05 18:18:02 +00:00
|
|
|
acct := k.GetAccountWithoutBalance(ctx, address)
|
2022-01-05 07:28:27 +00:00
|
|
|
|
|
|
|
var code []byte
|
|
|
|
if acct != nil && acct.IsContract() {
|
|
|
|
code = k.GetCode(ctx, common.BytesToHash(acct.CodeHash))
|
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
return &types.QueryCodeResponse{
|
|
|
|
Code: code,
|
|
|
|
}, 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) {
|
2021-10-04 08:44:38 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-07-09 09:04:46 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
var args types.TransactionArgs
|
2021-07-09 09:04:46 +00:00
|
|
|
err := json.Unmarshal(req.Args, &args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
cfg, err := k.EVMConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
2021-10-05 15:38:20 +00:00
|
|
|
}
|
|
|
|
|
2022-01-04 10:55:30 +00:00
|
|
|
// ApplyMessageWithConfig expect correct nonce set in msg
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := k.GetNonce(ctx, args.GetFrom())
|
2022-01-04 10:55:30 +00:00
|
|
|
args.Nonce = (*hexutil.Uint64)(&nonce)
|
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
msg, err := args.ToMessage(req.GasCap, cfg.BaseFee)
|
2021-10-05 15:38:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, err.Error())
|
|
|
|
}
|
2021-07-09 09:04:46 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
|
|
|
|
|
|
|
|
// pass false to not commit StateDB
|
|
|
|
res, err := k.ApplyMessageWithConfig(ctx, msg, nil, false, cfg, txConfig)
|
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) {
|
2021-10-04 08:44:38 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-07-19 15:19:23 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
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-10-05 15:38:20 +00:00
|
|
|
var args types.TransactionArgs
|
2021-07-19 15:19:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 08:44:38 +00:00
|
|
|
// TODO: Recap the highest gas limit with account's available balance.
|
2021-07-19 15:19:23 +00:00
|
|
|
|
|
|
|
// Recap the highest gas allowance with specified gascap.
|
|
|
|
if req.GasCap != 0 && hi > req.GasCap {
|
|
|
|
hi = req.GasCap
|
|
|
|
}
|
|
|
|
cap = hi
|
|
|
|
|
2021-10-22 17:21:03 +00:00
|
|
|
cfg, err := k.EVMConfig(ctx)
|
2021-07-19 15:19:23 +00:00
|
|
|
if err != nil {
|
2021-10-22 17:21:03 +00:00
|
|
|
return nil, status.Error(codes.Internal, "failed to load evm config")
|
2021-07-19 15:19:23 +00:00
|
|
|
}
|
2021-10-04 14:58:06 +00:00
|
|
|
|
2022-01-04 10:55:30 +00:00
|
|
|
// ApplyMessageWithConfig expect correct nonce set in msg
|
2022-01-05 07:28:27 +00:00
|
|
|
nonce := k.GetNonce(ctx, args.GetFrom())
|
2022-01-04 10:55:30 +00:00
|
|
|
args.Nonce = (*hexutil.Uint64)(&nonce)
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))
|
|
|
|
|
2021-07-19 15:19:23 +00:00
|
|
|
// Create a helper to check if a gas allowance results in an executable transaction
|
2021-10-04 14:58:06 +00:00
|
|
|
executable := func(gas uint64) (vmerror bool, rsp *types.MsgEthereumTxResponse, err error) {
|
2021-07-19 15:19:23 +00:00
|
|
|
args.Gas = (*hexutil.Uint64)(&gas)
|
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
msg, err := args.ToMessage(req.GasCap, cfg.BaseFee)
|
2021-10-05 15:38:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
2021-08-19 08:18:20 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// pass false to not commit StateDB
|
|
|
|
rsp, err = k.ApplyMessageWithConfig(ctx, msg, nil, false, cfg, txConfig)
|
2021-07-19 15:19:23 +00:00
|
|
|
if err != nil {
|
2021-11-26 14:19:28 +00:00
|
|
|
if errors.Is(err, core.ErrIntrinsicGas) {
|
2021-07-19 15:19:23 +00:00
|
|
|
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) {
|
2021-10-04 08:44:38 +00:00
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
if req.TraceConfig != nil && req.TraceConfig.Limit < 0 {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "output limit cannot be negative, got %d", req.TraceConfig.Limit)
|
|
|
|
}
|
|
|
|
|
2022-04-16 17:31:30 +00:00
|
|
|
// minus one to get the context of block beginning
|
|
|
|
contextHeight := req.BlockNumber - 1
|
|
|
|
if contextHeight < 1 {
|
|
|
|
// 0 is a special value in `ContextWithHeight`
|
|
|
|
contextHeight = 1
|
|
|
|
}
|
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2022-04-16 17:31:30 +00:00
|
|
|
ctx = ctx.WithBlockHeight(contextHeight)
|
2021-11-09 18:38:22 +00:00
|
|
|
ctx = ctx.WithBlockTime(req.BlockTime)
|
|
|
|
ctx = ctx.WithHeaderHash(common.Hex2Bytes(req.BlockHash))
|
2021-09-04 20:33:06 +00:00
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
cfg, err := k.EVMConfig(ctx)
|
|
|
|
if err != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "failed to load evm config: %s", err.Error())
|
2021-12-28 07:59:28 +00:00
|
|
|
}
|
|
|
|
signer := ethtypes.MakeSigner(cfg.ChainConfig, big.NewInt(ctx.BlockHeight()))
|
2021-10-04 08:44:38 +00:00
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))
|
2021-11-09 18:38:22 +00:00
|
|
|
for i, tx := range req.Predecessors {
|
|
|
|
ethTx := tx.AsTransaction()
|
2021-12-28 07:59:28 +00:00
|
|
|
msg, err := ethTx.AsMessage(signer, cfg.BaseFee)
|
2021-11-09 18:38:22 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig.TxHash = ethTx.Hash()
|
|
|
|
txConfig.TxIndex = uint(i)
|
|
|
|
rsp, err := k.ApplyMessageWithConfig(ctx, msg, types.NewNoOpTracer(), true, cfg, txConfig)
|
|
|
|
if err != nil {
|
2021-11-09 18:38:22 +00:00
|
|
|
continue
|
|
|
|
}
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig.LogIndex += uint(len(rsp.Logs))
|
2021-11-09 18:38:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tx := req.Msg.AsTransaction()
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig.TxHash = tx.Hash()
|
|
|
|
txConfig.TxIndex++
|
|
|
|
result, _, err := k.traceTx(ctx, cfg, txConfig, signer, tx, req.TraceConfig, false)
|
2021-09-15 08:46:01 +00:00
|
|
|
if err != nil {
|
2021-11-09 18:38:22 +00:00
|
|
|
// error will be returned with detail status from traceTx
|
2021-10-04 08:44:38 +00:00
|
|
|
return nil, err
|
2021-09-15 08:46:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resultData, err := json.Marshal(result)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryTraceTxResponse{
|
|
|
|
Data: resultData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:38:22 +00:00
|
|
|
// TraceBlock configures a new tracer according to the provided configuration, and
|
|
|
|
// executes the given message in the provided environment for all the transactions in the queried block.
|
|
|
|
// The return value will be tracer dependent.
|
|
|
|
func (k Keeper) TraceBlock(c context.Context, req *types.QueryTraceBlockRequest) (*types.QueryTraceBlockResponse, error) {
|
|
|
|
if req == nil {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "empty request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.TraceConfig != nil && req.TraceConfig.Limit < 0 {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "output limit cannot be negative, got %d", req.TraceConfig.Limit)
|
|
|
|
}
|
|
|
|
|
2022-04-16 17:31:30 +00:00
|
|
|
// minus one to get the context of block beginning
|
|
|
|
contextHeight := req.BlockNumber - 1
|
|
|
|
if contextHeight < 1 {
|
|
|
|
// 0 is a special value in `ContextWithHeight`
|
|
|
|
contextHeight = 1
|
|
|
|
}
|
|
|
|
|
2021-11-09 18:38:22 +00:00
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
2022-04-16 17:31:30 +00:00
|
|
|
ctx = ctx.WithBlockHeight(contextHeight)
|
2021-11-09 18:38:22 +00:00
|
|
|
ctx = ctx.WithBlockTime(req.BlockTime)
|
|
|
|
ctx = ctx.WithHeaderHash(common.Hex2Bytes(req.BlockHash))
|
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
cfg, err := k.EVMConfig(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, "failed to load evm config")
|
|
|
|
}
|
|
|
|
signer := ethtypes.MakeSigner(cfg.ChainConfig, big.NewInt(ctx.BlockHeight()))
|
2021-11-09 18:38:22 +00:00
|
|
|
txsLength := len(req.Txs)
|
|
|
|
results := make([]*types.TxTraceResult, 0, txsLength)
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes()))
|
2021-11-09 18:38:22 +00:00
|
|
|
for i, tx := range req.Txs {
|
|
|
|
result := types.TxTraceResult{}
|
|
|
|
ethTx := tx.AsTransaction()
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig.TxHash = ethTx.Hash()
|
|
|
|
txConfig.TxIndex = uint(i)
|
|
|
|
traceResult, logIndex, err := k.traceTx(ctx, cfg, txConfig, signer, ethTx, req.TraceConfig, true)
|
2021-11-09 18:38:22 +00:00
|
|
|
if err != nil {
|
|
|
|
result.Error = err.Error()
|
|
|
|
continue
|
|
|
|
}
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig.LogIndex = logIndex
|
2021-11-09 18:38:22 +00:00
|
|
|
result.Result = traceResult
|
|
|
|
results = append(results, &result)
|
|
|
|
}
|
|
|
|
|
|
|
|
resultData, err := json.Marshal(results)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return &types.QueryTraceBlockResponse{
|
|
|
|
Data: resultData,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
// traceTx do trace on one transaction, it returns a tuple: (traceResult, nextLogIndex, error).
|
2021-10-04 08:44:38 +00:00
|
|
|
func (k *Keeper) traceTx(
|
|
|
|
ctx sdk.Context,
|
2021-12-28 07:59:28 +00:00
|
|
|
cfg *types.EVMConfig,
|
2022-01-05 07:28:27 +00:00
|
|
|
txConfig statedb.TxConfig,
|
2021-10-04 08:44:38 +00:00
|
|
|
signer ethtypes.Signer,
|
|
|
|
tx *ethtypes.Transaction,
|
|
|
|
traceConfig *types.TraceConfig,
|
2021-11-09 18:38:22 +00:00
|
|
|
commitMessage bool,
|
2022-01-05 07:28:27 +00:00
|
|
|
) (*interface{}, uint, error) {
|
2021-09-15 08:46:01 +00:00
|
|
|
// Assemble the structured logger or the JavaScript tracer
|
|
|
|
var (
|
2022-02-14 23:08:41 +00:00
|
|
|
tracer vm.EVMLogger
|
2021-10-05 15:38:20 +00:00
|
|
|
overrides *ethparams.ChainConfig
|
|
|
|
err error
|
2021-09-15 08:46:01 +00:00
|
|
|
)
|
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
msg, err := tx.AsMessage(signer, cfg.BaseFee)
|
2021-09-04 20:33:06 +00:00
|
|
|
if err != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil, 0, status.Error(codes.Internal, err.Error())
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
if traceConfig != nil && traceConfig.Overrides != nil {
|
2021-12-28 07:59:28 +00:00
|
|
|
overrides = traceConfig.Overrides.EthereumConfig(cfg.ChainConfig.ChainID)
|
2021-10-05 15:38:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
switch {
|
2021-09-15 08:46:01 +00:00
|
|
|
case traceConfig != nil && traceConfig.Tracer != "":
|
2021-09-04 20:33:06 +00:00
|
|
|
timeout := defaultTraceTimeout
|
2021-10-04 08:44:38 +00:00
|
|
|
// TODO: change timeout to time.duration
|
2021-09-05 11:03:06 +00:00
|
|
|
// Used string to comply with go ethereum
|
2021-09-15 08:46:01 +00:00
|
|
|
if traceConfig.Timeout != "" {
|
2021-10-04 08:44:38 +00:00
|
|
|
timeout, err = time.ParseDuration(traceConfig.Timeout)
|
|
|
|
if err != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil, 0, status.Errorf(codes.InvalidArgument, "timeout value: %s", err.Error())
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
tCtx := &tracers.Context{
|
2022-01-05 07:28:27 +00:00
|
|
|
BlockHash: txConfig.BlockHash,
|
|
|
|
TxIndex: int(txConfig.TxIndex),
|
|
|
|
TxHash: txConfig.TxHash,
|
2021-10-05 15:38:20 +00:00
|
|
|
}
|
2021-10-04 08:44:38 +00:00
|
|
|
|
2021-09-15 08:46:01 +00:00
|
|
|
// Construct the JavaScript tracer to execute with
|
2021-10-05 15:38:20 +00:00
|
|
|
if tracer, err = tracers.New(traceConfig.Tracer, tCtx); err != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil, 0, status.Error(codes.Internal, err.Error())
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Handle timeouts and RPC cancellations
|
2021-10-04 08:44:38 +00:00
|
|
|
deadlineCtx, cancel := context.WithTimeout(ctx.Context(), timeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
go func() {
|
|
|
|
<-deadlineCtx.Done()
|
2021-10-04 08:44:38 +00:00
|
|
|
if errors.Is(deadlineCtx.Err(), context.DeadlineExceeded) {
|
2022-02-14 23:08:41 +00:00
|
|
|
tracer.(tracers.Tracer).Stop(errors.New("execution timeout"))
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
}()
|
2021-10-04 08:44:38 +00:00
|
|
|
|
2021-09-15 13:50:52 +00:00
|
|
|
case traceConfig != nil:
|
2022-02-14 23:08:41 +00:00
|
|
|
logConfig := logger.Config{
|
2021-10-05 15:38:20 +00:00
|
|
|
EnableMemory: traceConfig.EnableMemory,
|
|
|
|
DisableStorage: traceConfig.DisableStorage,
|
|
|
|
DisableStack: traceConfig.DisableStack,
|
|
|
|
EnableReturnData: traceConfig.EnableReturnData,
|
|
|
|
Debug: traceConfig.Debug,
|
|
|
|
Limit: int(traceConfig.Limit),
|
|
|
|
Overrides: overrides,
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
2022-02-14 23:08:41 +00:00
|
|
|
tracer = logger.NewStructLogger(&logConfig)
|
2021-09-04 20:33:06 +00:00
|
|
|
default:
|
2021-12-28 07:59:28 +00:00
|
|
|
tracer = types.NewTracer(types.TracerStruct, msg, cfg.ChainConfig, ctx.BlockHeight())
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
res, err := k.ApplyMessageWithConfig(ctx, msg, tracer, commitMessage, cfg, txConfig)
|
2021-09-04 20:33:06 +00:00
|
|
|
if err != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil, 0, status.Error(codes.Internal, err.Error())
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 08:46:01 +00:00
|
|
|
var result interface{}
|
2021-10-04 08:44:38 +00:00
|
|
|
|
2021-09-04 20:33:06 +00:00
|
|
|
// Depending on the tracer type, format and return the trace result data.
|
|
|
|
switch tracer := tracer.(type) {
|
2022-02-14 23:08:41 +00:00
|
|
|
case *logger.StructLogger:
|
2022-03-02 02:14:21 +00:00
|
|
|
returnVal := ""
|
|
|
|
revert := res.Revert()
|
|
|
|
if len(revert) > 0 {
|
|
|
|
returnVal = fmt.Sprintf("%x", revert)
|
|
|
|
} else {
|
|
|
|
returnVal = fmt.Sprintf("%x", res.Return())
|
|
|
|
}
|
2021-09-15 08:46:01 +00:00
|
|
|
result = types.ExecutionResult{
|
2021-09-04 20:33:06 +00:00
|
|
|
Gas: res.GasUsed,
|
|
|
|
Failed: res.Failed(),
|
2022-03-02 02:14:21 +00:00
|
|
|
ReturnValue: returnVal,
|
2021-09-04 20:33:06 +00:00
|
|
|
StructLogs: types.FormatLogs(tracer.StructLogs()),
|
|
|
|
}
|
2022-02-14 23:08:41 +00:00
|
|
|
case tracers.Tracer:
|
2021-09-15 08:46:01 +00:00
|
|
|
result, err = tracer.GetResult()
|
2021-09-04 20:33:06 +00:00
|
|
|
if err != nil {
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil, 0, status.Error(codes.Internal, err.Error())
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
2022-01-05 07:28:27 +00:00
|
|
|
return nil, 0, status.Errorf(codes.InvalidArgument, "invalid tracer type %T", tracer)
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
return &result, txConfig.LogIndex + uint(len(res.Logs)), nil
|
2021-09-04 20:33:06 +00:00
|
|
|
}
|
2022-04-30 16:11:28 +00:00
|
|
|
|
|
|
|
// BaseFee implements the Query/BaseFee gRPC method
|
|
|
|
func (k Keeper) BaseFee(c context.Context, _ *types.QueryBaseFeeRequest) (*types.QueryBaseFeeResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(c)
|
|
|
|
|
|
|
|
params := k.GetParams(ctx)
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(k.eip155ChainID)
|
|
|
|
baseFee := k.GetBaseFee(ctx, ethCfg)
|
|
|
|
|
|
|
|
res := &types.QueryBaseFeeResponse{}
|
|
|
|
if baseFee != nil {
|
|
|
|
aux := sdk.NewIntFromBigInt(baseFee)
|
|
|
|
res.BaseFee = &aux
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|