Kagome
Polkadot Runtime Engine in C++17
block_attributes.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_NETWORK_TYPES_BLOCK_ATTRIBUTES_HPP
7 #define KAGOME_CORE_NETWORK_TYPES_BLOCK_ATTRIBUTES_HPP
8 
9 #include <bitset>
10 #include <cstdint>
11 
12 #include "common/outcome_throw.hpp"
13 #include "scale/scale_error.hpp"
14 
15 namespace kagome::network {
16 
21  enum class BlockAttribute : uint8_t {
23  HEADER = 1u,
25  BODY = 1u << 1u,
27  RECEIPT = 1u << 2u,
29  MESSAGE_QUEUE = 1u << 3u,
31  JUSTIFICATION = 1u << 4u
32  };
33 
38  public:
39  BlockAttributes() = default;
40  constexpr BlockAttributes(const BlockAttributes &other) noexcept = default;
41  BlockAttributes(BlockAttributes &&other) noexcept = default;
42  ~BlockAttributes() = default;
43 
44  constexpr BlockAttributes(BlockAttribute attribute) noexcept
45  : attributes(static_cast<uint8_t>(attribute)) {}
46 
47  template <typename T, typename = std::enable_if<std::is_unsigned_v<T>>>
48  constexpr BlockAttributes(T attribute) noexcept : attributes(0) {
49  load(attribute);
50  }
51 
52  inline constexpr BlockAttributes &operator=(const BlockAttributes &other) =
53  default;
54  inline constexpr BlockAttributes &operator=(BlockAttributes &&other) =
55  default;
56 
57  template <typename T, typename = std::enable_if<std::is_unsigned_v<T>>>
58  constexpr void load(T value) {
59  attributes = mask_ & value;
60  }
61 
63  attributes = static_cast<uint8_t>(attribute);
64  return *this;
65  }
66 
67  inline constexpr BlockAttributes operator|(
68  const BlockAttributes &other) const {
69  BlockAttributes result;
70  result.attributes = attributes | static_cast<uint8_t>(other.attributes);
71  return result;
72  }
73 
74  inline constexpr BlockAttributes operator|(
75  const BlockAttribute &attribute) const {
76  BlockAttributes result;
77  result.attributes = attributes | static_cast<uint8_t>(attribute);
78  return result;
79  }
80 
81  inline BlockAttributes &operator|=(const BlockAttributes &other) {
82  attributes |= other.attributes;
83  return *this;
84  }
85 
86  inline BlockAttributes &operator|=(const BlockAttribute &attribute) {
87  attributes |= static_cast<uint8_t>(attribute);
88  return *this;
89  }
90 
91  inline constexpr BlockAttributes operator&(
92  const BlockAttributes &other) const {
93  BlockAttributes result;
94  result.attributes = attributes & static_cast<uint8_t>(other.attributes);
95  return result;
96  }
97 
98  inline constexpr BlockAttributes operator&(
99  const BlockAttribute &attribute) const {
100  BlockAttributes result;
101  result.attributes = attributes & static_cast<uint8_t>(attribute);
102  return result;
103  }
104 
106  attributes &= other.attributes;
107  return *this;
108  }
109 
111  attributes &= static_cast<uint8_t>(attribute);
112  return *this;
113  }
114 
115  inline BlockAttributes operator~() const {
116  BlockAttributes result;
117  result.attributes = ~attributes & mask_;
118  return result;
119  }
120 
121  inline bool operator==(const BlockAttributes &other) const {
122  return attributes == other.attributes;
123  }
124 
125  inline operator bool() const {
126  return attributes != 0;
127  }
128 
129  template <typename T, typename = std::enable_if<std::is_unsigned_v<T>>>
130  inline operator T() const {
131  return attributes;
132  }
133 
134  private:
135  static constexpr uint8_t mask_ = 0b00011111;
136  uint8_t attributes = 0;
137 
138  friend struct std::hash<BlockAttributes>;
139  };
140 
141  inline constexpr BlockAttributes operator|(const BlockAttribute &lhs,
142  const BlockAttribute &rhs) {
143  BlockAttributes result;
144  result.load(static_cast<uint8_t>(lhs) | static_cast<uint8_t>(rhs));
145  return result;
146  }
147 
148  inline constexpr BlockAttributes operator~(const BlockAttribute &attribute) {
149  BlockAttributes result;
150  result.load(~static_cast<uint8_t>(attribute));
151  return result;
152  }
153 
161  template <class Stream,
162  typename = std::enable_if_t<Stream::is_encoder_stream>>
163  Stream &operator<<(Stream &s, const BlockAttributes &v) {
164  return s << static_cast<uint8_t>(v);
165  }
166 
174  template <class Stream,
175  typename = std::enable_if_t<Stream::is_decoder_stream>>
176  Stream &operator>>(Stream &s, BlockAttributes &attributes) {
177  uint8_t value = 0u;
178  s >> value;
179 
180  attributes.load(value);
181  if ((uint8_t)attributes != value) {
182  common::raise(scale::DecodeError::UNEXPECTED_VALUE);
183  }
184  return s;
185  }
186 
187 } // namespace kagome::network
188 
189 template <>
190 struct std::hash<kagome::network::BlockAttributes> {
192  return std::hash<uint8_t>()(attr.attributes);
193  }
194 };
195 
196 #endif // KAGOME_CORE_NETWORK_TYPES_BLOCK_ATTRIBUTES_HPP
Include a justification for the block.
void raise(T t)
throws outcome::result error as boost exception
auto operator()(const kagome::network::BlockAttributes &attr) const
Stream & operator>>(Stream &s, BlockAttributes &attributes)
BlockAttributes & operator&=(BlockAttribute attribute)
constexpr BlockAttributes operator&(const BlockAttribute &attribute) const
constexpr BlockAttributes(T attribute) noexcept
bool operator==(const BlockAttributes &other) const
Stream & operator<<(Stream &s, const BlockAttributes &v)
outputs object of type BlockAttributes to stream
constexpr BlockAttributes operator&(const BlockAttributes &other) const
BlockAttributes & operator|=(const BlockAttribute &attribute)
constexpr BlockAttributes(BlockAttribute attribute) noexcept
constexpr BlockAttributes operator|(const BlockAttribute &attribute) const
constexpr BlockAttributes operator|(const BlockAttributes &other) const
constexpr BlockAttributes operator~(const BlockAttribute &attribute)
constexpr BlockAttributes operator|(const BlockAttribute &lhs, const BlockAttribute &rhs)
BlockAttributes & operator|=(const BlockAttributes &other)
BlockAttributes & operator=(BlockAttribute attribute)
libp2p::connection::Stream Stream
BlockAttributes operator~() const
BlockAttributes & operator&=(const BlockAttributes &other)