Kagome
Polkadot Runtime Engine in C++17
common.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_PRIMITIVES_COMMON_HPP
7 #define KAGOME_CORE_PRIMITIVES_COMMON_HPP
8 
9 #include <cstdint>
10 
11 #include <fmt/format.h>
12 #include <boost/operators.hpp>
13 
14 #include "common/blob.hpp"
16 
17 namespace kagome::primitives {
18  using BlockNumber = uint32_t;
19  using BlockHash = common::Hash256;
20  using ThreadNumber = size_t;
21 
22  namespace detail {
23  // base data structure for the types describing block information
24  // (BlockInfo, Prevote, Precommit, PrimaryPropose)
25  template <typename Tag>
26  struct BlockInfoT : public boost::equality_comparable<BlockInfoT<Tag>>,
27  public boost::less_than_comparable<BlockInfoT<Tag>> {
28  BlockInfoT() = default;
29 
30  BlockInfoT(const BlockNumber &n, const BlockHash &h)
31  : number(n), hash(h) {}
32 
33  BlockInfoT(const BlockHash &h, const BlockNumber &n)
34  : number(n), hash(h) {}
35 
38 
39  bool operator==(const BlockInfoT<Tag> &o) const {
40  return number == o.number && hash == o.hash;
41  }
42 
43  bool operator<(const BlockInfoT<Tag> &o) const {
44  return number < o.number or (number == o.number and hash < o.hash);
45  }
46  };
47 
48  template <class Stream,
49  typename Tag,
50  typename = std::enable_if_t<Stream::is_encoder_stream>>
51  Stream &operator<<(Stream &s, const BlockInfoT<Tag> &msg) {
52  return s << msg.hash << msg.number;
53  }
54 
55  template <class Stream,
56  typename Tag,
57  typename = std::enable_if_t<Stream::is_decoder_stream>>
59  return s >> msg.hash >> msg.number;
60  }
61  } // namespace detail
62 
64 
65 } // namespace kagome::primitives
66 
67 template <typename Tag>
68 struct fmt::formatter<kagome::primitives::detail::BlockInfoT<Tag>> {
69  // Presentation format: 's' - short, 'l' - long.
70  char presentation = 's';
71 
72  // Parses format specifications of the form ['s' | 'l'].
73  constexpr auto parse(format_parse_context &ctx) -> decltype(ctx.begin()) {
74  // Parse the presentation format and store it in the formatter:
75  auto it = ctx.begin(), end = ctx.end();
76  if (it != end && (*it == 's' || *it == 'l')) {
77  presentation = *it++;
78  }
79 
80  // Check if reached the end of the range:
81  if (it != end && *it != '}') {
82  throw format_error("invalid format");
83  }
84 
85  // Return an iterator past the end of the parsed range:
86  return it;
87  }
88 
89  // Formats the BlockInfo using the parsed format specification (presentation)
90  // stored in this formatter.
91  template <typename FormatContext>
93  FormatContext &ctx) -> decltype(ctx.out()) {
94  // ctx.out() is an output iterator to write to.
95 
96  if (presentation == 's') {
97  static_assert(decltype(block_info.hash)::size() > 4);
98  return format_to(
99  ctx.out(),
100  "#{} (0x{:04x}…{:04x})",
101  block_info.number,
102  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
103  htobe16(*reinterpret_cast<const uint16_t *>(block_info.hash.data())),
104  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
105  htobe16(*reinterpret_cast<const uint16_t *>(block_info.hash.data()
106  + block_info.hash.size()
107  - sizeof(uint16_t))));
108  }
109 
110  return format_to(
111  ctx.out(), "#{} (0x{})", block_info.number, block_info.hash.toHex());
112  }
113 };
114 
115 #endif // KAGOME_CORE_PRIMITIVES_COMMON_HPP
Stream & operator>>(Stream &s, BlockInfoT< Tag > &msg)
Definition: common.hpp:58
bool operator==(const BlockInfoT< Tag > &o) const
Definition: common.hpp:39
constexpr auto parse(format_parse_context &ctx) -> decltype(ctx.begin())
Definition: common.hpp:73
Blob< 32 > Hash256
Definition: blob.hpp:230
auto format(const kagome::primitives::detail::BlockInfoT< Tag > &block_info, FormatContext &ctx) -> decltype(ctx.out())
Definition: common.hpp:92
BlockInfoT(const BlockNumber &n, const BlockHash &h)
Definition: common.hpp:30
uint32_t BlockNumber
Definition: common.hpp:18
BlockInfoT(const BlockHash &h, const BlockNumber &n)
Definition: common.hpp:33
size_t ThreadNumber
Definition: common.hpp:20
libp2p::connection::Stream Stream