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