Skip to content

fix(db): let backing databases opt in to storage of absent accounts - #1112

Open
mablr wants to merge 2 commits into
alloy-rs:mainfrom
mablr:fix/cache-absent-account-storage
Open

mablr wants to merge 2 commits into
alloy-rs:mainfrom
mablr:fix/cache-absent-account-storage

Conversation

@mablr

@mablr mablr commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

CacheDB::get_storage reads an uncached slot of an account cached as absent as zero without asking its backing database. A database can hold storage for an account it reports as absent: Anvil loads empty accounts as absent under EIP-161 but keeps storage set on them with anvil_setStorageAt or a stateDiff override. Code delegated to such an account by an EIP-7702 authorization in the same transaction then read zero, while revm read the stored value.

This adds DynDatabase::has_storage_for_absent_accounts, false by default. When the backing database returns true, only a storage wipe short-circuits the read. Deleting an account already wipes its storage, and remove_account_storage keeps that wipe on an account cached as absent, so its reads stay zero. DbStats and CacheDB forward the opt-in to the database they wrap. Databases that don't opt in keep the short-circuit and do no extra backing reads, which matters for RPC-backed databases.

Foundry opts in from Anvil's EvmDb, returning true from Spurious Dragon on. With that change, Anvil's results for the four repro scenarios (two eth_calls with a stateDiff, and two sponsored 7702 transactions over anvil_setStorageAt) match pre-migration Foundry exactly, including gas used.

CacheDB::get_storage reads an uncached slot of an account cached as absent
as zero without asking its database. A database can hold storage for an
account it reports as absent, such as a development node that loads empty
accounts as absent under EIP-161 but keeps storage set on them, and code
delegated to that account by EIP-7702 reads it. Anvil read zero there,
while revm reads the database.

Add DynDatabase::has_storage_for_absent_accounts, false by default. When
the backing database returns true, only a storage wipe short-circuits the
read. Deleting an account already wipes its storage, and
remove_account_storage now leaves a wipe marker on an account cached as
absent so its reads stay zero. Other databases keep the short-circuit and
do no extra reads.

Co-authored-by: Mablr <59505383+mablr@users.noreply.github.com>
DbStats and a nested CacheDB now forward has_storage_for_absent_accounts.
remove_account_storage keeps a wipe only when it drops one on an absent
account, so an opted-in database still serves storage of an account it
reports as absent until a committed deletion wipes it.

@mattsse mattsse left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM with the pushed fix: DbStats and nested CacheDB now forward has_storage_for_absent_accounts, the backing database is only asked when the account is cached absent, and remove_account_storage keeps a wipe only for an absent account (e.g. after a committed selfdestruct). Covered by the updated test.

This branch was successfully deployed

1 active deployment
codspeed — 0a3811ff Deployed Nov 21, 2026 by mattsse via codspeed (jit) #3358
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