Kagome
Polkadot Runtime Engine in C++17
get_keys_paged.cpp
Go to the documentation of this file.
1 
7 
8 #include "scale/scale.hpp"
9 
11 
12  outcome::result<void> GetKeysPaged::init(
13  const jsonrpc::Request::Parameters &params) {
14  if (params.size() > 4 or params.size() <= 1) {
15  throw jsonrpc::InvalidParametersFault("Incorrect number of params");
16  }
17  auto &param0 = params[0];
18 
19  if (not param0.IsString() and not param0.IsNil()) {
20  throw jsonrpc::InvalidParametersFault(
21  "Parameter '[prefix]' must be a hex string");
22  }
23 
24  if (param0.IsNil()) {
25  prefix_ =
26  std::nullopt; // I suppose none here is better than empty Buffer
27  } else if (param0.IsString()) {
28  OUTCOME_TRY(key, common::unhexWith0x(param0.AsString()));
29  prefix_.emplace(std::move(key));
30  } else {
31  throw jsonrpc::InvalidParametersFault(
32  "Parameter '[prefix]' must be a hex string");
33  }
34 
35  if (not params[1].IsInteger32()) {
36  throw jsonrpc::InvalidParametersFault(
37  "Parameter '[key_amount]' must be a uint32_t");
38  }
39 
40  keys_amount_ = params[1].AsInteger32();
41  if (params.size() == 2) {
42  return outcome::success();
43  }
44 
45  // process prev_key param
46  if (not params[2].IsNil()) {
47  if (not params[2].IsString()) {
48  throw jsonrpc::InvalidParametersFault(
49  "Parameter '[prev_key]' must be a hex string representation of an "
50  "encoded optional byte sequence");
51  }
52  OUTCOME_TRY(prev_key, common::unhexWith0x(params[2].AsString()));
53  prev_key_.emplace(std::move(prev_key));
54  }
55 
56  if (params.size() == 3) {
57  return outcome::success();
58  }
59 
60  // process at param
61  if (not params[3].IsString()) {
62  throw jsonrpc::InvalidParametersFault(
63  "Parameter '[at]' must be a hex string representation of an encoded "
64  "optional byte sequence");
65  }
66  OUTCOME_TRY(at_span, common::unhexWith0x(params[3].AsString()));
67  OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
68  at_ = at;
69 
70  return outcome::success();
71  }
72 
73  outcome::result<std::vector<common::Buffer>> GetKeysPaged::execute() {
74  return api_->getKeysPaged(prefix_, keys_amount_, prev_key_, at_);
75  }
76 
77 } // namespace kagome::api::state::request
outcome::result< std::vector< common::Buffer > > execute()
outcome::result< void > init(const jsonrpc::Request::Parameters &params)
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
std::optional< primitives::BlockHash > at_
std::optional< common::Buffer > prev_key_
static outcome::result< Blob< size_ > > fromSpan(const gsl::span< const uint8_t > &span)
Definition: blob.hpp:208
std::optional< common::Buffer > prefix_