hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
query_validator.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_SHARED_MODEL_QUERY_VALIDATOR_HPP
7 #define IROHA_SHARED_MODEL_QUERY_VALIDATOR_HPP
8 
9 #include <boost/variant/apply_visitor.hpp>
10 #include <boost/variant/static_visitor.hpp>
11 
12 #include "common/bind.hpp"
28 #include "validators/answer.hpp"
29 
30 namespace shared_model {
31  namespace validation {
32 
37  template <typename FieldValidator>
39  : public boost::static_visitor<ReasonsGroupType> {
41  : validator_(std::move(validator)) {}
42 
43  public:
44  // todo igor-egorov 05.04.2018 IR-439 Remove ValidatorsConfig from
45  // FieldValidator => and from QueryValidatorVisitor too
46  QueryValidatorVisitor(std::shared_ptr<ValidatorsConfig> config)
47  : QueryValidatorVisitor(FieldValidator{std::move(config)}) {}
48 
50  ReasonsGroupType reason;
51  reason.first = "GetAccount";
52 
53  validator_.validateAccountId(reason, qry.accountId());
54 
55  return reason;
56  }
57 
59  ReasonsGroupType reason;
60  reason.first = "GetBlock";
61 
62  validator_.validateHeight(reason, qry.height());
63 
64  return reason;
65  }
66 
68  ReasonsGroupType reason;
69  reason.first = "GetSignatories";
70 
71  validator_.validateAccountId(reason, qry.accountId());
72 
73  return reason;
74  }
75 
77  const interface::GetAccountTransactions &qry) const {
78  ReasonsGroupType reason;
79  reason.first = "GetAccountTransactions";
80 
81  validator_.validateAccountId(reason, qry.accountId());
82  validator_.validateTxPaginationMeta(reason, qry.paginationMeta());
83 
84  return reason;
85  }
86 
88  const interface::GetAccountAssetTransactions &qry) const {
89  ReasonsGroupType reason;
90  reason.first = "GetAccountAssetTransactions";
91 
92  validator_.validateAccountId(reason, qry.accountId());
93  validator_.validateAssetId(reason, qry.assetId());
94  validator_.validateTxPaginationMeta(reason, qry.paginationMeta());
95 
96  return reason;
97  }
98 
100  ReasonsGroupType reason;
101  reason.first = "GetTransactions";
102 
103  const auto &hashes = qry.transactionHashes();
104  if (hashes.size() == 0) {
105  reason.second.push_back("tx_hashes cannot be empty");
106  }
107 
108  for (const auto &h : hashes) {
109  validator_.validateHash(reason, h);
110  }
111 
112  return reason;
113  }
114 
116  const interface::GetAccountAssets &qry) const {
117  ReasonsGroupType reason;
118  reason.first = "GetAccountAssets";
119 
120  validator_.validateAccountId(reason, qry.accountId());
121  if (qry.paginationMeta()) {
122  validator_.validateAssetPaginationMeta(reason, *qry.paginationMeta());
123  }
124  return reason;
125  }
126 
128  const interface::GetAccountDetail &qry) const {
129  ReasonsGroupType reason;
130  reason.first = "GetAccountDetail";
131 
132  using iroha::operator|;
133 
134  validator_.validateAccountId(reason, qry.accountId());
135  qry.key() | [&reason, this](const auto &key) {
136  this->validator_.validateAccountDetailKey(reason, key);
137  };
138  qry.writer() | [&reason, this](const auto &writer) {
139  this->validator_.validateAccountId(reason, writer);
140  };
141  qry.paginationMeta() | [&reason, this](const auto &pagination_meta) {
142  this->validator_.validateAccountDetailPaginationMeta(reason,
143  pagination_meta);
144  };
145 
146  return reason;
147  }
148 
150  ReasonsGroupType reason;
151  reason.first = "GetRoles";
152 
153  return reason;
154  }
155 
157  const interface::GetRolePermissions &qry) const {
158  ReasonsGroupType reason;
159  reason.first = "GetRolePermissions";
160 
161  validator_.validateRoleId(reason, qry.roleId());
162 
163  return reason;
164  }
165 
167  ReasonsGroupType reason;
168  reason.first = "GetAssetInfo";
169 
170  validator_.validateAssetId(reason, qry.assetId());
171 
172  return reason;
173  }
174 
176  const interface::GetPendingTransactions &qry) const {
177  ReasonsGroupType reason;
178  reason.first = "GetPendingTransactions";
179  if (qry.paginationMeta()) {
180  // TODO igor-egorov 2019-06-06 IR-516 Make meta non-optional
181  validator_.validateTxPaginationMeta(reason, *qry.paginationMeta());
182  }
183 
184  return reason;
185  }
186 
188  ReasonsGroupType reason;
189  reason.first = "GetPeers";
190 
191  return reason;
192  }
193 
194  private:
195  FieldValidator validator_;
196  };
197 
203  template <typename FieldValidator, typename QueryFieldValidator>
204  class QueryValidator : public AbstractValidator<interface::Query> {
205  QueryValidator(const FieldValidator &field_validator,
206  const QueryFieldValidator &query_field_validator)
207  : field_validator_(field_validator),
208  query_field_validator_(query_field_validator) {}
209 
210  public:
211  QueryValidator(std::shared_ptr<ValidatorsConfig> config)
212  : QueryValidator(FieldValidator{config},
213  QueryFieldValidator{config}) {}
214 
220  Answer validate(const interface::Query &qry) const override {
221  Answer answer;
222  std::string qry_reason_name = "Query";
223  ReasonsGroupType qry_reason(qry_reason_name, GroupedReasons());
224 
225  field_validator_.validateCreatorAccountId(qry_reason,
226  qry.creatorAccountId());
227  field_validator_.validateCreatedTime(qry_reason, qry.createdTime());
228  field_validator_.validateCounter(qry_reason, qry.queryCounter());
229 
230  if (not qry_reason.second.empty()) {
231  answer.addReason(std::move(qry_reason));
232  }
233 
234  auto field_reason =
235  boost::apply_visitor(query_field_validator_, qry.get());
236  if (not field_reason.second.empty()) {
237  answer.addReason(std::move(field_reason));
238  }
239 
240  return answer;
241  }
242 
243  protected:
246  QueryFieldValidator query_field_validator_;
247  };
248  } // namespace validation
249 } // namespace shared_model
250 
251 #endif // IROHA_SHARED_MODEL_QUERY_VALIDATOR_HPP
virtual boost::optional< const AccountDetailPaginationMeta & > paginationMeta() const =0
Get the query pagination metadata.
Definition: get_pending_transactions.hpp:21
QueryFieldValidator query_field_validator_
Definition: query_validator.hpp:246
virtual const types::AccountIdType & creatorAccountId() const =0
virtual const TxPaginationMeta & paginationMeta() const =0
Get the query pagination metadata.
ReasonsGroupType operator()(const interface::GetAccount &qry) const
Definition: query_validator.hpp:49
Definition: get_asset_info.hpp:17
virtual const types::AccountIdType & accountId() const =0
virtual types::HeightType height() const =0
void validateAccountId(ReasonsGroupType &reason, const interface::types::AccountIdType &account_id) const
Definition: field_validator.cpp:76
virtual const types::AccountIdType & accountId() const =0
Definition: query.hpp:35
Definition: get_transactions.hpp:14
ReasonsGroupType operator()(const interface::GetPendingTransactions &qry) const
Definition: query_validator.hpp:175
virtual const types::AccountIdType & assetId() const =0
void validateAssetId(ReasonsGroupType &reason, const interface::types::AssetIdType &asset_id) const
Definition: field_validator.cpp:89
ReasonsGroupType operator()(const interface::GetAccountTransactions &qry) const
Definition: query_validator.hpp:76
virtual const types::RoleIdType & roleId() const =0
void addReason(ReasonsGroupType &&reasons)
Definition: answer.hpp:64
ReasonsGroupType operator()(const interface::GetRoles &qry) const
Definition: query_validator.hpp:149
virtual boost::optional< types::AccountIdType > writer() const =0
Answer answer_
Definition: query_validator.hpp:244
Definition: abstract_validator.hpp:16
void validateHeight(ReasonsGroupType &reason, const interface::types::HeightType &height) const
Definition: field_validator.cpp:370
virtual boost::optional< const interface::AssetPaginationMeta & > paginationMeta() const =0
Get the query pagination metadata.
Definition: get_account_asset_transactions.hpp:21
void validateRoleId(ReasonsGroupType &reason, const interface::types::RoleIdType &role_id) const
Definition: field_validator.cpp:139
virtual const types::AccountIdType & accountId() const =0
virtual const TransactionHashesType & transactionHashes() const =0
virtual const QueryVariantType & get() const =0
QueryValidator(std::shared_ptr< ValidatorsConfig > config)
Definition: query_validator.hpp:211
ReasonsGroupType operator()(const interface::GetRolePermissions &qry) const
Definition: query_validator.hpp:156
void validateTxPaginationMeta(ReasonsGroupType &reason, const interface::TxPaginationMeta &tx_pagination_meta) const
Definition: field_validator.cpp:411
ReasonsGroupType operator()(const interface::GetAssetInfo &qry) const
Definition: query_validator.hpp:166
ReasonsGroupType operator()(const interface::GetTransactions &qry) const
Definition: query_validator.hpp:99
virtual const TxPaginationMeta & paginationMeta() const =0
Get the query pagination metadata.
QueryValidatorVisitor(std::shared_ptr< ValidatorsConfig > config)
Definition: query_validator.hpp:46
void validateAssetPaginationMeta(ReasonsGroupType &reason, const interface::AssetPaginationMeta &asset_pagination_meta) const
Definition: field_validator.cpp:421
virtual const types::AssetIdType & assetId() const =0
Definition: get_signatories.hpp:18
virtual types::TimestampType createdTime() const =0
Definition: get_account_transactions.hpp:21
ReasonsGroupType operator()(const interface::GetSignatories &qry) const
Definition: query_validator.hpp:67
Answer validate(const interface::Query &qry) const override
Definition: query_validator.hpp:220
virtual types::CounterType queryCounter() const =0
void validateHash(ReasonsGroupType &reason, const crypto::Hash &hash) const
Definition: field_validator.cpp:381
ReasonsGroupType operator()(const interface::GetPeers &qry) const
Definition: query_validator.hpp:187
Definition: get_roles.hpp:17
Definition: get_peers.hpp:17
ReasonsGroupType operator()(const interface::GetAccountAssetTransactions &qry) const
Definition: query_validator.hpp:87
Definition: get_account_detail.hpp:29
Definition: get_account.hpp:14
virtual const types::AccountIdType & accountId() const =0
virtual const types::AccountIdType & accountId() const =0
Definition: query_validator.hpp:38
virtual const types::AccountIdType & accountId() const =0
void validateAccountDetailKey(ReasonsGroupType &reason, const interface::types::AccountDetailKeyType &key) const
Definition: field_validator.cpp:189
Definition: field_validator.hpp:35
Definition: get_block.hpp:14
virtual boost::optional< types::AccountDetailKeyType > key() const =0
std::vector< ConcreteReasonType > GroupedReasons
Definition: answer.hpp:20
ReasonsGroupType operator()(const interface::GetBlock &qry) const
Definition: query_validator.hpp:58
ReasonsGroupType operator()(const interface::GetAccountAssets &qry) const
Definition: query_validator.hpp:115
Definition: get_role_permissions.hpp:18
ReasonsGroupType operator()(const interface::GetAccountDetail &qry) const
Definition: query_validator.hpp:127
std::pair< ReasonsGroupName, GroupedReasons > ReasonsGroupType
Definition: answer.hpp:22
Definition: command_executor.hpp:12
void validateAccountDetailPaginationMeta(ReasonsGroupType &reason, const interface::AccountDetailPaginationMeta &pagination_meta) const
Definition: field_validator.cpp:431
Definition: query_validator.hpp:204
FieldValidator field_validator_
Definition: query_validator.hpp:245
virtual boost::optional< const TxPaginationMeta & > paginationMeta() const =0
Definition: answer.hpp:28
Definition: get_account_assets.hpp:20