Skip to main content

cuprate_blockchain/
config.rs

1//! Database configuration.
2use std::path::PathBuf;
3
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6
7use cuprate_helper::fs::CUPRATE_DATA_DIR;
8
9/// The persistence mode to use on the database.
10#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12pub enum Persistence {
13    /// Buffer the changes but don't wait for them to be synced to disk.
14    ///
15    /// This can lead to corruption if there is a crash.
16    Buffer,
17    /// Sync all changes to disk.
18    ///
19    /// This prevents corruption but can be a bit slower.
20    Sync,
21    #[default]
22    /// Buffer changes while syncing but then switch to syncing all changes to disk once synced.
23    ///
24    /// This is a compromise between [`Self::Buffer`] and [`Self::Sync`].
25    BufferThenSync,
26}
27
28/// The tapes cache sizes.
29#[derive(Debug, Clone, Eq, PartialEq)]
30#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
31#[cfg_attr(feature = "serde", serde(deny_unknown_fields, default))]
32pub struct CacheSizes {
33    pub rct_outputs: u64,
34    pub tx_infos: u64,
35    pub block_infos: u64,
36    pub pruned_blobs: u64,
37    pub v1_prunable_blobs: u64,
38    pub prunable_blobs: u64,
39    pub prunable_tip: u64,
40}
41
42impl Default for CacheSizes {
43    fn default() -> Self {
44        Self {
45            rct_outputs: 100 * 1024 * 1024,
46            tx_infos: 1024 * 1024,
47            block_infos: 1024 * 1024,
48            pruned_blobs: 25 * 1024 * 1024,
49            v1_prunable_blobs: 8 * 1024,
50            prunable_blobs: 8 * 1024,
51            prunable_tip: 8 * 1024,
52        }
53    }
54}
55
56//---------------------------------------------------------------------------------------------------- Config
57/// The blockchain database configuration.
58#[derive(Debug, Clone, PartialEq, Eq)]
59#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
60pub struct Config {
61    /// The directory where the blockchain blobs are stored.
62    pub blob_dir: PathBuf,
63    /// The directory where the blockchain indexes are stored.
64    pub index_dir: PathBuf,
65    /// The tapes cache sizes.
66    pub cache_sizes: CacheSizes,
67    /// Whether to prune the blockchain database.
68    pub prune: bool,
69    /// The [`Persistence`] mode to use.
70    pub persistence: Persistence,
71}
72
73impl Default for Config {
74    fn default() -> Self {
75        Self {
76            blob_dir: CUPRATE_DATA_DIR.to_path_buf(),
77            index_dir: CUPRATE_DATA_DIR.to_path_buf(),
78            cache_sizes: CacheSizes::default(),
79            prune: false,
80            persistence: Persistence::default(),
81        }
82    }
83}