Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion hintfile/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::io::{self, Read, Write};
use std::{
collections::BTreeMap,
io::{self, Read, Write},
};

use bitcoin::{BlockHeight, BlockHeightInterval};

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

#[derive(Debug)]
pub struct Hints {
map: BTreeMap<BlockHeight, Vec<u64>>,
}

impl Hints {
// # Panics
//
// Panics when expected data is not present, or the hintfile overflows the maximum blockheight
pub fn from_file<R: Read>(reader: &mut R) -> Self {
let mut map = BTreeMap::new();
let mut height = BlockHeight::from_u32(1);
while let Ok(count) = read_compact_size(reader) {
// panics on 32 bit machines
let mut offsets = Vec::with_capacity(count as usize);
for _ in 0..count {
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.")
}
Self { map }
}

/// # Panics
///
/// If there are no offset present at that height, aka an overflow, or the entry has already
/// been fetched.
pub fn take_block_offsets(&mut self, height: BlockHeight) -> Vec<u64> {
self.map.remove(&height).expect("block height overflow")
}
}

#[cfg(test)]
mod tests {
use crate::{read_compact_size, write_compact_size};
Expand Down
Loading