2020-04-17 22:32:01 +00:00
|
|
|
package ante
|
|
|
|
|
|
|
|
import (
|
2021-09-05 11:03:06 +00:00
|
|
|
"errors"
|
2022-08-05 13:00:31 +00:00
|
|
|
"math"
|
2020-04-17 22:32:01 +00:00
|
|
|
"math/big"
|
2022-05-31 10:26:40 +00:00
|
|
|
"strconv"
|
2020-04-17 22:32:01 +00:00
|
|
|
|
2022-07-28 13:43:49 +00:00
|
|
|
sdkmath "cosmossdk.io/math"
|
|
|
|
|
2020-04-17 22:32:01 +00:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
|
|
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
|
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
|
|
|
evmkeeper "github.com/evmos/ethermint/x/evm/keeper"
|
|
|
|
"github.com/evmos/ethermint/x/evm/statedb"
|
|
|
|
evmtypes "github.com/evmos/ethermint/x/evm/types"
|
2020-04-17 22:32:01 +00:00
|
|
|
|
2020-06-22 16:07:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-05-10 16:34:00 +00:00
|
|
|
ethtypes "github.com/ethereum/go-ethereum/core/types"
|
2020-04-17 22:32:01 +00:00
|
|
|
)
|
|
|
|
|
2021-06-02 08:52:53 +00:00
|
|
|
// EthSigVerificationDecorator validates an ethereum signatures
|
2021-04-18 16:39:15 +00:00
|
|
|
type EthSigVerificationDecorator struct {
|
2021-05-10 16:34:00 +00:00
|
|
|
evmKeeper EVMKeeper
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
|
|
|
// NewEthSigVerificationDecorator creates a new EthSigVerificationDecorator
|
2021-05-10 16:34:00 +00:00
|
|
|
func NewEthSigVerificationDecorator(ek EVMKeeper) EthSigVerificationDecorator {
|
|
|
|
return EthSigVerificationDecorator{
|
|
|
|
evmKeeper: ek,
|
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
// AnteHandle validates checks that the registered chain id is the same as the one on the message, and
|
|
|
|
// that the signer address matches the one defined on the message.
|
2021-06-25 13:28:28 +00:00
|
|
|
// It's not skipped for RecheckTx, because it set `From` address which is critical from other ante handler to work.
|
|
|
|
// Failure in RecheckTx will prevent tx to be included into block, especially when CheckTx succeed, in which case user
|
|
|
|
// won't see the error message.
|
2020-04-17 22:32:01 +00:00
|
|
|
func (esvd EthSigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
2021-05-31 09:05:32 +00:00
|
|
|
chainID := esvd.evmKeeper.ChainID()
|
2022-10-19 16:21:59 +00:00
|
|
|
chainCfg := esvd.evmKeeper.GetChainConfig(ctx)
|
|
|
|
ethCfg := chainCfg.EthereumConfig(chainID)
|
2021-05-10 16:34:00 +00:00
|
|
|
blockNum := big.NewInt(ctx.BlockHeight())
|
|
|
|
signer := ethtypes.MakeSigner(ethCfg, blockNum)
|
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
for _, msg := range tx.GetMsgs() {
|
|
|
|
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
|
|
|
|
}
|
2021-05-10 16:34:00 +00:00
|
|
|
|
2022-10-19 16:21:59 +00:00
|
|
|
allowUnprotectedTxs := esvd.evmKeeper.GetAllowUnprotectedTxs(ctx)
|
2022-06-13 09:41:53 +00:00
|
|
|
ethTx := msgEthTx.AsTransaction()
|
2022-10-19 16:21:59 +00:00
|
|
|
if !allowUnprotectedTxs && !ethTx.Protected() {
|
2022-06-22 10:51:14 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrNotSupported,
|
|
|
|
"rejected unprotected Ethereum txs. Please EIP155 sign your transaction to protect it against replay-attacks")
|
2022-06-13 09:41:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sender, err := signer.Sender(ethTx)
|
2022-01-14 09:37:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrorInvalidSigner,
|
2022-07-19 15:12:48 +00:00
|
|
|
"couldn't retrieve sender address from the ethereum transaction: %s",
|
2022-01-14 09:37:33 +00:00
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
// set up the sender to the transaction field if not already
|
|
|
|
msgEthTx.From = sender.Hex()
|
|
|
|
}
|
2021-06-11 13:38:51 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
return next(ctx, tx, simulate)
|
2021-04-18 16:39:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EthAccountVerificationDecorator validates an account balance checks
|
|
|
|
type EthAccountVerificationDecorator struct {
|
2022-05-02 16:48:40 +00:00
|
|
|
ak evmtypes.AccountKeeper
|
|
|
|
evmKeeper EVMKeeper
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
// NewEthAccountVerificationDecorator creates a new EthAccountVerificationDecorator
|
2022-05-02 16:48:40 +00:00
|
|
|
func NewEthAccountVerificationDecorator(ak evmtypes.AccountKeeper, ek EVMKeeper) EthAccountVerificationDecorator {
|
2021-04-18 16:39:15 +00:00
|
|
|
return EthAccountVerificationDecorator{
|
2022-05-02 16:48:40 +00:00
|
|
|
ak: ak,
|
|
|
|
evmKeeper: ek,
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 08:52:53 +00:00
|
|
|
// AnteHandle validates checks that the sender balance is greater than the total transaction cost.
|
|
|
|
// The account will be set to store if it doesn't exis, i.e cannot be found on store.
|
|
|
|
// This AnteHandler decorator will fail if:
|
|
|
|
// - any of the msgs is not a MsgEthereumTx
|
|
|
|
// - from address is empty
|
|
|
|
// - account balance is lower than the transaction cost
|
2022-08-26 10:30:55 +00:00
|
|
|
func (avd EthAccountVerificationDecorator) AnteHandle(
|
|
|
|
ctx sdk.Context,
|
|
|
|
tx sdk.Tx,
|
|
|
|
simulate bool,
|
|
|
|
next sdk.AnteHandler,
|
|
|
|
) (newCtx sdk.Context, err error) {
|
2020-04-17 22:32:01 +00:00
|
|
|
if !ctx.IsCheckTx() {
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
|
|
|
|
2021-06-15 07:19:31 +00:00
|
|
|
for i, msg := range tx.GetMsgs() {
|
2021-05-31 09:05:32 +00:00
|
|
|
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
2022-01-14 09:37:33 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
|
2021-05-31 09:05:32 +00:00
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
txData, err := evmtypes.UnpackTxData(msgEthTx.Data)
|
|
|
|
if err != nil {
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(err, "failed to unpack tx data any for tx %d", i)
|
2021-07-05 16:39:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
// sender address should be in the tx cache from the previous AnteHandle call
|
|
|
|
from := msgEthTx.GetFrom()
|
|
|
|
if from.Empty() {
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "from address cannot be empty")
|
2021-05-31 09:05:32 +00:00
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
2021-06-15 17:39:41 +00:00
|
|
|
// check whether the sender address is EOA
|
|
|
|
fromAddr := common.BytesToAddress(from)
|
2022-01-05 18:18:02 +00:00
|
|
|
acct := avd.evmKeeper.GetAccount(ctx, fromAddr)
|
|
|
|
|
2022-01-05 07:28:27 +00:00
|
|
|
if acct == nil {
|
|
|
|
acc := avd.ak.NewAccountWithAddress(ctx, from)
|
2021-06-30 09:31:30 +00:00
|
|
|
avd.ak.SetAccount(ctx, acc)
|
2022-01-05 07:28:27 +00:00
|
|
|
acct = statedb.NewEmptyAccount()
|
|
|
|
} else if acct.IsContract() {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidType,
|
|
|
|
"the sender is not EOA: address %s, codeHash <%s>", fromAddr, acct.CodeHash)
|
2021-05-31 09:05:32 +00:00
|
|
|
}
|
2020-09-02 19:41:05 +00:00
|
|
|
|
2022-07-28 13:43:49 +00:00
|
|
|
if err := evmkeeper.CheckSenderBalance(sdkmath.NewIntFromBigInt(acct.Balance), txData); err != nil {
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrap(err, "failed to check sender balance")
|
2021-05-31 09:05:32 +00:00
|
|
|
}
|
2021-06-15 17:39:41 +00:00
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EthGasConsumeDecorator validates enough intrinsic gas for the transaction and
|
|
|
|
// gas consumption.
|
|
|
|
type EthGasConsumeDecorator struct {
|
2022-03-21 09:01:21 +00:00
|
|
|
evmKeeper EVMKeeper
|
|
|
|
maxGasWanted uint64
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewEthGasConsumeDecorator creates a new EthGasConsumeDecorator
|
2021-10-04 14:58:06 +00:00
|
|
|
func NewEthGasConsumeDecorator(
|
|
|
|
evmKeeper EVMKeeper,
|
2022-03-21 09:01:21 +00:00
|
|
|
maxGasWanted uint64,
|
2021-10-04 14:58:06 +00:00
|
|
|
) EthGasConsumeDecorator {
|
2020-04-17 22:32:01 +00:00
|
|
|
return EthGasConsumeDecorator{
|
2022-03-21 09:01:21 +00:00
|
|
|
evmKeeper,
|
|
|
|
maxGasWanted,
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnteHandle validates that the Ethereum tx message has enough to cover intrinsic gas
|
|
|
|
// (during CheckTx only) and that the sender has enough balance to pay for the gas cost.
|
|
|
|
//
|
2021-06-02 08:52:53 +00:00
|
|
|
// 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 plus any cost inccured by additional bytes
|
|
|
|
// of data supplied with the transaction.
|
|
|
|
//
|
|
|
|
// This AnteHandler decorator will fail if:
|
|
|
|
// - the message is not a MsgEthereumTx
|
|
|
|
// - sender account cannot be found
|
|
|
|
// - transaction's gas limit is lower than the intrinsic gas
|
|
|
|
// - user doesn't have enough balance to deduct the transaction fees (gas_limit * gas_price)
|
|
|
|
// - transaction or block gas meter runs out of gas
|
2022-03-06 14:09:28 +00:00
|
|
|
// - sets the gas meter limit
|
2022-08-05 13:00:31 +00:00
|
|
|
func (egcd EthGasConsumeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
2022-10-19 16:21:59 +00:00
|
|
|
chainCfg := egcd.evmKeeper.GetChainConfig(ctx)
|
|
|
|
ethCfg := chainCfg.EthereumConfig(egcd.evmKeeper.ChainID())
|
2021-05-10 16:34:00 +00:00
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
blockHeight := big.NewInt(ctx.BlockHeight())
|
|
|
|
homestead := ethCfg.IsHomestead(blockHeight)
|
|
|
|
istanbul := ethCfg.IsIstanbul(blockHeight)
|
2021-10-05 15:38:20 +00:00
|
|
|
london := ethCfg.IsLondon(blockHeight)
|
2022-03-06 14:09:28 +00:00
|
|
|
gasWanted := uint64(0)
|
2021-09-02 12:26:16 +00:00
|
|
|
var events sdk.Events
|
|
|
|
|
2022-08-05 13:00:31 +00:00
|
|
|
// Use the lowest priority of all the messages as the final one.
|
|
|
|
minPriority := int64(math.MaxInt64)
|
|
|
|
|
2021-11-26 14:19:28 +00:00
|
|
|
for _, msg := range tx.GetMsgs() {
|
2021-05-31 09:05:32 +00:00
|
|
|
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
2022-01-14 09:37:33 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
|
2021-05-31 09:05:32 +00:00
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
2021-07-05 16:39:08 +00:00
|
|
|
txData, err := evmtypes.UnpackTxData(msgEthTx.Data)
|
|
|
|
if err != nil {
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrap(err, "failed to unpack tx data")
|
2021-07-05 16:39:08 +00:00
|
|
|
}
|
2022-03-16 11:01:19 +00:00
|
|
|
|
2022-07-28 19:14:46 +00:00
|
|
|
if ctx.IsCheckTx() && egcd.maxGasWanted != 0 {
|
2022-03-16 11:01:19 +00:00
|
|
|
// We can't trust the tx gas limit, because we'll refund the unused gas.
|
2022-03-21 09:01:21 +00:00
|
|
|
if txData.GetGas() > egcd.maxGasWanted {
|
|
|
|
gasWanted += egcd.maxGasWanted
|
2022-03-16 11:01:19 +00:00
|
|
|
} else {
|
|
|
|
gasWanted += txData.GetGas()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gasWanted += txData.GetGas()
|
|
|
|
}
|
2021-07-05 16:39:08 +00:00
|
|
|
|
2022-10-19 16:21:59 +00:00
|
|
|
evmDenom := egcd.evmKeeper.GetEVMDenom(ctx)
|
2022-08-05 13:00:31 +00:00
|
|
|
fees, priority, err := egcd.evmKeeper.DeductTxCostsFromUserBalance(
|
2021-09-03 15:55:37 +00:00
|
|
|
ctx,
|
|
|
|
*msgEthTx,
|
|
|
|
txData,
|
|
|
|
evmDenom,
|
|
|
|
homestead,
|
|
|
|
istanbul,
|
2021-10-04 14:58:06 +00:00
|
|
|
london,
|
2021-09-03 15:55:37 +00:00
|
|
|
)
|
2020-04-17 22:32:01 +00:00
|
|
|
if err != nil {
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(err, "failed to deduct transaction costs from user balance")
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
2021-09-02 12:26:16 +00:00
|
|
|
|
|
|
|
events = append(events, sdk.NewEvent(sdk.EventTypeTx, sdk.NewAttribute(sdk.AttributeKeyFee, fees.String())))
|
2022-08-05 13:00:31 +00:00
|
|
|
if priority < minPriority {
|
|
|
|
minPriority = priority
|
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 12:26:16 +00:00
|
|
|
// TODO: change to typed events
|
|
|
|
ctx.EventManager().EmitEvents(events)
|
|
|
|
|
2021-06-14 14:24:08 +00:00
|
|
|
// TODO: deprecate after https://github.com/cosmos/cosmos-sdk/issues/9514 is fixed on SDK
|
|
|
|
blockGasLimit := ethermint.BlockGasLimit(ctx)
|
|
|
|
|
|
|
|
// NOTE: safety check
|
|
|
|
if blockGasLimit > 0 {
|
|
|
|
// generate a copy of the gas pool (i.e block gas meter) to see if we've run out of gas for this block
|
|
|
|
// if current gas consumed is greater than the limit, this funcion panics and the error is recovered on the Baseapp
|
|
|
|
gasPool := sdk.NewGasMeter(blockGasLimit)
|
|
|
|
gasPool.ConsumeGas(ctx.GasMeter().GasConsumedToLimit(), "gas pool check")
|
|
|
|
}
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2022-03-06 14:09:28 +00:00
|
|
|
// Set ctx.GasMeter with a limit of GasWanted (gasLimit)
|
|
|
|
gasConsumed := ctx.GasMeter().GasConsumed()
|
|
|
|
ctx = ctx.WithGasMeter(ethermint.NewInfiniteGasMeterWithLimit(gasWanted))
|
|
|
|
ctx.GasMeter().ConsumeGas(gasConsumed, "copy gas consumed")
|
|
|
|
|
2022-08-05 13:00:31 +00:00
|
|
|
newCtx := ctx.WithPriority(minPriority)
|
|
|
|
|
2021-05-31 09:05:32 +00:00
|
|
|
// we know that we have enough gas on the pool to cover the intrinsic gas
|
2022-08-05 13:00:31 +00:00
|
|
|
return next(newCtx, tx, simulate)
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-02 08:52:53 +00:00
|
|
|
// CanTransferDecorator checks if the sender is allowed to transfer funds according to the EVM block
|
|
|
|
// context rules.
|
|
|
|
type CanTransferDecorator struct {
|
2022-01-26 10:44:41 +00:00
|
|
|
evmKeeper EVMKeeper
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewCanTransferDecorator creates a new CanTransferDecorator instance.
|
2022-01-26 10:44:41 +00:00
|
|
|
func NewCanTransferDecorator(evmKeeper EVMKeeper) CanTransferDecorator {
|
2021-06-02 08:52:53 +00:00
|
|
|
return CanTransferDecorator{
|
2022-01-26 10:44:41 +00:00
|
|
|
evmKeeper: evmKeeper,
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnteHandle creates an EVM from the message and calls the BlockContext CanTransfer function to
|
|
|
|
// see if the address can execute the transaction.
|
|
|
|
func (ctd CanTransferDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
2021-07-14 09:13:55 +00:00
|
|
|
params := ctd.evmKeeper.GetParams(ctx)
|
|
|
|
ethCfg := params.ChainConfig.EthereumConfig(ctd.evmKeeper.ChainID())
|
2021-06-11 13:38:51 +00:00
|
|
|
signer := ethtypes.MakeSigner(ethCfg, big.NewInt(ctx.BlockHeight()))
|
2021-06-02 08:52:53 +00:00
|
|
|
|
2021-11-26 14:19:28 +00:00
|
|
|
for _, msg := range tx.GetMsgs() {
|
2021-06-02 08:52:53 +00:00
|
|
|
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
2022-01-14 09:37:33 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
|
2022-04-30 16:11:28 +00:00
|
|
|
baseFee := ctd.evmKeeper.GetBaseFee(ctx, ethCfg)
|
2021-10-04 14:58:06 +00:00
|
|
|
|
2021-10-05 15:38:20 +00:00
|
|
|
coreMsg, err := msgEthTx.AsMessage(signer, baseFee)
|
2021-06-02 08:52:53 +00:00
|
|
|
if err != nil {
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(
|
2021-06-15 07:19:31 +00:00
|
|
|
err,
|
|
|
|
"failed to create an ethereum core.Message from signer %T", signer,
|
|
|
|
)
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 08:18:20 +00:00
|
|
|
// NOTE: pass in an empty coinbase address and nil tracer as we don't need them for the check below
|
2021-10-22 17:21:03 +00:00
|
|
|
cfg := &evmtypes.EVMConfig{
|
|
|
|
ChainConfig: ethCfg,
|
|
|
|
Params: params,
|
|
|
|
CoinBase: common.Address{},
|
|
|
|
BaseFee: baseFee,
|
|
|
|
}
|
2022-01-05 07:28:27 +00:00
|
|
|
stateDB := statedb.New(ctx, ctd.evmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash().Bytes())))
|
|
|
|
evm := ctd.evmKeeper.NewEVM(ctx, coreMsg, cfg, evmtypes.NewNoOpTracer(), stateDB)
|
2021-06-02 08:52:53 +00:00
|
|
|
|
|
|
|
// check that caller has enough balance to cover asset transfer for **topmost** call
|
|
|
|
// NOTE: here the gas consumed is from the context with the infinite gas meter
|
2022-09-15 11:54:02 +00:00
|
|
|
if coreMsg.Value().Sign() > 0 && !evm.Context().CanTransfer(stateDB, coreMsg.From(), coreMsg.Value()) {
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrInsufficientFunds,
|
|
|
|
"failed to transfer %s from address %s using the EVM block context transfer function",
|
|
|
|
coreMsg.Value(),
|
|
|
|
coreMsg.From(),
|
2021-06-15 07:19:31 +00:00
|
|
|
)
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
2021-10-04 14:58:06 +00:00
|
|
|
|
2021-12-28 07:59:28 +00:00
|
|
|
if evmtypes.IsLondon(ethCfg, ctx.BlockHeight()) {
|
|
|
|
if baseFee == nil {
|
|
|
|
return ctx, sdkerrors.Wrap(
|
|
|
|
evmtypes.ErrInvalidBaseFee,
|
|
|
|
"base fee is supported but evm block context value is nil",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if coreMsg.GasFeeCap().Cmp(baseFee) < 0 {
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
2022-01-17 06:12:13 +00:00
|
|
|
sdkerrors.ErrInsufficientFee,
|
2021-12-28 07:59:28 +00:00
|
|
|
"max fee per gas less than block base fee (%s < %s)",
|
|
|
|
coreMsg.GasFeeCap(), baseFee,
|
|
|
|
)
|
|
|
|
}
|
2021-10-05 15:38:20 +00:00
|
|
|
}
|
2021-06-02 08:52:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
|
|
|
|
|
|
|
// EthIncrementSenderSequenceDecorator increments the sequence of the signers.
|
2021-04-18 16:39:15 +00:00
|
|
|
type EthIncrementSenderSequenceDecorator struct {
|
2021-09-03 15:55:37 +00:00
|
|
|
ak evmtypes.AccountKeeper
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 16:39:15 +00:00
|
|
|
// NewEthIncrementSenderSequenceDecorator creates a new EthIncrementSenderSequenceDecorator.
|
2021-09-03 15:55:37 +00:00
|
|
|
func NewEthIncrementSenderSequenceDecorator(ak evmtypes.AccountKeeper) EthIncrementSenderSequenceDecorator {
|
2021-04-18 16:39:15 +00:00
|
|
|
return EthIncrementSenderSequenceDecorator{
|
2020-04-17 22:32:01 +00:00
|
|
|
ak: ak,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 08:52:53 +00:00
|
|
|
// AnteHandle handles incrementing the sequence of the signer (i.e sender). If the transaction is a
|
|
|
|
// contract creation, the nonce will be incremented during the transaction execution and not within
|
|
|
|
// this AnteHandler decorator.
|
2021-04-18 16:39:15 +00:00
|
|
|
func (issd EthIncrementSenderSequenceDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
2021-11-26 14:19:28 +00:00
|
|
|
for _, msg := range tx.GetMsgs() {
|
2022-01-14 09:37:33 +00:00
|
|
|
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
|
|
|
|
}
|
2021-06-02 08:52:53 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
txData, err := evmtypes.UnpackTxData(msgEthTx.Data)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, sdkerrors.Wrap(err, "failed to unpack tx data")
|
|
|
|
}
|
2020-05-04 22:41:17 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
// increase sequence of sender
|
|
|
|
acc := issd.ak.GetAccount(ctx, msgEthTx.GetFrom())
|
|
|
|
if acc == nil {
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrUnknownAddress,
|
|
|
|
"account %s is nil", common.BytesToAddress(msgEthTx.GetFrom().Bytes()),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
nonce := acc.GetSequence()
|
|
|
|
|
|
|
|
// we merged the nonce verification to nonce increment, so when tx includes multiple messages
|
|
|
|
// with same sender, they'll be accepted.
|
|
|
|
if txData.GetNonce() != nonce {
|
|
|
|
return ctx, sdkerrors.Wrapf(
|
|
|
|
sdkerrors.ErrInvalidSequence,
|
|
|
|
"invalid nonce; got %d, expected %d", txData.GetNonce(), nonce,
|
|
|
|
)
|
|
|
|
}
|
2020-04-17 22:32:01 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
if err := acc.SetSequence(nonce + 1); err != nil {
|
|
|
|
return ctx, sdkerrors.Wrapf(err, "failed to set sequence to %d", acc.GetSequence()+1)
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
2022-01-14 09:37:33 +00:00
|
|
|
|
|
|
|
issd.ak.SetAccount(ctx, acc)
|
2020-04-17 22:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
2021-06-21 10:44:37 +00:00
|
|
|
|
|
|
|
// EthValidateBasicDecorator is adapted from ValidateBasicDecorator from cosmos-sdk, it ignores ErrNoSignatures
|
2021-11-08 13:00:35 +00:00
|
|
|
type EthValidateBasicDecorator struct {
|
|
|
|
evmKeeper EVMKeeper
|
|
|
|
}
|
2021-06-21 10:44:37 +00:00
|
|
|
|
|
|
|
// NewEthValidateBasicDecorator creates a new EthValidateBasicDecorator
|
2021-11-08 13:00:35 +00:00
|
|
|
func NewEthValidateBasicDecorator(ek EVMKeeper) EthValidateBasicDecorator {
|
|
|
|
return EthValidateBasicDecorator{
|
|
|
|
evmKeeper: ek,
|
|
|
|
}
|
2021-06-21 10:44:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnteHandle handles basic validation of tx
|
|
|
|
func (vbd EthValidateBasicDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
|
|
|
|
// no need to validate basic on recheck tx, call next antehandler
|
|
|
|
if ctx.IsReCheckTx() {
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := tx.ValidateBasic()
|
|
|
|
// ErrNoSignatures is fine with eth tx
|
2021-09-05 11:03:06 +00:00
|
|
|
if err != nil && !errors.Is(err, sdkerrors.ErrNoSignatures) {
|
2021-11-26 14:19:28 +00:00
|
|
|
return ctx, sdkerrors.Wrap(err, "tx basic validation failed")
|
2021-06-21 10:44:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:00:35 +00:00
|
|
|
// For eth type cosmos tx, some fields should be veified as zero values,
|
|
|
|
// since we will only verify the signature against the hash of the MsgEthereumTx.Data
|
2022-07-19 15:12:48 +00:00
|
|
|
wrapperTx, ok := tx.(protoTxProvider)
|
|
|
|
if !ok {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid tx type %T, didn't implement interface protoTxProvider", tx)
|
|
|
|
}
|
2021-11-08 13:00:35 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
protoTx := wrapperTx.GetProtoTx()
|
|
|
|
body := protoTx.Body
|
|
|
|
if body.Memo != "" || body.TimeoutHeight != uint64(0) || len(body.NonCriticalExtensionOptions) > 0 {
|
|
|
|
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest,
|
|
|
|
"for eth tx body Memo TimeoutHeight NonCriticalExtensionOptions should be empty")
|
|
|
|
}
|
2021-11-08 13:00:35 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
if len(body.ExtensionOptions) != 1 {
|
|
|
|
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "for eth tx length of ExtensionOptions should be 1")
|
|
|
|
}
|
2021-11-08 13:00:35 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
txFee := sdk.Coins{}
|
|
|
|
txGasLimit := uint64(0)
|
2022-05-03 17:51:02 +00:00
|
|
|
|
2022-10-19 16:21:59 +00:00
|
|
|
chainCfg := vbd.evmKeeper.GetChainConfig(ctx)
|
2022-07-19 15:12:48 +00:00
|
|
|
chainID := vbd.evmKeeper.ChainID()
|
2022-10-19 16:21:59 +00:00
|
|
|
ethCfg := chainCfg.EthereumConfig(chainID)
|
2022-07-19 15:12:48 +00:00
|
|
|
baseFee := vbd.evmKeeper.GetBaseFee(ctx, ethCfg)
|
2022-10-20 11:40:25 +00:00
|
|
|
enableCreate := vbd.evmKeeper.GetEnableCreate(ctx)
|
|
|
|
enableCall := vbd.evmKeeper.GetEnableCall(ctx)
|
|
|
|
evmDenom := vbd.evmKeeper.GetEVMDenom(ctx)
|
2022-05-03 17:51:02 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
for _, msg := range protoTx.GetMsgs() {
|
|
|
|
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
|
|
|
|
}
|
2021-12-28 07:59:28 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
// Validate `From` field
|
|
|
|
if msgEthTx.From != "" {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid From %s, expect empty string", msgEthTx.From)
|
|
|
|
}
|
2021-12-28 07:59:28 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
txGasLimit += msgEthTx.GetGas()
|
2022-05-03 17:51:02 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
txData, err := evmtypes.UnpackTxData(msgEthTx.Data)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, sdkerrors.Wrap(err, "failed to unpack MsgEthereumTx Data")
|
2022-01-14 09:37:33 +00:00
|
|
|
}
|
2021-11-08 13:00:35 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
// return error if contract creation or call are disabled through governance
|
2022-10-19 16:21:59 +00:00
|
|
|
if !enableCreate && txData.GetTo() == nil {
|
2022-07-19 15:12:48 +00:00
|
|
|
return ctx, sdkerrors.Wrap(evmtypes.ErrCreateDisabled, "failed to create new contract")
|
2022-10-19 16:21:59 +00:00
|
|
|
} else if !enableCall && txData.GetTo() != nil {
|
2022-07-19 15:12:48 +00:00
|
|
|
return ctx, sdkerrors.Wrap(evmtypes.ErrCallDisabled, "failed to call contract")
|
2021-11-08 13:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
if baseFee == nil && txData.TxType() == ethtypes.DynamicFeeTxType {
|
|
|
|
return ctx, sdkerrors.Wrap(ethtypes.ErrTxTypeNotSupported, "dynamic fee tx not supported")
|
2021-11-08 13:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 16:21:59 +00:00
|
|
|
txFee = txFee.Add(sdk.NewCoin(evmDenom, sdkmath.NewIntFromBigInt(txData.Fee())))
|
2022-07-19 15:12:48 +00:00
|
|
|
}
|
2021-11-08 13:00:35 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
authInfo := protoTx.AuthInfo
|
|
|
|
if len(authInfo.SignerInfos) > 0 {
|
|
|
|
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "for eth tx AuthInfo SignerInfos should be empty")
|
|
|
|
}
|
2021-11-08 13:00:35 +00:00
|
|
|
|
2022-07-19 15:12:48 +00:00
|
|
|
if authInfo.Fee.Payer != "" || authInfo.Fee.Granter != "" {
|
|
|
|
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "for eth tx AuthInfo Fee payer and granter should be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !authInfo.Fee.Amount.IsEqual(txFee) {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid AuthInfo Fee Amount (%s != %s)", authInfo.Fee.Amount, txFee)
|
|
|
|
}
|
|
|
|
|
|
|
|
if authInfo.Fee.GasLimit != txGasLimit {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid AuthInfo Fee GasLimit (%d != %d)", authInfo.Fee.GasLimit, txGasLimit)
|
|
|
|
}
|
|
|
|
|
|
|
|
sigs := protoTx.Signatures
|
|
|
|
if len(sigs) > 0 {
|
|
|
|
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "for eth tx Signatures should be empty")
|
2021-11-08 13:00:35 +00:00
|
|
|
}
|
|
|
|
|
2021-06-21 10:44:37 +00:00
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
2021-06-30 09:31:30 +00:00
|
|
|
|
|
|
|
// EthSetupContextDecorator is adapted from SetUpContextDecorator from cosmos-sdk, it ignores gas consumption
|
|
|
|
// by setting the gas meter to infinite
|
2022-01-14 09:37:33 +00:00
|
|
|
type EthSetupContextDecorator struct {
|
|
|
|
evmKeeper EVMKeeper
|
|
|
|
}
|
2021-06-30 09:31:30 +00:00
|
|
|
|
2022-01-14 09:37:33 +00:00
|
|
|
func NewEthSetUpContextDecorator(evmKeeper EVMKeeper) EthSetupContextDecorator {
|
|
|
|
return EthSetupContextDecorator{
|
|
|
|
evmKeeper: evmKeeper,
|
|
|
|
}
|
2021-06-30 09:31:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (esc EthSetupContextDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
|
|
|
// all transactions must implement GasTx
|
|
|
|
_, ok := tx.(authante.GasTx)
|
|
|
|
if !ok {
|
|
|
|
return newCtx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be GasTx")
|
|
|
|
}
|
|
|
|
|
|
|
|
newCtx = ctx.WithGasMeter(sdk.NewInfiniteGasMeter())
|
2022-01-14 09:37:33 +00:00
|
|
|
// Reset transient gas used to prepare the execution of current cosmos tx.
|
|
|
|
// Transient gas-used is necessary to sum the gas-used of cosmos tx, when it contains multiple eth msgs.
|
|
|
|
esc.evmKeeper.ResetTransientGasUsed(ctx)
|
2021-06-30 09:31:30 +00:00
|
|
|
return next(newCtx, tx, simulate)
|
|
|
|
}
|
2021-12-15 02:17:03 +00:00
|
|
|
|
|
|
|
// EthMempoolFeeDecorator will check if the transaction's effective fee is at least as large
|
|
|
|
// as the local validator's minimum gasFee (defined in validator config).
|
|
|
|
// If fee is too low, decorator returns error and tx is rejected from mempool.
|
|
|
|
// Note this only applies when ctx.CheckTx = true
|
|
|
|
// If fee is high enough or not CheckTx, then call next AnteHandler
|
|
|
|
// CONTRACT: Tx must implement FeeTx to use MempoolFeeDecorator
|
|
|
|
type EthMempoolFeeDecorator struct {
|
2022-01-26 10:44:41 +00:00
|
|
|
evmKeeper EVMKeeper
|
2021-12-15 02:17:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 10:44:41 +00:00
|
|
|
func NewEthMempoolFeeDecorator(ek EVMKeeper) EthMempoolFeeDecorator {
|
2021-12-15 02:17:03 +00:00
|
|
|
return EthMempoolFeeDecorator{
|
2022-01-26 10:44:41 +00:00
|
|
|
evmKeeper: ek,
|
2021-12-15 02:17:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnteHandle ensures that the provided fees meet a minimum threshold for the validator,
|
|
|
|
// if this is a CheckTx. This is only for local mempool purposes, and thus
|
|
|
|
// is only ran on check tx.
|
2022-01-26 10:44:41 +00:00
|
|
|
// It only do the check if london hardfork not enabled or feemarket not enabled, because in that case feemarket will take over the task.
|
2021-12-15 02:17:03 +00:00
|
|
|
func (mfd EthMempoolFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
|
|
|
if ctx.IsCheckTx() && !simulate {
|
2022-10-19 16:21:59 +00:00
|
|
|
chainCfg := mfd.evmKeeper.GetChainConfig(ctx)
|
|
|
|
ethCfg := chainCfg.EthereumConfig(mfd.evmKeeper.ChainID())
|
2022-04-30 16:11:28 +00:00
|
|
|
baseFee := mfd.evmKeeper.GetBaseFee(ctx, ethCfg)
|
2022-10-20 11:40:25 +00:00
|
|
|
evmDenom := mfd.evmKeeper.GetEVMDenom(ctx)
|
|
|
|
|
2022-01-26 10:44:41 +00:00
|
|
|
if baseFee == nil {
|
|
|
|
for _, msg := range tx.GetMsgs() {
|
|
|
|
ethMsg, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
feeAmt := ethMsg.GetFee()
|
|
|
|
glDec := sdk.NewDec(int64(ethMsg.GetGas()))
|
|
|
|
requiredFee := ctx.MinGasPrices().AmountOf(evmDenom).Mul(glDec)
|
|
|
|
if sdk.NewDecFromBigInt(feeAmt).LT(requiredFee) {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInsufficientFee, "insufficient fees; got: %s required: %s", feeAmt, requiredFee)
|
|
|
|
}
|
2022-01-14 09:37:33 +00:00
|
|
|
}
|
2021-12-15 02:17:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|
2022-05-31 10:26:40 +00:00
|
|
|
|
|
|
|
// EthEmitEventDecorator emit events in ante handler in case of tx execution failed (out of block gas limit).
|
|
|
|
type EthEmitEventDecorator struct {
|
|
|
|
evmKeeper EVMKeeper
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewEthEmitEventDecorator creates a new EthEmitEventDecorator
|
|
|
|
func NewEthEmitEventDecorator(evmKeeper EVMKeeper) EthEmitEventDecorator {
|
|
|
|
return EthEmitEventDecorator{evmKeeper}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AnteHandle emits some basic events for the eth messages
|
|
|
|
func (eeed EthEmitEventDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
|
|
|
|
// After eth tx passed ante handler, the fee is deducted and nonce increased, it shouldn't be ignored by json-rpc,
|
|
|
|
// we need to emit some basic events at the very end of ante handler to be indexed by tendermint.
|
|
|
|
txIndex := eeed.evmKeeper.GetTxIndexTransient(ctx)
|
|
|
|
for i, msg := range tx.GetMsgs() {
|
|
|
|
msgEthTx, ok := msg.(*evmtypes.MsgEthereumTx)
|
|
|
|
if !ok {
|
|
|
|
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
// emit ethereum tx hash as event, should be indexed by tm tx indexer for query purpose.
|
|
|
|
// it's emitted in ante handler so we can query failed transaction (out of block gas limit).
|
|
|
|
ctx.EventManager().EmitEvent(sdk.NewEvent(
|
|
|
|
evmtypes.EventTypeEthereumTx,
|
|
|
|
sdk.NewAttribute(evmtypes.AttributeKeyEthereumTxHash, msgEthTx.Hash),
|
|
|
|
sdk.NewAttribute(evmtypes.AttributeKeyTxIndex, strconv.FormatUint(txIndex+uint64(i), 10)),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
return next(ctx, tx, simulate)
|
|
|
|
}
|