Skip to content

feat: re-use tx validation rules from go-ethereum #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ante/evm/05_signature_verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (esvd EthSigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, s
return ctx, errorsmod.Wrapf(errortypes.ErrUnknownRequest, "invalid message type %T, expected %T", msg, (*evmtypes.MsgEthereumTx)(nil))
}

err := SignatureVerification(msgEthTx, signer, allowUnprotectedTxs)
err := SignatureVerification(msgEthTx, msgEthTx.AsTransaction(), signer, allowUnprotectedTxs)
if err != nil {
return ctx, err
}
Expand All @@ -64,10 +64,10 @@ func (esvd EthSigVerificationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, s
// computed from the signature of the Ethereum transaction.
func SignatureVerification(
msg *evmtypes.MsgEthereumTx,
ethTx *ethtypes.Transaction,
signer ethtypes.Signer,
allowUnprotectedTxs bool,
) error {
ethTx := msg.AsTransaction()
ethCfg := evmtypes.GetEthChainConfig()

if !allowUnprotectedTxs {
Expand Down
26 changes: 26 additions & 0 deletions ante/evm/mono_decorator.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package evm

import (
"math"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/txpool"
ethtypes "github.com/ethereum/go-ethereum/core/types"

anteinterfaces "github.com/cosmos/evm/ante/interfaces"
Expand All @@ -18,6 +20,12 @@ import (
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
)

const AcceptedTxType = 0 |
1<<ethtypes.LegacyTxType |
1<<ethtypes.AccessListTxType |
1<<ethtypes.DynamicFeeTxType |
1<<ethtypes.SetCodeTxType

// MonoDecorator is a single decorator that handles all the prechecks for
// ethereum transactions.
type MonoDecorator struct {
Expand Down Expand Up @@ -88,6 +96,23 @@ func (md MonoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
return ctx, err
}

// call go-ethereum transaction validation
header := ethtypes.Header{
GasLimit: ethTx.Gas(),
BaseFee: decUtils.BaseFee,
Number: big.NewInt(ctx.BlockHeight()),
Time: uint64(ctx.BlockTime().Unix()), //nolint:gosec
Difficulty: big.NewInt(0),
}
if err := txpool.ValidateTransaction(ethTx, &header, decUtils.Signer, &txpool.ValidationOptions{
Config: evmtypes.GetEthChainConfig(),
Accept: AcceptedTxType,
MaxSize: math.MaxUint64, // tx size is checked in cometbft
MinTip: new(big.Int),
}); err != nil {
return ctx, err
}

feeAmt := ethMsg.GetFee()
gas := ethTx.Gas()
fee := sdkmath.LegacyNewDecFromBigInt(feeAmt)
Expand Down Expand Up @@ -130,6 +155,7 @@ func (md MonoDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, ne
// 5. signature verification
if err := SignatureVerification(
ethMsg,
ethTx,
decUtils.Signer,
decUtils.EvmParams.AllowUnprotectedTxs,
); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/precompiles/gov/test_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestPrecompileIntegrationTestSuite(t *testing.T, create network.CreateEvmAp
BeforeEach(func() { callArgs.MethodName = method })

It("fails with low gas", func() {
txArgs.GasLimit = 30_000
txArgs.GasLimit = 37_790 // meed the requirement of floor data gas cost
jsonBlob := minimalBankSendProposalJSON(proposerAccAddr, s.network.GetBaseDenom(), "50")
callArgs.Args = []interface{}{proposerAddr, jsonBlob, minimalDeposit(s.network.GetBaseDenom(), big.NewInt(1))}

Expand Down
2 changes: 1 addition & 1 deletion x/vm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (k Keeper) EstimateGasInternal(c context.Context, req *types.EthCallRequest
// pass false to not commit StateDB
rsp, err = k.ApplyMessageWithConfig(tmpCtx, *msg, nil, false, cfg, txConfig, false)
if err != nil {
if errors.Is(err, core.ErrIntrinsicGas) {
if errors.Is(err, core.ErrIntrinsicGas) || errors.Is(err, core.ErrFloorDataGas) {
return true, nil, nil // Special case, raise gas limit
}
return true, nil, err // Bail out
Expand Down
12 changes: 11 additions & 1 deletion x/vm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,21 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, msg core.Message, trace
// eth_estimateGas will check for this exact error
return nil, errorsmod.Wrap(core.ErrIntrinsicGas, "apply message")
}
// Gas limit suffices for the floor data cost (EIP-7623)
rules := ethCfg.Rules(big.NewInt(ctx.BlockHeight()), true, uint64(ctx.BlockTime().Unix())) //#nosec G115 -- int overflow is not a concern here
if rules.IsPrague {
floorDataGas, err := core.FloorDataGas(msg.Data)
if err != nil {
return nil, err
}
if msg.GasLimit < floorDataGas {
return nil, fmt.Errorf("%w: have %d, want %d", core.ErrFloorDataGas, msg.GasLimit, floorDataGas)
}
}
leftoverGas -= intrinsicGas

// access list preparation is moved from ante handler to here, because it's needed when `ApplyMessage` is called
// under contexts where ante handlers are not run, for example `eth_call` and `eth_estimateGas`.
rules := ethCfg.Rules(big.NewInt(ctx.BlockHeight()), true, uint64(ctx.BlockTime().Unix())) //#nosec G115 -- int overflow is not a concern here
stateDB.Prepare(rules, msg.From, common.Address{}, msg.To, evm.ActivePrecompiles(), msg.AccessList)

convertedValue, err := utils.Uint256FromBigInt(msg.Value)
Expand Down
Loading