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 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<T: EthSpec>(
.parse::<usize>()
.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<T: EthSpec>(
"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) =
matches.value_of("testnet-dir")
{
@@ -197,19 +199,8 @@ fn run_new_validator_subcommand<T: EthSpec>(
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`.

View File

@@ -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<PathBuf>,
pub db_type: String,
pub db_name: String,
pub freezer_db_path: Option<PathBuf>,
@@ -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(),

View File

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

View File

@@ -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<E: EthSpec>(
}
// 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<E: EthSpec>(
client_config: &mut ClientConfig,
eth2_config: &mut Eth2Config,
) -> Result<()> {
let testnet_dir = client_config.testnet_dir.clone();
let eth2_testnet_dir: Eth2TestnetDir<E> = if testnet_dir.exists() {
let eth2_testnet_dir: Eth2TestnetDir<E> = 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

View File

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

View File

@@ -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<E: EthSpec> {
}
impl<E: EthSpec> Eth2TestnetDir<E> {
pub fn create_hardcoded(base_dir: PathBuf) -> Result<Self, String> {
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<Self, String> {
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<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]
fn round_trip() {
let spec = &E::default_spec();