Skip to content

Commit

Permalink
fix default network handling (#2029)
Browse files Browse the repository at this point in the history
## Issue Addressed
#1992 and #1987, and also to be considered a continuation of #1751

## Proposed Changes
many changed files but most are renaming to align the code with the semantics of `--network` 
- remove the `--network` default value (in clap) and instead set it after checking the `network` and `testnet-dir` flags
- move `eth2_testnet_config` crate to `eth2_network_config`
- move `Eth2TestnetConfig` to `Eth2NetworkConfig`
- move `DEFAULT_HARDCODED_TESTNET` to `DEFAULT_HARDCODED_NETWORK`
- `beacon_node`s `get_eth2_testnet_config` loads the `DEFAULT_HARDCODED_NETWORK` if there is no network nor testnet provided
- `boot_node`s config loads the config same as the `beacon_node`, it was using the configuration only for preconfigured networks (That code is ~1year old so I asume it was not intended)
- removed a one year old comment stating we should try to emulate `https://github.com/eth2-clients/eth2-testnets/tree/master/nimbus/testnet1` it looks outdated (?)
- remove `lighthouse`s `load_testnet_config` in favor of `get_eth2_network_config` to centralize that logic (It had differences)
- some spelling

## Additional Info
Both the command of #1992 and the scripts of #1987 seem to work fine, same as `bn` and `vc`
  • Loading branch information
divagant-martian committed Dec 8, 2020
1 parent f320078 commit 57489e6
Show file tree
Hide file tree
Showing 60 changed files with 145 additions and 182 deletions.
44 changes: 22 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ members = [
"common/eth2",
"common/eth2_config",
"common/eth2_interop_keypairs",
"common/eth2_testnet_config",
"common/eth2_network_config",
"common/eth2_wallet_manager",
"common/hashset_delay",
"common/lighthouse_metrics",
Expand Down
2 changes: 1 addition & 1 deletion account_manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ eth2_ssz = "0.1.2"
eth2_ssz_derive = "0.1.0"
hex = "0.4.2"
rayon = "1.4.1"
eth2_testnet_config = { path = "../common/eth2_testnet_config" }
eth2_network_config = { path = "../common/eth2_network_config" }
futures = { version = "0.3.7", features = ["compat"] }
clap_utils = { path = "../common/clap_utils" }
directory = { path = "../common/directory" }
Expand Down
4 changes: 2 additions & 2 deletions account_manager/src/validator/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use eth2::{
BeaconNodeHttpClient, Url,
};
use eth2_keystore::Keystore;
use eth2_testnet_config::Eth2TestnetConfig;
use eth2_network_config::Eth2NetworkConfig;
use safe_arith::SafeArith;
use slot_clock::{SlotClock, SystemTimeSlotClock};
use std::path::PathBuf;
Expand Down Expand Up @@ -99,7 +99,7 @@ async fn publish_voluntary_exit<E: EthSpec>(
client: &BeaconNodeHttpClient,
spec: &ChainSpec,
stdin_inputs: bool,
testnet_config: &Eth2TestnetConfig,
testnet_config: &Eth2NetworkConfig,
) -> Result<(), String> {
let genesis_data = get_geneisis_data(client).await?;
let testnet_genesis_root = testnet_config
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ futures = "0.3.7"
environment = { path = "../lighthouse/environment" }
task_executor = { path = "../common/task_executor" }
genesis = { path = "genesis" }
eth2_testnet_config = { path = "../common/eth2_testnet_config" }
eth2_network_config = { path = "../common/eth2_network_config" }
eth2_libp2p = { path = "./eth2_libp2p" }
eth2_ssz = "0.1.2"
serde = "1.0.116"
Expand Down
4 changes: 2 additions & 2 deletions beacon_node/eth2_libp2p/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::types::{GossipKind, MessageData};
use crate::{Enr, PeerIdSerialized};
use directory::{
DEFAULT_BEACON_NODE_DIR, DEFAULT_HARDCODED_TESTNET, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR,
DEFAULT_BEACON_NODE_DIR, DEFAULT_HARDCODED_NETWORK, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR,
};
use discv5::{Discv5Config, Discv5ConfigBuilder};
use libp2p::gossipsub::{
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Default for Config {
let network_dir = dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(DEFAULT_ROOT_DIR)
.join(DEFAULT_HARDCODED_TESTNET)
.join(DEFAULT_HARDCODED_NETWORK)
.join(DEFAULT_BEACON_NODE_DIR)
.join(DEFAULT_NETWORK_DIR);

Expand Down
27 changes: 13 additions & 14 deletions beacon_node/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use clap_utils::BAD_TESTNET_DIR_MESSAGE;
use client::{ClientConfig, ClientGenesis};
use directory::{DEFAULT_BEACON_NODE_DIR, DEFAULT_NETWORK_DIR, DEFAULT_ROOT_DIR};
use eth2_libp2p::{multiaddr::Protocol, Enr, Multiaddr, NetworkConfig, PeerIdSerialized};
use eth2_testnet_config::Eth2TestnetConfig;
use eth2_network_config::{Eth2NetworkConfig, DEFAULT_HARDCODED_NETWORK};
use slog::{info, warn, Logger};
use std::cmp;
use std::cmp::max;
Expand Down Expand Up @@ -237,13 +237,13 @@ pub fn get_config<E: EthSpec>(
}

/*
* Load the eth2 testnet dir to obtain some additional config values.
* Load the eth2 network dir to obtain some additional config values.
*/
let eth2_testnet_config = get_eth2_testnet_config(&cli_args)?;
let eth2_network_config = get_eth2_network_config(&cli_args)?;

client_config.eth1.deposit_contract_address = format!("{:?}", spec.deposit_contract_address);
client_config.eth1.deposit_contract_deploy_block =
eth2_testnet_config.deposit_contract_deploy_block;
eth2_network_config.deposit_contract_deploy_block;
client_config.eth1.lowest_cached_block_number =
client_config.eth1.deposit_contract_deploy_block;
client_config.eth1.follow_distance = spec.eth1_follow_distance;
Expand All @@ -260,11 +260,11 @@ pub fn get_config<E: EthSpec>(
"address" => &client_config.eth1.deposit_contract_address
);

if let Some(mut boot_nodes) = eth2_testnet_config.boot_enr {
if let Some(mut boot_nodes) = eth2_network_config.boot_enr {
client_config.network.boot_nodes_enr.append(&mut boot_nodes)
}

if let Some(genesis_state_bytes) = eth2_testnet_config.genesis_state_bytes {
if let Some(genesis_state_bytes) = eth2_network_config.genesis_state_bytes {
// Note: re-serializing the genesis state is not so efficient, however it avoids adding
// trait bounds to the `ClientGenesis` enum. This would have significant flow-on
// effects.
Expand Down Expand Up @@ -578,26 +578,25 @@ pub fn get_data_dir(cli_args: &ArgMatches) -> PathBuf {
.or_else(|| {
dirs::home_dir().map(|home| {
home.join(DEFAULT_ROOT_DIR)
.join(directory::get_testnet_name(cli_args))
.join(directory::get_network_dir(cli_args))
.join(DEFAULT_BEACON_NODE_DIR)
})
})
.unwrap_or_else(|| PathBuf::from("."))
}

/// Try to parse the eth2 testnet config from the `network`, `testnet-dir` flags in that order.
/// Try to parse the eth2 network config from the `network`, `testnet-dir` flags in that order.
/// Returns the default hardcoded testnet if neither flags are set.
pub fn get_eth2_testnet_config(cli_args: &ArgMatches) -> Result<Eth2TestnetConfig, String> {
let optional_testnet_config = if cli_args.is_present("network") {
pub fn get_eth2_network_config(cli_args: &ArgMatches) -> Result<Eth2NetworkConfig, String> {
let optional_network_config = if cli_args.is_present("network") {
clap_utils::parse_hardcoded_network(cli_args, "network")?
} else if cli_args.is_present("testnet-dir") {
clap_utils::parse_testnet_dir(cli_args, "testnet-dir")?
} else {
return Err(
"No --network or --testnet-dir flags provided, cannot load config.".to_string(),
);
// if neither is present, assume the default network
Eth2NetworkConfig::constant(DEFAULT_HARDCODED_NETWORK)?
};
optional_testnet_config.ok_or_else(|| BAD_TESTNET_DIR_MESSAGE.to_string())
optional_network_config.ok_or_else(|| BAD_TESTNET_DIR_MESSAGE.to_string())
}

/// A bit of hack to find an unused port.
Expand Down
2 changes: 1 addition & 1 deletion beacon_node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use beacon_chain::{
use clap::ArgMatches;
pub use cli::cli_app;
pub use client::{Client, ClientBuilder, ClientConfig, ClientGenesis};
pub use config::{get_config, get_data_dir, get_eth2_testnet_config, set_network_config};
pub use config::{get_config, get_data_dir, get_eth2_network_config, set_network_config};
use environment::RuntimeContext;
pub use eth2_config::Eth2Config;
use slasher::Slasher;
Expand Down
6 changes: 3 additions & 3 deletions boot_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ edition = "2018"
[dependencies]
beacon_node = { path = "../beacon_node" }
clap = "2.33.3"
eth2_libp2p = { path = "../beacon_node/eth2_libp2p" }
types = { path = "../consensus/types" }
eth2_testnet_config = { path = "../common/eth2_testnet_config" }
eth2_libp2p = { path = "../beacon_node/eth2_libp2p" }
types = { path = "../consensus/types" }
eth2_network_config = { path = "../common/eth2_network_config" }
eth2_ssz = "0.1.2"
slog = "2.5.2"
sloggers = "1.0.1"
Expand Down
34 changes: 10 additions & 24 deletions boot_node/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use beacon_node::{get_data_dir, get_eth2_testnet_config, set_network_config};
use beacon_node::{get_data_dir, get_eth2_network_config, set_network_config};
use clap::ArgMatches;
use eth2_libp2p::discv5::{enr::CombinedKey, Enr};
use eth2_libp2p::{
Expand Down Expand Up @@ -28,24 +28,16 @@ impl<T: EthSpec> TryFrom<&ArgMatches<'_>> for BootNodeConfig<T> {
fn try_from(matches: &ArgMatches<'_>) -> Result<Self, Self::Error> {
let data_dir = get_data_dir(matches);

// Try and grab testnet config from input CLI params
let eth2_testnet_config = {
if matches.is_present("network") {
Some(get_eth2_testnet_config(&matches)?)
} else {
None
}
};
// Try and grab network config from input CLI params
let eth2_network_config = get_eth2_network_config(&matches)?;

// Try and obtain bootnodes

let boot_nodes = {
let mut boot_nodes = Vec::new();

if let Some(testnet_config) = eth2_testnet_config.as_ref() {
if let Some(enr) = &testnet_config.boot_enr {
boot_nodes.extend_from_slice(enr);
}
if let Some(enr) = &eth2_network_config.boot_enr {
boot_nodes.extend_from_slice(enr);
}

if let Some(nodes) = matches.value_of("boot-nodes") {
Expand Down Expand Up @@ -80,16 +72,16 @@ impl<T: EthSpec> TryFrom<&ArgMatches<'_>> for BootNodeConfig<T> {
let local_key = CombinedKey::from_libp2p(&private_key)?;

// build the enr_fork_id and add it to the local_enr if it exists
let enr_fork = if let Some(config) = eth2_testnet_config.as_ref() {
let spec = config
let enr_fork = {
let spec = eth2_network_config
.yaml_config
.as_ref()
.ok_or("The testnet directory must contain a spec config")?
.ok_or("The network directory must contain a spec config")?
.apply_to_chain_spec::<T>(&T::default_spec())
.ok_or("The loaded config is not compatible with the current spec")?;

if config.beacon_state_is_known() {
let genesis_state = config.beacon_state::<T>()?;
if eth2_network_config.beacon_state_is_known() {
let genesis_state = eth2_network_config.beacon_state::<T>()?;

slog::info!(logger, "Genesis state found"; "root" => genesis_state.canonical_root().to_string());
let enr_fork = spec.enr_fork_id(
Expand All @@ -105,12 +97,6 @@ impl<T: EthSpec> TryFrom<&ArgMatches<'_>> for BootNodeConfig<T> {
);
None
}
} else {
slog::warn!(
logger,
"No testnet config provided. Not setting an eth2 field"
);
None
};

// Build the local ENR
Expand Down
2 changes: 1 addition & 1 deletion common/clap_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ clap = "2.33.3"
hex = "0.4.2"
dirs = "3.0.1"
types = { path = "../../consensus/types" }
eth2_testnet_config = { path = "../eth2_testnet_config" }
eth2_network_config = { path = "../eth2_network_config" }
eth2_ssz = "0.1.2"
10 changes: 5 additions & 5 deletions common/clap_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! A helper library for parsing values from `clap::ArgMatches`.
use clap::ArgMatches;
use eth2_testnet_config::Eth2TestnetConfig;
use eth2_network_config::Eth2NetworkConfig;
use ssz::Decode;
use std::path::PathBuf;
use std::str::FromStr;
Expand All @@ -16,9 +16,9 @@ pub const BAD_TESTNET_DIR_MESSAGE: &str = "The hard-coded testnet directory was
pub fn parse_testnet_dir(
matches: &ArgMatches,
name: &'static str,
) -> Result<Option<Eth2TestnetConfig>, String> {
) -> Result<Option<Eth2NetworkConfig>, String> {
let path = parse_required::<PathBuf>(matches, name)?;
Eth2TestnetConfig::load(path.clone())
Eth2NetworkConfig::load(path.clone())
.map_err(|e| format!("Unable to open testnet dir at {:?}: {}", path, e))
.map(Some)
}
Expand All @@ -28,9 +28,9 @@ pub fn parse_testnet_dir(
pub fn parse_hardcoded_network(
matches: &ArgMatches,
name: &str,
) -> Result<Option<Eth2TestnetConfig>, String> {
) -> Result<Option<Eth2NetworkConfig>, String> {
let network_name = parse_required::<String>(matches, name)?;
Eth2TestnetConfig::constant(network_name.as_str())
Eth2NetworkConfig::constant(network_name.as_str())
}

/// If `name` is in `matches`, parses the value as a path. Otherwise, attempts to find the user's
Expand Down
2 changes: 1 addition & 1 deletion common/directory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ edition = "2018"
clap = "2.33.3"
clap_utils = {path = "../clap_utils"}
dirs = "3.0.1"
eth2_testnet_config = { path = "../eth2_testnet_config" }
eth2_network_config = { path = "../eth2_network_config" }
Loading

0 comments on commit 57489e6

Please sign in to comment.