use std::cell::RefCell;
use arbitrary::Arbitrary;
thread_local! {
static CONFIG: RefCell<Config> = RefCell::new(Config::new());
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Arbitrary)]
struct Config {
pub merkle_tree_parallelization_cutoff: MerkleTreeParallelizationCutoff,
}
impl Config {
fn new() -> Self {
let merkle_tree_parallelization_cutoff = MerkleTreeParallelizationCutoff::new(None);
Self {
merkle_tree_parallelization_cutoff,
}
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Arbitrary)]
struct MerkleTreeParallelizationCutoff(usize);
impl MerkleTreeParallelizationCutoff {
const ENV_VAR: &'static str = "TWENTY_FIRST_MERKLE_TREE_PARALLELIZATION_CUTOFF";
const DEFAULT: usize = 512;
const MINIMUM: usize = 2;
fn new(config_value: Option<usize>) -> Self {
let cutoff = std::env::var(Self::ENV_VAR)
.ok()
.and_then(|s| s.parse().ok())
.or(config_value)
.unwrap_or(Self::DEFAULT)
.max(Self::MINIMUM);
Self(cutoff)
}
}
pub fn set_merkle_tree_parallelization_cutoff(cutoff: usize) {
let cutoff = MerkleTreeParallelizationCutoff::new(Some(cutoff));
CONFIG.with(|c| c.borrow_mut().merkle_tree_parallelization_cutoff = cutoff);
}
pub(crate) fn merkle_tree_parallelization_cutoff() -> usize {
CONFIG
.with(|c| c.borrow().merkle_tree_parallelization_cutoff)
.0
}