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> {
38  public:
40  : validator_(validator) {}
41 
43  ReasonsGroupType reason;
44  reason.first = "GetAccount";
45 
46  validator_.validateAccountId(reason, qry.accountId());
47 
48  return reason;
49  }
50 
52  ReasonsGroupType reason;
53  reason.first = "GetBlock";
54 
55  validator_.validateHeight(reason, qry.height());
56 
57  return reason;
58  }
59 
61  ReasonsGroupType reason;
62  reason.first = "GetSignatories";
63 
64  validator_.validateAccountId(reason, qry.accountId());
65 
66  return reason;
67  }
68 
70  const interface::GetAccountTransactions &qry) const {
71  ReasonsGroupType reason;
72  reason.first = "GetAccountTransactions";
73 
74  validator_.validateAccountId(reason, qry.accountId());
75  validator_.validateTxPaginationMeta(reason, qry.paginationMeta());
76 
77  return reason;
78  }
79 
81  const interface::GetAccountAssetTransactions &qry) const {
82  ReasonsGroupType reason;
83  reason.first = "GetAccountAssetTransactions";
84 
85  validator_.validateAccountId(reason, qry.accountId());
86  validator_.validateAssetId(reason, qry.assetId());
87  validator_.validateTxPaginationMeta(reason, qry.paginationMeta());
88 
89  return reason;
90  }
91 
93  ReasonsGroupType reason;
94  reason.first = "GetTransactions";
95 
96  const auto &hashes = qry.transactionHashes();
97  if (hashes.size() == 0) {
98  reason.second.push_back("tx_hashes cannot be empty");
99  }
100 
101  for (const auto &h : hashes) {
102  validator_.validateHash(reason, h);
103  }
104 
105  return reason;
106  }
107 
109  const interface::GetAccountAssets &qry) const {
110  ReasonsGroupType reason;
111  reason.first = "GetAccountAssets";
112 
113  validator_.validateAccountId(reason, qry.accountId());
114  return reason;
115  }
116 
118  const interface::GetAccountDetail &qry) const {
119  ReasonsGroupType reason;
120  reason.first = "GetAccountDetail";
121 
122  validator_.validateAccountId(reason, qry.accountId());
123 
124  return reason;
125  }
126 
128  ReasonsGroupType reason;
129  reason.first = "GetRoles";
130 
131  return reason;
132  }
133 
135  const interface::GetRolePermissions &qry) const {
136  ReasonsGroupType reason;
137  reason.first = "GetRolePermissions";
138 
139  validator_.validateRoleId(reason, qry.roleId());
140 
141  return reason;
142  }
143 
145  ReasonsGroupType reason;
146  reason.first = "GetAssetInfo";
147 
148  validator_.validateAssetId(reason, qry.assetId());
149 
150  return reason;
151  }
152 
154  const interface::GetPendingTransactions &qry) const {
155  ReasonsGroupType reason;
156  reason.first = "GetPendingTransactions";
157 
158  return reason;
159  }
160 
161  private:
162  FieldValidator validator_;
163  };
164 
170  template <typename FieldValidator, typename QueryFieldValidator>
171  class QueryValidator : public AbstractValidator<interface::Query> {
172  public:
173  QueryValidator(const FieldValidator &field_validator = FieldValidator(),
174  const QueryFieldValidator &query_field_validator =
175  QueryFieldValidator())
176  : field_validator_(field_validator),
177  query_field_validator_(query_field_validator) {}
178 
184  Answer validate(const interface::Query &qry) const override {
185  Answer answer;
186  std::string qry_reason_name = "Query";
187  ReasonsGroupType qry_reason(qry_reason_name, GroupedReasons());
188 
189  field_validator_.validateCreatorAccountId(qry_reason,
190  qry.creatorAccountId());
191  field_validator_.validateCreatedTime(qry_reason, qry.createdTime());
192  field_validator_.validateCounter(qry_reason, qry.queryCounter());
193 
194  if (not qry_reason.second.empty()) {
195  answer.addReason(std::move(qry_reason));
196  }
197 
198  auto field_reason =
199  boost::apply_visitor(query_field_validator_, qry.get());
200  if (not field_reason.second.empty()) {
201  answer.addReason(std::move(field_reason));
202  }
203 
204  return answer;
205  }
206 
207  protected:
210  QueryFieldValidator query_field_validator_;
211  };
212  } // namespace validation
213 } // namespace shared_model
214 
215 #endif // IROHA_SHARED_MODEL_QUERY_VALIDATOR_HPP
Definition: get_pending_transactions.hpp:19
QueryFieldValidator query_field_validator_
Definition: query_validator.hpp:210
ReasonsGroupType operator()(const interface::GetAccountAssetTransactions &qry) const
Definition: query_validator.hpp:80
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:144
Definition: get_transactions.hpp:14
void validateAccountId(ReasonsGroupType &reason, const interface::types::AccountIdType &account_id) const
Definition: field_validator.cpp:69
virtual const types::AccountIdType & assetId() const =0
ReasonsGroupType operator()(const interface::GetAccount &qry) const
Definition: query_validator.hpp:42
virtual const types::RoleIdType & roleId() const =0
void addReason(ReasonsGroupType &&reasons)
Definition: answer.hpp:64
Answer answer_
Definition: query_validator.hpp:208
Definition: abstract_validator.hpp:16
Definition: get_account_asset_transactions.hpp:21
string(REPLACE".""/"CMAKE_SWIG_OUTDIR ${SWIG_JAVA_PKG}) else() set(CMAKE_SWIG_FLAGS"") endif() myswig_add_library(irohajava LANGUAGE java SOURCES bindings.i) unset(CMAKE_SWIG_FLAGS) swig_link_libraries(irohajava $
Definition: CMakeLists.txt:54
QueryValidator(const FieldValidator &field_validator=FieldValidator(), const QueryFieldValidator &query_field_validator=QueryFieldValidator())
Definition: query_validator.hpp:173
virtual const types::AccountIdType & accountId() const =0
ReasonsGroupType operator()(const interface::GetAccountDetail &qry) const
Definition: query_validator.hpp:117
virtual const TransactionHashesType & transactionHashes() const =0
ReasonsGroupType operator()(const interface::GetPendingTransactions &qry) const
Definition: query_validator.hpp:153
void validateAssetId(ReasonsGroupType &reason, const interface::types::AssetIdType &asset_id) const
Definition: field_validator.cpp:82
ReasonsGroupType operator()(const interface::GetTransactions &qry) const
Definition: query_validator.hpp:92
virtual const TxPaginationMeta & paginationMeta() const =0
Get the query pagination metadata.
virtual const QueryVariantType & get() const =0
Definition: proto_query.cpp:91
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:184
void validateTxPaginationMeta(ReasonsGroupType &reason, const interface::TxPaginationMeta &tx_pagination_meta) const
Definition: field_validator.cpp:387
void validateRoleId(ReasonsGroupType &reason, const interface::types::RoleIdType &role_id) const
Definition: field_validator.cpp:135
Definition: get_roles.hpp:17
ReasonsGroupType operator()(const interface::GetBlock &qry) const
Definition: query_validator.hpp:51
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:108
virtual const types::AccountIdType & accountId() const =0
ReasonsGroupType operator()(const interface::GetRoles &qry) const
Definition: query_validator.hpp:127
void validateHash(ReasonsGroupType &reason, const crypto::Hash &hash) const
Definition: field_validator.cpp:368
ReasonsGroupType operator()(const interface::GetRolePermissions &qry) const
Definition: query_validator.hpp:134
Definition: query_validator.hpp:36
virtual const types::AccountIdType & accountId() const =0
Definition: field_validator.hpp:32
virtual types::CounterType queryCounter() const =0
Definition: proto_query.cpp:99
virtual const types::AccountIdType & creatorAccountId() const =0
Definition: proto_query.cpp:95
Definition: get_block.hpp:14
QueryValidatorVisitor(const FieldValidator &validator=FieldValidator())
Definition: query_validator.hpp:39
std::vector< ConcreteReasonType > GroupedReasons
Definition: answer.hpp:20
ReasonsGroupType operator()(const interface::GetSignatories &qry) const
Definition: query_validator.hpp:60
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:69
void validateHeight(ReasonsGroupType &reason, const interface::types::HeightType &height) const
Definition: field_validator.cpp:357
Definition: query_validator.hpp:171
FieldValidator field_validator_
Definition: query_validator.hpp:209
Definition: answer.hpp:28
Definition: get_account_assets.hpp:17