Stop hardcoded testnet dir from creating dir

This commit is contained in:
Paul Hauner
2019-12-02 11:42:04 +11:00
parent 300fe0bf47
commit 637efccc47
6 changed files with 53 additions and 64 deletions

View File

@@ -3,7 +3,7 @@ mod cli;
use clap::ArgMatches; use clap::ArgMatches;
use deposit_contract::DEPOSIT_GAS; use deposit_contract::DEPOSIT_GAS;
use environment::{Environment, RuntimeContext}; use environment::{Environment, RuntimeContext};
use eth2_testnet::{Eth2TestnetDir, TempDir}; use eth2_testnet::Eth2TestnetDir;
use futures::{future, stream::unfold, Future, IntoFuture, Stream}; use futures::{future, stream::unfold, Future, IntoFuture, Stream};
use rayon::prelude::*; use rayon::prelude::*;
use slog::{crit, error, info, Logger}; use slog::{crit, error, info, Logger};
@@ -145,6 +145,7 @@ fn run_new_validator_subcommand<T: EthSpec>(
.parse::<usize>() .parse::<usize>()
.map_err(|e| format!("Unable to parse account-index: {}", e))?; .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") { let password = if let Some(password_path) = matches.value_of("password") {
Some( Some(
File::open(password_path) File::open(password_path)
@@ -174,6 +175,7 @@ fn run_new_validator_subcommand<T: EthSpec>(
"eth1_node_http_endpoint" => eth1_endpoint "eth1_node_http_endpoint" => eth1_endpoint
); );
// Load the testnet configuration from disk, or use the default testnet.
let eth2_testnet_dir: Eth2TestnetDir<T> = if let Some(testnet_dir_str) = let eth2_testnet_dir: Eth2TestnetDir<T> = if let Some(testnet_dir_str) =
matches.value_of("testnet-dir") matches.value_of("testnet-dir")
{ {
@@ -197,19 +199,8 @@ fn run_new_validator_subcommand<T: EthSpec>(
Eth2TestnetDir::load(testnet_dir.clone()) Eth2TestnetDir::load(testnet_dir.clone())
.map_err(|e| format!("Failed to load testnet dir at {:?}: {}", testnet_dir, e))? .map_err(|e| format!("Failed to load testnet dir at {:?}: {}", testnet_dir, e))?
} else { } else {
let temp_dir = TempDir::new("lighthouse-account-manager") Eth2TestnetDir::hardcoded()
.map_err(|e| format!("Unable to create temporary directory: {}", e))?; .map_err(|e| format!("Failed to load hardcoded testnet dir: {}", 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
)
})?
}; };
// Convert from `types::Address` to `web3::types::Address`. // Convert from `types::Address` to `web3::types::Address`.

View File

@@ -44,7 +44,7 @@ impl Default for ClientGenesis {
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config { pub struct Config {
pub data_dir: PathBuf, pub data_dir: PathBuf,
pub testnet_dir: PathBuf, pub testnet_dir: Option<PathBuf>,
pub db_type: String, pub db_type: String,
pub db_name: String, pub db_name: String,
pub freezer_db_path: Option<PathBuf>, pub freezer_db_path: Option<PathBuf>,
@@ -69,7 +69,7 @@ impl Default for Config {
fn default() -> Self { fn default() -> Self {
Self { Self {
data_dir: PathBuf::from(".lighthouse"), data_dir: PathBuf::from(".lighthouse"),
testnet_dir: PathBuf::from("testnet"), testnet_dir: None,
log_file: PathBuf::from(""), log_file: PathBuf::from(""),
db_type: "disk".to_string(), db_type: "disk".to_string(),
db_name: "chain_db".to_string(), db_name: "chain_db".to_string(),

View File

@@ -29,7 +29,8 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.long("testnet-dir") .long("testnet-dir")
.value_name("DIR") .value_name("DIR")
.help("Path to directory containing eth2_testnet specs. Defaults to \ .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) .takes_value(true)
) )
/* /*
@@ -40,7 +41,7 @@ pub fn cli_app<'a, 'b>() -> App<'a, 'b> {
.long("zero-ports") .long("zero-ports")
.short("z") .short("z")
.help("Sets all listening TCP/UDP ports to 0, allowing the OS to choose some \ .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), .takes_value(false),
) )
.arg( .arg(

View File

@@ -14,7 +14,6 @@ use types::{Epoch, EthSpec, Fork};
pub const CLIENT_CONFIG_FILENAME: &str = "beacon-node.toml"; pub const CLIENT_CONFIG_FILENAME: &str = "beacon-node.toml";
pub const ETH2_CONFIG_FILENAME: &str = "eth2-spec.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 BEACON_NODE_DIR: &str = "beacon";
pub const NETWORK_DIR: &str = "network"; pub const NETWORK_DIR: &str = "network";
@@ -64,13 +63,9 @@ pub fn get_configs<E: EthSpec>(
} }
// Read the `--testnet-dir` flag. // Read the `--testnet-dir` flag.
// if let Some(val) = cli_args.value_of("testnet-dir") {
// If it's not present, use the default dir. client_config.testnet_dir = Some(PathBuf::from(val));
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())?;
/* /*
* Networking * Networking
@@ -310,18 +305,13 @@ fn init_new_client<E: EthSpec>(
client_config: &mut ClientConfig, client_config: &mut ClientConfig,
eth2_config: &mut Eth2Config, eth2_config: &mut Eth2Config,
) -> Result<()> { ) -> Result<()> {
let testnet_dir = client_config.testnet_dir.clone(); let eth2_testnet_dir: Eth2TestnetDir<E> = if let Some(testnet_dir) = &client_config.testnet_dir
{
let eth2_testnet_dir: Eth2TestnetDir<E> = if testnet_dir.exists() {
Eth2TestnetDir::load(testnet_dir.clone()) Eth2TestnetDir::load(testnet_dir.clone())
.map_err(|e| format!("Unable to open testnet dir at {:?}: {}", testnet_dir, e))? .map_err(|e| format!("Unable to open testnet dir at {:?}: {}", testnet_dir, e))?
} else { } else {
Eth2TestnetDir::create_hardcoded(testnet_dir.clone()).map_err(|e| { Eth2TestnetDir::hardcoded()
format!( .map_err(|e| format!("Unable to load hard-coded testnet dir: {}", e))?
"Unable to create and open testnet dir at {:?}: {}",
testnet_dir, e
)
})?
}; };
eth2_config.spec = eth2_testnet_dir eth2_config.spec = eth2_testnet_dir

View File

@@ -6,10 +6,12 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dev-dependencies]
tempdir = "0.3"
[dependencies] [dependencies]
serde = "1.0" serde = "1.0"
serde_yaml = "0.8" serde_yaml = "0.8"
types = { path = "../../types"} types = { path = "../../types"}
eth2-libp2p = { path = "../../../beacon_node/eth2-libp2p"} eth2-libp2p = { path = "../../../beacon_node/eth2-libp2p"}
eth2_ssz = { path = "../ssz"} eth2_ssz = { path = "../ssz"}
tempdir = "0.3"

View File

@@ -14,8 +14,6 @@ use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use types::{Address, BeaconState, EthSpec, YamlConfig}; use types::{Address, BeaconState, EthSpec, YamlConfig};
pub use tempdir::TempDir;
pub const ADDRESS_FILE: &str = "deposit_contract.txt"; pub const ADDRESS_FILE: &str = "deposit_contract.txt";
pub const DEPLOY_BLOCK_FILE: &str = "deploy_block.txt"; pub const DEPLOY_BLOCK_FILE: &str = "deploy_block.txt";
pub const BOOT_ENR_FILE: &str = "boot_enr.yaml"; pub const BOOT_ENR_FILE: &str = "boot_enr.yaml";
@@ -38,32 +36,29 @@ pub struct Eth2TestnetDir<E: EthSpec> {
} }
impl<E: EthSpec> Eth2TestnetDir<E> { impl<E: EthSpec> Eth2TestnetDir<E> {
pub fn create_hardcoded(base_dir: PathBuf) -> Result<Self, String> { // Creates the `Eth2TestnetDir` that was included in the binary at compile time. This can be
if base_dir.exists() { // considered the default Lighthouse testnet.
return Err("Testnet directory already exists".to_string()); //
} // Returns an error if those included bytes are invalid (this is unlikely).
pub fn hardcoded() -> Result<Self, String> {
create_dir_all(&base_dir) Ok(Self {
.map_err(|e| format!("Unable to create testnet directory: {:?}", e))?; deposit_contract_address: serde_yaml::from_reader(HARDCODED_DEPOSIT_CONTRACT)
.map_err(|e| format!("Unable to parse contract address: {:?}", e))?,
macro_rules! write_bytes_to_file { deposit_contract_deploy_block: serde_yaml::from_reader(HARDCODED_DEPLOY_BLOCK)
($file: ident, $bytes: expr) => { .map_err(|e| format!("Unable to parse deploy block: {:?}", e))?,
File::create(base_dir.join($file)) boot_enr: Some(
.map_err(|e| format!("Unable to create {}: {:?}", $file, e)) serde_yaml::from_reader(HARDCODED_BOOT_ENR)
.and_then(|mut file| { .map_err(|e| format!("Unable to parse boot enr: {:?}", e))?,
file.write_all($bytes) ),
.map_err(|e| format!("Unable to write bytes to {}: {}", $file, e)) genesis_state: Some(
})?; BeaconState::from_ssz_bytes(HARDCODED_GENESIS_STATE)
}; .map_err(|e| format!("Unable to parse genesis state: {:?}", e))?,
} ),
yaml_config: Some(
write_bytes_to_file!(YAML_CONFIG_FILE, HARDCODED_YAML_CONFIG); serde_yaml::from_reader(HARDCODED_YAML_CONFIG)
write_bytes_to_file!(DEPLOY_BLOCK_FILE, HARDCODED_DEPLOY_BLOCK); .map_err(|e| format!("Unable to parse genesis state: {:?}", e))?,
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)
} }
// Write the files to the directory, only if the directory doesn't already exist. // Write the files to the directory, only if the directory doesn't already exist.
@@ -204,6 +199,16 @@ mod tests {
type E = MinimalEthSpec; type E = MinimalEthSpec;
#[test]
fn hardcoded_works() {
let dir: Eth2TestnetDir<E> =
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] #[test]
fn round_trip() { fn round_trip() {
let spec = &E::default_spec(); let spec = &E::default_spec();