Kagome
Polkadot Runtime Engine in C++17
protobuf_block_response.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_ADAPTERS_PROTOBUF_BLOCK_RESPONSE
7 #define KAGOME_ADAPTERS_PROTOBUF_BLOCK_RESPONSE
8 
10 
12 #include "scale/scale.hpp"
13 
14 namespace kagome::network {
15 
16  template <>
18  static size_t size(const BlocksResponse &t) {
19  return 0;
20  }
21 
22  static std::vector<uint8_t>::iterator write(
23  const BlocksResponse &t,
24  std::vector<uint8_t> &out,
25  std::vector<uint8_t>::iterator loaded) {
26  ::api::v1::BlockResponse msg;
27  for (const auto &src_block : t.blocks) {
28  auto *dst_block = msg.add_blocks();
29  dst_block->set_hash(src_block.hash.toString());
30 
31  if (src_block.header)
32  dst_block->set_header(
33  vector_to_string(scale::encode(*src_block.header).value()));
34 
35  if (src_block.body)
36  for (const auto &ext_body : *src_block.body)
37  dst_block->add_body(
38  vector_to_string(scale::encode(ext_body).value()));
39 
40  if (src_block.receipt)
41  dst_block->set_receipt(src_block.receipt->toString());
42 
43  if (src_block.message_queue)
44  dst_block->set_message_queue(src_block.message_queue->toString());
45 
46  if (src_block.justification) {
47  dst_block->set_justification(
48  src_block.justification->data.toString());
49 
50  dst_block->set_is_empty_justification(
51  src_block.justification->data.empty());
52  };
53  }
54 
55  return appendToVec(msg, out, loaded);
56  }
57 
58  static outcome::result<std::vector<uint8_t>::const_iterator> read(
59  BlocksResponse &out,
60  const std::vector<uint8_t> &src,
61  std::vector<uint8_t>::const_iterator from) {
62  const auto remains = src.size() - std::distance(src.begin(), from);
63  assert(remains >= size(out));
64 
65  ::api::v1::BlockResponse msg;
66  if (!msg.ParseFromArray(from.base(), remains))
68 
69  auto &dst_blocks = out.blocks;
70  dst_blocks.reserve(msg.blocks().size());
71  for (const auto &src_block_data : msg.blocks()) {
72  OUTCOME_TRY(hash,
73  primitives::BlockHash::fromString(src_block_data.hash()));
74 
75  OUTCOME_TRY(header, extract_value<primitives::BlockHeader>([&]() {
76  return src_block_data.header();
77  }));
78 
79  std::optional<primitives::BlockBody> bodies;
80  for (const auto &b : src_block_data.body()) {
81  if (!bodies) bodies = primitives::BlockBody{};
82 
83  OUTCOME_TRY(
84  body, extract_value<primitives::Extrinsic>([&]() { return b; }));
85  bodies->emplace_back(std::move(body));
86  }
87 
88  auto receipt = common::Buffer::fromString(src_block_data.receipt());
89 
90  auto message_queue =
91  common::Buffer::fromString(src_block_data.message_queue());
92 
93  std::optional<primitives::Justification> justification;
94 
95  if (not src_block_data.justification().empty()
96  || src_block_data.is_empty_justification()) {
97  auto data =
98  common::Buffer::fromString(src_block_data.justification());
99 
100  justification.emplace(primitives::Justification{std::move(data)});
101  }
102 
103  dst_blocks.emplace_back(
104  primitives::BlockData{.hash = hash,
105  .header = std::move(header),
106  .body = std::move(bodies),
107  .receipt = std::move(receipt),
108  .message_queue = std::move(message_queue),
109  .justification = std::move(justification)});
110  }
111 
112  std::advance(from, msg.ByteSizeLong());
113  return from;
114  }
115 
116  private:
117  template <typename T, typename F>
118  static outcome::result<T> extract_value(F &&f) {
119  if (const auto &buffer = std::forward<F>(f)(); !buffer.empty()) {
120  OUTCOME_TRY(
121  decoded,
122  scale::decode<T>(gsl::span<const uint8_t>(
123  reinterpret_cast<const uint8_t *>(buffer.data()), // NOLINT
124  buffer.size())));
125  return std::move(decoded);
126  }
128  }
129 
130  static std::string vector_to_string(std::vector<uint8_t> &&src) {
131  return std::string(reinterpret_cast<char *>(src.data()), // NOLINT
132  src.size());
133  }
134  };
135 
136 } // namespace kagome::network
137 
138 #endif // KAGOME_ADAPTERS_PROTOBUF_BLOCK_RESPONSE
static std::vector< uint8_t >::iterator write(const BlocksResponse &t, std::vector< uint8_t > &out, std::vector< uint8_t >::iterator loaded)
std::vector< Extrinsic > BlockBody
Definition: block.hpp:14
std::vector< uint8_t >::iterator appendToVec(const T &msg, std::vector< uint8_t > &out, std::vector< uint8_t >::iterator loaded)
Definition: protobuf.hpp:24
static outcome::result< Blob< size_ > > fromString(std::string_view data)
Definition: blob.hpp:169
static std::string vector_to_string(std::vector< uint8_t > &&src)
static outcome::result< std::vector< uint8_t >::const_iterator > read(BlocksResponse &out, const std::vector< uint8_t > &src, std::vector< uint8_t >::const_iterator from)
static SLBuffer fromString(const std::string_view &src)
stores content of a string to byte array
Definition: buffer.hpp:205
Base::iterator emplace(Iter pos, Args &&...args)
primitives::BlockHash hash
Definition: block_data.hpp:24
common::SLVector< primitives::BlockData, kMaxBlocksInResponse > blocks