pub struct BlockIndex { /* private fields */ }Expand description
The in-memory block index tree.
Maps block hashes to their index entries. The “best chain” is the chain leading to the tip with the most cumulative proof-of-work.
Implementations§
Source§impl BlockIndex
impl BlockIndex
Sourcepub fn new_with_pow_limit(pow_limit_bits: u32) -> Self
pub fn new_with_pow_limit(pow_limit_bits: u32) -> Self
Create a new empty block index.
pow_limit_bits is the network’s maximum allowed target in compact
form (e.g. 0x1d00ffff for mainnet, 0x207fffff for regtest).
Pass 0 to disable PoW checks (for unit tests that don’t care about PoW).
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty block index with no PoW validation.
Equivalent to new_with_pow_limit(0). PoW checks are skipped,
which is convenient for tests that only care about chain structure.
Sourcepub fn load_checkpoints(&mut self, checkpoints: &[Checkpoint])
pub fn load_checkpoints(&mut self, checkpoints: &[Checkpoint])
Load checkpoints into the block index.
Call this after creation / before syncing so that add_header
can reject headers that violate a checkpoint.
Sourcepub fn last_checkpoint_height(&self) -> u32
pub fn last_checkpoint_height(&self) -> u32
Get the highest loaded checkpoint height, or 0 if none.
Sourcepub fn init_genesis(&mut self, header: BlockHeader)
pub fn init_genesis(&mut self, header: BlockHeader)
Initialize the block index with a genesis block header.
Sourcepub fn add_header(
&mut self,
header: BlockHeader,
) -> Result<(BlockHash, bool), BlockIndexError>
pub fn add_header( &mut self, header: BlockHeader, ) -> Result<(BlockHash, bool), BlockIndexError>
Add a new block header to the index. Returns the hash and whether a reorg occurred (the best chain tip changed to a different branch).
If the parent is unknown, returns an error.
Sourcepub fn set_status(&mut self, hash: &BlockHash, status: BlockValidationStatus)
pub fn set_status(&mut self, hash: &BlockHash, status: BlockValidationStatus)
Set the validation status of a block.
Sourcepub fn get(&self, hash: &BlockHash) -> Option<&BlockIndexEntry>
pub fn get(&self, hash: &BlockHash) -> Option<&BlockIndexEntry>
Get a block index entry by hash.
Sourcepub fn best_tip_entry(&self) -> Option<&BlockIndexEntry>
pub fn best_tip_entry(&self) -> Option<&BlockIndexEntry>
Get the best chain tip entry.
Sourcepub fn get_hash_at_height(&self, height: u32) -> Option<BlockHash>
pub fn get_hash_at_height(&self, height: u32) -> Option<BlockHash>
Get the block hash at a given height on the active chain.
Sourcepub fn best_height(&self) -> u32
pub fn best_height(&self) -> u32
Get the current best chain height.
Sourcepub fn header_count(&self) -> usize
pub fn header_count(&self) -> usize
Get the total number of known headers.
Sourcepub fn get_ancestor_chain(&self, hash: &BlockHash) -> Vec<BlockHash>
pub fn get_ancestor_chain(&self, hash: &BlockHash) -> Vec<BlockHash>
Walk ancestors from a given hash back to genesis. Returns hashes from the given block back to genesis (inclusive).
Sourcepub fn build_locator(&self) -> Vec<BlockHash>
pub fn build_locator(&self) -> Vec<BlockHash>
Build a block locator (list of block hashes for the getblocks protocol message).
Uses exponential backoff: recent blocks are listed individually, then
the step size doubles.
Sourcepub fn get_median_time_past(&self, height: u32) -> Option<u32>
pub fn get_median_time_past(&self, height: u32) -> Option<u32>
Compute the Median Time Past (MTP) for a block on the active chain.
BIP113: the “time” used for time-lock evaluation is the median of the timestamps of the previous 11 blocks (or all blocks if fewer than 11 exist). This prevents miners from manipulating the timestamp of a single block to bypass time locks.
height is the height of the block whose MTP we want. The MTP is
computed from blocks at heights max(0, height-10)..=height.
Returns None if the height is not on the active chain.
Sourcepub fn tip_median_time_past(&self) -> Option<u32>
pub fn tip_median_time_past(&self) -> Option<u32>
Compute the MTP for the current best chain tip.