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