reth_engine_primitives/
config.rs

1//! Engine tree configuration.
2
3/// Triggers persistence when the number of canonical blocks in memory exceeds this threshold.
4pub const DEFAULT_PERSISTENCE_THRESHOLD: u64 = 2;
5
6/// How close to the canonical head we persist blocks.
7pub const DEFAULT_MEMORY_BLOCK_BUFFER_TARGET: u64 = 2;
8
9/// Default maximum concurrency for proof tasks
10pub const DEFAULT_MAX_PROOF_TASK_CONCURRENCY: u64 = 256;
11
12/// Default number of reserved CPU cores for non-reth processes.
13///
14/// This will be deducated from the thread count of main reth global threadpool.
15pub const DEFAULT_RESERVED_CPU_CORES: usize = 1;
16
17const DEFAULT_BLOCK_BUFFER_LIMIT: u32 = 256;
18const DEFAULT_MAX_INVALID_HEADER_CACHE_LENGTH: u32 = 256;
19const DEFAULT_MAX_EXECUTE_BLOCK_BATCH_SIZE: usize = 4;
20const DEFAULT_CROSS_BLOCK_CACHE_SIZE: u64 = 4 * 1024 * 1024 * 1024;
21
22/// Determines if the host has enough parallelism to run the payload processor.
23///
24/// It requires at least 5 parallel threads:
25/// - Engine in main thread that spawns the state root task.
26/// - Multiproof task in payload processor
27/// - Sparse Trie task in payload processor
28/// - Multiproof computation spawned in payload processor
29/// - Storage root computation spawned in trie parallel proof
30pub fn has_enough_parallelism() -> bool {
31    #[cfg(feature = "std")]
32    {
33        std::thread::available_parallelism().is_ok_and(|num| num.get() >= 5)
34    }
35    #[cfg(not(feature = "std"))]
36    false
37}
38
39/// The configuration of the engine tree.
40#[derive(Debug)]
41pub struct TreeConfig {
42    /// Maximum number of blocks to be kept only in memory without triggering
43    /// persistence.
44    persistence_threshold: u64,
45    /// How close to the canonical head we persist blocks. Represents the ideal
46    /// number of most recent blocks to keep in memory for quick access and reorgs.
47    ///
48    /// Note: this should be less than or equal to `persistence_threshold`.
49    memory_block_buffer_target: u64,
50    /// Number of pending blocks that cannot be executed due to missing parent and
51    /// are kept in cache.
52    block_buffer_limit: u32,
53    /// Number of invalid headers to keep in cache.
54    max_invalid_header_cache_length: u32,
55    /// Maximum number of blocks to execute sequentially in a batch.
56    ///
57    /// This is used as a cutoff to prevent long-running sequential block execution when we receive
58    /// a batch of downloaded blocks.
59    max_execute_block_batch_size: usize,
60    /// Whether to use the legacy state root calculation method instead of the
61    /// new state root task.
62    legacy_state_root: bool,
63    /// Whether to always compare trie updates from the state root task to the trie updates from
64    /// the regular state root calculation.
65    always_compare_trie_updates: bool,
66    /// Whether to disable cross-block caching and parallel prewarming.
67    disable_caching_and_prewarming: bool,
68    /// Cross-block cache size in bytes.
69    cross_block_cache_size: u64,
70    /// Whether the host has enough parallelism to run state root task.
71    has_enough_parallelism: bool,
72    /// Maximum number of concurrent proof tasks
73    max_proof_task_concurrency: u64,
74    /// Number of reserved CPU cores for non-reth processes
75    reserved_cpu_cores: usize,
76}
77
78impl Default for TreeConfig {
79    fn default() -> Self {
80        Self {
81            persistence_threshold: DEFAULT_PERSISTENCE_THRESHOLD,
82            memory_block_buffer_target: DEFAULT_MEMORY_BLOCK_BUFFER_TARGET,
83            block_buffer_limit: DEFAULT_BLOCK_BUFFER_LIMIT,
84            max_invalid_header_cache_length: DEFAULT_MAX_INVALID_HEADER_CACHE_LENGTH,
85            max_execute_block_batch_size: DEFAULT_MAX_EXECUTE_BLOCK_BATCH_SIZE,
86            legacy_state_root: false,
87            always_compare_trie_updates: false,
88            disable_caching_and_prewarming: false,
89            cross_block_cache_size: DEFAULT_CROSS_BLOCK_CACHE_SIZE,
90            has_enough_parallelism: has_enough_parallelism(),
91            max_proof_task_concurrency: DEFAULT_MAX_PROOF_TASK_CONCURRENCY,
92            reserved_cpu_cores: DEFAULT_RESERVED_CPU_CORES,
93        }
94    }
95}
96
97impl TreeConfig {
98    /// Create engine tree configuration.
99    #[expect(clippy::too_many_arguments)]
100    pub const fn new(
101        persistence_threshold: u64,
102        memory_block_buffer_target: u64,
103        block_buffer_limit: u32,
104        max_invalid_header_cache_length: u32,
105        max_execute_block_batch_size: usize,
106        legacy_state_root: bool,
107        always_compare_trie_updates: bool,
108        disable_caching_and_prewarming: bool,
109        cross_block_cache_size: u64,
110        has_enough_parallelism: bool,
111        max_proof_task_concurrency: u64,
112        reserved_cpu_cores: usize,
113    ) -> Self {
114        Self {
115            persistence_threshold,
116            memory_block_buffer_target,
117            block_buffer_limit,
118            max_invalid_header_cache_length,
119            max_execute_block_batch_size,
120            legacy_state_root,
121            always_compare_trie_updates,
122            disable_caching_and_prewarming,
123            cross_block_cache_size,
124            has_enough_parallelism,
125            max_proof_task_concurrency,
126            reserved_cpu_cores,
127        }
128    }
129
130    /// Return the persistence threshold.
131    pub const fn persistence_threshold(&self) -> u64 {
132        self.persistence_threshold
133    }
134
135    /// Return the memory block buffer target.
136    pub const fn memory_block_buffer_target(&self) -> u64 {
137        self.memory_block_buffer_target
138    }
139
140    /// Return the block buffer limit.
141    pub const fn block_buffer_limit(&self) -> u32 {
142        self.block_buffer_limit
143    }
144
145    /// Return the maximum invalid cache header length.
146    pub const fn max_invalid_header_cache_length(&self) -> u32 {
147        self.max_invalid_header_cache_length
148    }
149
150    /// Return the maximum execute block batch size.
151    pub const fn max_execute_block_batch_size(&self) -> usize {
152        self.max_execute_block_batch_size
153    }
154
155    /// Return the maximum proof task concurrency.
156    pub const fn max_proof_task_concurrency(&self) -> u64 {
157        self.max_proof_task_concurrency
158    }
159
160    /// Return the number of reserved CPU cores for non-reth processes
161    pub const fn reserved_cpu_cores(&self) -> usize {
162        self.reserved_cpu_cores
163    }
164
165    /// Returns whether to use the legacy state root calculation method instead
166    /// of the new state root task
167    pub const fn legacy_state_root(&self) -> bool {
168        self.legacy_state_root
169    }
170
171    /// Returns whether or not cross-block caching and parallel prewarming should be used.
172    pub const fn disable_caching_and_prewarming(&self) -> bool {
173        self.disable_caching_and_prewarming
174    }
175
176    /// Returns whether to always compare trie updates from the state root task to the trie updates
177    /// from the regular state root calculation.
178    pub const fn always_compare_trie_updates(&self) -> bool {
179        self.always_compare_trie_updates
180    }
181
182    /// Return the cross-block cache size.
183    pub const fn cross_block_cache_size(&self) -> u64 {
184        self.cross_block_cache_size
185    }
186
187    /// Setter for persistence threshold.
188    pub const fn with_persistence_threshold(mut self, persistence_threshold: u64) -> Self {
189        self.persistence_threshold = persistence_threshold;
190        self
191    }
192
193    /// Setter for memory block buffer target.
194    pub const fn with_memory_block_buffer_target(
195        mut self,
196        memory_block_buffer_target: u64,
197    ) -> Self {
198        self.memory_block_buffer_target = memory_block_buffer_target;
199        self
200    }
201
202    /// Setter for block buffer limit.
203    pub const fn with_block_buffer_limit(mut self, block_buffer_limit: u32) -> Self {
204        self.block_buffer_limit = block_buffer_limit;
205        self
206    }
207
208    /// Setter for maximum invalid header cache length.
209    pub const fn with_max_invalid_header_cache_length(
210        mut self,
211        max_invalid_header_cache_length: u32,
212    ) -> Self {
213        self.max_invalid_header_cache_length = max_invalid_header_cache_length;
214        self
215    }
216
217    /// Setter for maximum execute block batch size.
218    pub const fn with_max_execute_block_batch_size(
219        mut self,
220        max_execute_block_batch_size: usize,
221    ) -> Self {
222        self.max_execute_block_batch_size = max_execute_block_batch_size;
223        self
224    }
225
226    /// Setter for whether to use the legacy state root calculation method.
227    pub const fn with_legacy_state_root(mut self, legacy_state_root: bool) -> Self {
228        self.legacy_state_root = legacy_state_root;
229        self
230    }
231
232    /// Setter for whether to disable cross-block caching and parallel prewarming.
233    pub const fn without_caching_and_prewarming(
234        mut self,
235        disable_caching_and_prewarming: bool,
236    ) -> Self {
237        self.disable_caching_and_prewarming = disable_caching_and_prewarming;
238        self
239    }
240
241    /// Setter for whether to always compare trie updates from the state root task to the trie
242    /// updates from the regular state root calculation.
243    pub const fn with_always_compare_trie_updates(
244        mut self,
245        always_compare_trie_updates: bool,
246    ) -> Self {
247        self.always_compare_trie_updates = always_compare_trie_updates;
248        self
249    }
250
251    /// Setter for cross block cache size.
252    pub const fn with_cross_block_cache_size(mut self, cross_block_cache_size: u64) -> Self {
253        self.cross_block_cache_size = cross_block_cache_size;
254        self
255    }
256
257    /// Setter for has enough parallelism.
258    pub const fn with_has_enough_parallelism(mut self, has_enough_parallelism: bool) -> Self {
259        self.has_enough_parallelism = has_enough_parallelism;
260        self
261    }
262
263    /// Setter for maximum number of concurrent proof tasks.
264    pub const fn with_max_proof_task_concurrency(
265        mut self,
266        max_proof_task_concurrency: u64,
267    ) -> Self {
268        self.max_proof_task_concurrency = max_proof_task_concurrency;
269        self
270    }
271
272    /// Setter for the number of reserved CPU cores for any non-reth processes
273    pub const fn with_reserved_cpu_cores(mut self, reserved_cpu_cores: usize) -> Self {
274        self.reserved_cpu_cores = reserved_cpu_cores;
275        self
276    }
277
278    /// Whether or not to use state root task
279    pub const fn use_state_root_task(&self) -> bool {
280        self.has_enough_parallelism && !self.legacy_state_root
281    }
282}