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.

A call is predicted more precisely by simulating it on the endpoint, see prefetch_call.

Re-exportsΒ§

use super::Fork; πŸ”’
use crate::constants::CHEATCODE_ADDRESS; πŸ”’
use crate::constants::HARDHAT_CONSOLE_ADDRESS; πŸ”’
use crate::precompiles::P256_VERIFY; πŸ”’
use alloy_network::Network; πŸ”’
use alloy_primitives::Address; πŸ”’
use alloy_primitives::B256; πŸ”’
use alloy_primitives::Bytes; πŸ”’
use alloy_primitives::KECCAK256_EMPTY; πŸ”’
use alloy_primitives::U256; πŸ”’
use alloy_rpc_types::TransactionRequest; πŸ”’
use alloy_rpc_types::state::StateOverride; πŸ”’
use evm2::bytecode::Bytecode; πŸ”’
use evm2::evm::Cache; πŸ”’
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Β§

CHEATCODE_STUB πŸ”’
Runtime code that returns one zero word, standing in for the cheatcode contract in a simulation on the fork’s endpoint: PUSH1 0x20 PUSH1 0x00 RETURN.
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.
MAX_OVERRIDE_BYTES πŸ”’
Largest local state, in bytes of code and storage, sent as overrides with a simulated call.

FunctionsΒ§

keys πŸ”’
Returns the accounts first, then the slots, up to MAX_KEYS.
prefetch_call πŸ”’
Simulates a call on the fork’s endpoint and prefetches the state it touches, see SharedBackend::prefetch_access_list. With overrides, the local state over the fork goes along as overrides.
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.
state_override πŸ”’
Returns the local state of a fork context that differs from remote, the state the fork loaded from its endpoint, as overrides for a simulation there, with a stub for the cheatcode contract. Such state is the test contract and balances set with cheatcodes.