diff --git a/.gitignore b/.gitignore index c44897441..7042b1a29 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ heaptrack* logging-toolkit *.profile *.heap +rust-fil-proofs.config.toml diff --git a/storage-proofs/Cargo.toml b/storage-proofs/Cargo.toml index b7c8f3185..9add212b3 100644 --- a/storage-proofs/Cargo.toml +++ b/storage-proofs/Cargo.toml @@ -37,6 +37,7 @@ serde = { version = "1.0", features = ["derive"]} base64 = "0.10.0" blake2b_simd = "0.4.1" blake2s_simd = { git = "https://github.com/oconnor663/blake2s_simd", branch = "master"} +toml = "0.4" [dependencies.pairing] version = "0.14.2" diff --git a/storage-proofs/src/config.rs b/storage-proofs/src/config.rs new file mode 100644 index 000000000..87de56526 --- /dev/null +++ b/storage-proofs/src/config.rs @@ -0,0 +1,87 @@ +use failure::{err_msg, Error}; +use std::collections::HashMap; +use std::env; +use std::fs::File; +use std::io::prelude::Read; +use std::path::Path; +use std::sync::Mutex; + +type Result = ::std::result::Result; +type Config = HashMap; + +const CONFIG_PATH: &str = "./rust-fil-proofs.config.toml"; + +const DEFAULT_CONFIG: &[(&str, bool)] = &[("MAXIMIZE_CACHING", false)]; + +lazy_static! { + pub static ref CONFIG: Mutex = Mutex::new(initialize_config().unwrap()); +} + +pub fn initialize_config() -> Result { + let mut config: Config = Default::default(); + + config.extend(load_config_from_defaults()?); + config.extend(load_config_from_toml(CONFIG_PATH)?); + config.extend(load_config_from_env()?); + + Ok(config) +} + +pub fn load_config_from_defaults() -> Result { + Ok(DEFAULT_CONFIG + .iter() + .map(|&(k, v)| (k.to_string(), v)) + .collect()) +} + +pub fn load_config_from_toml(path: &str) -> Result { + let path = Path::new(path); + + if path.exists() { + let mut f = File::open(path)?; + let mut contents = String::new(); + f.read_to_string(&mut contents)?; + + Ok(toml::from_str(&contents)?) + } else { + Ok(Default::default()) + } +} + +pub fn load_config_from_env() -> Result { + let mut env_config: Config = Default::default(); + + for key in DEFAULT_CONFIG.iter().map(|(k, _)| k) { + let var = env::var(&key); + + if var.is_ok() { + env_config.insert(key.to_string(), var.unwrap() != ""); + } + } + + Ok(env_config) +} + +pub fn set_config(key: &str, value: bool) -> Result<()> { + let config = &mut (*CONFIG).lock().unwrap(); + config.insert(key.to_string(), value); + + Ok(()) +} + +pub fn get_config(key: &str) -> Result { + let config = (*CONFIG).lock().unwrap(); + + match config.get(key) { + Some(&value) => Ok(value), + None => Err(err_msg("key not found in config")), + } +} + +pub fn debug_config() { + let config = (*CONFIG).lock().unwrap(); + + for (key, value) in config.iter() { + println!("{}: {}", key, value); + } +} diff --git a/storage-proofs/src/lib.rs b/storage-proofs/src/lib.rs index 339f760a2..0a6c62e8a 100644 --- a/storage-proofs/src/lib.rs +++ b/storage-proofs/src/lib.rs @@ -42,6 +42,7 @@ extern crate rayon; extern crate slog; #[macro_use] extern crate serde; +extern crate toml; #[cfg(test)] #[macro_use] @@ -57,6 +58,7 @@ pub mod beacon_post; pub mod challenge_derivation; pub mod circuit; pub mod compound_proof; +pub mod config; pub mod crypto; pub mod drgporep; pub mod drgraph;