Skip to content

sgns::blockchain::BlockTree

More...

#include <block_tree.hpp>

Inherits from IComponent

Inherited by sgns::blockchain::BlockTreeImpl

Public Types

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

Public Functions

Name
~BlockTree() override =default
virtual outcome::result< primitives::BlockHeader > getBlockHeader(const primitives::BlockId & block) const =0
virtual outcome::result< primitives::BlockBody > getBlockBody(const primitives::BlockId & block) const =0
virtual outcome::result< primitives::Justification > getBlockJustification(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 > addBlock(const primitives::Block & block) =0
virtual outcome::result< void > finalize(const primitives::BlockHash & block, const primitives::Justification & justification) =0
virtual BlockHashVecRes getChainByBlock(const primitives::BlockHash & block) =0
virtual BlockHashVecRes getChainByBlock(const primitives::BlockHash & block, bool ascending, uint64_t maximum) =0
virtual BlockHashVecRes getChainByBlocks(const primitives::BlockHash & top_block, const primitives::BlockHash & bottom_block) =0
virtual bool hasDirectChain(const primitives::BlockHash & ancestor, const primitives::BlockHash & descendant) =0
virtual BlockHashVecRes longestPath() =0
virtual primitives::BlockInfo deepestLeaf() const =0
virtual outcome::result< primitives::BlockInfo > getBestContaining(const primitives::BlockHash & target_hash, const boost::optional< primitives::BlockNumber > & max_number) const =0
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 =0
virtual BlockHashVecRes getChildren(const primitives::BlockHash & block) =0
virtual primitives::BlockInfo getLastFinalized() const =0

Additional inherited members

Public Functions inherited from IComponent

Name
virtual ~IComponent() =default
virtual std::string GetName() =0

Detailed Description

struct sgns::blockchain::BlockTree;

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 PRODUCTION block production (handling forks, pruning the blocks, resolving child-parent relations, etc)

Public Types Documentation

using BlockHashVecRes

using sgns::blockchain::BlockTree::BlockHashVecRes = outcome::result<std::vector<primitives::BlockHash>>;

Public Functions Documentation

function ~BlockTree

~BlockTree() override =default

function getBlockHeader

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

Parameters:

  • block id of the block header we are looking for

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::getBlockHeader

Get block header by provided block id

function getBlockBody

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

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::getBlockBody

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

function getBlockJustification

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

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::getBlockJustification

Get a justification of the block (if present)

function addBlockHeader

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

Parameters:

  • header that we are adding

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::addBlockHeader

Adds header to the storage

function addBlockBody

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

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::addBlockBody

Adds block body to the storage

function addBlock

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

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::addBlock

Add a new block to the tree

function finalize

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

Parameters:

  • block to be finalized
  • justification of the finalization

Return: nothing or error

Reimplemented by: sgns::blockchain::BlockTreeImpl::finalize

Mark the block as finalized and store a finalization justification

function getChainByBlock

virtual BlockHashVecRes getChainByBlock(
    const primitives::BlockHash & block
) =0

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::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
) =0

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::getChainByBlock

Get a chain of blocks from the block

function getChainByBlocks

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

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::getChainByBlocks

Get a chain of blocks

function hasDirectChain

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

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::hasDirectChain

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

function longestPath

virtual BlockHashVecRes longestPath() =0

Return: chain of blocks or error

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::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 =0

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::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 =0

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

Reimplemented by: sgns::blockchain::BlockTreeImpl::getBestContaining

function getLeaves

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

Return: collection of the leaves

Reimplemented by: sgns::blockchain::BlockTreeImpl::getLeaves

Get all leaves of our tree

function getChildren

virtual BlockHashVecRes getChildren(
    const primitives::BlockHash & block
) =0

Parameters:

  • block to get children of

Return: collection of children hashes or error

Reimplemented by: sgns::blockchain::BlockTreeImpl::getChildren

Get children of the block with specified hash

function getLastFinalized

virtual primitives::BlockInfo getLastFinalized() const =0

Return: hash of the block

Reimplemented by: sgns::blockchain::BlockTreeImpl::getLastFinalized

Get the last finalized block


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