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 allow_unwind_canonical_header: bool,
100}
101
102impl Default for TreeConfig {
103 fn default() -> Self {
104 Self {
105 persistence_threshold: DEFAULT_PERSISTENCE_THRESHOLD,
106 memory_block_buffer_target: DEFAULT_MEMORY_BLOCK_BUFFER_TARGET,
107 block_buffer_limit: DEFAULT_BLOCK_BUFFER_LIMIT,
108 max_invalid_header_cache_length: DEFAULT_MAX_INVALID_HEADER_CACHE_LENGTH,
109 max_execute_block_batch_size: DEFAULT_MAX_EXECUTE_BLOCK_BATCH_SIZE,
110 legacy_state_root: false,
111 always_compare_trie_updates: false,
112 disable_caching_and_prewarming: false,
113 disable_parallel_sparse_trie: false,
114 state_provider_metrics: false,
115 cross_block_cache_size: DEFAULT_CROSS_BLOCK_CACHE_SIZE,
116 has_enough_parallelism: has_enough_parallelism(),
117 max_proof_task_concurrency: DEFAULT_MAX_PROOF_TASK_CONCURRENCY,
118 reserved_cpu_cores: DEFAULT_RESERVED_CPU_CORES,
119 precompile_cache_disabled: false,
120 state_root_fallback: false,
121 always_process_payload_attributes_on_canonical_head: false,
122 allow_unwind_canonical_header: false,
123 }
124 }
125}
126
127impl TreeConfig {
128 #[expect(clippy::too_many_arguments)]
130 pub const fn new(
131 persistence_threshold: u64,
132 memory_block_buffer_target: u64,
133 block_buffer_limit: u32,
134 max_invalid_header_cache_length: u32,
135 max_execute_block_batch_size: usize,
136 legacy_state_root: bool,
137 always_compare_trie_updates: bool,
138 disable_caching_and_prewarming: bool,
139 disable_parallel_sparse_trie: bool,
140 state_provider_metrics: bool,
141 cross_block_cache_size: u64,
142 has_enough_parallelism: bool,
143 max_proof_task_concurrency: u64,
144 reserved_cpu_cores: usize,
145 precompile_cache_disabled: bool,
146 state_root_fallback: bool,
147 always_process_payload_attributes_on_canonical_head: bool,
148 allow_unwind_canonical_header: bool,
149 ) -> Self {
150 Self {
151 persistence_threshold,
152 memory_block_buffer_target,
153 block_buffer_limit,
154 max_invalid_header_cache_length,
155 max_execute_block_batch_size,
156 legacy_state_root,
157 always_compare_trie_updates,
158 disable_caching_and_prewarming,
159 disable_parallel_sparse_trie,
160 state_provider_metrics,
161 cross_block_cache_size,
162 has_enough_parallelism,
163 max_proof_task_concurrency,
164 reserved_cpu_cores,
165 precompile_cache_disabled,
166 state_root_fallback,
167 always_process_payload_attributes_on_canonical_head,
168 allow_unwind_canonical_header,
169 }
170 }
171
172 pub const fn persistence_threshold(&self) -> u64 {
174 self.persistence_threshold
175 }
176
177 pub const fn memory_block_buffer_target(&self) -> u64 {
179 self.memory_block_buffer_target
180 }
181
182 pub const fn block_buffer_limit(&self) -> u32 {
184 self.block_buffer_limit
185 }
186
187 pub const fn max_invalid_header_cache_length(&self) -> u32 {
189 self.max_invalid_header_cache_length
190 }
191
192 pub const fn max_execute_block_batch_size(&self) -> usize {
194 self.max_execute_block_batch_size
195 }
196
197 pub const fn max_proof_task_concurrency(&self) -> u64 {
199 self.max_proof_task_concurrency
200 }
201
202 pub const fn reserved_cpu_cores(&self) -> usize {
204 self.reserved_cpu_cores
205 }
206
207 pub const fn legacy_state_root(&self) -> bool {
210 self.legacy_state_root
211 }
212
213 pub const fn state_provider_metrics(&self) -> bool {
215 self.state_provider_metrics
216 }
217
218 pub const fn disable_parallel_sparse_trie(&self) -> bool {
220 self.disable_parallel_sparse_trie
221 }
222
223 pub const fn disable_caching_and_prewarming(&self) -> bool {
225 self.disable_caching_and_prewarming
226 }
227
228 pub const fn always_compare_trie_updates(&self) -> bool {
231 self.always_compare_trie_updates
232 }
233
234 pub const fn cross_block_cache_size(&self) -> u64 {
236 self.cross_block_cache_size
237 }
238
239 pub const fn precompile_cache_disabled(&self) -> bool {
241 self.precompile_cache_disabled
242 }
243
244 pub const fn state_root_fallback(&self) -> bool {
246 self.state_root_fallback
247 }
248
249 pub const fn with_always_process_payload_attributes_on_canonical_head(
251 mut self,
252 always_process_payload_attributes_on_canonical_head: bool,
253 ) -> Self {
254 self.always_process_payload_attributes_on_canonical_head =
255 always_process_payload_attributes_on_canonical_head;
256 self
257 }
258
259 pub const fn always_process_payload_attributes_on_canonical_head(&self) -> bool {
262 self.always_process_payload_attributes_on_canonical_head
263 }
264
265 pub const fn unwind_canonical_header(&self) -> bool {
267 self.allow_unwind_canonical_header
268 }
269
270 pub const fn with_persistence_threshold(mut self, persistence_threshold: u64) -> Self {
272 self.persistence_threshold = persistence_threshold;
273 self
274 }
275
276 pub const fn with_memory_block_buffer_target(
278 mut self,
279 memory_block_buffer_target: u64,
280 ) -> Self {
281 self.memory_block_buffer_target = memory_block_buffer_target;
282 self
283 }
284
285 pub const fn with_block_buffer_limit(mut self, block_buffer_limit: u32) -> Self {
287 self.block_buffer_limit = block_buffer_limit;
288 self
289 }
290
291 pub const fn with_max_invalid_header_cache_length(
293 mut self,
294 max_invalid_header_cache_length: u32,
295 ) -> Self {
296 self.max_invalid_header_cache_length = max_invalid_header_cache_length;
297 self
298 }
299
300 pub const fn with_max_execute_block_batch_size(
302 mut self,
303 max_execute_block_batch_size: usize,
304 ) -> Self {
305 self.max_execute_block_batch_size = max_execute_block_batch_size;
306 self
307 }
308
309 pub const fn with_legacy_state_root(mut self, legacy_state_root: bool) -> Self {
311 self.legacy_state_root = legacy_state_root;
312 self
313 }
314
315 pub const fn without_caching_and_prewarming(
317 mut self,
318 disable_caching_and_prewarming: bool,
319 ) -> Self {
320 self.disable_caching_and_prewarming = disable_caching_and_prewarming;
321 self
322 }
323
324 pub const fn with_always_compare_trie_updates(
327 mut self,
328 always_compare_trie_updates: bool,
329 ) -> Self {
330 self.always_compare_trie_updates = always_compare_trie_updates;
331 self
332 }
333
334 pub const fn with_cross_block_cache_size(mut self, cross_block_cache_size: u64) -> Self {
336 self.cross_block_cache_size = cross_block_cache_size;
337 self
338 }
339
340 pub const fn with_has_enough_parallelism(mut self, has_enough_parallelism: bool) -> Self {
342 self.has_enough_parallelism = has_enough_parallelism;
343 self
344 }
345
346 pub const fn with_state_provider_metrics(mut self, state_provider_metrics: bool) -> Self {
348 self.state_provider_metrics = state_provider_metrics;
349 self
350 }
351
352 pub const fn with_disable_parallel_sparse_trie(
354 mut self,
355 disable_parallel_sparse_trie: bool,
356 ) -> Self {
357 self.disable_parallel_sparse_trie = disable_parallel_sparse_trie;
358 self
359 }
360
361 pub const fn with_max_proof_task_concurrency(
363 mut self,
364 max_proof_task_concurrency: u64,
365 ) -> Self {
366 self.max_proof_task_concurrency = max_proof_task_concurrency;
367 self
368 }
369
370 pub const fn with_reserved_cpu_cores(mut self, reserved_cpu_cores: usize) -> Self {
372 self.reserved_cpu_cores = reserved_cpu_cores;
373 self
374 }
375
376 pub const fn without_precompile_cache(mut self, precompile_cache_disabled: bool) -> Self {
378 self.precompile_cache_disabled = precompile_cache_disabled;
379 self
380 }
381
382 pub const fn with_state_root_fallback(mut self, state_root_fallback: bool) -> Self {
384 self.state_root_fallback = state_root_fallback;
385 self
386 }
387
388 pub const fn with_unwind_canonical_header(mut self, unwind_canonical_header: bool) -> Self {
390 self.allow_unwind_canonical_header = unwind_canonical_header;
391 self
392 }
393
394 pub const fn use_state_root_task(&self) -> bool {
396 self.has_enough_parallelism && !self.legacy_state_root
397 }
398}