Kagome
Polkadot Runtime Engine in C++17
protobuf_block_request.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_ADAPTERS_PROTOBUF_BLOCK_REQUEST
7 #define KAGOME_ADAPTERS_PROTOBUF_BLOCK_REQUEST
8 
10 
11 #include <gsl/span>
12 #include <libp2p/multi/uvarint.hpp>
13 
14 #include "common/visitor.hpp"
17 
18 namespace kagome::network {
19 
20  template <>
22  static size_t size(const BlocksRequest &t) {
23  return 0;
24  }
25 
26  static std::vector<uint8_t>::iterator write(
27  const BlocksRequest &t,
28  std::vector<uint8_t> &out,
29  std::vector<uint8_t>::iterator loaded) {
30  ::api::v1::BlockRequest msg;
31 
32  msg.set_fields(htobe32(t.fields));
33 
34  kagome::visit_in_place(
35  t.from,
36  [&](const primitives::BlockHash &block_hash) {
37  msg.set_hash(block_hash.toString());
38  },
39  [&](const primitives::BlockNumber &block_number) {
40  uint32_t n = htole32(block_number);
41  msg.set_number(&n, sizeof(n));
42  });
43 
44  msg.set_direction(static_cast<::api::v1::Direction>(t.direction));
45 
46  if (t.max.has_value()) {
47  msg.set_max_blocks(t.max.value());
48  }
49 
50  return appendToVec(msg, out, loaded);
51  }
52 
53  static outcome::result<std::vector<uint8_t>::const_iterator> read(
54  BlocksRequest &out,
55  const std::vector<uint8_t> &src,
56  std::vector<uint8_t>::const_iterator from) {
57  const auto remains = src.size() - std::distance(src.begin(), from);
58  assert(remains >= size(out));
59 
60  ::api::v1::BlockRequest msg;
61  if (!msg.ParseFromArray(from.base(), remains)) {
63  }
64 
65  out.fields.load(be32toh(msg.fields()));
66  out.direction = static_cast<decltype(out.direction)>(msg.direction());
67 
68  switch (msg.from_block_case()) {
69  case msg.kHash: {
70  OUTCOME_TRY(data, primitives::BlockHash::fromString(msg.hash()));
71  out.from = data;
72  } break;
73 
74  case msg.kNumber: {
75  uint32_t n = 0;
76  memcpy(&n,
77  msg.number().data(),
78  std::min(sizeof(n), msg.number().size()));
79  primitives::BlockNumber bn(le32toh(n));
80  out.from = bn;
81  } break;
82 
83  default:
85  }
86 
87  if (msg.max_blocks() > 0) {
88  out.max.emplace(msg.max_blocks());
89  }
90 
91  std::advance(from, msg.ByteSizeLong());
92  return from;
93  }
94  };
95 
96 } // namespace kagome::network
97 
98 #endif // KAGOME_ADAPTERS_PROTOBUF_BLOCK_REQUEST
std::vector< uint8_t >::iterator appendToVec(const T &msg, std::vector< uint8_t > &out, std::vector< uint8_t >::iterator loaded)
Definition: protobuf.hpp:24
BlockAttributes fields
bits, showing, which parts of BlockData to return
uint32_t BlockNumber
Definition: common.hpp:18
static outcome::result< Blob< size_ > > fromString(std::string_view data)
Definition: blob.hpp:169
primitives::BlockId from
start from this block
static outcome::result< std::vector< uint8_t >::const_iterator > read(BlocksRequest &out, const std::vector< uint8_t > &src, std::vector< uint8_t >::const_iterator from)
std::optional< uint32_t > max
static std::vector< uint8_t >::iterator write(const BlocksRequest &t, std::vector< uint8_t > &out, std::vector< uint8_t >::iterator loaded)
Direction direction
sequence direction