Kagome
Polkadot Runtime Engine in C++17
get_storage.cpp
Go to the documentation of this file.
1 
7 
9 
10  outcome::result<void> GetStorage::init(
11  const jsonrpc::Request::Parameters &params) {
12  if (params.size() > 2 or params.empty()) {
13  throw jsonrpc::InvalidParametersFault("Incorrect number of params");
14  }
15  auto &param0 = params[0];
16  if (not param0.IsString()) {
17  throw jsonrpc::InvalidParametersFault(
18  "Parameter 'key' must be a hex string");
19  }
20  auto &&key_str = param0.AsString();
21  OUTCOME_TRY(key, common::unhexWith0x(key_str));
22 
23  key_ = common::Buffer(std::move(key));
24 
25  if (params.size() > 1) {
26  auto &param1 = params[1];
27  if (param1.IsString()) {
28  auto &&at_str = param1.AsString();
29  OUTCOME_TRY(at_span, common::unhexWith0x(at_str));
30  OUTCOME_TRY(at, primitives::BlockHash::fromSpan(at_span));
31  at_.emplace(at);
32  } else if (param1.IsNil()) {
33  at_ = std::nullopt;
34  } else {
35  throw jsonrpc::InvalidParametersFault(
36  "Parameter 'at' must be a hex string or null");
37  }
38  } else {
39  at_.reset();
40  }
41  return outcome::success();
42  }
43 
44  outcome::result<std::optional<common::Buffer>> GetStorage::execute() {
45  return at_ ? api_->getStorageAt(key_, at_.value()) : api_->getStorage(key_);
46  }
47 
48 } // namespace kagome::api::state::request
outcome::result< std::optional< common::Buffer > > execute()
Definition: get_storage.cpp:44
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< kagome::primitives::BlockHash > at_
Definition: get_storage.hpp:38
SLBuffer< std::numeric_limits< size_t >::max()> Buffer
Definition: buffer.hpp:244
outcome::result< void > init(const jsonrpc::Request::Parameters &params)
Definition: get_storage.cpp:10
static outcome::result< Blob< size_ > > fromSpan(const gsl::span< const uint8_t > &span)
Definition: blob.hpp:208
std::shared_ptr< StateApi > api_
Definition: get_storage.hpp:36