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