1pub const DEFAULT_PERSISTENCE_THRESHOLD: u64 = 2;
5
6pub const DEFAULT_MEMORY_BLOCK_BUFFER_TARGET: u64 = 0;
8
9pub const DEFAULT_MAX_PROOF_TASK_CONCURRENCY: u64 = 256;
11
12pub 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
22pub 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#[derive(Debug, Clone)]
41pub struct TreeConfig {
42 persistence_threshold: u64,
45 memory_block_buffer_target: u64,
50 block_buffer_limit: u32,
53 max_invalid_header_cache_length: u32,
55 max_execute_block_batch_size: usize,
60 legacy_state_root: bool,
63 always_compare_trie_updates: bool,
66 disable_caching_and_prewarming: bool,
68 disable_parallel_sparse_trie: bool,
70 state_provider_metrics: bool,
72 cross_block_cache_size: u64,
74 has_enough_parallelism: bool,
76 max_proof_task_concurrency: u64,
78 reserved_cpu_cores: usize,
80 precompile_cache_disabled: bool,
82 state_root_fallback: bool,
84 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 #[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 pub const fn persistence_threshold(&self) -> u64 {
169 self.persistence_threshold
170 }
171
172 pub const fn memory_block_buffer_target(&self) -> u64 {
174 self.memory_block_buffer_target
175 }
176
177 pub const fn block_buffer_limit(&self) -> u32 {
179 self.block_buffer_limit
180 }
181
182 pub const fn max_invalid_header_cache_length(&self) -> u32 {
184 self.max_invalid_header_cache_length
185 }
186
187 pub const fn max_execute_block_batch_size(&self) -> usize {
189 self.max_execute_block_batch_size
190 }
191
192 pub const fn max_proof_task_concurrency(&self) -> u64 {
194 self.max_proof_task_concurrency
195 }
196
197 pub const fn reserved_cpu_cores(&self) -> usize {
199 self.reserved_cpu_cores
200 }
201
202 pub const fn legacy_state_root(&self) -> bool {
205 self.legacy_state_root
206 }
207
208 pub const fn state_provider_metrics(&self) -> bool {
210 self.state_provider_metrics
211 }
212
213 pub const fn disable_parallel_sparse_trie(&self) -> bool {
215 self.disable_parallel_sparse_trie
216 }
217
218 pub const fn disable_caching_and_prewarming(&self) -> bool {
220 self.disable_caching_and_prewarming
221 }
222
223 pub const fn always_compare_trie_updates(&self) -> bool {
226 self.always_compare_trie_updates
227 }
228
229 pub const fn cross_block_cache_size(&self) -> u64 {
231 self.cross_block_cache_size
232 }
233
234 pub const fn precompile_cache_disabled(&self) -> bool {
236 self.precompile_cache_disabled
237 }
238
239 pub const fn state_root_fallback(&self) -> bool {
241 self.state_root_fallback
242 }
243
244 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 pub const fn always_process_payload_attributes_on_canonical_head(&self) -> bool {
257 self.always_process_payload_attributes_on_canonical_head
258 }
259
260 pub const fn with_persistence_threshold(mut self, persistence_threshold: u64) -> Self {
262 self.persistence_threshold = persistence_threshold;
263 self
264 }
265
266 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 pub const fn use_state_root_task(&self) -> bool {
380 self.has_enough_parallelism && !self.legacy_state_root
381 }
382}