1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use std::sync::Mutex;

use config::{Config, ConfigError, Environment, File};

#[cfg(not(feature = "disk-trees"))]
use crate::SP_LOG;

lazy_static! {
    pub static ref SETTINGS: Mutex<Settings> =
        Mutex::new(Settings::new().expect("invalid configuration"));
}

const SETTINGS_PATH: &str = "./rust-fil-proofs.config.toml";

#[derive(Debug, Serialize, Deserialize)]
#[serde(default)]
pub struct Settings {
    pub maximize_caching: bool,
    pub merkle_tree_path: String,
    pub num_proving_threads: usize,
    pub replicated_trees_dir: String,
    pub generate_merkle_trees_in_parallel: bool,
    // Generating MTs in parallel optimizes for speed while generating them
    // in sequence (`false`) optimizes for memory.
}

impl Default for Settings {
    fn default() -> Self {
        Settings {
            maximize_caching: false,
            merkle_tree_path: "/tmp/merkle-trees".into(),
            num_proving_threads: 1,
            replicated_trees_dir: "".into(),
            generate_merkle_trees_in_parallel: true,
        }
    }
}

impl Settings {
    fn new() -> Result<Settings, ConfigError> {
        let mut s = Config::new();

        s.merge(File::with_name(SETTINGS_PATH).required(false))?;
        s.merge(Environment::with_prefix("FIL_PROOFS"))?;

        let settings: Result<Settings, ConfigError> = s.try_into();

        #[cfg(not(feature = "disk-trees"))]
        {
            if settings.is_ok() && !settings.as_ref().unwrap().generate_merkle_trees_in_parallel {
                warn!(SP_LOG, "{}", "Setting GENERATE_MERKLE_TREES_IN_PARALLEL to false (sequiental generation) \ndoesn't add any value if the `disk-trees` feature is not set (no offload possible)".to_string());
            }
        }

        settings
    }
}