foundry icon indicating copy to clipboard operation
foundry copied to clipboard

feat(anvil): 7368 reorg

Open tsnewnami opened this issue 1 year ago • 4 comments

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

tsnewnami avatar Jul 21 '24 22:07 tsnewnami

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 🙏

tsnewnami avatar Jul 29 '24 02:07 tsnewnami

hey @EdwardJES sorry for missing the pings... taking a look shortly

mattsse avatar Aug 04 '24 09:08 mattsse

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
}'

tsnewnami avatar Aug 16 '24 05:08 tsnewnami

Hey @zerosnacks, @mattsse, could I please get another round of reviews at your earliest convenience? 🙏 Tyvm.

tsnewnami avatar Aug 26 '24 06:08 tsnewnami

Thank you @mattsse, I think this is ready for a final review now.

tsnewnami avatar Aug 31 '24 10:08 tsnewnami

Sorry @mattsse, last bump on this one 🙏

tsnewnami avatar Sep 03 '24 15:09 tsnewnami

No problems @mattsse, thanks for your help. That should be the last of the nits 🙏

tsnewnami avatar Sep 05 '24 08:09 tsnewnami

Is this should add doc to book?

s7v7nislands avatar Sep 19 '24 02:09 s7v7nislands

Is this should add doc to book?

That would be great, feel free to open a PR documenting it

zerosnacks avatar Sep 19 '24 07:09 zerosnacks

Hi guys , Thanks for adding this feature , Is there any guide on how to use it , in forge tests.

amankakar avatar Sep 30 '24 04:09 amankakar