Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Forest when using a config file to run calibnet #2796

Merged
merged 12 commits into from
Apr 27, 2023
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@

### Changed

- [#2796] (https://github.com/ChainSafe/forest/pull/2796): Remove ability to use
at the same time `--chain` and `--config` flags for forest binary.

### Removed

### Fixed

- [#2796] (https://github.com/ChainSafe/forest/pull/2796): Fix issue when
running Forest on calibnet using a configuration file only.

## Forest v0.8.1 "Cold Exposure"

### Fixed
Expand Down
14 changes: 13 additions & 1 deletion forest/shared/src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{path::PathBuf, sync::Arc};
use forest_chain_sync::SyncConfig;
use forest_db::db_engine::DbConfig;
use forest_libp2p::Libp2pConfig;
use forest_networks::ChainConfig;
use forest_networks::{ChainConfig, NetworkChain};
use log::LevelFilter;
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down Expand Up @@ -197,6 +197,18 @@ pub struct Config {
pub tokio: TokioConfig,
}

impl Config {
pub fn from_chain(network_type: &NetworkChain) -> Self {
match network_type {
NetworkChain::Mainnet => Config::default(),
NetworkChain::Calibnet => Config {
chain: Arc::new(ChainConfig::calibnet()),
..Config::default()
},
}
}
}

impl Config {
cfg_if::cfg_if! {
if #[cfg(feature = "rocksdb")] {
Expand Down
45 changes: 34 additions & 11 deletions forest/shared/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ mod snapshot_fetch;
use std::{
net::SocketAddr,
path::{Path, PathBuf},
sync::Arc,
};

use ahash::HashSet;
use byte_unit::Byte;
use clap::Parser;
use directories::ProjectDirs;
use forest_networks::ChainConfig;
use forest_networks::NetworkChain;
use forest_utils::io::{read_file_to_string, read_toml, ProgressBarVisibility};
use log::error;
use num::BigInt;
Expand All @@ -39,7 +38,7 @@ OPTIONS:
";

/// CLI options
#[derive(Debug, Parser)]
#[derive(Default, Debug, Parser)]
pub struct CliOpts {
/// A TOML file containing relevant configurations
#[arg(short, long)]
Expand Down Expand Up @@ -98,7 +97,7 @@ pub struct CliOpts {
pub encrypt_keystore: Option<bool>,
/// Choose network chain to sync to
#[arg(long)]
pub chain: Option<String>,
pub chain: Option<NetworkChain>,
/// Daemonize Forest process
#[arg(long)]
pub detach: bool,
Expand Down Expand Up @@ -141,6 +140,10 @@ pub struct CliOpts {

impl CliOpts {
pub fn to_config(&self) -> Result<(Config, Option<ConfigPath>), anyhow::Error> {
if self.config.is_some() && self.chain.is_some() {
elmattic marked this conversation as resolved.
Show resolved Hide resolved
anyhow::bail!("Can't use a config file and chain flag at the same time!")
}

let path = find_config_path(self);
let mut cfg: Config = match &path {
Some(path) => {
Expand All @@ -149,15 +152,16 @@ impl CliOpts {
// Parse and return the configuration file
read_toml(&toml)?
}
None => Config::default(),
None => {
if let Some(chain) = &self.chain {
Config::from_chain(chain)
} else {
// Create the default `mainnet` configuration.
Config::default()
}
}
};

match &self.chain {
// override the chain configuration
Some(name) if name == "calibnet" => cfg.chain = Arc::new(ChainConfig::calibnet()),
_ => cfg.chain = Arc::new(ChainConfig::mainnet()),
}

if let Some(genesis_file) = &self.genesis {
cfg.client.genesis_file = Some(genesis_file.to_owned());
}
Expand Down Expand Up @@ -425,4 +429,23 @@ mod tests {
]
);
}

#[test]
fn combination_of_following_flags_should_fail() {
// Check for --chain and --config
let options = CliOpts {
config: Some("config.toml".into()),
chain: Some(NetworkChain::Calibnet),
..Default::default()
};
assert!(options.to_config().is_err());

// Check for --import_snapshot and --import_chain
let options = CliOpts {
import_snapshot: Some("snapshot.car".into()),
import_chain: Some("snapshot.car".into()),
..Default::default()
};
assert!(options.to_config().is_err());
}
}
6 changes: 6 additions & 0 deletions forest/shared/src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ impl LoggingColor {
}
}

impl Default for LoggingColor {
fn default() -> Self {
Self::Auto
}
}

impl FromStr for LoggingColor {
type Err = anyhow::Error;

Expand Down
23 changes: 22 additions & 1 deletion networks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright 2019-2023 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use std::sync::Arc;
use std::{str::FromStr, sync::Arc};

use anyhow::Error;
use cid::Cid;
use fil_actors_runtime_v10::runtime::Policy;
use forest_beacon::{BeaconPoint, BeaconSchedule, DrandBeacon, DrandConfig};
Expand All @@ -25,6 +26,26 @@ const CALIBNET_ETH_CHAIN_ID: u64 = 314159;
/// Newest network version for all networks
pub const NEWEST_NETWORK_VERSION: NetworkVersion = NetworkVersion::V17;

/// The `filecoin` network chain. In general only `mainnet` and its chain
/// information should be considered stable.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NetworkChain {
Mainnet,
Calibnet,
}

impl FromStr for NetworkChain {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"mainnet" => Ok(NetworkChain::Mainnet),
"calibnet" => Ok(NetworkChain::Calibnet),
name => Err(anyhow::anyhow!("unsupported network chain: {name}")),
}
}
}

/// Defines the meaningful heights of the protocol.
#[derive(Debug, Display, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum Height {
Expand Down