127 lines
4.8 KiB
Go
127 lines
4.8 KiB
Go
package ante
|
|
|
|
import (
|
|
"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"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
"github.com/cosmos/cosmos-sdk/types/tx/signing"
|
|
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
|
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
"github.com/cosmos/ethermint/crypto/ethsecp256k1"
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
|
)
|
|
|
|
const (
|
|
// 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
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// NewAnteHandler returns an ante handler responsible for attempting to route an
|
|
// 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.
|
|
func NewAnteHandler(
|
|
ak AccountKeeper, bankKeeper BankKeeper, evmKeeper EVMKeeper, signModeHandler authsigning.SignModeHandler,
|
|
) sdk.AnteHandler {
|
|
return func(
|
|
ctx sdk.Context, tx sdk.Tx, sim bool,
|
|
) (newCtx sdk.Context, err error) {
|
|
var anteHandler sdk.AnteHandler
|
|
switch tx.(type) {
|
|
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:
|
|
anteHandler = sdk.ChainAnteDecorators(
|
|
authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
|
|
authante.NewRejectExtensionOptionsDecorator(),
|
|
authante.NewMempoolFeeDecorator(),
|
|
authante.NewValidateBasicDecorator(),
|
|
authante.TxTimeoutHeightDecorator{},
|
|
authante.NewValidateMemoDecorator(ak),
|
|
authante.NewConsumeGasForTxSizeDecorator(ak),
|
|
authante.NewRejectFeeGranterDecorator(),
|
|
authante.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators
|
|
authante.NewValidateSigCountDecorator(ak),
|
|
authante.NewDeductFeeDecorator(ak, bankKeeper),
|
|
authante.NewSigGasConsumeDecorator(ak, DefaultSigVerificationGasConsumer),
|
|
authante.NewSigVerificationDecorator(ak, signModeHandler),
|
|
authante.NewIncrementSequenceDecorator(ak), // innermost AnteDecorator
|
|
)
|
|
default:
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid transaction type: %T", tx)
|
|
}
|
|
|
|
return anteHandler(ctx, tx, sim)
|
|
}
|
|
}
|
|
|
|
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,
|
|
) error {
|
|
pubkey := sig.PubKey
|
|
switch pubkey := pubkey.(type) {
|
|
case *ed25519.PubKey:
|
|
meter.ConsumeGas(params.SigVerifyCostED25519, "ante verify: ed25519")
|
|
return nil
|
|
|
|
case *secp256k1.PubKey:
|
|
meter.ConsumeGas(params.SigVerifyCostSecp256k1, "ante verify: secp256k1")
|
|
return nil
|
|
|
|
// support for etherum ECDSA secp256k1 keys
|
|
case *ethsecp256k1.PubKey:
|
|
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: eth_secp256k1")
|
|
return nil
|
|
|
|
case multisig.PubKey:
|
|
multisignature, ok := sig.Data.(*signing.MultiSignatureData)
|
|
if !ok {
|
|
return fmt.Errorf("expected %T, got, %T", &signing.MultiSignatureData{}, sig.Data)
|
|
}
|
|
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)
|
|
}
|
|
}
|