Skip to main content

Module external_storage

Module external_storage 

Source
Expand description

Storage layouts for contracts that are not part of the local project.

When a forked test touches a contract that isn’t in the local artifacts, its storage slots can still be decoded by compiling the verified source a block explorer has for it.

Finding that source is the caller’s job β€” foundry-evm-traces already knows how to ask a block explorer for it. This module owns what happens next: compiling it for a storage layout, which costs a full solc invocation, and making sure that cost is paid once. forge test runs test contracts in parallel, so lookups are:

  • deduplicated process-wide by LOOKUPS, so concurrent tests touching the same contract compile it once instead of racing each other;
  • persisted to disk once resolved, so later runs skip straight to the layout.

Unverified responses are memoized only for this run so later runs can discover newly verified contracts.

Re-exportsΒ§

use crate::fs; πŸ”’
use crate::namespaced_storage; πŸ”’
use alloy_chains::Chain; πŸ”’
use alloy_primitives::Address; πŸ”’
use alloy_primitives::map::AddressMap; πŸ”’
use foundry_block_explorers::contract::Metadata; πŸ”’
use foundry_compilers::artifacts::CompilerOutput; πŸ”’
use foundry_compilers::artifacts::SolcInput; πŸ”’
use foundry_compilers::artifacts::SolcLanguage; πŸ”’
use foundry_compilers::artifacts::Source; πŸ”’
use foundry_compilers::artifacts::Sources; πŸ”’
use foundry_compilers::artifacts::StorageLayout; πŸ”’
use foundry_compilers::artifacts::output_selection::OutputSelection; πŸ”’
use foundry_compilers::solc::Solc; πŸ”’
use foundry_config::Config; πŸ”’
use serde::Deserialize; πŸ”’
use serde::Deserialize; πŸ”’
use serde::Serialize; πŸ”’
use serde::Serialize; πŸ”’
use std::io::Read; πŸ”’
use std::io::Seek; πŸ”’
use std::io::SeekFrom; πŸ”’
use std::path::Path; πŸ”’
use std::path::PathBuf; πŸ”’
use std::process::Command; πŸ”’
use std::process::Stdio; πŸ”’
use std::sync::Arc; πŸ”’
use std::sync::LazyLock; πŸ”’
use std::sync::Mutex; πŸ”’
use std::sync::MutexGuard; πŸ”’
use std::sync::TryLockError; πŸ”’
use std::sync::atomic::AtomicU64; πŸ”’
use std::sync::atomic::Ordering; πŸ”’
use std::time::Duration; πŸ”’
use std::time::Instant; πŸ”’
use wait_timeout::ChildExt; πŸ”’

StructsΒ§

CachedStorageLayout πŸ”’
Disk representation of a resolved lookup. Cleared by forge cache clean.

ConstantsΒ§

BASE_PATH_SOLC πŸ”’
First solc release with --base-path support.
CACHE_VERSION πŸ”’
Current CachedStorageLayout format.
MIN_STORAGE_LAYOUT_SOLC πŸ”’
First solc release that emits storageLayout. Matches the floor cast storage enforces.
NO_IMPORT_CALLBACK_SOLC πŸ”’
First solc release with --no-import-callback support.

StaticsΒ§

CACHE_TEMP_ID πŸ”’
Makes cache publication paths unique within one process; the PID separates processes.
COMPILER πŸ”’
Bounds expensive compiler installation and execution across parallel tests.
LOOKUPS πŸ”’
Per-(chain, address) lookup slots, shared across the whole process.

FunctionsΒ§

cache_path πŸ”’
Path of the cache entry for address.
compile_storage_layout πŸ”’
Compiles a verified source with storageLayout output enabled and extracts the layout.
extend_external_storage_layout πŸ”’
Appends the ERC-7201 namespaces of contract name in source path to its solc layout.
fetch_external_storage_layouts
Resolves the storage layouts of addresses on chain, compiling verified sources as needed.
lock_until
Acquires a lock without exceeding the caller’s deadline.
read_cached_layout πŸ”’
Reads the layout a previous run resolved for address, if there is one.
resolve πŸ”’
fetch_external_storage_layouts with the cache location supplied, so tests can point it somewhere other than the user’s home directory.
run_solc πŸ”’
Runs solc in an empty directory and terminates it if the remaining lookup budget expires.
write_cached_layout πŸ”’
Persists a resolved layout so later runs can skip the fetch and the compile.

Type AliasesΒ§

ExternalStorageLayout πŸ”’
A resolved storage layout, or None if the contract has no layout we can use.
LookupSlot πŸ”’
A single address’ lookup slot: None until the lookup completes, then its memoized result.