Skip to content

src/blockchain/impl/storage_util.hpp

Namespaces

Name
sgns
sgns::blockchain
sgns::blockchain::prefix

Types

Name
enum uint8_t Prefix
enum class KeyValueRepositoryError

Functions

Name
base::Buffer prependPrefix(const base::Buffer & key, prefix::Prefix key_column)
outcome::result< void > putWithPrefix(crdt::GlobalDB & db, prefix::Prefix prefix, primitives::BlockNumber num, base::Hash256 block_hash, const base::Buffer & value)
outcome::result< base::Buffer > getWithPrefix(crdt::GlobalDB & db, prefix::Prefix prefix, const primitives::BlockId & block_id)
base::Buffer NumberToBuffer(primitives::BlockNumber n)
base::Buffer numberAndHashToLookupKey(primitives::BlockNumber number, const base::Hash256 & hash)
outcome::result< primitives::BlockNumber > BufferToNumber(const base::Buffer & key)
bool isNotFoundError(outcome::result< void > result)
OUTCOME_HPP_DECLARE_ERROR_2(sgns::blockchain , KeyValueRepositoryError )

Types Documentation

enum Prefix

Enumerator Value Description
ID_TO_LOOKUP_KEY 3
HEADER 4
BLOCK_DATA 5
JUSTIFICATION 6
TRIE_NODE 7

enum KeyValueRepositoryError

Enumerator Value Description
INVALID_KEY 1

Errors that might occur during work with storage

Functions Documentation

function prependPrefix

base::Buffer prependPrefix(
    const base::Buffer & key,
    prefix::Prefix key_column
)

Parameters:

  • key key buffer
  • key_column prefix keyspace

Return: key_column|key

Concatenate prefix with key.

function putWithPrefix

outcome::result< void > putWithPrefix(
    crdt::GlobalDB & db,
    prefix::Prefix prefix,
    primitives::BlockNumber num,
    base::Hash256 block_hash,
    const base::Buffer & value
)

Parameters:

  • db database to put the entry to
  • prefix keyspace for the entry value
  • num block number that could be used to retrieve the value
  • block_hash block hash that could be used to retrieve the value
  • value data to be put to the storage

Return: storage error if any

Put an entry to key space prefix and corresponding lookup keys to ID_TO_LOOKUP_KEY space.

function getWithPrefix

outcome::result< base::Buffer > getWithPrefix(
    crdt::GlobalDB & db,
    prefix::Prefix prefix,
    const primitives::BlockId & block_id
)

Parameters:

  • db database to get the entry from
  • prefix prefix with which the entry was put into
  • block_id id of the block to get entry for

Return: encoded entry or error

Get an entry from the database.

function NumberToBuffer

base::Buffer NumberToBuffer(
    primitives::BlockNumber n
)

Convert block number into short lookup key (LE representation) for blocks that are in the canonical chain.

In the current database schema, this kind of key is only used for lookups into an index, NOT for storing header data or others.

base::Buffer retval;

Little endian for ( std::size_t i = 0; i < sizeof(primitives::BlockNumber); ++i ) { retval.putUint8(static_cast((n >> (i * 8)) & 0xffu)) ; } return retval;

function numberAndHashToLookupKey

base::Buffer numberAndHashToLookupKey(
    primitives::BlockNumber number,
    const base::Hash256 & hash
)

Convert number and hash into long lookup key for blocks that are not in the canonical chain.

function BufferToNumber

outcome::result< primitives::BlockNumber > BufferToNumber(
    const base::Buffer & key
)

Converts buffer data to a block number

function isNotFoundError

bool isNotFoundError(
    outcome::result< void > result
)

For a persistant map based storage checks whether result should be considered as NOT FOUND error

function OUTCOME_HPP_DECLARE_ERROR_2

OUTCOME_HPP_DECLARE_ERROR_2(
    sgns::blockchain ,
    KeyValueRepositoryError 
)

Source code

#ifndef SUPERGENIUS_CORE_BLOCKCHAIN_IMPL_PERSISTENT_MAP_UTIL_HPP
#define SUPERGENIUS_CORE_BLOCKCHAIN_IMPL_PERSISTENT_MAP_UTIL_HPP

#include "base/buffer.hpp"
#include "primitives/block_id.hpp"
#include "crdt/globaldb/globaldb.hpp"
#include "crdt/globaldb/keypair_file_storage.hpp"


namespace sgns::blockchain {

  namespace prefix {
    enum Prefix : uint8_t {
      // mapping of block id to a storage lookup key
      ID_TO_LOOKUP_KEY = 3,

      // block headers
      HEADER = 4,

      // body of the block (extrinsics)
      BLOCK_DATA = 5,

      // justification of the finalized block
      JUSTIFICATION = 6,

      // node of a trie db
      TRIE_NODE = 7
    };
  }

  enum class KeyValueRepositoryError { INVALID_KEY = 1 };

  base::Buffer prependPrefix(const base::Buffer &key,
                               prefix::Prefix key_column);

  outcome::result<void> putWithPrefix(crdt::GlobalDB &db,
                                      prefix::Prefix prefix,
                                      primitives::BlockNumber num,
                                      base::Hash256 block_hash,
                                      const base::Buffer &value);

  outcome::result<base::Buffer> getWithPrefix(
      crdt::GlobalDB &db,
      prefix::Prefix prefix,
      const primitives::BlockId &block_id);

  base::Buffer NumberToBuffer(primitives::BlockNumber n);

  base::Buffer numberAndHashToLookupKey(primitives::BlockNumber number,
                                          const base::Hash256 &hash);

  outcome::result<primitives::BlockNumber> BufferToNumber(
      const base::Buffer &key);

  bool isNotFoundError(outcome::result<void> result);

}  // namespace sgns::blockchain

OUTCOME_HPP_DECLARE_ERROR_2(sgns::blockchain, KeyValueRepositoryError);

#endif  // SUPERGENIUS_CORE_BLOCKCHAIN_IMPL_PERSISTENT_MAP_UTIL_HPP

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