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_SHARED_MODEL_SIGNABLE_VALIDATOR_HPP
7 : #define IROHA_SHARED_MODEL_SIGNABLE_VALIDATOR_HPP
8 :
9 : #include "validators/answer.hpp"
10 :
11 : namespace shared_model {
12 : namespace validation {
13 :
14 : template <typename ModelValidator,
15 : typename Model,
16 : typename FieldValidator,
17 : bool SignatureRequired = true>
18 : class SignableModelValidator : public ModelValidator {
19 : private:
20 : template <typename Validator>
21 : Answer validateImpl(const Model &model, Validator &&validator) const {
22 1494 : auto answer = std::forward<Validator>(validator)(model);
23 1494 : std::string reason_name = "Signature";
24 1494 : ReasonsGroupType reason(reason_name, GroupedReasons());
25 0 : if (SignatureRequired or not model.signatures().empty()) {
26 1494 : field_validator_.validateSignatures(
27 1494 : reason, model.signatures(), model.payload());
28 0 : }
29 1494 : if (not reason.second.empty()) {
30 2 : answer.addReason(std::move(reason));
31 2 : }
32 1494 : return answer;
33 1494 : }
34 :
35 : public:
36 : explicit SignableModelValidator(
37 : FieldValidator &&validator = FieldValidator())
38 46 : : ModelValidator(validator), field_validator_(std::move(validator)) {}
39 :
40 : Answer validate(const Model &model,
41 : interface::types::TimestampType current_timestamp) const {
42 : return validateImpl(model, [&, current_timestamp](const Model &m) {
43 1465 : return ModelValidator::validate(m, current_timestamp);
44 : });
45 : }
46 :
47 : Answer validate(const Model &model) const {
48 29 : return validateImpl(
49 : model, [&](const Model &m) { return ModelValidator::validate(m); });
50 : }
51 :
52 : private:
53 : FieldValidator field_validator_;
54 : };
55 : } // namespace validation
56 : } // namespace shared_model
57 : #endif // IROHA_SHARED_MODEL_SIGNABLE_VALIDATOR_HPP
|