hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
query_template.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_PROTO_QUERY_BUILDER_TEMPLATE_HPP
7 #define IROHA_PROTO_QUERY_BUILDER_TEMPLATE_HPP
8 
9 #include <boost/range/algorithm/for_each.hpp>
10 #include <optional>
11 #include <string_view>
12 
19 #include "module/irohad/common/validators_config.hpp"
20 #include "queries.pb.h"
22 
23 namespace shared_model {
24  namespace proto {
25 
34  template <int S = 0,
36  typename BT = UnsignedWrapper<Query>>
37  class [[deprecated]] TemplateQueryBuilder {
38  private:
39  template <int, typename, typename>
40  friend class TemplateQueryBuilder;
41 
42  enum RequiredFields {
43  CreatedTime,
44  CreatorAccountId,
45  QueryField,
46  QueryCounter,
47  TOTAL
48  };
49 
50  template <int s>
52 
53  using ProtoQuery = iroha::protocol::Query;
54 
55  template <int Sp>
57  : query_(o.query_), stateless_validator_(o.stateless_validator_) {}
58 
65  template <int Fields, typename Transformation>
66  auto transform(Transformation t) const {
67  NextBuilder<Fields> copy = *this;
68  t(copy.query_);
69  return copy;
70  }
71 
78  template <typename Transformation>
79  auto queryField(Transformation t) const {
80  NextBuilder<QueryField> copy = *this;
81  t(copy.query_.mutable_payload());
82  return copy;
83  }
84 
86  template <typename PageMetaPayload>
87  static auto setTxPaginationMeta(
88  PageMetaPayload * page_meta_payload,
90  const std::optional<interface::types::HashType> &first_hash =
91  std::nullopt,
92  const interface::Ordering *ordering = nullptr) {
93  auto from_interface_2_proto_field =
94  [](interface::Ordering::Field value) {
95  switch (value) {
98 
100  return iroha::protocol::Field::kPosition;
101 
102  default:
103  BOOST_ASSERT_MSG(false, "Unexpected Field value!");
104  }
105  };
106 
107  auto from_interface_2_proto_direction =
109  switch (value) {
111  return iroha::protocol::Direction::kAscending;
112 
114  return iroha::protocol::Direction::kDescending;
115 
116  default: {
117  BOOST_ASSERT_MSG(false, "Unexpected Direction value!");
118  }
119  }
120  };
121 
122  page_meta_payload->set_page_size(page_size);
123  if (first_hash) {
124  page_meta_payload->set_first_tx_hash(first_hash->hex());
125  }
126  if (ordering) {
127  interface::Ordering::OrderingEntry const *ptr = nullptr;
128  size_t count = 0;
129  (*ordering).get(ptr, count);
130 
131  for (size_t ix = 0; ix < count; ++ix) {
132  interface::Ordering::OrderingEntry const &entry = ptr[ix];
133 
134  auto sequence =
135  page_meta_payload->mutable_ordering()->add_sequence();
136  sequence->set_field(from_interface_2_proto_field(entry.field));
137  sequence->set_direction(
138  from_interface_2_proto_direction(entry.direction));
139  }
140  }
141  }
142 
143  TemplateQueryBuilder(const SV &validator)
144  : stateless_validator_(validator) {}
145 
146  public:
147  // we do such default initialization only because it is deprecated and
148  // used only in tests
150  : TemplateQueryBuilder(SV(iroha::test::kTestsValidatorsConfig)) {}
151 
152  auto createdTime(interface::types::TimestampType created_time) const {
153  return transform<CreatedTime>([&](auto &qry) {
154  qry.mutable_payload()->mutable_meta()->set_created_time(created_time);
155  });
156  }
157 
159  const interface::types::AccountIdType &creator_account_id) const {
160  return transform<CreatorAccountId>([&](auto &qry) {
161  qry.mutable_payload()->mutable_meta()->set_creator_account_id(
162  creator_account_id);
163  });
164  }
165 
166  auto queryCounter(interface::types::CounterType query_counter) const {
167  return transform<QueryCounter>([&](auto &qry) {
168  qry.mutable_payload()->mutable_meta()->set_query_counter(
169  query_counter);
170  });
171  }
172 
173  auto getAccount(const interface::types::AccountIdType &account_id) const {
174  return queryField([&](auto proto_query) {
175  auto query = proto_query->mutable_get_account();
176  query->set_account_id(account_id);
177  });
178  }
179 
181  const {
182  return queryField([&](auto proto_query) {
183  auto query = proto_query->mutable_get_signatories();
184  query->set_account_id(account_id);
185  });
186  }
187 
189  const interface::types::AccountIdType &account_id,
191  const std::optional<interface::types::HashType> &first_hash =
192  std::nullopt,
193  const interface::Ordering *ordering = nullptr) const {
194  return queryField([&](auto proto_query) {
195  auto query = proto_query->mutable_get_account_transactions();
196  query->set_account_id(account_id);
197  setTxPaginationMeta(query->mutable_pagination_meta(),
198  page_size,
199  first_hash,
200  ordering);
201  });
202  }
203 
205  const interface::types::AccountIdType &account_id,
206  const interface::types::AssetIdType &asset_id,
208  const std::optional<interface::types::HashType> &first_hash =
209  std::nullopt,
210  const interface::Ordering *ordering = nullptr) const {
211  return queryField([&](auto proto_query) {
212  auto query = proto_query->mutable_get_account_asset_transactions();
213  query->set_account_id(account_id);
214  query->set_asset_id(asset_id);
215  setTxPaginationMeta(query->mutable_pagination_meta(),
216  page_size,
217  first_hash,
218  ordering);
219  });
220  }
221 
223  const interface::types::AccountIdType &account_id,
224  size_t page_size,
225  std::optional<shared_model::interface::types::AssetIdType>
226  first_asset_id) const {
227  return queryField([&](auto proto_query) {
228  auto query = proto_query->mutable_get_account_assets();
229  query->set_account_id(account_id);
230  auto pagination_meta = query->mutable_pagination_meta();
231  pagination_meta->set_page_size(page_size);
232  if (first_asset_id) {
233  pagination_meta->set_first_asset_id(*first_asset_id);
234  }
235  });
236  }
237 
239  size_t page_size,
240  const interface::types::AccountIdType &account_id = "",
242  const interface::types::AccountIdType &writer = "",
243  const std::optional<plain::AccountDetailRecordId> &first_record_id =
244  std::nullopt) {
245  return queryField([&](auto proto_query) {
246  auto query = proto_query->mutable_get_account_detail();
247  if (not account_id.empty()) {
248  query->set_account_id(account_id);
249  }
250  if (not key.empty()) {
251  query->set_key(key);
252  }
253  if (not writer.empty()) {
254  query->set_writer(writer);
255  }
256  auto pagination_meta = query->mutable_pagination_meta();
257  pagination_meta->set_page_size(page_size);
258  if (first_record_id) {
259  auto proto_first_record_id =
260  pagination_meta->mutable_first_record_id();
261  proto_first_record_id->set_writer(first_record_id->writer());
262  proto_first_record_id->set_key(first_record_id->key());
263  }
264  });
265  }
266 
268  return queryField([&](auto proto_query) {
269  auto query = proto_query->mutable_get_block();
270  query->set_height(height);
271  });
272  }
273 
274  auto getEngineReceipts(std::string_view tx_hash) {
275  return queryField([&](auto proto_query) {
276  auto query = proto_query->mutable_get_engine_receipts();
277  query->set_tx_hash(tx_hash.data(), tx_hash.size());
278  });
279  }
280 
281  auto getRoles() const {
282  return queryField(
283  [&](auto proto_query) { proto_query->mutable_get_roles(); });
284  }
285 
286  auto getAssetInfo(const interface::types::AssetIdType &asset_id) const {
287  return queryField([&](auto proto_query) {
288  auto query = proto_query->mutable_get_asset_info();
289  query->set_asset_id(asset_id);
290  });
291  }
292 
294  const {
295  return queryField([&](auto proto_query) {
296  auto query = proto_query->mutable_get_role_permissions();
297  query->set_role_id(role_id);
298  });
299  }
300 
301  template <typename Collection>
302  auto getTransactions(const Collection &hashes) const {
303  return queryField([&](auto proto_query) {
304  auto query = proto_query->mutable_get_transactions();
305  boost::for_each(hashes, [&query](const auto &hash) {
306  query->add_tx_hashes(hash.hex());
307  });
308  });
309  }
310 
312  std::initializer_list<interface::types::HashType> hashes) const {
313  return getTransactions(hashes);
314  }
315 
316  template <typename... Hash>
317  auto getTransactions(const Hash &... hashes) const {
318  return getTransactions({hashes...});
319  }
320 
321  auto getPendingTransactions() const {
322  return queryField([&](auto proto_query) {
323  proto_query->mutable_get_pending_transactions();
324  });
325  }
326 
329  const std::optional<interface::types::HashType> &first_hash =
330  std::nullopt) const {
331  return queryField([&](auto proto_query) {
332  auto query = proto_query->mutable_get_pending_transactions();
333  setTxPaginationMeta(
334  query->mutable_pagination_meta(), page_size, first_hash);
335  });
336  }
337 
338  auto getPeers() const {
339  return queryField(
340  [&](auto proto_query) { proto_query->mutable_get_peers(); });
341  }
342 
343  auto build() const {
344  static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
345  if (not query_.has_payload()) {
346  throw std::invalid_argument("Query missing payload");
347  }
348  if (query_.payload().query_case()
349  == iroha::protocol::Query_Payload::QueryCase::QUERY_NOT_SET) {
350  throw std::invalid_argument("Missing concrete query");
351  }
352  auto result = Query(iroha::protocol::Query(query_));
353  if (auto error = stateless_validator_.validate(result)) {
354  throw std::invalid_argument(error->toString());
355  }
356  return BT(std::move(result));
357  }
358 
359  static const int total = RequiredFields::TOTAL;
360 
361  private:
362  ProtoQuery query_;
363  SV stateless_validator_;
364  };
365  } // namespace proto
366 } // namespace shared_model
367 
368 #endif // IROHA_PROTO_QUERY_BUILDER_TEMPLATE_HPP
auto getBlock(interface::types::HeightType height) const
Definition: query_template.hpp:267
Definition: ordering.hpp:19
auto getTransactions(const Hash &... hashes) const
Definition: query_template.hpp:317
uint64_t HeightType
Type of height (for Block, Proposal etc)
Definition: types.hpp:48
Direction direction
Definition: ordering.hpp:48
auto createdTime(interface::types::TimestampType created_time) const
Definition: query_template.hpp:152
QueryValidator< FieldValidator, QueryValidatorVisitor< FieldValidator > > DefaultUnsignedQueryValidator
Definition: default_validator.hpp:57
auto getAccountAssets(const interface::types::AccountIdType &account_id, size_t page_size, std::optional< shared_model::interface::types::AssetIdType > first_asset_id) const
Definition: query_template.hpp:222
auto creatorAccountId(const interface::types::AccountIdType &creator_account_id) const
Definition: query_template.hpp:158
Definition: query_response_handler.cpp:72
auto getTransactions(const Collection &hashes) const
Definition: query_template.hpp:302
auto getAssetInfo(const interface::types::AssetIdType &asset_id) const
Definition: query_template.hpp:286
auto getPeers() const
Definition: query_template.hpp:338
auto getAccountTransactions(const interface::types::AccountIdType &account_id, interface::types::TransactionsNumberType page_size, const std::optional< interface::types::HashType > &first_hash=std::nullopt, const interface::Ordering *ordering=nullptr) const
Definition: query_template.hpp:188
Field field
Definition: ordering.hpp:47
Field
Definition: ordering.hpp:24
auto getAccount(const interface::types::AccountIdType &account_id) const
Definition: query_template.hpp:173
auto getPendingTransactions(interface::types::TransactionsNumberType page_size, const std::optional< interface::types::HashType > &first_hash=std::nullopt) const
Definition: query_template.hpp:327
TemplateQueryBuilder()
Definition: query_template.hpp:149
auto queryCounter(interface::types::CounterType query_counter) const
Definition: query_template.hpp:166
auto getPendingTransactions() const
Definition: query_template.hpp:321
std::string AccountDetailKeyType
Type of account detail key.
Definition: types.hpp:79
Definition: block_query.hpp:15
Definition: proto_query.hpp:16
std::string AccountIdType
Type of account id.
Definition: types.hpp:38
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
auto getSignatories(const interface::types::AccountIdType &account_id) const
Definition: query_template.hpp:180
auto getRoles() const
Definition: query_template.hpp:281
uint16_t TransactionsNumberType
Type of a number of transactions in block and query response page.
Definition: types.hpp:84
auto getTransactions(std::initializer_list< interface::types::HashType > hashes) const
Definition: query_template.hpp:311
uint64_t CounterType
Type of counter.
Definition: types.hpp:69
auto getAccountDetail(size_t page_size, const interface::types::AccountIdType &account_id="", const interface::types::AccountDetailKeyType &key="", const interface::types::AccountIdType &writer="", const std::optional< plain::AccountDetailRecordId > &first_record_id=std::nullopt)
Definition: query_template.hpp:238
auto getAccountAssetTransactions(const interface::types::AccountIdType &account_id, const interface::types::AssetIdType &asset_id, interface::types::TransactionsNumberType page_size, const std::optional< interface::types::HashType > &first_hash=std::nullopt, const interface::Ordering *ordering=nullptr) const
Definition: query_template.hpp:204
Definition: command_executor.hpp:12
auto build() const
Definition: query_template.hpp:343
auto getEngineReceipts(std::string_view tx_hash)
Definition: query_template.hpp:274
Direction
Definition: ordering.hpp:35
Definition: query_template.hpp:37
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:54
auto getRolePermissions(const interface::types::RoleIdType &role_id) const
Definition: query_template.hpp:293
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:67
std::string AssetIdType
Type of asset id.
Definition: types.hpp:58