|
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}; |
2 | 7 |
|
3 | 8 | pub fn write_compact_size<W: Write>(value: u64, writer: &mut W) -> Result<(), io::Error> { |
4 | 9 | match value { |
@@ -37,6 +42,41 @@ pub fn read_compact_size<R: Read>(reader: &mut R) -> Result<u64, io::Error> { |
37 | 42 | } |
38 | 43 | } |
39 | 44 |
|
| 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 | + |
40 | 80 | #[cfg(test)] |
41 | 81 | mod tests { |
42 | 82 | use crate::{read_compact_size, write_compact_size}; |
|
0 commit comments