@@ -29,7 +29,7 @@ use metrics::{Counter, Gauge, Histogram};
2929use reth_evm:: { execute:: ExecutableTxFor , ConfigureEvm , Evm , EvmFor , SpecFor } ;
3030use reth_metrics:: Metrics ;
3131use reth_primitives_traits:: NodePrimitives ;
32- use reth_provider:: { BlockReader , StateProviderFactory , StateReader } ;
32+ use reth_provider:: { BlockReader , StateProvider , StateProviderFactory , StateReader } ;
3333use reth_revm:: { database:: StateProviderDatabase , db:: BundleState , state:: EvmState } ;
3434use reth_trie:: MultiProofTargets ;
3535use std:: {
@@ -255,31 +255,35 @@ where
255255 self ;
256256 let hash = env. hash ;
257257
258- debug ! ( target: "engine::caching" , parent_hash=?hash, "Updating execution cache" ) ;
259- // Perform all cache operations atomically under the lock
260- execution_cache. update_with_guard ( |cached| {
261- // consumes the `SavedCache` held by the prewarming task, which releases its usage guard
262- let ( caches, cache_metrics) = saved_cache. split ( ) ;
263- let new_cache = SavedCache :: new ( hash, caches, cache_metrics) ;
264-
265- // Insert state into cache while holding the lock
266- if new_cache. cache ( ) . insert_state ( & state) . is_err ( ) {
267- // Clear the cache on error to prevent having a polluted cache
268- * cached = None ;
269- debug ! ( target: "engine::caching" , "cleared execution cache on update error" ) ;
270- return ;
271- }
258+ if let Some ( saved_cache) = saved_cache {
259+ debug ! ( target: "engine::caching" , parent_hash=?hash, "Updating execution cache" ) ;
260+ // Perform all cache operations atomically under the lock
261+ execution_cache. update_with_guard ( |cached| {
262+ // consumes the `SavedCache` held by the prewarming task, which releases its usage
263+ // guard
264+ let ( caches, cache_metrics) = saved_cache. split ( ) ;
265+ let new_cache = SavedCache :: new ( hash, caches, cache_metrics) ;
266+
267+ // Insert state into cache while holding the lock
268+ if new_cache. cache ( ) . insert_state ( & state) . is_err ( ) {
269+ // Clear the cache on error to prevent having a polluted cache
270+ * cached = None ;
271+ debug ! ( target: "engine::caching" , "cleared execution cache on update error" ) ;
272+ return ;
273+ }
272274
273- new_cache. update_metrics ( ) ;
275+ new_cache. update_metrics ( ) ;
274276
275- // Replace the shared cache with the new one; the previous cache (if any) is dropped.
276- * cached = Some ( new_cache) ;
277- } ) ;
277+ // Replace the shared cache with the new one; the previous cache (if any) is
278+ // dropped.
279+ * cached = Some ( new_cache) ;
280+ } ) ;
278281
279- let elapsed = start. elapsed ( ) ;
280- debug ! ( target: "engine::caching" , parent_hash=?hash, elapsed=?elapsed, "Updated execution cache" ) ;
282+ let elapsed = start. elapsed ( ) ;
283+ debug ! ( target: "engine::caching" , parent_hash=?hash, elapsed=?elapsed, "Updated execution cache" ) ;
281284
282- metrics. cache_saving_duration . set ( elapsed. as_secs_f64 ( ) ) ;
285+ metrics. cache_saving_duration . set ( elapsed. as_secs_f64 ( ) ) ;
286+ }
283287 }
284288
285289 /// Executes the task.
@@ -356,7 +360,7 @@ where
356360{
357361 pub ( super ) env : ExecutionEnv < Evm > ,
358362 pub ( super ) evm_config : Evm ,
359- pub ( super ) saved_cache : SavedCache ,
363+ pub ( super ) saved_cache : Option < SavedCache > ,
360364 /// Provider to obtain the state
361365 pub ( super ) provider : StateProviderBuilder < N , P > ,
362366 pub ( super ) metrics : PrewarmMetrics ,
@@ -400,10 +404,13 @@ where
400404 } ;
401405
402406 // Use the caches to create a new provider with caching
403- let caches = saved_cache. cache ( ) . clone ( ) ;
404- let cache_metrics = saved_cache. metrics ( ) . clone ( ) ;
405- let state_provider =
406- CachedStateProvider :: new_with_caches ( state_provider, caches, cache_metrics) ;
407+ let state_provider: Box < dyn StateProvider > = if let Some ( saved_cache) = saved_cache {
408+ let caches = saved_cache. cache ( ) . clone ( ) ;
409+ let cache_metrics = saved_cache. metrics ( ) . clone ( ) ;
410+ Box :: new ( CachedStateProvider :: new_with_caches ( state_provider, caches, cache_metrics) )
411+ } else {
412+ state_provider
413+ } ;
407414
408415 let state_provider = StateProviderDatabase :: new ( state_provider) ;
409416
0 commit comments