Line data Source code
1 : /**
2 : * Copyright Soramitsu Co., Ltd. All Rights Reserved.
3 : * SPDX-License-Identifier: Apache-2.0
4 : */
5 :
6 : #include "main/impl/consensus_init.hpp"
7 :
8 : #include "consensus/yac/consistency_model.hpp"
9 : #include "consensus/yac/impl/peer_orderer_impl.hpp"
10 : #include "consensus/yac/impl/timer_impl.hpp"
11 : #include "consensus/yac/impl/yac_crypto_provider_impl.hpp"
12 : #include "consensus/yac/impl/yac_gate_impl.hpp"
13 : #include "consensus/yac/impl/yac_hash_provider_impl.hpp"
14 : #include "consensus/yac/storage/buffered_cleanup_strategy.hpp"
15 : #include "consensus/yac/storage/yac_proposal_storage.hpp"
16 : #include "consensus/yac/transport/impl/network_impl.hpp"
17 : #include "logger/logger_manager.hpp"
18 :
19 : using namespace iroha::consensus::yac;
20 :
21 : namespace {
22 : auto createPeerOrderer(
23 : std::shared_ptr<iroha::ametsuchi::PeerQueryFactory> peer_query_factory) {
24 247 : return std::make_shared<PeerOrdererImpl>(peer_query_factory);
25 : }
26 :
27 : auto createCryptoProvider(
28 : const shared_model::crypto::Keypair &keypair,
29 : std::shared_ptr<shared_model::interface::CommonObjectsFactory>
30 : common_objects_factory) {
31 247 : auto crypto = std::make_shared<CryptoProviderImpl>(
32 247 : keypair, std::move(common_objects_factory));
33 :
34 247 : return crypto;
35 247 : }
36 :
37 : auto createHashProvider() {
38 247 : return std::make_shared<YacHashProviderImpl>();
39 : }
40 :
41 : std::shared_ptr<Yac> createYac(
42 : ClusterOrdering initial_order,
43 : const shared_model::crypto::Keypair &keypair,
44 : std::shared_ptr<Timer> timer,
45 : std::shared_ptr<YacNetwork> network,
46 : std::shared_ptr<shared_model::interface::CommonObjectsFactory>
47 : common_objects_factory,
48 : ConsistencyModel consistency_model,
49 : const logger::LoggerManagerTreePtr &consensus_log_manager) {
50 247 : std::shared_ptr<iroha::consensus::yac::CleanupStrategy> cleanup_strategy =
51 247 : std::make_shared<iroha::consensus::yac::BufferedCleanupStrategy>();
52 247 : return Yac::create(
53 247 : YacVoteStorage(cleanup_strategy,
54 247 : getSupermajorityChecker(consistency_model),
55 247 : consensus_log_manager->getChild("VoteStorage")),
56 247 : std::move(network),
57 247 : createCryptoProvider(keypair, std::move(common_objects_factory)),
58 247 : std::move(timer),
59 247 : initial_order,
60 247 : consensus_log_manager->getChild("HashGate")->getLogger());
61 247 : }
62 : } // namespace
63 :
64 : namespace iroha {
65 : namespace consensus {
66 : namespace yac {
67 :
68 : std::shared_ptr<NetworkImpl> YacInit::getConsensusNetwork() const {
69 247 : BOOST_ASSERT_MSG(initialized_,
70 : "YacInit::initConsensusGate(...) must be called prior "
71 : "to YacInit::getConsensusNetwork()!");
72 247 : return consensus_network_;
73 : }
74 :
75 : auto YacInit::createTimer(std::chrono::milliseconds delay_milliseconds) {
76 : return std::make_shared<TimerImpl>([delay_milliseconds, this] {
77 : // static factory with a single thread
78 : //
79 : // observe_on_new_thread -- coordination which creates new thread with
80 : // observe_on strategy -- all subsequent operations will be performed
81 : // on this thread.
82 : //
83 : // scheduler owns a timeline that is exposed by the now() method.
84 : // scheduler is also a factory for workers in that timeline.
85 : //
86 : // coordination is a factory for coordinators and has a scheduler.
87 430 : return rxcpp::observable<>::timer(
88 430 : std::chrono::milliseconds(delay_milliseconds), coordination_);
89 0 : });
90 : }
91 :
92 : std::shared_ptr<YacGate> YacInit::initConsensusGate(
93 : std::shared_ptr<iroha::ametsuchi::PeerQueryFactory>
94 : peer_query_factory,
95 : std::shared_ptr<simulator::BlockCreator> block_creator,
96 : std::shared_ptr<network::BlockLoader> block_loader,
97 : const shared_model::crypto::Keypair &keypair,
98 : std::shared_ptr<consensus::ConsensusResultCache>
99 : consensus_result_cache,
100 : std::chrono::milliseconds vote_delay_milliseconds,
101 : std::shared_ptr<
102 : iroha::network::AsyncGrpcClient<google::protobuf::Empty>>
103 : async_call,
104 : std::shared_ptr<shared_model::interface::CommonObjectsFactory>
105 : common_objects_factory,
106 : ConsistencyModel consistency_model,
107 : const logger::LoggerManagerTreePtr &consensus_log_manager) {
108 247 : auto peer_orderer = createPeerOrderer(peer_query_factory);
109 :
110 247 : consensus_network_ = std::make_shared<NetworkImpl>(
111 : async_call,
112 247 : consensus_log_manager->getChild("Network")->getLogger());
113 :
114 247 : auto yac = createYac(peer_orderer->getInitialOrdering().value(),
115 247 : keypair,
116 247 : createTimer(vote_delay_milliseconds),
117 247 : consensus_network_,
118 247 : std::move(common_objects_factory),
119 247 : consistency_model,
120 247 : consensus_log_manager);
121 247 : consensus_network_->subscribe(yac);
122 :
123 247 : auto hash_provider = createHashProvider();
124 :
125 247 : initialized_ = true;
126 :
127 247 : return std::make_shared<YacGateImpl>(
128 247 : std::move(yac),
129 247 : std::move(peer_orderer),
130 : hash_provider,
131 : block_creator,
132 247 : std::move(consensus_result_cache),
133 247 : consensus_log_manager->getChild("Gate")->getLogger());
134 247 : }
135 : } // namespace yac
136 : } // namespace consensus
137 : } // namespace iroha
|