2020-12-02 17:37:11 +00:00
|
|
|
package ante
|
|
|
|
|
|
|
|
import (
|
2022-04-21 20:16:28 +00:00
|
|
|
"fmt"
|
|
|
|
"runtime/debug"
|
|
|
|
|
2020-12-02 17:37:11 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
2022-04-21 20:16:28 +00:00
|
|
|
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
2022-01-08 00:39:27 +00:00
|
|
|
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
|
2022-04-21 20:16:28 +00:00
|
|
|
ibcante "github.com/cosmos/ibc-go/v3/modules/core/ante"
|
|
|
|
ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper"
|
|
|
|
tmlog "github.com/tendermint/tendermint/libs/log"
|
|
|
|
evmante "github.com/tharsis/ethermint/app/ante"
|
|
|
|
evmtypes "github.com/tharsis/ethermint/x/evm/types"
|
2020-12-02 17:37:11 +00:00
|
|
|
)
|
|
|
|
|
2022-04-21 20:16:28 +00:00
|
|
|
// HandlerOptions extend the SDK's AnteHandler options by requiring the IBC
|
|
|
|
// channel keeper, EVM Keeper and Fee Market Keeper.
|
|
|
|
type HandlerOptions struct {
|
|
|
|
AccountKeeper evmtypes.AccountKeeper
|
|
|
|
BankKeeper evmtypes.BankKeeper
|
|
|
|
IBCKeeper *ibckeeper.Keeper
|
|
|
|
EvmKeeper evmante.EVMKeeper
|
|
|
|
FeegrantKeeper authante.FeegrantKeeper
|
|
|
|
SignModeHandler authsigning.SignModeHandler
|
|
|
|
SigGasConsumer authante.SignatureVerificationGasConsumer
|
|
|
|
FeeMarketKeeper evmtypes.FeeMarketKeeper
|
|
|
|
MaxTxGasWanted uint64
|
|
|
|
AddressFetchers []AddressFetcher
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
|
2022-04-21 20:16:28 +00:00
|
|
|
func (options HandlerOptions) Validate() error {
|
|
|
|
if options.AccountKeeper == nil {
|
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrLogic, "account keeper is required for AnteHandler")
|
2022-01-08 00:39:27 +00:00
|
|
|
}
|
2022-04-21 20:16:28 +00:00
|
|
|
if options.BankKeeper == nil {
|
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrLogic, "bank keeper is required for AnteHandler")
|
2022-01-08 00:39:27 +00:00
|
|
|
}
|
2022-04-21 20:16:28 +00:00
|
|
|
if options.SignModeHandler == nil {
|
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
|
|
|
|
}
|
|
|
|
if options.EvmKeeper == nil {
|
|
|
|
return sdkerrors.Wrap(sdkerrors.ErrLogic, "evm keeper is required for AnteHandler")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
|
2022-04-21 20:16:28 +00:00
|
|
|
// NewAnteHandler returns an 'AnteHandler' that will run actions before a tx is sent to a module's handler.
|
|
|
|
func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
|
|
|
|
if err := options.Validate(); err != nil {
|
|
|
|
return nil, err
|
2022-01-08 00:39:27 +00:00
|
|
|
}
|
2020-12-02 17:37:11 +00:00
|
|
|
|
2022-04-21 20:16:28 +00:00
|
|
|
return func(
|
|
|
|
ctx sdk.Context, tx sdk.Tx, sim bool,
|
|
|
|
) (newCtx sdk.Context, err error) {
|
|
|
|
var anteHandler sdk.AnteHandler
|
|
|
|
|
|
|
|
defer Recover(ctx.Logger(), &err)
|
|
|
|
|
|
|
|
txWithExtensions, ok := tx.(authante.HasExtensionOptionsTx)
|
|
|
|
if ok {
|
|
|
|
opts := txWithExtensions.GetExtensionOptions()
|
|
|
|
if len(opts) > 0 {
|
|
|
|
switch typeURL := opts[0].GetTypeUrl(); typeURL {
|
|
|
|
case "/ethermint.evm.v1.ExtensionOptionsEthereumTx":
|
|
|
|
// handle as *evmtypes.MsgEthereumTx
|
|
|
|
anteHandler = newEthAnteHandler(options)
|
|
|
|
default:
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrUnknownExtensionOptions,
|
|
|
|
"rejecting tx with unsupported extension option: %s", typeURL,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return anteHandler(ctx, tx, sim)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle as totally normal Cosmos SDK tx
|
|
|
|
switch tx.(type) {
|
|
|
|
case sdk.Tx:
|
|
|
|
anteHandler = newCosmosAnteHandler(options)
|
|
|
|
default:
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return anteHandler(ctx, tx, sim)
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCosmosAnteHandler(options HandlerOptions) sdk.AnteHandler {
|
2022-01-08 00:39:27 +00:00
|
|
|
decorators := []sdk.AnteDecorator{}
|
2020-12-02 17:37:11 +00:00
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
decorators = append(decorators,
|
2022-04-21 20:16:28 +00:00
|
|
|
evmante.RejectMessagesDecorator{}, // reject MsgEthereumTxs
|
|
|
|
authante.NewSetUpContextDecorator(), // second decorator. SetUpContext must be called before other decorators
|
|
|
|
authante.NewRejectExtensionOptionsDecorator(),
|
2022-01-08 00:39:27 +00:00
|
|
|
)
|
2022-04-21 20:16:28 +00:00
|
|
|
if len(options.AddressFetchers) > 0 {
|
|
|
|
decorators = append(decorators, NewAuthenticatedMempoolDecorator(options.AddressFetchers...))
|
2020-12-02 17:37:11 +00:00
|
|
|
}
|
|
|
|
decorators = append(decorators,
|
2022-04-21 20:16:28 +00:00
|
|
|
authante.NewMempoolFeeDecorator(),
|
2022-01-08 00:39:27 +00:00
|
|
|
NewVestingAccountDecorator(),
|
2022-04-21 20:16:28 +00:00
|
|
|
authante.NewValidateBasicDecorator(),
|
|
|
|
authante.NewTxTimeoutHeightDecorator(),
|
|
|
|
authante.NewValidateMemoDecorator(options.AccountKeeper),
|
|
|
|
authante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
|
|
|
|
authante.NewDeductFeeDecorator(options.AccountKeeper, options.BankKeeper, options.FeegrantKeeper),
|
|
|
|
authante.NewSetPubKeyDecorator(options.AccountKeeper), // SetPubKeyDecorator must be called before all signature verification decorators
|
|
|
|
authante.NewValidateSigCountDecorator(options.AccountKeeper),
|
|
|
|
authante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
|
|
|
|
authante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
|
|
|
|
authante.NewIncrementSequenceDecorator(options.AccountKeeper), // innermost AnteDecorator
|
|
|
|
ibcante.NewAnteDecorator(options.IBCKeeper),
|
|
|
|
)
|
|
|
|
return sdk.ChainAnteDecorators(decorators...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newEthAnteHandler(options HandlerOptions) sdk.AnteHandler {
|
|
|
|
return sdk.ChainAnteDecorators(
|
|
|
|
evmante.NewEthSetUpContextDecorator(options.EvmKeeper), // outermost AnteDecorator. SetUpContext must be called first
|
|
|
|
evmante.NewEthMempoolFeeDecorator(options.EvmKeeper), // Check eth effective gas price against minimal-gas-prices
|
|
|
|
evmante.NewEthValidateBasicDecorator(options.EvmKeeper),
|
|
|
|
evmante.NewEthSigVerificationDecorator(options.EvmKeeper),
|
|
|
|
evmante.NewEthAccountVerificationDecorator(options.AccountKeeper, options.BankKeeper, options.EvmKeeper),
|
|
|
|
evmante.NewEthGasConsumeDecorator(options.EvmKeeper, options.MaxTxGasWanted),
|
|
|
|
evmante.NewCanTransferDecorator(options.EvmKeeper),
|
|
|
|
evmante.NewEthIncrementSenderSequenceDecorator(options.AccountKeeper), // innermost AnteDecorator.
|
2020-12-02 17:37:11 +00:00
|
|
|
)
|
2022-04-21 20:16:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Recover(logger tmlog.Logger, err *error) {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
*err = sdkerrors.Wrapf(sdkerrors.ErrPanic, "%v", r)
|
|
|
|
|
|
|
|
if e, ok := r.(error); ok {
|
|
|
|
logger.Error(
|
|
|
|
"ante handler panicked",
|
|
|
|
"error", e,
|
|
|
|
"stack trace", string(debug.Stack()),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
logger.Error(
|
|
|
|
"ante handler panicked",
|
|
|
|
"recover", fmt.Sprintf("%v", r),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 17:37:11 +00:00
|
|
|
}
|