Struct blockchain::reference::Reference
source · pub struct Reference {
ledger: Ledger,
header: Header,
epoch_info: Arc<EpochInfo>,
previous_epoch_state: Option<Arc<Reference>>,
}
Fields§
§ledger: Ledger
the ledger at the state of the left by applying the current block and all the previous blocks before that.
header: Header
keeping the block’s header here to save some lookup time in the storage it contains all needed to retrieve the block from the storage (the HeaderId) but also all the metadata associated to the block (parent, date, depth…).
epoch_info: Arc<EpochInfo>
the block’s epoch info
previous_epoch_state: Option<Arc<Reference>>
last Ref
. Every time there is a transition this value will be filled with
the parent Ref
. Otherwise it will be copied from Ref
to Ref
.
Implementations§
source§impl Reference
impl Reference
sourcepub fn new(block0: &Block) -> Result<Self, Error>
pub fn new(block0: &Block) -> Result<Self, Error>
create a new block reference with the given block0
This will mark the beginning of a new blockchain as there is no expected parents before this block. Thought he block_parent_hash may refer to a block hash from another blockchain or may have a specific meaning
sourcepub fn approximate_common_ancestor(
self: &Arc<Self>,
other: &Arc<Self>
) -> Option<Arc<Self>>
pub fn approximate_common_ancestor( self: &Arc<Self>, other: &Arc<Self> ) -> Option<Arc<Self>>
approximate a common ancestor between the given References
This will lead to a common ancestor within the epoch boundary as this is the only References that may be kept.
There is only 2 reasons for this function to return None:
- the 2 blocks are from different blockchain;
- one of the blocks are from the first epoch
sourcepub fn select(self: &Arc<Self>, candidate: &Arc<Self>) -> Selection
pub fn select(self: &Arc<Self>, candidate: &Arc<Self>) -> Selection
compare the current Reference with the candidate one
sourcepub fn chain(self: Arc<Self>, block: &Block) -> Result<Self, Error>
pub fn chain(self: Arc<Self>, block: &Block) -> Result<Self, Error>
chain a new block, expecting the new block to be a child of the given block
This function will also perform all the necessary checks to make sure this block is valid within the initial context (parent hash, chain length, ledger and block signatures)
sourcepub fn epoch_transition(&self) -> Result<Self, Error>
pub fn epoch_transition(&self) -> Result<Self, Error>
once we suppose the end of an epoch as come, we can compute the missing steps to finalize the epoch: apply the protocol changes and distribute the rewards
sourcepub fn new_epoch_info(&self, epoch: Epoch) -> Result<Arc<EpochInfo>, Error>
pub fn new_epoch_info(&self, epoch: Epoch) -> Result<Arc<EpochInfo>, Error>
compute a new epoch info from the given Reference for the given Epoch
We are not performing any checks here, merely generating a new Leadership object of the given state.
fn chain_epoch_info(self: Arc<Self>, block: &Block) -> Result<Arc<Self>, Error>
fn check_child(&self, block: &Block) -> Result<(), Error>
fn check_chain_length(&self, block: &Block) -> Result<(), Error>
fn check_block_date(&self, block: &Block) -> Result<(), Error>
sourcepub fn block_parent_hash(&self) -> HeaderId
pub fn block_parent_hash(&self) -> HeaderId
access the reference’s parent hash
sourcepub fn block_date(&self) -> BlockDate
pub fn block_date(&self) -> BlockDate
retrieve the block date of the Ref
sourcepub fn chain_length(&self) -> ChainLength
pub fn chain_length(&self) -> ChainLength
retrieve the chain length, the number of blocks created between the block0 and this block. This is useful to compare the density of 2 branches.
pub fn ledger(&self) -> &Ledger
sourcepub fn epoch_info(&self) -> Arc<EpochInfo>
pub fn epoch_info(&self) -> Arc<EpochInfo>
retrieve the block’s epoch info
sourcepub fn time(&self) -> SystemTime
pub fn time(&self) -> SystemTime
get the time the block was schedule for
panics
This function will panic is the block does not coincide with the epoch’s time era.
This should not happen by construct as the Reference
has been constructed and
validated already.
sourcepub fn elapsed(&self) -> Result<Duration, SystemTimeError>
pub fn elapsed(&self) -> Result<Duration, SystemTimeError>
retrieve the number of seconds since this block was schedule
If the block was schedule in the future, the function will return an error.