fix default network handling (#2029)

## 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`
This commit is contained in:
divma
2020-12-08 05:41:10 +00:00
parent f3200784b4
commit 57489e620f
60 changed files with 145 additions and 182 deletions

View File

@@ -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"

View File

@@ -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;
@@ -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)
}
@@ -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

View File

@@ -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" }

View File

@@ -1,5 +1,5 @@
use clap::ArgMatches;
pub use eth2_testnet_config::DEFAULT_HARDCODED_TESTNET;
pub use eth2_network_config::DEFAULT_HARDCODED_NETWORK;
use std::fs::{self, create_dir_all};
use std::path::{Path, PathBuf};
@@ -19,13 +19,13 @@ pub const CUSTOM_TESTNET_DIR: &str = "custom";
/// Tries to get the name first from the "network" flag,
/// if not present, then checks the "testnet-dir" flag and returns a custom name
/// If neither flags are present, returns the default hardcoded network name.
pub fn get_testnet_name(matches: &ArgMatches) -> String {
if let Some(testnet_name) = matches.value_of("network") {
testnet_name.to_string()
pub fn get_network_dir(matches: &ArgMatches) -> String {
if let Some(network_name) = matches.value_of("network") {
network_name.to_string()
} else if matches.value_of("testnet-dir").is_some() {
CUSTOM_TESTNET_DIR.to_string()
} else {
eth2_testnet_config::DEFAULT_HARDCODED_TESTNET.to_string()
eth2_network_config::DEFAULT_HARDCODED_NETWORK.to_string()
}
}
@@ -54,7 +54,7 @@ pub fn parse_path_or_default_with_flag(
arg,
PathBuf::new()
.join(DEFAULT_ROOT_DIR)
.join(get_testnet_name(matches))
.join(get_network_dir(matches))
.join(flag),
)
}

View File

@@ -5,13 +5,13 @@ use types::{ChainSpec, EthSpecId};
// A macro is used to define this constant so it can be used with `include_bytes!`.
#[macro_export]
macro_rules! testnets_dir {
macro_rules! predefined_networks_dir {
() => {
"built_in_testnet_configs"
"built_in_network_configs"
};
}
pub const TESTNETS_DIR: &str = testnets_dir!();
pub const PREDEFINED_NETWORKS_DIR: &str = predefined_networks_dir!();
pub const GENESIS_FILE_NAME: &str = "genesis.ssz";
pub const GENESIS_ZIP_FILE_NAME: &str = "genesis.ssz.zip";
@@ -57,7 +57,7 @@ impl Eth2Config {
/// A directory that can be built by downloading files via HTTP.
///
/// Used by the `eth2_testnet_config` crate to initialize testnet directories during build and
/// Used by the `eth2_network_config` crate to initialize the network directories during build and
/// access them at runtime.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Eth2NetArchiveAndDirectory<'a> {
@@ -73,7 +73,7 @@ impl<'a> Eth2NetArchiveAndDirectory<'a> {
.expect("should know manifest dir")
.parse::<PathBuf>()
.expect("should parse manifest dir as path")
.join(TESTNETS_DIR)
.join(PREDEFINED_NETWORKS_DIR)
.join(self.unique_id)
}
@@ -94,7 +94,7 @@ macro_rules! define_net {
genesis_is_known: $genesis_is_known,
};
// A wrapper around `std::include_bytes` which includes a file from a specific testnet
// A wrapper around `std::include_bytes` which includes a file from a specific network
// directory. Used by upstream crates to import files at compile time.
#[macro_export]
macro_rules! $macro_title {
@@ -102,7 +102,7 @@ macro_rules! define_net {
include_bytes!(concat!(
$base_dir,
"/",
testnets_dir!(),
predefined_networks_dir!(),
"/",
$name,
"/",

View File

@@ -1,5 +1,5 @@
[package]
name = "eth2_testnet_config"
name = "eth2_network_config"
version = "0.2.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"

View File

@@ -1,4 +1,4 @@
//! Downloads a testnet configuration from Github.
//! Downloads a network configuration from Github.
use eth2_config::{
altona, mainnet, medalla, pyrmont, spadina, toledo, Eth2NetArchiveAndDirectory,
@@ -18,21 +18,21 @@ const ETH2_NET_DIRS: &[Eth2NetArchiveAndDirectory<'static>] = &[
];
fn main() {
for testnet in ETH2_NET_DIRS {
match uncompress_state(testnet) {
for network in ETH2_NET_DIRS {
match uncompress_state(network) {
Ok(()) => (),
Err(e) => panic!(
"Failed to uncompress {} genesis state zip file: {}",
testnet.name, e
network.name, e
),
}
}
}
/// Uncompress the testnet configs archive into a testnet configs folder.
fn uncompress_state(testnet: &Eth2NetArchiveAndDirectory<'static>) -> Result<(), String> {
if testnet.genesis_is_known {
let archive_path = testnet.genesis_state_archive();
/// Uncompress the network configs archive into a network configs folder.
fn uncompress_state(network: &Eth2NetArchiveAndDirectory<'static>) -> Result<(), String> {
if network.genesis_is_known {
let archive_path = network.genesis_state_archive();
let archive_file = File::open(&archive_path)
.map_err(|e| format!("Failed to open archive file {:?}: {:?}", archive_path, e))?;
@@ -45,14 +45,14 @@ fn uncompress_state(testnet: &Eth2NetArchiveAndDirectory<'static>) -> Result<(),
GENESIS_FILE_NAME, e
)
})?;
let path = testnet.dir().join(GENESIS_FILE_NAME);
let path = network.dir().join(GENESIS_FILE_NAME);
let mut outfile = File::create(&path)
.map_err(|e| format!("Error while creating file {:?}: {}", path, e))?;
io::copy(&mut file, &mut outfile)
.map_err(|e| format!("Error writing file {:?}: {}", path, e))?;
} else {
// Create empty genesis.ssz if genesis is unknown
let genesis_file = testnet.dir().join(GENESIS_FILE_NAME);
let genesis_file = network.dir().join(GENESIS_FILE_NAME);
if !genesis_file.exists() {
File::create(genesis_file)
.map_err(|e| format!("Failed to create {}: {}", GENESIS_FILE_NAME, e))?;

View File

@@ -1,13 +1,4 @@
//! This crate should eventually represent the structure at this repo:
//!
//! https://github.com/eth2-clients/eth2-testnets/tree/master/nimbus/testnet1
//!
//! It is not accurate at the moment, we include extra files and we also don't support a few
//! others. We are unable to conform to the repo until we have the following PR merged:
//!
//! https://github.com/sigp/lighthouse/pull/605
//!
use eth2_config::{testnets_dir, *};
use eth2_config::{predefined_networks_dir, *};
use enr::{CombinedKey, Enr};
use ssz::Decode;
@@ -55,13 +46,13 @@ const MAINNET: HardcodedNet = define_net!(mainnet, include_mainnet_file);
const TOLEDO: HardcodedNet = define_net!(toledo, include_toledo_file);
const HARDCODED_NETS: &[HardcodedNet] = &[ALTONA, MEDALLA, SPADINA, PYRMONT, MAINNET, TOLEDO];
pub const DEFAULT_HARDCODED_TESTNET: &str = "mainnet";
pub const DEFAULT_HARDCODED_NETWORK: &str = "mainnet";
/// Specifies an Eth2 testnet.
/// Specifies an Eth2 network.
///
/// See the crate-level documentation for more details.
#[derive(Clone, PartialEq, Debug)]
pub struct Eth2TestnetConfig {
pub struct Eth2NetworkConfig {
/// Note: instead of the block where the contract is deployed, it is acceptable to set this
/// value to be the block number where the first deposit occurs.
pub deposit_contract_deploy_block: u64,
@@ -70,7 +61,7 @@ pub struct Eth2TestnetConfig {
pub yaml_config: Option<YamlConfig>,
}
impl Eth2TestnetConfig {
impl Eth2NetworkConfig {
/// When Lighthouse is built it includes zero or more "hardcoded" network specifications. This
/// function allows for instantiating one of these nets by name.
pub fn constant(name: &str) -> Result<Option<Self>, String> {
@@ -100,7 +91,7 @@ impl Eth2TestnetConfig {
}
/// Returns an identifier that should be used for selecting an `EthSpec` instance for this
/// testnet.
/// network configuration.
pub fn eth_spec_id(&self) -> Result<EthSpecId, String> {
self.yaml_config
.as_ref()
@@ -133,7 +124,7 @@ impl Eth2TestnetConfig {
/// Overwrites files if specified to do so.
pub fn write_to_file(&self, base_dir: PathBuf, overwrite: bool) -> Result<(), String> {
if base_dir.exists() && !overwrite {
return Err("Testnet directory already exists".to_string());
return Err("Network directory already exists".to_string());
}
self.force_write_to_file(base_dir)
@@ -257,7 +248,7 @@ mod tests {
fn hard_coded_nets_work() {
for net in HARDCODED_NETS {
let config =
Eth2TestnetConfig::from_hardcoded_net(net).expect(&format!("{:?}", net.name));
Eth2NetworkConfig::from_hardcoded_net(net).expect(&format!("{:?}", net.name));
if net.name == "mainnet" || net.name == "toledo" || net.name == "pyrmont" {
// Ensure we can parse the YAML config to a chain spec.
@@ -314,7 +305,7 @@ mod tests {
let base_dir = temp_dir.path().join("my_testnet");
let deposit_contract_deploy_block = 42;
let testnet: Eth2TestnetConfig = Eth2TestnetConfig {
let testnet: Eth2NetworkConfig = Eth2NetworkConfig {
deposit_contract_deploy_block,
boot_enr,
genesis_state_bytes: genesis_state.as_ref().map(Encode::as_ssz_bytes),
@@ -325,7 +316,7 @@ mod tests {
.write_to_file(base_dir.clone(), false)
.expect("should write to file");
let decoded = Eth2TestnetConfig::load(base_dir).expect("should load struct");
let decoded = Eth2NetworkConfig::load(base_dir).expect("should load struct");
assert_eq!(testnet, decoded, "should decode as encoded");
}