Skip to content

Commit

Permalink
chore: integrate into execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected committed Jan 8, 2025
1 parent e4d6be1 commit 8da1c4f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
28 changes: 10 additions & 18 deletions crates/engine/tree/src/tree/cached_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! Implements a state provider that has a shared cache in front of it.
#![allow(dead_code)]
use alloy_primitives::{map::B256HashMap, Address, StorageKey, StorageValue, B256};
use metrics::Gauge;
use moka::sync::Cache;
use moka::sync::CacheBuilder;
use reth_errors::ProviderResult;
use reth_metrics::Metrics;
use reth_primitives::{Account, Bytecode};
Expand All @@ -14,6 +13,9 @@ use reth_trie::{
updates::TrieUpdates, AccountProof, HashedPostState, HashedStorage, MultiProof,
MultiProofTargets, StorageMultiProof, StorageProof, TrieInput,
};
use revm_primitives::map::DefaultHashBuilder;

type Cache<K, V> = moka::sync::Cache<K, V, alloy_primitives::map::DefaultHashBuilder>;

/// A wrapper of a state provider and a shared cache.
pub(crate) struct CachedStateProvider<S> {
Expand All @@ -31,19 +33,6 @@ impl<S> CachedStateProvider<S>
where
S: StateProvider,
{
/// Creates a new [`CachedStateProvider`] that contains the given state provider and caches.
pub(crate) const fn new(
state_provider: S,
code_cache: Cache<B256, Option<Bytecode>>,
storage_cache: Cache<(Address, StorageKey), Option<StorageValue>>,
account_cache: Cache<Address, Option<Account>>,
metrics: CachedStateMetrics,
) -> Self {
let caches = ProviderCaches { code_cache, account_cache, storage_cache };

Self { state_provider, caches, metrics }
}

/// Creates a new [`CachedStateProvider`] from a [`ProviderCaches`], state provider, and
/// [`CachedStateMetrics`].
pub(crate) const fn new_with_caches(
Expand Down Expand Up @@ -278,9 +267,12 @@ impl ProviderCacheBuilder {
/// Build a [`ProviderCaches`] struct, so that provider caches can be easily cloned.
pub(crate) fn build_caches(self) -> ProviderCaches {
ProviderCaches {
code_cache: Cache::new(self.code_cache_size),
storage_cache: Cache::new(self.storage_cache_size),
account_cache: Cache::new(self.account_cache_size),
code_cache: CacheBuilder::new(self.code_cache_size)
.build_with_hasher(DefaultHashBuilder::default()),
storage_cache: CacheBuilder::new(self.storage_cache_size)
.build_with_hasher(DefaultHashBuilder::default()),
account_cache: CacheBuilder::new(self.account_cache_size)
.build_with_hasher(DefaultHashBuilder::default()),
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion crates/engine/tree/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::{
chain::FromOrchestrator,
engine::{DownloadRequest, EngineApiEvent, EngineApiKind, EngineApiRequest, FromEngine},
persistence::PersistenceHandle,
tree::metrics::EngineApiMetrics,
tree::{
cached_state::{CachedStateMetrics, CachedStateProvider, ProviderCacheBuilder},
metrics::EngineApiMetrics,
},
};
use alloy_consensus::BlockHeader;
use alloy_eips::BlockNumHash;
Expand Down Expand Up @@ -2252,6 +2255,11 @@ where
return Err(e.into())
}

let caches = ProviderCacheBuilder::default().build_caches();
let cache_metrics = CachedStateMetrics::zeroed();
let state_provider =
CachedStateProvider::new_with_caches(state_provider, caches, cache_metrics);

trace!(target: "engine::tree", block=?block.num_hash(), "Executing block");
let executor = self.executor_provider.executor(StateProviderDatabase::new(&state_provider));

Expand Down

0 comments on commit 8da1c4f

Please sign in to comment.