1use 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
32const FIXED_CACHE_ALIGNMENT: usize = 128;
37
38const FIXED_CACHE_ENTRY_OVERHEAD: usize = size_of::<usize>();
40
41const fn fixed_cache_entry_size<K, V>() -> usize {
46 fixed_cache_key_size_with_value::<K>(size_of::<V>())
47}
48
49const fn fixed_cache_key_size_with_value<K>(value: usize) -> usize {
54 let raw_size = FIXED_CACHE_ENTRY_OVERHEAD + size_of::<K>() + value;
55 raw_size.div_ceil(FIXED_CACHE_ALIGNMENT) * FIXED_CACHE_ALIGNMENT
57}
58
59const CODE_CACHE_ENTRY_SIZE: usize = fixed_cache_key_size_with_value::<Address>(MAX_CODE_SIZE);
61
62const STORAGE_CACHE_ENTRY_SIZE: usize =
64 fixed_cache_entry_size::<(Address, StorageKey), StorageValue>();
65
66const ACCOUNT_CACHE_ENTRY_SIZE: usize = fixed_cache_entry_size::<Address, Option<Account>>();
68
69struct EpochCacheConfig;
71impl CacheConfig for EpochCacheConfig {
72 const EPOCHS: bool = true;
73}
74
75type FixedCache<K, V, H = DefaultHashBuilder> = fixed_cache::Cache<K, V, H, EpochCacheConfig>;
77
78#[derive(Debug)]
88pub struct CachedStateProvider<S, const PREWARM: bool = false> {
89 state_provider: S,
91
92 caches: ExecutionCache,
94
95 metrics: CachedStateMetrics,
97}
98
99impl<S> CachedStateProvider<S> {
100 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 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#[derive(Metrics, Clone)]
127#[metrics(scope = "sync.caching")]
128pub struct CachedStateMetrics {
129 execution_cache_created_total: Counter,
131
132 execution_cache_creation_duration_seconds: Histogram,
134
135 code_cache_hits: Gauge,
137
138 code_cache_misses: Gauge,
140
141 code_cache_size: Gauge,
143
144 code_cache_capacity: Gauge,
146
147 code_cache_collisions: Gauge,
149
150 storage_cache_hits: Gauge,
152
153 storage_cache_misses: Gauge,
155
156 storage_cache_size: Gauge,
158
159 storage_cache_capacity: Gauge,
161
162 storage_cache_collisions: Gauge,
164
165 account_cache_hits: Gauge,
167
168 account_cache_misses: Gauge,
170
171 account_cache_size: Gauge,
173
174 account_cache_capacity: Gauge,
176
177 account_cache_collisions: Gauge,
179}
180
181impl CachedStateMetrics {
182 pub fn reset(&self) {
184 self.code_cache_hits.set(0);
186 self.code_cache_misses.set(0);
187 self.code_cache_collisions.set(0);
188
189 self.storage_cache_hits.set(0);
191 self.storage_cache_misses.set(0);
192 self.storage_cache_collisions.set(0);
193
194 self.account_cache_hits.set(0);
196 self.account_cache_misses.set(0);
197 self.account_cache_collisions.set(0);
198 }
199
200 pub fn zeroed() -> Self {
202 let zeroed = Self::default();
203 zeroed.reset();
204 zeroed
205 }
206
207 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#[derive(Debug)]
229pub(crate) struct CacheStatsHandler {
230 collisions: AtomicU64,
231 size: AtomicUsize,
232 capacity: usize,
233}
234
235impl CacheStatsHandler {
236 pub(crate) const fn new(capacity: usize) -> Self {
238 Self { collisions: AtomicU64::new(0), size: AtomicUsize::new(0), capacity }
239 }
240
241 pub(crate) fn collisions(&self) -> u64 {
243 self.collisions.load(Ordering::Relaxed)
244 }
245
246 pub(crate) fn size(&self) -> usize {
248 self.size.load(Ordering::Relaxed)
249 }
250
251 pub(crate) const fn capacity(&self) -> usize {
253 self.capacity
254 }
255
256 pub(crate) fn increment_size(&self) {
258 let _ = self.size.fetch_add(1, Ordering::Relaxed);
259 }
260
261 pub(crate) fn decrement_size(&self) {
263 let _ = self.size.fetch_sub(1, Ordering::Relaxed);
264 }
265
266 pub(crate) fn reset_size(&self) {
268 self.size.store(0, Ordering::Relaxed);
269 }
270
271 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 self.increment_size();
287 }
288 Some((evicted_key, _)) if evicted_key != key => {
289 self.collisions.fetch_add(1, Ordering::Relaxed);
291 }
292 Some(_) => {
293 }
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#[derive(Debug, Clone, PartialEq, Eq)]
323pub enum CachedStatus<T> {
324 NotCached(T),
326 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 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#[derive(Debug, Clone)]
492pub struct ExecutionCache(Arc<ExecutionCacheInner>);
493
494#[derive(Debug)]
496struct ExecutionCacheInner {
497 code_cache: FixedCache<B256, Option<Bytecode>, FbBuildHasher<32>>,
499
500 storage_cache: FixedCache<(Address, StorageKey), StorageValue>,
502
503 account_cache: FixedCache<Address, Option<Account>, FbBuildHasher<20>>,
505
506 code_stats: Arc<CacheStatsHandler>,
508
509 storage_stats: Arc<CacheStatsHandler>,
511
512 account_stats: Arc<CacheStatsHandler>,
514
515 selfdestruct_encountered: Once,
517}
518
519impl ExecutionCache {
520 const MIN_CACHE_SIZE_WITH_EPOCHS: usize = 1 << 12; pub const fn bytes_to_entries(size_bytes: usize, entry_size: usize) -> usize {
529 let entries = size_bytes / entry_size;
530 let rounded = if entries == 0 { 1 } else { (entries + 1).next_power_of_two() >> 1 };
532 if rounded < Self::MIN_CACHE_SIZE_WITH_EPOCHS {
534 Self::MIN_CACHE_SIZE_WITH_EPOCHS
535 } else {
536 rounded
537 }
538 }
539
540 pub fn new(total_cache_size: usize) -> Self {
542 let code_cache_size = (total_cache_size * 556) / 10000; let storage_cache_size = (total_cache_size * 8888) / 10000; let account_cache_size = (total_cache_size * 556) / 10000; 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 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 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 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 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 fn insert_code(&self, hash: B256, code: Option<Bytecode>) {
633 self.0.code_cache.insert(hash, code);
634 }
635
636 fn insert_account(&self, address: Address, account: Option<Account>) {
638 self.0.account_cache.insert(address, account);
639 }
640
641 #[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 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 account.status.is_not_modified() {
683 continue
684 }
685
686 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 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 for (key, slot) in &account.storage {
723 self.insert_storage(*addr, (*key).into(), Some(slot.present_value));
724 }
725
726 self.insert_account(*addr, Some(Account::from(account_info)));
729 }
730
731 Ok(())
732 }
733
734 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 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#[derive(Debug, Clone)]
769pub struct SavedCache {
770 hash: B256,
772
773 caches: ExecutionCache,
775
776 metrics: CachedStateMetrics,
778
779 usage_guard: Arc<()>,
782
783 disable_cache_metrics: bool,
785}
786
787impl SavedCache {
788 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 pub const fn with_disable_cache_metrics(mut self, disable: bool) -> Self {
795 self.disable_cache_metrics = disable;
796 self
797 }
798
799 pub const fn executed_block_hash(&self) -> B256 {
801 self.hash
802 }
803
804 pub fn split(self) -> (ExecutionCache, CachedStateMetrics, bool) {
806 (self.caches, self.metrics, self.disable_cache_metrics)
807 }
808
809 pub fn is_available(&self) -> bool {
811 Arc::strong_count(&self.usage_guard) == 1
812 }
813
814 pub fn usage_count(&self) -> usize {
816 Arc::strong_count(&self.usage_guard)
817 }
818
819 pub const fn cache(&self) -> &ExecutionCache {
821 &self.caches
822 }
823
824 pub const fn metrics(&self) -> &CachedStateMetrics {
826 &self.metrics
827 }
828
829 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 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 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 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 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(), code: None,
987 account_id: None,
988 }),
989 None, 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 let result = caches.insert_state(&bundle);
1002 assert!(result.is_ok());
1003
1004 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 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 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, code: None,
1032 account_id: None,
1033 }),
1034 None, 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 assert!(caches.insert_state(&bundle).is_ok());
1047
1048 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 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 state: HashMap::from_iter([(
1067 addr1,
1068 BundleAccount::new(
1069 None, None, 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 assert!(caches.insert_state(&bundle).is_ok());
1083
1084 assert!(caches.0.account_cache.get(&addr1).is_none());
1086 assert!(caches.0.account_cache.get(&addr2).is_some());
1087 }
1088}