-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: configurable block delay and propagation delay (#4608)
- Loading branch information
1 parent
3733765
commit 6067514
Showing
8 changed files
with
94 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2019-2024 ChainSafe Systems | ||
// SPDX-License-Identifier: Apache-2.0, MIT | ||
|
||
use std::str::FromStr; | ||
|
||
/// Get the value of an environment variable, or a default value if it is not set or cannot be | ||
/// parsed. | ||
pub fn env_or_default<T: FromStr>(key: &str, default: T) -> T { | ||
std::env::var(key) | ||
.ok() | ||
.and_then(|v| v.parse().ok()) | ||
.unwrap_or(default) | ||
} | ||
|
||
/// Check if the given environment variable is set to truthy value. | ||
pub fn is_env_truthy(env: &str) -> bool { | ||
match std::env::var(env) { | ||
Ok(var) => matches!(var.to_lowercase().as_str(), "1" | "true"), | ||
_ => false, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_env_or_default() { | ||
// variable set, should return its parsed value | ||
std::env::set_var("TEST_ENV", "42"); | ||
assert_eq!(env_or_default("TEST_ENV", 0), 42); | ||
|
||
// variable not set, should return default | ||
std::env::remove_var("TEST_ENV"); | ||
assert_eq!(env_or_default("TEST_ENV", 0), 0); | ||
|
||
// unparsable value given the default type, should return default | ||
std::env::set_var("TEST_ENV", "42"); | ||
assert!(!env_or_default("TEST_ENV", false)); | ||
} | ||
|
||
#[test] | ||
fn test_is_env_truthy() { | ||
let cases = [ | ||
("1", true), | ||
("true", true), | ||
("0", false), | ||
("false", false), | ||
("", false), | ||
("cthulhu", false), | ||
]; | ||
|
||
for (input, expected) in cases.iter() { | ||
std::env::set_var("TEST_ENV", input); | ||
assert_eq!(is_env_truthy("TEST_ENV"), *expected); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters