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/static_visitor.hpp>
10 
26 #include "validators/answer.hpp"
27 
28 namespace shared_model {
29  namespace validation {
30 
35  template <typename FieldValidator>
37  : public boost::static_visitor<ReasonsGroupType> {
39  : validator_(std::move(validator)) {}
40 
41  public:
42  // todo igor-egorov 05.04.2018 IR-439 Remove ValidatorsConfig from
43  // FieldValidator => and from QueryValidatorVisitor too
44  QueryValidatorVisitor(std::shared_ptr<ValidatorsConfig> config)
45  : QueryValidatorVisitor(FieldValidator{std::move(config)}) {}
46 
48  ReasonsGroupType reason;
49  reason.first = "GetAccount";
50 
51  validator_.validateAccountId(reason, qry.accountId());
52 
53  return reason;
54  }
55 
57  ReasonsGroupType reason;
58  reason.first = "GetBlock";
59 
60  validator_.validateHeight(reason, qry.height());
61 
62  return reason;
63  }
64 
66  ReasonsGroupType reason;
67  reason.first = "GetSignatories";
68 
69  validator_.validateAccountId(reason, qry.accountId());
70 
71  return reason;
72  }
73 
75  const interface::GetAccountTransactions &qry) const {
76  ReasonsGroupType reason;
77  reason.first = "GetAccountTransactions";
78 
79  validator_.validateAccountId(reason, qry.accountId());
80  validator_.validateTxPaginationMeta(reason, qry.paginationMeta());
81 
82  return reason;
83  }
84 
86  const interface::GetAccountAssetTransactions &qry) const {
87  ReasonsGroupType reason;
88  reason.first = "GetAccountAssetTransactions";
89 
90  validator_.validateAccountId(reason, qry.accountId());
91  validator_.validateAssetId(reason, qry.assetId());
92  validator_.validateTxPaginationMeta(reason, qry.paginationMeta());
93 
94  return reason;
95  }
96 
98  ReasonsGroupType reason;
99  reason.first = "GetTransactions";
100 
101  const auto &hashes = qry.transactionHashes();
102  if (hashes.size() == 0) {
103  reason.second.push_back("tx_hashes cannot be empty");
104  }
105 
106  for (const auto &h : hashes) {
107  validator_.validateHash(reason, h);
108  }
109 
110  return reason;
111  }
112 
114  const interface::GetAccountAssets &qry) const {
115  ReasonsGroupType reason;
116  reason.first = "GetAccountAssets";
117 
118  validator_.validateAccountId(reason, qry.accountId());
119  return reason;
120  }
121 
123  const interface::GetAccountDetail &qry) const {
124  ReasonsGroupType reason;
125  reason.first = "GetAccountDetail";
126 
127  validator_.validateAccountId(reason, qry.accountId());
128 
129  return reason;
130  }
131 
133  ReasonsGroupType reason;
134  reason.first = "GetRoles";
135 
136  return reason;
137  }
138 
140  const interface::GetRolePermissions &qry) const {
141  ReasonsGroupType reason;
142  reason.first = "GetRolePermissions";
143 
144  validator_.validateRoleId(reason, qry.roleId());
145 
146  return reason;
147  }
148 
150  ReasonsGroupType reason;
151  reason.first = "GetAssetInfo";
152 
153  validator_.validateAssetId(reason, qry.assetId());
154 
155  return reason;
156  }
157 
159  const interface::GetPendingTransactions &qry) const {
160  ReasonsGroupType reason;
161  reason.first = "GetPendingTransactions";
162 
163  return reason;
164  }
165 
166  private:
167  FieldValidator validator_;
168  };
169 
175  template <typename FieldValidator, typename QueryFieldValidator>
176  class QueryValidator : public AbstractValidator<interface::Query> {
177  QueryValidator(const FieldValidator &field_validator,
178  const QueryFieldValidator &query_field_validator)
179  : field_validator_(field_validator),
180  query_field_validator_(query_field_validator) {}
181 
182  public:
183  QueryValidator(std::shared_ptr<ValidatorsConfig> config)
184  : QueryValidator(FieldValidator{config},
185  QueryFieldValidator{config}) {}
186 
192  Answer validate(const interface::Query &qry) const override {
193  Answer answer;
194  std::string qry_reason_name = "Query";
195  ReasonsGroupType qry_reason(qry_reason_name, GroupedReasons());
196 
197  field_validator_.validateCreatorAccountId(qry_reason,
198  qry.creatorAccountId());
199  field_validator_.validateCreatedTime(qry_reason, qry.createdTime());
200  field_validator_.validateCounter(qry_reason, qry.queryCounter());
201 
202  if (not qry_reason.second.empty()) {
203  answer.addReason(std::move(qry_reason));
204  }
205 
206  auto field_reason =
207  boost::apply_visitor(query_field_validator_, qry.get());
208  if (not field_reason.second.empty()) {
209  answer.addReason(std::move(field_reason));
210  }
211 
212  return answer;
213  }
214 
215  protected:
218  QueryFieldValidator query_field_validator_;
219  };
220  } // namespace validation
221 } // namespace shared_model
222 
223 #endif // IROHA_SHARED_MODEL_QUERY_VALIDATOR_HPP
Definition: get_pending_transactions.hpp:19
QueryFieldValidator query_field_validator_
Definition: query_validator.hpp:218
ReasonsGroupType operator()(const interface::GetAccountAssetTransactions &qry) const
Definition: query_validator.hpp:85
virtual const types::AccountIdType & creatorAccountId() const =0
virtual const TxPaginationMeta & paginationMeta() const =0
Get the query pagination metadata.
Definition: get_asset_info.hpp:17
virtual const types::AccountIdType & accountId() const =0
virtual types::HeightType height() const =0
virtual const types::AccountIdType & accountId() const =0
Definition: query.hpp:34
ReasonsGroupType operator()(const interface::GetAssetInfo &qry) const
Definition: query_validator.hpp:149
Definition: get_transactions.hpp:14
void validateAccountId(ReasonsGroupType &reason, const interface::types::AccountIdType &account_id) const
Definition: field_validator.cpp:70
virtual const types::AccountIdType & assetId() const =0
ReasonsGroupType operator()(const interface::GetAccount &qry) const
Definition: query_validator.hpp:47
virtual const types::RoleIdType & roleId() const =0
void addReason(ReasonsGroupType &&reasons)
Definition: answer.hpp:64
Answer answer_
Definition: query_validator.hpp:216
Definition: abstract_validator.hpp:16
Definition: get_account_asset_transactions.hpp:21
virtual const types::AccountIdType & accountId() const =0
ReasonsGroupType operator()(const interface::GetAccountDetail &qry) const
Definition: query_validator.hpp:122
virtual const TransactionHashesType & transactionHashes() const =0
virtual const QueryVariantType & get() const =0
ReasonsGroupType operator()(const interface::GetPendingTransactions &qry) const
Definition: query_validator.hpp:158
void validateAssetId(ReasonsGroupType &reason, const interface::types::AssetIdType &asset_id) const
Definition: field_validator.cpp:83
QueryValidator(std::shared_ptr< ValidatorsConfig > config)
Definition: query_validator.hpp:183
ReasonsGroupType operator()(const interface::GetTransactions &qry) const
Definition: query_validator.hpp:97
virtual const TxPaginationMeta & paginationMeta() const =0
Get the query pagination metadata.
QueryValidatorVisitor(std::shared_ptr< ValidatorsConfig > config)
Definition: query_validator.hpp:44
virtual const types::AssetIdType & assetId() const =0
Definition: get_signatories.hpp:18
virtual types::TimestampType createdTime() const =0
Definition: get_account_transactions.hpp:21
Answer validate(const interface::Query &qry) const override
Definition: query_validator.hpp:192
void validateTxPaginationMeta(ReasonsGroupType &reason, const interface::TxPaginationMeta &tx_pagination_meta) const
Definition: field_validator.cpp:388
virtual types::CounterType queryCounter() const =0
void validateRoleId(ReasonsGroupType &reason, const interface::types::RoleIdType &role_id) const
Definition: field_validator.cpp:136
Definition: get_roles.hpp:17
ReasonsGroupType operator()(const interface::GetBlock &qry) const
Definition: query_validator.hpp:56
Definition: get_account_detail.hpp:28
Definition: get_account.hpp:14
virtual const types::AccountIdType & accountId() const =0
ReasonsGroupType operator()(const interface::GetAccountAssets &qry) const
Definition: query_validator.hpp:113
virtual const types::AccountIdType & accountId() const =0
ReasonsGroupType operator()(const interface::GetRoles &qry) const
Definition: query_validator.hpp:132
void validateHash(ReasonsGroupType &reason, const crypto::Hash &hash) const
Definition: field_validator.cpp:369
ReasonsGroupType operator()(const interface::GetRolePermissions &qry) const
Definition: query_validator.hpp:139
Definition: query_validator.hpp:36
virtual const types::AccountIdType & accountId() const =0
Definition: field_validator.hpp:33
Definition: get_block.hpp:14
std::vector< ConcreteReasonType > GroupedReasons
Definition: answer.hpp:20
ReasonsGroupType operator()(const interface::GetSignatories &qry) const
Definition: query_validator.hpp:65
Definition: get_role_permissions.hpp:18
std::pair< ReasonsGroupName, GroupedReasons > ReasonsGroupType
Definition: answer.hpp:22
Definition: command_executor.hpp:12
ReasonsGroupType operator()(const interface::GetAccountTransactions &qry) const
Definition: query_validator.hpp:74
void validateHeight(ReasonsGroupType &reason, const interface::types::HeightType &height) const
Definition: field_validator.cpp:358
Definition: query_validator.hpp:176
FieldValidator field_validator_
Definition: query_validator.hpp:217
Definition: answer.hpp:28
Definition: get_account_assets.hpp:17