1use clap::{
4 builder::{RangedU64ValueParser, Resettable},
5 Args,
6};
7use eyre::ensure;
8use reth_cli_util::{
9 parse_duration_from_secs_or_ms,
10 parsers::{format_duration_as_secs_or_ms, parse_duration},
11};
12use reth_engine_primitives::{
13 TreeConfig, DEFAULT_BACKFILL_RUN_THRESHOLD, DEFAULT_INVALID_HEADER_HIT_EVICTION_THRESHOLD,
14 DEFAULT_MULTIPROOF_TASK_CHUNK_SIZE, DEFAULT_NUM_STATE_MASKING_BLOCKS,
15 MIN_PERSISTENCE_BACKPRESSURE_THRESHOLD,
16};
17use std::{num::NonZeroUsize, sync::OnceLock, time::Duration};
18
19use crate::node_config::{
20 DEFAULT_CROSS_BLOCK_CACHE_SIZE_MB, DEFAULT_MEMORY_BLOCK_BUFFER_TARGET,
21 DEFAULT_PERSISTENCE_THRESHOLD, DEFAULT_RESERVED_CPU_CORES,
22};
23
24static ENGINE_DEFAULTS: OnceLock<DefaultEngineValues> = OnceLock::new();
26
27#[derive(Debug, Clone)]
31pub struct DefaultEngineValues {
32 persistence_threshold: u64,
33 persistence_backpressure_threshold: u64,
34 num_state_masking_blocks: u64,
35 memory_block_buffer_target: u64,
36 backfill_run_threshold: u64,
37 invalid_header_hit_eviction_threshold: u8,
38 state_cache_disabled: bool,
39 prewarming_disabled: bool,
40 txpool_prewarming_enabled: bool,
41 sender_recovery_cache_enabled: bool,
42 state_provider_metrics: bool,
43 cross_block_cache_size: usize,
44 accept_execution_requests_hash: bool,
45 multiproof_chunk_size: usize,
46 reserved_cpu_cores: usize,
47 precompile_cache_disabled: bool,
48 state_root_fallback: bool,
49 always_process_payload_attributes_on_canonical_head: bool,
50 allow_unwind_canonical_header: bool,
51 storage_worker_count: Option<usize>,
52 account_worker_count: Option<usize>,
53 prewarming_threads: Option<usize>,
54 cache_metrics_disabled: bool,
55 slow_block_threshold: Option<Duration>,
56 disable_sparse_trie_cache_pruning: bool,
57 state_root_task_timeout: Option<String>,
58 share_execution_cache_with_payload_builder: bool,
59 share_sparse_trie_with_payload_builder: bool,
60 suppress_persistence_during_build: bool,
61 bal_parallel_execution_disabled: bool,
62 bal_parallel_state_root_disabled: bool,
63}
64
65impl DefaultEngineValues {
66 pub fn try_init(self) -> Result<(), Self> {
68 ENGINE_DEFAULTS.set(self)
69 }
70
71 pub fn get_global() -> &'static Self {
73 ENGINE_DEFAULTS.get_or_init(Self::default)
74 }
75
76 pub const fn with_persistence_threshold(mut self, v: u64) -> Self {
78 self.persistence_threshold = v;
79 self
80 }
81
82 pub const fn with_persistence_backpressure_threshold(mut self, v: u64) -> Self {
84 self.persistence_backpressure_threshold = v;
85 self
86 }
87
88 pub const fn with_num_state_masking_blocks(mut self, v: u64) -> Self {
90 self.num_state_masking_blocks = v;
91 self
92 }
93
94 pub const fn with_memory_block_buffer_target(mut self, v: u64) -> Self {
96 self.memory_block_buffer_target = v;
97 self
98 }
99
100 pub const fn with_backfill_run_threshold(mut self, v: u64) -> Self {
102 self.backfill_run_threshold = v;
103 self
104 }
105
106 pub const fn with_invalid_header_hit_eviction_threshold(mut self, v: u8) -> Self {
108 self.invalid_header_hit_eviction_threshold = v;
109 self
110 }
111
112 pub const fn with_state_cache_disabled(mut self, v: bool) -> Self {
114 self.state_cache_disabled = v;
115 self
116 }
117
118 pub const fn with_prewarming_disabled(mut self, v: bool) -> Self {
120 self.prewarming_disabled = v;
121 self
122 }
123
124 pub const fn with_txpool_prewarming_enabled(mut self, v: bool) -> Self {
126 self.txpool_prewarming_enabled = v;
127 self
128 }
129
130 pub const fn with_sender_recovery_cache_enabled(mut self, v: bool) -> Self {
132 self.sender_recovery_cache_enabled = v;
133 self
134 }
135
136 pub const fn with_state_provider_metrics(mut self, v: bool) -> Self {
138 self.state_provider_metrics = v;
139 self
140 }
141
142 pub const fn with_cross_block_cache_size(mut self, v: usize) -> Self {
144 self.cross_block_cache_size = v;
145 self
146 }
147
148 pub const fn with_accept_execution_requests_hash(mut self, v: bool) -> Self {
150 self.accept_execution_requests_hash = v;
151 self
152 }
153
154 pub const fn with_multiproof_chunk_size(mut self, v: usize) -> Self {
156 self.multiproof_chunk_size = v;
157 self
158 }
159
160 pub const fn with_reserved_cpu_cores(mut self, v: usize) -> Self {
162 self.reserved_cpu_cores = v;
163 self
164 }
165
166 pub const fn with_precompile_cache_disabled(mut self, v: bool) -> Self {
168 self.precompile_cache_disabled = v;
169 self
170 }
171
172 pub const fn with_state_root_fallback(mut self, v: bool) -> Self {
174 self.state_root_fallback = v;
175 self
176 }
177
178 pub const fn with_always_process_payload_attributes_on_canonical_head(
180 mut self,
181 v: bool,
182 ) -> Self {
183 self.always_process_payload_attributes_on_canonical_head = v;
184 self
185 }
186
187 pub const fn with_allow_unwind_canonical_header(mut self, v: bool) -> Self {
189 self.allow_unwind_canonical_header = v;
190 self
191 }
192
193 pub const fn with_storage_worker_count(mut self, v: Option<usize>) -> Self {
195 self.storage_worker_count = v;
196 self
197 }
198
199 pub const fn with_account_worker_count(mut self, v: Option<usize>) -> Self {
201 self.account_worker_count = v;
202 self
203 }
204
205 pub const fn with_prewarming_threads(mut self, v: Option<usize>) -> Self {
207 self.prewarming_threads = v;
208 self
209 }
210
211 pub const fn with_cache_metrics_disabled(mut self, v: bool) -> Self {
213 self.cache_metrics_disabled = v;
214 self
215 }
216
217 pub const fn with_slow_block_threshold(mut self, v: Option<Duration>) -> Self {
219 self.slow_block_threshold = v;
220 self
221 }
222
223 pub const fn with_disable_sparse_trie_cache_pruning(mut self, v: bool) -> Self {
225 self.disable_sparse_trie_cache_pruning = v;
226 self
227 }
228
229 pub fn with_state_root_task_timeout(mut self, v: Option<String>) -> Self {
231 self.state_root_task_timeout = v;
232 self
233 }
234
235 pub const fn with_share_execution_cache_with_payload_builder(mut self, v: bool) -> Self {
237 self.share_execution_cache_with_payload_builder = v;
238 self
239 }
240
241 pub const fn with_share_sparse_trie_with_payload_builder(mut self, v: bool) -> Self {
243 self.share_sparse_trie_with_payload_builder = v;
244 self
245 }
246
247 pub const fn with_suppress_persistence_during_build(mut self, v: bool) -> Self {
249 self.suppress_persistence_during_build = v;
250 self
251 }
252
253 pub const fn with_bal_parallel_execution_disabled(mut self, v: bool) -> Self {
255 self.bal_parallel_execution_disabled = v;
256 self
257 }
258
259 pub const fn with_bal_parallel_state_root_disabled(mut self, v: bool) -> Self {
261 self.bal_parallel_state_root_disabled = v;
262 self
263 }
264}
265
266impl Default for DefaultEngineValues {
267 fn default() -> Self {
268 Self {
269 persistence_threshold: DEFAULT_PERSISTENCE_THRESHOLD,
270 persistence_backpressure_threshold: MIN_PERSISTENCE_BACKPRESSURE_THRESHOLD,
271 num_state_masking_blocks: DEFAULT_NUM_STATE_MASKING_BLOCKS,
272 memory_block_buffer_target: DEFAULT_MEMORY_BLOCK_BUFFER_TARGET,
273 backfill_run_threshold: DEFAULT_BACKFILL_RUN_THRESHOLD,
274 invalid_header_hit_eviction_threshold: DEFAULT_INVALID_HEADER_HIT_EVICTION_THRESHOLD,
275 state_cache_disabled: false,
276 prewarming_disabled: false,
277 txpool_prewarming_enabled: false,
278 sender_recovery_cache_enabled: true,
279 state_provider_metrics: false,
280 cross_block_cache_size: DEFAULT_CROSS_BLOCK_CACHE_SIZE_MB,
281 accept_execution_requests_hash: false,
282 multiproof_chunk_size: DEFAULT_MULTIPROOF_TASK_CHUNK_SIZE,
283 reserved_cpu_cores: DEFAULT_RESERVED_CPU_CORES,
284 precompile_cache_disabled: false,
285 state_root_fallback: false,
286 always_process_payload_attributes_on_canonical_head: false,
287 allow_unwind_canonical_header: false,
288 storage_worker_count: None,
289 account_worker_count: None,
290 prewarming_threads: None,
291 cache_metrics_disabled: false,
292 slow_block_threshold: None,
293 disable_sparse_trie_cache_pruning: false,
294 state_root_task_timeout: Some("4s".to_string()),
295 share_execution_cache_with_payload_builder: false,
296 share_sparse_trie_with_payload_builder: false,
297 suppress_persistence_during_build: false,
298 bal_parallel_execution_disabled: false,
299 bal_parallel_state_root_disabled: false,
300 }
301 }
302}
303
304fn default_persistence_backpressure_threshold(persistence_threshold: u64) -> u64 {
305 DefaultEngineValues::get_global()
306 .persistence_backpressure_threshold
307 .max(persistence_threshold.saturating_mul(2))
308}
309
310#[derive(Debug, Clone, Args, PartialEq, Eq)]
312#[command(next_help_heading = "Engine")]
313pub struct EngineArgs {
314 #[arg(
321 long = "engine.persistence-threshold",
322 env = "RETH_ENGINE_PERSISTENCE_THRESHOLD",
323 default_value_t = DefaultEngineValues::get_global().persistence_threshold
324 )]
325 pub persistence_threshold: u64,
326
327 #[arg(long = "engine.persistence-backpressure-threshold")]
335 pub persistence_backpressure_threshold: Option<u64>,
336
337 #[arg(
340 long = "engine.num-state-masking-blocks",
341 env = "RETH_ENGINE_NUM_STATE_MASKING_BLOCKS",
342 default_value_t = DefaultEngineValues::get_global().num_state_masking_blocks
343 )]
344 pub num_state_masking_blocks: u64,
345
346 #[arg(long = "engine.memory-block-buffer-target")]
362 pub memory_block_buffer_target: Option<u64>,
363
364 #[arg(
372 long = "engine.backfill-threshold",
373 env = "RETH_ENGINE_BACKFILL_THRESHOLD",
374 default_value_t = DefaultEngineValues::get_global().backfill_run_threshold
375 )]
376 pub backfill_run_threshold: u64,
377
378 #[arg(long = "engine.invalid-header-cache-hit-eviction-threshold", default_value_t = DefaultEngineValues::get_global().invalid_header_hit_eviction_threshold)]
384 pub invalid_header_hit_eviction_threshold: u8,
385
386 #[arg(long = "engine.disable-state-cache", default_value_t = DefaultEngineValues::get_global().state_cache_disabled)]
388 pub state_cache_disabled: bool,
389
390 #[arg(long = "engine.disable-prewarming", alias = "engine.disable-caching-and-prewarming", default_value_t = DefaultEngineValues::get_global().prewarming_disabled)]
392 pub prewarming_disabled: bool,
393
394 #[arg(
396 long = "engine.txpool-prewarming",
397 env = "RETH_ENGINE_TXPOOL_PREWARMING",
398 default_value_t = DefaultEngineValues::get_global().txpool_prewarming_enabled
399 )]
400 pub txpool_prewarming_enabled: bool,
401
402 #[arg(
406 long = "engine.sender-recovery-cache",
407 env = "RETH_ENGINE_SENDER_RECOVERY_CACHE",
408 default_value_t = DefaultEngineValues::get_global().sender_recovery_cache_enabled,
409 num_args = 0..=1,
410 default_missing_value = "true",
411 )]
412 pub sender_recovery_cache_enabled: bool,
413
414 #[arg(long = "engine.state-provider-metrics", default_value_t = DefaultEngineValues::get_global().state_provider_metrics)]
418 pub state_provider_metrics: bool,
419
420 #[arg(long = "engine.cross-block-cache-size", default_value_t = DefaultEngineValues::get_global().cross_block_cache_size)]
422 pub cross_block_cache_size: usize,
423
424 #[arg(long = "engine.accept-execution-requests-hash", default_value_t = DefaultEngineValues::get_global().accept_execution_requests_hash)]
426 pub accept_execution_requests_hash: bool,
427
428 #[arg(long = "engine.multiproof-chunk-size", default_value_t = DefaultEngineValues::get_global().multiproof_chunk_size, value_parser = RangedU64ValueParser::<usize>::new().range(1..))]
430 pub multiproof_chunk_size: usize,
431
432 #[arg(long = "engine.reserved-cpu-cores", default_value_t = DefaultEngineValues::get_global().reserved_cpu_cores)]
434 pub reserved_cpu_cores: usize,
435
436 #[arg(long = "cpu-cores", value_name = "COUNT")]
439 pub cpu_cores: Option<NonZeroUsize>,
440
441 #[arg(long = "engine.disable-precompile-cache", default_value_t = DefaultEngineValues::get_global().precompile_cache_disabled)]
443 pub precompile_cache_disabled: bool,
444
445 #[arg(long = "engine.state-root-fallback", default_value_t = DefaultEngineValues::get_global().state_root_fallback)]
447 pub state_root_fallback: bool,
448
449 #[arg(
455 long = "engine.always-process-payload-attributes-on-canonical-head",
456 default_value_t = DefaultEngineValues::get_global().always_process_payload_attributes_on_canonical_head
457 )]
458 pub always_process_payload_attributes_on_canonical_head: bool,
459
460 #[arg(long = "engine.allow-unwind-canonical-header", default_value_t = DefaultEngineValues::get_global().allow_unwind_canonical_header)]
463 pub allow_unwind_canonical_header: bool,
464
465 #[arg(long = "engine.storage-worker-count", default_value = Resettable::from(DefaultEngineValues::get_global().storage_worker_count.map(|v| v.to_string().into())))]
469 pub storage_worker_count: Option<usize>,
470
471 #[arg(long = "engine.account-worker-count", default_value = Resettable::from(DefaultEngineValues::get_global().account_worker_count.map(|v| v.to_string().into())))]
475 pub account_worker_count: Option<usize>,
476
477 #[arg(long = "engine.prewarming-threads", default_value = Resettable::from(DefaultEngineValues::get_global().prewarming_threads.map(|v| v.to_string().into())))]
480 pub prewarming_threads: Option<usize>,
481
482 #[arg(long = "engine.disable-cache-metrics", default_value_t = DefaultEngineValues::get_global().cache_metrics_disabled)]
484 pub cache_metrics_disabled: bool,
485
486 #[arg(long = "engine.slow-block-threshold", value_parser = parse_duration_from_secs_or_ms, value_name = "DURATION", default_value = Resettable::from(DefaultEngineValues::get_global().slow_block_threshold.map(|threshold| format_duration_as_secs_or_ms(threshold).into())))]
495 pub slow_block_threshold: Option<Duration>,
496
497 #[arg(long = "engine.disable-sparse-trie-cache-pruning", default_value_t = DefaultEngineValues::get_global().disable_sparse_trie_cache_pruning)]
501 pub disable_sparse_trie_cache_pruning: bool,
502
503 #[arg(
512 long = "engine.state-root-task-timeout",
513 value_parser = parse_duration,
514 default_value = Resettable::from(DefaultEngineValues::get_global().state_root_task_timeout.as_ref().map(|v| v.as_str().into())),
515 )]
516 pub state_root_task_timeout: Option<Duration>,
517
518 #[arg(
526 long = "engine.share-execution-cache-with-payload-builder",
527 default_value_t = DefaultEngineValues::get_global().share_execution_cache_with_payload_builder,
528 )]
529 pub share_execution_cache_with_payload_builder: bool,
530
531 #[arg(
543 long = "engine.share-sparse-trie-with-payload-builder",
544 default_value_t = DefaultEngineValues::get_global().share_sparse_trie_with_payload_builder,
545 )]
546 pub share_sparse_trie_with_payload_builder: bool,
547
548 #[arg(
554 long = "engine.suppress-persistence-during-build",
555 default_value_t = DefaultEngineValues::get_global().suppress_persistence_during_build,
556 )]
557 pub suppress_persistence_during_build: bool,
558
559 #[arg(long = "engine.disable-bal-parallel-execution", default_value_t = DefaultEngineValues::get_global().bal_parallel_execution_disabled)]
561 pub bal_parallel_execution_disabled: bool,
562
563 #[arg(long = "engine.disable-bal-parallel-state-root", default_value_t = DefaultEngineValues::get_global().bal_parallel_state_root_disabled)]
566 pub bal_parallel_state_root_disabled: bool,
567
568 #[arg(long = "engine.disable-bal-batch-io", default_value_t = false)]
571 pub disable_bal_batch_io: bool,
572
573 #[arg(
576 long = "engine.prewarm-reuse",
577 env = "RETH_ENGINE_PREWARM_REUSE",
578 default_value_t = false
579 )]
580 pub prewarm_reuse: bool,
581
582 #[cfg(feature = "trie-debug")]
589 #[arg(
590 long = "engine.proof-jitter",
591 value_parser = parse_duration,
592 )]
593 pub proof_jitter: Option<Duration>,
594}
595
596impl Default for EngineArgs {
597 fn default() -> Self {
598 let DefaultEngineValues {
599 persistence_threshold,
600 persistence_backpressure_threshold: _,
601 num_state_masking_blocks,
602 memory_block_buffer_target: _,
603 backfill_run_threshold,
604 invalid_header_hit_eviction_threshold,
605 state_cache_disabled,
606 prewarming_disabled,
607 txpool_prewarming_enabled,
608 sender_recovery_cache_enabled,
609 state_provider_metrics,
610 cross_block_cache_size,
611 accept_execution_requests_hash,
612 multiproof_chunk_size,
613 reserved_cpu_cores,
614 precompile_cache_disabled,
615 state_root_fallback,
616 always_process_payload_attributes_on_canonical_head,
617 allow_unwind_canonical_header,
618 storage_worker_count,
619 account_worker_count,
620 prewarming_threads,
621 cache_metrics_disabled,
622 slow_block_threshold,
623 disable_sparse_trie_cache_pruning,
624 state_root_task_timeout,
625 share_execution_cache_with_payload_builder,
626 share_sparse_trie_with_payload_builder,
627 suppress_persistence_during_build,
628 bal_parallel_execution_disabled,
629 bal_parallel_state_root_disabled,
630 } = DefaultEngineValues::get_global().clone();
631 Self {
632 persistence_threshold,
633 persistence_backpressure_threshold: None,
634 num_state_masking_blocks,
635 memory_block_buffer_target: None,
636 backfill_run_threshold,
637 invalid_header_hit_eviction_threshold,
638 state_cache_disabled,
639 prewarming_disabled,
640 txpool_prewarming_enabled,
641 sender_recovery_cache_enabled,
642 state_provider_metrics,
643 cross_block_cache_size,
644 accept_execution_requests_hash,
645 multiproof_chunk_size,
646 reserved_cpu_cores,
647 cpu_cores: None,
648 precompile_cache_disabled,
649 state_root_fallback,
650 always_process_payload_attributes_on_canonical_head,
651 allow_unwind_canonical_header,
652 storage_worker_count,
653 account_worker_count,
654 prewarming_threads,
655 cache_metrics_disabled,
656 slow_block_threshold,
657 disable_sparse_trie_cache_pruning,
658 state_root_task_timeout: state_root_task_timeout
659 .as_deref()
660 .map(|s| parse_duration(s).expect("valid default duration")),
661 share_execution_cache_with_payload_builder,
662 share_sparse_trie_with_payload_builder,
663 suppress_persistence_during_build,
664 bal_parallel_execution_disabled,
665 bal_parallel_state_root_disabled,
666 disable_bal_batch_io: false,
667 prewarm_reuse: false,
668 #[cfg(feature = "trie-debug")]
669 proof_jitter: None,
670 }
671 }
672}
673
674impl EngineArgs {
675 pub const fn num_state_masking_blocks(&self) -> u64 {
677 if self.persistence_threshold == 0 {
678 0
679 } else {
680 self.num_state_masking_blocks
681 }
682 }
683
684 pub fn memory_block_buffer_target(&self) -> u64 {
686 self.memory_block_buffer_target.unwrap_or_else(|| {
687 self.persistence_threshold
688 .min(DefaultEngineValues::get_global().memory_block_buffer_target)
689 })
690 }
691
692 pub fn persistence_backpressure_threshold(&self) -> u64 {
694 self.persistence_backpressure_threshold.unwrap_or_else(|| {
695 default_persistence_backpressure_threshold(self.persistence_threshold)
696 })
697 }
698
699 pub fn validate(&self) -> eyre::Result<()> {
701 let persistence_backpressure_threshold = self.persistence_backpressure_threshold();
702 let memory_block_buffer_target = self.memory_block_buffer_target();
703 let num_state_masking_blocks = self.num_state_masking_blocks();
704 ensure!(
705 persistence_backpressure_threshold > self.persistence_threshold,
706 "--engine.persistence-backpressure-threshold ({}) must be greater than --engine.persistence-threshold ({})",
707 persistence_backpressure_threshold,
708 self.persistence_threshold
709 );
710 ensure!(
711 memory_block_buffer_target <= self.persistence_threshold,
712 "--engine.memory-block-buffer-target ({}) must be less than or equal to --engine.persistence-threshold ({})",
713 memory_block_buffer_target,
714 self.persistence_threshold,
715 );
716 ensure!(
717 num_state_masking_blocks == 0 ||
718 matches!(
719 num_state_masking_blocks.checked_add(memory_block_buffer_target),
720 Some(window) if window < self.persistence_threshold
721 ),
722 "--engine.num-state-masking-blocks ({}) + --engine.memory-block-buffer-target ({}) must be less than --engine.persistence-threshold ({})",
723 num_state_masking_blocks,
724 memory_block_buffer_target,
725 self.persistence_threshold,
726 );
727 ensure!(
728 !self.state_cache_disabled || !self.txpool_prewarming_enabled,
729 "--engine.txpool-prewarming conflicts with --engine.disable-state-cache"
730 );
731 ensure!(
732 self.bal_parallel_execution_disabled || !self.bal_parallel_state_root_disabled,
733 "--engine.disable-bal-parallel-state-root requires --engine.disable-bal-parallel-execution because BAL parallel execution depends on BAL prewarm state-root updates"
734 );
735 Ok(())
736 }
737
738 pub fn tree_config(&self) -> TreeConfig {
740 let config = TreeConfig::default()
741 .with_persistence_threshold(0)
743 .with_persistence_backpressure_threshold(self.persistence_backpressure_threshold())
744 .with_persistence_threshold(self.persistence_threshold)
745 .with_memory_block_buffer_target(self.memory_block_buffer_target())
746 .with_num_state_masking_blocks(self.num_state_masking_blocks())
747 .with_backfill_run_threshold(self.backfill_run_threshold)
748 .with_invalid_header_hit_eviction_threshold(self.invalid_header_hit_eviction_threshold)
749 .without_state_cache(self.state_cache_disabled)
750 .without_prewarming(self.prewarming_disabled)
751 .with_txpool_prewarming(self.txpool_prewarming_enabled)
752 .with_state_provider_metrics(self.state_provider_metrics)
753 .with_cross_block_cache_size(self.cross_block_cache_size * 1024 * 1024)
754 .with_multiproof_chunk_size(self.multiproof_chunk_size)
755 .with_reserved_cpu_cores(self.reserved_cpu_cores)
756 .without_precompile_cache(self.precompile_cache_disabled)
757 .with_state_root_fallback(self.state_root_fallback)
758 .with_always_process_payload_attributes_on_canonical_head(
759 self.always_process_payload_attributes_on_canonical_head,
760 )
761 .with_unwind_canonical_header(self.allow_unwind_canonical_header)
762 .without_cache_metrics(self.cache_metrics_disabled)
763 .with_slow_block_threshold(self.slow_block_threshold)
764 .with_disable_sparse_trie_cache_pruning(self.disable_sparse_trie_cache_pruning)
765 .with_state_root_task_timeout(self.state_root_task_timeout.filter(|d| !d.is_zero()))
766 .with_share_execution_cache_with_payload_builder(
767 self.share_execution_cache_with_payload_builder,
768 )
769 .with_share_sparse_trie_with_payload_builder(
770 self.share_sparse_trie_with_payload_builder,
771 )
772 .with_suppress_persistence_during_build(self.suppress_persistence_during_build)
773 .without_bal_parallel_execution(self.bal_parallel_execution_disabled)
774 .without_bal_parallel_state_root(self.bal_parallel_state_root_disabled)
775 .without_bal_batch_io(self.disable_bal_batch_io)
776 .with_prewarm_reuse(self.prewarm_reuse);
777 #[cfg(feature = "trie-debug")]
778 let config = config.with_proof_jitter(self.proof_jitter);
779 if let Some(cpu_cores) = self.cpu_cores {
780 config.with_has_enough_parallelism(cpu_cores.get() >= 5)
781 } else {
782 config
783 }
784 }
785}
786
787#[cfg(test)]
788mod tests {
789 use super::*;
790 use clap::Parser;
791
792 #[derive(Parser)]
794 struct CommandParser<T: Args> {
795 #[command(flatten)]
796 args: T,
797 }
798
799 #[test]
800 fn cpu_cores_override_controls_parallel_state_root() {
801 for (cores, enabled) in [(1, false), (4, false), (5, true), (8, true)] {
802 let args = CommandParser::<EngineArgs>::parse_from([
803 "reth",
804 "--cpu-cores",
805 &cores.to_string(),
806 ])
807 .args;
808 assert_eq!(args.cpu_cores.unwrap().get(), cores);
809 assert_eq!(args.tree_config().use_state_root_task(), enabled);
810 }
811 }
812
813 #[test]
814 fn cpu_cores_override_preserves_state_root_fallback() {
815 let args = CommandParser::<EngineArgs>::parse_from([
816 "reth",
817 "--cpu-cores",
818 "8",
819 "--engine.state-root-fallback",
820 ])
821 .args;
822 assert!(!args.tree_config().use_state_root_task());
823 }
824
825 #[test]
826 fn test_parse_engine_args() {
827 let default_args = EngineArgs::default();
828 let args = CommandParser::<EngineArgs>::parse_from(["reth"]).args;
829 assert_eq!(args, default_args);
830 assert_eq!(args.persistence_threshold, 50);
831 assert_eq!(args.num_state_masking_blocks, 30);
832 assert_eq!(args.memory_block_buffer_target, None);
833 assert_eq!(args.memory_block_buffer_target(), 5);
834 assert_eq!(args.persistence_backpressure_threshold(), 100);
835 args.validate().unwrap();
836 let config = args.tree_config();
837 assert_eq!(config.persistence_threshold(), 50);
838 assert_eq!(config.num_state_masking_blocks(), 30);
839 assert_eq!(config.persistence_backpressure_threshold(), 100);
840 }
841
842 #[test]
843 fn txpool_prewarming_is_disabled_by_default_and_can_be_enabled() {
844 let args = CommandParser::<EngineArgs>::parse_from(["reth"]).args;
845 assert!(!args.txpool_prewarming_enabled);
846 assert!(!args.tree_config().txpool_prewarming());
847
848 let args =
849 CommandParser::<EngineArgs>::parse_from(["reth", "--engine.txpool-prewarming"]).args;
850 assert!(args.txpool_prewarming_enabled);
851 assert!(args.tree_config().txpool_prewarming());
852 }
853
854 #[test]
855 fn validate_rejects_txpool_prewarming_with_disabled_state_cache() {
856 let args = EngineArgs {
857 state_cache_disabled: true,
858 txpool_prewarming_enabled: true,
859 ..EngineArgs::default()
860 };
861
862 let err = args.validate().unwrap_err().to_string();
863 assert!(err.contains("engine.txpool-prewarming"));
864 assert!(err.contains("engine.disable-state-cache"));
865 }
866
867 #[test]
868 fn default_backpressure_threshold_uses_parsed_persistence_args() {
869 let args = CommandParser::<EngineArgs>::parse_from([
870 "reth",
871 "--engine.persistence-threshold",
872 "100",
873 "--engine.memory-block-buffer-target",
874 "50",
875 ])
876 .args;
877
878 assert_eq!(args.persistence_backpressure_threshold(), 200);
879
880 let tree_config = args.tree_config();
881 assert_eq!(tree_config.persistence_threshold(), 100);
882 assert_eq!(tree_config.memory_block_buffer_target(), 50);
883 assert_eq!(tree_config.persistence_backpressure_threshold(), 200);
884 }
885
886 #[test]
887 fn default_backpressure_threshold_uses_minimum_when_larger() {
888 let args = CommandParser::<EngineArgs>::parse_from([
889 "reth",
890 "--engine.persistence-threshold",
891 "4",
892 ])
893 .args;
894
895 assert_eq!(
896 args.persistence_backpressure_threshold(),
897 DefaultEngineValues::get_global().persistence_backpressure_threshold
898 );
899 assert_eq!(args.persistence_backpressure_threshold(), 16);
900 }
901
902 #[test]
903 fn explicit_backpressure_threshold_overrides_calculated_default() {
904 let args = CommandParser::<EngineArgs>::parse_from([
905 "reth",
906 "--engine.persistence-threshold",
907 "100",
908 "--engine.memory-block-buffer-target",
909 "50",
910 "--engine.persistence-backpressure-threshold",
911 "101",
912 ])
913 .args;
914
915 assert_eq!(args.persistence_backpressure_threshold(), 101);
916 }
917
918 #[test]
919 fn sender_recovery_cache_is_enabled_by_default_and_can_be_disabled() {
920 let args = CommandParser::<EngineArgs>::parse_from(["reth"]).args;
921 assert!(args.sender_recovery_cache_enabled);
922
923 let args =
924 CommandParser::<EngineArgs>::parse_from(["reth", "--engine.sender-recovery-cache"])
925 .args;
926 assert!(args.sender_recovery_cache_enabled);
927
928 let args = CommandParser::<EngineArgs>::parse_from([
929 "reth",
930 "--engine.sender-recovery-cache=true",
931 ])
932 .args;
933 assert!(args.sender_recovery_cache_enabled);
934
935 let args = CommandParser::<EngineArgs>::parse_from([
936 "reth",
937 "--engine.sender-recovery-cache=false",
938 ])
939 .args;
940 assert!(!args.sender_recovery_cache_enabled);
941
942 let args = CommandParser::<EngineArgs>::parse_from([
943 "reth",
944 "--engine.sender-recovery-cache",
945 "false",
946 ])
947 .args;
948 assert!(!args.sender_recovery_cache_enabled);
949 }
950
951 #[test]
952 fn engine_args() {
953 let args = EngineArgs {
954 persistence_threshold: 100,
955 persistence_backpressure_threshold: Some(101),
956 num_state_masking_blocks: DEFAULT_NUM_STATE_MASKING_BLOCKS,
957 memory_block_buffer_target: Some(50),
958 backfill_run_threshold: 100,
959 invalid_header_hit_eviction_threshold: 7,
960 state_cache_disabled: true,
961 prewarming_disabled: true,
962 txpool_prewarming_enabled: false,
964 sender_recovery_cache_enabled: true,
965 state_provider_metrics: true,
966 cross_block_cache_size: 256,
967 accept_execution_requests_hash: true,
968 multiproof_chunk_size: 512,
969 reserved_cpu_cores: 4,
970 cpu_cores: Some(NonZeroUsize::new(8).unwrap()),
971 precompile_cache_disabled: true,
972 state_root_fallback: true,
973 always_process_payload_attributes_on_canonical_head: true,
974 allow_unwind_canonical_header: true,
975 storage_worker_count: Some(16),
976 account_worker_count: Some(8),
977 prewarming_threads: Some(4),
978 cache_metrics_disabled: true,
979 slow_block_threshold: None,
980 disable_sparse_trie_cache_pruning: true,
981 state_root_task_timeout: Some(Duration::from_secs(2)),
982 share_execution_cache_with_payload_builder: false,
983 share_sparse_trie_with_payload_builder: false,
984 suppress_persistence_during_build: false,
985 bal_parallel_execution_disabled: true,
986 bal_parallel_state_root_disabled: true,
987 disable_bal_batch_io: true,
988 prewarm_reuse: true,
989 #[cfg(feature = "trie-debug")]
990 proof_jitter: None,
991 };
992
993 let parsed_args = CommandParser::<EngineArgs>::parse_from([
994 "reth",
995 "--engine.persistence-threshold",
996 "100",
997 "--engine.persistence-backpressure-threshold",
998 "101",
999 "--engine.memory-block-buffer-target",
1000 "50",
1001 "--engine.backfill-threshold",
1002 "100",
1003 "--engine.invalid-header-cache-hit-eviction-threshold",
1004 "7",
1005 "--engine.disable-state-cache",
1006 "--engine.disable-prewarming",
1007 "--engine.sender-recovery-cache",
1008 "--engine.state-provider-metrics",
1009 "--engine.cross-block-cache-size",
1010 "256",
1011 "--engine.accept-execution-requests-hash",
1012 "--engine.multiproof-chunk-size",
1013 "512",
1014 "--engine.reserved-cpu-cores",
1015 "4",
1016 "--cpu-cores",
1017 "8",
1018 "--engine.disable-precompile-cache",
1019 "--engine.state-root-fallback",
1020 "--engine.always-process-payload-attributes-on-canonical-head",
1021 "--engine.allow-unwind-canonical-header",
1022 "--engine.storage-worker-count",
1023 "16",
1024 "--engine.account-worker-count",
1025 "8",
1026 "--engine.prewarming-threads",
1027 "4",
1028 "--engine.disable-cache-metrics",
1029 "--engine.disable-sparse-trie-cache-pruning",
1030 "--engine.state-root-task-timeout",
1031 "2s",
1032 "--engine.disable-bal-parallel-execution",
1033 "--engine.disable-bal-parallel-state-root",
1034 "--engine.disable-bal-batch-io",
1035 "--engine.prewarm-reuse",
1036 ])
1037 .args;
1038
1039 assert_eq!(parsed_args, args);
1040 }
1041
1042 #[test]
1043 fn validate_rejects_invalid_backpressure_threshold() {
1044 let args = EngineArgs {
1045 persistence_threshold: 4,
1046 persistence_backpressure_threshold: Some(4),
1047 ..EngineArgs::default()
1048 };
1049
1050 let err = args.validate().unwrap_err().to_string();
1051 assert!(err.contains("engine.persistence-backpressure-threshold"));
1052 assert!(err.contains("engine.persistence-threshold"));
1053 }
1054
1055 #[test]
1056 fn validate_memory_block_buffer_target() {
1057 let args = EngineArgs {
1058 persistence_threshold: 4,
1059 num_state_masking_blocks: 0,
1060 memory_block_buffer_target: Some(4),
1061 ..EngineArgs::default()
1062 };
1063 args.validate().unwrap();
1064
1065 let args = EngineArgs { memory_block_buffer_target: Some(5), ..args };
1066 let err = args.validate().unwrap_err().to_string();
1067 assert!(err.contains("engine.memory-block-buffer-target"));
1068 assert!(err.contains("engine.persistence-threshold"));
1069 }
1070
1071 #[test]
1072 fn test_parse_backfill_threshold() {
1073 let args = CommandParser::<EngineArgs>::parse_from(["reth"]).args;
1074 let config = args.tree_config();
1075 assert_eq!(config.backfill_run_threshold(), DEFAULT_BACKFILL_RUN_THRESHOLD);
1076
1077 for (threshold, expected) in [("500", 500), ("2048", 1024)] {
1078 let args = CommandParser::<EngineArgs>::parse_from([
1079 "reth",
1080 "--engine.backfill-threshold",
1081 threshold,
1082 ])
1083 .args;
1084 assert_eq!(args.tree_config().backfill_run_threshold(), expected);
1085 }
1086 }
1087
1088 #[test]
1089 fn validate_rejects_state_masking_window_at_or_above_threshold() {
1090 let args = EngineArgs {
1091 persistence_threshold: 4,
1092 num_state_masking_blocks: 2,
1093 memory_block_buffer_target: Some(2),
1094 ..EngineArgs::default()
1095 };
1096
1097 let err = args.validate().unwrap_err().to_string();
1098 assert!(err.contains("engine.num-state-masking-blocks"));
1099 }
1100
1101 #[test]
1102 fn validate_rejects_overflowing_state_masking_window() {
1103 let args = EngineArgs {
1104 persistence_threshold: 7,
1105 num_state_masking_blocks: u64::MAX,
1106 ..EngineArgs::default()
1107 };
1108
1109 let err = args.validate().unwrap_err().to_string();
1110 assert!(err.contains("engine.num-state-masking-blocks"));
1111 }
1112
1113 #[test]
1114 fn default_memory_block_buffer_target_is_bounded_by_persistence_threshold() {
1115 let args = CommandParser::<EngineArgs>::parse_from([
1116 "reth",
1117 "--engine.persistence-threshold",
1118 "4",
1119 "--engine.num-state-masking-blocks",
1120 "0",
1121 ])
1122 .args;
1123
1124 assert_eq!(args.memory_block_buffer_target, None);
1125 assert_eq!(args.memory_block_buffer_target(), 4);
1126 assert_eq!(args.tree_config().memory_block_buffer_target(), 4);
1127 args.validate().unwrap();
1128 }
1129
1130 #[test]
1131 fn explicit_persistence_settings_can_be_lower_than_defaults() {
1132 let args = CommandParser::<EngineArgs>::parse_from([
1133 "reth",
1134 "--engine.persistence-threshold",
1135 "0",
1136 "--engine.persistence-backpressure-threshold",
1137 "1",
1138 ])
1139 .args;
1140
1141 args.validate().unwrap();
1142 let config = args.tree_config();
1143 assert_eq!(config.persistence_threshold(), 0);
1144 assert_eq!(config.num_state_masking_blocks(), 0);
1145 assert_eq!(config.memory_block_buffer_target(), 0);
1146 assert_eq!(config.persistence_backpressure_threshold(), 1);
1147 }
1148
1149 #[test]
1150 fn parse_rejects_zero_multiproof_chunk_size() {
1151 let result = CommandParser::<EngineArgs>::try_parse_from([
1152 "reth",
1153 "--engine.multiproof-chunk-size",
1154 "0",
1155 ]);
1156
1157 assert!(result.is_err());
1158 }
1159
1160 #[test]
1161 fn validate_rejects_bal_parallel_execution_without_bal_parallel_state_root() {
1162 let args = EngineArgs {
1163 bal_parallel_execution_disabled: false,
1164 bal_parallel_state_root_disabled: true,
1165 ..EngineArgs::default()
1166 };
1167
1168 let err = args.validate().unwrap_err().to_string();
1169 assert!(err.contains("engine.disable-bal-parallel-state-root"));
1170 assert!(err.contains("engine.disable-bal-parallel-execution"));
1171 }
1172
1173 #[test]
1174 fn test_parse_slow_block_threshold() {
1175 let args = CommandParser::<EngineArgs>::parse_from(["reth"]).args;
1177 assert_eq!(args.slow_block_threshold, None);
1178
1179 let args =
1181 CommandParser::<EngineArgs>::parse_from(["reth", "--engine.slow-block-threshold", "0"])
1182 .args;
1183 assert_eq!(args.slow_block_threshold, Some(Duration::ZERO));
1184
1185 let args = CommandParser::<EngineArgs>::parse_from([
1187 "reth",
1188 "--engine.slow-block-threshold",
1189 "500",
1190 ])
1191 .args;
1192 assert_eq!(args.slow_block_threshold, Some(Duration::from_secs(500)));
1193
1194 let args = CommandParser::<EngineArgs>::parse_from([
1195 "reth",
1196 "--engine.slow-block-threshold",
1197 "500ms",
1198 ])
1199 .args;
1200 assert_eq!(args.slow_block_threshold, Some(Duration::from_millis(500)));
1201 }
1202
1203 #[test]
1204 fn test_parse_invalid_header_hit_eviction_threshold() {
1205 let args = CommandParser::<EngineArgs>::parse_from(["reth"]).args;
1206 assert_eq!(
1207 args.invalid_header_hit_eviction_threshold,
1208 DEFAULT_INVALID_HEADER_HIT_EVICTION_THRESHOLD
1209 );
1210 assert_eq!(
1211 args.tree_config().invalid_header_hit_eviction_threshold(),
1212 DEFAULT_INVALID_HEADER_HIT_EVICTION_THRESHOLD
1213 );
1214
1215 let args = CommandParser::<EngineArgs>::parse_from([
1216 "reth",
1217 "--engine.invalid-header-cache-hit-eviction-threshold",
1218 "0",
1219 ])
1220 .args;
1221 assert_eq!(args.invalid_header_hit_eviction_threshold, 0);
1222 assert_eq!(args.tree_config().invalid_header_hit_eviction_threshold(), 0);
1223 }
1224
1225 #[test]
1226 fn test_parse_share_sparse_trie_flag() {
1227 let args = CommandParser::<EngineArgs>::parse_from(["reth"]).args;
1228 assert!(!args.share_sparse_trie_with_payload_builder);
1229 assert!(!args.tree_config().share_sparse_trie_with_payload_builder());
1230
1231 let args = CommandParser::<EngineArgs>::parse_from([
1232 "reth",
1233 "--engine.share-sparse-trie-with-payload-builder",
1234 ])
1235 .args;
1236 assert!(args.share_sparse_trie_with_payload_builder);
1237 assert!(args.tree_config().share_sparse_trie_with_payload_builder());
1238 }
1239}