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_PROTO_TRANSPORT_FACTORY_HPP
7 : #define IROHA_PROTO_TRANSPORT_FACTORY_HPP
8 :
9 : #include "interfaces/iroha_internal/abstract_transport_factory.hpp"
10 :
11 : #include "backend/protobuf/util.hpp"
12 : #include "cryptography/hash_providers/sha3_256.hpp"
13 : #include "validators/abstract_validator.hpp"
14 :
15 : #ifdef _MSC_VER
16 : #pragma push_macro("GetMessage")
17 : #undef GetMessage
18 : #endif
19 :
20 : namespace shared_model {
21 : namespace proto {
22 :
23 : template <typename Interface, typename Proto>
24 : class ProtoTransportFactory : public interface::AbstractTransportFactory<
25 : Interface,
26 : typename Proto::TransportType> {
27 : public:
28 : using typename interface::AbstractTransportFactory<
29 : Interface,
30 : typename Proto::TransportType>::Error;
31 : using ValidatorType = std::unique_ptr<
32 : shared_model::validation::AbstractValidator<Interface>>;
33 : using ProtoValidatorType =
34 : std::shared_ptr<shared_model::validation::AbstractValidator<
35 : typename Proto::TransportType>>;
36 :
37 : ProtoTransportFactory(ValidatorType interface_validator,
38 : ProtoValidatorType proto_validator)
39 771 : : interface_validator_(std::move(interface_validator)),
40 771 : proto_validator_{std::move(proto_validator)} {}
41 :
42 : iroha::expected::Result<std::unique_ptr<Interface>, Error> build(
43 : typename Proto::TransportType m) const override {
44 8739 : if (auto answer = proto_validator_->validate(m)) {
45 1 : auto payload_field_descriptor =
46 1 : m.GetDescriptor()->FindFieldByLowercaseName("payload");
47 1 : shared_model::crypto::Hash hash;
48 1 : if (payload_field_descriptor) {
49 1 : const auto &payload =
50 1 : m.GetReflection()->GetMessage(m, payload_field_descriptor);
51 1 : hash = HashProvider::makeHash(makeBlob(payload));
52 1 : }
53 1 : return iroha::expected::makeError(Error{hash, answer.reason()});
54 1 : }
55 :
56 4496 : std::unique_ptr<Interface> result =
57 4496 : std::make_unique<Proto>(std::move(m));
58 8764 : if (auto answer = interface_validator_->validate(*result)) {
59 81 : return iroha::expected::makeError(
60 81 : Error{result->hash(), answer.reason()});
61 : }
62 :
63 8791 : return iroha::expected::makeValue(std::move(result));
64 8775 : }
65 :
66 : private:
67 : using HashProvider = shared_model::crypto::Sha3_256;
68 :
69 : ValidatorType interface_validator_;
70 : ProtoValidatorType proto_validator_;
71 : };
72 :
73 : } // namespace proto
74 : } // namespace shared_model
75 :
76 : #ifdef _MSC_VER
77 : #pragma pop_macro("GetMessage")
78 : #endif
79 :
80 : #endif // IROHA_PROTO_TRANSPORT_FACTORY_HPP
|