Kagome
Polkadot Runtime Engine in C++17
inherent_data.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_INHERENT_DATA_HPP
7 #define KAGOME_INHERENT_DATA_HPP
8 
9 #include <map>
10 #include <vector>
11 
12 #include <boost/iterator_adaptors.hpp>
13 #include <optional>
14 #include <outcome/outcome.hpp>
15 #include "common/blob.hpp"
16 #include "common/buffer.hpp"
17 #include "common/outcome_throw.hpp"
18 #include "scale/scale.hpp"
19 #include "scale/scale_error.hpp"
20 
21 namespace kagome::primitives {
25  enum class InherentDataError {
28  };
29 } // namespace kagome::primitives
30 
32 
33 namespace kagome::primitives {
35 
39  struct InherentData {
48  template <typename T>
49  outcome::result<void> putData(InherentIdentifier identifier,
50  const T &inherent) {
51  auto [it, inserted] =
52  data.try_emplace(std::move(identifier), common::Buffer());
53  if (inserted) {
54  it->second = common::Buffer(scale::encode(inherent).value());
55  return outcome::success();
56  }
58  }
59 
64  template <typename T>
65  void replaceData(InherentIdentifier identifier, const T &inherent) {
66  data[identifier] = common::Buffer(scale::encode(inherent).value());
67  }
68 
72  template <typename T>
73  outcome::result<T> getData(const InherentIdentifier &identifier) const {
74  auto inherent = data.find(identifier);
75  if (inherent != data.end()) {
76  auto buf = inherent->second;
77  return scale::decode<T>(buf);
78  }
80  }
81 
82  bool operator==(const InherentData &rhs) const;
83 
84  bool operator!=(const InherentData &rhs) const;
85 
86  std::map<InherentIdentifier, common::Buffer> data;
87  };
88 
96  template <class Stream,
97  typename = std::enable_if_t<Stream::is_encoder_stream>>
99  const auto &data = v.data;
100  std::vector<std::pair<InherentIdentifier, common::Buffer>> vec;
101  vec.reserve(data.size());
102  for (auto &pair : data) {
103  vec.emplace_back(pair);
104  }
105  s << vec;
106  return s;
107  }
108 
116  template <class Stream,
117  typename = std::enable_if_t<Stream::is_decoder_stream>>
119  std::vector<std::pair<InherentIdentifier, common::Buffer>> vec;
120  s >> vec;
121 
122  for (const auto &item : vec) {
123  // throw if identifier already exists
124  if (v.data.find(item.first) != v.data.end()) {
126  }
127  v.data.insert(item);
128  }
129 
130  return s;
131  }
132 } // namespace kagome::primitives
133 
134 #endif // KAGOME_INHERENT_DATA_HPP
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
void raise(T t)
throws outcome::result error as boost exception
OUTCOME_HPP_DECLARE_ERROR(kagome::primitives, InherentDataError)
std::map< InherentIdentifier, common::Buffer > data
Stream & operator>>(Stream &s, ArithmeticError &v)
void replaceData(InherentIdentifier identifier, const T &inherent)
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
outcome::result< T > getData(const InherentIdentifier &identifier) const
bool operator==(const Transaction &v1, const Transaction &v2)
Definition: transaction.hpp:61
Stream & operator<<(Stream &s, const ArithmeticError &v)
libp2p::connection::Stream Stream
InherentDataError
inherent data encode/decode error codes
outcome::result< void > putData(InherentIdentifier identifier, const T &inherent)