Skip to content

Commit 42c3144

Browse files
committed
Move get_eth2_network_config
1 parent 6153191 commit 42c3144

File tree

5 files changed

+48
-49
lines changed

5 files changed

+48
-49
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/clap_utils/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ hex = "0.4.2"
1212
dirs = "3.0.1"
1313
eth2_network_config = { path = "../eth2_network_config" }
1414
eth2_ssz = "0.4.0"
15+
ethereum-types = "0.12.1"

common/clap_utils/src/lib.rs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! A helper library for parsing values from `clap::ArgMatches`.
22
33
use clap::ArgMatches;
4-
use eth2_network_config::Eth2NetworkConfig;
4+
use eth2_network_config::{Eth2NetworkConfig, DEFAULT_HARDCODED_NETWORK};
5+
use ethereum_types::U256 as Uint256;
56
use ssz::Decode;
67
use std::path::PathBuf;
78
use std::str::FromStr;
@@ -13,6 +14,47 @@ pub const BAD_TESTNET_DIR_MESSAGE: &str = "The hard-coded testnet directory was
1314
or when there is no default public network to connect to. \
1415
During these times you must specify a --testnet-dir.";
1516

17+
/// Try to parse the eth2 network config from the `network`, `testnet-dir` flags in that order.
18+
/// Returns the default hardcoded testnet if neither flags are set.
19+
pub fn get_eth2_network_config(cli_args: &ArgMatches) -> Result<Eth2NetworkConfig, String> {
20+
let optional_network_config = if cli_args.is_present("network") {
21+
parse_hardcoded_network(cli_args, "network")?
22+
} else if cli_args.is_present("testnet-dir") {
23+
parse_testnet_dir(cli_args, "testnet-dir")?
24+
} else {
25+
// if neither is present, assume the default network
26+
Eth2NetworkConfig::constant(DEFAULT_HARDCODED_NETWORK)?
27+
};
28+
29+
let mut eth2_network_config =
30+
optional_network_config.ok_or_else(|| BAD_TESTNET_DIR_MESSAGE.to_string())?;
31+
32+
if let Some(string) = parse_optional::<String>(cli_args, "terminal-total-difficulty-override")?
33+
{
34+
let stripped = string.replace(",", "");
35+
let terminal_total_difficulty = Uint256::from_dec_str(&stripped).map_err(|e| {
36+
format!(
37+
"Could not parse --terminal-total-difficulty-override as decimal value: {:?}",
38+
e
39+
)
40+
})?;
41+
42+
eth2_network_config.config.terminal_total_difficulty = terminal_total_difficulty;
43+
}
44+
45+
if let Some(hash) = parse_optional(cli_args, "terminal-block-hash-override")? {
46+
eth2_network_config.config.terminal_block_hash = hash;
47+
}
48+
49+
if let Some(epoch) = parse_optional(cli_args, "terminal-block-hash-epoch-override")? {
50+
eth2_network_config
51+
.config
52+
.terminal_block_hash_activation_epoch = epoch;
53+
}
54+
55+
Ok(eth2_network_config)
56+
}
57+
1658
/// Attempts to load the testnet dir at the path if `name` is in `matches`, returning an error if
1759
/// the path cannot be found or the testnet dir is invalid.
1860
pub fn parse_testnet_dir(

lighthouse/src/main.rs

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod metrics;
44

55
use beacon_node::ProductionBeaconNode;
66
use clap::{App, Arg, ArgMatches};
7-
use clap_utils::{flags::DISABLE_MALLOC_TUNING_FLAG, BAD_TESTNET_DIR_MESSAGE};
7+
use clap_utils::{flags::DISABLE_MALLOC_TUNING_FLAG, get_eth2_network_config};
88
use env_logger::{Builder, Env};
99
use environment::EnvironmentBuilder;
1010
use eth2_hashing::have_sha_extensions;
@@ -16,7 +16,7 @@ use std::fs::File;
1616
use std::path::PathBuf;
1717
use std::process::exit;
1818
use task_executor::ShutdownReason;
19-
use types::{EthSpec, EthSpecId, Uint256};
19+
use types::{EthSpec, EthSpecId};
2020
use validator_client::ProductionValidatorClient;
2121

2222
fn bls_library_name() -> &'static str {
@@ -465,46 +465,3 @@ fn run<E: EthSpec>(
465465
ShutdownReason::Failure(msg) => Err(msg.to_string()),
466466
}
467467
}
468-
469-
/// Try to parse the eth2 network config from the `network`, `testnet-dir` flags in that order.
470-
/// Returns the default hardcoded testnet if neither flags are set.
471-
pub fn get_eth2_network_config(cli_args: &ArgMatches) -> Result<Eth2NetworkConfig, String> {
472-
let optional_network_config = if cli_args.is_present("network") {
473-
clap_utils::parse_hardcoded_network(cli_args, "network")?
474-
} else if cli_args.is_present("testnet-dir") {
475-
clap_utils::parse_testnet_dir(cli_args, "testnet-dir")?
476-
} else {
477-
// if neither is present, assume the default network
478-
Eth2NetworkConfig::constant(DEFAULT_HARDCODED_NETWORK)?
479-
};
480-
481-
let mut eth2_network_config =
482-
optional_network_config.ok_or_else(|| BAD_TESTNET_DIR_MESSAGE.to_string())?;
483-
484-
if let Some(string) =
485-
clap_utils::parse_optional::<String>(cli_args, "terminal-total-difficulty-override")?
486-
{
487-
let stripped = string.replace(",", "");
488-
let terminal_total_difficulty = Uint256::from_dec_str(&stripped).map_err(|e| {
489-
format!(
490-
"Could not parse --terminal-total-difficulty-override as decimal value: {:?}",
491-
e
492-
)
493-
})?;
494-
495-
eth2_network_config.config.terminal_total_difficulty = terminal_total_difficulty;
496-
}
497-
498-
if let Some(hash) = clap_utils::parse_optional(cli_args, "terminal-block-hash-override")? {
499-
eth2_network_config.config.terminal_block_hash = hash;
500-
}
501-
502-
if let Some(epoch) = clap_utils::parse_optional(cli_args, "terminal-block-hash-epoch-override")?
503-
{
504-
eth2_network_config
505-
.config
506-
.terminal_block_hash_activation_epoch = epoch;
507-
}
508-
509-
Ok(eth2_network_config)
510-
}

lighthouse/tests/boot_node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use boot_node::config::BootNodeConfigSerialization;
22

33
use crate::exec::{CommandLineTestExec, CompletedTest};
4-
use beacon_node::get_eth2_network_config;
54
use clap::ArgMatches;
5+
use clap_utils::get_eth2_network_config;
66
use lighthouse_network::discovery::ENR_FILENAME;
77
use lighthouse_network::Enr;
88
use std::fs::File;

0 commit comments

Comments
 (0)