foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Anvil treats "pending" and "latest" block the same in `eth_estimateGas`

Open ckoopmann opened this issue 3 years ago • 4 comments

Component

Anvil

Have you ensured that all of these are up to date?

  • [X] Foundry
  • [X] Foundryup

What version of Foundry are you on?

forge 0.2.0 (9b0575e 2022-08-04T00:10:43.515642Z)

What command(s) is the bug in?

anvil

Operating System

macOS (Intel)

Describe the bug

Anvil treats block ids "pending" and "latest" the same (on several methods including eth_estimateGas). Therefore pending transactions are disregarded resulting in incorrect behaviour (since the state should include "the pending state/transactions" as per the eth json-rpc docs).

Relevant code: do_estimate_gas ensure_block_number

ckoopmann avatar Aug 04 '22 06:08 ckoopmann

Therefore pending transactions are disregarded resulting in incorrect behaviour

can you elaborate on this?

we only estimate the transaction with the current state

mattsse avatar Aug 04 '22 11:08 mattsse

Therefore pending transactions are disregarded resulting in incorrect behaviour

can you elaborate on this?

we only estimate the transaction with the current state

I might be misunderstanding the "pending" option for defaultBlock but I'd expect it to run the respective operation based on the state after applying all pending transactions. As opposed to "latest" which uses the state of the latest mined block.

For example if there is a pending (yet unmined) transaction that alters the state such that the transaction I'm trying to estimate fails i'd expect:

  1. The estimation to fail/revert when run with "pending"
  2. Succeed when run with "latest"

Right now both "latest" and "pending" use the state of the latest mined block as far as I can see.

ckoopmann avatar Aug 04 '22 13:08 ckoopmann

For example in the case of the transactionCount we are already incorporating the pending transactions: https://github.com/foundry-rs/foundry/blob/26893ff421093f0904f04db02407b180ee8d5e6e/anvil/src/eth/api.rs#L1875

ckoopmann avatar Aug 04 '22 13:08 ckoopmann

There is a pending StateDB in geth that transactions from the mempool are applied against, it prevents transactions with the same nonce from entering the mempool unless its a replace by fee transaction. I'm not sure if anvil has the same kind of thing, but the pending block tag executes against that StateDB while latest executes against a StateDB rooted from the tip of the chain

tynes avatar Aug 04 '22 19:08 tynes