foundry: bug(`anvil`): inconsistent simulation behaviour
Component
Anvil
Have you ensured that all of these are up to date?
- Foundry
- Foundryup
What version of Foundry are you on?
forge 0.2.0 (b174c3a 2024-02-09T00:16:22.953958126Z)
What command(s) is the bug in?
No response
Operating System
Linux
Describe the bug
Hi! I am experiencing inconsistent behaviour from anvil when simulating a transaction on an old block. There are 2 commands: First set up anvil:
anvil --chain-id 1 --port 8545 --base-fee 1000 --fork-block-number 14684306 -f "<NODE_URL>"
Then run eth_sendUnsignedTransaction:
curl -X POST -H "Content-Type: application/json" --data \
'{
"jsonrpc":"2.0",
"method":"eth_sendUnsignedTransaction",
"params":[{
"from": "0x63341ba917de90498f3903b199df5699b4a55ac0",
"to": "0x7336F819775B1D31Ea472681D70cE7A903482191",
"data": "0xaf8271f7",
"gas": "0x20EF3F",
"gasPrice": "0x34257"
}],
"id":1
}' "http://127.0.0.1:8545"
You might need to run it several times but sometimes it succeeds (as it should) and sometimes it fails. To rerun it, re-run both commands again (to ensure anvil is at the right block level + so it’s the same state):
Listening on 127.0.0.1:8545
eth_sendUnsignedTransaction
Transaction: 0x83b8909a89584001b122eba49310f13a8c7dc690acbc8c7d90350494a1cf7537
Gas used: 1815058
Block Number: 14684307
Block Hash: 0x3f59e4fa81cc720c5d0d7df38ac08fa650cd72c046fe665d428a0e3965595cb9
Block Time: "Sat, 30 Apr 2022 07:40:14 +0000"
Other times it fails:
Listening on 127.0.0.1:8545
eth_sendUnsignedTransaction
Transaction: 0x83b8909a89584001b122eba49310f13a8c7dc690acbc8c7d90350494a1cf7537
Gas used: 2158038
Error: reverted with: EvmError: Revert
Block Number: 14684307
Block Hash: 0x667ea63188956e7fe25fa2806d76651afb745b0c102cf3606c33eb398942c712
Block Time: "Sat, 30 Apr 2022 07:40:07 +0000"
In general, I’m getting a lot of inconsistent behaviour from anvil VS simulating using trace_call using Erigon:
curl -X POST -H "Content-Type: application/json" --data \
'{
"jsonrpc":"2.0",
"method":"trace_call",
"params":[{
"from": "0x63341ba917de90498f3903b199df5699b4a55ac0",
"to": "0x7336F819775B1D31Ea472681D70cE7A903482191",
"data": "0xaf8271f7",
"gas": "0x20EF3F",
"gasPrice": "0x34257"
}, ["trace"], "0xE01092"],
"id":1
}' "<NODE_URL>"
This succeeds every single time I run it.
(Also the txn I’m replicating in this instance is: “0x2b023d65485c4bb68d781960c2196588d03b871dc9eb1c054f596b7ca6f7da56”)
Anvil isn’t just failing on this txn, it’s failing on many txns across the entire history of the chain.
Thank you in advance!
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 16 (1 by maintainers)
Nice finding. Cool, please open new issues if you find unexpected anvil behaviour. Thank you!
It’s not my contract but I’m actively working on understanding how it operates. It certainly performs some safety checks, and the timestamp failure mechanism is very interesting to me.
I added a line
cast rpc --rpc-url=$FORK_URL "anvil_setBlockTimestampInterval" 12 --silentafter the coinbase setter that matches the block interval for mainnet. Interestingly, the TX only succeeds if the value is 12.There are two DELEGATECALL opcodes in there, so perhaps the contract relies on safety checks via another contract.
Either way I’ll keep looking, but I think this bug can be closed — the TX should revert if it includes this obfuscated timestamp check, so Anvil is working as expected.
Ah sorry I forgot to report back on some findings that I had. I was able to isolate the root cause of my inconsistencies down to anvil’s unexpected state differences when forking the previous block and simulating to the next.
For example in BTD’s example above we fork the block at 19922506, at thus have a time of May-22-2024 02:39:23 AM UTC. But what time will the transactions land when we simulate it? It depends on the wall clock time. So in the example above it’ll be simulating the transactions at timestamp May-22-2024 02:39:24 AM UTC (since we slept 1 second).
Some transactions are sensitive to time, thus will revert. Perhaps the mev bot has that logic built in.
Workaround = use
evm_setTimeand those transactions will succeedI’ve also found some transactions which were sensitive to block gas limit (
evm_setBlockGasLimit) and base gas priceanvil_setNextBlockBaseFeePerGasso those are also things you need to set ahead of time prior to propagating the blockHopefully this helps!
The two-TX failure I shared in https://github.com/foundry-rs/foundry/issues/7068#issuecomment-2412141772 is still present on the latest nightly (commit 9f11e6df38)
Here is a minimal repro using cast/anvil only, written in bash:
The first TX succeeds, but the second TX fails.
Here are the full receipts for both transactions:
TX 0:
TX1:
@DicksonWu654 thank you, really useful! yep, was also looking at @BowTiedDevil example to set the time when block should be mined to avoid any internal logic / revert due to this, good to know this worked in your case.
Reopening per above, thanks for detailed steps
Thanks for the reply ser. My concern was more based on the fact that I was getting inconsistent tracing behavior when simulating the same transaction at the same state…
Perhaps related to this issue - I was working on simulating a transaction using reth then sending that transaction to anvil. Reth trace succeeds but anvil fails when forked at the same block. I’m still isolating the bug (and make sure it’s actually anvil & have something reproduceable for others), but I think there might be something wonky with anvil.