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  // getKeysPaged(childKey, prefix, count, [opt]startKey, [opt]at)
15 
16  if (params.size() > 5 or params.size() < 3) {
17  throw jsonrpc::InvalidParametersFault("Incorrect number of params");
18  }
19  // childKey
20  auto &param0 = params[0];
21 
22  if (not param0.IsString() or param0.IsNil()) {
23  throw jsonrpc::InvalidParametersFault(
24  "Parameter '[child_storage_key]' must be a hex string");
25  }
26 
27  OUTCOME_TRY(child_storage_key, common::unhexWith0x(param0.AsString()));
28  child_storage_key_ = common::Buffer(std::move(child_storage_key));
29 
30  auto &param1 = params[1];
31 
32  if (not param1.IsString() and not param1.IsNil()) {
33  throw jsonrpc::InvalidParametersFault(
34  "Parameter '[prefix]' must be a hex string");
35  }
36 
37  if (param1.IsNil()) {
38  prefix_ = std::nullopt;
39  } else if (param1.IsString()) {
40  OUTCOME_TRY(key, common::unhexWith0x(param1.AsString()));
41  prefix_ = common::Buffer(std::move(key));
42  } else {
43  throw jsonrpc::InvalidParametersFault(
44  "Parameter '[prefix]' must be a hex string");
45  }
46 
47  if (not params[2].IsInteger32()) {
48  throw jsonrpc::InvalidParametersFault(
49  "Parameter '[key_amount]' must be a uint32_t");
50  }
51 
52  keys_amount_ = params[2].AsInteger32();
53  if (params.size() == 3) {
54  return outcome::success();
55  }
56 
57  // process prev_key param
58  if (not params[3].IsNil()) {
59  if (not params[3].IsString()) {
60  throw jsonrpc::InvalidParametersFault(
61  "Parameter '[prev_key]' must be a hex string representation of an "
62  "encoded optional byte sequence");
63  }
64  OUTCOME_TRY(prev_key, common::unhexWith0x(params[3].AsString()));
65  prev_key_ = common::Buffer{prev_key};
66  }
67 
68  if (params.size() == 4) {
69  return outcome::success();
70  }
71 
72  // process at param
73  if (not params[4].IsString()) {
74  throw jsonrpc::InvalidParametersFault(
75  "Parameter '[at]' must be a hex string representation of an encoded "
76  "optional byte sequence");
77  }
78  OUTCOME_TRY(at_span, common::unhexWith0x(params[4].AsString()));
79  OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
80  at_ = at;
81 
82  return outcome::success();
83  }
84 
85  outcome::result<std::vector<common::Buffer>> GetKeysPaged::execute() {
86  return api_->getKeysPaged(
88  }
89 } // namespace kagome::api::child_state::request
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
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< void > init(const jsonrpc::Request::Parameters &params)
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
std::optional< common::Buffer > prev_key_
static outcome::result< Blob< size_ > > fromSpan(const gsl::span< const uint8_t > &span)
Definition: blob.hpp:208
outcome::result< std::vector< common::Buffer > > execute()
std::optional< primitives::BlockHash > at_