ethereum-optimism / op-geth

GNU Lesser General Public License v3.0
255 stars 653 forks source link

New overlays feature for doing ad-hoc simulations of existing contracts with modified bytecode #259

Open crebsy opened 4 months ago

crebsy commented 4 months ago

Description

In this PR I'm adding so-called overlays for doing ad-hoc state simulations of contracts which replay the state with a modified bytecode for any given block range post-bedrock. I'm planning to port the same feature into the legacy optimism geth later and delegate the RPC calls to the historical RPC as well like it's being done for other RPC methods already.

The new feature adds two new RPC methods: overlay_callConstructor and overlay_getLogs which can be used to patch a contract with new bytecode and get the modified logs for it.

Tests

I've added a new postman collection for integration testing/refactoring which patches two contracts on OP mainnet which have been created with CREATE and CREATE2. I've also added tests that retrieve all logs with the original bytecode and with the modified one and check the results.

Additional context

Overlays allow you to add your custom logic to already deployed contracts and simulate on top of them. With overlays you can create new view functions, modify existing ones, change field visibility, emit new events and query the historical data of any contract with your modified source code.

Similar commercial products exist in the space and I believe making this feature opensource in op-geth will make it easier for everyone to tinker with it and build new cool things on top of it ✨

Usage See README

crebsy commented 2 months ago

I just rebased this PR to fbf1ff74c288c6e4080f78b70147c2cd5aeab3d6 anyone wants to test this, pls have a look at my README documenting the new overlay API and the new postman collection. It contains several integration tests for contracts created with CREATE and CREATE2 which are deployed on op-mainnet and can be run against an archive node with postman.

There's also a TODO to add support for patching pre-bedrock contracts if you wanted to use overlays with older contracts which have been originally deployed pre-bedrock. But this could be done later as well imo.

The functionality is very similar to the one from erigon: https://github.com/ledgerwatch/erigon/pull/9438 However, the main difference is performance. Thanks to the call indexes in erigon, the query performance for large block ranges is pretty amazing. Unfortunately, I couldn't find such an index in op-geth's code base so this will result in way slower response times for bigger block ranges because all blocks from a given range will be simulated. But once we build such an index, it's an easy fix :mechanical_arm:

Would be great if someone could review this and lmk if anything needs to be adapted.