Kagome
Polkadot Runtime Engine in C++17
kagome::common Namespace Reference

Namespaces

 dynamic_variant
 Impl details for boost::variant initialization by type index at runtime.
 
 literals
 

Classes

class  Blob
 
class  BufferView
 
class  MaxSizeException
 
class  SizeLimitedContainer
 
class  SLBuffer
 Class represents arbitrary (including empty) byte buffer. More...
 
class  spin_lock
 
struct  Uri
 
class  VariantBuilder
 
struct  Wrapper
 Make strongly typed structures from different concepts of the equal types. E.g. block height and round number are both uint64_t, but better to be different types. Or, ID and Signature both vectors. More...
 

Typedefs

using byte_t = uint8_t
 
using Hash64 = Blob< 8 >
 
using Hash128 = Blob< 16 >
 
using Hash256 = Blob< 32 >
 
using Hash512 = Blob< 64 >
 
typedef SLBuffer< std::numeric_limits< size_t >::max()> Buffer
 
using BufferMutRef = std::reference_wrapper< Buffer >
 
using BufferConstRef = std::reference_wrapper< const Buffer >
 
using uint128_t = boost::multiprecision::uint128_t
 
using uint256_t = boost::multiprecision::uint256_t
 
template<typename ElementType , size_t MaxSize, typename... Args>
using SLVector = SizeLimitedContainer< std::vector< ElementType, Args... >, MaxSize >
 

Enumerations

enum  BlobError { BlobError::INCORRECT_LENGTH = 1 }
 
enum  UnhexError {
  UnhexError::NOT_ENOUGH_INPUT = 1, UnhexError::NON_HEX_INPUT, UnhexError::VALUE_OUT_OF_RANGE, UnhexError::MISSING_0X_PREFIX,
  UnhexError::UNKNOWN
}
 error codes for exceptions that may occur during unhexing More...
 

Functions

template<size_t N>
std::ostream & operator<< (std::ostream &os, const Blob< N > &blob)
 
template<size_t MaxSize>
std::ostream & operator<< (std::ostream &os, const SLBuffer< MaxSize > &buffer)
 
std::ostream & operator<< (std::ostream &os, BufferView view)
 
std::optional< size_t > getFdLimit ()
 
void setFdLimit (size_t limit)
 
std::string int_to_hex (uint64_t n, size_t fixed_width=2) noexcept
 Converts an integer to an uppercase hex representation. More...
 
std::string hex_upper (gsl::span< const uint8_t > bytes) noexcept
 Converts bytes to uppercase hex representation. More...
 
std::string hex_lower (gsl::span< const uint8_t > bytes) noexcept
 Converts bytes to hex representation. More...
 
std::string hex_lower_0x (gsl::span< const uint8_t > bytes) noexcept
 Converts bytes to hex representation with prefix 0x. More...
 
outcome::result< std::vector< uint8_t > > unhex (std::string_view hex)
 Converts hex representation to bytes. More...
 
outcome::result< std::vector< uint8_t > > unhexWith0x (std::string_view hex)
 Unhex hex-string with 0x in the begining. More...
 
std::string hex_lower_0x (const uint8_t *data, size_t size) noexcept
 Adapter for ptr+size. More...
 
template<class T , typename = std::enable_if<std::is_unsigned_v<T>>>
outcome::result< T > unhexNumber (std::string_view value)
 unhex hex-string with 0x or without it in the beginning More...
 
template<typename T , typename F , typename R = std::invoke_result_t<F, const T &>>
std::optional< R > map_optional (std::optional< T > const &opt, F const &f)
 
template<typename T , typename F , typename R = std::invoke_result_t<F, T &&>>
std::optional< R > map_optional (std::optional< T > &&opt, F const &f)
 
template<typename T , typename F , typename R = std::invoke_result_t<F, const T &>>
outcome::result< R > map_result (outcome::result< T > const &res, F const &f)
 
template<typename T , typename F , typename R = std::invoke_result_t<F, T &&>>
outcome::result< R > map_result (outcome::result< T > &&res, F const &f)
 
template<typename T , typename F , typename R = std::invoke_result_t<F, T const &>>
outcome::result< std::optional< R > > map_result_optional (outcome::result< std::optional< T >> const &res_opt, F const &f)
 
template<typename T , typename F , typename R = std::invoke_result_t<F, T &&>>
outcome::result< std::optional< R > > map_result_optional (outcome::result< std::optional< T >> &&res_opt, F const &f)
 
std::array< uint8_t, 8 > uint64_to_le_bytes (uint64_t number)
 
uint64_t le_bytes_to_uint64 (gsl::span< const uint8_t, 8 > bytes)
 
std::array< uint8_t, 8 > uint64_to_be_bytes (uint64_t number)
 
uint64_t be_bytes_to_uint64 (gsl::span< const uint8_t, 8 > bytes)
 
std::array< uint8_t, 16 > uint128_to_le_bytes (const boost::multiprecision::uint128_t &i)
 
boost::multiprecision::uint128_t le_bytes_to_uint128 (gsl::span< const uint8_t, 16 > bytes)
 
std::array< uint8_t, 16 > uint128_to_be_bytes (const boost::multiprecision::uint128_t &i)
 
boost::multiprecision::uint128_t be_bytes_to_uint128 (gsl::span< const uint8_t, 16 > bytes)
 
std::array< uint8_t, 32 > uint256_to_le_bytes (const boost::multiprecision::uint256_t &i)
 
boost::multiprecision::uint256_t le_bytes_to_uint256 (gsl::span< const uint8_t, 32 > bytes)
 
std::array< uint8_t, 32 > uint256_to_be_bytes (const boost::multiprecision::uint256_t &i)
 
boost::multiprecision::uint256_t be_bytes_to_uint256 (gsl::span< const uint8_t, 32 > bytes)
 
template<typename T , typename = std::enable_if_t<std::is_enum_v<T>>>
void raise (T t)
 throws outcome::result error as boost exception More...
 
template<typename T , typename = std::enable_if_t<!std::is_enum_v<T>>>
void raise (const T &t)
 throws outcome::result error made of error as boost exception More...
 
template<typename T , typename Tag , typename = std::enable_if<std::is_arithmetic<T>::value>>
bool operator< (const Wrapper< T, Tag > &a, const Wrapper< T, Tag > &b)
 

Variables

static const Buffer kEmptyBuffer {}
 

Detailed Description

Copyright Soramitsu Co., Ltd. All Rights Reserved. SPDX-License-Identifier: Apache-2.0

Typedef Documentation

typedef SLBuffer<std::numeric_limits<size_t>::max()> kagome::common::Buffer

Definition at line 244 of file buffer.hpp.

using kagome::common::BufferConstRef = typedef std::reference_wrapper<const Buffer>

Definition at line 249 of file buffer.hpp.

using kagome::common::BufferMutRef = typedef std::reference_wrapper<Buffer>

Definition at line 248 of file buffer.hpp.

using kagome::common::byte_t = typedef uint8_t

Definition at line 104 of file blob.hpp.

using kagome::common::Hash128 = typedef Blob<16>

Definition at line 229 of file blob.hpp.

using kagome::common::Hash256 = typedef Blob<32>

Definition at line 230 of file blob.hpp.

using kagome::common::Hash512 = typedef Blob<64>

Definition at line 231 of file blob.hpp.

using kagome::common::Hash64 = typedef Blob<8>

Definition at line 228 of file blob.hpp.

template<typename ElementType , size_t MaxSize, typename... Args>
using kagome::common::SLVector = typedef SizeLimitedContainer<std::vector<ElementType, Args...>, MaxSize>

Definition at line 402 of file size_limited_containers.hpp.

using kagome::common::uint128_t = typedef boost::multiprecision::uint128_t

Definition at line 14 of file mp_utils.hpp.

using kagome::common::uint256_t = typedef boost::multiprecision::uint256_t

Definition at line 15 of file mp_utils.hpp.

Enumeration Type Documentation

Error codes for exceptions that may occur during blob initialization

Enumerator
INCORRECT_LENGTH 

Definition at line 102 of file blob.hpp.

error codes for exceptions that may occur during unhexing

Enumerator
NOT_ENOUGH_INPUT 
NON_HEX_INPUT 
VALUE_OUT_OF_RANGE 
MISSING_0X_PREFIX 
UNKNOWN 

Definition at line 20 of file hexutil.hpp.

Function Documentation

uint128_t kagome::common::be_bytes_to_uint128 ( gsl::span< const uint8_t, 16 >  bytes)

Definition at line 91 of file mp_utils.cpp.

uint256_t kagome::common::be_bytes_to_uint256 ( gsl::span< const uint8_t, 32 >  bytes)

Definition at line 111 of file mp_utils.cpp.

uint64_t kagome::common::be_bytes_to_uint64 ( gsl::span< const uint8_t, 8 >  bytes)

Definition at line 70 of file mp_utils.cpp.

std::optional< size_t > kagome::common::getFdLimit ( )

Definition at line 36 of file fd_limit.cpp.

std::string kagome::common::hex_lower ( gsl::span< const uint8_t >  bytes)
noexcept

Converts bytes to hex representation.

Parameters
arraybytes
lenlength of bytes
Returns
hexstring

Definition at line 52 of file hexutil.cpp.

std::string kagome::common::hex_lower_0x ( gsl::span< const uint8_t >  bytes)
noexcept

Converts bytes to hex representation with prefix 0x.

Parameters
arraybytes
Returns
hexstring

Definition at line 58 of file hexutil.cpp.

std::string kagome::common::hex_lower_0x ( const uint8_t *  data,
size_t  size 
)
inlinenoexcept

Adapter for ptr+size.

Definition at line 59 of file hexutil.hpp.

std::string kagome::common::hex_upper ( gsl::span< const uint8_t >  bytes)
noexcept

Converts bytes to uppercase hex representation.

Parameters
arraybytes
lenlength of bytes
Returns
hexstring

Definition at line 46 of file hexutil.cpp.

std::string kagome::common::int_to_hex ( uint64_t  n,
size_t  fixed_width 
)
noexcept

Converts an integer to an uppercase hex representation.

Definition at line 30 of file hexutil.cpp.

uint128_t kagome::common::le_bytes_to_uint128 ( gsl::span< const uint8_t, 16 >  bytes)

Definition at line 81 of file mp_utils.cpp.

uint256_t kagome::common::le_bytes_to_uint256 ( gsl::span< const uint8_t, 32 >  bytes)

Definition at line 101 of file mp_utils.cpp.

uint64_t kagome::common::le_bytes_to_uint64 ( gsl::span< const uint8_t, 8 >  bytes)

Definition at line 58 of file mp_utils.cpp.

template<typename T , typename F , typename R = std::invoke_result_t<F, const T &>>
std::optional<R> kagome::common::map_optional ( std::optional< T > const &  opt,
F const &  f 
)

Applies

  • f to the value, stored in an optional.
    Returns
    a new optional with the result of
  • f call, in case optional contains a value. Otherwise, just returns an std::nullopt.

Definition at line 24 of file monadic_utils.hpp.

template<typename T , typename F , typename R = std::invoke_result_t<F, T &&>>
std::optional<R> kagome::common::map_optional ( std::optional< T > &&  opt,
F const &  f 
)

Applies

  • f to the value, stored in an optional.
    Returns
    a new optional with the result of
  • f call, in case optional contains a value. Otherwise, just returns an std::nullopt.

Definition at line 37 of file monadic_utils.hpp.

template<typename T , typename F , typename R = std::invoke_result_t<F, const T &>>
outcome::result<R> kagome::common::map_result ( outcome::result< T > const &  res,
F const &  f 
)

Applies

  • f to the value, stored in an outcome::result.
    Returns
    a new outcome::result with the result of
  • f call, in case outcome::result contains a value. Otherwise, just returns the contained error.

Definition at line 53 of file monadic_utils.hpp.

template<typename T , typename F , typename R = std::invoke_result_t<F, T &&>>
outcome::result<R> kagome::common::map_result ( outcome::result< T > &&  res,
F const &  f 
)

Applies

  • f to the value, stored in an outcome::result.
    Returns
    a new outcome::result with the result of
  • f call, in case outcome::result contains a value. Otherwise, just returns the contained error.

Definition at line 67 of file monadic_utils.hpp.

template<typename T , typename F , typename R = std::invoke_result_t<F, T const &>>
outcome::result<std::optional<R> > kagome::common::map_result_optional ( outcome::result< std::optional< T >> const &  res_opt,
F const &  f 
)

Applies

  • f to the value, stored in an optional wrapped in an outcome::result.
    Returns
    a new outcome::result of optional with the result of
  • f call, in case outcome::result AND optional both contain a value. Otherwise, just returns the contained error OR std::nullopt.

Definition at line 84 of file monadic_utils.hpp.

template<typename T , typename F , typename R = std::invoke_result_t<F, T &&>>
outcome::result<std::optional<R> > kagome::common::map_result_optional ( outcome::result< std::optional< T >> &&  res_opt,
F const &  f 
)

Applies

  • f to the value, stored in an optional wrapped in an outcome::result.
    Returns
    a new outcome::result of optional with the result of
  • f call, in case outcome::result AND optional both contain a value. Otherwise, just returns the contained error OR std::nullopt.

Definition at line 99 of file monadic_utils.hpp.

template<typename T , typename Tag , typename = std::enable_if<std::is_arithmetic<T>::value>>
bool kagome::common::operator< ( const Wrapper< T, Tag > &  a,
const Wrapper< T, Tag > &  b 
)

Definition at line 50 of file wrapper.hpp.

std::ostream& kagome::common::operator<< ( std::ostream &  os,
BufferView  view 
)
inline

Definition at line 59 of file buffer_view.hpp.

template<size_t N>
std::ostream& kagome::common::operator<< ( std::ostream &  os,
const Blob< N > &  blob 
)
inline

Definition at line 234 of file blob.hpp.

template<size_t MaxSize>
std::ostream& kagome::common::operator<< ( std::ostream &  os,
const SLBuffer< MaxSize > &  buffer 
)
inline

Definition at line 239 of file buffer.hpp.

template<typename T , typename = std::enable_if_t<std::is_enum_v<T>>>
void kagome::common::raise ( t)

throws outcome::result error as boost exception

Template Parameters
Tenum error type, only outcome::result enums are allowed
Parameters
terror value

Definition at line 19 of file outcome_throw.hpp.

template<typename T , typename = std::enable_if_t<!std::is_enum_v<T>>>
void kagome::common::raise ( const T &  t)

throws outcome::result error made of error as boost exception

Template Parameters
Toutcome error type
Parameters
toutcome error value

Definition at line 30 of file outcome_throw.hpp.

void kagome::common::setFdLimit ( size_t  limit)

Definition at line 44 of file fd_limit.cpp.

std::array< uint8_t, 16 > kagome::common::uint128_to_be_bytes ( const boost::multiprecision::uint128_t &  i)

Definition at line 86 of file mp_utils.cpp.

std::array< uint8_t, 16 > kagome::common::uint128_to_le_bytes ( const boost::multiprecision::uint128_t &  i)

Definition at line 76 of file mp_utils.cpp.

std::array< uint8_t, 32 > kagome::common::uint256_to_be_bytes ( const boost::multiprecision::uint256_t &  i)

Definition at line 106 of file mp_utils.cpp.

std::array< uint8_t, 32 > kagome::common::uint256_to_le_bytes ( const boost::multiprecision::uint256_t &  i)

Definition at line 96 of file mp_utils.cpp.

std::array< uint8_t, 8 > kagome::common::uint64_to_be_bytes ( uint64_t  number)

Definition at line 64 of file mp_utils.cpp.

std::array< uint8_t, 8 > kagome::common::uint64_to_le_bytes ( uint64_t  number)

Definition at line 52 of file mp_utils.cpp.

outcome::result< std::vector< uint8_t > > kagome::common::unhex ( std::string_view  hex)

Converts hex representation to bytes.

Parameters
arrayindividual chars
lenlength of chars
Returns
result containing array of bytes if input string is hex encoded and has even length
Note
reads both uppercase and lowercase hexstrings
See also
https://www.boost.org/doc/libs/1_51_0/libs/algorithm/doc/html/the_boost_algorithm_library/Misc/hex.html

Definition at line 70 of file hexutil.cpp.

template<class T , typename = std::enable_if<std::is_unsigned_v<T>>>
outcome::result<T> kagome::common::unhexNumber ( std::string_view  value)

unhex hex-string with 0x or without it in the beginning

Template Parameters
Tunsigned integer value type to decode
Parameters
valuesource hex string
Returns
unhexed value

Definition at line 91 of file hexutil.hpp.

outcome::result< std::vector< uint8_t > > kagome::common::unhexWith0x ( std::string_view  hex)

Unhex hex-string with 0x in the begining.

Parameters
hexhex string with 0x in the beginning
Returns
unhexed buffer

Definition at line 89 of file hexutil.cpp.

Variable Documentation

const Buffer kagome::common::kEmptyBuffer {}
inlinestatic

Definition at line 246 of file buffer.hpp.