Browse Source

Test blk*.dat parsing logic

tmp/parse_blk
Roman Zeyde 6 years ago
parent
commit
ac6da9481c
No known key found for this signature in database GPG Key ID: 87CAE5FA46917CBB
  1. 26
      examples/parse_blk.rs
  2. 5
      src/bulk.rs

26
examples/parse_blk.rs

@ -0,0 +1,26 @@
/// Test blk*.dat parsing.
extern crate electrs;
extern crate error_chain;
extern crate stderrlog;
use electrs::{bulk, errors::*};
fn run(files: Vec<String>, magic: u32) -> Result<()> {
for f in files {
dbg!(&f);
let blob = std::fs::read(&f).expect("failed to read file");
let blocks = bulk::parse_blocks(blob, magic)?;
dbg!(blocks.len());
}
Ok(())
}
fn main() {
let mut log = stderrlog::new();
log.verbosity(4);
log.timestamp(stderrlog::Timestamp::Millisecond);
log.init().expect("logging initialization failed");
run(std::env::args().skip(1).collect(), 0xD9B4BEF9).expect("failed to parse");
}

5
src/bulk.rs

@ -115,7 +115,7 @@ impl Parser {
}
}
fn parse_blocks(blob: Vec<u8>, magic: u32) -> Result<Vec<Block>> {
pub fn parse_blocks(blob: Vec<u8>, magic: u32) -> Result<Vec<Block>> {
let mut cursor = Cursor::new(&blob);
let mut blocks = vec![];
let max_pos = blob.len() as u64;
@ -124,6 +124,7 @@ fn parse_blocks(blob: Vec<u8>, magic: u32) -> Result<Vec<Block>> {
match u32::consensus_decode(&mut cursor) {
Ok(value) => {
if magic != value {
warn!("incorrect magic");
cursor.set_position(offset + 1);
continue;
}
@ -141,12 +142,14 @@ fn parse_blocks(blob: Vec<u8>, magic: u32) -> Result<Vec<Block>> {
match u32::consensus_decode(&mut cursor) {
Ok(value) => {
if magic == value {
warn!("magic instead of version");
cursor.set_position(start);
continue;
}
}
Err(_) => break, // EOF
}
trace!("parsing block [{},{}) = {} bytes", start, end, block_size);
let block: Block = deserialize(&blob[start as usize..end as usize])
.chain_err(|| format!("failed to parse block at {}..{}", start, end))?;
blocks.push(block);

Loading…
Cancel
Save