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_PROPOSAL_FACTORY_HPP
7 : #define IROHA_PROTO_PROPOSAL_FACTORY_HPP
8 :
9 : #include "interfaces/iroha_internal/proposal_factory.hpp"
10 : #include "interfaces/iroha_internal/unsafe_proposal_factory.hpp"
11 :
12 : #include "backend/protobuf/proposal.hpp"
13 : #include "backend/protobuf/transaction.hpp"
14 : #include "proposal.pb.h"
15 :
16 : namespace shared_model {
17 : namespace proto {
18 : template <typename Validator>
19 : class ProtoProposalFactory : public interface::ProposalFactory,
20 : public interface::UnsafeProposalFactory {
21 : public:
22 : using TransactionsCollectionType =
23 : interface::ProposalFactory::TransactionsCollectionType;
24 : using UnsafeTransactionsCollectionType =
25 : interface::UnsafeProposalFactory::TransactionsCollectionType;
26 :
27 : FactoryResult<std::unique_ptr<interface::Proposal>> createProposal(
28 : interface::types::HeightType height,
29 : interface::types::TimestampType created_time,
30 : TransactionsCollectionType transactions) override {
31 2007 : return createProposal(
32 2007 : createProtoProposal(height, created_time, transactions));
33 0 : }
34 :
35 : std::unique_ptr<interface::Proposal> unsafeCreateProposal(
36 : interface::types::HeightType height,
37 : interface::types::TimestampType created_time,
38 : UnsafeTransactionsCollectionType transactions) override {
39 27 : return std::make_unique<Proposal>(
40 27 : createProtoProposal(height, created_time, transactions));
41 0 : }
42 :
43 : /**
44 : * Create and validate proposal using protobuf object
45 : */
46 : FactoryResult<std::unique_ptr<interface::Proposal>> createProposal(
47 : const iroha::protocol::Proposal &proposal) {
48 2008 : return validate(std::make_unique<Proposal>(proposal));
49 0 : }
50 :
51 : private:
52 : iroha::protocol::Proposal createProtoProposal(
53 : interface::types::HeightType height,
54 : interface::types::TimestampType created_time,
55 : UnsafeTransactionsCollectionType transactions) {
56 2034 : iroha::protocol::Proposal proposal;
57 :
58 2034 : proposal.set_height(height);
59 2034 : proposal.set_created_time(created_time);
60 :
61 4125 : for (const auto &tx : transactions) {
62 2091 : *proposal.add_transactions() =
63 2091 : static_cast<const shared_model::proto::Transaction &>(tx)
64 2091 : .getTransport();
65 : }
66 :
67 2034 : return proposal;
68 2034 : }
69 :
70 : FactoryResult<std::unique_ptr<interface::Proposal>> validate(
71 : std::unique_ptr<Proposal> proposal) {
72 2008 : auto errors = validator_.validate(*proposal);
73 :
74 2008 : if (errors) {
75 1 : return iroha::expected::makeError(errors.reason());
76 : }
77 :
78 2008 : return iroha::expected::makeValue<std::unique_ptr<interface::Proposal>>(
79 2008 : std::move(proposal));
80 2008 : }
81 :
82 : Validator validator_;
83 : };
84 : } // namespace proto
85 : } // namespace shared_model
86 :
87 : #endif // IROHA_PROTO_PROPOSAL_FACTORY_HPP
|