Skip to content

Commit 95c55bd

Browse files
authored
Merge pull request #30 from rustaceanrob/8-12-hint-type
Add a shallow `.hints` file parser
2 parents 323e5b4 + 6a77235 commit 95c55bd

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

hintfile/src/lib.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
use std::io::{self, Read, Write};
1+
use std::{
2+
collections::BTreeMap,
3+
io::{self, Read, Write},
4+
};
5+
6+
use bitcoin::{BlockHeight, BlockHeightInterval};
27

38
pub fn write_compact_size<W: Write>(value: u64, writer: &mut W) -> Result<(), io::Error> {
49
match value {
@@ -37,6 +42,41 @@ pub fn read_compact_size<R: Read>(reader: &mut R) -> Result<u64, io::Error> {
3742
}
3843
}
3944

45+
#[derive(Debug)]
46+
pub struct Hints {
47+
map: BTreeMap<BlockHeight, Vec<u64>>,
48+
}
49+
50+
impl Hints {
51+
// # Panics
52+
//
53+
// Panics when expected data is not present, or the hintfile overflows the maximum blockheight
54+
pub fn from_file<R: Read>(reader: &mut R) -> Self {
55+
let mut map = BTreeMap::new();
56+
let mut height = BlockHeight::from_u32(1);
57+
while let Ok(count) = read_compact_size(reader) {
58+
// panics on 32 bit machines
59+
let mut offsets = Vec::with_capacity(count as usize);
60+
for _ in 0..count {
61+
offsets.push(read_compact_size(reader).expect("unexpected end of hintfile"));
62+
}
63+
map.insert(height, offsets);
64+
height = height
65+
.checked_add(BlockHeightInterval::from_u32(1))
66+
.expect("hintfile absurdly large.")
67+
}
68+
Self { map }
69+
}
70+
71+
/// # Panics
72+
///
73+
/// If there are no offset present at that height, aka an overflow, or the entry has already
74+
/// been fetched.
75+
pub fn take_block_offsets(&mut self, height: BlockHeight) -> Vec<u64> {
76+
self.map.remove(&height).expect("block height overflow")
77+
}
78+
}
79+
4080
#[cfg(test)]
4181
mod tests {
4282
use crate::{read_compact_size, write_compact_size};

0 commit comments

Comments
 (0)