Skip to content

sgns::blockchain::BlockTreeImpl

More...

#include <block_tree_impl.hpp>

Inherits from sgns::blockchain::BlockTree, IComponent

Public Types

Name
enum class Error

Public Functions

Name
outcome::result< std::shared_ptr< BlockTreeImpl > > create(std::shared_ptr< BlockHeaderRepository > header_repo, std::shared_ptr< BlockStorage > storage, const primitives::BlockId & last_finalized_block, std::shared_ptr< network::ExtrinsicObserver > extrinsic_observer, std::shared_ptr< crypto::Hasher > hasher)
~BlockTreeImpl() override =default
virtual outcome::result< primitives::BlockHeader > getBlockHeader(const primitives::BlockId & block) const override
virtual outcome::result< primitives::BlockBody > getBlockBody(const primitives::BlockId & block) const override
virtual outcome::result< primitives::Justification > getBlockJustification(const primitives::BlockId & block) const override
virtual outcome::result< void > addBlockHeader(const primitives::BlockHeader & header) override
virtual outcome::result< void > addBlock(const primitives::Block & block) override
virtual outcome::result< void > addBlockBody(primitives::BlockNumber block_number, const primitives::BlockHash & block_hash, const primitives::BlockBody & block_body) override
virtual outcome::result< void > finalize(const primitives::BlockHash & block, const primitives::Justification & justification) override
virtual BlockHashVecRes getChainByBlock(const primitives::BlockHash & block) override
virtual BlockHashVecRes getChainByBlock(const primitives::BlockHash & block, bool ascending, uint64_t maximum) override
virtual BlockHashVecRes getChainByBlocks(const primitives::BlockHash & top_block, const primitives::BlockHash & bottom_block) override
virtual bool hasDirectChain(const primitives::BlockHash & ancestor, const primitives::BlockHash & descendant) override
virtual BlockHashVecRes longestPath() override
virtual primitives::BlockInfo deepestLeaf() const override
virtual outcome::result< primitives::BlockInfo > getBestContaining(const primitives::BlockHash & target_hash, const boost::optional< primitives::BlockNumber > & max_number) const override
Get the most recent block of the best (longest) chain among those that contain a block with target_hash.
virtual std::vector< primitives::BlockHash > getLeaves() const override
virtual BlockHashVecRes getChildren(const primitives::BlockHash & block) override
virtual primitives::BlockInfo getLastFinalized() const override
virtual std::string GetName() override

Additional inherited members

Public Types inherited from sgns::blockchain::BlockTree

Name
using outcome::result< std::vector< primitives::BlockHash > > BlockHashVecRes

Public Functions inherited from sgns::blockchain::BlockTree

Name
~BlockTree() override =default

Public Functions inherited from IComponent

Name
virtual ~IComponent() =default

Detailed Description

class sgns::blockchain::BlockTreeImpl;

Block tree implementation

Public Types Documentation

enum Error

Enumerator Value Description
TARGET_IS_PAST_MAX 1
BLOCK_ON_DEAD_END
BLOCK_NOT_FOUND

Public Functions Documentation

function create

static outcome::result< std::shared_ptr< BlockTreeImpl > > create(
    std::shared_ptr< BlockHeaderRepository > header_repo,
    std::shared_ptr< BlockStorage > storage,
    const primitives::BlockId & last_finalized_block,
    std::shared_ptr< network::ExtrinsicObserver > extrinsic_observer,
    std::shared_ptr< crypto::Hasher > hasher
)

Parameters:

  • header_repo - block headers repository
  • storage - block storage for the tree to be put in
  • last_finalized_block - last finalized block, from which the tree is going to grow
  • extrinsic_observer - extrinsic observer
  • hasher - pointer to the hasher

Return: ptr to the created instance or error

Create an instance of block tree

function ~BlockTreeImpl

~BlockTreeImpl() override =default

function getBlockHeader

virtual outcome::result< primitives::BlockHeader > getBlockHeader(
    const primitives::BlockId & block
) const override

Parameters:

  • block id of the block header we are looking for

Return: result containing block header if it exists, error otherwise

Reimplements: sgns::blockchain::BlockTree::getBlockHeader

Get block header by provided block id

function getBlockBody

virtual outcome::result< primitives::BlockBody > getBlockBody(
    const primitives::BlockId & block
) const override

Parameters:

  • block - id of the block to get body for

Return: body, if the block exists in our storage, error in case it does not exist in our storage, or actual error happens

Reimplements: sgns::blockchain::BlockTree::getBlockBody

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

function getBlockJustification

virtual outcome::result< primitives::Justification > getBlockJustification(
    const primitives::BlockId & block
) const override

Parameters:

  • block - id of the block to get justification for

Return: body, if the block exists in our storage, error in case it does not exist in our storage, or actual error happens

Reimplements: sgns::blockchain::BlockTree::getBlockJustification

Get a justification of the block (if present)

function addBlockHeader

virtual outcome::result< void > addBlockHeader(
    const primitives::BlockHeader & header
) override

Parameters:

  • header that we are adding

Return: result with success if header's parent exists on storage and new header was added. Error otherwise

Reimplements: sgns::blockchain::BlockTree::addBlockHeader

Adds header to the storage

function addBlock

virtual outcome::result< void > addBlock(
    const primitives::Block & block
) override

Parameters:

  • block to be added

Return: nothing or error; if error happens, no changes in the tree are made

Note: if block, which is specified in PARENT_HASH field of (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

Reimplements: sgns::blockchain::BlockTree::addBlock

Add a new block to the tree

function addBlockBody

virtual outcome::result< void > addBlockBody(
    primitives::BlockNumber block_number,
    const primitives::BlockHash & block_hash,
    const primitives::BlockBody & block_body
) override

Parameters:

  • block_number that corresponds to the block which body we are adding
  • block_hash that corresponds to the block which body we are adding
  • block_body that we are adding

Return: result with success if block body was inserted. Error otherwise

Reimplements: sgns::blockchain::BlockTree::addBlockBody

Adds block body to the storage

function finalize

virtual outcome::result< void > finalize(
    const primitives::BlockHash & block,
    const primitives::Justification & justification
) override

Parameters:

  • block to be finalized
  • justification of the finalization

Return: nothing or error

Reimplements: sgns::blockchain::BlockTree::finalize

Mark the block as finalized and store a finalization justification

function getChainByBlock

virtual BlockHashVecRes getChainByBlock(
    const primitives::BlockHash & block
) override

Parameters:

  • block to get a chain from

Return: chain of blocks in top-to-bottom order (from the last finalized block to the provided one) or error

Reimplements: sgns::blockchain::BlockTree::getChainByBlock

Get a chain of blocks from the specified block up to the closest finalized one

function getChainByBlock

virtual BlockHashVecRes getChainByBlock(
    const primitives::BlockHash & block,
    bool ascending,
    uint64_t maximum
) override

Parameters:

  • block block from which the chain is started
  • ascending - if true, the chain will grow up from the provided block (it is the lowest one); if false, down
  • maximum number of blocks to be retrieved

Return: chain or blocks or error

Reimplements: sgns::blockchain::BlockTree::getChainByBlock

Get a chain of blocks from the block

function getChainByBlocks

virtual BlockHashVecRes getChainByBlocks(
    const primitives::BlockHash & top_block,
    const primitives::BlockHash & bottom_block
) override

Parameters:

  • top_block - block, which is at the top of the chain
  • bottom_block - block, which is the bottom of the chain

Return: chain of blocks in top-to-bottom order or error

Reimplements: sgns::blockchain::BlockTree::getChainByBlocks

Get a chain of blocks

function hasDirectChain

virtual bool hasDirectChain(
    const primitives::BlockHash & ancestor,
    const primitives::BlockHash & descendant
) override

Parameters:

  • ancestor - block, which is at the top of the chain
  • descendant - block, which is the bottom of the chain

Return: true if ancestor is ancestor of descendant

Reimplements: sgns::blockchain::BlockTree::hasDirectChain

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

function longestPath

virtual BlockHashVecRes longestPath() override

Return: chain of blocks or error

Note: this function is equivalent to "getChainByBlock(deepestLeaf())"

Reimplements: sgns::blockchain::BlockTree::longestPath

Get a longest path (chain of blocks) from the last finalized block down to the deepest leaf

function deepestLeaf

virtual primitives::BlockInfo deepestLeaf() const override

Return: 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

Reimplements: sgns::blockchain::BlockTree::deepestLeaf

Get a deepest leaf of the tree

function getBestContaining

virtual outcome::result< primitives::BlockInfo > getBestContaining(
    const primitives::BlockHash & target_hash,
    const boost::optional< primitives::BlockNumber > & max_number
) const override

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

Parameters:

  • target_hash is a hash of a block that the chosen chain must contain
  • max_number is the max block number that the resulting block (and the target one) may possess

Reimplements: sgns::blockchain::BlockTree::getBestContaining

function getLeaves

virtual std::vector< primitives::BlockHash > getLeaves() const override

Return: collection of the leaves

Reimplements: sgns::blockchain::BlockTree::getLeaves

Get all leaves of our tree

function getChildren

virtual BlockHashVecRes getChildren(
    const primitives::BlockHash & block
) override

Parameters:

  • block to get children of

Return: collection of children hashes or error

Reimplements: sgns::blockchain::BlockTree::getChildren

Get children of the block with specified hash

function getLastFinalized

virtual primitives::BlockInfo getLastFinalized() const override

Return: hash of the block

Reimplements: sgns::blockchain::BlockTree::getLastFinalized

Get the last finalized block

function GetName

inline virtual std::string GetName() override

Reimplements: IComponent::GetName


Updated on 2026-03-04 at 13:10:43 -0800