Skip to content

Fix/Benchmark Antehandler #349

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

heijiLee
Copy link

@heijiLee heijiLee commented Jul 24, 2025

Description

Closes: #338


Summary

This PR fixes two benchmark-breaking sub-issues:

  1. Invalid gas cap - prevented by setting DefaultMinGasPrice to 1 so the basefee can no longer decay to zero.
  2. Fee rounding error - resolved by replacing TruncateInt() with Ceil().RoundInt() in fee calculation, ensuring the total fee is never under-priced.

Details

These issues surfaced only after commit 9fd2afe the one that introduced EIP-1559.
You can reproduce this errors by

cd evmd/ante
go test -benchmem -run=^$ -bench ^BenchmarkAnteHandler$ github.com/cosmos/evm/evmd/ante

First sub-issues : Fix for "invalid gas cap" in benchmark

During BenchmarkAnteHandler, each tx is placed into its own block. Because every block then consumes only a tiny amount of gas, the EIP-1559 algorithm repeatedly lowers the baseFee until it eventually reaches 0 atom. Here's some log for this.

base fee: 29965
base fee: 26220
…
base fee: 9
base fee: 8
…
base fee: 1
base fee: 0
--- FAIL: BenchmarkAnteHandler/tx_type_evm_transfer_sim-10
    failed to validate transaction: max priority fee per gas higher than max fee per gas (1 > 0): invalid gas cap

Once baseFee hits zero, the back-calculated maxFeePerGas for new tx also becomes zero, whereas default maxPriorityFeePerGas = 1 . The inequality maxPriorityFeePerGas (1) > maxFeePerGas (0) violates protocol rules, and each transaction is rejected with an invalid gas cap error.

Fix : in the test initializer we raise the network's floor by setting (ref)

So baseFee can never decay below 1 aatom. With this safeguard in place, maxFeePerGas is always >=1 atatom, the inequality is avoided, and the benchmark suite runs to completion without errors.

Second sub-issues : Insufficient fee caused by fee rounding error

In the test environment, a Cosmos transaction's fee is calculated like this (ref)
So the code multiplies baseFee × gasLimit and then uses TruncateInt() to drop the fractional part (This is a test shortcut; in production it should be maxFeePerGas × gasLimit).

But here's the benchmark fails.
FeeChecker later re-derives maxFeePerGas by dividing the total fee by gasLimit and compares it to baseFee (ref). Because the total fee was truncated, it is up to 1 aatom lower, making maxFeePerGas < baseFee and triggering 'gas prices too low; insufficient fee'. Here's some log for this.

...
114486203125000aatom-fees (Truncate)
114486203125000aatom-fees (ceil)
feeCap: 765625000.000000000000000000, baseFee: 765625000.000000000000000000
100175427734375aatom-fees (Truncate)
100175427734375aatom-fees (ceil)
feeCap: 669921875.000000000000000000, baseFee: 669921875.000000000000000000
87653499267578aatom-fees (Truncate)
87653499267579aatom-fees (ceil)
feeCap: 586181640.624999164064119625, baseFee: 586181640.625000000000000000
--- FAIL: BenchmarkAnteHandler/tx_type_bank_msg_send-10
    evm_antehandler_benchmark_test.go:108: failed to run ante handler: gas prices too low, got: 586181640.624999164064119625aatom required: 586181640.625000000000000000aatom. Please retry using a higher gas price or a higher fee: insufficient fee 

Fix : Replace TruncateInt() with Ceil().RoundInt() (ref)

So the total fee is always ≥ baseFee × gasLimit, guaranteeing maxFeePerGas ≥ baseFee

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • tackled an existing issue or discussed with a team member
  • left instructions on how to review the changes
  • targeted the main branch

Reviewers Checklist

All items are required.
Please add a note if the item is not applicable
and please add your handle next to the items reviewed
if you only reviewed selected items.

I have...

  • added a relevant changelog entry to the Unreleased section in CHANGELOG.md
  • confirmed all author checklist items have been addressed
  • confirmed that this PR does not change production code
  • reviewed content
  • tested instructions (if applicable)
  • confirmed all CI checks have passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: BenchmarkAnteHandler fails - invalid gas cap & fee rounding error
1 participant