lighthouse/tests/cli_util/src/parse_hex.rs

48 lines
1.4 KiB
Rust
Raw Normal View History

2019-09-09 16:21:41 +00:00
use clap::ArgMatches;
use serde::Serialize;
use ssz::{Decode, Encode};
use state_processing::{per_block_processing, per_slot_processing, BlockSignatureStrategy};
use std::fs::File;
use std::io::prelude::*;
use std::path::PathBuf;
use types::{BeaconBlock, BeaconState, EthSpec, MinimalEthSpec};
pub fn run_parse_hex(matches: &ArgMatches) -> Result<(), String> {
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))?;
info!("Using minimal spec");
info!("Type: {:?}", type_str);
match type_str.as_ref() {
"block" => decode_and_print::<BeaconBlock<MinimalEthSpec>>(&hex)?,
"state" => decode_and_print::<BeaconState<MinimalEthSpec>>(&hex)?,
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(())
}