From c197a51bc4f5bb6b9ec6e6d4abe1ee956cf81e33 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Mon, 25 Jun 2018 22:52:31 +0300 Subject: [PATCH] Support testnet network magic constant --- src/daemon.rs | 7 +++++++ src/parse.rs | 21 +++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 93c089f..4322a1c 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -228,6 +228,13 @@ impl Daemon { .collect()) } + pub fn magic(&self) -> u32 { + match self.network { + Network::Mainnet => 0xD9B4BEF9, + Network::Testnet => 0x0709110B, + } + } + fn call_jsonrpc(&self, method: &str, request: &Value) -> Result { let timer = self.latency.with_label_values(&[method]).start_timer(); let mut conn = self.conn.lock().unwrap(); diff --git a/src/parse.rs b/src/parse.rs index eb347c1..c7fc7fe 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -28,6 +28,7 @@ fn load_headers(daemon: &Daemon) -> Result { } pub struct Parser { + magic: u32, files: Vec, indexed_blockhashes: HashSet, current_headers: HeaderList, @@ -39,6 +40,7 @@ pub struct Parser { impl Parser { pub fn new(daemon: &Daemon, store: &ReadStore, metrics: &Metrics) -> Result { Ok(Parser { + magic: daemon.magic(), files: daemon.list_blk_files()?, indexed_blockhashes: read_indexed_blockhashes(store), current_headers: load_headers(daemon)?, @@ -78,7 +80,7 @@ impl Parser { pub fn start(mut self) -> Receiver>>> { let chan = SyncChannel::new(1); let tx = chan.sender(); - let parser = parse_files(self.files.split_off(0), self.duration.clone()); + let parser = parse_files(self.files.split_off(0), self.duration.clone(), self.magic); spawn_thread("bulk_indexer", move || { for blocks in parser.iter() { let rows = blocks.map(|b| self.index_blocks(&b)); @@ -98,7 +100,11 @@ impl Parser { } } -fn parse_files(files: Vec, duration: HistogramVec) -> Receiver>> { +fn parse_files( + files: Vec, + duration: HistogramVec, + magic: u32, +) -> Receiver>> { let chan = SyncChannel::new(1); let tx = chan.sender(); let blobs = read_files(files, duration.clone()); @@ -107,7 +113,7 @@ fn parse_files(files: Vec, duration: HistogramVec) -> Receiver { let timer = duration.with_label_values(&["parse"]).start_timer(); - let blocks = parse_blocks(&blob); + let blocks = parse_blocks(&blob, magic); timer.observe_duration(); tx.send(blocks).unwrap(); } @@ -138,7 +144,7 @@ fn read_files(files: Vec, duration: HistogramVec) -> Receiver Result> { +fn parse_blocks(blob: &[u8], magic: u32) -> Result> { let mut cursor = Cursor::new(&blob); let mut blocks = vec![]; let max_pos = blob.len() as u64; @@ -150,8 +156,11 @@ fn parse_blocks(blob: &[u8]) -> Result> { cursor = decoder.into_inner(); // skip zeroes continue; } - Ok(0xD9B4BEF9) => (), - Ok(x) => bail!("incorrect magic {:x} at {}", x, pos), + Ok(x) => { + if x != magic { + bail!("incorrect magic {:08x} at {}", x, pos) + } + } Err(_) => break, // EOF }; let block_size = decoder.read_u32().chain_err(|| "no block size")?;