Skip to content

Conversation

dimartiro
Copy link

@dimartiro dimartiro commented Oct 7, 2025

Description

This PR implements lazy state loading for Anvil fork functionality, inspired by Moonbeam's lazy loading implementation.

Instead of eagerly fetching all blockchain state during fork initialization, this implementation loads state on-demand during transaction execution. This significantly improves fork performance and reduces resource consumption

Key Components

1. ForkedLazyBackend

  • Core backend implementing sp_state_machine::Backend trait
  • Fetches storage values lazily from the forked network via RPC
  • Maintains an in-memory cache (db) of previously accessed state
  • Tracks removed keys to prevent unnecessary remote fetches

2. LazyLoadingCallExecutor

  • Wraps the standard executor to integrate lazy loading into the runtime call flow
  • Delegates execution to the inner executor while providing the lazy backend

3. RPCClient

  • Abstraction layer for RPC communication with the forked network
  • Implements retry logic and rate limiting to handle transient failures
  • Provides methods for fetching: blocks, headers, storage values, storage hashes, and paginated keys

4. Blockchain

  • In-memory blockchain storage for cached blocks and headers
  • Lazy-loads block data from RPC when not found locally
  • Maintains fork metadata (best block, finalized block, leaves)

5. RawIter

  • Custom storage iterator that merges local and remote state
  • Iterates over storage keys by first checking the local cache, then falling back to RPC
  • Respects prefix filters and removed keys during iteration

Benefits

  • Faster fork initialization: No upfront state loading required
  • Reduced memory usage: Only accessed state is stored locally
  • Better developer experience: Quicker iteration when testing against forked networks

@dimartiro dimartiro changed the base branch from master to feat/initialForking October 20, 2025 18:30
@jimjbrettj jimjbrettj mentioned this pull request Oct 20, 2025
3 tasks
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.

2 participants