Kagome
Polkadot Runtime Engine in C++17
value_converter.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CORE_API_EXTRINSIC_RESPONSE_VALUE_CONVERTER_HPP
7 #define KAGOME_CORE_API_EXTRINSIC_RESPONSE_VALUE_CONVERTER_HPP
8 
9 #include <vector>
10 
11 #include <jsonrpc-lean/value.h>
12 #include <boost/range/adaptor/transformed.hpp>
13 
15 #include "common/blob.hpp"
16 #include "common/hexutil.hpp"
17 #include "common/visitor.hpp"
20 #include "primitives/digest.hpp"
22 #include "primitives/extrinsic.hpp"
25 #include "primitives/version.hpp"
26 #include "scale/scale.hpp"
27 
28 namespace kagome::api {
29 
30  namespace {
31  using jString = jsonrpc::Value::String;
32  using jArray = jsonrpc::Value::Array;
33  using jStruct = jsonrpc::Value::Struct;
34  } // namespace
35 
36  inline jsonrpc::Value makeValue(const uint32_t &);
37  inline jsonrpc::Value makeValue(const uint64_t &);
38  inline jsonrpc::Value makeValue(const std::nullptr_t &);
39  inline jsonrpc::Value makeValue(const std::nullopt_t &);
40  inline jsonrpc::Value makeValue(const std::nullopt_t &);
41 
42  template <typename T>
43  inline jsonrpc::Value makeValue(const std::reference_wrapper<T> &v);
44 
45  template <typename T>
46  inline jsonrpc::Value makeValue(const std::optional<T> &val);
47 
48  template <typename... Ts>
49  inline jsonrpc::Value makeValue(const boost::variant<Ts...> &v);
50 
51  template <typename T1, typename T2>
52  inline jsonrpc::Value makeValue(const std::pair<T1, T2> &val);
53 
54  template <typename T>
55  inline jsonrpc::Value makeValue(const std::vector<T> &);
56 
57  template <size_t N>
58  inline jsonrpc::Value makeValue(const common::Blob<N> &);
59 
60  inline jsonrpc::Value makeValue(const common::Buffer &);
61  inline jsonrpc::Value makeValue(common::BufferView);
62  inline jsonrpc::Value makeValue(const primitives::Extrinsic &);
63  inline jsonrpc::Value makeValue(const primitives::RuntimeDispatchInfo &v);
64  inline jsonrpc::Value makeValue(const primitives::DigestItem &);
65  inline jsonrpc::Value makeValue(const primitives::BlockData &);
66  inline jsonrpc::Value makeValue(const primitives::BlockHeader &);
67  inline jsonrpc::Value makeValue(const primitives::Version &);
68  inline jsonrpc::Value makeValue(const primitives::Justification &);
69  inline jsonrpc::Value makeValue(const primitives::RpcMethods &);
70 
71  inline jsonrpc::Value makeValue(const uint32_t &val) {
72  return static_cast<int64_t>(val);
73  }
74 
75  inline jsonrpc::Value makeValue(const uint64_t &val) {
76  return static_cast<int64_t>(val);
77  }
78 
79  inline jsonrpc::Value makeValue(const std::nullptr_t &) {
80  return {};
81  }
82 
83  inline jsonrpc::Value makeValue(const std::nullopt_t &) {
84  return {};
85  }
86 
87  template <typename T>
88  inline jsonrpc::Value makeValue(const T &val) {
89  jsonrpc::Value ret(val);
90  return ret;
91  }
92 
93  template <typename T>
94  inline jsonrpc::Value makeValue(std::remove_reference_t<T> &&val) {
95  return makeValue(val);
96  }
97 
98  template <typename T>
99  inline jsonrpc::Value makeValue(const std::reference_wrapper<T> &v) {
100  return makeValue(v.get());
101  }
102 
103  template <typename T>
104  inline jsonrpc::Value makeValue(const std::optional<T> &val) {
105  if (!val) return {};
106  return makeValue(*val);
107  }
108 
109  template <typename... Ts>
110  inline jsonrpc::Value makeValue(const boost::variant<Ts...> &v) {
111  return visit_in_place(v,
112  [](const auto &value) { return makeValue(value); });
113  }
114 
115  template <typename T1, typename T2>
116  inline jsonrpc::Value makeValue(const std::pair<T1, T2> &val) {
117  jArray data;
118 
119  data.reserve(2);
120  data.emplace_back(makeValue(val.first));
121  data.emplace_back(makeValue(val.second));
122 
123  return data;
124  }
125 
126  template <typename T>
127  inline jsonrpc::Value makeValue(const std::vector<T> &v) {
128  jArray value;
129 
130  value.reserve(v.size());
131  for (auto &item : v) {
132  value.emplace_back(makeValue(item));
133  }
134 
135  return value;
136  }
137 
138  template <size_t N>
139  inline jsonrpc::Value makeValue(const common::Blob<N> &val) {
140  return common::hex_lower_0x(val.data(), val.size());
141  }
142 
143  inline jsonrpc::Value makeValue(const common::Buffer &val) {
144  return common::hex_lower_0x(val.asVector().data(), val.asVector().size());
145  }
146 
147  inline jsonrpc::Value makeValue(common::BufferView val) {
148  return common::hex_lower_0x(val.data(), val.size());
149  }
150 
151  inline jsonrpc::Value makeValue(const primitives::DigestItem &val) {
152  auto result = scale::encode(val);
153  if (result.has_value()) {
154  return common::hex_lower_0x(result.value());
155  }
156  throw jsonrpc::InternalErrorFault("Unable to encode arguments.");
157  }
158 
159  inline jsonrpc::Value makeValue(const primitives::Version &val) {
160  jStruct data;
161  data["authoringVersion"] = makeValue(val.authoring_version);
162 
163  data["specName"] = makeValue(val.spec_name);
164  data["implName"] = makeValue(val.impl_name);
165 
166  data["specVersion"] = makeValue(val.spec_version);
167  data["implVersion"] = makeValue(val.impl_version);
168  data["transactionVersion"] = makeValue(val.transaction_version);
169  data["stateVersion"] = makeValue(val.state_version);
170 
171  jArray apis;
172  std::transform(val.apis.begin(),
173  val.apis.end(),
174  std::back_inserter(apis),
175  [](const auto &api) {
176  jArray api_data;
177  api_data.emplace_back(hex_lower_0x(api.first));
178  api_data.emplace_back(makeValue(api.second));
179  return api_data;
180  });
181 
182  data["apis"] = std::move(apis);
183  return data;
184  }
185 
186  inline jsonrpc::Value makeValue(const primitives::BlockHeader &val) {
187  jStruct data;
188  data["parentHash"] = makeValue(common::hex_lower_0x(val.parent_hash));
189  std::stringstream stream;
190  stream << "0x" << std::hex << val.number;
191  data["number"] = makeValue(stream.str());
192  data["stateRoot"] = makeValue(common::hex_lower_0x(val.state_root));
193  data["extrinsicsRoot"] =
195 
196  jArray logs;
197  logs.reserve(val.digest.size());
198  for (const auto &d : val.digest) {
199  logs.emplace_back(makeValue(d));
200  }
201 
202  jStruct digest;
203  digest["logs"] = std::move(logs);
204 
205  data["digest"] = std::move(digest);
206  return data;
207  }
208 
209  inline jsonrpc::Value makeValue(const primitives::Justification &val) {
210  jArray frnk;
211  frnk.emplace_back(jArray({'F', 'R', 'N', 'K'}));
212  frnk.emplace_back(jArray(val.data.begin(), val.data.end()));
213  jArray res;
214  res.emplace_back(std::move(frnk));
215  return res;
216  }
217 
218  inline jsonrpc::Value makeValue(const primitives::RpcMethods &v) {
219  jStruct res;
220  res["version"] = makeValue(v.version);
221  res["methods"] = makeValue(v.methods);
222  return res;
223  }
224 
225  inline jsonrpc::Value makeValue(const primitives::BlockData &val) {
226  jStruct block;
227  block["extrinsics"] = makeValue(val.body);
228  block["header"] = makeValue(val.header);
229 
230  jStruct data;
231  data["block"] = std::move(block);
232  data["justifications"] = makeValue(val.justification);
233  return data;
234  }
235 
236  inline jsonrpc::Value makeValue(const primitives::RuntimeDispatchInfo &v) {
237  jStruct res;
238  res["weight"] = makeValue(v.weight);
239  res["partialFee"] = makeValue(v.partial_fee);
241  switch (v.dispatch_class) {
242  case Class::Normal:
243  res["class"] = "normal";
244  break;
245  case Class::Mandatory:
246  res["class"] = "mandatory";
247  break;
248  case Class::Operational:
249  res["class"] = "operational";
250  break;
251  }
252  return res;
253  }
254 
255  inline jsonrpc::Value makeValue(
257  return visit_in_place(
258  event.params,
259  [&event](std::nullopt_t) -> jsonrpc::Value {
260  switch (event.type) {
261  case primitives::events::ExtrinsicEventType::FUTURE:
262  return "future";
263  case primitives::events::ExtrinsicEventType::READY:
264  return "ready";
265  case primitives::events::ExtrinsicEventType::INVALID:
266  return "invalid";
267  case primitives::events::ExtrinsicEventType::DROPPED:
268  return "dropped";
269  default:
270  BOOST_UNREACHABLE_RETURN({});
271  }
272  },
274  -> jsonrpc::Value {
275  jArray peers;
276  peers.resize(params.peers.size());
277  std::transform(
278  params.peers.cbegin(),
279  params.peers.cend(),
280  peers.begin(),
281  [](const auto &peer_id) { return makeValue(peer_id.toHex()); });
282  return jStruct{std::pair{"broadcast", std::move(peers)}};
283  },
284  [](const primitives::events::InBlockEventParams &params) {
285  return jStruct{std::pair{
286  "inBlock", makeValue(common::hex_lower_0x(params.block))}};
287  },
288  [](const primitives::events::RetractedEventParams &params) {
289  return jStruct{std::pair{
290  "retracted",
291  makeValue(common::hex_lower_0x(params.retracted_block))}};
292  },
294  return jStruct{
295  std::pair{"finalityTimeout",
296  makeValue(common::hex_lower_0x(params.block))}};
297  },
298  [](const primitives::events::FinalizedEventParams &params) {
299  return jStruct{std::pair{
300  "finalized", makeValue(common::hex_lower_0x(params.block))}};
301  },
302  [](const primitives::events::UsurpedEventParams &params) {
303  return jStruct{std::pair{
304  "usurped",
305  makeValue(common::hex_lower_0x(params.transaction_hash))}};
306  });
307  }
308 
309  inline jsonrpc::Value makeValue(const primitives::Extrinsic &v) {
310  return common::hex_lower_0x(scale::encode(v.data).value());
311  }
312 
313 } // namespace kagome::api
314 
315 #endif // KAGOME_CORE_API_EXTRINSIC_RESPONSE_VALUE_CONVERTER_HPP
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
std::optional< primitives::BlockHeader > header
Definition: block_data.hpp:25
const std::vector< uint8_t > & asVector() const
getter for vector of bytes
Definition: buffer.hpp:135
common::BufferView BufferView
Version version
Descriptor version.
Definition: rpc_methods.hpp:30
Digest digest
chain-specific auxiliary data
storage::trie::RootHash state_root
root of the Merkle tree
common::Buffer data
extrinsic content as byte array
Definition: extrinsic.hpp:27
ApisVec apis
List of supported API "features" along with their versions.
Definition: version.hpp:78
uint8_t state_version
Version of the state implementation used by this runtime.
Definition: version.hpp:83
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
enum kagome::primitives::RuntimeDispatchInfo::DispatchClass dispatch_class
std::string hex_lower_0x(gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation with prefix 0x.
Definition: hexutil.cpp:58
uint32_t authoring_version
authoring_version is the version of the authorship interface
Definition: version.hpp:63
jsonrpc::Value makeValue(const uint32_t &)
boost::variant< Other, Unused< 1 >, Unused< 2 >, Unused< 3 >, Consensus, Seal, PreRuntime, Unused< 7 >, RuntimeEnvironmentUpdated > DigestItem
Definition: digest.hpp:182
static constexpr size_t size()
Definition: blob.hpp:146
BlockNumber number
index of the block in the chain
Extrinsic class represents extrinsic.
Definition: extrinsic.hpp:24
Methods methods
A set of methods names as strings.
Definition: rpc_methods.hpp:33
BlockHash parent_hash
32-byte Blake2s hash of parent header
common::Hash256 extrinsics_root
field for validation integrity
std::optional< primitives::BlockBody > body
Definition: block_data.hpp:26
std::optional< primitives::Justification > justification
Definition: block_data.hpp:29