Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. 2018 All Rights Reserved.
3 : * http://soramitsu.co.jp
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #include "main/impl/consensus_init.hpp"
19 : #include "consensus/yac/impl/peer_orderer_impl.hpp"
20 : #include "consensus/yac/impl/timer_impl.hpp"
21 : #include "consensus/yac/impl/yac_crypto_provider_impl.hpp"
22 : #include "consensus/yac/impl/yac_gate_impl.hpp"
23 : #include "consensus/yac/impl/yac_hash_provider_impl.hpp"
24 : #include "consensus/yac/storage/yac_proposal_storage.hpp"
25 : #include "consensus/yac/transport/impl/network_impl.hpp"
26 :
27 : namespace iroha {
28 : namespace consensus {
29 : namespace yac {
30 :
31 : auto YacInit::createPeerOrderer(
32 : std::shared_ptr<ametsuchi::PeerQueryFactory> peer_query_factory) {
33 245 : return std::make_shared<PeerOrdererImpl>(peer_query_factory);
34 : }
35 :
36 : auto YacInit::createNetwork(
37 : std::shared_ptr<
38 : iroha::network::AsyncGrpcClient<google::protobuf::Empty>>
39 : async_call) {
40 245 : consensus_network = std::make_shared<NetworkImpl>(async_call);
41 245 : return consensus_network;
42 : }
43 :
44 : auto YacInit::createCryptoProvider(
45 : const shared_model::crypto::Keypair &keypair,
46 : std::shared_ptr<shared_model::interface::CommonObjectsFactory>
47 : common_objects_factory) {
48 245 : auto crypto = std::make_shared<CryptoProviderImpl>(
49 245 : keypair, std::move(common_objects_factory));
50 :
51 245 : return crypto;
52 245 : }
53 :
54 : auto YacInit::createTimer(std::chrono::milliseconds delay_milliseconds) {
55 : return std::make_shared<TimerImpl>([delay_milliseconds] {
56 : // static factory with a single thread
57 : //
58 : // observe_on_new_thread -- coordination which creates new thread with
59 : // observe_on strategy -- all subsequent operations will be performed
60 : // on this thread.
61 : //
62 : // scheduler owns a timeline that is exposed by the now() method.
63 : // scheduler is also a factory for workers in that timeline.
64 : //
65 : // coordination is a factory for coordinators and has a scheduler.
66 : //
67 : // coordinator has a worker, and is a factory for coordinated
68 : // observables, subscribers and schedulable functions.
69 : //
70 : // A new thread scheduler is created
71 : // by calling .create_coordinator().get_scheduler()
72 : //
73 : // static allows to reuse the same thread in subsequent calls to this
74 : // lambda
75 2 : static rxcpp::observe_on_one_worker coordination(
76 1 : rxcpp::observe_on_new_thread()
77 1 : .create_coordinator()
78 1 : .get_scheduler());
79 2 : return rxcpp::observable<>::timer(
80 2 : std::chrono::milliseconds(delay_milliseconds), coordination);
81 0 : });
82 : }
83 :
84 : auto YacInit::createHashProvider() {
85 245 : return std::make_shared<YacHashProviderImpl>();
86 : }
87 :
88 : std::shared_ptr<consensus::yac::Yac> YacInit::createYac(
89 : ClusterOrdering initial_order,
90 : const shared_model::crypto::Keypair &keypair,
91 : std::chrono::milliseconds delay_milliseconds,
92 : std::shared_ptr<
93 : iroha::network::AsyncGrpcClient<google::protobuf::Empty>>
94 : async_call,
95 : std::shared_ptr<shared_model::interface::CommonObjectsFactory>
96 : common_objects_factory) {
97 245 : return Yac::create(
98 245 : YacVoteStorage(),
99 245 : createNetwork(std::move(async_call)),
100 245 : createCryptoProvider(keypair, std::move(common_objects_factory)),
101 245 : createTimer(delay_milliseconds),
102 : initial_order);
103 0 : }
104 :
105 : std::shared_ptr<YacGate> YacInit::initConsensusGate(
106 : std::shared_ptr<ametsuchi::PeerQueryFactory> peer_query_factory,
107 : std::shared_ptr<simulator::BlockCreator> block_creator,
108 : std::shared_ptr<network::BlockLoader> block_loader,
109 : const shared_model::crypto::Keypair &keypair,
110 : std::shared_ptr<consensus::ConsensusResultCache>
111 : consensus_result_cache,
112 : std::chrono::milliseconds vote_delay_milliseconds,
113 : std::shared_ptr<
114 : iroha::network::AsyncGrpcClient<google::protobuf::Empty>>
115 : async_call,
116 : std::shared_ptr<shared_model::interface::CommonObjectsFactory>
117 : common_objects_factory) {
118 245 : auto peer_orderer = createPeerOrderer(peer_query_factory);
119 :
120 245 : auto yac = createYac(peer_orderer->getInitialOrdering().value(),
121 245 : keypair,
122 245 : vote_delay_milliseconds,
123 245 : std::move(async_call),
124 245 : std::move(common_objects_factory));
125 245 : consensus_network->subscribe(yac);
126 :
127 245 : auto hash_provider = createHashProvider();
128 245 : return std::make_shared<YacGateImpl>(std::move(yac),
129 245 : std::move(peer_orderer),
130 : hash_provider,
131 : block_creator,
132 : block_loader,
133 245 : std::move(consensus_result_cache));
134 245 : }
135 : } // namespace yac
136 : } // namespace consensus
137 : } // namespace iroha
|