Kagome
Polkadot Runtime Engine in C++17
kagome::blockchain::BlockTree Class Referenceabstract

#include <block_tree.hpp>

Inheritance diagram for kagome::blockchain::BlockTree:
Collaboration diagram for kagome::blockchain::BlockTree:

Public Types

enum  GetChainDirection { GetChainDirection::ASCEND, GetChainDirection::DESCEND }
 
using BlockHashVecRes = outcome::result< std::vector< primitives::BlockHash >>
 

Public Member Functions

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

Detailed Description

Storage for blocks, which has a form of tree; it serves two functions:

  • keep tracking of all finalized blocks (they are kept in the non-volatile storage)
  • work with blocks, which participate in the current round of BABE block production (handling forks, pruning the blocks, resolving child-parent relations, etc)

Definition at line 30 of file block_tree.hpp.

Member Typedef Documentation

Definition at line 32 of file block_tree.hpp.

Member Enumeration Documentation

Enumerator
ASCEND 
DESCEND 

Definition at line 140 of file block_tree.hpp.

Constructor & Destructor Documentation

virtual kagome::blockchain::BlockTree::~BlockTree ( )
virtualdefault

Member Function Documentation

virtual outcome::result<void> kagome::blockchain::BlockTree::addBlock ( const primitives::Block block)
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

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

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual outcome::result<void> kagome::blockchain::BlockTree::addBlockHeader ( const primitives::BlockHeader header)
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual outcome::result<void> kagome::blockchain::BlockTree::addExistingBlock ( const primitives::BlockHash block_hash,
const primitives::BlockHeader block_header 
)
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual primitives::BlockInfo kagome::blockchain::BlockTree::deepestLeaf ( ) const
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual outcome::result<void> kagome::blockchain::BlockTree::finalize ( const primitives::BlockHash block,
const primitives::Justification justification 
)
pure virtual

Mark the block as finalized and store a finalization justification

Parameters
blockto be finalized
justificationof the finalization
Returns
nothing or error

Implemented in kagome::blockchain::BlockTreeImpl.

virtual BlockHashVecRes kagome::blockchain::BlockTree::getBestChainFromBlock ( const primitives::BlockHash block,
uint64_t  maximum 
) const
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

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

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual outcome::result<primitives::BlockBody> kagome::blockchain::BlockTree::getBlockBody ( const primitives::BlockId block) const
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual outcome::result<primitives::BlockHeader> kagome::blockchain::BlockTree::getBlockHeader ( const primitives::BlockId block) const
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual outcome::result<primitives::Justification> kagome::blockchain::BlockTree::getBlockJustification ( const primitives::BlockId block) const
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual BlockHashVecRes kagome::blockchain::BlockTree::getChainByBlocks ( const primitives::BlockHash ancestor,
const primitives::BlockHash descendant 
) const
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual BlockHashVecRes kagome::blockchain::BlockTree::getChildren ( const primitives::BlockHash block) const
pure virtual

Get children of the block with specified hash

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual BlockHashVecRes kagome::blockchain::BlockTree::getDescendingChainToBlock ( const primitives::BlockHash block,
uint64_t  maximum 
) const
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual const primitives::BlockHash& kagome::blockchain::BlockTree::getGenesisBlockHash ( ) const
pure virtual
Returns
hash of genesis block

Implemented in kagome::blockchain::BlockTreeImpl.

virtual primitives::BlockInfo kagome::blockchain::BlockTree::getLastFinalized ( ) const
pure virtual

Get the last finalized block

Returns
hash of the block

Implemented in kagome::blockchain::BlockTreeImpl.

virtual std::vector<primitives::BlockHash> kagome::blockchain::BlockTree::getLeaves ( ) const
pure virtual

Get all leaves of our tree

Returns
collection of the leaves

Implemented in kagome::blockchain::BlockTreeImpl.

virtual outcome::result<bool> kagome::blockchain::BlockTree::hasBlockHeader ( const primitives::BlockId block) const
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual bool kagome::blockchain::BlockTree::hasDirectChain ( const primitives::BlockHash ancestor,
const primitives::BlockHash descendant 
) const
pure virtual

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

Implemented in kagome::blockchain::BlockTreeImpl.

virtual outcome::result<void> kagome::blockchain::BlockTree::removeLeaf ( const primitives::BlockHash block_hash)
pure virtual

Remove leaf

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

Implemented in kagome::blockchain::BlockTreeImpl.


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