2019-09-09 16:21:41 +00:00
|
|
|
use clap::ArgMatches;
|
|
|
|
use serde::Serialize;
|
2019-09-09 16:29:50 +00:00
|
|
|
use ssz::Decode;
|
2019-12-09 06:23:56 +00:00
|
|
|
use types::{BeaconBlock, BeaconState, EthSpec};
|
2019-09-09 16:21:41 +00:00
|
|
|
|
2019-12-09 06:23:56 +00:00
|
|
|
pub fn run_parse_hex<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
|
2019-09-09 16:21:41 +00:00
|
|
|
let type_str = matches
|
|
|
|
.value_of("type")
|
|
|
|
.ok_or_else(|| "No type supplied".to_string())?;
|
|
|
|
let mut hex: String = matches
|
|
|
|
.value_of("hex_ssz")
|
|
|
|
.ok_or_else(|| "No hex ssz supplied".to_string())?
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
if hex.starts_with("0x") {
|
|
|
|
hex = hex[2..].to_string();
|
|
|
|
}
|
|
|
|
|
|
|
|
let hex = hex::decode(&hex).map_err(|e| format!("Failed to parse hex: {:?}", e))?;
|
|
|
|
|
2020-05-19 03:49:23 +00:00
|
|
|
info!("Using {} spec", T::spec_name());
|
2019-09-09 16:21:41 +00:00
|
|
|
info!("Type: {:?}", type_str);
|
|
|
|
|
2019-09-30 03:58:45 +00:00
|
|
|
match type_str {
|
2019-12-09 06:23:56 +00:00
|
|
|
"block" => decode_and_print::<BeaconBlock<T>>(&hex)?,
|
|
|
|
"state" => decode_and_print::<BeaconState<T>>(&hex)?,
|
2019-09-09 16:21:41 +00:00
|
|
|
other => return Err(format!("Unknown type: {}", other)),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decode_and_print<T: Decode + Serialize>(bytes: &[u8]) -> Result<(), String> {
|
|
|
|
let item = T::from_ssz_bytes(&bytes).map_err(|e| format!("Ssz decode failed: {:?}", e))?;
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"{}",
|
|
|
|
serde_yaml::to_string(&item)
|
|
|
|
.map_err(|e| format!("Unable to write object to YAML: {:?}", e))?
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|