feat(anvil): 7368 reorg
closes https://github.com/foundry-rs/foundry/issues/7368
Motivation
Chain reorgs are an unavoidable part of the Ethereum PoS network, and applications should be tolerant to them. L2's also often rely on Ethereum for their finality, such as OP and the derivation pipeline. Having functionality to test reorgs is an important part of building fault-tolerant and resilient systems. What happens when your indexer reorgs? How will you relayer service handle duplicate nonces being consumed? Should the reorged relay transactions be resubmitted? Any service that has side effects from chain state needs to be tested against reorg scenarios.
Solution
- Find the common ancestor between current chain and reorg chain
- Revert state back to common ancestor
- Mine new blocks to the desired len, optionally passing in a set of tx block pairs
Hey @zerosnacks, @mattsse
I've had a go at trying to implement this, from what I can see the logic seems to be sound. However, there are likely some edge cases that were missed, or more efficient code paths could have been taken.
I would also appreciate any feedback on the ergonomics of this API. What are your thoughts on supplying a JSON file where each object is Vec<(TransactionRequest, u64).
Thank you 🙏
hey @EdwardJES sorry for missing the pings... taking a look shortly
Hey @mattsse, this is ready for another review. I've simplified the logic that touches storage, but struggled to simplify the logic of transforming the TransactionRequests into a mineable state.
Here is an example payload if you'd like to try it out tyvm 🙏
curl -X POST http://127.0.0.1:8545/ \
-H "Content-Type: application/json" \
-d '{
"jsonrpc": "2.0",
"method": "anvil_reorg",
"params": [
5, # Example depth
[
[
{
"from": "0x976EA74026E726554dB657fA54763abd0C3a0aa9",
"to": "0x1199bc69f16FDD6690DC40339EC445FaE1b6DD11",
"value": 100
},
1
],
[
{
"from": "0x976EA74026E726554dB657fA54763abd0C3a0aa9",
"to": "0x1199bc69f16FDD6690DC40339EC445FaE1b6DD11",
"value": 200
},
2
]
]
],
"id": 1
}'
Hey @zerosnacks, @mattsse, could I please get another round of reviews at your earliest convenience? 🙏 Tyvm.
Thank you @mattsse, I think this is ready for a final review now.
Sorry @mattsse, last bump on this one 🙏
No problems @mattsse, thanks for your help. That should be the last of the nits 🙏
Is this should add doc to book?
Is this should add doc to book?
That would be great, feel free to open a PR documenting it
Hi guys , Thanks for adding this feature , Is there any guide on how to use it , in forge tests.