Kagome
Polkadot Runtime Engine in C++17
kagome::blockchain::BlockTreeImpl Class Reference

#include <block_tree_impl.hpp>

Inheritance diagram for kagome::blockchain::BlockTreeImpl:
Collaboration diagram for kagome::blockchain::BlockTreeImpl:

Public Member Functions

 ~BlockTreeImpl () override=default
 
const primitives::BlockHashgetGenesisBlockHash () const override
 
outcome::result< bool > hasBlockHeader (const primitives::BlockId &block) const override
 
outcome::result< primitives::BlockHeadergetBlockHeader (const primitives::BlockId &block) const override
 
outcome::result< primitives::BlockBodygetBlockBody (const primitives::BlockId &block) const override
 
outcome::result< primitives::JustificationgetBlockJustification (const primitives::BlockId &block) const override
 
outcome::result< void > addBlockHeader (const primitives::BlockHeader &header) override
 
outcome::result< void > addBlock (const primitives::Block &block) override
 
outcome::result< void > removeLeaf (const primitives::BlockHash &block_hash) override
 
outcome::result< void > addExistingBlock (const primitives::BlockHash &block_hash, const primitives::BlockHeader &block_header) override
 
outcome::result< void > addBlockBody (primitives::BlockNumber block_number, const primitives::BlockHash &block_hash, const primitives::BlockBody &body) override
 
outcome::result< void > finalize (const primitives::BlockHash &block_hash, const primitives::Justification &justification) override
 
BlockHashVecRes getBestChainFromBlock (const primitives::BlockHash &block, uint64_t maximum) const override
 
BlockHashVecRes getDescendingChainToBlock (const primitives::BlockHash &block, uint64_t maximum) const override
 
BlockHashVecRes getChainByBlocks (const primitives::BlockHash &ancestor, const primitives::BlockHash &descendant) const override
 
bool hasDirectChain (const primitives::BlockHash &ancestor, const primitives::BlockHash &descendant) const override
 
primitives::BlockInfo deepestLeaf () const override
 
outcome::result< primitives::BlockInfogetBestContaining (const primitives::BlockHash &target_hash, const std::optional< primitives::BlockNumber > &max_number) const override
 Get the most recent block of the best (longest) chain among those that contain a block with. More...
 
std::vector< primitives::BlockHashgetLeaves () const override
 
BlockHashVecRes getChildren (const primitives::BlockHash &block) const override
 
primitives::BlockInfo getLastFinalized () const override
 
- Public Member Functions inherited from kagome::blockchain::BlockTree
virtual ~BlockTree ()=default
 

Static Public Member Functions

static outcome::result< std::shared_ptr< BlockTreeImpl > > create (std::shared_ptr< BlockHeaderRepository > header_repo, std::shared_ptr< BlockStorage > storage, std::shared_ptr< network::ExtrinsicObserver > extrinsic_observer, std::shared_ptr< crypto::Hasher > hasher, primitives::events::ChainSubscriptionEnginePtr chain_events_engine, primitives::events::ExtrinsicSubscriptionEnginePtr extrinsic_events_engine, std::shared_ptr< subscription::ExtrinsicEventKeyRepository > extrinsic_event_key_repo, std::shared_ptr< storage::changes_trie::ChangesTracker > changes_tracker, std::shared_ptr< const class JustificationStoragePolicy > justification_storage_policy)
 Create an instance of block tree. More...
 
static outcome::result< void > recover (primitives::BlockId target_block, std::shared_ptr< BlockStorage > storage, std::shared_ptr< BlockHeaderRepository > header_repo, std::shared_ptr< const storage::trie::TrieStorage > trie_storage, std::shared_ptr< blockchain::BlockTree > block_tree)
 Recover block tree state at provided block. More...
 

Private Member Functions

 BlockTreeImpl (std::shared_ptr< BlockHeaderRepository > header_repo, std::shared_ptr< BlockStorage > storage, std::unique_ptr< CachedTree > cached_tree, std::shared_ptr< network::ExtrinsicObserver > extrinsic_observer, std::shared_ptr< crypto::Hasher > hasher, primitives::events::ChainSubscriptionEnginePtr chain_events_engine, primitives::events::ExtrinsicSubscriptionEnginePtr extrinsic_events_engine, std::shared_ptr< subscription::ExtrinsicEventKeyRepository > extrinsic_event_key_repo, std::shared_ptr< storage::changes_trie::ChangesTracker > changes_tracker, std::shared_ptr< const class JustificationStoragePolicy > justification_storage_policy)
 
outcome::result< primitives::BlockHashwalkBackUntilLess (const primitives::BlockHash &start, const primitives::BlockNumber &limit) const
 
std::vector< primitives::BlockHashgetLeavesSorted () const
 
outcome::result< void > prune (const std::shared_ptr< TreeNode > &lastFinalizedNode)
 
outcome::result< void > reorganize ()
 

Private Attributes

std::shared_ptr< BlockHeaderRepositoryheader_repo_
 
std::shared_ptr< BlockStoragestorage_
 
std::unique_ptr< CachedTreetree_
 
std::shared_ptr< network::ExtrinsicObserverextrinsic_observer_
 
std::shared_ptr< crypto::Hasherhasher_
 
primitives::events::ChainSubscriptionEnginePtr chain_events_engine_
 
primitives::events::ExtrinsicSubscriptionEnginePtr extrinsic_events_engine_
 
std::shared_ptr< subscription::ExtrinsicEventKeyRepositoryextrinsic_event_key_repo_
 
std::shared_ptr< storage::changes_trie::ChangesTrackertrie_changes_tracker_
 
std::shared_ptr< const class JustificationStoragePolicyjustification_storage_policy_
 
std::optional< primitives::BlockHashgenesis_block_hash_
 
log::Logger log_ = log::createLogger("BlockTree", "blockchain")
 
metrics::RegistryPtr metrics_registry_ = metrics::createRegistry()
 
metrics::Gaugemetric_best_block_height_
 
metrics::Gaugemetric_finalized_block_height_
 
metrics::Gaugemetric_known_chain_leaves_
 
telemetry::Telemetry telemetry_ = telemetry::createTelemetryService()
 

Additional Inherited Members

- Public Types inherited from kagome::blockchain::BlockTree
enum  GetChainDirection { GetChainDirection::ASCEND, GetChainDirection::DESCEND }
 
using BlockHashVecRes = outcome::result< std::vector< primitives::BlockHash >>
 

Detailed Description

Definition at line 44 of file block_tree_impl.hpp.

Constructor & Destructor Documentation

kagome::blockchain::BlockTreeImpl::~BlockTreeImpl ( )
overridedefault
kagome::blockchain::BlockTreeImpl::BlockTreeImpl ( std::shared_ptr< BlockHeaderRepository header_repo,
std::shared_ptr< BlockStorage storage,
std::unique_ptr< CachedTree cached_tree,
std::shared_ptr< network::ExtrinsicObserver extrinsic_observer,
std::shared_ptr< crypto::Hasher hasher,
primitives::events::ChainSubscriptionEnginePtr  chain_events_engine,
primitives::events::ExtrinsicSubscriptionEnginePtr  extrinsic_events_engine,
std::shared_ptr< subscription::ExtrinsicEventKeyRepository extrinsic_event_key_repo,
std::shared_ptr< storage::changes_trie::ChangesTracker changes_tracker,
std::shared_ptr< const class JustificationStoragePolicy justification_storage_policy 
)
private

Private constructor, so that instances are created only through the factory method

Definition at line 327 of file block_tree_impl.cpp.

Member Function Documentation

outcome::result< void > kagome::blockchain::BlockTreeImpl::addBlock ( const primitives::Block block)
overridevirtual

Add a new block to the tree

Parameters
blockto be stored and added to tree
Returns
nothing or error; if error happens, no changes in the tree are made
Note
if block, which is specified in PARENT_HASH field of (
Parameters
block)is not in our local storage, corresponding error is returned. It is suggested that after getting that error, the caller would ask another peer for the parent block and try to insert it; this operation is to be repeated until a successful insertion happens

Implements kagome::blockchain::BlockTree.

Definition at line 438 of file block_tree_impl.cpp.

outcome::result< void > kagome::blockchain::BlockTreeImpl::addBlockBody ( primitives::BlockNumber  block_number,
const primitives::BlockHash block_hash,
const primitives::BlockBody block_body 
)
overridevirtual

Adds block body to the storage

Parameters
block_numberthat corresponds to the block which body we are adding
block_hashthat corresponds to the block which body we are adding
block_bodythat we are adding
Returns
result with success if block body was inserted. Error otherwise

Implements kagome::blockchain::BlockTree.

Definition at line 627 of file block_tree_impl.cpp.

outcome::result< void > kagome::blockchain::BlockTreeImpl::addBlockHeader ( const primitives::BlockHeader header)
overridevirtual

Adds header to the storage

Parameters
headerthat we are adding
Returns
result with success if header's parent exists on storage and new header was added. Error otherwise

Implements kagome::blockchain::BlockTree.

Definition at line 402 of file block_tree_impl.cpp.

outcome::result< void > kagome::blockchain::BlockTreeImpl::addExistingBlock ( const primitives::BlockHash block_hash,
const primitives::BlockHeader block_header 
)
overridevirtual

Add an existent block to the tree

Parameters
block_hashis hash of the added block in the tree
block_headeris header of that block
Returns
nothing or error; if error happens, no changes in the tree are made

Implements kagome::blockchain::BlockTree.

Definition at line 526 of file block_tree_impl.cpp.

outcome::result< std::shared_ptr< BlockTreeImpl > > kagome::blockchain::BlockTreeImpl::create ( std::shared_ptr< BlockHeaderRepository header_repo,
std::shared_ptr< BlockStorage storage,
std::shared_ptr< network::ExtrinsicObserver extrinsic_observer,
std::shared_ptr< crypto::Hasher hasher,
primitives::events::ChainSubscriptionEnginePtr  chain_events_engine,
primitives::events::ExtrinsicSubscriptionEnginePtr  extrinsic_events_engine,
std::shared_ptr< subscription::ExtrinsicEventKeyRepository extrinsic_event_key_repo,
std::shared_ptr< storage::changes_trie::ChangesTracker changes_tracker,
std::shared_ptr< const class JustificationStoragePolicy justification_storage_policy 
)
static

Create an instance of block tree.

Definition at line 115 of file block_tree_impl.cpp.

primitives::BlockInfo kagome::blockchain::BlockTreeImpl::deepestLeaf ( ) const
overridevirtual

Get a deepest leaf of the tree

Returns
deepest leaf
Note
deepest leaf is also a result of "SelectBestChain": if we are the leader, we connect a block, which we constructed, to that deepest leaf

Implements kagome::blockchain::BlockTree.

Definition at line 963 of file block_tree_impl.cpp.

outcome::result< void > kagome::blockchain::BlockTreeImpl::finalize ( const primitives::BlockHash block,
const primitives::Justification justification 
)
overridevirtual

Mark the block as finalized and store a finalization justification

Parameters
blockto be finalized
justificationof the finalization
Returns
nothing or error

Implements kagome::blockchain::BlockTree.

Definition at line 635 of file block_tree_impl.cpp.

BlockTree::BlockHashVecRes kagome::blockchain::BlockTreeImpl::getBestChainFromBlock ( const primitives::BlockHash block,
uint64_t  maximum 
) const
overridevirtual

Get a chain of blocks from provided block to best block direction

Parameters
block,fromwhich the chain is started
maximumnumber of blocks to be retrieved
Returns
chain or blocks or error

Implements kagome::blockchain::BlockTree.

Definition at line 756 of file block_tree_impl.cpp.

outcome::result< primitives::BlockInfo > kagome::blockchain::BlockTreeImpl::getBestContaining ( const primitives::BlockHash target_hash,
const std::optional< primitives::BlockNumber > &  max_number 
) const
overridevirtual

Get the most recent block of the best (longest) chain among those that contain a block with.

Parameters
target_hash
target_hashis a hash of a block that the chosen chain must contain
max_numberis the max block number that the resulting block (and the target one) may possess

Implements kagome::blockchain::BlockTree.

Definition at line 969 of file block_tree_impl.cpp.

outcome::result< primitives::BlockBody > kagome::blockchain::BlockTreeImpl::getBlockBody ( const primitives::BlockId block) const
overridevirtual

Get a body (extrinsics) of the block (if present)

Parameters
block- id of the block to get body for
Returns
body, if the block exists in our storage, error in case it does not exist in our storage, or actual error happens

Implements kagome::blockchain::BlockTree.

Definition at line 742 of file block_tree_impl.cpp.

outcome::result< primitives::BlockHeader > kagome::blockchain::BlockTreeImpl::getBlockHeader ( const primitives::BlockId block) const
overridevirtual

Get block header by provided block id

Parameters
blockid of the block header we are looking for
Returns
result containing block header if it exists, error otherwise

Implements kagome::blockchain::BlockTree.

Definition at line 735 of file block_tree_impl.cpp.

outcome::result< primitives::Justification > kagome::blockchain::BlockTreeImpl::getBlockJustification ( const primitives::BlockId block) const
overridevirtual

Get a justification of the block (if present)

Parameters
block- id of the block to get justification for
Returns
body, if the block exists in our storage, error in case it does not exist in our storage, or actual error happens

Implements kagome::blockchain::BlockTree.

Definition at line 750 of file block_tree_impl.cpp.

BlockTreeImpl::BlockHashVecRes kagome::blockchain::BlockTreeImpl::getChainByBlocks ( const primitives::BlockHash ancestor,
const primitives::BlockHash descendant 
) const
overridevirtual

Get a chain of blocks. Implies hasDirectChain(ancestor, descendant).

Parameters
ancestor- block, which is closest to the genesis
descendant- block, which is farthest from the genesis
Returns
chain of blocks in ascending order or error

Implements kagome::blockchain::BlockTree.

Definition at line 847 of file block_tree_impl.cpp.

BlockTreeImpl::BlockHashVecRes kagome::blockchain::BlockTreeImpl::getChildren ( const primitives::BlockHash block) const
overridevirtual

Get children of the block with specified hash

Parameters
blockto get children of
Returns
collection of children hashes or error

Implements kagome::blockchain::BlockTree.

Definition at line 1035 of file block_tree_impl.cpp.

BlockTree::BlockHashVecRes kagome::blockchain::BlockTreeImpl::getDescendingChainToBlock ( const primitives::BlockHash block,
uint64_t  maximum 
) const
overridevirtual

Get a chain of blocks before provided block including its

Parameters
block,towhich the chain is ended
maximumnumber of blocks to be retrieved
Returns
chain or blocks or error

Implements kagome::blockchain::BlockTree.

Definition at line 803 of file block_tree_impl.cpp.

const primitives::BlockHash & kagome::blockchain::BlockTreeImpl::getGenesisBlockHash ( ) const
overridevirtual
Returns
hash of genesis block

Implements kagome::blockchain::BlockTree.

Definition at line 387 of file block_tree_impl.cpp.

primitives::BlockInfo kagome::blockchain::BlockTreeImpl::getLastFinalized ( ) const
overridevirtual

Get the last finalized block

Returns
hash of the block

Implements kagome::blockchain::BlockTree.

Definition at line 1053 of file block_tree_impl.cpp.

std::vector< primitives::BlockHash > kagome::blockchain::BlockTreeImpl::getLeaves ( ) const
overridevirtual

Get all leaves of our tree

Returns
collection of the leaves

Implements kagome::blockchain::BlockTree.

Definition at line 1025 of file block_tree_impl.cpp.

std::vector< primitives::BlockHash > kagome::blockchain::BlockTreeImpl::getLeavesSorted ( ) const
private
Returns
the tree leaves sorted by their depth

Definition at line 1059 of file block_tree_impl.cpp.

outcome::result< bool > kagome::blockchain::BlockTreeImpl::hasBlockHeader ( const primitives::BlockId block) const
overridevirtual

Checks containing of block header by provided block id

Parameters
blockid of the block header we are checking
Returns
containing block header or does not, or error

Implements kagome::blockchain::BlockTree.

Definition at line 730 of file block_tree_impl.cpp.

bool kagome::blockchain::BlockTreeImpl::hasDirectChain ( const primitives::BlockHash ancestor,
const primitives::BlockHash descendant 
) const
overridevirtual

Check if one block is ancestor of second one (direct chain exists)

Parameters
ancestor- block, which is closest to the genesis
descendant- block, which is farthest from the genesis
Returns
true if
Parameters
ancestoris ancestor of
descendant

Implements kagome::blockchain::BlockTree.

Definition at line 865 of file block_tree_impl.cpp.

outcome::result< void > kagome::blockchain::BlockTreeImpl::prune ( const std::shared_ptr< TreeNode > &  lastFinalizedNode)
private

Definition at line 1094 of file block_tree_impl.cpp.

outcome::result< void > kagome::blockchain::BlockTreeImpl::recover ( primitives::BlockId  target_block,
std::shared_ptr< BlockStorage storage,
std::shared_ptr< BlockHeaderRepository header_repo,
std::shared_ptr< const storage::trie::TrieStorage trie_storage,
std::shared_ptr< blockchain::BlockTree block_tree 
)
static

Recover block tree state at provided block.

Definition at line 239 of file block_tree_impl.cpp.

outcome::result< void > kagome::blockchain::BlockTreeImpl::removeLeaf ( const primitives::BlockHash block_hash)
overridevirtual

Remove leaf

Parameters
block_hash- hash of block to be deleted. The block must be leaf.
Returns
nothing or error

Implements kagome::blockchain::BlockTree.

Definition at line 487 of file block_tree_impl.cpp.

outcome::result< void > kagome::blockchain::BlockTreeImpl::reorganize ( )
private

Definition at line 1161 of file block_tree_impl.cpp.

outcome::result< primitives::BlockHash > kagome::blockchain::BlockTreeImpl::walkBackUntilLess ( const primitives::BlockHash start,
const primitives::BlockNumber limit 
) const
private

Walks the chain backwards starting from

Parameters
startuntil the current block number is less or equal than
limit

Definition at line 1081 of file block_tree_impl.cpp.

Member Data Documentation

primitives::events::ChainSubscriptionEnginePtr kagome::blockchain::BlockTreeImpl::chain_events_engine_
private

Definition at line 179 of file block_tree_impl.hpp.

std::shared_ptr<subscription::ExtrinsicEventKeyRepository> kagome::blockchain::BlockTreeImpl::extrinsic_event_key_repo_
private

Definition at line 182 of file block_tree_impl.hpp.

primitives::events::ExtrinsicSubscriptionEnginePtr kagome::blockchain::BlockTreeImpl::extrinsic_events_engine_
private

Definition at line 180 of file block_tree_impl.hpp.

std::shared_ptr<network::ExtrinsicObserver> kagome::blockchain::BlockTreeImpl::extrinsic_observer_
private

Definition at line 176 of file block_tree_impl.hpp.

std::optional<primitives::BlockHash> kagome::blockchain::BlockTreeImpl::genesis_block_hash_
private

Definition at line 188 of file block_tree_impl.hpp.

std::shared_ptr<crypto::Hasher> kagome::blockchain::BlockTreeImpl::hasher_
private

Definition at line 178 of file block_tree_impl.hpp.

std::shared_ptr<BlockHeaderRepository> kagome::blockchain::BlockTreeImpl::header_repo_
private

Definition at line 171 of file block_tree_impl.hpp.

std::shared_ptr<const class JustificationStoragePolicy> kagome::blockchain::BlockTreeImpl::justification_storage_policy_
private

Definition at line 186 of file block_tree_impl.hpp.

log::Logger kagome::blockchain::BlockTreeImpl::log_ = log::createLogger("BlockTree", "blockchain")
private

Definition at line 190 of file block_tree_impl.hpp.

metrics::Gauge* kagome::blockchain::BlockTreeImpl::metric_best_block_height_
private

Definition at line 194 of file block_tree_impl.hpp.

metrics::Gauge* kagome::blockchain::BlockTreeImpl::metric_finalized_block_height_
private

Definition at line 195 of file block_tree_impl.hpp.

metrics::Gauge* kagome::blockchain::BlockTreeImpl::metric_known_chain_leaves_
private

Definition at line 196 of file block_tree_impl.hpp.

metrics::RegistryPtr kagome::blockchain::BlockTreeImpl::metrics_registry_ = metrics::createRegistry()
private

Definition at line 193 of file block_tree_impl.hpp.

std::shared_ptr<BlockStorage> kagome::blockchain::BlockTreeImpl::storage_
private

Definition at line 172 of file block_tree_impl.hpp.

telemetry::Telemetry kagome::blockchain::BlockTreeImpl::telemetry_ = telemetry::createTelemetryService()
private

Definition at line 197 of file block_tree_impl.hpp.

std::unique_ptr<CachedTree> kagome::blockchain::BlockTreeImpl::tree_
private

Definition at line 174 of file block_tree_impl.hpp.

std::shared_ptr<storage::changes_trie::ChangesTracker> kagome::blockchain::BlockTreeImpl::trie_changes_tracker_
private

Definition at line 184 of file block_tree_impl.hpp.


The documentation for this class was generated from the following files: