2018-11-28 22:19:22 +00:00
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
2018-12-18 16:10:04 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
"math/big"
|
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"
|
2018-11-28 22:19:22 +00:00
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
2019-11-04 20:45:02 +00:00
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/ante"
|
2019-07-04 19:46:54 +00:00
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
|
"github.com/cosmos/ethermint/crypto"
|
2019-07-04 19:46:54 +00:00
|
|
|
|
emint "github.com/cosmos/ethermint/types"
|
2018-11-28 22:19:22 +00:00
|
|
|
|
evmtypes "github.com/cosmos/ethermint/x/evm/types"
|
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
|
ethcore "github.com/ethereum/go-ethereum/core"
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
|
tmcrypto "github.com/tendermint/tendermint/crypto"
|
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
|
|
|
|
// 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
|
|
|
|
|
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.
|
2019-07-04 19:46:54 +00:00
|
|
|
|
func NewAnteHandler(ak auth.AccountKeeper, sk types.SupplyKeeper) 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) {
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
|
switch castTx := tx.(type) {
|
|
|
|
|
case auth.StdTx:
|
2019-11-04 20:45:02 +00:00
|
|
|
|
stdAnte := sdk.ChainAnteDecorators(
|
|
|
|
|
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
|
|
|
|
|
ante.NewMempoolFeeDecorator(),
|
|
|
|
|
ante.NewValidateBasicDecorator(),
|
|
|
|
|
ante.NewValidateMemoDecorator(ak),
|
|
|
|
|
ante.NewConsumeGasForTxSizeDecorator(ak),
|
|
|
|
|
ante.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators
|
|
|
|
|
ante.NewValidateSigCountDecorator(ak),
|
|
|
|
|
ante.NewDeductFeeDecorator(ak, sk),
|
|
|
|
|
ante.NewSigGasConsumeDecorator(ak, consumeSigGas),
|
|
|
|
|
ante.NewSigVerificationDecorator(ak),
|
|
|
|
|
ante.NewIncrementSequenceDecorator(ak), // innermost AnteDecorator
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return stdAnte(ctx, tx, sim)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
|
|
case *evmtypes.EthereumTxMsg:
|
2019-10-18 23:14:38 +00:00
|
|
|
|
return ethAnteHandler(ctx, ak, sk, castTx, sim)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
|
|
default:
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return ctx, sdk.ErrInternal(fmt.Sprintf("transaction type invalid: %T", tx))
|
2019-07-04 19:46:54 +00:00
|
|
|
|
}
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
func consumeSigGas(
|
|
|
|
|
meter sdk.GasMeter, sig []byte, pubkey tmcrypto.PubKey, params types.Params,
|
|
|
|
|
) error {
|
2018-12-18 16:10:04 +00:00
|
|
|
|
switch pubkey.(type) {
|
|
|
|
|
case crypto.PubKeySecp256k1:
|
|
|
|
|
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: secp256k1")
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return nil
|
2019-08-19 15:31:40 +00:00
|
|
|
|
case tmcrypto.PubKey:
|
|
|
|
|
meter.ConsumeGas(secp256k1VerifyCost, "ante verify: tendermint secp256k1")
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return nil
|
2018-12-18 16:10:04 +00:00
|
|
|
|
default:
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return sdkerrors.Wrapf(sdkerrors.ErrInvalidPubKey, "unrecognized public key type: %T", pubkey)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
// Ethereum Ante Handler
|
|
|
|
|
|
|
|
|
|
// ethAnteHandler defines an internal ante handler for an Ethereum transaction
|
2018-12-18 16:10:04 +00:00
|
|
|
|
// ethTxMsg. During CheckTx, the transaction is passed through a series of
|
|
|
|
|
// pre-message execution validation checks such as signature and account
|
|
|
|
|
// verification in addition to minimum fees being checked. Otherwise, during
|
|
|
|
|
// DeliverTx, the transaction is simply passed to the EVM which will also
|
|
|
|
|
// perform the same series of checks. The distinction is made in CheckTx to
|
|
|
|
|
// prevent spam and DoS attacks.
|
2018-11-28 22:19:22 +00:00
|
|
|
|
func ethAnteHandler(
|
2019-10-18 23:14:38 +00:00
|
|
|
|
ctx sdk.Context, ak auth.AccountKeeper, sk types.SupplyKeeper,
|
|
|
|
|
ethTxMsg *evmtypes.EthereumTxMsg, sim bool,
|
2019-11-04 20:45:02 +00:00
|
|
|
|
) (newCtx sdk.Context, err error) {
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
|
var senderAddr sdk.AccAddress
|
|
|
|
|
|
|
|
|
|
// This is done to ignore costs in Ante handler checks
|
|
|
|
|
ctx = ctx.WithBlockGasMeter(sdk.NewInfiniteGasMeter())
|
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
|
if ctx.IsCheckTx() {
|
|
|
|
|
// Only perform pre-message (Ethereum transaction) execution validation
|
|
|
|
|
// during CheckTx. Otherwise, during DeliverTx the EVM will handle them.
|
2019-11-04 20:45:02 +00:00
|
|
|
|
if senderAddr, err = validateEthTxCheckTx(ctx, ak, ethTxMsg); err != nil {
|
|
|
|
|
return ctx, err
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// This is still currently needed to retrieve the sender address
|
2019-11-04 20:45:02 +00:00
|
|
|
|
if senderAddr, err = validateSignature(ctx, ethTxMsg); err != nil {
|
|
|
|
|
return ctx, err
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Explicit nonce check is also needed in case of multiple txs with same nonce not being handled
|
2019-11-04 20:45:02 +00:00
|
|
|
|
if err := checkNonce(ctx, ak, ethTxMsg, senderAddr); err != nil {
|
|
|
|
|
return ctx, err
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
|
// Recover and catch out of gas error
|
|
|
|
|
defer func() {
|
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
|
switch rType := r.(type) {
|
|
|
|
|
case sdk.ErrorOutOfGas:
|
|
|
|
|
log := fmt.Sprintf("out of gas in location: %v", rType.Descriptor)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
err = sdk.ErrOutOfGas(log)
|
2019-10-18 23:14:38 +00:00
|
|
|
|
default:
|
|
|
|
|
panic(r)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
2018-11-28 22:19:22 +00:00
|
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
|
// Fetch sender account from signature
|
2019-11-04 20:45:02 +00:00
|
|
|
|
senderAcc, err := auth.GetSignerAcc(ctx, ak, senderAddr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx, err
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
|
// Charge sender for gas up to limit
|
|
|
|
|
if ethTxMsg.Data.GasLimit != 0 {
|
|
|
|
|
// Cost calculates the fees paid to validators based on gas limit and price
|
|
|
|
|
cost := new(big.Int).Mul(ethTxMsg.Data.Price, new(big.Int).SetUint64(ethTxMsg.Data.GasLimit))
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
err = auth.DeductFees(sk, ctx, senderAcc, sdk.Coins{
|
2019-10-18 23:14:38 +00:00
|
|
|
|
sdk.NewCoin(emint.DenomDefault, sdk.NewIntFromBigInt(cost)),
|
|
|
|
|
})
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
return ctx, err
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
|
// Set gas meter after ante handler to ignore gaskv costs
|
|
|
|
|
newCtx = auth.SetGasMeter(sim, ctx, ethTxMsg.Data.GasLimit)
|
|
|
|
|
|
2019-10-29 15:52:26 +00:00
|
|
|
|
gas, _ := ethcore.IntrinsicGas(ethTxMsg.Data.Payload, ethTxMsg.To() == nil, true)
|
2019-10-18 23:14:38 +00:00
|
|
|
|
newCtx.GasMeter().ConsumeGas(gas, "eth intrinsic gas")
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return newCtx, nil
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func validateEthTxCheckTx(
|
|
|
|
|
ctx sdk.Context, ak auth.AccountKeeper, ethTxMsg *evmtypes.EthereumTxMsg,
|
2019-11-04 20:45:02 +00:00
|
|
|
|
) (sdk.AccAddress, error) {
|
2018-12-18 16:10:04 +00:00
|
|
|
|
// Validate sufficient fees have been provided that meet a minimum threshold
|
|
|
|
|
// defined by the proposer (for mempool purposes during CheckTx).
|
2019-11-04 20:45:02 +00:00
|
|
|
|
if err := ensureSufficientMempoolFees(ctx, ethTxMsg); err != nil {
|
|
|
|
|
return nil, err
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate enough intrinsic gas
|
2019-11-04 20:45:02 +00:00
|
|
|
|
if err := validateIntrinsicGas(ethTxMsg); err != nil {
|
|
|
|
|
return nil, err
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
signer, err := validateSignature(ctx, ethTxMsg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate account (nonce and balance checks)
|
2019-11-04 20:45:02 +00:00
|
|
|
|
if err := validateAccount(ctx, ak, ethTxMsg, signer); err != nil {
|
|
|
|
|
return nil, err
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return sdk.AccAddress(signer.Bytes()), nil
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validates signature and returns sender address
|
2019-11-04 20:45:02 +00:00
|
|
|
|
func validateSignature(ctx sdk.Context, ethTxMsg *evmtypes.EthereumTxMsg) (sdk.AccAddress, error) {
|
2019-10-18 23:14:38 +00:00
|
|
|
|
// parse the chainID from a string to a base-10 integer
|
|
|
|
|
chainID, ok := new(big.Int).SetString(ctx.ChainID(), 10)
|
|
|
|
|
if !ok {
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return nil, emint.ErrInvalidChainID(fmt.Sprintf("invalid chainID: %s", ctx.ChainID()))
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate sender/signature
|
|
|
|
|
signer, err := ethTxMsg.VerifySig(chainID)
|
|
|
|
|
if err != nil {
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return nil, sdk.ErrUnauthorized(fmt.Sprintf("signature verification failed: %s", err))
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return sdk.AccAddress(signer.Bytes()), nil
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validateIntrinsicGas validates that the Ethereum tx message has enough to
|
|
|
|
|
// cover intrinsic gas. Intrinsic gas for a transaction is the amount of gas
|
|
|
|
|
// that the transaction uses before the transaction is executed. The gas is a
|
|
|
|
|
// constant value of 21000 plus any cost inccured by additional bytes of data
|
|
|
|
|
// supplied with the transaction.
|
2019-11-04 20:45:02 +00:00
|
|
|
|
func validateIntrinsicGas(ethTxMsg *evmtypes.EthereumTxMsg) error {
|
2019-10-29 15:52:26 +00:00
|
|
|
|
gas, err := ethcore.IntrinsicGas(ethTxMsg.Data.Payload, ethTxMsg.To() == nil, true)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
if err != nil {
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return sdk.ErrInternal(fmt.Sprintf("failed to compute intrinsic gas cost: %s", err))
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ethTxMsg.Data.GasLimit < gas {
|
|
|
|
|
return sdk.ErrInternal(
|
|
|
|
|
fmt.Sprintf("intrinsic gas too low; %d < %d", ethTxMsg.Data.GasLimit, gas),
|
2019-11-04 20:45:02 +00:00
|
|
|
|
)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return nil
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validateAccount validates the account nonce and that the account has enough
|
|
|
|
|
// funds to cover the tx cost.
|
|
|
|
|
func validateAccount(
|
2019-10-18 23:14:38 +00:00
|
|
|
|
ctx sdk.Context, ak auth.AccountKeeper, ethTxMsg *evmtypes.EthereumTxMsg, signer sdk.AccAddress,
|
2019-11-04 20:45:02 +00:00
|
|
|
|
) error {
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
|
acc := ak.GetAccount(ctx, signer)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
|
|
// on InitChain make sure account number == 0
|
|
|
|
|
if ctx.BlockHeight() == 0 && acc.GetAccountNumber() != 0 {
|
|
|
|
|
return sdk.ErrInternal(
|
|
|
|
|
fmt.Sprintf(
|
|
|
|
|
"invalid account number for height zero; got %d, expected 0", acc.GetAccountNumber(),
|
2019-11-04 20:45:02 +00:00
|
|
|
|
))
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
|
// Validate nonce is correct
|
2019-11-04 20:45:02 +00:00
|
|
|
|
if err := checkNonce(ctx, ak, ethTxMsg, signer); err != nil {
|
|
|
|
|
return err
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// validate sender has enough funds
|
2019-07-04 19:46:54 +00:00
|
|
|
|
balance := acc.GetCoins().AmountOf(emint.DenomDefault)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
if balance.BigInt().Cmp(ethTxMsg.Cost()) < 0 {
|
|
|
|
|
return sdk.ErrInsufficientFunds(
|
|
|
|
|
fmt.Sprintf("insufficient funds: %s < %s", balance, ethTxMsg.Cost()),
|
2019-11-04 20:45:02 +00:00
|
|
|
|
)
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return nil
|
2018-12-18 16:10:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-18 23:14:38 +00:00
|
|
|
|
func checkNonce(
|
|
|
|
|
ctx sdk.Context, ak auth.AccountKeeper, ethTxMsg *evmtypes.EthereumTxMsg, signer sdk.AccAddress,
|
2019-11-04 20:45:02 +00:00
|
|
|
|
) error {
|
2019-10-18 23:14:38 +00:00
|
|
|
|
acc := ak.GetAccount(ctx, signer)
|
|
|
|
|
// Validate the transaction nonce is valid (equivalent to the sender account’s
|
|
|
|
|
// current nonce).
|
|
|
|
|
seq := acc.GetSequence()
|
|
|
|
|
if ethTxMsg.Data.AccountNonce != seq {
|
|
|
|
|
return sdk.ErrInvalidSequence(
|
2019-11-04 20:45:02 +00:00
|
|
|
|
fmt.Sprintf("invalid nonce; got %d, expected %d", ethTxMsg.Data.AccountNonce, seq))
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return nil
|
2019-10-18 23:14:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-18 16:10:04 +00:00
|
|
|
|
// ensureSufficientMempoolFees verifies that enough fees have been provided by the
|
|
|
|
|
// Ethereum transaction that meet the minimum threshold set by the block
|
|
|
|
|
// proposer.
|
|
|
|
|
//
|
|
|
|
|
// NOTE: This should only be ran during a CheckTx mode.
|
2019-11-04 20:45:02 +00:00
|
|
|
|
func ensureSufficientMempoolFees(ctx sdk.Context, ethTxMsg *evmtypes.EthereumTxMsg) error {
|
2018-12-18 16:10:04 +00:00
|
|
|
|
// fee = GP * GL
|
2019-07-04 19:46:54 +00:00
|
|
|
|
fee := sdk.NewDecCoinFromCoin(sdk.NewInt64Coin(emint.DenomDefault, ethTxMsg.Fee().Int64()))
|
|
|
|
|
|
|
|
|
|
minGasPrices := ctx.MinGasPrices()
|
|
|
|
|
allGTE := true
|
|
|
|
|
for _, v := range minGasPrices {
|
|
|
|
|
if !fee.IsGTE(v) {
|
|
|
|
|
allGTE = false
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-18 16:10:04 +00:00
|
|
|
|
|
|
|
|
|
// it is assumed that the minimum fees will only include the single valid denom
|
2019-07-04 19:46:54 +00:00
|
|
|
|
if !ctx.MinGasPrices().IsZero() && !allGTE {
|
2018-12-18 16:10:04 +00:00
|
|
|
|
// reject the transaction that does not meet the minimum fee
|
|
|
|
|
return sdk.ErrInsufficientFee(
|
2019-07-04 19:46:54 +00:00
|
|
|
|
fmt.Sprintf("insufficient fee, got: %q required: %q", fee, ctx.MinGasPrices()),
|
2019-11-04 20:45:02 +00:00
|
|
|
|
)
|
2018-11-28 22:19:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-11-04 20:45:02 +00:00
|
|
|
|
return nil
|
2018-11-28 22:19:22 +00:00
|
|
|
|
}
|