Kagome
Polkadot Runtime Engine in C++17
types.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_OFFCHAIN_TYPES
7 #define KAGOME_OFFCHAIN_TYPES
8 
9 #include <boost/optional.hpp>
10 #include <boost/variant.hpp>
11 #include <libp2p/multi/multiaddress.hpp>
12 #include <libp2p/peer/peer_id.hpp>
13 
14 #include "common/blob.hpp"
15 #include "common/buffer.hpp"
16 #include "scale/scale.hpp"
17 
18 namespace kagome::offchain {
19 
21  using Timestamp = uint64_t;
22 
24 
25  enum class StorageType : int32_t { Undefined = 0, Persistent = 1, Local = 2 };
26 
27  enum class HttpMethod { Undefined = 0, Get = 1, Post = 2 };
28 
29  using RequestId = int16_t;
30 
31  enum class HttpError : int32_t {
32  Timeout = 0,
33  IoError = 1,
34  InvalidId = 2
35  };
36 
46  using HttpStatus = uint16_t;
47  constexpr HttpStatus InvalidIdentifier(0);
48  constexpr HttpStatus DeadlineHasReached(10);
49  constexpr HttpStatus ErrorHasOccurred(20);
50 
51  struct NoPayload {};
53 
54  struct Success : public NoPayload {};
55  struct Failure : public NoPayload {};
56 
57  template <typename S, typename F>
58  struct Result final : boost::variant<S, F> {
59  using boost::variant<S, F>::variant;
60  bool isSuccess() const {
61  return boost::variant<S, F>::which() == 0;
62  }
63  bool isFailure() const {
64  return boost::variant<S, F>::which() == 1;
65  }
66  S &value() {
67  return boost::relaxed_get<S>(*this);
68  }
69  F &error() {
70  return boost::relaxed_get<F>(*this);
71  }
72  };
73 
76  std::list<libp2p::multi::Multiaddress> address;
77 
79  std::list<libp2p::multi::Multiaddress> address)
80  : peer_id(std::move(peer_id)), address(std::move(address)) {}
81 
83  : peer_id(
84  libp2p::peer::PeerId::fromPublicKey(libp2p::crypto::ProtobufKey{{}})
85  .value()) {}
86  };
87 
88  template <class Stream,
89  typename = std::enable_if_t<Stream::is_encoder_stream>>
91  s << v.peer_id.toVector();
92 
93  s << scale::CompactInteger(v.address.size());
94 
95  for (auto &address : v.address) {
96  s << address.getBytesAddress();
97  }
98 
99  return s;
100  }
101 
102  template <class Stream,
103  typename = std::enable_if_t<Stream::is_decoder_stream>>
105  common::Buffer buff;
106 
107  s >> buff;
108  auto peer_id_res = libp2p::peer::PeerId::fromBytes(buff);
109  if (peer_id_res.has_error()) {
110  throw std::runtime_error(peer_id_res.error().message());
111  }
112  v.peer_id = std::move(peer_id_res.value());
113 
115  s >> size;
116  v.address.resize(size.convert_to<uint64_t>());
117 
118  for (auto &address : v.address) {
119  s >> buff;
120  auto ma_res = libp2p::multi::Multiaddress::create(buff);
121  if (ma_res.has_error()) {
122  throw std::runtime_error(ma_res.error().message());
123  }
124  address = std::move(ma_res.value());
125  }
126 
127  return s;
128  }
129 
130 } // namespace kagome::offchain
131 
132 #endif // KAGOME_OFFCHAIN_TYPES
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
Stream & operator<<(Stream &s, const OpaqueNetworkState &v)
Definition: types.hpp:90
The deadline was reached.
STL namespace.
SCALE_EMPTY_CODER(NoPayload)
uint16_t HttpStatus
HTTP status codes that can get returned by certain Offchain funcs. 0: the specified request identifie...
Definition: types.hpp:46
There was an IO error while processing the request.
libp2p::peer::PeerId PeerId
constexpr HttpStatus InvalidIdentifier(0)
Stream & operator>>(Stream &s, OpaqueNetworkState &v)
Definition: types.hpp:104
scale::CompactInteger CompactInteger
int16_t RequestId
Definition: types.hpp:29
std::list< libp2p::multi::Multiaddress > address
Definition: types.hpp:76
constexpr HttpStatus ErrorHasOccurred(20)
bool isFailure() const
Definition: types.hpp:63
uint64_t Timestamp
Timestamp is milliseconds since UNIX Epoch.
Definition: types.hpp:21
The ID of the request is invalid.
libp2p::connection::Stream Stream
OpaqueNetworkState(libp2p::peer::PeerId peer_id, std::list< libp2p::multi::Multiaddress > address)
Definition: types.hpp:78
bool isSuccess() const
Definition: types.hpp:60
libp2p::peer::PeerId peer_id
Definition: types.hpp:75
constexpr HttpStatus DeadlineHasReached(10)