Skip to content

Estimate gas at pending block to fix FailedToObtainBlockhash#507

Open
syntrust wants to merge 3 commits intomainfrom
blockhash-p2
Open

Estimate gas at pending block to fix FailedToObtainBlockhash#507
syntrust wants to merge 3 commits intomainfrom
blockhash-p2

Conversation

@syntrust
Copy link
Collaborator

This PR is another solution as an alternative to #506.

Rationale

The EVM specification requires that BLOCKHASH can only access the most recent 256 generated blocks, i.e., [block.number - 256, block.number - 1], excluding the current block itself.

eth_estimateGas executes in the context of the latest chain head block N by default, so block.number = N.
If BLOCKHASH(N) is called, since N >= block.number, it returns 0.

When explicitly using the pending block tag, the execution context changes. In Geth, block.number = N + 1, i.e., the next block being constructed. Therefore, BLOCKHASH(N) becomes valid (since N = block.number - 1).

In real execution, the transaction is included in block N + k (where k ≥ 1), so block.number = N + k. In the common case where k = 1, this matches the pending context.

Tests

Tests have been performed on the local testnet using extra temporary debug messages to confirm that the mining tx is completed successfully in the case of mining and estimation occurring in the same slot.

@syntrust syntrust requested review from ping-ke and qzhodl March 18, 2026 08:15
@qzhodl
Copy link
Collaborator

qzhodl commented Mar 18, 2026

Do we also need to remove the logic that waits for one slot before calling estimateGas?

@syntrust
Copy link
Collaborator Author

Do we also need to remove the logic that waits for one slot before calling estimateGas?

Done.

@syntrust syntrust requested a review from iteyelmp March 18, 2026 09:57
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.

2 participants