2020-04-01 18:49:21 +00:00
|
|
|
package ante
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
import (
|
2021-08-19 16:55:13 +00:00
|
|
|
"fmt"
|
2021-04-18 16:39:15 +00:00
|
|
|
"runtime/debug"
|
|
|
|
|
2021-08-19 16:55:13 +00:00
|
|
|
tmlog "github.com/tendermint/tendermint/libs/log"
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2018-11-28 22:19:22 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-11-04 20:45:02 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2021-04-17 10:00:07 +00:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
2020-04-16 15:47:39 +00:00
|
|
|
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
2021-04-17 10:00:07 +00:00
|
|
|
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2021-10-22 13:38:52 +00:00
|
|
|
channelkeeper "github.com/cosmos/ibc-go/v2/modules/core/04-channel/keeper"
|
|
|
|
ibcante "github.com/cosmos/ibc-go/v2/modules/core/ante"
|
2021-07-26 19:26:53 +00:00
|
|
|
|
2021-06-22 10:49:18 +00:00
|
|
|
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
|
2021-09-03 15:55:37 +00:00
|
|
|
evmtypes "github.com/tharsis/ethermint/x/evm/types"
|
2018-11-28 22:19:22 +00:00
|
|
|
)
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
const (
|
2019-11-04 20:45:02 +00:00
|
|
|
secp256k1VerifyCost uint64 = 21000
|
2018-12-18 16:10:04 +00:00
|
|
|
)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
// NewAnteHandler returns an ante handler responsible for attempting to route an
|
2018-11-28 22:19:22 +00:00
|
|
|
// Ethereum or SDK transaction to an internal ante handler for performing
|
|
|
|
// transaction-level processing (e.g. fee payment, signature verification) before
|
|
|
|
// being passed onto it's respective handler.
|
2021-04-17 10:00:07 +00:00
|
|
|
func NewAnteHandler(
|
2021-09-03 15:55:37 +00:00
|
|
|
ak evmtypes.AccountKeeper,
|
|
|
|
bankKeeper evmtypes.BankKeeper,
|
2021-04-18 16:39:15 +00:00
|
|
|
evmKeeper EVMKeeper,
|
2021-06-29 17:02:21 +00:00
|
|
|
feeGrantKeeper authante.FeegrantKeeper,
|
2021-07-26 19:26:53 +00:00
|
|
|
channelKeeper channelkeeper.Keeper,
|
2021-10-04 14:58:06 +00:00
|
|
|
feeMarketKeeper evmtypes.FeeMarketKeeper,
|
2021-04-18 16:39:15 +00:00
|
|
|
signModeHandler authsigning.SignModeHandler,
|
2021-04-17 10:00:07 +00:00
|
|
|
) sdk.AnteHandler {
|
2018-11-28 22:19:22 +00:00
|
|
|
return func(
|
|
|
|
ctx sdk.Context, tx sdk.Tx, sim bool,
|
2019-11-04 20:45:02 +00:00
|
|
|
) (newCtx sdk.Context, err error) {
|
2020-04-17 22:32:01 +00:00
|
|
|
var anteHandler sdk.AnteHandler
|
2021-04-17 10:00:07 +00:00
|
|
|
|
2021-08-19 16:55:13 +00:00
|
|
|
defer Recover(ctx.Logger(), &err)
|
2021-04-18 16:39:15 +00:00
|
|
|
|
|
|
|
txWithExtensions, ok := tx.(authante.HasExtensionOptionsTx)
|
|
|
|
if ok {
|
|
|
|
opts := txWithExtensions.GetExtensionOptions()
|
|
|
|
if len(opts) > 0 {
|
|
|
|
switch typeURL := opts[0].GetTypeUrl(); typeURL {
|
2021-08-17 15:30:35 +00:00
|
|
|
case "/ethermint.evm.v1.ExtensionOptionsEthereumTx":
|
2021-04-18 16:39:15 +00:00
|
|
|
// handle as *evmtypes.MsgEthereumTx
|
|
|
|
|
|
|
|
anteHandler = sdk.ChainAnteDecorators(
|
2021-12-15 02:17:03 +00:00
|
|
|
NewEthSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
|
|
|
|
NewEthMempoolFeeDecorator(evmKeeper, feeMarketKeeper), // Check eth effective gas price against minimal-gas-prices
|
2021-06-29 17:02:21 +00:00
|
|
|
authante.NewTxTimeoutHeightDecorator(),
|
2021-06-21 07:59:45 +00:00
|
|
|
authante.NewValidateMemoDecorator(ak),
|
2021-11-08 13:00:35 +00:00
|
|
|
NewEthValidateBasicDecorator(evmKeeper),
|
2021-05-10 16:34:00 +00:00
|
|
|
NewEthSigVerificationDecorator(evmKeeper),
|
2021-04-18 16:39:15 +00:00
|
|
|
NewEthAccountVerificationDecorator(ak, bankKeeper, evmKeeper),
|
|
|
|
NewEthNonceVerificationDecorator(ak),
|
2021-09-22 10:26:29 +00:00
|
|
|
NewEthGasConsumeDecorator(evmKeeper),
|
2021-10-04 14:58:06 +00:00
|
|
|
NewCanTransferDecorator(evmKeeper, feeMarketKeeper),
|
2021-04-18 16:39:15 +00:00
|
|
|
NewEthIncrementSenderSequenceDecorator(ak), // innermost AnteDecorator.
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrUnknownExtensionOptions,
|
|
|
|
"rejecting tx with unsupported extension option: %s",
|
|
|
|
typeURL,
|
2021-06-15 07:19:31 +00:00
|
|
|
)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return anteHandler(ctx, tx, sim)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle as totally normal Cosmos SDK tx
|
|
|
|
|
|
|
|
switch tx.(type) {
|
2021-04-17 10:00:07 +00:00
|
|
|
case sdk.Tx:
|
2020-04-17 22:32:01 +00:00
|
|
|
anteHandler = sdk.ChainAnteDecorators(
|
2020-04-16 15:47:39 +00:00
|
|
|
authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
|
2021-04-17 10:00:07 +00:00
|
|
|
authante.NewRejectExtensionOptionsDecorator(),
|
2020-04-16 15:47:39 +00:00
|
|
|
authante.NewMempoolFeeDecorator(),
|
|
|
|
authante.NewValidateBasicDecorator(),
|
2021-06-29 17:02:21 +00:00
|
|
|
authante.NewTxTimeoutHeightDecorator(),
|
2020-04-16 15:47:39 +00:00
|
|
|
authante.NewValidateMemoDecorator(ak),
|
2021-07-26 19:26:53 +00:00
|
|
|
ibcante.NewAnteDecorator(channelKeeper),
|
2020-04-16 15:47:39 +00:00
|
|
|
authante.NewConsumeGasForTxSizeDecorator(ak),
|
|
|
|
authante.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators
|
|
|
|
authante.NewValidateSigCountDecorator(ak),
|
2021-06-29 17:02:21 +00:00
|
|
|
authante.NewDeductFeeDecorator(ak, bankKeeper, feeGrantKeeper),
|
2021-04-17 10:00:07 +00:00
|
|
|
authante.NewSigGasConsumeDecorator(ak, DefaultSigVerificationGasConsumer),
|
|
|
|
authante.NewSigVerificationDecorator(ak, signModeHandler),
|
2020-08-23 21:41:54 +00:00
|
|
|
authante.NewIncrementSequenceDecorator(ak), // innermost AnteDecorator
|
2019-11-04 20:45:02 +00:00
|
|
|
)
|
2018-12-18 16:10:04 +00:00
|
|
|
default:
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx)
|
2019-07-04 19:46:54 +00:00
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
|
|
|
return anteHandler(ctx, tx, sim)
|
2018-12-18 16:10:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 16:55:13 +00:00
|
|
|
func Recover(logger tmlog.Logger, err *error) {
|
2021-04-18 16:39:15 +00:00
|
|
|
if r := recover(); r != nil {
|
|
|
|
*err = sdkerrors.Wrapf(sdkerrors.ErrPanic, "%v", r)
|
|
|
|
|
|
|
|
if e, ok := r.(error); ok {
|
2021-08-19 16:55:13 +00:00
|
|
|
logger.Error(
|
|
|
|
"ante handler panicked",
|
|
|
|
"error", e,
|
|
|
|
"stack trace", string(debug.Stack()),
|
|
|
|
)
|
2021-04-18 16:39:15 +00:00
|
|
|
} else {
|
2021-08-19 16:55:13 +00:00
|
|
|
logger.Error(
|
|
|
|
"ante handler panicked",
|
|
|
|
"recover", fmt.Sprintf("%v", r),
|
|
|
|
)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ authante.SignatureVerificationGasConsumer = DefaultSigVerificationGasConsumer
|
2021-04-17 10:00:07 +00:00
|
|
|
|
|
|
|
// DefaultSigVerificationGasConsumer is the default implementation of SignatureVerificationGasConsumer. It consumes gas
|
|
|
|
// for signature verification based upon the public key type. The cost is fetched from the given params and is matched
|
|
|
|
// by the concrete type.
|
|
|
|
func DefaultSigVerificationGasConsumer(
|
|
|
|
meter sdk.GasMeter, sig signing.SignatureV2, params authtypes.Params,
|
2019-11-04 20:45:02 +00:00
|
|
|
) error {
|
2021-04-18 16:39:15 +00:00
|
|
|
// support for ethereum ECDSA secp256k1 keys
|
2021-05-31 09:05:32 +00:00
|
|
|
_, ok := sig.PubKey.(*ethsecp256k1.PubKey)
|
|
|
|
if ok {
|
2021-04-17 10:00:07 +00:00
|
|
|
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: eth_secp256k1")
|
|
|
|
return nil
|
2020-09-17 22:04:36 +00:00
|
|
|
}
|
2021-05-31 09:05:32 +00:00
|
|
|
|
|
|
|
return authante.DefaultSigVerificationGasConsumer(meter, sig, params)
|
2020-09-17 22:04:36 +00:00
|
|
|
}
|