diff --git a/hintfile/Cargo.toml b/hintfile/Cargo.toml index 639c434..b36597f 100644 --- a/hintfile/Cargo.toml +++ b/hintfile/Cargo.toml @@ -4,7 +4,6 @@ version = "0.1.0" edition = "2021" [dependencies] -bitcoin = { workspace = true } kernel = { workspace = true } [[bin]] diff --git a/hintfile/src/lib.rs b/hintfile/src/lib.rs index efb65d1..477e78d 100644 --- a/hintfile/src/lib.rs +++ b/hintfile/src/lib.rs @@ -3,7 +3,8 @@ use std::{ io::{self, Read, Write}, }; -use bitcoin::{consensus, BlockHash, BlockHeight, BlockHeightInterval}; +type BlockHash = [u8; 32]; +type BlockHeight = u32; pub fn write_compact_size(value: u64, writer: &mut W) -> Result<(), io::Error> { match value { @@ -54,10 +55,9 @@ impl Hints { // Panics when expected data is not present, or the hintfile overflows the maximum blockheight pub fn from_file(reader: &mut R) -> Self { let mut map = BTreeMap::new(); - let mut height = BlockHeight::from_u32(1); - let mut buf = [0; 32]; - reader.read_exact(&mut buf).expect("empty file"); - let assume_valid = consensus::deserialize::(&buf).expect("empty file."); + let mut height = 1; + let mut assume_valid = [0; 32]; + reader.read_exact(&mut assume_valid).expect("empty file"); while let Ok(count) = read_compact_size(reader) { // panics on 32 bit machines let mut offsets = Vec::with_capacity(count as usize); @@ -65,9 +65,7 @@ impl Hints { offsets.push(read_compact_size(reader).expect("unexpected end of hintfile")); } map.insert(height, offsets); - height = height - .checked_add(BlockHeightInterval::from_u32(1)) - .expect("hintfile absurdly large.") + height += 1; } Self { map, assume_valid } } diff --git a/node/src/bin/ibd.rs b/node/src/bin/ibd.rs index 5d0ece6..fc43610 100644 --- a/node/src/bin/ibd.rs +++ b/node/src/bin/ibd.rs @@ -5,7 +5,7 @@ use std::{ time::Instant, }; -use bitcoin::Network; +use bitcoin::{consensus, BlockHash, Network}; use hintfile::Hints; use kernel::{ChainType, ChainstateManager, ChainstateManagerOptions, ContextBuilder}; @@ -33,7 +33,8 @@ fn main() { elapsed_time(hintfile_start_time); let block_file_path = Path::new(BLOCK_FILE_PATH); std::fs::create_dir(block_file_path).expect("could not create block file directory"); - let stop_hash = hints.stop_hash(); + let stop_hash = + consensus::deserialize::(&hints.stop_hash()).expect("stop hash is not valid"); tracing::info!("Assume valid hash: {stop_hash}"); tracing::info!("Finding peers with DNS"); let dns_start_time = Instant::now(); diff --git a/node/src/lib.rs b/node/src/lib.rs index 20bee5b..0bb5fd5 100644 --- a/node/src/lib.rs +++ b/node/src/lib.rs @@ -17,7 +17,7 @@ use bitcoin::{ key::rand::{seq::SliceRandom, thread_rng}, script::ScriptExt, transaction::TransactionExt, - BlockHash, BlockHeight, Network, OutPoint, + BlockHash, Network, OutPoint, }; use hintfile::Hints; use kernel::ChainstateManager; @@ -202,10 +202,10 @@ pub fn get_blocks_for_range( let kernal_hash: kernel::BlockHash = kernel::BlockHash { hash: hash.to_byte_array(), }; - let height = chain + let block_index = chain .block_index_by_hash(kernal_hash) .expect("header is in best chain."); - let block_height = BlockHeight::from_u32(height.height().unsigned_abs()); + let block_height = block_index.height().unsigned_abs(); let unspent_indexes: HashSet = hints.get_block_offsets(block_height).into_iter().collect(); // tracing::info!("{task_id} -> {block_height}:{hash}");