Fix issues with testnet dir, update docs (#992)

* Fix issues with testnet dir, update docs

* Remove "simple testnet" docs

* Tear out old "bn testnet" stuff

* Add back ClientGenesis::Interop

* Tidy

* Remove lighthouse-bootstrap module

* Fix bug with spec constant mismatch

* Ensure beacon-node.toml is written to correct dir

* Add -t alias for --testnet-dir

* Update book/src/local-testnets.md

Co-Authored-By: Age Manning <Age@AgeManning.com>

* Add --purge CLI flag

* Update purge docs

* Perform manual delete of files in purge

* Rename --purge to --purge-db

* Address Michael's comments

Co-authored-by: Age Manning <Age@AgeManning.com>
This commit is contained in:
Paul Hauner
2020-04-17 17:49:29 +10:00
committed by GitHub
parent a8ee3389c2
commit 1a3d1b3077
23 changed files with 280 additions and 1139 deletions

View File

@@ -16,10 +16,7 @@ use eth1::{Config as Eth1Config, Service as Eth1Service};
use eth2_config::Eth2Config;
use exit_future::Signal;
use futures::{future, Future, IntoFuture};
use genesis::{
generate_deterministic_keypairs, interop_genesis_state, state_from_ssz_file, Eth1GenesisService,
};
use lighthouse_bootstrap::Bootstrapper;
use genesis::{interop_genesis_state, Eth1GenesisService};
use network::{NetworkConfig, NetworkMessage, Service as NetworkService};
use slog::info;
use ssz::Decode;
@@ -28,7 +25,7 @@ use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc::UnboundedSender;
use types::{BeaconState, ChainSpec, EthSpec};
use types::{test_utils::generate_deterministic_keypairs, BeaconState, ChainSpec, EthSpec};
use websocket_server::{Config as WebSocketConfig, WebSocketSender};
/// Interval between polling the eth1 node for genesis information.
@@ -161,12 +158,13 @@ where
//
// Alternatively, if there's a beacon chain in the database then always resume
// using it.
let client_genesis = if client_genesis == ClientGenesis::Resume && !chain_exists {
let client_genesis = if client_genesis == ClientGenesis::FromStore && !chain_exists
{
info!(context.log, "Defaulting to deposit contract genesis");
ClientGenesis::DepositContract
} else if chain_exists {
ClientGenesis::Resume
ClientGenesis::FromStore
} else {
client_genesis
};
@@ -187,16 +185,6 @@ where
Box::new(future)
}
ClientGenesis::SszFile { path } => {
let result = state_from_ssz_file(path);
let future = result
.and_then(move |genesis_state| builder.genesis_state(genesis_state))
.into_future()
.map(|v| (v, None));
Box::new(future)
}
ClientGenesis::SszBytes {
genesis_state_bytes,
} => {
@@ -235,25 +223,7 @@ where
Box::new(future)
}
ClientGenesis::RemoteNode { server, .. } => {
let future = Bootstrapper::connect(server, &context.log)
.map_err(|e| {
format!("Failed to initialize bootstrap client: {}", e)
})
.into_future()
.and_then(|bootstrapper| {
let (genesis_state, _genesis_block) =
bootstrapper.genesis().map_err(|e| {
format!("Failed to bootstrap genesis state: {}", e)
})?;
builder.genesis_state(genesis_state)
})
.map(|v| (v, None));
Box::new(future)
}
ClientGenesis::Resume => {
ClientGenesis::FromStore => {
let future = builder.resume_from_db().into_future().map(|v| (v, None));
Box::new(future)

View File

@@ -1,4 +1,3 @@
use beacon_chain::builder::PUBKEY_CACHE_FILENAME;
use network::NetworkConfig;
use serde_derive::{Deserialize, Serialize};
use std::fs;
@@ -12,32 +11,24 @@ const TESTNET_SPEC_CONSTANTS: &str = "minimal";
/// Default directory name for the freezer database under the top-level data dir.
const DEFAULT_FREEZER_DB_DIR: &str = "freezer_db";
/// Trap file indicating if chain_db was purged
const CHAIN_DB_PURGED_TRAP_FILE: &str = ".db_purged";
/// Defines how the client should initialize the `BeaconChain` and other components.
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
pub enum ClientGenesis {
/// Reads the genesis state and other persisted data from the `Store`.
Resume,
/// Creates a genesis state as per the 2019 Canada interop specifications.
Interop {
validator_count: usize,
genesis_time: u64,
},
/// Reads the genesis state and other persisted data from the `Store`.
FromStore,
/// Connects to an eth1 node and waits until it can create the genesis state from the deposit
/// contract.
DepositContract,
/// Loads the genesis state from a SSZ-encoded `BeaconState` file.
SszFile { path: PathBuf },
/// Loads the genesis state from SSZ-encoded `BeaconState` bytes.
///
/// We include the bytes instead of the `BeaconState<E>` because the `EthSpec` type
/// parameter would be very annoying.
SszBytes { genesis_state_bytes: Vec<u8> },
/// Connects to another Lighthouse instance and reads the genesis state and other data via the
/// HTTP API.
RemoteNode { server: String, port: Option<u16> },
}
impl Default for ClientGenesis {
@@ -101,68 +92,6 @@ impl Config {
.map(|data_dir| data_dir.join(&self.db_name))
}
/// Get the path of the chain db purged trap file
pub fn get_db_purged_trap_file_path(&self) -> Option<PathBuf> {
self.get_data_dir()
.map(|data_dir| data_dir.join(CHAIN_DB_PURGED_TRAP_FILE))
}
/// returns whether chain_db was recently purged
pub fn chain_db_was_purged(&self) -> bool {
self.get_db_purged_trap_file_path()
.map_or(false, |trap_file| trap_file.exists())
}
/// purges the chain_db and creates trap file
pub fn purge_chain_db(&self) -> Result<(), String> {
// create the trap file
let trap_file = self
.get_db_purged_trap_file_path()
.ok_or("Failed to get trap file path".to_string())?;
fs::File::create(trap_file)
.map_err(|err| format!("Failed to create trap file: {}", err))?;
// remove the chain_db
fs::remove_dir_all(
self.get_db_path()
.ok_or("Failed to get db_path".to_string())?,
)
.map_err(|err| format!("Failed to remove chain_db: {}", err))?;
// remove the freezer db
fs::remove_dir_all(
self.get_freezer_db_path()
.ok_or("Failed to get freezer db path".to_string())?,
)
.map_err(|err| format!("Failed to remove chain_db: {}", err))?;
// also need to remove pubkey cache file if it exists
let pubkey_cache_file = self
.get_data_dir()
.map(|data_dir| data_dir.join(PUBKEY_CACHE_FILENAME))
.ok_or("Failed to get pubkey cache file path".to_string())?;
if !pubkey_cache_file.exists() {
return Ok(());
}
fs::remove_file(pubkey_cache_file)
.map_err(|err| format!("Failed to remove pubkey cache: {}", err))?;
Ok(())
}
/// cleans up purge_db trap file
pub fn cleanup_after_purge_db(&self) -> Result<(), String> {
let trap_file = self
.get_db_purged_trap_file_path()
.ok_or("Failed to get trap file path".to_string())?;
if !trap_file.exists() {
return Ok(());
}
fs::remove_file(trap_file).map_err(|err| format!("Failed to remove trap file: {}", err))?;
Ok(())
}
/// Get the database path, creating it if necessary.
pub fn create_db_path(&self) -> Result<PathBuf, String> {
let db_path = self