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