Skip to main content

reth_engine_tree/tree/
cached_state.rs

1//! Execution cache implementation for block processing.
2use alloy_primitives::{
3    map::{DefaultHashBuilder, FbBuildHasher},
4    Address, StorageKey, StorageValue, B256,
5};
6use fixed_cache::{AnyRef, CacheConfig, Stats, StatsHandler};
7use metrics::{Counter, Gauge, Histogram};
8use parking_lot::Once;
9use reth_errors::ProviderResult;
10use reth_metrics::Metrics;
11use reth_primitives_traits::{Account, Bytecode};
12use reth_provider::{
13    AccountReader, BlockHashReader, BytecodeReader, HashedPostStateProvider, StateProofProvider,
14    StateProvider, StateRootProvider, StorageRootProvider,
15};
16use reth_revm::db::BundleState;
17use reth_trie::{
18    updates::TrieUpdates, AccountProof, HashedPostState, HashedStorage, MultiProof,
19    MultiProofTargets, StorageMultiProof, StorageProof, TrieInput,
20};
21use revm_primitives::eip7907::MAX_CODE_SIZE;
22use std::{
23    mem::size_of,
24    sync::{
25        atomic::{AtomicU64, AtomicUsize, Ordering},
26        Arc,
27    },
28    time::Duration,
29};
30use tracing::{debug_span, instrument, trace, warn};
31
32/// Alignment in bytes for entries in the fixed-cache.
33///
34/// Each bucket in `fixed-cache` is aligned to 128 bytes (cache line) due to
35/// `#[repr(C, align(128))]` on the internal `Bucket` struct.
36const FIXED_CACHE_ALIGNMENT: usize = 128;
37
38/// Overhead per entry in the fixed-cache (the `AtomicUsize` tag field).
39const FIXED_CACHE_ENTRY_OVERHEAD: usize = size_of::<usize>();
40
41/// Calculates the actual size of a fixed-cache entry for a given key-value pair.
42///
43/// The entry size is `overhead + size_of::<K>() + size_of::<V>()`, rounded up to the
44/// next multiple of [`FIXED_CACHE_ALIGNMENT`] (128 bytes).
45const fn fixed_cache_entry_size<K, V>() -> usize {
46    fixed_cache_key_size_with_value::<K>(size_of::<V>())
47}
48
49/// Calculates the actual size of a fixed-cache entry for a given key-value pair.
50///
51/// The entry size is `overhead + size_of::<K>() + size_of::<V>()`, rounded up to the
52/// next multiple of [`FIXED_CACHE_ALIGNMENT`] (128 bytes).
53const fn fixed_cache_key_size_with_value<K>(value: usize) -> usize {
54    let raw_size = FIXED_CACHE_ENTRY_OVERHEAD + size_of::<K>() + value;
55    // Round up to next multiple of alignment
56    raw_size.div_ceil(FIXED_CACHE_ALIGNMENT) * FIXED_CACHE_ALIGNMENT
57}
58
59/// Size in bytes of a single code cache entry.
60const CODE_CACHE_ENTRY_SIZE: usize = fixed_cache_key_size_with_value::<Address>(MAX_CODE_SIZE);
61
62/// Size in bytes of a single storage cache entry.
63const STORAGE_CACHE_ENTRY_SIZE: usize =
64    fixed_cache_entry_size::<(Address, StorageKey), StorageValue>();
65
66/// Size in bytes of a single account cache entry.
67const ACCOUNT_CACHE_ENTRY_SIZE: usize = fixed_cache_entry_size::<Address, Option<Account>>();
68
69/// Cache configuration with epoch tracking enabled for O(1) cache invalidation.
70struct EpochCacheConfig;
71impl CacheConfig for EpochCacheConfig {
72    const EPOCHS: bool = true;
73}
74
75/// Type alias for the fixed-cache used for accounts and storage.
76type FixedCache<K, V, H = DefaultHashBuilder> = fixed_cache::Cache<K, V, H, EpochCacheConfig>;
77
78/// A wrapper of a state provider and a shared cache.
79///
80/// The const generic `PREWARM` controls whether every cache miss is populated. This is only
81/// relevant for pre-warm transaction execution with the intention to pre-populate the cache with
82/// data for regular block execution. During regular block execution the cache doesn't need to be
83/// populated because the actual EVM database [`State`](revm::database::State) also caches
84/// internally during block execution and the cache is then updated after the block with the entire
85/// [`BundleState`] output of that block which contains all accessed accounts, code, storage. See
86/// also [`ExecutionCache::insert_state`].
87#[derive(Debug)]
88pub struct CachedStateProvider<S, const PREWARM: bool = false> {
89    /// The state provider
90    state_provider: S,
91
92    /// The caches used for the provider
93    caches: ExecutionCache,
94
95    /// Metrics for the cached state provider
96    metrics: CachedStateMetrics,
97}
98
99impl<S> CachedStateProvider<S> {
100    /// Creates a new [`CachedStateProvider`] from an [`ExecutionCache`], state provider, and
101    /// [`CachedStateMetrics`].
102    pub const fn new(
103        state_provider: S,
104        caches: ExecutionCache,
105        metrics: CachedStateMetrics,
106    ) -> Self {
107        Self { state_provider, caches, metrics }
108    }
109}
110
111impl<S> CachedStateProvider<S, true> {
112    /// Creates a new [`CachedStateProvider`] with prewarming enabled.
113    pub const fn new_prewarm(
114        state_provider: S,
115        caches: ExecutionCache,
116        metrics: CachedStateMetrics,
117    ) -> Self {
118        Self { state_provider, caches, metrics }
119    }
120}
121
122/// Metrics for the cached state provider, showing hits / misses / size for each cache.
123///
124/// This struct combines both the provider-level metrics (hits/misses tracked by the provider)
125/// and the fixed-cache internal stats (collisions, size, capacity).
126#[derive(Metrics, Clone)]
127#[metrics(scope = "sync.caching")]
128pub struct CachedStateMetrics {
129    /// Number of times a new execution cache was created
130    execution_cache_created_total: Counter,
131
132    /// Duration of execution cache creation in seconds
133    execution_cache_creation_duration_seconds: Histogram,
134
135    /// Code cache hits
136    code_cache_hits: Gauge,
137
138    /// Code cache misses
139    code_cache_misses: Gauge,
140
141    /// Code cache size (number of entries)
142    code_cache_size: Gauge,
143
144    /// Code cache capacity (maximum entries)
145    code_cache_capacity: Gauge,
146
147    /// Code cache collisions (hash collisions causing eviction)
148    code_cache_collisions: Gauge,
149
150    /// Storage cache hits
151    storage_cache_hits: Gauge,
152
153    /// Storage cache misses
154    storage_cache_misses: Gauge,
155
156    /// Storage cache size (number of entries)
157    storage_cache_size: Gauge,
158
159    /// Storage cache capacity (maximum entries)
160    storage_cache_capacity: Gauge,
161
162    /// Storage cache collisions (hash collisions causing eviction)
163    storage_cache_collisions: Gauge,
164
165    /// Account cache hits
166    account_cache_hits: Gauge,
167
168    /// Account cache misses
169    account_cache_misses: Gauge,
170
171    /// Account cache size (number of entries)
172    account_cache_size: Gauge,
173
174    /// Account cache capacity (maximum entries)
175    account_cache_capacity: Gauge,
176
177    /// Account cache collisions (hash collisions causing eviction)
178    account_cache_collisions: Gauge,
179}
180
181impl CachedStateMetrics {
182    /// Sets all values to zero, indicating that a new block is being executed.
183    pub fn reset(&self) {
184        // code cache
185        self.code_cache_hits.set(0);
186        self.code_cache_misses.set(0);
187        self.code_cache_collisions.set(0);
188
189        // storage cache
190        self.storage_cache_hits.set(0);
191        self.storage_cache_misses.set(0);
192        self.storage_cache_collisions.set(0);
193
194        // account cache
195        self.account_cache_hits.set(0);
196        self.account_cache_misses.set(0);
197        self.account_cache_collisions.set(0);
198    }
199
200    /// Returns a new zeroed-out instance of [`CachedStateMetrics`].
201    pub fn zeroed() -> Self {
202        let zeroed = Self::default();
203        zeroed.reset();
204        zeroed
205    }
206
207    /// Records a new execution cache creation with its duration.
208    pub(crate) fn record_cache_creation(&self, duration: Duration) {
209        self.execution_cache_created_total.increment(1);
210        self.execution_cache_creation_duration_seconds.record(duration.as_secs_f64());
211    }
212}
213
214/// A stats handler for fixed-cache that tracks collisions and size.
215///
216/// Note: Hits and misses are tracked directly by the [`CachedStateProvider`] via
217/// [`CachedStateMetrics`], not here. The stats handler is used for:
218/// - Collision detection (hash collisions causing eviction of a different key)
219/// - Size tracking
220///
221/// ## Size Tracking
222///
223/// Size is tracked via `on_insert` and `on_remove` callbacks:
224/// - `on_insert`: increment size only when inserting into an empty bucket (no eviction)
225/// - `on_remove`: always decrement size
226///
227/// Collisions (evicting a different key) don't change size since they replace an existing entry.
228#[derive(Debug)]
229pub(crate) struct CacheStatsHandler {
230    collisions: AtomicU64,
231    size: AtomicUsize,
232    capacity: usize,
233}
234
235impl CacheStatsHandler {
236    /// Creates a new stats handler with all counters initialized to zero.
237    pub(crate) const fn new(capacity: usize) -> Self {
238        Self { collisions: AtomicU64::new(0), size: AtomicUsize::new(0), capacity }
239    }
240
241    /// Returns the number of cache collisions.
242    pub(crate) fn collisions(&self) -> u64 {
243        self.collisions.load(Ordering::Relaxed)
244    }
245
246    /// Returns the current size (number of entries).
247    pub(crate) fn size(&self) -> usize {
248        self.size.load(Ordering::Relaxed)
249    }
250
251    /// Returns the capacity (maximum number of entries).
252    pub(crate) const fn capacity(&self) -> usize {
253        self.capacity
254    }
255
256    /// Increments the size counter. Called on cache insert.
257    pub(crate) fn increment_size(&self) {
258        let _ = self.size.fetch_add(1, Ordering::Relaxed);
259    }
260
261    /// Decrements the size counter. Called on cache remove.
262    pub(crate) fn decrement_size(&self) {
263        let _ = self.size.fetch_sub(1, Ordering::Relaxed);
264    }
265
266    /// Resets size to zero. Called on cache clear.
267    pub(crate) fn reset_size(&self) {
268        self.size.store(0, Ordering::Relaxed);
269    }
270
271    /// Resets collision counter to zero (but not size).
272    pub(crate) fn reset_stats(&self) {
273        self.collisions.store(0, Ordering::Relaxed);
274    }
275}
276
277impl<K: PartialEq, V> StatsHandler<K, V> for CacheStatsHandler {
278    fn on_hit(&self, _key: &K, _value: &V) {}
279
280    fn on_miss(&self, _key: AnyRef<'_>) {}
281
282    fn on_insert(&self, key: &K, _value: &V, evicted: Option<(&K, &V)>) {
283        match evicted {
284            None => {
285                // Inserting into an empty bucket
286                self.increment_size();
287            }
288            Some((evicted_key, _)) if evicted_key != key => {
289                // Collision: evicting a different key
290                self.collisions.fetch_add(1, Ordering::Relaxed);
291            }
292            Some(_) => {
293                // Updating the same key, size unchanged
294            }
295        }
296    }
297
298    fn on_remove(&self, _key: &K, _value: &V) {
299        self.decrement_size();
300    }
301}
302
303impl<S: AccountReader, const PREWARM: bool> AccountReader for CachedStateProvider<S, PREWARM> {
304    fn basic_account(&self, address: &Address) -> ProviderResult<Option<Account>> {
305        if PREWARM {
306            match self.caches.get_or_try_insert_account_with(*address, || {
307                self.state_provider.basic_account(address)
308            })? {
309                CachedStatus::NotCached(value) | CachedStatus::Cached(value) => Ok(value),
310            }
311        } else if let Some(account) = self.caches.0.account_cache.get(address) {
312            self.metrics.account_cache_hits.increment(1);
313            Ok(account)
314        } else {
315            self.metrics.account_cache_misses.increment(1);
316            self.state_provider.basic_account(address)
317        }
318    }
319}
320
321/// Represents the status of a key in the cache.
322#[derive(Debug, Clone, PartialEq, Eq)]
323pub enum CachedStatus<T> {
324    /// The key is not in the cache (or was invalidated). The value was recalculated.
325    NotCached(T),
326    /// The key exists in cache and has a specific value.
327    Cached(T),
328}
329
330impl<S: StateProvider, const PREWARM: bool> StateProvider for CachedStateProvider<S, PREWARM> {
331    fn storage(
332        &self,
333        account: Address,
334        storage_key: StorageKey,
335    ) -> ProviderResult<Option<StorageValue>> {
336        if PREWARM {
337            match self.caches.get_or_try_insert_storage_with(account, storage_key, || {
338                self.state_provider.storage(account, storage_key).map(Option::unwrap_or_default)
339            })? {
340                CachedStatus::NotCached(value) | CachedStatus::Cached(value) => {
341                    // The slot that was never written to is indistinguishable from a slot
342                    // explicitly set to zero. We return `None` in both cases.
343                    Ok(Some(value).filter(|v| !v.is_zero()))
344                }
345            }
346        } else if let Some(value) = self.caches.0.storage_cache.get(&(account, storage_key)) {
347            self.metrics.storage_cache_hits.increment(1);
348            Ok(Some(value).filter(|v| !v.is_zero()))
349        } else {
350            self.metrics.storage_cache_misses.increment(1);
351            self.state_provider.storage(account, storage_key)
352        }
353    }
354}
355
356impl<S: BytecodeReader, const PREWARM: bool> BytecodeReader for CachedStateProvider<S, PREWARM> {
357    fn bytecode_by_hash(&self, code_hash: &B256) -> ProviderResult<Option<Bytecode>> {
358        if PREWARM {
359            match self.caches.get_or_try_insert_code_with(*code_hash, || {
360                self.state_provider.bytecode_by_hash(code_hash)
361            })? {
362                CachedStatus::NotCached(code) | CachedStatus::Cached(code) => Ok(code),
363            }
364        } else if let Some(code) = self.caches.0.code_cache.get(code_hash) {
365            self.metrics.code_cache_hits.increment(1);
366            Ok(code)
367        } else {
368            self.metrics.code_cache_misses.increment(1);
369            self.state_provider.bytecode_by_hash(code_hash)
370        }
371    }
372}
373
374impl<S: StateRootProvider, const PREWARM: bool> StateRootProvider
375    for CachedStateProvider<S, PREWARM>
376{
377    fn state_root(&self, hashed_state: HashedPostState) -> ProviderResult<B256> {
378        self.state_provider.state_root(hashed_state)
379    }
380
381    fn state_root_from_nodes(&self, input: TrieInput) -> ProviderResult<B256> {
382        self.state_provider.state_root_from_nodes(input)
383    }
384
385    fn state_root_with_updates(
386        &self,
387        hashed_state: HashedPostState,
388    ) -> ProviderResult<(B256, TrieUpdates)> {
389        self.state_provider.state_root_with_updates(hashed_state)
390    }
391
392    fn state_root_from_nodes_with_updates(
393        &self,
394        input: TrieInput,
395    ) -> ProviderResult<(B256, TrieUpdates)> {
396        self.state_provider.state_root_from_nodes_with_updates(input)
397    }
398}
399
400impl<S: StateProofProvider, const PREWARM: bool> StateProofProvider
401    for CachedStateProvider<S, PREWARM>
402{
403    fn proof(
404        &self,
405        input: TrieInput,
406        address: Address,
407        slots: &[B256],
408    ) -> ProviderResult<AccountProof> {
409        self.state_provider.proof(input, address, slots)
410    }
411
412    fn multiproof(
413        &self,
414        input: TrieInput,
415        targets: MultiProofTargets,
416    ) -> ProviderResult<MultiProof> {
417        self.state_provider.multiproof(input, targets)
418    }
419
420    fn witness(
421        &self,
422        input: TrieInput,
423        target: HashedPostState,
424    ) -> ProviderResult<Vec<alloy_primitives::Bytes>> {
425        self.state_provider.witness(input, target)
426    }
427}
428
429impl<S: StorageRootProvider, const PREWARM: bool> StorageRootProvider
430    for CachedStateProvider<S, PREWARM>
431{
432    fn storage_root(
433        &self,
434        address: Address,
435        hashed_storage: HashedStorage,
436    ) -> ProviderResult<B256> {
437        self.state_provider.storage_root(address, hashed_storage)
438    }
439
440    fn storage_proof(
441        &self,
442        address: Address,
443        slot: B256,
444        hashed_storage: HashedStorage,
445    ) -> ProviderResult<StorageProof> {
446        self.state_provider.storage_proof(address, slot, hashed_storage)
447    }
448
449    fn storage_multiproof(
450        &self,
451        address: Address,
452        slots: &[B256],
453        hashed_storage: HashedStorage,
454    ) -> ProviderResult<StorageMultiProof> {
455        self.state_provider.storage_multiproof(address, slots, hashed_storage)
456    }
457}
458
459impl<S: BlockHashReader, const PREWARM: bool> BlockHashReader for CachedStateProvider<S, PREWARM> {
460    fn block_hash(&self, number: alloy_primitives::BlockNumber) -> ProviderResult<Option<B256>> {
461        self.state_provider.block_hash(number)
462    }
463
464    fn canonical_hashes_range(
465        &self,
466        start: alloy_primitives::BlockNumber,
467        end: alloy_primitives::BlockNumber,
468    ) -> ProviderResult<Vec<B256>> {
469        self.state_provider.canonical_hashes_range(start, end)
470    }
471}
472
473impl<S: HashedPostStateProvider, const PREWARM: bool> HashedPostStateProvider
474    for CachedStateProvider<S, PREWARM>
475{
476    fn hashed_post_state(&self, bundle_state: &reth_revm::db::BundleState) -> HashedPostState {
477        self.state_provider.hashed_post_state(bundle_state)
478    }
479}
480
481/// Execution cache used during block processing.
482///
483/// Optimizes state access by maintaining in-memory copies of frequently accessed
484/// accounts, storage slots, and bytecode. Works in conjunction with prewarming
485/// to reduce database I/O during block execution.
486///
487/// ## Storage Invalidation
488///
489/// Since EIP-6780, SELFDESTRUCT only works within the same transaction where the
490/// contract was created, so we don't need to handle clearing the storage.
491#[derive(Debug, Clone)]
492pub struct ExecutionCache(Arc<ExecutionCacheInner>);
493
494/// Inner state of the [`ExecutionCache`], wrapped in a single [`Arc`].
495#[derive(Debug)]
496struct ExecutionCacheInner {
497    /// Cache for contract bytecode, keyed by code hash.
498    code_cache: FixedCache<B256, Option<Bytecode>, FbBuildHasher<32>>,
499
500    /// Flat storage cache: maps `(Address, StorageKey)` to storage value.
501    storage_cache: FixedCache<(Address, StorageKey), StorageValue>,
502
503    /// Cache for basic account information (nonce, balance, code hash).
504    account_cache: FixedCache<Address, Option<Account>, FbBuildHasher<20>>,
505
506    /// Stats handler for the code cache (shared with the cache via [`Stats`]).
507    code_stats: Arc<CacheStatsHandler>,
508
509    /// Stats handler for the storage cache (shared with the cache via [`Stats`]).
510    storage_stats: Arc<CacheStatsHandler>,
511
512    /// Stats handler for the account cache (shared with the cache via [`Stats`]).
513    account_stats: Arc<CacheStatsHandler>,
514
515    /// One-time notification when SELFDESTRUCT is encountered
516    selfdestruct_encountered: Once,
517}
518
519impl ExecutionCache {
520    /// Minimum cache size required when epochs are enabled.
521    /// With EPOCHS=true, fixed-cache requires 12 bottom bits to be zero (2 needed + 10 epoch).
522    const MIN_CACHE_SIZE_WITH_EPOCHS: usize = 1 << 12; // 4096
523
524    /// Converts a byte size to number of cache entries, rounding down to a power of two.
525    ///
526    /// Fixed-cache requires power-of-two sizes for efficient indexing.
527    /// With epochs enabled, the minimum size is 4096 entries.
528    pub const fn bytes_to_entries(size_bytes: usize, entry_size: usize) -> usize {
529        let entries = size_bytes / entry_size;
530        // Round down to nearest power of two
531        let rounded = if entries == 0 { 1 } else { (entries + 1).next_power_of_two() >> 1 };
532        // Ensure minimum size for epoch tracking
533        if rounded < Self::MIN_CACHE_SIZE_WITH_EPOCHS {
534            Self::MIN_CACHE_SIZE_WITH_EPOCHS
535        } else {
536            rounded
537        }
538    }
539
540    /// Build an [`ExecutionCache`] struct, so that execution caches can be easily cloned.
541    pub fn new(total_cache_size: usize) -> Self {
542        let code_cache_size = (total_cache_size * 556) / 10000; // 5.56% of total
543        let storage_cache_size = (total_cache_size * 8888) / 10000; // 88.88% of total
544        let account_cache_size = (total_cache_size * 556) / 10000; // 5.56% of total
545
546        let code_capacity = Self::bytes_to_entries(code_cache_size, CODE_CACHE_ENTRY_SIZE);
547        let storage_capacity = Self::bytes_to_entries(storage_cache_size, STORAGE_CACHE_ENTRY_SIZE);
548        let account_capacity = Self::bytes_to_entries(account_cache_size, ACCOUNT_CACHE_ENTRY_SIZE);
549
550        let code_stats = Arc::new(CacheStatsHandler::new(code_capacity));
551        let storage_stats = Arc::new(CacheStatsHandler::new(storage_capacity));
552        let account_stats = Arc::new(CacheStatsHandler::new(account_capacity));
553
554        Self(Arc::new(ExecutionCacheInner {
555            code_cache: FixedCache::new(code_capacity, FbBuildHasher::<32>::default())
556                .with_stats(Some(Stats::new(code_stats.clone()))),
557            storage_cache: FixedCache::new(storage_capacity, DefaultHashBuilder::default())
558                .with_stats(Some(Stats::new(storage_stats.clone()))),
559            account_cache: FixedCache::new(account_capacity, FbBuildHasher::<20>::default())
560                .with_stats(Some(Stats::new(account_stats.clone()))),
561            code_stats,
562            storage_stats,
563            account_stats,
564            selfdestruct_encountered: Once::new(),
565        }))
566    }
567
568    /// Gets code from cache, or inserts using the provided function.
569    pub fn get_or_try_insert_code_with<E>(
570        &self,
571        hash: B256,
572        f: impl FnOnce() -> Result<Option<Bytecode>, E>,
573    ) -> Result<CachedStatus<Option<Bytecode>>, E> {
574        let mut miss = false;
575        let result = self.0.code_cache.get_or_try_insert_with(hash, |_| {
576            miss = true;
577            f()
578        })?;
579
580        if miss {
581            Ok(CachedStatus::NotCached(result))
582        } else {
583            Ok(CachedStatus::Cached(result))
584        }
585    }
586
587    /// Gets storage from cache, or inserts using the provided function.
588    pub fn get_or_try_insert_storage_with<E>(
589        &self,
590        address: Address,
591        key: StorageKey,
592        f: impl FnOnce() -> Result<StorageValue, E>,
593    ) -> Result<CachedStatus<StorageValue>, E> {
594        let mut miss = false;
595        let result = self.0.storage_cache.get_or_try_insert_with((address, key), |_| {
596            miss = true;
597            f()
598        })?;
599
600        if miss {
601            Ok(CachedStatus::NotCached(result))
602        } else {
603            Ok(CachedStatus::Cached(result))
604        }
605    }
606
607    /// Gets account from cache, or inserts using the provided function.
608    pub fn get_or_try_insert_account_with<E>(
609        &self,
610        address: Address,
611        f: impl FnOnce() -> Result<Option<Account>, E>,
612    ) -> Result<CachedStatus<Option<Account>>, E> {
613        let mut miss = false;
614        let result = self.0.account_cache.get_or_try_insert_with(address, |_| {
615            miss = true;
616            f()
617        })?;
618
619        if miss {
620            Ok(CachedStatus::NotCached(result))
621        } else {
622            Ok(CachedStatus::Cached(result))
623        }
624    }
625
626    /// Insert storage value into cache.
627    pub fn insert_storage(&self, address: Address, key: StorageKey, value: Option<StorageValue>) {
628        self.0.storage_cache.insert((address, key), value.unwrap_or_default());
629    }
630
631    /// Insert code into cache.
632    fn insert_code(&self, hash: B256, code: Option<Bytecode>) {
633        self.0.code_cache.insert(hash, code);
634    }
635
636    /// Insert account into cache.
637    fn insert_account(&self, address: Address, account: Option<Account>) {
638        self.0.account_cache.insert(address, account);
639    }
640
641    /// Inserts the post-execution state changes into the cache.
642    ///
643    /// This method is called after transaction execution to update the cache with
644    /// the touched and modified state. The insertion order is critical:
645    ///
646    /// 1. Bytecodes: Insert contract code first
647    /// 2. Storage slots: Update storage values for each account
648    /// 3. Accounts: Update account info (nonce, balance, code hash)
649    ///
650    /// ## Why This Order Matters
651    ///
652    /// Account information references bytecode via code hash. If we update accounts
653    /// before bytecode, we might create cache entries pointing to non-existent code.
654    /// The current order ensures cache consistency.
655    ///
656    /// ## Error Handling
657    ///
658    /// Returns an error if the state updates are inconsistent and should be discarded.
659    #[instrument(level = "debug", target = "engine::caching", skip_all)]
660    #[expect(clippy::result_unit_err)]
661    pub fn insert_state(&self, state_updates: &BundleState) -> Result<(), ()> {
662        let _enter =
663            debug_span!(target: "engine::tree", "contracts", len = state_updates.contracts.len())
664                .entered();
665        // Insert bytecodes
666        for (code_hash, bytecode) in &state_updates.contracts {
667            self.insert_code(*code_hash, Some(Bytecode(bytecode.clone())));
668        }
669        drop(_enter);
670
671        let _enter = debug_span!(
672            target: "engine::tree",
673            "accounts",
674            accounts = state_updates.state.len(),
675            storages =
676                state_updates.state.values().map(|account| account.storage.len()).sum::<usize>()
677        )
678        .entered();
679        for (addr, account) in &state_updates.state {
680            // If the account was not modified, as in not changed and not destroyed, then we have
681            // nothing to do w.r.t. this particular account and can move on
682            if account.status.is_not_modified() {
683                continue
684            }
685
686            // If the original account had code (was a contract), we must clear the entire cache
687            // because we can't efficiently invalidate all storage slots for a single address.
688            // This should only happen on pre-Dencun networks.
689            //
690            // If the original account had no code (was an EOA or a not yet deployed contract), we
691            // just remove the account from cache - no storage exists for it.
692            if account.was_destroyed() {
693                let had_code =
694                    account.original_info.as_ref().is_some_and(|info| !info.is_empty_code_hash());
695                if had_code {
696                    self.0.selfdestruct_encountered.call_once(|| {
697                        warn!(
698                            target: "engine::caching",
699                            address = ?addr,
700                            info = ?account.info,
701                            original_info = ?account.original_info,
702                            "Encountered an inter-transaction SELFDESTRUCT that reset the storage cache. Are you running a pre-Dencun network?"
703                        );
704                    });
705                    self.clear();
706                    return Ok(())
707                }
708
709                self.0.account_cache.remove(addr);
710                continue
711            }
712
713            // If we have an account that was modified, but it has a `None` account info, some wild
714            // error has occurred because this state should be unrepresentable. An account with
715            // `None` current info, should be destroyed.
716            let Some(ref account_info) = account.info else {
717                trace!(target: "engine::caching", ?account, "Account with None account info found in state updates");
718                return Err(())
719            };
720
721            // Now we iterate over all storage and make updates to the cached storage values
722            for (key, slot) in &account.storage {
723                self.insert_storage(*addr, (*key).into(), Some(slot.present_value));
724            }
725
726            // Insert will update if present, so we just use the new account info as the new value
727            // for the account cache
728            self.insert_account(*addr, Some(Account::from(account_info)));
729        }
730
731        Ok(())
732    }
733
734    /// Clears storage and account caches, resetting them to empty state.
735    ///
736    /// We do not clear the bytecodes cache, because its mapping can never change, as it's
737    /// `keccak256(bytecode) => bytecode`.
738    pub(crate) fn clear(&self) {
739        self.0.storage_cache.clear();
740        self.0.account_cache.clear();
741
742        self.0.storage_stats.reset_size();
743        self.0.account_stats.reset_size();
744    }
745
746    /// Updates the provided metrics with the current stats from the cache's stats handlers,
747    /// and resets the hit/miss/collision counters.
748    pub(crate) fn update_metrics(&self, metrics: &CachedStateMetrics) {
749        metrics.code_cache_size.set(self.0.code_stats.size() as f64);
750        metrics.code_cache_capacity.set(self.0.code_stats.capacity() as f64);
751        metrics.code_cache_collisions.set(self.0.code_stats.collisions() as f64);
752        self.0.code_stats.reset_stats();
753
754        metrics.storage_cache_size.set(self.0.storage_stats.size() as f64);
755        metrics.storage_cache_capacity.set(self.0.storage_stats.capacity() as f64);
756        metrics.storage_cache_collisions.set(self.0.storage_stats.collisions() as f64);
757        self.0.storage_stats.reset_stats();
758
759        metrics.account_cache_size.set(self.0.account_stats.size() as f64);
760        metrics.account_cache_capacity.set(self.0.account_stats.capacity() as f64);
761        metrics.account_cache_collisions.set(self.0.account_stats.collisions() as f64);
762        self.0.account_stats.reset_stats();
763    }
764}
765
766/// A saved cache that has been used for executing a specific block, which has been updated for its
767/// execution.
768#[derive(Debug, Clone)]
769pub struct SavedCache {
770    /// The hash of the block these caches were used to execute.
771    hash: B256,
772
773    /// The caches used for the provider.
774    caches: ExecutionCache,
775
776    /// Metrics for the cached state provider (includes size/capacity/collisions from fixed-cache)
777    metrics: CachedStateMetrics,
778
779    /// A guard to track in-flight usage of this cache.
780    /// The cache is considered available if the strong count is 1.
781    usage_guard: Arc<()>,
782
783    /// Whether to skip cache metrics recording (can be expensive with large cached state).
784    disable_cache_metrics: bool,
785}
786
787impl SavedCache {
788    /// Creates a new instance with the internals
789    pub fn new(hash: B256, caches: ExecutionCache, metrics: CachedStateMetrics) -> Self {
790        Self { hash, caches, metrics, usage_guard: Arc::new(()), disable_cache_metrics: false }
791    }
792
793    /// Sets whether to disable cache metrics recording.
794    pub const fn with_disable_cache_metrics(mut self, disable: bool) -> Self {
795        self.disable_cache_metrics = disable;
796        self
797    }
798
799    /// Returns the hash for this cache
800    pub const fn executed_block_hash(&self) -> B256 {
801        self.hash
802    }
803
804    /// Splits the cache into its caches, metrics, and `disable_cache_metrics` flag, consuming it.
805    pub fn split(self) -> (ExecutionCache, CachedStateMetrics, bool) {
806        (self.caches, self.metrics, self.disable_cache_metrics)
807    }
808
809    /// Returns true if the cache is available for use (no other tasks are currently using it).
810    pub fn is_available(&self) -> bool {
811        Arc::strong_count(&self.usage_guard) == 1
812    }
813
814    /// Returns the current strong count of the usage guard.
815    pub fn usage_count(&self) -> usize {
816        Arc::strong_count(&self.usage_guard)
817    }
818
819    /// Returns the [`ExecutionCache`] belonging to the tracked hash.
820    pub const fn cache(&self) -> &ExecutionCache {
821        &self.caches
822    }
823
824    /// Returns the metrics associated with this cache.
825    pub const fn metrics(&self) -> &CachedStateMetrics {
826        &self.metrics
827    }
828
829    /// Updates the cache metrics (size/capacity/collisions) from the stats handlers.
830    ///
831    /// Note: This can be expensive with large cached state. Use
832    /// `with_disable_cache_metrics(true)` to skip.
833    pub(crate) fn update_metrics(&self) {
834        if self.disable_cache_metrics {
835            return
836        }
837        self.caches.update_metrics(&self.metrics);
838    }
839
840    /// Clears all caches, resetting them to empty state.
841    pub(crate) fn clear(&self) {
842        self.caches.clear();
843    }
844}
845
846#[cfg(test)]
847impl SavedCache {
848    fn clone_guard_for_test(&self) -> Arc<()> {
849        self.usage_guard.clone()
850    }
851}
852
853#[cfg(test)]
854mod tests {
855    use super::*;
856    use alloy_primitives::{map::HashMap, U256};
857    use reth_provider::test_utils::{ExtendedAccount, MockEthProvider};
858    use reth_revm::db::{AccountStatus, BundleAccount};
859    use revm_state::AccountInfo;
860
861    #[test]
862    fn test_empty_storage_cached_state_provider() {
863        let address = Address::random();
864        let storage_key = StorageKey::random();
865        let account = ExtendedAccount::new(0, U256::ZERO);
866
867        let provider = MockEthProvider::default();
868        provider.extend_accounts(vec![(address, account)]);
869
870        let caches = ExecutionCache::new(1000);
871        let state_provider =
872            CachedStateProvider::new(provider, caches, CachedStateMetrics::zeroed());
873
874        let res = state_provider.storage(address, storage_key);
875        assert!(res.is_ok());
876        assert_eq!(res.unwrap(), None);
877    }
878
879    #[test]
880    fn test_uncached_storage_cached_state_provider() {
881        let address = Address::random();
882        let storage_key = StorageKey::random();
883        let storage_value = U256::from(1);
884        let account =
885            ExtendedAccount::new(0, U256::ZERO).extend_storage(vec![(storage_key, storage_value)]);
886
887        let provider = MockEthProvider::default();
888        provider.extend_accounts(vec![(address, account)]);
889
890        let caches = ExecutionCache::new(1000);
891        let state_provider =
892            CachedStateProvider::new(provider, caches, CachedStateMetrics::zeroed());
893
894        let res = state_provider.storage(address, storage_key);
895        assert!(res.is_ok());
896        assert_eq!(res.unwrap(), Some(storage_value));
897    }
898
899    #[test]
900    fn test_get_storage_populated() {
901        let address = Address::random();
902        let storage_key = StorageKey::random();
903        let storage_value = U256::from(1);
904
905        let caches = ExecutionCache::new(1000);
906        caches.insert_storage(address, storage_key, Some(storage_value));
907
908        let result = caches
909            .get_or_try_insert_storage_with(address, storage_key, || Ok::<_, ()>(U256::from(999)));
910        assert_eq!(result.unwrap(), CachedStatus::Cached(storage_value));
911    }
912
913    #[test]
914    fn test_get_storage_empty() {
915        let address = Address::random();
916        let storage_key = StorageKey::random();
917
918        let caches = ExecutionCache::new(1000);
919        caches.insert_storage(address, storage_key, None);
920
921        let result = caches
922            .get_or_try_insert_storage_with(address, storage_key, || Ok::<_, ()>(U256::from(999)));
923        assert_eq!(result.unwrap(), CachedStatus::Cached(U256::ZERO));
924    }
925
926    #[test]
927    fn test_saved_cache_is_available() {
928        let execution_cache = ExecutionCache::new(1000);
929        let cache = SavedCache::new(B256::ZERO, execution_cache, CachedStateMetrics::zeroed());
930
931        assert!(cache.is_available(), "Cache should be available initially");
932
933        let _guard = cache.clone_guard_for_test();
934
935        assert!(!cache.is_available(), "Cache should not be available with active guard");
936    }
937
938    #[test]
939    fn test_saved_cache_multiple_references() {
940        let execution_cache = ExecutionCache::new(1000);
941        let cache =
942            SavedCache::new(B256::from([2u8; 32]), execution_cache, CachedStateMetrics::zeroed());
943
944        let guard1 = cache.clone_guard_for_test();
945        let guard2 = cache.clone_guard_for_test();
946        let guard3 = guard1.clone();
947
948        assert!(!cache.is_available());
949
950        drop(guard1);
951        assert!(!cache.is_available());
952
953        drop(guard2);
954        assert!(!cache.is_available());
955
956        drop(guard3);
957        assert!(cache.is_available());
958    }
959
960    #[test]
961    fn test_insert_state_destroyed_account_with_code_clears_cache() {
962        let caches = ExecutionCache::new(1000);
963
964        // Pre-populate caches with some data
965        let addr1 = Address::random();
966        let addr2 = Address::random();
967        let storage_key = StorageKey::random();
968        caches.insert_account(addr1, Some(Account::default()));
969        caches.insert_account(addr2, Some(Account::default()));
970        caches.insert_storage(addr1, storage_key, Some(U256::from(42)));
971
972        // Verify caches are populated
973        assert!(caches.0.account_cache.get(&addr1).is_some());
974        assert!(caches.0.account_cache.get(&addr2).is_some());
975        assert!(caches.0.storage_cache.get(&(addr1, storage_key)).is_some());
976
977        let bundle = BundleState {
978            // BundleState with a destroyed contract (had code)
979            state: HashMap::from_iter([(
980                Address::random(),
981                BundleAccount::new(
982                    Some(AccountInfo {
983                        balance: U256::ZERO,
984                        nonce: 1,
985                        code_hash: B256::random(), // Non-empty code hash
986                        code: None,
987                        account_id: None,
988                    }),
989                    None, // Destroyed, so no current info
990                    Default::default(),
991                    AccountStatus::Destroyed,
992                ),
993            )]),
994            contracts: Default::default(),
995            reverts: Default::default(),
996            state_size: 0,
997            reverts_size: 0,
998        };
999
1000        // Insert state should clear all caches because a contract was destroyed
1001        let result = caches.insert_state(&bundle);
1002        assert!(result.is_ok());
1003
1004        // Verify all caches were cleared
1005        assert!(caches.0.account_cache.get(&addr1).is_none());
1006        assert!(caches.0.account_cache.get(&addr2).is_none());
1007        assert!(caches.0.storage_cache.get(&(addr1, storage_key)).is_none());
1008    }
1009
1010    #[test]
1011    fn test_insert_state_destroyed_account_without_code_removes_only_account() {
1012        let caches = ExecutionCache::new(1000);
1013
1014        // Pre-populate caches with some data
1015        let addr1 = Address::random();
1016        let addr2 = Address::random();
1017        let storage_key = StorageKey::random();
1018        caches.insert_account(addr1, Some(Account::default()));
1019        caches.insert_account(addr2, Some(Account::default()));
1020        caches.insert_storage(addr1, storage_key, Some(U256::from(42)));
1021
1022        let bundle = BundleState {
1023            // BundleState with a destroyed EOA (no code)
1024            state: HashMap::from_iter([(
1025                addr1,
1026                BundleAccount::new(
1027                    Some(AccountInfo {
1028                        balance: U256::from(100),
1029                        nonce: 1,
1030                        code_hash: alloy_primitives::KECCAK256_EMPTY, // Empty code hash = EOA
1031                        code: None,
1032                        account_id: None,
1033                    }),
1034                    None, // Destroyed
1035                    Default::default(),
1036                    AccountStatus::Destroyed,
1037                ),
1038            )]),
1039            contracts: Default::default(),
1040            reverts: Default::default(),
1041            state_size: 0,
1042            reverts_size: 0,
1043        };
1044
1045        // Insert state should only remove the destroyed account
1046        assert!(caches.insert_state(&bundle).is_ok());
1047
1048        // Verify only addr1 was removed, other data is still present
1049        assert!(caches.0.account_cache.get(&addr1).is_none());
1050        assert!(caches.0.account_cache.get(&addr2).is_some());
1051        assert!(caches.0.storage_cache.get(&(addr1, storage_key)).is_some());
1052    }
1053
1054    #[test]
1055    fn test_insert_state_destroyed_account_no_original_info_removes_only_account() {
1056        let caches = ExecutionCache::new(1000);
1057
1058        // Pre-populate caches
1059        let addr1 = Address::random();
1060        let addr2 = Address::random();
1061        caches.insert_account(addr1, Some(Account::default()));
1062        caches.insert_account(addr2, Some(Account::default()));
1063
1064        let bundle = BundleState {
1065            // BundleState with a destroyed account (has no original info)
1066            state: HashMap::from_iter([(
1067                addr1,
1068                BundleAccount::new(
1069                    None, // No original info
1070                    None, // Destroyed
1071                    Default::default(),
1072                    AccountStatus::Destroyed,
1073                ),
1074            )]),
1075            contracts: Default::default(),
1076            reverts: Default::default(),
1077            state_size: 0,
1078            reverts_size: 0,
1079        };
1080
1081        // Insert state should only remove the destroyed account (no code = no full clear)
1082        assert!(caches.insert_state(&bundle).is_ok());
1083
1084        // Verify only addr1 was removed
1085        assert!(caches.0.account_cache.get(&addr1).is_none());
1086        assert!(caches.0.account_cache.get(&addr2).is_some());
1087    }
1088}