2022-12-16 09:48:38 +00:00
|
|
|
// Copyright 2021 Evmos Foundation
|
|
|
|
// This file is part of Evmos' Ethermint library.
|
|
|
|
//
|
|
|
|
// The Ethermint library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The Ethermint library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the Ethermint library. If not, see https://github.com/evmos/ethermint/blob/main/LICENSE
|
2020-04-17 22:32:01 +00:00
|
|
|
package ante
|
|
|
|
|
|
|
|
import (
|
2022-08-05 13:00:31 +00:00
|
|
|
"math"
|
2020-04-17 22:32:01 +00:00
|
|
|
"math/big"
|
|
|
|
|
2022-11-25 08:24:55 +00:00
|
|
|
errorsmod "cosmossdk.io/errors"
|
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"
|
2022-11-25 08:24:55 +00:00
|
|
|
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
|
2020-04-17 22:32:01 +00:00
|
|
|
|
2022-06-19 09:43:41 +00:00
|
|
|
ethermint "github.com/evmos/ethermint/types"
|
2022-11-29 17:00:48 +00:00
|
|
|
"github.com/evmos/ethermint/x/evm/keeper"
|
2022-06-19 09:43:41 +00:00
|
|
|
"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-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-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(errortypes.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 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.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() {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrap(errortypes.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() {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(errortypes.ErrInvalidType,
|
2022-01-05 07:28:27 +00:00
|
|
|
"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-11-29 17:00:48 +00:00
|
|
|
if err := keeper.CheckSenderBalance(sdkmath.NewIntFromBigInt(acct.Balance), txData); err != nil {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.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
|
2022-11-18 21:41:16 +00:00
|
|
|
// transaction is executed. The gas is a constant value plus any cost incurred by additional bytes
|
2021-06-02 08:52:53 +00:00
|
|
|
// 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-11-18 21:41:16 +00:00
|
|
|
// - gas limit is greater than the block 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-12-22 08:07:52 +00:00
|
|
|
gasWanted := uint64(0)
|
2022-11-18 21:41:16 +00:00
|
|
|
// gas consumption limit already checked during CheckTx so there's no need to
|
|
|
|
// verify it again during ReCheckTx
|
|
|
|
if ctx.IsReCheckTx() {
|
2022-12-22 08:07:52 +00:00
|
|
|
// Use new context with gasWanted = 0
|
|
|
|
// Otherwise, there's an error on txmempool.postCheck (tendermint)
|
|
|
|
// that is not bubbled up. Thus, the Tx never runs on DeliverMode
|
|
|
|
// Error: "gas wanted -1 is negative"
|
|
|
|
// For more information, see issue #1554
|
|
|
|
// https://github.com/evmos/ethermint/issues/1554
|
|
|
|
newCtx := ctx.WithGasMeter(ethermint.NewInfiniteGasMeterWithLimit(gasWanted))
|
|
|
|
return next(newCtx, tx, simulate)
|
2022-11-18 21:41:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 19:01:28 +00:00
|
|
|
evmParams := egcd.evmKeeper.GetParams(ctx)
|
|
|
|
chainCfg := evmParams.GetChainConfig()
|
2022-10-19 16:21:59 +00:00
|
|
|
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-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)
|
2022-11-25 08:24:55 +00:00
|
|
|
baseFee := egcd.evmKeeper.GetBaseFee(ctx, ethCfg)
|
2022-08-05 13:00:31 +00:00
|
|
|
|
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-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(errortypes.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 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.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
|
|
|
|
2023-01-23 19:01:28 +00:00
|
|
|
evmDenom := evmParams.GetEvmDenom()
|
2022-11-25 08:24:55 +00:00
|
|
|
|
2022-11-29 17:00:48 +00:00
|
|
|
fees, err := keeper.VerifyFee(txData, evmDenom, baseFee, homestead, istanbul, ctx.IsCheckTx())
|
|
|
|
if err != nil {
|
|
|
|
return ctx, errorsmod.Wrapf(err, "failed to verify the fees")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = egcd.evmKeeper.DeductTxCostsFromUserBalance(ctx, fees, common.HexToAddress(msgEthTx.From))
|
2020-04-17 22:32:01 +00:00
|
|
|
if err != nil {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.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
|
|
|
|
2022-11-18 21:41:16 +00:00
|
|
|
events = append(events,
|
|
|
|
sdk.NewEvent(
|
|
|
|
sdk.EventTypeTx,
|
|
|
|
sdk.NewAttribute(sdk.AttributeKeyFee, fees.String()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2022-11-25 08:24:55 +00:00
|
|
|
priority := evmtypes.GetTxPriority(txData, baseFee)
|
|
|
|
|
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
|
|
|
ctx.EventManager().EmitEvents(events)
|
|
|
|
|
2021-06-14 14:24:08 +00:00
|
|
|
blockGasLimit := ethermint.BlockGasLimit(ctx)
|
|
|
|
|
2022-11-18 21:41:16 +00:00
|
|
|
// return error if the tx gas is greater than the block limit (max gas)
|
|
|
|
|
|
|
|
// NOTE: it's important here to use the gas wanted instead of the gas consumed
|
|
|
|
// from the tx gas pool. The later only has the value so far since the
|
|
|
|
// EthSetupContextDecorator so it will never exceed the block gas limit.
|
|
|
|
if gasWanted > blockGasLimit {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(
|
|
|
|
errortypes.ErrOutOfGas,
|
2022-11-18 21:41:16 +00:00
|
|
|
"tx gas (%d) exceeds block gas limit (%d)",
|
|
|
|
gasWanted,
|
|
|
|
blockGasLimit,
|
|
|
|
)
|
2021-06-14 14:24:08 +00:00
|
|
|
}
|
2021-05-25 12:56:36 +00:00
|
|
|
|
2022-11-18 21:41:16 +00:00
|
|
|
// Set tx GasMeter with a limit of GasWanted (i.e gas limit from the Ethereum tx).
|
|
|
|
// The gas consumed will be then reset to the gas used by the state transition
|
|
|
|
// in the EVM.
|
2022-03-06 14:09:28 +00:00
|
|
|
|
2022-11-18 21:41:16 +00:00
|
|
|
// FIXME: use a custom gas configuration that doesn't add any additional gas and only
|
|
|
|
// takes into account the gas consumed at the end of the EVM transaction.
|
|
|
|
newCtx := ctx.
|
|
|
|
WithGasMeter(ethermint.NewInfiniteGasMeterWithLimit(gasWanted)).
|
|
|
|
WithPriority(minPriority)
|
2022-08-05 13:00:31 +00:00
|
|
|
|
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-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(errortypes.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 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.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
|
|
|
}
|
|
|
|
|
2022-11-18 21:41:16 +00:00
|
|
|
if evmtypes.IsLondon(ethCfg, ctx.BlockHeight()) {
|
|
|
|
if baseFee == nil {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrap(
|
2022-11-18 21:41:16 +00:00
|
|
|
evmtypes.ErrInvalidBaseFee,
|
|
|
|
"base fee is supported but evm block context value is nil",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
if coreMsg.GasFeeCap().Cmp(baseFee) < 0 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(
|
|
|
|
errortypes.ErrInsufficientFee,
|
2022-11-18 21:41:16 +00:00
|
|
|
"max fee per gas less than block base fee (%s < %s)",
|
|
|
|
coreMsg.GasFeeCap(), baseFee,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2022-12-30 12:20:14 +00:00
|
|
|
cfg := &statedb.EVMConfig{
|
2021-10-22 17:21:03 +00:00
|
|
|
ChainConfig: ethCfg,
|
|
|
|
Params: params,
|
|
|
|
CoinBase: common.Address{},
|
|
|
|
BaseFee: baseFee,
|
|
|
|
}
|
2022-12-30 12:20:14 +00:00
|
|
|
|
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()) {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(
|
|
|
|
errortypes.ErrInsufficientFunds,
|
2021-11-26 14:19:28 +00:00
|
|
|
"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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(errortypes.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
|
2022-01-14 09:37:33 +00:00
|
|
|
}
|
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 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrap(err, "failed to unpack tx data")
|
2022-01-14 09:37:33 +00:00
|
|
|
}
|
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 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(
|
|
|
|
errortypes.ErrUnknownAddress,
|
2022-01-14 09:37:33 +00:00
|
|
|
"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 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.Wrapf(
|
|
|
|
errortypes.ErrInvalidSequence,
|
2022-01-14 09:37:33 +00:00
|
|
|
"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 {
|
2022-11-25 08:24:55 +00:00
|
|
|
return ctx, errorsmod.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)
|
|
|
|
}
|