Skip to content

Conversation

@dimartiro
Copy link

@dimartiro dimartiro commented Nov 21, 2025

Description

This PR implements forking functionality for anvil-polkadot, allowing users to fork from a remote Substrate/Polkadot node and run local tests against live network state.

Adds the ability to fork from a running anvil-polkadot node (or compatible Substrate node) using the --fork-url flag. The implementation uses a lazy-loading approach to fetch state from the remote node on-demand, caching values locally for performance

Manual testing

After compiling the entire project using cargo build --release

1. Start base node

RUST_LOG=info ./target/release/anvil-polkadot --port 9970

2. Modify state on base node

./target/release/cast send 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 \
    --value 5ether \
    --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
    --rpc-url http://localhost:9970

3. Verify modified balance

./target/release/cast balance 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 \
    --rpc-url http://localhost:9970

Expected Output: ~9995 ETH (10000 - 5 - gas)

4. Start fork node (inherits modified state)

RUST_LOG=info ./target/release/anvil-polkadot \
    --port 8545 \
    --fork-url http://localhost:9944

5. Verify fork inherited state

./target/release/cast balance 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 \
    --rpc-url http://localhost:8545

Expected Output: ~9995 ETH ✅ (matches base node)

6. Make changes on fork (doesn't affect base)

./target/release/cast send 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 \
    --value 1ether \
    --private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
    --rpc-url http://localhost:8545

7. Verify isolation

./target/release/cast balance 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 \
    --rpc-url http://localhost:9970

Exepected Output: ~9995 ETH (unchanged)

./target/release/cast balance 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 \
    --rpc-url http://localhost:8545

Expected Output: ~9994 ETH (changed)

Integration tests

  • Check forked node retrieves right state
  • Test forking using latest finalized block and block height (positive and negative numbers)
  • Fork a node with a smart contract deployed and check storage is the same

Closes: ChainSafe/gossamer-parity#52, ChainSafe/gossamer-parity#58, ChainSafe/gossamer-parity#65, ChainSafe/gossamer-parity#84

@dimartiro dimartiro changed the title [WIP] Implement forking support with lazy loading backend Implement forking support with lazy loading backend Nov 24, 2025
Copy link

@alindima alindima left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. Let's bring in the latest changes from the feature branch

@dimartiro dimartiro requested a review from alindima November 25, 2025 16:49
/// Tests that forking preserves contract state from source chain and that multiple contract
/// instances maintain independent storage
#[tokio::test(flavor = "multi_thread")]
async fn test_fork_with_contract_deployment() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not neccessarily for this PR, but have a look at what other forking-related test scenarios regular anvil has. We should mirror them where it makes sense

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as an example, what we discussed earlier in the call, using calls which mutate the state on top of a forked network (anvil_setBalance, etc)

Copy link
Author

@dimartiro dimartiro Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we are already doing that with test_fork_preserves_state_and_allows_modifications right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants