Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #ifndef IROHA_CONTAINER_VALIDATOR_HPP
7 : #define IROHA_CONTAINER_VALIDATOR_HPP
8 :
9 : #include <boost/format.hpp>
10 : #include "datetime/time.hpp"
11 : #include "interfaces/common_objects/types.hpp"
12 : #include "interfaces/iroha_internal/block.hpp"
13 : #include "validators/answer.hpp"
14 :
15 : namespace shared_model {
16 : namespace validation {
17 :
18 : /**
19 : * Class that validates blocks and proposal common fields
20 : */
21 : template <typename Iface,
22 : typename FieldValidator,
23 : typename TransactionsCollectionValidator>
24 : class ContainerValidator {
25 : protected:
26 : void validateTransactions(
27 : ReasonsGroupType &reason,
28 : const interface::types::TransactionsCollectionType &transactions,
29 : interface::types::TimestampType current_timestamp) const {
30 265 : auto answer = transactions_collection_validator_.validate(
31 265 : transactions, current_timestamp);
32 265 : if (answer.hasErrors()) {
33 3 : reason.second.push_back(answer.reason());
34 3 : }
35 265 : }
36 :
37 : public:
38 : explicit ContainerValidator(
39 : const FieldValidator &field_validator = FieldValidator(),
40 : const TransactionsCollectionValidator
41 : &transactions_collection_validator =
42 : TransactionsCollectionValidator())
43 759 : : transactions_collection_validator_(
44 759 : transactions_collection_validator),
45 759 : field_validator_(field_validator) {}
46 :
47 : template <typename Validator>
48 : Answer validate(const Iface &cont,
49 : const std::string &reason_name,
50 : Validator &&validator) const {
51 265 : Answer answer;
52 265 : ReasonsGroupType reason;
53 265 : reason.first = reason_name;
54 265 : field_validator_.validateHeight(reason, cont.height());
55 265 : std::forward<Validator>(validator)(reason, cont);
56 :
57 265 : validateTransactions(reason, cont.transactions(), cont.createdTime());
58 265 : if (not reason.second.empty()) {
59 4 : answer.addReason(std::move(reason));
60 4 : }
61 265 : return answer;
62 265 : }
63 :
64 : Answer validate(const Iface &cont, const std::string &reason_name) const {
65 : return validate(cont, reason_name, [](auto &, const auto &) {});
66 : }
67 :
68 : private:
69 : TransactionsCollectionValidator transactions_collection_validator_;
70 :
71 : protected:
72 : FieldValidator field_validator_;
73 : };
74 :
75 : } // namespace validation
76 : } // namespace shared_model
77 :
78 : #endif // IROHA_CONTAINER_VALIDATOR_HPP
|