Allow starting testnet from JSON state

This commit is contained in:
Paul Hauner 2019-09-02 15:58:53 +10:00
parent ba22d28026
commit 70f4052b2e
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
6 changed files with 25 additions and 2 deletions

View File

@ -18,6 +18,7 @@ rayon = "1.0"
serde = "1.0" serde = "1.0"
serde_derive = "1.0" serde_derive = "1.0"
serde_yaml = "0.8" serde_yaml = "0.8"
serde_json = "^1.0"
slog = { version = "^2.2.3" , features = ["max_level_trace"] } slog = { version = "^2.2.3" , features = ["max_level_trace"] }
sloggers = { version = "^0.3" } sloggers = { version = "^0.3" }
slot_clock = { path = "../../eth2/utils/slot_clock" } slot_clock = { path = "../../eth2/utils/slot_clock" }

View File

@ -76,6 +76,16 @@ impl<T: BeaconChainTypes> BeaconChainBuilder<T> {
Ok(Self::from_genesis_state(genesis_state, spec, log)) Ok(Self::from_genesis_state(genesis_state, spec, log))
} }
pub fn json_state(file: &PathBuf, spec: ChainSpec, log: Logger) -> Result<Self, String> {
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<Self, String> { pub fn http_bootstrap(server: &str, spec: ChainSpec, log: Logger) -> Result<Self, String> {
let bootstrapper = Bootstrapper::from_server_string(server.to_string()) let bootstrapper = Bootstrapper::from_server_string(server.to_string())
.map_err(|e| format!("Failed to initialize bootstrap client: {}", e))?; .map_err(|e| format!("Failed to initialize bootstrap client: {}", e))?;

View File

@ -56,6 +56,8 @@ pub enum BeaconChainStartMethod {
Yaml { file: PathBuf }, Yaml { file: PathBuf },
/// Create a new beacon chain by loading a SSZ-encoded genesis state from a file. /// Create a new beacon chain by loading a SSZ-encoded genesis state from a file.
Ssz { file: PathBuf }, 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 /// Create a new beacon chain by using a HTTP server (running our REST-API) to load genesis and
/// finalized states and blocks. /// finalized states and blocks.
HttpBootstrap { server: String, port: Option<u16> }, HttpBootstrap { server: String, port: Option<u16> },

View File

@ -147,6 +147,15 @@ where
); );
BeaconChainBuilder::ssz_state(file, spec.clone(), log.clone())? 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 } => { BeaconChainStartMethod::HttpBootstrap { server, port } => {
info!( info!(
log, log,

View File

@ -186,6 +186,7 @@ fn process_testnet_subcommand(
let start_method = match format { let start_method = match format {
"yaml" => BeaconChainStartMethod::Yaml { file }, "yaml" => BeaconChainStartMethod::Yaml { file },
"ssz" => BeaconChainStartMethod::Ssz { file }, "ssz" => BeaconChainStartMethod::Ssz { file },
"json" => BeaconChainStartMethod::Json { file },
other => return Err(format!("Unknown genesis file format: {}", other)), other => return Err(format!("Unknown genesis file format: {}", other)),
}; };

View File

@ -318,7 +318,7 @@ fn main() {
.arg(Arg::with_name("format") .arg(Arg::with_name("format")
.value_name("FORMAT") .value_name("FORMAT")
.required(true) .required(true)
.possible_values(&["yaml", "ssz"]) .possible_values(&["yaml", "ssz", "json"])
.help("The encoding of the state in the file.")) .help("The encoding of the state in the file."))
.arg(Arg::with_name("file") .arg(Arg::with_name("file")
.value_name("YAML_FILE") .value_name("YAML_FILE")
@ -344,7 +344,7 @@ fn main() {
_ => unreachable!("guarded by clap"), _ => unreachable!("guarded by clap"),
}; };
let mut log = slog::Logger::root(drain.fuse(), o!()); let log = slog::Logger::root(drain.fuse(), o!());
warn!( warn!(
log, log,