Kagome
Polkadot Runtime Engine in C++17
transcript.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_TRANSACRIPT_HPP
7 #define KAGOME_TRANSACRIPT_HPP
8 
9 #include "primitives/strobe.hpp"
10 
11 namespace kagome::primitives {
12 
17  class Transcript final {
19 
20  template <typename T>
21  inline void decompose(const T &value, uint8_t (&dst)[sizeof(value)]) {
22  static_assert(std::is_pod_v<T>, "T must be pod!");
23  static_assert(!std::is_reference_v<T>, "T must not be a reference!");
24 
25  for (size_t i = 0; i < sizeof(value); ++i) {
26 #if __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__
27  dst[sizeof(value) - i - 1] =
28 #else
29  dst[i] =
30 #endif
31  static_cast<uint8_t>((value >> (8 * i)) & 0xff);
32  }
33  }
34 
35  public:
36  Transcript() = default;
37 
38  Transcript(const Transcript &) = default;
39  Transcript &operator=(const Transcript &) = default;
40 
41  Transcript(Transcript &&) = delete;
42  Transcript &operator=(Transcript &&) = delete;
43 
44  template <typename T, size_t N>
45  void initialize(const T (&label)[N]) {
46  strobe_.initialize(
47  (uint8_t[11]){'M', 'e', 'r', 'l', 'i', 'n', ' ', 'v', '1', '.', '0'});
48  append_message((uint8_t[]){'d', 'o', 'm', '-', 's', 'e', 'p'}, label);
49  }
50 
51  template <typename T, size_t N, typename K, size_t M>
52  void append_message(const T (&label)[N], const K (&msg)[M]) {
53  const uint32_t data_len = sizeof(msg);
54  strobe_.metaAd<false>(label);
55 
56  uint8_t tmp[sizeof(data_len)];
57  decompose(data_len, tmp);
58 
59  strobe_.metaAd<true>(tmp);
60  strobe_.ad<false>(msg);
61  }
62 
63  template <typename T, size_t N>
64  void append_message(const T (&label)[N], const uint64_t value) {
65  uint8_t tmp[sizeof(value)];
66  decompose(value, tmp);
67  append_message(label, tmp);
68  }
69 
70  auto data() {
71  return strobe_.data();
72  }
73 
74  auto data() const {
75  return strobe_.data();
76  }
77 
78  bool operator==(const Transcript &other) const {
79  return other.strobe_.data() == strobe_.data();
80  }
81  };
82 
83 } // namespace kagome::primitives
84 
85 #endif // KAGOME_TRANSACRIPT_HPP
void metaAd(const T(&label)[N])
Definition: strobe.hpp:212
bool operator==(const Transcript &other) const
Definition: transcript.hpp:78
void initialize(const T(&label)[N])
Definition: strobe.hpp:167
void initialize(const T(&label)[N])
Definition: transcript.hpp:45
Transcript & operator=(const Transcript &)=default
void append_message(const T(&label)[N], const K(&msg)[M])
Definition: transcript.hpp:52
void ad(const T(&src)[N])
Definition: strobe.hpp:206
void append_message(const T(&label)[N], const uint64_t value)
Definition: transcript.hpp:64
void decompose(const T &value, uint8_t(&dst)[sizeof(value)])
Definition: transcript.hpp:21