hyperledger/iroha
Iroha - A simple, decentralized ledger http://iroha.tech
proto_proposal_factory.hpp
Go to the documentation of this file.
1 
6 #ifndef IROHA_PROTO_PROPOSAL_FACTORY_HPP
7 #define IROHA_PROTO_PROPOSAL_FACTORY_HPP
8 
12 
15 #include "proposal.pb.h"
16 
17 namespace shared_model {
18  namespace proto {
19  template <typename Validator>
22  public:
27 
28  ProtoProposalFactory(std::shared_ptr<validation::ValidatorsConfig> config)
29  : validator_{config} {}
30 
34  TransactionsCollectionType transactions) override {
35  return createProposal(
36  createProtoProposal(height, created_time, transactions));
37  }
38 
39  // TODO mboldyrev 13.02.2019 IR-323
40  // make it return std::shared_ptr<const interface::Proposal>
41  std::unique_ptr<interface::Proposal> unsafeCreateProposal(
44  UnsafeTransactionsCollectionType transactions) override {
45  return std::make_unique<Proposal>(
46  createProtoProposal(height, created_time, transactions));
47  }
48 
53  const iroha::protocol::Proposal &proposal) {
54  return validate(std::make_unique<Proposal>(proposal));
55  }
56 
57  private:
58  iroha::protocol::Proposal createProtoProposal(
61  UnsafeTransactionsCollectionType transactions) {
62  iroha::protocol::Proposal proposal;
63 
64  proposal.set_height(height);
65  proposal.set_created_time(created_time);
66 
67  for (const auto &tx : transactions) {
68  *proposal.add_transactions() =
69  static_cast<const shared_model::proto::Transaction &>(tx)
70  .getTransport();
71  }
72 
73  return proposal;
74  }
75 
77  std::unique_ptr<Proposal> proposal) {
78  auto errors = validator_.validate(*proposal);
79 
80  if (errors) {
81  return iroha::expected::makeError(errors.reason());
82  }
83 
84  return iroha::expected::makeValue<std::unique_ptr<interface::Proposal>>(
85  std::move(proposal));
86  }
87 
88  Validator validator_;
89  };
90  } // namespace proto
91 } // namespace shared_model
92 
93 #endif // IROHA_PROTO_PROPOSAL_FACTORY_HPP
uint64_t HeightType
Type of height (for Block, Proposal etc)
Definition: types.hpp:39
Definition: unsafe_proposal_factory.hpp:22
Definition: transaction.hpp:14
boost::any_range< Transaction, boost::forward_traversal_tag, const Transaction & > TransactionsCollectionType
Definition: unsafe_proposal_factory.hpp:27
std::unique_ptr< interface::Proposal > unsafeCreateProposal(interface::types::HeightType height, interface::types::TimestampType created_time, UnsafeTransactionsCollectionType transactions) override
Definition: proto_proposal_factory.hpp:41
Definition: proto_proposal_factory.hpp:20
interface::UnsafeProposalFactory::TransactionsCollectionType UnsafeTransactionsCollectionType
Definition: proto_proposal_factory.hpp:26
FactoryResult< std::unique_ptr< interface::Proposal > > createProposal(interface::types::HeightType height, interface::types::TimestampType created_time, TransactionsCollectionType transactions) override
Definition: proto_proposal_factory.hpp:31
boost::any_range< Transaction, boost::forward_traversal_tag, const Transaction & > TransactionsCollectionType
Definition: proposal_factory.hpp:30
Definition: proposal_factory.hpp:22
ProtoProposalFactory(std::shared_ptr< validation::ValidatorsConfig > config)
Definition: proto_proposal_factory.hpp:28
FactoryResult< std::unique_ptr< interface::Proposal > > createProposal(const iroha::protocol::Proposal &proposal)
Definition: proto_proposal_factory.hpp:52
Definition: command_executor.hpp:12
interface::ProposalFactory::TransactionsCollectionType TransactionsCollectionType
Definition: proto_proposal_factory.hpp:24
iroha::expected::Result< T, std::string > FactoryResult
Definition: proposal_factory.hpp:25
uint64_t TimestampType
Type of timestamp.
Definition: types.hpp:59