Skip to main content

Module prefetch

Module prefetch 

Source
Expand description

Prefetches a new fork blockโ€™s remote cache from the keys of an older cached block.

A fork of latest gets an empty cache on each new head, and every cold account or storage read then blocks the EVM for one RPC round trip. Earlier runs on the same chain read mostly the same accounts and slots, so the keys of the newest older cache file predict the reads. The keys are read again at the new block through the fork backend, so the cache stays exact for that block. The cache file keeps only the keys the EVM then reads, so a wrong prediction only costs an RPC request and does not carry over to the next block.

Re-exportsยง

use super::Fork; ๐Ÿ”’
use alloy_network::Network; ๐Ÿ”’
use alloy_primitives::Address; ๐Ÿ”’
use alloy_primitives::U256; ๐Ÿ”’
use foundry_config::Config; ๐Ÿ”’
use foundry_fork_db::ForkBlockEnv; ๐Ÿ”’
use foundry_fork_db::SharedBackend; ๐Ÿ”’
use foundry_fork_db::backend::BlockingMode; ๐Ÿ”’
use foundry_fork_db::cache::JsonBlockCacheDB; ๐Ÿ”’
use foundry_fork_db::cache::MemDb; ๐Ÿ”’
use parking_lot::Mutex; ๐Ÿ”’
use std::path::Path; ๐Ÿ”’
use std::path::PathBuf; ๐Ÿ”’
use std::sync::Arc; ๐Ÿ”’
use std::sync::atomic::AtomicBool; ๐Ÿ”’
use std::sync::atomic::Ordering; ๐Ÿ”’
use std::sync::mpsc; ๐Ÿ”’

Structsยง

Prefetch ๐Ÿ”’
Background prefetch workers of one fork backend.

Enumsยง

Key ๐Ÿ”’

Constantsยง

MAX_CONCURRENT_REQUESTS ๐Ÿ”’
Prefetch requests in flight at once. The fork backend sends its own cold reads without a limit; this bound keeps a large key set from flooding a public endpoint.
MAX_KEYS ๐Ÿ”’
Largest number of accounts and slots to prefetch. Older cache files can hold the union of many runs; past this size, a prefetch can cost more requests than it saves.

Functionsยง

keys ๐Ÿ”’
Returns the accounts first, then the slots, up to MAX_KEYS.
previous_cache_file ๐Ÿ”’
Returns the cache file of the newest block below number in a chain cache directory.
spawn ๐Ÿ”’
Starts a background prefetch through backend if its cache is empty.