Kagome
Polkadot Runtime Engine in C++17
get_block_hash.cpp
Go to the documentation of this file.
1 
7 
8 #include "common/hexutil.hpp"
9 #include "common/visitor.hpp"
10 
14 
15  outcome::result<void> GetBlockhash::init(
16  const jsonrpc::Request::Parameters &params) {
17  if (params.empty()) {
18  param_ = NoParameters{};
19  return outcome::success();
20  }
21 
22  if (params.size() > 1) {
23  throw jsonrpc::InvalidParametersFault("incorrect number of arguments");
24  }
25 
26  const auto &arg0 = params[0];
27  if (arg0.IsInteger32()) {
28  param_ = static_cast<uint32_t>(arg0.AsInteger32());
29  } else if (arg0.IsString()) {
30  param_ = arg0.AsString();
31  } else if (arg0.IsArray()) {
32  const auto &array = arg0.AsArray();
33  // empty array would cause problems within `execute`
34  if (array.empty()) {
35  throw jsonrpc::InvalidParametersFault("invalid argument");
36  }
37  std::vector<VectorParam> param;
38  param.reserve(array.size());
39  for (const auto &v : array) {
40  if (v.IsInteger32()) {
41  param.emplace_back(VectorParam(v.AsInteger32()));
42  } else if (v.IsString()) {
43  param.emplace_back(VectorParam(v.AsString()));
44  } else {
45  throw jsonrpc::InvalidParametersFault("invalid argument");
46  }
47  }
48  param_ = std::move(param);
49  } else {
50  throw jsonrpc::InvalidParametersFault("invalid argument");
51  }
52 
53  return outcome::success();
54  } // namespace kagome::api::chain::request
55 
56  namespace {
57  std::string formatBlockHash(const BlockHash &bh) {
58  const static std::string prefix = "0x";
59  auto hex = common::hex_lower(bh);
60  return prefix + hex;
61  }
62  } // namespace
63 
64  outcome::result<GetBlockhash::ResultType> GetBlockhash::execute() {
65  return kagome::visit_in_place(
66  param_,
67  [this](const NoParameters &v) -> outcome::result<ResultType> {
68  // return last finalized
69  OUTCOME_TRY(bh, api_->getBlockHash());
70  return formatBlockHash(bh);
71  },
72  [this](BlockNumber v) -> outcome::result<ResultType> {
73  OUTCOME_TRY(bh, api_->getBlockHash(v));
74  return formatBlockHash(bh);
75  },
76  [this](std::string_view v) -> outcome::result<ResultType> {
77  OUTCOME_TRY(bh, api_->getBlockHash(v));
78  return formatBlockHash(bh);
79  },
80  [this](
81  const std::vector<VectorParam> &v) -> outcome::result<ResultType> {
82  OUTCOME_TRY(rr, api_->getBlockHash(gsl::make_span(v)));
83  std::vector<std::string> results{};
84  results.reserve(v.size());
85  for (const auto &it : rr) {
86  results.emplace_back(formatBlockHash(it));
87  }
88 
89  return results;
90  });
91  };
92 
93 } // namespace kagome::api::chain::request
outcome::result< void > init(const jsonrpc::Request::Parameters &params)
std::string hex_lower(const gsl::span< const uint8_t > bytes) noexcept
Converts bytes to hex representation.
Definition: hexutil.cpp:52
gsl::span< const uint8_t > make_span(const rocksdb::Slice &s)
uint32_t BlockNumber
Definition: common.hpp:18
boost::variant< Param1, Param2 > VectorParam
outcome::result< ResultType > execute()
primitives::BlockNumber BlockNumber
Definition: common.hpp:24
common::Hash256 BlockHash
Definition: block_id.hpp:15
primitives::BlockHash BlockHash
Definition: common.hpp:23