Skip to content

Commit

Permalink
feat: configurable block delay and propagation delay (#4608)
Browse files Browse the repository at this point in the history
  • Loading branch information
LesnyRumcajs authored Aug 2, 2024
1 parent 3733765 commit 6067514
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
- [#4607](https://github.com/ChainSafe/forest/pull/4607) Expose usage and timing
metrics for RPC methods.

- [#4599](https://github.com/ChainSafe/forest/issues/4599) Block delay and block
propagation delays are now configurable via
[environment variables](https://github.com/ChainSafe/forest/blob/main/documentation/src/environment_variables.md).

### Changed

- [#4583](https://github.com/ChainSafe/forest/pull/4583) Removed the expiration
Expand Down
2 changes: 2 additions & 0 deletions documentation/src/environment_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ process.
| IPFS_GATEWAY | URL | https://proofs.filecoin.io/ipfs/ | The IPFS gateway to use for downloading proofs parameters |
| FOREST_RPC_DEFAULT_TIMEOUT | Duration (in seconds) | 60 | The default timeout for RPC calls |
| FOREST_MAX_CONCURRENT_REQUEST_RESPONSE_STREAMS_PER_PEER | positive integer | 10 | the maximum concurrent streams per peer for request-response-based p2p protocols |
| FOREST_BLOCK_DELAY_SECS | positive integer | Depends on the network | Duration of each tipset epoch |
| FOREST_PROPAGATION_DELAY_SECS | positive integer | Depends on the network | How long to wait for a block to propagate through the network |

### FOREST_DB_DEV_MODE

Expand Down
29 changes: 21 additions & 8 deletions src/networks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::eth::EthChainId;
use crate::shim::clock::{ChainEpoch, EPOCH_DURATION_SECONDS};
use crate::shim::sector::{RegisteredPoStProofV3, RegisteredSealProofV3};
use crate::shim::version::NetworkVersion;
use crate::utils::misc::env::env_or_default;
use crate::{make_butterfly_policy, make_calibnet_policy, make_devnet_policy, make_mainnet_policy};

mod actors_bundle;
Expand All @@ -39,6 +40,9 @@ pub mod metrics;
/// Newest network version for all networks
pub const NEWEST_NETWORK_VERSION: NetworkVersion = NetworkVersion::V17;

const ENV_FOREST_BLOCK_DELAY_SECS: &str = "FOREST_BLOCK_DELAY_SECS";
const ENV_FOREST_PROPAGATION_DELAY_SECS: &str = "FOREST_PROPAGATION_DELAY_SECS";

/// Forest builtin `filecoin` network chains. In general only `mainnet` and its
/// chain information should be considered stable.
#[derive(
Expand Down Expand Up @@ -228,8 +232,11 @@ impl ChainConfig {
network: NetworkChain::Mainnet,
genesis_cid: Some(GENESIS_CID.to_string()),
bootstrap_peers: DEFAULT_BOOTSTRAP.clone(),
block_delay_secs: EPOCH_DURATION_SECONDS as u32,
propagation_delay_secs: 10,
block_delay_secs: env_or_default(
ENV_FOREST_BLOCK_DELAY_SECS,
EPOCH_DURATION_SECONDS as u32,
),
propagation_delay_secs: env_or_default(ENV_FOREST_PROPAGATION_DELAY_SECS, 10),
genesis_network: GENESIS_NETWORK_VERSION,
height_infos: HEIGHT_INFOS.clone(),
policy: make_mainnet_policy!(v13),
Expand All @@ -244,8 +251,11 @@ impl ChainConfig {
network: NetworkChain::Calibnet,
genesis_cid: Some(GENESIS_CID.to_string()),
bootstrap_peers: DEFAULT_BOOTSTRAP.clone(),
block_delay_secs: EPOCH_DURATION_SECONDS as u32,
propagation_delay_secs: 10,
block_delay_secs: env_or_default(
ENV_FOREST_BLOCK_DELAY_SECS,
EPOCH_DURATION_SECONDS as u32,
),
propagation_delay_secs: env_or_default(ENV_FOREST_PROPAGATION_DELAY_SECS, 10),
genesis_network: GENESIS_NETWORK_VERSION,
height_infos: HEIGHT_INFOS.clone(),
policy: make_calibnet_policy!(v13),
Expand All @@ -260,8 +270,8 @@ impl ChainConfig {
network: NetworkChain::Devnet("devnet".to_string()),
genesis_cid: None,
bootstrap_peers: Vec::new(),
block_delay_secs: 4,
propagation_delay_secs: 1,
block_delay_secs: env_or_default(ENV_FOREST_BLOCK_DELAY_SECS, 4),
propagation_delay_secs: env_or_default(ENV_FOREST_PROPAGATION_DELAY_SECS, 1),
genesis_network: *GENESIS_NETWORK_VERSION,
height_infos: HEIGHT_INFOS.clone(),
policy: make_devnet_policy!(v13),
Expand All @@ -277,8 +287,11 @@ impl ChainConfig {
network: NetworkChain::Butterflynet,
genesis_cid: Some(GENESIS_CID.to_string()),
bootstrap_peers: DEFAULT_BOOTSTRAP.clone(),
block_delay_secs: EPOCH_DURATION_SECONDS as u32,
propagation_delay_secs: 6,
block_delay_secs: env_or_default(
ENV_FOREST_BLOCK_DELAY_SECS,
EPOCH_DURATION_SECONDS as u32,
),
propagation_delay_secs: env_or_default(ENV_FOREST_PROPAGATION_DELAY_SECS, 6),
genesis_network: GENESIS_NETWORK_VERSION,
height_infos: HEIGHT_INFOS.clone(),
policy: make_butterfly_policy!(v13),
Expand Down
58 changes: 58 additions & 0 deletions src/utils/misc/env.rs
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);
}
}
}
1 change: 1 addition & 0 deletions src/utils/misc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod adaptive_value_provider;
pub use adaptive_value_provider::*;
mod logo;
pub use logo::*;
pub mod env;

#[derive(Debug, Clone, PartialEq, Eq, strum::EnumString)]
#[strum(serialize_all = "kebab-case")]
Expand Down
8 changes: 0 additions & 8 deletions src/utils/proofs_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,3 @@ mod paramfetch;

pub use parameters::set_proofs_parameter_cache_dir_env;
pub use paramfetch::{ensure_params_downloaded, get_params_default, SectorSizeOpt};

/// Check if the given environment variable is set to truthy value.
fn is_env_truthy(env: &str) -> bool {
match std::env::var(env) {
Ok(var) => matches!(var.to_lowercase().as_str(), "1" | "true"),
_ => false,
}
}
2 changes: 1 addition & 1 deletion src/utils/proofs_api/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use cid::Cid;
use serde::{Deserialize, Serialize};
use tracing::{debug, warn};

use super::is_env_truthy;
use crate::utils::misc::env::is_env_truthy;

const PROOF_DIGEST_LEN: usize = 16;

Expand Down
14 changes: 7 additions & 7 deletions src/utils/proofs_api/paramfetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ use std::{

use crate::{
shim::sector::SectorSize,
utils::net::{download_ipfs_file_trustlessly, global_http_client},
utils::{
misc::env::is_env_truthy,
net::{download_ipfs_file_trustlessly, global_http_client},
},
};
use anyhow::{bail, Context};
use backoff::{future::retry, ExponentialBackoffBuilder};
use futures::{stream::FuturesUnordered, AsyncWriteExt, TryStreamExt};
use tokio::fs::{self};
use tracing::{debug, info, warn};

use super::{
is_env_truthy,
parameters::{
check_parameter_file, param_dir, ParameterData, ParameterMap, DEFAULT_PARAMETERS,
PROOFS_PARAMETER_CACHE_ENV,
},
use super::parameters::{
check_parameter_file, param_dir, ParameterData, ParameterMap, DEFAULT_PARAMETERS,
PROOFS_PARAMETER_CACHE_ENV,
};

/// Default IPFS gateway to use for fetching parameters.
Expand Down

0 comments on commit 6067514

Please sign in to comment.