From 637efccc477f3363c01e17af9d23cc1b990c7d74 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 2 Dec 2019 11:42:04 +1100 Subject: [PATCH] Stop hardcoded testnet dir from creating dir --- account_manager/src/lib.rs | 19 +++------- beacon_node/client/src/config.rs | 4 +- beacon_node/src/cli.rs | 5 ++- beacon_node/src/config.rs | 24 ++++-------- eth2/utils/eth2_testnet/Cargo.toml | 4 +- eth2/utils/eth2_testnet/src/lib.rs | 61 ++++++++++++++++-------------- 6 files changed, 53 insertions(+), 64 deletions(-) diff --git a/account_manager/src/lib.rs b/account_manager/src/lib.rs index 7070cc87ab..867f82ad30 100644 --- a/account_manager/src/lib.rs +++ b/account_manager/src/lib.rs @@ -3,7 +3,7 @@ mod cli; use clap::ArgMatches; use deposit_contract::DEPOSIT_GAS; use environment::{Environment, RuntimeContext}; -use eth2_testnet::{Eth2TestnetDir, TempDir}; +use eth2_testnet::Eth2TestnetDir; use futures::{future, stream::unfold, Future, IntoFuture, Stream}; use rayon::prelude::*; use slog::{crit, error, info, Logger}; @@ -145,6 +145,7 @@ fn run_new_validator_subcommand( .parse::() .map_err(|e| format!("Unable to parse account-index: {}", e))?; + // If supplied, load the eth1 account password from file. let password = if let Some(password_path) = matches.value_of("password") { Some( File::open(password_path) @@ -174,6 +175,7 @@ fn run_new_validator_subcommand( "eth1_node_http_endpoint" => eth1_endpoint ); + // Load the testnet configuration from disk, or use the default testnet. let eth2_testnet_dir: Eth2TestnetDir = if let Some(testnet_dir_str) = matches.value_of("testnet-dir") { @@ -197,19 +199,8 @@ fn run_new_validator_subcommand( Eth2TestnetDir::load(testnet_dir.clone()) .map_err(|e| format!("Failed to load testnet dir at {:?}: {}", testnet_dir, e))? } else { - let temp_dir = TempDir::new("lighthouse-account-manager") - .map_err(|e| format!("Unable to create temporary directory: {}", e))?; - - info!(log, "Using default deposit contract address"); - - let testnet_dir = PathBuf::from(temp_dir.path()); - - Eth2TestnetDir::load(testnet_dir.clone()).map_err(|e| { - format!( - "Failed to load default testnet dir at {:?}: {}", - testnet_dir, e - ) - })? + Eth2TestnetDir::hardcoded() + .map_err(|e| format!("Failed to load hardcoded testnet dir: {}", e))? }; // Convert from `types::Address` to `web3::types::Address`. diff --git a/beacon_node/client/src/config.rs b/beacon_node/client/src/config.rs index dc01a25d6a..330aadbc37 100644 --- a/beacon_node/client/src/config.rs +++ b/beacon_node/client/src/config.rs @@ -44,7 +44,7 @@ impl Default for ClientGenesis { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Config { pub data_dir: PathBuf, - pub testnet_dir: PathBuf, + pub testnet_dir: Option, pub db_type: String, pub db_name: String, pub freezer_db_path: Option, @@ -69,7 +69,7 @@ impl Default for Config { fn default() -> Self { Self { data_dir: PathBuf::from(".lighthouse"), - testnet_dir: PathBuf::from("testnet"), + testnet_dir: None, log_file: PathBuf::from(""), db_type: "disk".to_string(), db_name: "chain_db".to_string(), diff --git a/beacon_node/src/cli.rs b/beacon_node/src/cli.rs index c48881abe7..649520c191 100644 --- a/beacon_node/src/cli.rs +++ b/beacon_node/src/cli.rs @@ -29,7 +29,8 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> { .long("testnet-dir") .value_name("DIR") .help("Path to directory containing eth2_testnet specs. Defaults to \ - ~/.lighthouse/testnet.") + a hard-coded Lighthouse testnet. Only effective if there is no \ + existing database.") .takes_value(true) ) /* @@ -40,7 +41,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> { .long("zero-ports") .short("z") .help("Sets all listening TCP/UDP ports to 0, allowing the OS to choose some \ - arbitrary free port number.") + arbitrary free ports.") .takes_value(false), ) .arg( diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index acfee6b893..47d74b5208 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -14,7 +14,6 @@ use types::{Epoch, EthSpec, Fork}; pub const CLIENT_CONFIG_FILENAME: &str = "beacon-node.toml"; pub const ETH2_CONFIG_FILENAME: &str = "eth2-spec.toml"; -pub const ETH2_TESTNET_DIR: &str = "testnet"; pub const BEACON_NODE_DIR: &str = "beacon"; pub const NETWORK_DIR: &str = "network"; @@ -64,13 +63,9 @@ pub fn get_configs( } // Read the `--testnet-dir` flag. - // - // If it's not present, use the default dir. - client_config.testnet_dir = cli_args - .value_of("testnet-dir") - .map(PathBuf::from) - .or_else(|| dirs::home_dir().map(|home| home.join(".lighthouse").join(ETH2_TESTNET_DIR))) - .ok_or_else(|| "Unable to find a home directory for the testnet-dir".to_string())?; + if let Some(val) = cli_args.value_of("testnet-dir") { + client_config.testnet_dir = Some(PathBuf::from(val)); + } /* * Networking @@ -310,18 +305,13 @@ fn init_new_client( client_config: &mut ClientConfig, eth2_config: &mut Eth2Config, ) -> Result<()> { - let testnet_dir = client_config.testnet_dir.clone(); - - let eth2_testnet_dir: Eth2TestnetDir = if testnet_dir.exists() { + let eth2_testnet_dir: Eth2TestnetDir = if let Some(testnet_dir) = &client_config.testnet_dir + { Eth2TestnetDir::load(testnet_dir.clone()) .map_err(|e| format!("Unable to open testnet dir at {:?}: {}", testnet_dir, e))? } else { - Eth2TestnetDir::create_hardcoded(testnet_dir.clone()).map_err(|e| { - format!( - "Unable to create and open testnet dir at {:?}: {}", - testnet_dir, e - ) - })? + Eth2TestnetDir::hardcoded() + .map_err(|e| format!("Unable to load hard-coded testnet dir: {}", e))? }; eth2_config.spec = eth2_testnet_dir diff --git a/eth2/utils/eth2_testnet/Cargo.toml b/eth2/utils/eth2_testnet/Cargo.toml index 213bbee5b6..cc04cb739d 100644 --- a/eth2/utils/eth2_testnet/Cargo.toml +++ b/eth2/utils/eth2_testnet/Cargo.toml @@ -6,10 +6,12 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dev-dependencies] +tempdir = "0.3" + [dependencies] serde = "1.0" serde_yaml = "0.8" types = { path = "../../types"} eth2-libp2p = { path = "../../../beacon_node/eth2-libp2p"} eth2_ssz = { path = "../ssz"} -tempdir = "0.3" diff --git a/eth2/utils/eth2_testnet/src/lib.rs b/eth2/utils/eth2_testnet/src/lib.rs index 414857c448..5484d45bd4 100644 --- a/eth2/utils/eth2_testnet/src/lib.rs +++ b/eth2/utils/eth2_testnet/src/lib.rs @@ -14,8 +14,6 @@ use std::io::{Read, Write}; use std::path::PathBuf; use types::{Address, BeaconState, EthSpec, YamlConfig}; -pub use tempdir::TempDir; - pub const ADDRESS_FILE: &str = "deposit_contract.txt"; pub const DEPLOY_BLOCK_FILE: &str = "deploy_block.txt"; pub const BOOT_ENR_FILE: &str = "boot_enr.yaml"; @@ -38,32 +36,29 @@ pub struct Eth2TestnetDir { } impl Eth2TestnetDir { - pub fn create_hardcoded(base_dir: PathBuf) -> Result { - if base_dir.exists() { - return Err("Testnet directory already exists".to_string()); - } - - create_dir_all(&base_dir) - .map_err(|e| format!("Unable to create testnet directory: {:?}", e))?; - - macro_rules! write_bytes_to_file { - ($file: ident, $bytes: expr) => { - File::create(base_dir.join($file)) - .map_err(|e| format!("Unable to create {}: {:?}", $file, e)) - .and_then(|mut file| { - file.write_all($bytes) - .map_err(|e| format!("Unable to write bytes to {}: {}", $file, e)) - })?; - }; - } - - write_bytes_to_file!(YAML_CONFIG_FILE, HARDCODED_YAML_CONFIG); - write_bytes_to_file!(DEPLOY_BLOCK_FILE, HARDCODED_DEPLOY_BLOCK); - write_bytes_to_file!(ADDRESS_FILE, HARDCODED_DEPOSIT_CONTRACT); - write_bytes_to_file!(GENESIS_STATE_FILE, HARDCODED_GENESIS_STATE); - write_bytes_to_file!(BOOT_ENR_FILE, HARDCODED_BOOT_ENR); - - Self::load(base_dir) + // Creates the `Eth2TestnetDir` that was included in the binary at compile time. This can be + // considered the default Lighthouse testnet. + // + // Returns an error if those included bytes are invalid (this is unlikely). + pub fn hardcoded() -> Result { + Ok(Self { + deposit_contract_address: serde_yaml::from_reader(HARDCODED_DEPOSIT_CONTRACT) + .map_err(|e| format!("Unable to parse contract address: {:?}", e))?, + deposit_contract_deploy_block: serde_yaml::from_reader(HARDCODED_DEPLOY_BLOCK) + .map_err(|e| format!("Unable to parse deploy block: {:?}", e))?, + boot_enr: Some( + serde_yaml::from_reader(HARDCODED_BOOT_ENR) + .map_err(|e| format!("Unable to parse boot enr: {:?}", e))?, + ), + genesis_state: Some( + BeaconState::from_ssz_bytes(HARDCODED_GENESIS_STATE) + .map_err(|e| format!("Unable to parse genesis state: {:?}", e))?, + ), + yaml_config: Some( + serde_yaml::from_reader(HARDCODED_YAML_CONFIG) + .map_err(|e| format!("Unable to parse genesis state: {:?}", e))?, + ), + }) } // Write the files to the directory, only if the directory doesn't already exist. @@ -204,6 +199,16 @@ mod tests { type E = MinimalEthSpec; + #[test] + fn hardcoded_works() { + let dir: Eth2TestnetDir = + Eth2TestnetDir::hardcoded().expect("should decode hardcoded params"); + + assert!(dir.boot_enr.is_some()); + assert!(dir.genesis_state.is_some()); + assert!(dir.yaml_config.is_some()); + } + #[test] fn round_trip() { let spec = &E::default_spec();