bdk_chain::local_chain

Struct 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

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

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

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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

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

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

§

fn vzip(self) -> V