mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-22 22:34:45 +00:00
Merge latest master
This commit is contained in:
@@ -29,6 +29,14 @@ pub fn parse_path_with_default_in_home_dir(
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse_path(matches: &ArgMatches, name: &'static str) -> Result<PathBuf, String> {
|
||||
matches
|
||||
.value_of(name)
|
||||
.ok_or_else(|| format!("{} not specified", name))?
|
||||
.parse::<PathBuf>()
|
||||
.map_err(|e| format!("Unable to parse {}: {}", name, e))
|
||||
}
|
||||
|
||||
pub fn parse_u64(matches: &ArgMatches, name: &'static str) -> Result<u64, String> {
|
||||
matches
|
||||
.value_of(name)
|
||||
|
||||
40
lcli/src/parse_ssz.rs
Normal file
40
lcli/src/parse_ssz.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use crate::helpers::parse_path;
|
||||
use clap::ArgMatches;
|
||||
use serde::Serialize;
|
||||
use ssz::Decode;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use types::{EthSpec, SignedBeaconBlock};
|
||||
|
||||
pub fn run<T: EthSpec>(matches: &ArgMatches) -> Result<(), String> {
|
||||
let type_str = matches
|
||||
.value_of("type")
|
||||
.ok_or_else(|| "No type supplied".to_string())?;
|
||||
let path = parse_path(matches, "path")?;
|
||||
|
||||
info!("Type: {:?}", type_str);
|
||||
|
||||
let mut bytes = vec![];
|
||||
let mut file = File::open(&path).map_err(|e| format!("Unable to open {:?}: {}", path, e))?;
|
||||
file.read_to_end(&mut bytes)
|
||||
.map_err(|e| format!("Unable to read {:?}: {}", path, e))?;
|
||||
|
||||
match type_str {
|
||||
"SignedBeaconBlock" => decode_and_print::<SignedBeaconBlock<T>>(&bytes)?,
|
||||
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(())
|
||||
}
|
||||
Reference in New Issue
Block a user