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 {
170  return queryField([&](auto proto_query) {
171  auto query = proto_query->mutable_get_account_assets();
172  query->set_account_id(account_id);
173  });
174  }
175 
177  const interface::types::AccountIdType &account_id = "",
179  const interface::types::AccountIdType &writer = "") {
180  return queryField([&](auto proto_query) {
181  auto query = proto_query->mutable_get_account_detail();
182  if (not account_id.empty()) {
183  query->set_account_id(account_id);
184  }
185  if (not key.empty()) {
186  query->set_key(key);
187  }
188  if (not writer.empty()) {
189  query->set_writer(writer);
190  }
191  });
192  }
193 
195  return queryField([&](auto proto_query) {
196  auto query = proto_query->mutable_get_block();
197  query->set_height(height);
198  });
199  }
200 
201  auto getRoles() const {
202  return queryField(
203  [&](auto proto_query) { proto_query->mutable_get_roles(); });
204  }
205 
206  auto getAssetInfo(const interface::types::AssetIdType &asset_id) const {
207  return queryField([&](auto proto_query) {
208  auto query = proto_query->mutable_get_asset_info();
209  query->set_asset_id(asset_id);
210  });
211  }
212 
214  const {
215  return queryField([&](auto proto_query) {
216  auto query = proto_query->mutable_get_role_permissions();
217  query->set_role_id(role_id);
218  });
219  }
220 
221  template <typename Collection>
222  auto getTransactions(const Collection &hashes) const {
223  return queryField([&](auto proto_query) {
224  auto query = proto_query->mutable_get_transactions();
225  boost::for_each(hashes, [&query](const auto &hash) {
226  query->add_tx_hashes(hash.hex());
227  });
228  });
229  }
230 
232  std::initializer_list<interface::types::HashType> hashes) const {
233  return getTransactions(hashes);
234  }
235 
236  template <typename... Hash>
237  auto getTransactions(const Hash &... hashes) const {
238  return getTransactions({hashes...});
239  }
240 
241  auto getPendingTransactions() const {
242  return queryField([&](auto proto_query) {
243  proto_query->mutable_get_pending_transactions();
244  });
245  }
246 
247  auto build() const {
248  static_assert(S == (1 << TOTAL) - 1, "Required fields are not set");
249  if (not query_.has_payload()) {
250  throw std::invalid_argument("Query missing payload");
251  }
252  if (query_.payload().query_case()
253  == iroha::protocol::Query_Payload::QueryCase::QUERY_NOT_SET) {
254  throw std::invalid_argument("Missing concrete query");
255  }
256  auto result = Query(iroha::protocol::Query(query_));
257  auto answer = stateless_validator_.validate(result);
258  if (answer.hasErrors()) {
259  throw std::invalid_argument(answer.reason());
260  }
261  return BT(std::move(result));
262  }
263 
264  static const int total = RequiredFields::TOTAL;
265 
266  private:
267  ProtoQuery query_;
268  SV stateless_validator_;
269  };
270  } // namespace proto
271 } // namespace shared_model
272 
273 #endif // IROHA_PROTO_QUERY_BUILDER_TEMPLATE_HPP
uint64_t HeightType
Type of height (for Block, Proposal etc)
Definition: types.hpp:39
auto getTransactions(std::initializer_list< interface::types::HashType > hashes) const
Definition: query_template.hpp:231
auto createdTime(interface::types::TimestampType created_time) const
Definition: query_template.hpp:104
auto getAccountAssets(const interface::types::AccountIdType &account_id) const
Definition: query_template.hpp:168
TemplateQueryBuilder()
Definition: query_template.hpp:101
auto getRoles() const
Definition: query_template.hpp:201
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:194
Definition: block_query.hpp:16
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:176
std::string AccountIdType
Type of account id.
Definition: types.hpp:35
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:105
auto getTransactions(const Hash &...hashes) const
Definition: query_template.hpp:237
auto getRolePermissions(const interface::types::RoleIdType &role_id) const
Definition: query_template.hpp:213
auto getAssetInfo(const interface::types::AssetIdType &asset_id) const
Definition: query_template.hpp:206
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:241
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:222
auto build() const
Definition: query_template.hpp:247
Definition: query_template.hpp:34
std::string RoleIdType
Type of role (i.e admin, user)
Definition: types.hpp:47
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:59
std::string AssetIdType
Type of asset id.
Definition: types.hpp:51