pub struct BlkReader { /* private fields */ }Expand description
Low-level reader for Bitcoin Core blk*.dat files.
Manages a pool of up to 8 open file handles (LRU eviction) and applies
XOR decoding automatically when blocks/xor.dat is present.
For height-ordered iteration prefer BlockIterator.
Implementations§
Source§impl BlkReader
impl BlkReader
Sourcepub fn new(blocks_dir: PathBuf, buffer_size: usize) -> Self
pub fn new(blocks_dir: PathBuf, buffer_size: usize) -> Self
Creates a new reader for the given blocks/ directory.
buffer_size is the I/O read buffer per open file in bytes.
A value of 8 * 1024 * 1024 (8 MiB) works well for sequential reads.
XOR key detection from xor.dat happens here at construction time.
Sourcepub fn read_block_at(
&mut self,
n_file: u32,
n_data_pos: u32,
) -> Result<Vec<u8>, BlkReaderError>
pub fn read_block_at( &mut self, n_file: u32, n_data_pos: u32, ) -> Result<Vec<u8>, BlkReaderError>
Reads the raw serialized block at a given file index and byte offset.
n_file corresponds to blk{n_file:05}.dat (e.g. 0 → blk00000.dat).
n_data_pos is the offset of the block data as stored in the LevelDB index
(CDiskBlockIndex::nDataPos). The 8 bytes immediately before it contain the
network magic and block size.
Returns the raw block bytes without any header prefix. Pass the result to
bitcoin::consensus::deserialize or similar to decode transactions.
§Errors
Returns BlkReaderError if the file is missing, the magic bytes don’t
match mainnet, or an I/O error occurs.