2020-04-01 18:49:21 +00:00
|
|
|
package ante
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
|
|
import (
|
2021-04-17 10:00:07 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
|
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
|
|
|
|
2020-10-06 18:57:55 +00:00
|
|
|
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
|
2018-11-28 22:19:22 +00:00
|
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
)
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
const (
|
2019-11-04 20:45:02 +00:00
|
|
|
// TODO: Use this cost per byte through parameter or overriding NewConsumeGasForTxSizeDecorator
|
|
|
|
// which currently defaults at 10, if intended
|
|
|
|
// memoCostPerByte sdk.Gas = 3
|
|
|
|
secp256k1VerifyCost uint64 = 21000
|
2018-12-18 16:10:04 +00:00
|
|
|
)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// AccountKeeper defines an expected keeper interface for the auth module's AccountKeeper
|
|
|
|
type AccountKeeper interface {
|
|
|
|
authante.AccountKeeper
|
|
|
|
NewAccountWithAddress(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI
|
|
|
|
}
|
|
|
|
|
|
|
|
// BankKeeper defines an expected keeper interface for the bank module's Keeper
|
|
|
|
type BankKeeper interface {
|
|
|
|
authtypes.BankKeeper
|
|
|
|
GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin
|
|
|
|
SetBalance(ctx sdk.Context, addr sdk.AccAddress, balance sdk.Coin) error
|
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
ak AccountKeeper, bankKeeper BankKeeper, evmKeeper EVMKeeper, signModeHandler authsigning.SignModeHandler,
|
|
|
|
) 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
|
|
|
|
switch tx.(type) {
|
2021-04-17 10:00:07 +00:00
|
|
|
case *evmtypes.MsgEthereumTx:
|
|
|
|
anteHandler = sdk.ChainAnteDecorators(
|
|
|
|
NewEthSetupContextDecorator(), // outermost AnteDecorator. EthSetUpContext must be called first
|
|
|
|
authante.NewRejectExtensionOptionsDecorator(),
|
|
|
|
NewEthMempoolFeeDecorator(evmKeeper),
|
|
|
|
authante.NewValidateBasicDecorator(),
|
|
|
|
NewEthSigVerificationDecorator(),
|
|
|
|
NewAccountVerificationDecorator(ak, bankKeeper, evmKeeper),
|
|
|
|
NewNonceVerificationDecorator(ak),
|
|
|
|
NewEthGasConsumeDecorator(ak, bankKeeper, evmKeeper),
|
|
|
|
NewIncrementSenderSequenceDecorator(ak), // innermost AnteDecorator.
|
|
|
|
)
|
|
|
|
|
|
|
|
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-04-17 10:00:07 +00:00
|
|
|
authante.TxTimeoutHeightDecorator{},
|
2020-04-16 15:47:39 +00:00
|
|
|
authante.NewValidateMemoDecorator(ak),
|
|
|
|
authante.NewConsumeGasForTxSizeDecorator(ak),
|
2021-04-17 10:00:07 +00:00
|
|
|
authante.NewRejectFeeGranterDecorator(),
|
2020-04-16 15:47:39 +00:00
|
|
|
authante.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators
|
|
|
|
authante.NewValidateSigCountDecorator(ak),
|
2021-04-17 10:00:07 +00:00
|
|
|
authante.NewDeductFeeDecorator(ak, bankKeeper),
|
|
|
|
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:
|
2020-04-16 15:47:39 +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-04-17 10:00:07 +00:00
|
|
|
var _ = DefaultSigVerificationGasConsumer
|
|
|
|
|
|
|
|
// 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-17 10:00:07 +00:00
|
|
|
pubkey := sig.PubKey
|
|
|
|
switch pubkey := pubkey.(type) {
|
|
|
|
case *ed25519.PubKey:
|
|
|
|
meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519")
|
2019-11-04 20:45:02 +00:00
|
|
|
return nil
|
2020-09-17 22:04:36 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
case *secp256k1.PubKey:
|
|
|
|
meter.ConsumeGas(params.SigVerifyCostSecp256k1, "ante verify: secp256k1")
|
|
|
|
return nil
|
2020-09-17 22:04:36 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
// support for etherum ECDSA secp256k1 keys
|
|
|
|
case *ethsecp256k1.PubKey:
|
|
|
|
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: eth_secp256k1")
|
|
|
|
return nil
|
2020-09-17 22:04:36 +00:00
|
|
|
|
2021-04-17 10:00:07 +00:00
|
|
|
case multisig.PubKey:
|
|
|
|
multisignature, ok := sig.Data.(*signing.MultiSignatureData)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("expected %T, got, %T", &signing.MultiSignatureData{}, sig.Data)
|
2020-09-23 14:49:20 +00:00
|
|
|
}
|
2021-04-17 10:00:07 +00:00
|
|
|
err := authante.ConsumeMultisignatureVerificationGas(meter, multisignature, pubkey, params, sig.Sequence)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized public key type: %T", pubkey)
|
2020-09-17 22:04:36 +00:00
|
|
|
}
|
|
|
|
}
|