Skip to content

Commit de3f7be

Browse files
authored
Merge pull request #37 from rustaceanrob/8-18-hintfile-dep
Remove `bitcoin` from `hintfile`
2 parents 2b8bc66 + fa6b8e3 commit de3f7be

File tree

4 files changed

+12
-14
lines changed

4 files changed

+12
-14
lines changed

hintfile/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
bitcoin = { workspace = true }
87
kernel = { workspace = true }
98

109
[[bin]]

hintfile/src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::{
33
io::{self, Read, Write},
44
};
55

6-
use bitcoin::{consensus, BlockHash, BlockHeight, BlockHeightInterval};
6+
type BlockHash = [u8; 32];
7+
type BlockHeight = u32;
78

89
pub fn write_compact_size<W: Write>(value: u64, writer: &mut W) -> Result<(), io::Error> {
910
match value {
@@ -54,20 +55,17 @@ impl Hints {
5455
// Panics when expected data is not present, or the hintfile overflows the maximum blockheight
5556
pub fn from_file<R: Read>(reader: &mut R) -> Self {
5657
let mut map = BTreeMap::new();
57-
let mut height = BlockHeight::from_u32(1);
58-
let mut buf = [0; 32];
59-
reader.read_exact(&mut buf).expect("empty file");
60-
let assume_valid = consensus::deserialize::<BlockHash>(&buf).expect("empty file.");
58+
let mut height = 1;
59+
let mut assume_valid = [0; 32];
60+
reader.read_exact(&mut assume_valid).expect("empty file");
6161
while let Ok(count) = read_compact_size(reader) {
6262
// panics on 32 bit machines
6363
let mut offsets = Vec::with_capacity(count as usize);
6464
for _ in 0..count {
6565
offsets.push(read_compact_size(reader).expect("unexpected end of hintfile"));
6666
}
6767
map.insert(height, offsets);
68-
height = height
69-
.checked_add(BlockHeightInterval::from_u32(1))
70-
.expect("hintfile absurdly large.")
68+
height += 1;
7169
}
7270
Self { map, assume_valid }
7371
}

node/src/bin/ibd.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
time::Instant,
66
};
77

8-
use bitcoin::Network;
8+
use bitcoin::{consensus, BlockHash, Network};
99
use hintfile::Hints;
1010
use kernel::{ChainType, ChainstateManager, ChainstateManagerOptions, ContextBuilder};
1111

@@ -33,7 +33,8 @@ fn main() {
3333
elapsed_time(hintfile_start_time);
3434
let block_file_path = Path::new(BLOCK_FILE_PATH);
3535
std::fs::create_dir(block_file_path).expect("could not create block file directory");
36-
let stop_hash = hints.stop_hash();
36+
let stop_hash =
37+
consensus::deserialize::<BlockHash>(&hints.stop_hash()).expect("stop hash is not valid");
3738
tracing::info!("Assume valid hash: {stop_hash}");
3839
tracing::info!("Finding peers with DNS");
3940
let dns_start_time = Instant::now();

node/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bitcoin::{
1717
key::rand::{seq::SliceRandom, thread_rng},
1818
script::ScriptExt,
1919
transaction::TransactionExt,
20-
BlockHash, BlockHeight, Network, OutPoint,
20+
BlockHash, Network, OutPoint,
2121
};
2222
use hintfile::Hints;
2323
use kernel::ChainstateManager;
@@ -202,10 +202,10 @@ pub fn get_blocks_for_range(
202202
let kernal_hash: kernel::BlockHash = kernel::BlockHash {
203203
hash: hash.to_byte_array(),
204204
};
205-
let height = chain
205+
let block_index = chain
206206
.block_index_by_hash(kernal_hash)
207207
.expect("header is in best chain.");
208-
let block_height = BlockHeight::from_u32(height.height().unsigned_abs());
208+
let block_height = block_index.height().unsigned_abs();
209209
let unspent_indexes: HashSet<u64> =
210210
hints.get_block_offsets(block_height).into_iter().collect();
211211
// tracing::info!("{task_id} -> {block_height}:{hash}");

0 commit comments

Comments
 (0)