Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ heaptrack*
logging-toolkit
*.profile
*.heap
rust-fil-proofs.config.toml
1 change: 1 addition & 0 deletions storage-proofs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
87 changes: 87 additions & 0 deletions storage-proofs/src/config.rs
Original file line number Diff line number Diff line change
@@ -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<T> = ::std::result::Result<T, Error>;
type Config = HashMap<String, bool>;

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

const DEFAULT_CONFIG: &[(&str, bool)] = &[("MAXIMIZE_CACHING", false)];

lazy_static! {
pub static ref CONFIG: Mutex<Config> = Mutex::new(initialize_config().unwrap());
}

pub fn initialize_config() -> Result<Config> {
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<Config> {
Ok(DEFAULT_CONFIG
.iter()
.map(|&(k, v)| (k.to_string(), v))
.collect())
}

pub fn load_config_from_toml(path: &str) -> Result<Config> {
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<Config> {
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<bool> {
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);
}
}
2 changes: 2 additions & 0 deletions storage-proofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ extern crate rayon;
extern crate slog;
#[macro_use]
extern crate serde;
extern crate toml;

#[cfg(test)]
#[macro_use]
Expand All @@ -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;
Expand Down