Struct bdk_chain::local_chain::LocalChain

source ·
pub struct LocalChain { /* private fields */ }
Expand description

This is a local implementation of ChainOracle.

Implementations§

source§

impl LocalChain

source

pub fn genesis_hash(&self) -> BlockHash

Get the genesis hash.

source

pub fn from_genesis_hash(hash: BlockHash) -> (Self, ChangeSet)

Construct LocalChain from genesis hash.

source

pub fn from_changeset(changeset: ChangeSet) -> Result<Self, MissingGenesisError>

Construct a LocalChain from an initial changeset.

source

pub fn from_tip(tip: CheckPoint) -> Result<Self, MissingGenesisError>

Construct a LocalChain from a given checkpoint tip.

source

pub fn from_blocks( blocks: BTreeMap<u32, BlockHash> ) -> Result<Self, MissingGenesisError>

Constructs a LocalChain from a BTreeMap of height to [BlockHash].

The BTreeMap enforces the height order. However, the caller must ensure the blocks are all of the same chain.

source

pub fn tip(&self) -> CheckPoint

Get the highest checkpoint.

source

pub fn apply_update( &mut self, update: CheckPoint ) -> Result<ChangeSet, CannotConnectError>

Applies the given update to the chain.

The method returns ChangeSet on success. This represents the changes applied to self.

There must be no ambiguity about which of the existing chain’s blocks are still valid and which are now invalid. That is, the new chain must implicitly connect to a definite block in the existing chain and invalidate the block after it (if it exists) by including a block at the same height but with a different hash to explicitly exclude it as a connection point.

§Errors

An error will occur if the update does not correctly connect with self.

source

pub fn apply_header_connected_to( &mut self, header: &Header, height: u32, connected_to: BlockId ) -> Result<ChangeSet, ApplyHeaderError>

Update the chain with a given [Header] at height which you claim is connected to a existing block in the chain.

This is useful when you have a block header that you want to record as part of the chain but don’t necessarily know that the prev_blockhash is in the chain.

This will usually insert two new BlockIds into the chain: the header’s block and the header’s prev_blockhash block. connected_to must already be in the chain but is allowed to be prev_blockhash (in which case only one new block id will be inserted). To be successful, connected_to must be chosen carefully so that LocalChain’s update rules are satisfied.

§Errors

ApplyHeaderError::InconsistentBlocks occurs if the connected_to block and the [Header] is inconsistent. For example, if the connected_to block is the same height as header or prev_blockhash, but has a different block hash. Or if the connected_to height is greater than the header’s height.

ApplyHeaderError::CannotConnect occurs if the internal call to apply_update fails.

source

pub fn apply_header( &mut self, header: &Header, height: u32 ) -> Result<ChangeSet, CannotConnectError>

Update the chain with a given [Header] connecting it with the previous block.

This is a convenience method to call apply_header_connected_to with the connected_to parameter being height-1:prev_blockhash. If there is no previous block (i.e. genesis), we use the current block as connected_to.

source

pub fn apply_changeset( &mut self, changeset: &ChangeSet ) -> Result<(), MissingGenesisError>

Apply the given changeset.

source

pub fn insert_block( &mut self, block_id: BlockId ) -> Result<ChangeSet, AlterCheckPointError>

Insert a BlockId.

§Errors

Replacing the block hash of an existing checkpoint will result in an error.

source

pub fn disconnect_from( &mut self, block_id: BlockId ) -> Result<ChangeSet, MissingGenesisError>

Removes blocks from (and inclusive of) the given block_id.

This will remove blocks with a height equal or greater than block_id, but only if block_id exists in the chain.

§Errors

This will fail with MissingGenesisError if the caller attempts to disconnect from the genesis block.

source

pub fn initial_changeset(&self) -> ChangeSet

Derives an initial ChangeSet, meaning that it can be applied to an empty chain to recover the current chain.

source

pub fn iter_checkpoints(&self) -> CheckPointIter

Iterate over checkpoints in descending height order.

source

pub fn get(&self, height: u32) -> Option<CheckPoint>

Get checkpoint at given height (if it exists).

This is a shorthand for calling CheckPoint::get on the tip.

source

pub fn range<R>(&self, range: R) -> impl Iterator<Item = CheckPoint>
where R: RangeBounds<u32>,

Iterate checkpoints over a height range.

Note that we always iterate checkpoints in reverse height order (iteration starts at tip height).

This is a shorthand for calling CheckPoint::range on the tip.

Trait Implementations§

source§

impl ChainOracle for LocalChain

§

type Error = Infallible

Error type.
source§

fn is_block_in_chain( &self, block: BlockId, chain_tip: BlockId ) -> Result<Option<bool>, Self::Error>

Determines whether block of BlockId exists as an ancestor of chain_tip. Read more
source§

fn get_chain_tip(&self) -> Result<BlockId, Self::Error>

Get the best chain’s chain tip.
source§

impl Clone for LocalChain

source§

fn clone(&self) -> LocalChain

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for LocalChain

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for LocalChain

source§

fn eq(&self, other: &LocalChain) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for LocalChain

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, 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> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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>,

§

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

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

§

fn vzip(self) -> V