Skip to content

ethclient: Add EstimateGasAt[Hash] to estimate gas against a specific block #27508

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

Conversation

PlasmaPower
Copy link
Contributor

The main use case I see of this is that it allows users to estimate gas against the same state that they query for their nonce, and the same state they base the data of their transaction against. This helps ensure that gas estimation won't fail and the transaction won't revert on-chain because of a mismatch between the state used for gas estimation and the state used to generate the inputs to gas estimation or the transaction's nonce when submitted to the mempool.

This PR also updates the EstimateGas comment based on the new geth eth_estimateGas default of using latest state as of v1.12.0: #24363

@roberto-bayardo
Copy link
Contributor

I just was bitten by this: I had a contract that (sometimes) calls blockhash against the latest block, and it was returning 0 in EstimateGas due to default of latest over pending.

I think it does make sense for EstimateGas to run over latest state instead of pending, since who knows what will actually end up the pending state of the blockchain when the tx actually executes. But perhaps it could be made to think the block.number is the pending block, since we do know that (ignoring re-orgs) it will execute at a strictly higher block.number than "latest".

Copy link
Member

@MariusVanDerWijden MariusVanDerWijden left a comment

Choose a reason for hiding this comment

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

LGTM, judging from the RPC spec, specifying the blocknumber is already part of the spec, so all clients should ™️ support it, so we can add it to ethclient. Not sure about the blockhash though

cc @lightclient

@fjl fjl added this to the 1.15.6 milestone Mar 18, 2025
@MariusVanDerWijden MariusVanDerWijden merged commit c4f0450 into ethereum:master Mar 18, 2025
3 of 4 checks passed
sivaratrisrinivas pushed a commit to sivaratrisrinivas/go-ethereum that referenced this pull request Apr 21, 2025
…c block (ethereum#27508)

The main use case I see of this is that it allows users to estimate gas
against the same state that they query for their nonce, and the same
state they base the data of their transaction against. This helps ensure
that gas estimation won't fail and the transaction won't revert on-chain
because of a mismatch between the state used for gas estimation and the
state used to generate the inputs to gas estimation or the transaction's
nonce when submitted to the mempool.

This PR also updates the EstimateGas comment based on the new geth
`eth_estimateGas` default of using latest state as of v1.12.0:
ethereum#24363

---------

Co-authored-by: Felix Lange <fjl@twurst.com>
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.

4 participants