Skip to content
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

fix: compare gas prices properly #3771

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

zivkovicmilos
Copy link
Member

@zivkovicmilos zivkovicmilos commented Feb 18, 2025

Description

This PR fixes a bug in the gas price comparison logic, where a larger gas price would return false for being larger, when compared to a smaller gas price:

Rejected bad transaction	{"module": "mempool", "tx": "E810F32DC9AE46ADECC0B3657369C45B8595307C1E5B18F18C3F3FADDF432FE6", "res": {"Error":{},"Data":null,"Events":null,"Log":"--= Error =--\nData: std.InsufficientFeeError{abciError:std.abciError{}}\nMsg Traces:\n    0  /Users/zmilos/Work/gno/tm2/pkg/std/errors.go:114 - insufficient fees; got: {Gas-Wanted: 100000, Gas-Fee 5ugnot}, fee required: {Gas:1000 Price:1ugnot} as block gas 

In this example, a user sent a transaction with the following gas pricing data:

  • Gas Wanted: 10000
  • Gas Fee: 5ugnot

This returned false, when being compared to a gas price of Gas Wanted: 1000, Gas Fee: 1ugnot 🤷‍♂️

The issue was that the function cross-multiplied mismatched fields, comparing fee-per-gas ratios instead of comparing total fees (gas x price), which led to an incorrect result when gas values differ. In this example, it would compare the ratios 0.00005 and 0.001, and return false:

	gpg := big.NewInt(gp.Gas)
	gpa := big.NewInt(gp.Price.Amount)

	gpBg := big.NewInt(gpB.Gas)
	gpBa := big.NewInt(gpB.Price.Amount)

	prod1 := big.NewInt(0).Mul(gpa, gpBg) // gp's price amount * gpB's gas
	prod2 := big.NewInt(0).Mul(gpg, gpBa) // gpB's gas * pg's price amount
	// This is equivalent to checking
	// That the Fee / GasWanted ratio is greater than or equal to the minimum GasPrice per gas.
	// This approach helps us avoid dealing with configurations where the value of
	// the minimum gas price is set to 0.00001ugnot/gas.
	return prod1.Cmp(prod2) >= 0, nil

How our entire testing suite did not catch a bug like this is beyond me, but here we are.

EDIT:
I've figured it out.

Our std.Fee is very misleading.
The amount the user puts in the tx.Fee is actually the total amount of whatever the user is willing to pay for the entire transaction, not just a single unit of gas. The effective gas price is the ratio of the maximum gas wanted and the maximum fee (for the entire tx).

@zivkovicmilos zivkovicmilos self-assigned this Feb 18, 2025
@github-actions github-actions bot added the 📦 🌐 tendermint v2 Issues or PRs tm2 related label Feb 18, 2025
@Gno2D2
Copy link
Collaborator

Gno2D2 commented Feb 18, 2025

🛠 PR Checks Summary

All Automated Checks passed. ✅

Manual Checks (for Reviewers):
  • IGNORE the bot requirements for this PR (force green CI check)
Read More

🤖 This bot helps streamline PR reviews by verifying automated checks and providing guidance for contributors and reviewers.

✅ Automated Checks (for Contributors):

No automated checks match this pull request.

☑️ Contributor Actions:
  1. Fix any issues flagged by automated checks.
  2. Follow the Contributor Checklist to ensure your PR is ready for review.
    • Add new tests, or document why they are unnecessary.
    • Provide clear examples/screenshots, if necessary.
    • Update documentation, if required.
    • Ensure no breaking changes, or include BREAKING CHANGE notes.
    • Link related issues/PRs, where applicable.
☑️ Reviewer Actions:
  1. Complete manual checks for the PR, including the guidelines and additional checks if applicable.
📚 Resources:
Debug
Manual Checks
**IGNORE** the bot requirements for this PR (force green CI check)

If

🟢 Condition met
└── 🟢 On every pull request

Can be checked by

  • Any user with comment edit permission

Copy link
Contributor

@aeddi aeddi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Copy link

codecov bot commented Feb 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 🌐 tendermint v2 Issues or PRs tm2 related
Projects
Status: In Progress
Development

Successfully merging this pull request may close these issues.

3 participants