From 70f4052b2e67e0d7f14630f4e24c2c45fea54892 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 2 Sep 2019 15:58:53 +1000 Subject: [PATCH] Allow starting testnet from JSON state --- beacon_node/beacon_chain/Cargo.toml | 1 + beacon_node/beacon_chain/src/beacon_chain_builder.rs | 10 ++++++++++ beacon_node/client/src/config.rs | 2 ++ beacon_node/client/src/lib.rs | 9 +++++++++ beacon_node/src/config.rs | 1 + beacon_node/src/main.rs | 4 ++-- 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/beacon_node/beacon_chain/Cargo.toml b/beacon_node/beacon_chain/Cargo.toml index d5594a49a..ae89ac1e1 100644 --- a/beacon_node/beacon_chain/Cargo.toml +++ b/beacon_node/beacon_chain/Cargo.toml @@ -18,6 +18,7 @@ rayon = "1.0" serde = "1.0" serde_derive = "1.0" serde_yaml = "0.8" +serde_json = "^1.0" slog = { version = "^2.2.3" , features = ["max_level_trace"] } sloggers = { version = "^0.3" } slot_clock = { path = "../../eth2/utils/slot_clock" } diff --git a/beacon_node/beacon_chain/src/beacon_chain_builder.rs b/beacon_node/beacon_chain/src/beacon_chain_builder.rs index 514a72a40..93c67447e 100644 --- a/beacon_node/beacon_chain/src/beacon_chain_builder.rs +++ b/beacon_node/beacon_chain/src/beacon_chain_builder.rs @@ -76,6 +76,16 @@ impl BeaconChainBuilder { Ok(Self::from_genesis_state(genesis_state, spec, log)) } + pub fn json_state(file: &PathBuf, spec: ChainSpec, log: Logger) -> Result { + let file = File::open(file.clone()) + .map_err(|e| format!("Unable to open JSON genesis state file {:?}: {:?}", file, e))?; + + let genesis_state = serde_json::from_reader(file) + .map_err(|e| format!("Unable to parse JSON genesis state file: {:?}", e))?; + + Ok(Self::from_genesis_state(genesis_state, spec, log)) + } + pub fn http_bootstrap(server: &str, spec: ChainSpec, log: Logger) -> Result { let bootstrapper = Bootstrapper::from_server_string(server.to_string()) .map_err(|e| format!("Failed to initialize bootstrap client: {}", e))?; diff --git a/beacon_node/client/src/config.rs b/beacon_node/client/src/config.rs index 2fb62c3f9..f9b366eb1 100644 --- a/beacon_node/client/src/config.rs +++ b/beacon_node/client/src/config.rs @@ -56,6 +56,8 @@ pub enum BeaconChainStartMethod { Yaml { file: PathBuf }, /// Create a new beacon chain by loading a SSZ-encoded genesis state from a file. Ssz { file: PathBuf }, + /// Create a new beacon chain by loading a JSON-encoded genesis state from a file. + Json { file: PathBuf }, /// 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 }, diff --git a/beacon_node/client/src/lib.rs b/beacon_node/client/src/lib.rs index 1396ed45f..e14da2af9 100644 --- a/beacon_node/client/src/lib.rs +++ b/beacon_node/client/src/lib.rs @@ -147,6 +147,15 @@ where ); BeaconChainBuilder::ssz_state(file, spec.clone(), log.clone())? } + BeaconChainStartMethod::Json { file } => { + info!( + log, + "Starting beacon chain"; + "file" => format!("{:?}", file), + "method" => "json" + ); + BeaconChainBuilder::json_state(file, spec.clone(), log.clone())? + } BeaconChainStartMethod::HttpBootstrap { server, port } => { info!( log, diff --git a/beacon_node/src/config.rs b/beacon_node/src/config.rs index ba831c733..4a3f6b6a7 100644 --- a/beacon_node/src/config.rs +++ b/beacon_node/src/config.rs @@ -186,6 +186,7 @@ fn process_testnet_subcommand( let start_method = match format { "yaml" => BeaconChainStartMethod::Yaml { file }, "ssz" => BeaconChainStartMethod::Ssz { file }, + "json" => BeaconChainStartMethod::Json { file }, other => return Err(format!("Unknown genesis file format: {}", other)), }; diff --git a/beacon_node/src/main.rs b/beacon_node/src/main.rs index 6ca85bd56..b914be549 100644 --- a/beacon_node/src/main.rs +++ b/beacon_node/src/main.rs @@ -318,7 +318,7 @@ fn main() { .arg(Arg::with_name("format") .value_name("FORMAT") .required(true) - .possible_values(&["yaml", "ssz"]) + .possible_values(&["yaml", "ssz", "json"]) .help("The encoding of the state in the file.")) .arg(Arg::with_name("file") .value_name("YAML_FILE") @@ -344,7 +344,7 @@ fn main() { _ => unreachable!("guarded by clap"), }; - let mut log = slog::Logger::root(drain.fuse(), o!()); + let log = slog::Logger::root(drain.fuse(), o!()); warn!( log,