pub struct LocalChain { /* private fields */ }
Expand description
This is a local implementation of ChainOracle
.
Implementations§
Source§impl LocalChain
impl LocalChain
Sourcepub fn genesis_hash(&self) -> BlockHash
pub fn genesis_hash(&self) -> BlockHash
Get the genesis hash.
Sourcepub fn from_genesis_hash(hash: BlockHash) -> (Self, ChangeSet)
pub fn from_genesis_hash(hash: BlockHash) -> (Self, ChangeSet)
Construct LocalChain
from genesis hash
.
Sourcepub fn from_changeset(changeset: ChangeSet) -> Result<Self, MissingGenesisError>
pub fn from_changeset(changeset: ChangeSet) -> Result<Self, MissingGenesisError>
Construct a LocalChain
from an initial changeset
.
Sourcepub fn from_tip(tip: CheckPoint) -> Result<Self, MissingGenesisError>
pub fn from_tip(tip: CheckPoint) -> Result<Self, MissingGenesisError>
Construct a LocalChain
from a given checkpoint
tip.
Sourcepub fn from_blocks(
blocks: BTreeMap<u32, BlockHash>,
) -> Result<Self, MissingGenesisError>
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.
Sourcepub fn tip(&self) -> CheckPoint
pub fn tip(&self) -> CheckPoint
Get the highest checkpoint.
Sourcepub fn apply_update(
&mut self,
update: CheckPoint,
) -> Result<ChangeSet, CannotConnectError>
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
.
Sourcepub fn apply_header_connected_to(
&mut self,
header: &Header,
height: u32,
connected_to: BlockId,
) -> Result<ChangeSet, ApplyHeaderError>
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 BlockId
s 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.
Sourcepub fn apply_header(
&mut self,
header: &Header,
height: u32,
) -> Result<ChangeSet, CannotConnectError>
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
.
Sourcepub fn apply_changeset(
&mut self,
changeset: &ChangeSet,
) -> Result<(), MissingGenesisError>
pub fn apply_changeset( &mut self, changeset: &ChangeSet, ) -> Result<(), MissingGenesisError>
Apply the given changeset
.
Sourcepub fn insert_block(
&mut self,
block_id: BlockId,
) -> Result<ChangeSet, AlterCheckPointError>
pub fn insert_block( &mut self, block_id: BlockId, ) -> Result<ChangeSet, AlterCheckPointError>
Sourcepub fn disconnect_from(
&mut self,
block_id: BlockId,
) -> Result<ChangeSet, MissingGenesisError>
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.
Sourcepub fn initial_changeset(&self) -> ChangeSet
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.
Sourcepub fn iter_checkpoints(&self) -> CheckPointIter ⓘ
pub fn iter_checkpoints(&self) -> CheckPointIter ⓘ
Iterate over checkpoints in descending height order.
Sourcepub fn get(&self, height: u32) -> Option<CheckPoint>
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
.
Sourcepub fn range<R>(&self, range: R) -> impl Iterator<Item = CheckPoint>where
R: RangeBounds<u32>,
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
impl ChainOracle for LocalChain
Source§impl Clone for LocalChain
impl Clone for LocalChain
Source§fn clone(&self) -> LocalChain
fn clone(&self) -> LocalChain
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more