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/optional.hpp>
10 #include <boost/range/algorithm/for_each.hpp>
11 
16 #include "module/irohad/common/validators_config.hpp"
17 #include "queries.pb.h"
19 
20 namespace shared_model {
21  namespace proto {
22 
31  template <int S = 0,
33  typename BT = UnsignedWrapper<Query>>
34  class [[deprecated]] TemplateQueryBuilder {
35  private:
36  template <int, typename, typename>
37  friend class TemplateQueryBuilder;
38 
39  enum RequiredFields {
40  CreatedTime,
41  CreatorAccountId,
42  QueryField,
43  QueryCounter,
44  TOTAL
45  };
46 
47  template <int s>
49 
50  using ProtoQuery = iroha::protocol::Query;
51 
52  template <int Sp>
54  : query_(o.query_), stateless_validator_(o.stateless_validator_) {}
55 
62  template <int Fields, typename Transformation>
63  auto transform(Transformation t) const {
64  NextBuilder<Fields> copy = *this;
65  t(copy.query_);
66  return copy;
67  }
68 
75  template <typename Transformation>
76  auto queryField(Transformation t) const {
77  NextBuilder<QueryField> copy = *this;
78  t(copy.query_.mutable_payload());
79  return copy;
80  }
81 
83  template <typename PageMetaPayload>
84  static auto setTxPaginationMeta(
85  PageMetaPayload * page_meta_payload,
87  const boost::optional<interface::types::HashType> &first_hash =
88  boost::none) {
89  page_meta_payload->set_page_size(page_size);
90  if (first_hash) {
91  page_meta_payload->set_first_tx_hash(first_hash->hex());
92  }
93  }
94 
95  TemplateQueryBuilder(const SV &validator)
96  : stateless_validator_(validator) {}
97 
98  public:
99  // we do such default initialization only because it is deprecated and
100  // used only in tests
102  : TemplateQueryBuilder(SV(iroha::test::kTestsValidatorsConfig)) {}
103 
104  auto createdTime(interface::types::TimestampType created_time) const {
105  return transform<CreatedTime>([&](auto &qry) {
106  qry.mutable_payload()->mutable_meta()->set_created_time(created_time);
107  });
108  }
109 
111  const interface::types::AccountIdType &creator_account_id) const {
112  return transform<CreatorAccountId>([&](auto &qry) {
113  qry.mutable_payload()->mutable_meta()->set_creator_account_id(
114  creator_account_id);
115  });
116  }
117 
118  auto queryCounter(interface::types::CounterType query_counter) const {
119  return transform<QueryCounter>([&](auto &qry) {
120  qry.mutable_payload()->mutable_meta()->set_query_counter(
121  query_counter);
122  });
123  }
124 
125  auto getAccount(const interface::types::AccountIdType &account_id) const {
126  return queryField([&](auto proto_query) {
127  auto query = proto_query->mutable_get_account();
128  query->set_account_id(account_id);
129  });
130  }
131 
133  const {
134  return queryField([&](auto proto_query) {
135  auto query = proto_query->mutable_get_signatories();
136  query->set_account_id(account_id);
137  });
138  }
139 
141  const interface::types::AccountIdType &account_id,
143  const boost::optional<interface::types::HashType> &first_hash =
144  boost::none) const {
145  return queryField([&](auto proto_query) {
146  auto query = proto_query->mutable_get_account_transactions();
147  query->set_account_id(account_id);
148  setTxPaginationMeta(
149  query->mutable_pagination_meta(), page_size, first_hash);
150  });
151  }
152 
154  const interface::types::AccountIdType &account_id,
155  const interface::types::AssetIdType &asset_id,
157  const boost::optional<interface::types::HashType> &first_hash =
158  boost::none) const {
159  return queryField([&](auto proto_query) {
160  auto query = proto_query->mutable_get_account_asset_transactions();
161  query->set_account_id(account_id);
162  query->set_asset_id(asset_id);
163  setTxPaginationMeta(
164  query->mutable_pagination_meta(), page_size, first_hash);
165  });
166  }
167 
169  const interface::types::AccountIdType &account_id,
170  size_t page_size,
171  boost::optional<shared_model::interface::types::AssetIdType>
172  first_asset_id) const {
173  return queryField([&](auto proto_query) {
174  auto query = proto_query->mutable_get_account_assets();
175  query->set_account_id(account_id);
176  auto pagination_meta = query->mutable_pagination_meta();
177  pagination_meta->set_page_size(page_size);
178  if (first_asset_id) {
179  pagination_meta->set_first_asset_id(*first_asset_id);
180  }
181  });
182  }
183 
185  const interface::types::AccountIdType &account_id = "",
187  const interface::types::AccountIdType &writer = "") {
188  return queryField([&](auto proto_query) {
189  auto query = proto_query->mutable_get_account_detail();
190  if (not account_id.empty()) {
191  query->set_account_id(account_id);
192  }
193  if (not key.empty()) {
194  query->set_key(key);
195  }
196  if (not writer.empty()) {
197  query->set_writer(writer);
198  }
199  });
200  }
201 
203  return queryField([&](auto proto_query) {
204  auto query = proto_query->mutable_get_block();
205  query->set_height(height);
206  });
207  }
208 
209  auto getRoles() const {
210  return queryField(
211  [&](auto proto_query) { proto_query->mutable_get_roles(); });
212  }
213 
214  auto getAssetInfo(const interface::types::AssetIdType &asset_id) const {
215  return queryField([&](auto proto_query) {
216  auto query = proto_query->mutable_get_asset_info();
217  query->set_asset_id(asset_id);
218  });
219  }
220 
222  const {
223  return queryField([&](auto proto_query) {
224  auto query = proto_query->mutable_get_role_permissions();
225  query->set_role_id(role_id);
226  });
227  }
228 
229  template <typename Collection>
230  auto getTransactions(const Collection &hashes) const {
231  return queryField([&](auto proto_query) {
232  auto query = proto_query->mutable_get_transactions();
233  boost::for_each(hashes, [&query](const auto &hash) {
234  query->add_tx_hashes(hash.hex());
235  });
236  });
237  }
238 
240  std::initializer_list<interface::types::HashType> hashes) const {
241  return getTransactions(hashes);
242  }
243 
244  template <typename... Hash>
245  auto getTransactions(const Hash &... hashes) const {
246  return getTransactions({hashes...});
247  }
248 
249  auto getPendingTransactions() const {
250  return queryField([&](auto proto_query) {
251  proto_query->mutable_get_pending_transactions();
252  });
253  }
254 
255  auto build() const {
256  static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
257  if (not query_.has_payload()) {
258  throw std::invalid_argument("Query missing payload");
259  }
260  if (query_.payload().query_case()
261  == iroha::protocol::Query_Payload::QueryCase::QUERY_NOT_SET) {
262  throw std::invalid_argument("Missing concrete query");
263  }
264  auto result = Query(iroha::protocol::Query(query_));
265  auto answer = stateless_validator_.validate(result);
266  if (answer.hasErrors()) {
267  throw std::invalid_argument(answer.reason());
268  }
269  return BT(std::move(result));
270  }
271 
272  static const int total = RequiredFields::TOTAL;
273 
274  private:
275  ProtoQuery query_;
276  SV stateless_validator_;
277  };
278  } // namespace proto
279 } // namespace shared_model
280 
281 #endif // IROHA_PROTO_QUERY_BUILDER_TEMPLATE_HPP
auto getAccountAssets(const interface::types::AccountIdType &account_id, size_t page_size, boost::optional< shared_model::interface::types::AssetIdType > first_asset_id) const
Definition: query_template.hpp:168
uint64_t HeightType
Type of height (for Block, Proposal etc)
Definition: types.hpp:41
auto getTransactions(std::initializer_list< interface::types::HashType > hashes) const
Definition: query_template.hpp:239
auto createdTime(interface::types::TimestampType created_time) const
Definition: query_template.hpp:104
TemplateQueryBuilder()
Definition: query_template.hpp:101
auto getRoles() const
Definition: query_template.hpp:209
std::string AccountDetailKeyType
Type of account detail key.
Definition: types.hpp:73
auto getAccount(const interface::types::AccountIdType &account_id) const
Definition: query_template.hpp:125
auto getAccountTransactions(const interface::types::AccountIdType &account_id, interface::types::TransactionsNumberType page_size, const boost::optional< interface::types::HashType > &first_hash=boost::none) const
Definition: query_template.hpp:140
auto getBlock(interface::types::HeightType height) const
Definition: query_template.hpp:202
Definition: block_query.hpp:14
Definition: proto_query.hpp:16
auto getAccountDetail(const interface::types::AccountIdType &account_id="", const interface::types::AccountDetailKeyType &key="", const interface::types::AccountIdType &writer="")
Definition: query_template.hpp:184
std::string AccountIdType
Type of account id.
Definition: types.hpp:37
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
auto getSignatories(const interface::types::AccountIdType &account_id) const
Definition: query_template.hpp:132
auto creatorAccountId(const interface::types::AccountIdType &creator_account_id) const
Definition: query_template.hpp:110
QueryValidator< FieldValidator, QueryValidatorVisitor< FieldValidator >> DefaultUnsignedQueryValidator
Definition: default_validator.hpp:57
static std::vector< std::shared_ptr< shared_model::interface::Transaction > > getTransactions(size_t requested_tx_amount, detail::BatchSetType &batch_collection, boost::optional< size_t & > discarded_txs_amount)
Definition: on_demand_ordering_service_impl.cpp:107
auto getTransactions(const Hash &...hashes) const
Definition: query_template.hpp:245
auto getRolePermissions(const interface::types::RoleIdType &role_id) const
Definition: query_template.hpp:221
auto getAssetInfo(const interface::types::AssetIdType &asset_id) const
Definition: query_template.hpp:214
uint16_t TransactionsNumberType
Type of a number of transactions in block and query response page.
Definition: types.hpp:77
uint64_t CounterType
Type of counter.
Definition: types.hpp:63
auto getAccountAssetTransactions(const interface::types::AccountIdType &account_id, const interface::types::AssetIdType &asset_id, interface::types::TransactionsNumberType page_size, const boost::optional< interface::types::HashType > &first_hash=boost::none) const
Definition: query_template.hpp:153
auto getPendingTransactions() const
Definition: query_template.hpp:249
auto queryCounter(interface::types::CounterType query_counter) const
Definition: query_template.hpp:118
Definition: command_executor.hpp:12
auto getTransactions(const Collection &hashes) const
Definition: query_template.hpp:230
auto build() const
Definition: query_template.hpp:255
Definition: query_template.hpp:34
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:49
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:61
std::string AssetIdType
Type of asset id.
Definition: types.hpp:53