Skip to main content

BlockIndex

Struct BlockIndex 

Source
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

Source

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).

Source

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.

Source

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.

Source

pub fn last_checkpoint_height(&self) -> u32

Get the highest loaded checkpoint height, or 0 if none.

Source

pub fn init_genesis(&mut self, header: BlockHeader)

Initialize the block index with a genesis block header.

Source

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.

Source

pub fn set_status(&mut self, hash: &BlockHash, status: BlockValidationStatus)

Set the validation status of a block.

Source

pub fn get(&self, hash: &BlockHash) -> Option<&BlockIndexEntry>

Get a block index entry by hash.

Source

pub fn best_tip(&self) -> BlockHash

Get the best chain tip hash.

Source

pub fn best_tip_entry(&self) -> Option<&BlockIndexEntry>

Get the best chain tip entry.

Source

pub fn get_hash_at_height(&self, height: u32) -> Option<BlockHash>

Get the block hash at a given height on the active chain.

Source

pub fn best_height(&self) -> u32

Get the current best chain height.

Source

pub fn header_count(&self) -> usize

Get the total number of known headers.

Source

pub fn contains(&self, hash: &BlockHash) -> bool

Check if we have a header for the given hash.

Source

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).

Source

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.

Source

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.

Source

pub fn tip_median_time_past(&self) -> Option<u32>

Compute the MTP for the current best chain tip.

Trait Implementations§

Source§

impl Default for BlockIndex

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more