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 5416 : auto answer = std::forward<Validator>(validator)(model);
23 5416 : std::string reason_name = "Signature";
24 8592 : ReasonsGroupType reason(reason_name, GroupedReasons());
25 7476 : if (SignatureRequired or not model.signatures().empty()) {
26 8795 : field_validator_.validateSignatures(
27 8651 : reason, model.signatures(), model.payload());
28 7433 : }
29 8824 : if (not reason.second.empty()) {
30 11 : answer.addReason(std::move(reason));
31 11 : }
32 8796 : return answer;
33 8796 : }
34 :
35 : public:
36 : explicit SignableModelValidator(
37 : FieldValidator &&validator = FieldValidator())
38 1023 : : 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 1338 : return ModelValidator::validate(m, current_timestamp);
44 : });
45 : }
46 :
47 : Answer validate(const Model &model) const {
48 7380 : 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
|