reth_engine_tree/tree/
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/// Triggers backup when the number of canonical blocks persisted exceeds this threshold.
7pub const DEFAULT_BACKUP_THRESHOLD: u64 = 10;
8
9/// How close to the canonical head we persist blocks.
10pub const DEFAULT_MEMORY_BLOCK_BUFFER_TARGET: u64 = 2;
11
12const DEFAULT_BLOCK_BUFFER_LIMIT: u32 = 256;
13const DEFAULT_MAX_INVALID_HEADER_CACHE_LENGTH: u32 = 256;
14
15const DEFAULT_MAX_EXECUTE_BLOCK_BATCH_SIZE: usize = 4;
16
17/// The configuration of the engine tree.
18#[derive(Debug)]
19pub struct TreeConfig {
20    /// Maximum number of blocks to be kept only in memory without triggering
21    /// persistence.
22    persistence_threshold: u64,
23    /// How close to the canonical head we persist blocks. Represents the ideal
24    /// number of most recent blocks to keep in memory for quick access and reorgs.
25    ///
26    /// Note: this should be less than or equal to `persistence_threshold`.
27    memory_block_buffer_target: u64,
28    /// Number of pending blocks that cannot be executed due to missing parent and
29    /// are kept in cache.
30    block_buffer_limit: u32,
31    /// Number of invalid headers to keep in cache.
32    max_invalid_header_cache_length: u32,
33    /// Maximum number of blocks to execute sequentially in a batch.
34    ///
35    /// This is used as a cutoff to prevent long-running sequential block execution when we receive
36    /// a batch of downloaded blocks.
37    max_execute_block_batch_size: usize,
38    /// Maximum number of blocks to be persisted without triggering a backup
39    backup_threshold: u64,
40}
41
42impl Default for TreeConfig {
43    fn default() -> Self {
44        Self {
45            persistence_threshold: DEFAULT_PERSISTENCE_THRESHOLD,
46            memory_block_buffer_target: DEFAULT_MEMORY_BLOCK_BUFFER_TARGET,
47            block_buffer_limit: DEFAULT_BLOCK_BUFFER_LIMIT,
48            max_invalid_header_cache_length: DEFAULT_MAX_INVALID_HEADER_CACHE_LENGTH,
49            max_execute_block_batch_size: DEFAULT_MAX_EXECUTE_BLOCK_BATCH_SIZE,
50            backup_threshold: DEFAULT_BACKUP_THRESHOLD,
51        }
52    }
53}
54
55impl TreeConfig {
56    /// Create engine tree configuration.
57    pub const fn new(
58        persistence_threshold: u64,
59        memory_block_buffer_target: u64,
60        block_buffer_limit: u32,
61        max_invalid_header_cache_length: u32,
62        max_execute_block_batch_size: usize,
63        backup_threshold: u64,
64    ) -> Self {
65        Self {
66            persistence_threshold,
67            memory_block_buffer_target,
68            block_buffer_limit,
69            max_invalid_header_cache_length,
70            max_execute_block_batch_size,
71            backup_threshold,
72        }
73    }
74
75    /// Return the persistence threshold.
76    pub const fn persistence_threshold(&self) -> u64 {
77        self.persistence_threshold
78    }
79
80    /// Return the memory block buffer target.
81    pub const fn memory_block_buffer_target(&self) -> u64 {
82        self.memory_block_buffer_target
83    }
84
85    /// Return the block buffer limit.
86    pub const fn block_buffer_limit(&self) -> u32 {
87        self.block_buffer_limit
88    }
89
90    /// Return the maximum invalid cache header length.
91    pub const fn max_invalid_header_cache_length(&self) -> u32 {
92        self.max_invalid_header_cache_length
93    }
94
95    /// Return the maximum execute block batch size.
96    pub const fn max_execute_block_batch_size(&self) -> usize {
97        self.max_execute_block_batch_size
98    }
99
100    /// Return the backup threshold.
101    pub const fn backup_threshold(&self) -> u64 {
102        self.backup_threshold
103    }
104
105    /// Setter for persistence threshold.
106    pub const fn with_persistence_threshold(mut self, persistence_threshold: u64) -> Self {
107        self.persistence_threshold = persistence_threshold;
108        self
109    }
110
111    /// Setter for memory block buffer target.
112    pub const fn with_memory_block_buffer_target(
113        mut self,
114        memory_block_buffer_target: u64,
115    ) -> Self {
116        self.memory_block_buffer_target = memory_block_buffer_target;
117        self
118    }
119
120    /// Setter for block buffer limit.
121    pub const fn with_block_buffer_limit(mut self, block_buffer_limit: u32) -> Self {
122        self.block_buffer_limit = block_buffer_limit;
123        self
124    }
125
126    /// Setter for maximum invalid header cache length.
127    pub const fn with_max_invalid_header_cache_length(
128        mut self,
129        max_invalid_header_cache_length: u32,
130    ) -> Self {
131        self.max_invalid_header_cache_length = max_invalid_header_cache_length;
132        self
133    }
134
135    /// Setter for maximum execute block batch size.
136    pub const fn with_max_execute_block_batch_size(
137        mut self,
138        max_execute_block_batch_size: usize,
139    ) -> Self {
140        self.max_execute_block_batch_size = max_execute_block_batch_size;
141        self
142    }
143
144    /// Setter for backup threshold.
145    pub const fn with_backup_threshold(mut self, backup_threshold: u64) -> Self {
146        self.backup_threshold = backup_threshold;
147        self
148    }
149}