Export formats and to file.

This commit is contained in:
Gav Wood 2016-05-23 18:42:59 +02:00
parent 564a996620
commit 6c64aec137
2 changed files with 27 additions and 3 deletions

View File

@ -148,6 +148,8 @@ Export Options:
hash [default: 0].
--to BLOCK Export to (including) block NUMBER, which may be an
index, hash or 'latest' [default: latest].
--format FORMAT Export in given format. FORMAT must be one of 'hex'
and 'binary' [default: hex].
Virtual Machine Options:
--jitvm Enable the JIT VM.
@ -241,6 +243,7 @@ pub struct Args {
pub flag_version: bool,
pub flag_from: String,
pub flag_to: String,
pub flag_format: String,
pub flag_jitvm: bool,
// legacy...
pub flag_geth: bool,

View File

@ -65,6 +65,7 @@ mod configuration;
use ctrlc::CtrlC;
use util::*;
use std::fs::File;
use util::panics::{MayPanic, ForwardPanic, PanicHandler};
use ethcore::client::{BlockID, BlockChainClient};
use ethcore::service::ClientService;
@ -222,6 +223,11 @@ fn flush_stdout() {
::std::io::stdout().flush().expect("stdout is flushable; qed");
}
enum DataFormat {
Hex,
Binary,
}
fn execute_export(conf: Configuration) {
println!("Exporting to {:?} from {}, to {}", conf.args.arg_file, conf.args.flag_from, conf.args.flag_to);
@ -265,14 +271,29 @@ fn execute_export(conf: Configuration) {
die!("Unknown block hash passed to --to parameter: {:?}", s);
})
} else {
die!("Invalid --to parameter given: {:?}", s);
die!("Invalid block ID parameter given: {:?}", s);
}
};
let from = parse_block_id(&conf.args.flag_from);
let to = parse_block_id(&conf.args.flag_to);
let format = match conf.args.flag_format.deref() {
"binary" | "bin" => DataFormat::Binary,
"hex" => DataFormat::Hex,
x => die!("Invalid --format parameter given: {:?}", x),
};
for i in from..to {
println!("{}", client.deref().block(BlockID::Number(i)).unwrap().pretty());
let mut out: Box<Write> = if let Some(f) = conf.args.arg_file {
Box::new(File::create(&f).unwrap_or_else(|_| die!("Cannot write to file given: {}", f)))
} else {
Box::new(::std::io::stdout())
};
for i in from..(to + 1) {
let b = client.deref().block(BlockID::Number(i)).unwrap();
match format {
DataFormat::Binary => { out.write(&b).expect("Couldn't write to stream."); }
DataFormat::Hex => { out.write_fmt(format_args!("{}", b.pretty())).expect("Couldn't write to stream."); }
}
}
}