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 1 : return createProposal(
32 1 : createProtoProposal(height, created_time, transactions));
33 0 : }
34 :
35 : // TODO mboldyrev 13.02.2019 IR-323
36 : // make it return std::shared_ptr<const interface::Proposal>
37 : std::unique_ptr<interface::Proposal> unsafeCreateProposal(
38 : interface::types::HeightType height,
39 : interface::types::TimestampType created_time,
40 : UnsafeTransactionsCollectionType transactions) override {
41 3498 : return std::make_unique<Proposal>(
42 3498 : createProtoProposal(height, created_time, transactions));
43 0 : }
44 :
45 : /**
46 : * Create and validate proposal using protobuf object
47 : */
48 : FactoryResult<std::unique_ptr<interface::Proposal>> createProposal(
49 : const iroha::protocol::Proposal &proposal) {
50 1 : return validate(std::make_unique<Proposal>(proposal));
51 0 : }
52 :
53 : private:
54 : iroha::protocol::Proposal createProtoProposal(
55 : interface::types::HeightType height,
56 : interface::types::TimestampType created_time,
57 : UnsafeTransactionsCollectionType transactions) {
58 3499 : iroha::protocol::Proposal proposal;
59 :
60 3499 : proposal.set_height(height);
61 3499 : proposal.set_created_time(created_time);
62 :
63 6390 : for (const auto &tx : transactions) {
64 2891 : *proposal.add_transactions() =
65 2891 : static_cast<const shared_model::proto::Transaction &>(tx)
66 2891 : .getTransport();
67 : }
68 :
69 3499 : return proposal;
70 3499 : }
71 :
72 : FactoryResult<std::unique_ptr<interface::Proposal>> validate(
73 : std::unique_ptr<Proposal> proposal) {
74 1 : auto errors = validator_.validate(*proposal);
75 :
76 1 : if (errors) {
77 1 : return iroha::expected::makeError(errors.reason());
78 : }
79 :
80 1 : return iroha::expected::makeValue<std::unique_ptr<interface::Proposal>>(
81 1 : std::move(proposal));
82 1 : }
83 :
84 : Validator validator_;
85 : };
86 : } // namespace proto
87 : } // namespace shared_model
88 :
89 : #endif // IROHA_PROTO_PROPOSAL_FACTORY_HPP
|