Struct jormungandr::blockchain::Blockchain
source · pub struct Blockchain {
ref_cache: RefCache,
ledgers: Multiverse<Ledger>,
storage: Storage,
block0: HeaderHash,
rewards_report_all: bool,
}
Expand description
blockchain object, can be safely shared across multiple threads. However it is better not to as some operations may require a mutex.
This objects holds a reference to the persistent storage. It also holds 2 different caching of objects:
RefCache
: a cache of blocks headers and associated states;Multiverse
: of ledger. It is a cache of different ledger states.
Fields§
§ref_cache: RefCache
§ledgers: Multiverse<Ledger>
§storage: Storage
§block0: HeaderHash
§rewards_report_all: bool
Implementations§
source§impl Blockchain
impl Blockchain
pub fn new( block0: HeaderHash, storage: Storage, cache_capacity: usize, rewards_report_all: bool ) -> Self
pub fn block0(&self) -> &HeaderHash
pub fn storage(&self) -> &Storage
pub async fn branches(&self) -> Result<Vec<Branch>, Error>
pub async fn gc(&self, tip: Arc<Ref>) -> Result<(), Error>
sourceasync fn create_and_store_reference(
&self,
header_hash: HeaderHash,
header: Header,
ledger: Ledger,
time_frame: Arc<TimeFrame>,
leadership: Arc<Leadership>,
epoch_rewards_info: Option<Arc<EpochRewardsInfo>>,
previous_epoch_state: Option<Arc<Ref>>
) -> Arc<Ref>
async fn create_and_store_reference( &self, header_hash: HeaderHash, header: Header, ledger: Ledger, time_frame: Arc<TimeFrame>, leadership: Arc<Leadership>, epoch_rewards_info: Option<Arc<EpochRewardsInfo>>, previous_epoch_state: Option<Arc<Ref>> ) -> Arc<Ref>
create and store a reference of this leader to the new
sourcepub async fn get_ref(
&self,
header_hash: HeaderHash
) -> Result<Option<Arc<Ref>>, Error>
pub async fn get_ref( &self, header_hash: HeaderHash ) -> Result<Option<Arc<Ref>>, Error>
get Ref
of the given header hash
once the Ref
is in hand, it means we have the Leadership schedule associated
to this block and the Ledger
state after this block.
If the future returns None
it means we don’t know about this block locally
and it might be necessary to contacts the network to retrieve a missing
branch
TODO: the case where the block is in storage but not yet in the cache is not implemented
sourceasync fn load_header_parent(
&self,
header: Header,
force: bool
) -> Result<PreCheckedHeader, Error>
async fn load_header_parent( &self, header: Header, force: bool ) -> Result<PreCheckedHeader, Error>
load the header’s parent Ref
.
sourcepub async fn pre_check_header(
&self,
header: Header,
force: bool
) -> Result<PreCheckedHeader, Error>
pub async fn pre_check_header( &self, header: Header, force: bool ) -> Result<PreCheckedHeader, Error>
load the header’s parent and perform some simple verification:
- check the block_date is increasing
- check the chain_length is monotonically increasing
At the end of this future we know either of:
- the block is already present (nothing to do);
- the block’s parent is already present (we can then continue validation)
- the block’s parent is missing: we need to download it and call again this function.
sourcepub async fn post_check_header(
&self,
header: Header,
parent: Arc<Ref>,
check_header_proof: CheckHeaderProof
) -> Result<PostCheckedHeader, Error>
pub async fn post_check_header( &self, header: Header, parent: Arc<Ref>, check_header_proof: CheckHeaderProof ) -> Result<PostCheckedHeader, Error>
check the header cryptographic properties and leadership’s schedule
on success returns the PostCheckedHeader:
- the header,
- the ledger state associated to the parent block
- the leadership schedule associated to the header
fn apply_block_dry_run( &self, post_checked_header: &PostCheckedHeader, block: &Block ) -> Result<Ledger, Error>
fn apply_block_check_rewards( &self, post_checked_header: &PostCheckedHeader, ledger: &Ledger ) -> Result<(), Error>
async fn apply_block_finalize( &self, post_checked_header: PostCheckedHeader, new_ledger: Ledger ) -> Arc<Ref>
async fn store_and_apply_block_finalize( &self, post_checked_header: PostCheckedHeader, block: Block, new_ledger: Ledger ) -> Result<AppliedBlock, Error>
sourcepub async fn apply_and_store_block(
&self,
post_checked_header: PostCheckedHeader,
block: Block
) -> Result<AppliedBlock, Error>
pub async fn apply_and_store_block( &self, post_checked_header: PostCheckedHeader, block: Block ) -> Result<AppliedBlock, Error>
Apply the block on the blockchain from a post checked header and add it to the storage. If the block is already present in the storage, the returned future resolves to None. Otherwise it returns the reference to the block.
sourcepub async fn apply_and_store_leadership_block(
&self,
leadership_block: LeadershipBlock
) -> Result<AppliedBlock, Error>
pub async fn apply_and_store_leadership_block( &self, leadership_block: LeadershipBlock ) -> Result<AppliedBlock, Error>
Apply the block generated by this node. This should already have a valid state attached to it, so we do not need to verify this one.
sourceasync fn apply_block0(&self, block0: &Block) -> Result<Branch, Error>
async fn apply_block0(&self, block0: &Block) -> Result<Branch, Error>
Apply the given block0 in the blockchain (updating the RefCache and the other objects)
This function returns the created block0 branch. Having it will
avoid searching for it in the blockchain’s branches
and perform
operations to update the branch as we move along already.
Errors
The resulted future may fail if
- the block0 does build an invalid
Ledger
:Error::Block0InitialLedgerError
;
sourcepub async fn load_from_block0(&self, block0: Block) -> Result<Tip, Error>
pub async fn load_from_block0(&self, block0: Block) -> Result<Tip, Error>
function to do the initial application of the block0 in the Blockchain
and its
storage. We assume Block0
is not already in the Storage
.
This function returns the create block0 branch. Having it will
avoid searching for it in the blockchain’s branches
and perform
operations to update the branch as we move along already.
Errors
The resulted future may fail if
- the block0 already exists in the storage:
Error::Block0AlreadyInStorage
; - the block0 does build a valid
Ledger
:Error::Block0InitialLedgerError
; - other errors while interacting with the storage (IO errors)
pub(super) async fn handle_bootstrap_block( &self, block: Block, check_header: CheckHeaderProof ) -> Result<Arc<Ref>, Error>
sourcepub async fn load_from_storage(&self, block0: Block) -> Result<Tip, Error>
pub async fn load_from_storage(&self, block0: Block) -> Result<Tip, Error>
returns a future that will propagate the initial states and leadership
from the block0 to the Head
of the storage (the last known block which
made consensus).
The Future will returns a branch pointing to the Head
.
Errors
The resulted future may fail if
- the block0 is not already in the storage:
Error::Block0NotAlreadyInStorage
; - the block0 does build a valid
Ledger
:Error::Block0InitialLedgerError
; - other errors while interacting with the storage (IO errors)
pub fn get_checkpoints(&self, branch: &Branch) -> Checkpoints
Trait Implementations§
source§impl Clone for Blockchain
impl Clone for Blockchain
source§fn clone(&self) -> Blockchain
fn clone(&self) -> Blockchain
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for Blockchain
impl Send for Blockchain
impl Sync for Blockchain
impl Unpin for Blockchain
impl !UnwindSafe for Blockchain
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request
source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request