Skip to content
Snippets Groups Projects
Commit 8c5f7744 authored by Chevdor's avatar Chevdor Committed by Gav Wood
Browse files

Fix export when starting from block 0 (#406)

* More explicit variable names

* Fix export when from block is 0

Fix #402
parent b65700d0
No related merge requests found
......@@ -376,18 +376,22 @@ fn export_blocks<E>(matches: &clap::ArgMatches, exit: E) -> error::Result<()>
let _ = exit.wait();
let _ = exit_send.send(());
});
info!("Exporting blocks");
let mut block: u32 = match matches.value_of("from") {
let mut from_block: u32 = match matches.value_of("from") {
Some(v) => v.parse().map_err(|_| "Invalid --from argument")?,
None => 1,
};
let last = match matches.value_of("to") {
if from_block < 1 {
from_block = 1;
}
let to_block = match matches.value_of("to") {
Some(v) => v.parse().map_err(|_| "Invalid --to argument")?,
None => client.info()?.chain.best_number as u32,
};
info!("Exporting blocks from #{} to #{}", from_block, to_block);
if last < block {
if to_block < from_block {
return Err("Invalid block range specified".into());
}
......@@ -399,30 +403,30 @@ fn export_blocks<E>(matches: &clap::ArgMatches, exit: E) -> error::Result<()>
};
if !json {
file.write(&(last - block + 1).encode())?;
file.write(&(to_block - from_block + 1).encode())?;
}
loop {
if exit_recv.try_recv().is_ok() {
break;
}
match client.block(&BlockId::number(block as u64))? {
Some(block) => {
match client.block(&BlockId::number(from_block as u64))? {
Some(from_block) => {
if json {
serde_json::to_writer(&mut *file, &block).map_err(|e| format!("Eror writing JSON: {}", e))?;
serde_json::to_writer(&mut *file, &from_block).map_err(|e| format!("Eror writing JSON: {}", e))?;
} else {
file.write(&block.encode())?;
file.write(&from_block.encode())?;
}
},
None => break,
}
if block % 10000 == 0 {
info!("#{}", block);
if from_block % 10000 == 0 {
info!("#{}", from_block);
}
if block == last {
if from_block == to_block {
break;
}
block += 1;
from_block += 1;
}
Ok(())
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment