Remove GenesisConfig, add BeaconChainStartMethod

This commit is contained in:
Paul Hauner 2019-08-25 09:43:03 +10:00
parent cdf3ade63f
commit 66d7838707
No known key found for this signature in database
GPG Key ID: 303E4494BB28068C
5 changed files with 46 additions and 53 deletions

View File

@ -1,6 +1,6 @@
use crate::bootstrapper::Bootstrapper;
use crate::error::Result;
use crate::{config::GenesisState, ClientConfig};
use crate::{config::BeaconChainStartMethod, ClientConfig};
use beacon_chain::{
lmd_ghost::{LmdGhost, ThreadSafeReducedTree},
slot_clock::SystemTimeSlotClock,
@ -59,19 +59,19 @@ where
T: BeaconChainTypes<Store = U, EthSpec = V>,
T::LmdGhost: LmdGhost<U, V>,
{
let genesis_state = match &config.genesis_state {
GenesisState::Mainnet => {
let genesis_state = match &config.beacon_chain_start_method {
BeaconChainStartMethod::Resume => {
crit!(log, "This release does not support mainnet genesis state.");
return Err("Mainnet is unsupported".into());
}
GenesisState::RecentGenesis { validator_count } => {
BeaconChainStartMethod::RecentGenesis { validator_count } => {
generate_testnet_genesis_state(*validator_count, recent_genesis_time(), &spec)
}
GenesisState::Generated {
BeaconChainStartMethod::Generated {
validator_count,
genesis_time,
} => generate_testnet_genesis_state(*validator_count, *genesis_time, &spec),
GenesisState::Yaml { file } => {
BeaconChainStartMethod::Yaml { file } => {
let file = File::open(file).map_err(|e| {
format!("Unable to open YAML genesis state file {:?}: {:?}", file, e)
})?;
@ -79,7 +79,7 @@ where
serde_yaml::from_reader(file)
.map_err(|e| format!("Unable to parse YAML genesis state file: {:?}", e))?
}
GenesisState::HttpBootstrap { server } => {
BeaconChainStartMethod::HttpBootstrap { server, .. } => {
let bootstrapper = Bootstrapper::from_server_string(server.to_string())
.map_err(|e| format!("Failed to initialize bootstrap client: {}", e))?;

View File

@ -1,15 +1,11 @@
use crate::Bootstrapper;
use clap::ArgMatches;
use network::NetworkConfig;
use serde_derive::{Deserialize, Serialize};
use slog::{info, o, warn, Drain};
use slog::{info, o, Drain};
use std::fs::{self, OpenOptions};
use std::path::PathBuf;
use std::sync::Mutex;
/// The number initial validators when starting the `Minimal`.
const TESTNET_VALIDATOR_COUNT: usize = 16;
/// The number initial validators when starting the `Minimal`.
const TESTNET_SPEC_CONSTANTS: &str = "minimal";
@ -21,63 +17,52 @@ pub struct Config {
db_name: String,
pub log_file: PathBuf,
pub spec_constants: String,
/// Defines how we should initialize a BeaconChain instances.
///
/// This field is not serialized, there for it will not be written to (or loaded from) config
/// files. It can only be configured via the CLI.
#[serde(skip)]
pub boot_method: BootMethod,
pub beacon_chain_start_method: BeaconChainStartMethod,
pub network: network::NetworkConfig,
pub rpc: rpc::RPCConfig,
pub rest_api: rest_api::ApiConfig,
}
/// Defines how the client should initialize a BeaconChain.
///
/// In general, there are two methods:
/// - resuming a new chain, or
/// - initializing a new one.
#[derive(Debug, Clone)]
pub enum BootMethod {
/// Resume from an existing database.
pub enum BeaconChainStartMethod {
/// Resume from an existing BeaconChain, loaded from the existing local database.
Resume,
/// Generate a state with `validator_count` validators, all with well-known secret keys.
/// Create a new beacon chain with `validator_count` validators, all with well-known secret keys.
///
/// Set the genesis time to be the start of the previous 30-minute window.
RecentGenesis { validator_count: usize },
/// Generate a state with `genesis_time` and `validator_count` validators, all with well-known
/// Create a new beacon chain with `genesis_time` and `validator_count` validators, all with well-known
/// secret keys.
Generated {
validator_count: usize,
genesis_time: u64,
},
/// Load a YAML-encoded genesis state from a file.
/// Create a new beacon chain by loading a YAML-encoded genesis state from a file.
Yaml { file: PathBuf },
/// Use a HTTP server (running our REST-API) to load genesis and finalized states and blocks.
/// Create a new beacon chain by using a HTTP server (running our REST-API) to load genesis and
/// finalized states and blocks.
HttpBootstrap {
server: String,
port: Option<String>,
},
}
impl Default for BootMethod {
impl Default for BeaconChainStartMethod {
fn default() -> Self {
BootMethod::Resume
BeaconChainStartMethod::Resume
}
}
pub enum GenesisState {
/// Use the mainnet genesis state.
///
/// Mainnet genesis state is not presently known, so this is a place-holder.
Mainnet,
/// Generate a state with `validator_count` validators, all with well-known secret keys.
///
/// Set the genesis time to be the start of the previous 30-minute window.
RecentGenesis { validator_count: usize },
/// Generate a state with `genesis_time` and `validator_count` validators, all with well-known
/// secret keys.
Generated {
validator_count: usize,
genesis_time: u64,
},
/// Load a YAML-encoded genesis state from a file.
Yaml { file: PathBuf },
/// Use a HTTP server (running our REST-API) to load genesis and finalized states and blocks.
HttpBootstrap { server: String },
}
impl Default for Config {
fn default() -> Self {
Self {
@ -86,10 +71,10 @@ impl Default for Config {
db_type: "disk".to_string(),
db_name: "chain_db".to_string(),
network: NetworkConfig::new(),
rpc: rpc::RPCConfig::default(),
rest_api: rest_api::ApiConfig::default(),
rpc: <_>::default(),
rest_api: <_>::default(),
spec_constants: TESTNET_SPEC_CONSTANTS.into(),
boot_method: BootMethod::default(),
beacon_chain_start_method: <_>::default(),
}
}
}

View File

@ -23,7 +23,7 @@ pub use beacon_chain::BeaconChainTypes;
pub use beacon_chain_types::ClientType;
pub use beacon_chain_types::InitialiseBeaconChain;
pub use bootstrapper::Bootstrapper;
pub use config::{Config as ClientConfig, GenesisState};
pub use config::Config as ClientConfig;
pub use eth2_config::Eth2Config;
/// Main beacon node client service. This provides the connection and initialisation of the clients

View File

@ -2,7 +2,7 @@ use clap::ArgMatches;
use client::{Bootstrapper, ClientConfig, Eth2Config};
use eth2_config::{read_from_file, write_to_file};
use rand::{distributions::Alphanumeric, Rng};
use slog::{crit, info, Logger};
use slog::{crit, info, warn, Logger};
use std::fs;
use std::path::PathBuf;
@ -35,15 +35,16 @@ pub fn get_configs(matches: &ArgMatches, log: &Logger) -> Result<Config> {
// The bootstrap testnet method requires inserting a libp2p address into the
// network config.
("bootstrap", Some(sub_matches)) => {
let server = sub_matches
let server: String = sub_matches
.value_of("server")
.ok_or_else(|| "No bootstrap server specified".into())?;
.ok_or_else(|| "No bootstrap server specified")?
.to_string();
let bootstrapper = Bootstrapper::from_server_string(server.to_string())?;
if let Some(server_multiaddr) =
bootstrapper.best_effort_multiaddr(sub_matches.value_of("libp2p_port"))
{
if let Some(server_multiaddr) = bootstrapper.best_effort_multiaddr(
parse_port_option(sub_matches.value_of("libp2p_port")),
) {
info!(
log,
"Estimated bootstrapper libp2p address";
@ -83,6 +84,11 @@ pub fn get_configs(matches: &ArgMatches, log: &Logger) -> Result<Config> {
builder.build()
}
/// Decodes an optional string into an optional u16.
fn parse_port_option(o: Option<&str>) -> Option<u16> {
o.and_then(|s| s.parse::<u16>().ok())
}
/// Allows for building a set of configurations based upon `clap` arguments.
struct ConfigBuilder<'a> {
matches: &'a ArgMatches<'a>,

View File

@ -227,7 +227,9 @@ fn main() {
.short("p")
.long("port")
.value_name("TCP_PORT")
.help("A libp2p listen port used to peer with the bootstrap server"))
.help("A libp2p listen port used to peer with the bootstrap server. This flag is useful \
when port-fowarding is used: you may connect using a different port than \
the one the server is immediately listening on."))
)
.subcommand(SubCommand::with_name("recent")
.about("Creates a new genesis state where the genesis time was at the previous \