casper_node/components/storage/
config.rs

1use std::path::PathBuf;
2
3use datasize::DataSize;
4use serde::{Deserialize, Serialize};
5#[cfg(test)]
6use tempfile::TempDir;
7
8const GIB: usize = 1024 * 1024 * 1024;
9const DEFAULT_MAX_BLOCK_STORE_SIZE: usize = 450 * GIB;
10const DEFAULT_MAX_DEPLOY_STORE_SIZE: usize = 300 * GIB;
11const DEFAULT_MAX_DEPLOY_METADATA_STORE_SIZE: usize = 300 * GIB;
12const DEFAULT_MAX_STATE_STORE_SIZE: usize = 10 * GIB;
13
14/// On-disk storage configuration.
15#[derive(Clone, DataSize, Debug, Deserialize, Serialize)]
16#[serde(deny_unknown_fields)]
17pub struct Config {
18    /// The path to the folder where any files created or read by the storage component will exist.
19    ///
20    /// If the folder doesn't exist, it and any required parents will be created.
21    pub path: PathBuf,
22    /// The maximum size of the database to use for the block store.
23    ///
24    /// The size should be a multiple of the OS page size.
25    pub max_block_store_size: usize,
26    /// The maximum size of the database to use for the deploy store.
27    ///
28    /// The size should be a multiple of the OS page size.
29    pub max_deploy_store_size: usize,
30    /// The maximum size of the database to use for the deploy metadata store.
31    ///
32    /// The size should be a multiple of the OS page size.
33    pub max_deploy_metadata_store_size: usize,
34    /// The maximum size of the database to use for the component state store.
35    ///
36    /// The size should be a multiple of the OS page size.
37    pub max_state_store_size: usize,
38    /// Whether or not memory deduplication is enabled.
39    pub enable_mem_deduplication: bool,
40    /// How many loads before memory duplication checks for dead references.
41    pub mem_pool_prune_interval: u16,
42}
43
44impl Default for Config {
45    fn default() -> Self {
46        Config {
47            // No one should be instantiating a config with storage set to default.
48            path: "/dev/null".into(),
49            max_block_store_size: DEFAULT_MAX_BLOCK_STORE_SIZE,
50            max_deploy_store_size: DEFAULT_MAX_DEPLOY_STORE_SIZE,
51            max_deploy_metadata_store_size: DEFAULT_MAX_DEPLOY_METADATA_STORE_SIZE,
52            max_state_store_size: DEFAULT_MAX_STATE_STORE_SIZE,
53            enable_mem_deduplication: true,
54            mem_pool_prune_interval: 4096,
55        }
56    }
57}
58
59impl Config {
60    /// Returns a `Config` suitable for tests, along with a `TempDir` which must be kept alive for
61    /// the duration of the test since its destructor removes the dir from the filesystem.
62    ///
63    /// `size_multiplier` is used to multiply the default DB sizes.
64    #[cfg(test)]
65    pub(crate) fn new_for_tests(size_multiplier: u8) -> (Self, TempDir) {
66        if size_multiplier == 0 {
67            panic!("size_multiplier cannot be zero");
68        }
69        let tempdir = tempfile::tempdir().expect("should get tempdir");
70        let path = tempdir.path().join("lmdb");
71
72        let config = Config {
73            path,
74            max_block_store_size: 1024 * 1024 * size_multiplier as usize,
75            max_deploy_store_size: 1024 * 1024 * size_multiplier as usize,
76            max_deploy_metadata_store_size: 1024 * 1024 * size_multiplier as usize,
77            max_state_store_size: 12 * 1024 * size_multiplier as usize,
78            ..Default::default()
79        };
80        (config, tempdir)
81    }
82}