diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index b4e99ce48..912daa72b 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -2431,8 +2431,11 @@ where // Atomic bool for letting the prewarm tasks know when to stop let cancel_execution = ManualCancel::default(); + let use_legacy_state_root = + self.config.legacy_state_root() || !root::has_enough_parallelism(); + let (state_root_handle, state_root_task_config, state_root_sender, state_hook) = - if is_descendant_of_persisting_blocks && !self.config.legacy_state_root() { + if is_descendant_of_persisting_blocks && !use_legacy_state_root { let consistent_view = ConsistentDbView::new_with_latest_tip(self.provider.clone())?; // Compute trie input @@ -2557,7 +2560,7 @@ where // a different database transaction per thread and it might end up with a // different view of the database. let (state_root, trie_output, root_elapsed) = if is_descendant_of_persisting_blocks { - if self.config.legacy_state_root() { + if use_legacy_state_root { match self.compute_state_root_parallel(block.header().parent_hash(), &hashed_state) { Ok(result) => { diff --git a/crates/engine/tree/src/tree/root.rs b/crates/engine/tree/src/tree/root.rs index 863799e5d..463c41927 100644 --- a/crates/engine/tree/src/tree/root.rs +++ b/crates/engine/tree/src/tree/root.rs @@ -52,6 +52,18 @@ pub(crate) fn thread_pool_size() -> usize { std::thread::available_parallelism().map_or(3, |num| (num.get() / 2).max(3)) } +/// Determines if the host has enough parallelism to run the state root task. +/// +/// It requires at least 5 parallel threads: +/// - Engine in main thread that spawns the state root task. +/// - State Root Task spawned in [`StateRootTask::spawn`] +/// - Sparse Trie spawned in [`run_sparse_trie`] +/// - Multiproof computation spawned in [`MultiproofManager::spawn_multiproof`] +/// - Storage root computation spawned in [`ParallelProof::multiproof`] +pub(crate) fn has_enough_parallelism() -> bool { + std::thread::available_parallelism().is_ok_and(|num| num.get() >= 5) +} + /// Outcome of the state root computation, including the state root itself with /// the trie updates and the total time spent. #[derive(Debug)]