hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
proto_transport_factory.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_PROTO_TRANSPORT_FACTORY_HPP
7 #define IROHA_PROTO_TRANSPORT_FACTORY_HPP
8 
10 
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>
25  Interface,
26  typename Proto::TransportType> {
27  public:
29  Interface,
30  typename Proto::TransportType>::Error;
31  using ValidatorType = std::unique_ptr<
33  using ProtoValidatorType =
35  typename Proto::TransportType>>;
36 
37  ProtoTransportFactory(ValidatorType interface_validator,
38  ProtoValidatorType proto_validator)
39  : interface_validator_(std::move(interface_validator)),
40  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  if (auto answer = proto_validator_->validate(m)) {
45  auto payload_field_descriptor =
46  m.GetDescriptor()->FindFieldByLowercaseName("payload");
48  if (payload_field_descriptor) {
49  const auto &payload =
50  m.GetReflection()->GetMessage(m, payload_field_descriptor);
51  hash = HashProvider::makeHash(makeBlob(payload));
52  }
53  return iroha::expected::makeError(Error{hash, answer.reason()});
54  }
55 
56  std::unique_ptr<Interface> result =
57  std::make_unique<Proto>(std::move(m));
58  if (auto answer = interface_validator_->validate(*result)) {
59  return iroha::expected::makeError(
60  Error{result->hash(), answer.reason()});
61  }
62 
63  return iroha::expected::makeValue(std::move(result));
64  }
65 
66  private:
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
Definition: hash.hpp:18
Definition: sha3_256.hpp:15
Definition: abstract_validator.hpp:16
Definition: peer.hpp:48
ProtoTransportFactory(ValidatorType interface_validator, ProtoValidatorType proto_validator)
Definition: proto_transport_factory.hpp:37
crypto::Blob makeBlob(T &&message)
Definition: util.hpp:17
hash256_t hash(const T &pb)
Definition: pb_common.hpp:43
iroha::expected::Result< std::unique_ptr< Interface >, Error > build(typename Proto::TransportType m) const override
Definition: proto_transport_factory.hpp:42
static Hash makeHash(const Blob &blob)
Definition: sha3_256.hpp:17
Definition: proto_transport_factory.hpp:24
Definition: command_executor.hpp:12
std::unique_ptr< shared_model::validation::AbstractValidator< Interface >> ValidatorType
Definition: proto_transport_factory.hpp:32
std::shared_ptr< shared_model::validation::AbstractValidator< typename Proto::TransportType >> ProtoValidatorType
Definition: proto_transport_factory.hpp:35