foundry-rs / foundry-rs/foundry
feat: support offline forks in `vm.createFork`
- Dominant language
- Rust
- Stars
- 10.6k
- Forks
- 2.6k
- Avg merge
- 18h 20m
- Merged PRs (30d)
- 510
Description
### Component
Forge
### Describe the feature you would like
I'm building a cross-chain bridge, and forge's `vm.createFork` is being super useful! However in the unit tests my intent is not fork a real network, instead I just want to create many isolated networks locally, so I can simulate sending cross-chain messages between them.
I need isolated networks because:
1 - Support two contracts deployed at the same address, but with different states.
2 - Isolation: A contract on `Chain A` doesn't see the contract on `Chain B`.
If you just want two isolated networks, `vm.createFork()` has the limitation that it must request a real RPC node, this has several disadvantages:
1. it slowdown the tests
2. can't alter the chain_id, block gas limit, etc...
3. Lot of tests does a lot calls, so you can easily get rate limited by the RPC node, for now I'm running a local node and forking it.. but still not optimal.
Here an example of what I'm doing:
[https://github.com/Analog-Labs/analog-gmp-examples/simple/Counter.t.sol](https://github.com/Analog-Labs/analog-gmp-examples/blob/88b89c36000c985b180835f3b01930915a9117fc/examples/simple/Counter.t.sol#L46-L79)
I wish some way to use the `createFork` to create a clean state context, ex:
```solidity
uint256 forkId = vm.createFork("default");
// or a specialized method
uint256 forkId = vm.createLocalFork();
// or reading some genesis state file
uint256 forkId = vm.createFork("file:../genesis.json");
```
The other solution is using `vm.snapshot()`.. but this one is harder because it doesn't persist the state, so I need to create a new snapshot and delete the previous one everytime I switch between networks, it also doesn't create a clean context as it will contains everything you deployed in the constructor and in the `setUp()`.
### Additional context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.