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 = 0;
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 deducted 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, Clone)]
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    /// Whether to disable the parallel sparse trie state root algorithm.
69    disable_parallel_sparse_trie: bool,
70    /// Whether to enable state provider metrics.
71    state_provider_metrics: bool,
72    /// Cross-block cache size in bytes.
73    cross_block_cache_size: u64,
74    /// Whether the host has enough parallelism to run state root task.
75    has_enough_parallelism: bool,
76    /// Maximum number of concurrent proof tasks
77    max_proof_task_concurrency: u64,
78    /// Number of reserved CPU cores for non-reth processes
79    reserved_cpu_cores: usize,
80    /// Whether to disable the precompile cache
81    precompile_cache_disabled: bool,
82    /// Whether to use state root fallback for testing
83    state_root_fallback: bool,
84    /// Whether to always process payload attributes and begin a payload build process
85    /// even if `forkchoiceState.headBlockHash` is already the canonical head or an ancestor.
86    ///
87    /// The Engine API specification generally states that client software "MUST NOT begin a
88    /// payload build process if `forkchoiceState.headBlockHash` references a `VALID`
89    /// ancestor of the head of canonical chain".
90    /// See: <https://github.com/ethereum/execution-apis/blob/main/src/engine/paris.md#engine_forkchoiceupdatedv1> (Rule 2)
91    ///
92    /// This flag allows overriding that behavior.
93    /// This is useful for specific chain configurations (e.g., OP Stack where proposers
94    /// can reorg their own chain), various custom chains, or for development/testing purposes
95    /// where immediate payload regeneration is desired despite the head not changing or moving to
96    /// an ancestor.
97    always_process_payload_attributes_on_canonical_head: bool,
98}
99
100impl Default for TreeConfig {
101    fn default() -> Self {
102        Self {
103            persistence_threshold: DEFAULT_PERSISTENCE_THRESHOLD,
104            memory_block_buffer_target: DEFAULT_MEMORY_BLOCK_BUFFER_TARGET,
105            block_buffer_limit: DEFAULT_BLOCK_BUFFER_LIMIT,
106            max_invalid_header_cache_length: DEFAULT_MAX_INVALID_HEADER_CACHE_LENGTH,
107            max_execute_block_batch_size: DEFAULT_MAX_EXECUTE_BLOCK_BATCH_SIZE,
108            legacy_state_root: false,
109            always_compare_trie_updates: false,
110            disable_caching_and_prewarming: false,
111            disable_parallel_sparse_trie: false,
112            state_provider_metrics: false,
113            cross_block_cache_size: DEFAULT_CROSS_BLOCK_CACHE_SIZE,
114            has_enough_parallelism: has_enough_parallelism(),
115            max_proof_task_concurrency: DEFAULT_MAX_PROOF_TASK_CONCURRENCY,
116            reserved_cpu_cores: DEFAULT_RESERVED_CPU_CORES,
117            precompile_cache_disabled: false,
118            state_root_fallback: false,
119            always_process_payload_attributes_on_canonical_head: false,
120        }
121    }
122}
123
124impl TreeConfig {
125    /// Create engine tree configuration.
126    #[expect(clippy::too_many_arguments)]
127    pub const fn new(
128        persistence_threshold: u64,
129        memory_block_buffer_target: u64,
130        block_buffer_limit: u32,
131        max_invalid_header_cache_length: u32,
132        max_execute_block_batch_size: usize,
133        legacy_state_root: bool,
134        always_compare_trie_updates: bool,
135        disable_caching_and_prewarming: bool,
136        disable_parallel_sparse_trie: bool,
137        state_provider_metrics: bool,
138        cross_block_cache_size: u64,
139        has_enough_parallelism: bool,
140        max_proof_task_concurrency: u64,
141        reserved_cpu_cores: usize,
142        precompile_cache_disabled: bool,
143        state_root_fallback: bool,
144        always_process_payload_attributes_on_canonical_head: bool,
145    ) -> Self {
146        Self {
147            persistence_threshold,
148            memory_block_buffer_target,
149            block_buffer_limit,
150            max_invalid_header_cache_length,
151            max_execute_block_batch_size,
152            legacy_state_root,
153            always_compare_trie_updates,
154            disable_caching_and_prewarming,
155            disable_parallel_sparse_trie,
156            state_provider_metrics,
157            cross_block_cache_size,
158            has_enough_parallelism,
159            max_proof_task_concurrency,
160            reserved_cpu_cores,
161            precompile_cache_disabled,
162            state_root_fallback,
163            always_process_payload_attributes_on_canonical_head,
164        }
165    }
166
167    /// Return the persistence threshold.
168    pub const fn persistence_threshold(&self) -> u64 {
169        self.persistence_threshold
170    }
171
172    /// Return the memory block buffer target.
173    pub const fn memory_block_buffer_target(&self) -> u64 {
174        self.memory_block_buffer_target
175    }
176
177    /// Return the block buffer limit.
178    pub const fn block_buffer_limit(&self) -> u32 {
179        self.block_buffer_limit
180    }
181
182    /// Return the maximum invalid cache header length.
183    pub const fn max_invalid_header_cache_length(&self) -> u32 {
184        self.max_invalid_header_cache_length
185    }
186
187    /// Return the maximum execute block batch size.
188    pub const fn max_execute_block_batch_size(&self) -> usize {
189        self.max_execute_block_batch_size
190    }
191
192    /// Return the maximum proof task concurrency.
193    pub const fn max_proof_task_concurrency(&self) -> u64 {
194        self.max_proof_task_concurrency
195    }
196
197    /// Return the number of reserved CPU cores for non-reth processes
198    pub const fn reserved_cpu_cores(&self) -> usize {
199        self.reserved_cpu_cores
200    }
201
202    /// Returns whether to use the legacy state root calculation method instead
203    /// of the new state root task
204    pub const fn legacy_state_root(&self) -> bool {
205        self.legacy_state_root
206    }
207
208    /// Returns whether or not state provider metrics are enabled.
209    pub const fn state_provider_metrics(&self) -> bool {
210        self.state_provider_metrics
211    }
212
213    /// Returns whether or not the parallel sparse trie is disabled.
214    pub const fn disable_parallel_sparse_trie(&self) -> bool {
215        self.disable_parallel_sparse_trie
216    }
217
218    /// Returns whether or not cross-block caching and parallel prewarming should be used.
219    pub const fn disable_caching_and_prewarming(&self) -> bool {
220        self.disable_caching_and_prewarming
221    }
222
223    /// Returns whether to always compare trie updates from the state root task to the trie updates
224    /// from the regular state root calculation.
225    pub const fn always_compare_trie_updates(&self) -> bool {
226        self.always_compare_trie_updates
227    }
228
229    /// Returns the cross-block cache size.
230    pub const fn cross_block_cache_size(&self) -> u64 {
231        self.cross_block_cache_size
232    }
233
234    /// Returns whether precompile cache is disabled.
235    pub const fn precompile_cache_disabled(&self) -> bool {
236        self.precompile_cache_disabled
237    }
238
239    /// Returns whether to use state root fallback.
240    pub const fn state_root_fallback(&self) -> bool {
241        self.state_root_fallback
242    }
243
244    /// Sets whether to always process payload attributes when the FCU head is already canonical.
245    pub const fn with_always_process_payload_attributes_on_canonical_head(
246        mut self,
247        always_process_payload_attributes_on_canonical_head: bool,
248    ) -> Self {
249        self.always_process_payload_attributes_on_canonical_head =
250            always_process_payload_attributes_on_canonical_head;
251        self
252    }
253
254    /// Returns true if payload attributes should always be processed even when the FCU head is
255    /// canonical.
256    pub const fn always_process_payload_attributes_on_canonical_head(&self) -> bool {
257        self.always_process_payload_attributes_on_canonical_head
258    }
259
260    /// Setter for persistence threshold.
261    pub const fn with_persistence_threshold(mut self, persistence_threshold: u64) -> Self {
262        self.persistence_threshold = persistence_threshold;
263        self
264    }
265
266    /// Setter for memory block buffer target.
267    pub const fn with_memory_block_buffer_target(
268        mut self,
269        memory_block_buffer_target: u64,
270    ) -> Self {
271        self.memory_block_buffer_target = memory_block_buffer_target;
272        self
273    }
274
275    /// Setter for block buffer limit.
276    pub const fn with_block_buffer_limit(mut self, block_buffer_limit: u32) -> Self {
277        self.block_buffer_limit = block_buffer_limit;
278        self
279    }
280
281    /// Setter for maximum invalid header cache length.
282    pub const fn with_max_invalid_header_cache_length(
283        mut self,
284        max_invalid_header_cache_length: u32,
285    ) -> Self {
286        self.max_invalid_header_cache_length = max_invalid_header_cache_length;
287        self
288    }
289
290    /// Setter for maximum execute block batch size.
291    pub const fn with_max_execute_block_batch_size(
292        mut self,
293        max_execute_block_batch_size: usize,
294    ) -> Self {
295        self.max_execute_block_batch_size = max_execute_block_batch_size;
296        self
297    }
298
299    /// Setter for whether to use the legacy state root calculation method.
300    pub const fn with_legacy_state_root(mut self, legacy_state_root: bool) -> Self {
301        self.legacy_state_root = legacy_state_root;
302        self
303    }
304
305    /// Setter for whether to disable cross-block caching and parallel prewarming.
306    pub const fn without_caching_and_prewarming(
307        mut self,
308        disable_caching_and_prewarming: bool,
309    ) -> Self {
310        self.disable_caching_and_prewarming = disable_caching_and_prewarming;
311        self
312    }
313
314    /// Setter for whether to always compare trie updates from the state root task to the trie
315    /// updates from the regular state root calculation.
316    pub const fn with_always_compare_trie_updates(
317        mut self,
318        always_compare_trie_updates: bool,
319    ) -> Self {
320        self.always_compare_trie_updates = always_compare_trie_updates;
321        self
322    }
323
324    /// Setter for cross block cache size.
325    pub const fn with_cross_block_cache_size(mut self, cross_block_cache_size: u64) -> Self {
326        self.cross_block_cache_size = cross_block_cache_size;
327        self
328    }
329
330    /// Setter for has enough parallelism.
331    pub const fn with_has_enough_parallelism(mut self, has_enough_parallelism: bool) -> Self {
332        self.has_enough_parallelism = has_enough_parallelism;
333        self
334    }
335
336    /// Setter for state provider metrics.
337    pub const fn with_state_provider_metrics(mut self, state_provider_metrics: bool) -> Self {
338        self.state_provider_metrics = state_provider_metrics;
339        self
340    }
341
342    /// Setter for using the parallel sparse trie
343    pub const fn with_disable_parallel_sparse_trie(
344        mut self,
345        disable_parallel_sparse_trie: bool,
346    ) -> Self {
347        self.disable_parallel_sparse_trie = disable_parallel_sparse_trie;
348        self
349    }
350
351    /// Setter for maximum number of concurrent proof tasks.
352    pub const fn with_max_proof_task_concurrency(
353        mut self,
354        max_proof_task_concurrency: u64,
355    ) -> Self {
356        self.max_proof_task_concurrency = max_proof_task_concurrency;
357        self
358    }
359
360    /// Setter for the number of reserved CPU cores for any non-reth processes
361    pub const fn with_reserved_cpu_cores(mut self, reserved_cpu_cores: usize) -> Self {
362        self.reserved_cpu_cores = reserved_cpu_cores;
363        self
364    }
365
366    /// Setter for whether to disable the precompile cache.
367    pub const fn without_precompile_cache(mut self, precompile_cache_disabled: bool) -> Self {
368        self.precompile_cache_disabled = precompile_cache_disabled;
369        self
370    }
371
372    /// Setter for whether to use state root fallback, useful for testing.
373    pub const fn with_state_root_fallback(mut self, state_root_fallback: bool) -> Self {
374        self.state_root_fallback = state_root_fallback;
375        self
376    }
377
378    /// Whether or not to use state root task
379    pub const fn use_state_root_task(&self) -> bool {
380        self.has_enough_parallelism && !self.legacy_state_root
381    }
382}