Skip to main content

tycho_core/storage/
config.rs

1use std::time::Duration;
2
3use bytesize::ByteSize;
4use serde::{Deserialize, Serialize};
5use tycho_util::config::PartialConfig;
6use tycho_util::serde_helpers;
7
8#[derive(Debug, Clone, Serialize, Deserialize, PartialConfig)]
9#[serde(deny_unknown_fields, default)]
10pub struct CoreStorageConfig {
11    /// Runtime cells cache size.
12    ///
13    /// Default: 256 MB.
14    #[important]
15    pub cells_cache_size: ByteSize,
16
17    /// Minimal epoch interval when the state can be reused.
18    ///
19    /// Default: 3.
20    pub drop_interval: u32,
21
22    /// Whether to pack blocks into archives.
23    ///
24    /// Default: `true`.
25    pub store_archives: bool,
26
27    /// Archives storage config.
28    ///
29    /// Archives are disabled if this field is `None`.
30    pub archives_gc: Option<ArchivesGcConfig>,
31
32    /// States GC config.
33    ///
34    /// States GC is disabled if this field is `None`.
35    pub states_gc: Option<StatesGcConfig>,
36
37    /// Blocks GC config.
38    ///
39    /// Blocks GC is disabled if this field is `None`.
40    pub blocks_gc: Option<BlocksGcConfig>,
41
42    /// Blocks cache config.
43    pub blocks_cache: BlocksCacheConfig,
44
45    /// Blob DB config.
46    pub blob_db: BlobDbConfig,
47}
48
49impl CoreStorageConfig {
50    #[cfg(any(test, feature = "test"))]
51    pub fn new_potato() -> Self {
52        Self {
53            cells_cache_size: ByteSize::kb(1024),
54            blob_db: BlobDbConfig {
55                pre_create_cas_tree: false,
56            },
57            archives_gc: None,
58            states_gc: None,
59            blocks_gc: None,
60            ..Default::default()
61        }
62    }
63
64    pub fn without_gc(mut self) -> Self {
65        self.archives_gc = None;
66        self.states_gc = None;
67        self.blocks_gc = None;
68        self
69    }
70}
71
72impl Default for CoreStorageConfig {
73    fn default() -> Self {
74        Self {
75            cells_cache_size: ByteSize::mb(256),
76            drop_interval: 3,
77            store_archives: true,
78            archives_gc: Some(ArchivesGcConfig::default()),
79            states_gc: Some(StatesGcConfig::default()),
80            blocks_gc: Some(BlocksGcConfig::default()),
81            blocks_cache: BlocksCacheConfig::default(),
82            blob_db: BlobDbConfig::default(),
83        }
84    }
85}
86
87#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
88#[serde(default, deny_unknown_fields)]
89pub struct ArchivesGcConfig {
90    /// Remove archives after this interval after the new persistent state
91    #[serde(with = "serde_helpers::humantime")]
92    pub persistent_state_offset: Duration,
93}
94
95impl Default for ArchivesGcConfig {
96    fn default() -> Self {
97        Self {
98            persistent_state_offset: Duration::from_secs(300),
99        }
100    }
101}
102
103#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
104#[serde(default, deny_unknown_fields)]
105pub struct StatesGcConfig {
106    /// Wether to add random offset to the first interval.
107    ///
108    /// Default: true.
109    pub random_offset: bool,
110    /// Default: 900
111    #[serde(with = "serde_helpers::humantime")]
112    pub interval: Duration,
113}
114
115impl Default for StatesGcConfig {
116    fn default() -> Self {
117        Self {
118            random_offset: true,
119            interval: Duration::from_secs(60),
120        }
121    }
122}
123
124#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
125#[serde(default)]
126pub struct BlocksGcConfig {
127    /// Blocks GC type
128    /// - `before_previous_key_block` - on each new key block delete all blocks before the previous one
129    /// - `before_previous_persistent_state` - on each new key block delete all blocks before the
130    ///   previous key block with persistent state
131    #[serde(flatten)]
132    pub ty: BlocksGcType,
133
134    /// Whether to enable blocks GC during sync. Default: true
135    pub enable_for_sync: bool,
136
137    /// Max `WriteBatch` entries before apply
138    pub max_blocks_per_batch: Option<usize>,
139}
140
141impl Default for BlocksGcConfig {
142    fn default() -> Self {
143        Self {
144            ty: BlocksGcType::BeforeSafeDistance {
145                safe_distance: 1000,
146                min_interval: Duration::from_secs(60),
147            },
148            enable_for_sync: true,
149            max_blocks_per_batch: Some(100_000),
150        }
151    }
152}
153
154#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
155#[serde(tag = "type")]
156pub enum BlocksGcType {
157    /// Remove all blocks before the specified safe distance (of mc blocks).
158    BeforeSafeDistance {
159        /// Number of masterchain blocks to keep.
160        safe_distance: u32,
161        /// Minimum interval between GC runs.
162        ///
163        /// Should be about 1 minute.
164        #[serde(with = "serde_helpers::humantime")]
165        min_interval: Duration,
166    },
167    /// Remove all blocks before the previous key block.
168    BeforePreviousKeyBlock,
169    /// Remove all blocks before the previous persistent state.
170    BeforePreviousPersistentState,
171}
172
173#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
174#[serde(default, deny_unknown_fields)]
175pub struct BlocksCacheConfig {
176    /// Entry TTL.
177    ///
178    /// Default: `5 min`.
179    #[serde(with = "serde_helpers::humantime")]
180    pub ttl: Duration,
181
182    /// Cache capacity in bytes.
183    ///
184    /// Default: `500 MB`.
185    pub size: ByteSize,
186}
187
188impl Default for BlocksCacheConfig {
189    fn default() -> Self {
190        Self {
191            ttl: Duration::from_secs(300),
192            size: ByteSize::mb(500),
193        }
194    }
195}
196
197#[derive(Debug, Clone, Serialize, Deserialize)]
198#[serde(default, deny_unknown_fields)]
199pub struct BlobDbConfig {
200    pub pre_create_cas_tree: bool,
201}
202
203impl Default for BlobDbConfig {
204    fn default() -> Self {
205        Self {
206            pre_create_cas_tree: true,
207        }
208    }
209}