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  // TODO: 2019-03-21 @muratovv refactor with template parameter
52  // IR-422
53  hash = HashProvider::makeHash(makeBlob(payload));
54  }
55  return iroha::expected::makeError(Error{hash, answer.reason()});
56  }
57 
58  std::unique_ptr<Interface> result =
59  std::make_unique<Proto>(std::move(m));
60  if (auto answer = interface_validator_->validate(*result)) {
61  return iroha::expected::makeError(
62  Error{result->hash(), answer.reason()});
63  }
64 
65  return iroha::expected::makeValue(std::move(result));
66  }
67 
68  private:
70 
71  ValidatorType interface_validator_;
72  ProtoValidatorType proto_validator_;
73  };
74 
75  } // namespace proto
76 } // namespace shared_model
77 
78 #ifdef _MSC_VER
79 #pragma pop_macro("GetMessage")
80 #endif
81 
82 #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