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 : namespace shared_model {
16 : namespace proto {
17 :
18 : template <typename Interface, typename Proto>
19 : class ProtoTransportFactory : public interface::AbstractTransportFactory<
20 : Interface,
21 : typename Proto::TransportType> {
22 : public:
23 : using typename interface::AbstractTransportFactory<
24 : Interface,
25 : typename Proto::TransportType>::Error;
26 : using ValidatorType = std::unique_ptr<
27 : shared_model::validation::AbstractValidator<Interface>>;
28 : using ProtoValidatorType =
29 : std::unique_ptr<shared_model::validation::AbstractValidator<
30 : typename Proto::TransportType>>;
31 :
32 : ProtoTransportFactory(ValidatorType interface_validator,
33 : ProtoValidatorType proto_validator)
34 519 : : interface_validator_(std::move(interface_validator)),
35 519 : proto_validator_{std::move(proto_validator)} {}
36 :
37 : iroha::expected::Result<std::unique_ptr<Interface>, Error> build(
38 : typename Proto::TransportType m) const override {
39 1578 : if (auto answer = proto_validator_->validate(m)) {
40 1 : return iroha::expected::makeError(Error{
41 1 : HashProvider::makeHash(makeBlob(m.payload())), answer.reason()});
42 : }
43 :
44 1576 : std::unique_ptr<Interface> result =
45 1576 : std::make_unique<Proto>(std::move(m));
46 1577 : if (auto answer = interface_validator_->validate(*result)) {
47 61 : return iroha::expected::makeError(
48 61 : Error{result->hash(), answer.reason()});
49 : }
50 :
51 1516 : return iroha::expected::makeValue(std::move(result));
52 1578 : }
53 :
54 : private:
55 : using HashProvider = shared_model::crypto::Sha3_256;
56 :
57 : ValidatorType interface_validator_;
58 : ProtoValidatorType proto_validator_;
59 : };
60 :
61 : } // namespace proto
62 : } // namespace shared_model
63 :
64 : #endif // IROHA_PROTO_TRANSPORT_FACTORY_HPP
|