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  if (qry.paginationMeta()) {
120  validator_.validateAssetPaginationMeta(reason, *qry.paginationMeta());
121  }
122  return reason;
123  }
124 
126  const interface::GetAccountDetail &qry) const {
127  ReasonsGroupType reason;
128  reason.first = "GetAccountDetail";
129 
130  validator_.validateAccountId(reason, qry.accountId());
131  if (qry.key()) {
132  validator_.validateAccountDetailKey(reason, *qry.key());
133  }
134  if (qry.writer()) {
135  validator_.validateAccountId(reason, *qry.writer());
136  }
137 
138  return reason;
139  }
140 
142  ReasonsGroupType reason;
143  reason.first = "GetRoles";
144 
145  return reason;
146  }
147 
149  const interface::GetRolePermissions &qry) const {
150  ReasonsGroupType reason;
151  reason.first = "GetRolePermissions";
152 
153  validator_.validateRoleId(reason, qry.roleId());
154 
155  return reason;
156  }
157 
159  ReasonsGroupType reason;
160  reason.first = "GetAssetInfo";
161 
162  validator_.validateAssetId(reason, qry.assetId());
163 
164  return reason;
165  }
166 
168  const interface::GetPendingTransactions &qry) const {
169  ReasonsGroupType reason;
170  reason.first = "GetPendingTransactions";
171 
172  return reason;
173  }
174 
175  private:
176  FieldValidator validator_;
177  };
178 
184  template <typename FieldValidator, typename QueryFieldValidator>
185  class QueryValidator : public AbstractValidator<interface::Query> {
186  QueryValidator(const FieldValidator &field_validator,
187  const QueryFieldValidator &query_field_validator)
188  : field_validator_(field_validator),
189  query_field_validator_(query_field_validator) {}
190 
191  public:
192  QueryValidator(std::shared_ptr<ValidatorsConfig> config)
193  : QueryValidator(FieldValidator{config},
194  QueryFieldValidator{config}) {}
195 
201  Answer validate(const interface::Query &qry) const override {
202  Answer answer;
203  std::string qry_reason_name = "Query";
204  ReasonsGroupType qry_reason(qry_reason_name, GroupedReasons());
205 
206  field_validator_.validateCreatorAccountId(qry_reason,
207  qry.creatorAccountId());
208  field_validator_.validateCreatedTime(qry_reason, qry.createdTime());
209  field_validator_.validateCounter(qry_reason, qry.queryCounter());
210 
211  if (not qry_reason.second.empty()) {
212  answer.addReason(std::move(qry_reason));
213  }
214 
215  auto field_reason =
216  boost::apply_visitor(query_field_validator_, qry.get());
217  if (not field_reason.second.empty()) {
218  answer.addReason(std::move(field_reason));
219  }
220 
221  return answer;
222  }
223 
224  protected:
227  QueryFieldValidator query_field_validator_;
228  };
229  } // namespace validation
230 } // namespace shared_model
231 
232 #endif // IROHA_SHARED_MODEL_QUERY_VALIDATOR_HPP
Definition: get_pending_transactions.hpp:19
QueryFieldValidator query_field_validator_
Definition: query_validator.hpp:227
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:158
Definition: get_transactions.hpp:14
void validateAccountId(ReasonsGroupType &reason, const interface::types::AccountIdType &account_id) const
Definition: field_validator.cpp:71
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
virtual boost::optional< types::AccountIdType > writer() const =0
Answer answer_
Definition: query_validator.hpp:225
Definition: abstract_validator.hpp:16
virtual boost::optional< const interface::AssetPaginationMeta & > paginationMeta() const =0
Get the query pagination metadata.
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:125
virtual const TransactionHashesType & transactionHashes() const =0
virtual const QueryVariantType & get() const =0
ReasonsGroupType operator()(const interface::GetPendingTransactions &qry) const
Definition: query_validator.hpp:167
void validateAssetId(ReasonsGroupType &reason, const interface::types::AssetIdType &asset_id) const
Definition: field_validator.cpp:84
QueryValidator(std::shared_ptr< ValidatorsConfig > config)
Definition: query_validator.hpp:192
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:201
void validateTxPaginationMeta(ReasonsGroupType &reason, const interface::TxPaginationMeta &tx_pagination_meta) const
Definition: field_validator.cpp:400
virtual types::CounterType queryCounter() const =0
void validateRoleId(ReasonsGroupType &reason, const interface::types::RoleIdType &role_id) const
Definition: field_validator.cpp:137
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:141
void validateHash(ReasonsGroupType &reason, const crypto::Hash &hash) const
Definition: field_validator.cpp:370
ReasonsGroupType operator()(const interface::GetRolePermissions &qry) const
Definition: query_validator.hpp:148
Definition: query_validator.hpp:36
virtual const types::AccountIdType & accountId() const =0
Definition: field_validator.hpp:34
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::GetSignatories &qry) const
Definition: query_validator.hpp:65
Definition: get_role_permissions.hpp:18
void validateAccountDetailKey(ReasonsGroupType &reason, const interface::types::AccountDetailKeyType &key) const
Definition: field_validator.cpp:187
std::pair< ReasonsGroupName, GroupedReasons > ReasonsGroupType
Definition: answer.hpp:22
void validateAssetPaginationMeta(ReasonsGroupType &reason, const interface::AssetPaginationMeta &asset_pagination_meta) const
Definition: field_validator.cpp:410
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:359
Definition: query_validator.hpp:185
FieldValidator field_validator_
Definition: query_validator.hpp:226
Definition: answer.hpp:28
Definition: get_account_assets.hpp:20