Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with testnet dir, update docs #992

Merged
merged 16 commits into from
Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 2 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ members = [
"eth2/utils/logging",
"eth2/utils/eth2_hashing",
"eth2/utils/lighthouse_metrics",
"eth2/utils/lighthouse_bootstrap",
"eth2/utils/merkle_proof",
"eth2/utils/int_to_bytes",
"eth2/utils/serde_hex",
Expand Down
3 changes: 2 additions & 1 deletion beacon_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ node_test_rig = { path = "../tests/node_test_rig" }

[dependencies]
eth2_config = { path = "../eth2/utils/eth2_config" }
lighthouse_bootstrap = { path = "../eth2/utils/lighthouse_bootstrap" }
beacon_chain = { path = "beacon_chain" }
types = { path = "../eth2/types" }
store = { path = "./store" }
Expand All @@ -37,3 +36,5 @@ genesis = { path = "genesis" }
eth2_testnet_config = { path = "../eth2/utils/eth2_testnet_config" }
eth2-libp2p = { path = "./eth2-libp2p" }
eth2_ssz = { path = "../eth2/utils/ssz" }
toml = "0.5.4"
serde = "1.0.102"
1 change: 0 additions & 1 deletion beacon_node/beacon_chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ store = { path = "../store" }
parking_lot = "0.9.0"
lazy_static = "1.4.0"
lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" }
lighthouse_bootstrap = { path = "../../eth2/utils/lighthouse_bootstrap" }
log = "0.4.8"
operation_pool = { path = "../../eth2/operation_pool" }
rayon = "1.2.0"
Expand Down
1 change: 0 additions & 1 deletion beacon_node/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ url = "2.1.0"
eth1 = { path = "../eth1" }
genesis = { path = "../genesis" }
environment = { path = "../../lighthouse/environment" }
lighthouse_bootstrap = { path = "../../eth2/utils/lighthouse_bootstrap" }
eth2_ssz = { path = "../../eth2/utils/ssz" }
lazy_static = "1.4.0"
lighthouse_metrics = { path = "../../eth2/utils/lighthouse_metrics" }
42 changes: 6 additions & 36 deletions beacon_node/client/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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
};
Expand All @@ -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,
} => {
Expand Down Expand Up @@ -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)
Expand Down
75 changes: 2 additions & 73 deletions beacon_node/client/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use beacon_chain::builder::PUBKEY_CACHE_FILENAME;
use network::NetworkConfig;
use serde_derive::{Deserialize, Serialize};
use std::fs;
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions beacon_node/genesis/src/interop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use eth2_hashing::hash;
use rayon::prelude::*;
use ssz::Encode;
use state_processing::initialize_beacon_state_from_eth1;
use std::time::SystemTime;
use types::{BeaconState, ChainSpec, DepositData, EthSpec, Hash256, Keypair, PublicKey, Signature};

/// Builds a genesis state as defined by the Eth2 interop procedure (see below).
Expand Down Expand Up @@ -57,18 +56,6 @@ pub fn interop_genesis_state<T: EthSpec>(
Ok(state)
}

/// Returns the system time, mod 30 minutes.
///
/// Used for easily creating testnets.
pub fn recent_genesis_time(minutes: u64) -> u64 {
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
let secs_after_last_period = now.checked_rem(minutes * 60).unwrap_or(0);
now - secs_after_last_period
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
25 changes: 1 addition & 24 deletions beacon_node/genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,5 @@ mod interop;

pub use eth1::Config as Eth1Config;
pub use eth1_genesis_service::Eth1GenesisService;
pub use interop::{interop_genesis_state, recent_genesis_time};
pub use interop::interop_genesis_state;
pub use types::test_utils::generate_deterministic_keypairs;

use ssz::Decode;
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use types::{BeaconState, EthSpec};

/// Load a `BeaconState` from the given `path`. The file should contain raw SSZ bytes (i.e., no
/// ASCII encoding or schema).
pub fn state_from_ssz_file<E: EthSpec>(path: PathBuf) -> Result<BeaconState<E>, String> {
File::open(path.clone())
.map_err(move |e| format!("Unable to open SSZ genesis state file {:?}: {:?}", path, e))
.and_then(|mut file| {
let mut bytes = vec![];
file.read_to_end(&mut bytes)
.map_err(|e| format!("Failed to read SSZ file: {:?}", e))?;
Ok(bytes)
})
.and_then(|bytes| {
BeaconState::from_ssz_bytes(&bytes)
.map_err(|e| format!("Unable to parse SSZ genesis state file: {:?}", e))
})
}
Loading