Skip to content

Commit

Permalink
merge FOREST_TEST_RNG_FIXED_SEED and FOREST_TEST_OS_RNG_FIXED_SEED
Browse files Browse the repository at this point in the history
  • Loading branch information
hanabi1224 committed Mar 5, 2025
1 parent 24f76f2 commit 3ae3d34
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
3 changes: 1 addition & 2 deletions docs/docs/users/reference/env_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ process.
| `FOREST_MAX_FILTER_HEIGHT_RANGE` | positive integer | 2880 | 2880 | The maximum filter height range allowed, a conservative limit of one day |
| `FOREST_STATE_MIGRATION_THREADS` | integer | Depends on the machine. | 3 | The number of threads for state migration thread-pool. Advanced users only. |
| `FOREST_CONFIG_PATH` | string | /$FOREST_HOME/com.ChainSafe.Forest/config.toml | `/path/to/config.toml` | Forest configuration path. Alternatively supplied via `--config` cli parameter. |
| `FOREST_TEST_RNG_FIXED_SEED` | non-negative integer | empty | 0 | Override thread RNG with a reproducible one seeded by the value |
| `FOREST_TEST_OS_RNG_FIXED_SEED` | non-negative integer | empty | 0 | Override OS RNG with a reproducible one seeded by the value |
| `FOREST_TEST_RNG_FIXED_SEED` | non-negative integer | empty | 0 | Override RNG with a reproducible one seeded by the value |
| `RUST_LOG` | string | empty | `debug,forest_libp2p::service=info` | Allows for log level customization. |
| `FOREST_IGNORE_DRAND` | 1 or true | empty | 1 | Ignore Drand validation. |
| `FOREST_F3_SIDECAR_RPC_ENDPOINT` | string | 127.0.0.1:23456 | `127.0.0.1:23456` | An RPC endpoint of F3 sidecar. |
Expand Down
18 changes: 7 additions & 11 deletions src/utils/rand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,25 @@ use rand::{CryptoRng, Rng, RngCore, SeedableRng as _};
/// [`rand_chacha::ChaChaRng`] via `FOREST_TEST_RNG_FIXED_SEED` environment variable.
/// This is required for reproducible test cases for normally non-deterministic methods.
pub fn forest_rng() -> impl Rng + CryptoRng {
const ENV_KEY: &str = "FOREST_TEST_RNG_FIXED_SEED";
forest_rng_internal(ENV_KEY, false)
forest_rng_internal(false)
}

/// A wrapper of [`rand::rngs::OsRng`] that can be overridden by reproducible seeded
/// [`rand_chacha::ChaChaRng`] via `FOREST_TEST_RNG_FIXED_SEED` environment variable.
/// This is required for reproducible test cases for normally non-deterministic methods.
pub fn forest_os_rng() -> impl Rng + CryptoRng {
const ENV_KEY: &str = "FOREST_TEST_OS_RNG_FIXED_SEED";
forest_rng_internal(ENV_KEY, true)
forest_rng_internal(true)
}

fn forest_rng_internal(
env_key: &str,
prefer_os_rng_for_enhanced_security: bool,
) -> impl Rng + CryptoRng {
if let Ok(v) = std::env::var(env_key) {
fn forest_rng_internal(prefer_os_rng_for_enhanced_security: bool) -> impl Rng + CryptoRng {
const ENV_KEY: &str = "FOREST_TEST_RNG_FIXED_SEED";
if let Ok(v) = std::env::var(ENV_KEY) {
if let Ok(seed) = v.parse() {
tracing::warn!("[security] using test RNG with fixed seed {seed} set by {env_key}");
tracing::warn!("[security] using test RNG with fixed seed {seed} set by {ENV_KEY}");
return Either::Left(rand_chacha::ChaChaRng::seed_from_u64(seed));
} else {
tracing::warn!(
"invalid u64 seed set by {env_key}: {v}. Falling back to the default RNG."
"invalid u64 seed set by {ENV_KEY}: {v}. Falling back to the default RNG."
);
}
}
Expand Down

0 comments on commit 3ae3d34

Please sign in to comment.