Kagome
Polkadot Runtime Engine in C++17
protobuf_state_response.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_ADAPTERS_PROTOBUF_STATE_RESPONSE
7 #define KAGOME_ADAPTERS_PROTOBUF_STATE_RESPONSE
8 
10 
12 #include "scale/scale.hpp"
13 
14 namespace kagome::network {
15 
16  template <>
18  static size_t size(const StateResponse &t) {
19  return 0;
20  }
21 
22  static std::vector<uint8_t>::iterator write(
23  const StateResponse &t,
24  std::vector<uint8_t> &out,
25  std::vector<uint8_t>::iterator loaded) {
26  ::api::v1::StateResponse msg;
27  for (const auto &entries : t.entries) {
28  auto *dst_entries = msg.add_entries();
29  dst_entries->set_state_root(entries.state_root.toString());
30  for (const auto &entry : entries.entries) {
31  auto *dst_entry = dst_entries->add_entries();
32  dst_entry->set_key(entry.key.toString());
33  dst_entry->set_value(entry.value.toString());
34  }
35  dst_entries->set_complete(entries.complete);
36  }
37  msg.set_proof(t.proof.toString());
38 
39  return appendToVec(msg, out, loaded);
40  }
41 
42  static outcome::result<std::vector<uint8_t>::const_iterator> read(
43  StateResponse &out,
44  const std::vector<uint8_t> &src,
45  std::vector<uint8_t>::const_iterator from) {
46  const auto remains = src.size() - std::distance(src.begin(), from);
47  assert(remains >= size(out));
48 
49  ::api::v1::StateResponse msg;
50  if (!msg.ParseFromArray(from.base(), remains))
52 
53  for (const auto &kvEntry : msg.entries()) {
55  auto root = kvEntry.state_root();
56  if(!root.empty()) {
58  }
59  for (const auto &sEntry : kvEntry.entries()) {
60  kv.entries.emplace_back(StateEntry{common::Buffer().put(sEntry.key()),
61  common::Buffer().put(sEntry.value())});
62  }
63  kv.complete = kvEntry.complete();
64 
65  out.entries.emplace_back(std::move(kv));
66  }
67  out.proof = common::Buffer().put(msg.proof());
68 
69  std::advance(from, msg.ByteSizeLong());
70  return from;
71  }
72  };
73 
74 } // namespace kagome::network
75 
76 #endif // KAGOME_ADAPTERS_PROTOBUF_STATE_RESPONSE
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
std::vector< uint8_t >::iterator appendToVec(const T &msg, std::vector< uint8_t > &out, std::vector< uint8_t >::iterator loaded)
Definition: protobuf.hpp:24
A key value state.
common::Buffer proof
If no_proof is false in request, this contains proof nodes.
std::string toString() const
return content of bytearray as string
Definition: buffer.hpp:187
std::vector< StateEntry > entries
A collection of keys-values.
A key-value pair.
storage::trie::RootHash state_root
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
static outcome::result< Blob< size_ > > fromString(std::string_view data)
Definition: blob.hpp:169
SLBuffer & put(std::string_view view)
Put a string into byte buffer.
Definition: buffer.hpp:117
static outcome::result< std::vector< uint8_t >::const_iterator > read(StateResponse &out, const std::vector< uint8_t > &src, std::vector< uint8_t >::const_iterator from)
std::vector< KeyValueStateEntry > entries
static std::vector< uint8_t >::iterator write(const StateResponse &t, std::vector< uint8_t > &out, std::vector< uint8_t >::iterator loaded)
bool complete
Set to true when there are no more keys to return.