Kagome
Polkadot Runtime Engine in C++17
query_storage.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_API_REQUESTS_QUERY_STORAGE_H
7 #define KAGOME_API_REQUESTS_QUERY_STORAGE_H
8 
10 
11 #include <boost/range/adaptor/transformed.hpp>
12 #include <boost/range/algorithm_ext/push_back.hpp>
13 
16 
17 namespace kagome::api {
18 
19  inline jsonrpc::Value makeValue(const StateApi::StorageChangeSet &changes) {
20  jsonrpc::Value ret;
21  std::vector<jsonrpc::Value::Array> j_changes;
22  boost::range::push_back(
23  j_changes,
24  changes.changes | boost::adaptors::transformed([](auto &change) {
25  return jsonrpc::Value::Array{makeValue(change.key),
26  makeValue(change.data)};
27  }));
28 
29  return jsonrpc::Value::Struct{
30  std::pair{"block", makeValue(common::hex_lower_0x(changes.block))},
31  std::pair{"changes", makeValue(j_changes)}};
32  }
33 
34 } // namespace kagome::api
35 
37 
38  class QueryStorage final
39  : public details::RequestType<std::vector<StateApi::StorageChangeSet>,
40  std::vector<std::string>,
41  std::string,
42  std::optional<std::string>> {
43  public:
44  explicit QueryStorage(std::shared_ptr<StateApi> api)
45  : api_(std::move(api)) {
46  BOOST_ASSERT(api_);
47  };
48 
49  outcome::result<std::vector<StateApi::StorageChangeSet>> execute()
50  override {
51  std::vector<common::Buffer> keys;
52  keys.reserve(getParam<0>().size());
53  for (auto &str_key : getParam<0>()) {
54  OUTCOME_TRY(key, kagome::common::unhexWith0x(str_key));
55  keys.emplace_back(std::move(key));
56  }
57  OUTCOME_TRY(from,
59  std::optional<primitives::BlockHash> to{};
60  if (auto opt_to = getParam<2>(); opt_to.has_value()) {
61  OUTCOME_TRY(to_,
63  to = std::move(to_);
64  }
65  return api_->queryStorage(keys, from, std::move(to));
66  }
67 
68  private:
69  std::shared_ptr<StateApi> api_;
70  };
71 
72  class QueryStorageAt final
73  : public details::RequestType<std::vector<StateApi::StorageChangeSet>,
74  std::vector<std::string>,
75  std::optional<std::string>> {
76  public:
77  explicit QueryStorageAt(std::shared_ptr<StateApi> api)
78  : api_(std::move(api)) {
79  BOOST_ASSERT(api_);
80  }
81 
82  outcome::result<std::vector<StateApi::StorageChangeSet>> execute()
83  override {
84  std::vector<common::Buffer> keys;
85  keys.reserve(getParam<0>().size());
86  for (auto &str_key : getParam<0>()) {
87  OUTCOME_TRY(key, common::unhexWith0x(str_key));
88  keys.emplace_back(std::move(key));
89  }
90  std::optional<primitives::BlockHash> at{};
91  if (auto opt_at = getParam<1>(); opt_at.has_value()) {
92  OUTCOME_TRY(at_,
94  at = std::move(at_);
95  }
96 
97  return api_->queryStorageAt(keys, std::move(at));
98  }
99 
100  private:
101  std::shared_ptr<StateApi> api_;
102  };
103 
104 } // namespace kagome::api::state::request
105 
106 #endif // KAGOME_API_REQUESTS_QUERY_STORAGE_H
outcome::result< std::vector< uint8_t > > unhexWith0x(std::string_view hex_with_prefix)
Unhex hex-string with 0x in the begining.
Definition: hexutil.cpp:89
outcome::result< std::vector< StateApi::StorageChangeSet > > execute() override
QueryStorageAt(std::shared_ptr< StateApi > api)
STL namespace.
static outcome::result< Blob< size_ > > fromHexWithPrefix(std::string_view hex)
Definition: blob.hpp:197
QueryStorage(std::shared_ptr< StateApi > api)
std::string hex_lower_0x(gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation with prefix 0x.
Definition: hexutil.cpp:58
jsonrpc::Value makeValue(const uint32_t &)
outcome::result< std::vector< StateApi::StorageChangeSet > > execute() override