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"
29 
30 namespace shared_model {
31  namespace validation {
32 
37  template <typename FieldValidator>
39  : public boost::static_visitor<std::optional<ValidationError>> {
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 
49  std::optional<ValidationError> operator()(
50  const interface::GetAccount &get_account) const {
51  return aggregateErrors(
52  "GetAccount",
53  {},
54  {validator_.validateAccountId(get_account.accountId())});
55  }
56 
57  std::optional<ValidationError> operator()(
58  const interface::GetBlock &get_block) const {
59  return aggregateErrors(
60  "GetBlock", {}, {validator_.validateHeight(get_block.height())});
61  }
62 
63  std::optional<ValidationError> operator()(
64  const interface::GetSignatories &get_signatories) const {
65  return aggregateErrors(
66  "GetSignatories",
67  {},
68  {validator_.validateAccountId(get_signatories.accountId())});
69  }
70 
71  std::optional<ValidationError> operator()(
72  const interface::GetAccountTransactions &get_account_transactions)
73  const {
74  return aggregateErrors(
75  "GetAccountTransactions",
76  {},
77  {validator_.validateAccountId(get_account_transactions.accountId()),
78  validator_.validateTxPaginationMeta(
79  get_account_transactions.paginationMeta())});
80  }
81 
82  std::optional<ValidationError> operator()(
84  &get_account_asset_transactions) const {
85  return aggregateErrors(
86  "GetAccountAssetTransactions",
87  {},
88  {validator_.validateAccountId(
89  get_account_asset_transactions.accountId()),
90  validator_.validateAssetId(
91  get_account_asset_transactions.assetId()),
92  validator_.validateTxPaginationMeta(
93  get_account_asset_transactions.paginationMeta())});
94  }
95 
96  std::optional<ValidationError> operator()(
97  const interface::GetTransactions &get_transactions) const {
98  ValidationErrorCreator error_creator;
99 
100  const auto &hashes = get_transactions.transactionHashes();
101  if (hashes.size() == 0) {
102  error_creator.addReason("tx_hashes cannot be empty");
103  }
104 
105  for (const auto &h : hashes) {
106  error_creator |= validator_.validateHash(h);
107  }
108 
109  return std::move(error_creator).getValidationError("GetTransactions");
110  }
111 
112  std::optional<ValidationError> operator()(
113  const interface::GetAccountAssets &get_account_assets) const {
114  using iroha::operator|;
115  return aggregateErrors(
116  "GetAccountAssets",
117  {},
118  {validator_.validateAccountId(get_account_assets.accountId()),
119  get_account_assets.paginationMeta() |
120  [this](const auto &pagination_meta) {
121  return validator_.validateAssetPaginationMeta(
122  pagination_meta);
123  }});
124  }
125 
126  std::optional<ValidationError> operator()(
127  const interface::GetAccountDetail &get_account_detail) const {
128  using iroha::operator|;
129  return aggregateErrors(
130  "GetAccountDetail",
131  {},
132  {validator_.validateAccountId(get_account_detail.accountId()),
133  get_account_detail.key() |
134  [this](const auto &key) {
135  return validator_.validateAccountDetailKey(key);
136  },
137  get_account_detail.writer() |
138  [this](const auto &writer) {
139  return validator_.validateAccountId(writer);
140  },
141  get_account_detail.paginationMeta() |
142  [this](const auto &pagination_meta) {
143  return validator_.validateAccountDetailPaginationMeta(
144  pagination_meta);
145  }});
146  }
147 
148  std::optional<ValidationError> operator()(
149  const interface::GetRoles &get_roles) const {
150  return std::nullopt;
151  }
152 
153  std::optional<ValidationError> operator()(
154  const interface::GetRolePermissions &get_role_permissions) const {
155  return aggregateErrors(
156  "GetRolePermissions",
157  {},
158  {validator_.validateRoleId(get_role_permissions.roleId())});
159  }
160 
161  std::optional<ValidationError> operator()(
162  const interface::GetAssetInfo &get_asset_info) const {
163  return aggregateErrors(
164  "GetAssetInfo",
165  {},
166  {validator_.validateAssetId(get_asset_info.assetId())});
167  }
168 
169  std::optional<ValidationError> operator()(
170  const interface::GetPendingTransactions &get_pending_transactions)
171  const {
172  using iroha::operator|;
173  return aggregateErrors(
174  "GetPendingTransactions",
175  {},
176  {// TODO igor-egorov 2019-06-06 IR-516 Make meta non-optional
177  get_pending_transactions.paginationMeta() |
178  [this](const auto &pagination_meta) {
179  return validator_.validateTxPaginationMeta(pagination_meta);
180  }});
181  }
182 
183  std::optional<ValidationError> operator()(
184  const interface::GetPeers &get_peers) const {
185  return std::nullopt;
186  }
187 
188  private:
189  FieldValidator validator_;
190  };
191 
197  template <typename FieldValidator, typename QueryFieldValidator>
198  class QueryValidator : public AbstractValidator<interface::Query> {
199  QueryValidator(const FieldValidator &field_validator,
200  const QueryFieldValidator &query_field_validator)
201  : field_validator_(field_validator),
202  query_field_validator_(query_field_validator) {}
203 
204  public:
205  QueryValidator(std::shared_ptr<ValidatorsConfig> config)
206  : QueryValidator(FieldValidator{config},
207  QueryFieldValidator{config}) {}
208 
214  std::optional<ValidationError> validate(
215  const interface::Query &qry) const override {
216  ValidationErrorCreator error_creator;
217 
218  error_creator |=
219  field_validator_.validateCreatorAccountId(qry.creatorAccountId());
220  error_creator |=
221  field_validator_.validateCreatedTime(qry.createdTime());
222  error_creator |= field_validator_.validateCounter(qry.queryCounter());
223  error_creator |=
224  boost::apply_visitor(query_field_validator_, qry.get());
225 
226  return std::move(error_creator).getValidationError("Query");
227  }
228 
229  protected:
231  QueryFieldValidator query_field_validator_;
232  };
233  } // namespace validation
234 } // namespace shared_model
235 
236 #endif // IROHA_SHARED_MODEL_QUERY_VALIDATOR_HPP
std::optional< ValidationError > validateHash(const crypto::Hash &hash) const
Definition: field_validator.cpp:370
Definition: get_pending_transactions.hpp:21
QueryFieldValidator query_field_validator_
Definition: query_validator.hpp:231
virtual const types::AccountIdType & creatorAccountId() const =0
std::optional< ValidationError > operator()(const interface::GetAccountAssetTransactions &get_account_asset_transactions) const
Definition: query_validator.hpp:82
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
std::optional< ValidationError > validateHeight(const interface::types::HeightType &height) const
Definition: field_validator.cpp:360
virtual const types::AccountIdType & accountId() const =0
Definition: query.hpp:35
Definition: get_transactions.hpp:14
virtual const types::AccountIdType & assetId() const =0
virtual std::optional< types::AccountIdType > writer() const =0
virtual const types::RoleIdType & roleId() const =0
std::optional< ValidationError > operator()(const interface::GetRolePermissions &get_role_permissions) const
Definition: query_validator.hpp:153
std::optional< ValidationError > operator()(const interface::GetTransactions &get_transactions) const
Definition: query_validator.hpp:96
Definition: abstract_validator.hpp:17
std::optional< ValidationError > operator()(const interface::GetAccount &get_account) const
Definition: query_validator.hpp:49
std::optional< ValidationError > operator()(const interface::GetAssetInfo &get_asset_info) const
Definition: query_validator.hpp:161
std::optional< ValidationError > operator()(const interface::GetPeers &get_peers) const
Definition: query_validator.hpp:183
std::optional< ValidationError > validate(const interface::Query &qry) const override
Definition: query_validator.hpp:214
std::optional< ValidationError > operator()(const interface::GetAccountTransactions &get_account_transactions) const
Definition: query_validator.hpp:71
Definition: get_account_asset_transactions.hpp:21
virtual const types::AccountIdType & accountId() const =0
virtual const TransactionHashesType & transactionHashes() const =0
std::optional< ValidationError > operator()(const interface::GetPendingTransactions &get_pending_transactions) const
Definition: query_validator.hpp:169
virtual const QueryVariantType & get() const =0
std::optional< ValidationError > validateAccountDetailPaginationMeta(const interface::AccountDetailPaginationMeta &pagination_meta) const
Definition: field_validator.cpp:458
std::optional< ValidationError > validateAccountDetailKey(const interface::types::AccountDetailKeyType &key) const
Definition: field_validator.cpp:191
virtual std::optional< std::reference_wrapper< const AccountDetailPaginationMeta > > paginationMeta() const =0
Get the query pagination metadata.
std::optional< ValidationError > aggregateErrors(const ReasonName &name, std::vector< std::optional< ReasonType >> optional_reasons, std::vector< std::optional< ValidationError >> optional_child_errors)
Definition: validation_error_helpers.cpp:57
QueryValidator(std::shared_ptr< ValidatorsConfig > config)
Definition: query_validator.hpp:205
std::optional< ValidationError > operator()(const interface::GetRoles &get_roles) const
Definition: query_validator.hpp:148
virtual std::optional< types::AccountDetailKeyType > key() const =0
virtual const TxPaginationMeta & paginationMeta() const =0
Get the query pagination metadata.
QueryValidatorVisitor(std::shared_ptr< ValidatorsConfig > config)
Definition: query_validator.hpp:46
virtual const types::AssetIdType & assetId() const =0
Definition: get_signatories.hpp:18
std::optional< ValidationError > validateAccountId(const interface::types::AccountIdType &account_id) const
Definition: field_validator.cpp:126
virtual types::TimestampType createdTime() const =0
Definition: get_account_transactions.hpp:21
ValidationErrorCreator & addReason(ReasonType reason)
Add a reason to error.
Definition: validation_error_helpers.cpp:23
virtual types::CounterType queryCounter() const =0
std::optional< ValidationError > getValidationError(const ReasonName &name) &&
Definition: validation_error_helpers.cpp:15
Definition: get_roles.hpp:17
Definition: get_peers.hpp:17
Definition: get_account_detail.hpp:29
std::optional< ValidationError > validateAssetPaginationMeta(const interface::AssetPaginationMeta &asset_pagination_meta) const
Definition: field_validator.cpp:435
Definition: get_account.hpp:14
virtual const types::AccountIdType & accountId() const =0
virtual const types::AccountIdType & accountId() const =0
virtual std::optional< std::reference_wrapper< const interface::AssetPaginationMeta > > paginationMeta() const =0
Get the query pagination metadata.
Definition: query_validator.hpp:38
virtual const types::AccountIdType & accountId() const =0
Definition: field_validator.hpp:40
std::optional< ValidationError > operator()(const interface::GetSignatories &get_signatories) const
Definition: query_validator.hpp:63
Definition: get_block.hpp:14
virtual std::optional< std::reference_wrapper< const TxPaginationMeta > > paginationMeta() const =0
Helper class for ValidationError creation.
Definition: validation_error_helpers.hpp:16
Definition: get_role_permissions.hpp:18
Definition: command_executor.hpp:12
std::optional< ValidationError > validateRoleId(const interface::types::RoleIdType &role_id) const
Definition: field_validator.cpp:163
Definition: query_validator.hpp:198
std::optional< ValidationError > validateTxPaginationMeta(const interface::TxPaginationMeta &tx_pagination_meta) const
Definition: field_validator.cpp:405
FieldValidator field_validator_
Definition: query_validator.hpp:230
std::optional< ValidationError > validateAssetId(const interface::types::AssetIdType &asset_id) const
Definition: field_validator.cpp:131
std::optional< ValidationError > operator()(const interface::GetBlock &get_block) const
Definition: query_validator.hpp:57
std::optional< ValidationError > operator()(const interface::GetAccountDetail &get_account_detail) const
Definition: query_validator.hpp:126
std::optional< ValidationError > operator()(const interface::GetAccountAssets &get_account_assets) const
Definition: query_validator.hpp:112
Definition: get_account_assets.hpp:20