laconicd/app/ante/ante.go

149 lines
4.9 KiB
Go
Raw Normal View History

package ante
2018-11-28 22:19:22 +00:00
import (
"fmt"
2021-04-18 16:39:15 +00:00
"runtime/debug"
2021-06-15 07:19:31 +00:00
"github.com/palantir/stacktrace"
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"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
2021-04-17 10:00:07 +00:00
"github.com/cosmos/cosmos-sdk/types/tx/signing"
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
channelkeeper "github.com/cosmos/ibc-go/v2/modules/core/04-channel/keeper"
ibcante "github.com/cosmos/ibc-go/v2/modules/core/ante"
"github.com/tharsis/ethermint/crypto/ethsecp256k1"
evmtypes "github.com/tharsis/ethermint/x/evm/types"
2018-11-28 22:19:22 +00:00
)
const (
secp256k1VerifyCost uint64 = 21000
)
2018-11-28 22:19:22 +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(
ak evmtypes.AccountKeeper,
bankKeeper evmtypes.BankKeeper,
2021-04-18 16:39:15 +00:00
evmKeeper EVMKeeper,
feeGrantKeeper authante.FeegrantKeeper,
channelKeeper channelkeeper.Keeper,
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,
) (newCtx sdk.Context, err error) {
var anteHandler sdk.AnteHandler
2021-04-17 10:00:07 +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 {
case "/ethermint.evm.v1.ExtensionOptionsEthereumTx":
2021-04-18 16:39:15 +00:00
// handle as *evmtypes.MsgEthereumTx
anteHandler = sdk.ChainAnteDecorators(
NewEthSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
authante.NewMempoolFeeDecorator(),
authante.NewTxTimeoutHeightDecorator(),
authante.NewValidateMemoDecorator(ak),
NewEthValidateBasicDecorator(evmKeeper),
NewEthSigVerificationDecorator(evmKeeper),
2021-04-18 16:39:15 +00:00
NewEthAccountVerificationDecorator(ak, bankKeeper, evmKeeper),
NewEthNonceVerificationDecorator(ak),
NewEthGasConsumeDecorator(evmKeeper),
NewCanTransferDecorator(evmKeeper, feeMarketKeeper),
2021-04-18 16:39:15 +00:00
NewEthIncrementSenderSequenceDecorator(ak), // innermost AnteDecorator.
)
default:
2021-06-15 07:19:31 +00:00
return ctx, stacktrace.Propagate(
sdkerrors.Wrap(sdkerrors.ErrUnknownExtensionOptions, typeURL),
"rejecting tx with unsupported extension option",
)
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:
anteHandler = sdk.ChainAnteDecorators(
authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
2021-04-17 10:00:07 +00:00
authante.NewRejectExtensionOptionsDecorator(),
authante.NewMempoolFeeDecorator(),
authante.NewValidateBasicDecorator(),
authante.NewTxTimeoutHeightDecorator(),
authante.NewValidateMemoDecorator(ak),
ibcante.NewAnteDecorator(channelKeeper),
authante.NewConsumeGasForTxSizeDecorator(ak),
authante.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators
authante.NewValidateSigCountDecorator(ak),
authante.NewDeductFeeDecorator(ak, bankKeeper, feeGrantKeeper),
2021-04-17 10:00:07 +00:00
authante.NewSigGasConsumeDecorator(ak, DefaultSigVerificationGasConsumer),
authante.NewSigVerificationDecorator(ak, signModeHandler),
authante.NewIncrementSequenceDecorator(ak), // innermost AnteDecorator
)
default:
2021-06-15 07:19:31 +00:00
return ctx, stacktrace.Propagate(
sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx),
"transaction is not an SDK tx",
)
}
return anteHandler(ctx, tx, sim)
}
}
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 {
logger.Error(
"ante handler panicked",
"error", e,
"stack trace", string(debug.Stack()),
)
2021-04-18 16:39:15 +00:00
} else {
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,
) error {
2021-04-18 16:39:15 +00:00
// support for ethereum ECDSA secp256k1 keys
_, ok := sig.PubKey.(*ethsecp256k1.PubKey)
if ok {
2021-04-17 10:00:07 +00:00
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: eth_secp256k1")
return nil
}
return authante.DefaultSigVerificationGasConsumer(meter, sig, params)
}