Kagome
Polkadot Runtime Engine in C++17
babe_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_BABE_IMPL_HPP
7 #define KAGOME_BABE_IMPL_HPP
8 
10 
11 #include <boost/asio/basic_waitable_timer.hpp>
12 #include <memory>
13 
15 #include "authorship/proposer.hpp"
17 #include "clock/timer.hpp"
23 #include "crypto/hasher.hpp"
25 #include "crypto/sr25519_types.hpp"
26 #include "log/logger.hpp"
27 #include "metrics/metrics.hpp"
28 #include "outcome/outcome.hpp"
30 #include "primitives/common.hpp"
33 #include "telemetry/service.hpp"
34 
35 namespace kagome::application {
36  class AppConfiguration;
37 }
38 
39 namespace kagome::network {
40  class Synchronizer;
41  class BlockAnnounceTransmitter;
42 } // namespace kagome::network
43 
44 namespace kagome::runtime {
45  class OffchainWorkerApi;
46  class Core;
47 } // namespace kagome::runtime
48 
49 namespace kagome::consensus::babe {
50  class BabeConfigRepository;
51  class ConsistencyKeeper;
52 } // namespace kagome::consensus::babe
53 
54 namespace kagome::consensus::babe {
55 
56  inline const auto kTimestampId =
58  inline const auto kBabeSlotId =
60  inline const auto kParachainId =
62  inline const auto kUnsupportedInherentId_uncles00 =
64 
67  static constexpr auto kMaxBlockSlotsOvertime = 2;
68 
69  class BabeImpl : public Babe, public std::enable_shared_from_this<BabeImpl> {
70  public:
74  BabeImpl(
75  const application::AppConfiguration &app_config,
76  std::shared_ptr<application::AppStateManager> app_state_manager,
77  std::shared_ptr<BabeLottery> lottery,
78  std::shared_ptr<consensus::babe::BabeConfigRepository> babe_config_repo,
79  std::shared_ptr<authorship::Proposer> proposer,
80  std::shared_ptr<blockchain::BlockTree> block_tree,
81  std::shared_ptr<network::BlockAnnounceTransmitter>
82  block_announce_transmitter,
83  std::shared_ptr<crypto::Sr25519Provider> sr25519_provider,
84  const std::shared_ptr<crypto::Sr25519Keypair> &keypair,
85  std::shared_ptr<clock::SystemClock> clock,
86  std::shared_ptr<crypto::Hasher> hasher,
87  std::unique_ptr<clock::Timer> timer,
88  std::shared_ptr<authority::AuthorityUpdateObserver>
89  authority_update_observer,
90  std::shared_ptr<network::Synchronizer> synchronizer,
91  std::shared_ptr<BabeUtil> babe_util,
93  std::shared_ptr<runtime::OffchainWorkerApi> offchain_worker_api,
94  std::shared_ptr<runtime::Core> core,
95  std::shared_ptr<babe::ConsistencyKeeper> consistency_keeper);
96 
97  ~BabeImpl() override = default;
98 
100  bool prepare();
101 
103  bool start();
104 
106  void stop();
107 
108  void runEpoch(EpochDescriptor epoch) override;
109 
110  State getCurrentState() const override;
111 
112  void onRemoteStatus(const libp2p::peer::PeerId &peer_id,
113  const network::Status &status) override;
114 
115  void onBlockAnnounce(const libp2p::peer::PeerId &peer_id,
116  const network::BlockAnnounce &announce) override;
117 
118  void onSynchronized() override;
119 
120  bool wasSynchronized() const override;
121 
122  private:
123  outcome::result<EpochDescriptor> getInitialEpochDescriptor();
124 
125  void adjustEpochDescriptor();
126 
127  void startCatchUp(const libp2p::peer::PeerId &peer_id,
128  const primitives::BlockInfo &target_block);
129 
130  void startStateSyncing(const libp2p::peer::PeerId &peer_id);
131 
132  void runSlot();
133 
137  void processSlot();
138 
145  void processSlotLeadership(
146  SlotType slot_type,
147  std::optional<std::reference_wrapper<const crypto::VRFOutput>> output,
148  primitives::AuthorityIndex authority_index);
149 
153  void startNextEpoch();
154 
155  void changeLotteryEpoch(
156  const EpochDescriptor &epoch,
157  std::shared_ptr<const primitives::BabeConfiguration> babe_config) const;
158 
159  outcome::result<primitives::PreRuntime> babePreDigest(
160  SlotType slot_type,
161  std::optional<std::reference_wrapper<const crypto::VRFOutput>> output,
162  primitives::AuthorityIndex authority_index) const;
163 
164  outcome::result<primitives::Seal> sealBlock(
165  const primitives::Block &block) const;
166 
168  std::shared_ptr<BabeLottery> lottery_;
169  std::shared_ptr<consensus::babe::BabeConfigRepository> babe_config_repo_;
170  std::shared_ptr<authorship::Proposer> proposer_;
171  std::shared_ptr<blockchain::BlockTree> block_tree_;
172  std::shared_ptr<network::BlockAnnounceTransmitter>
174  const std::shared_ptr<crypto::Sr25519Keypair> &keypair_;
175  std::shared_ptr<clock::SystemClock> clock_;
176  std::shared_ptr<crypto::Hasher> hasher_;
177  std::shared_ptr<crypto::Sr25519Provider> sr25519_provider_;
178  std::unique_ptr<clock::Timer> timer_;
179  std::shared_ptr<authority::AuthorityUpdateObserver>
181  std::shared_ptr<network::Synchronizer> synchronizer_;
182  std::shared_ptr<BabeUtil> babe_util_;
184  std::shared_ptr<primitives::events::ChainEventSubscriber> chain_sub_;
185  std::optional<primitives::Version> actual_runtime_version_;
186  std::shared_ptr<runtime::OffchainWorkerApi> offchain_worker_api_;
187  std::shared_ptr<runtime::Core> runtime_core_;
188  std::shared_ptr<babe::ConsistencyKeeper> consistency_keeper_;
189 
190  State current_state_{State::WAIT_REMOTE_STATUS};
191 
192  bool was_synchronized_{false};
193 
194  std::atomic_bool active_{false};
195 
197 
198  BabeSlotNumber current_slot_{};
199 
200  primitives::BlockInfo best_block_{};
201 
202  // Metrics
205 
208  };
209 } // namespace kagome::consensus::babe
210 
211 #endif // KAGOME_BABE_IMPL_HPP
static constexpr auto kMaxBlockSlotsOvertime
Definition: babe_impl.hpp:67
telemetry::Telemetry telemetry_
Definition: babe_impl.hpp:207
uint32_t AuthorityIndex
Definition: authority.hpp:36
std::shared_ptr< BabeUtil > babe_util_
Definition: babe_impl.hpp:182
std::shared_ptr< runtime::OffchainWorkerApi > offchain_worker_api_
Definition: babe_impl.hpp:186
std::shared_ptr< clock::SystemClock > clock_
Definition: babe_impl.hpp:175
Block class represents polkadot block primitive.
Definition: block.hpp:19
std::shared_ptr< authorship::Proposer > proposer_
Definition: babe_impl.hpp:170
std::shared_ptr< consensus::babe::BabeConfigRepository > babe_config_repo_
Definition: babe_impl.hpp:169
std::optional< primitives::Version > actual_runtime_version_
Definition: babe_impl.hpp:185
A histogram metric to represent aggregatable distributions of events.
Definition: metrics.hpp:108
std::shared_ptr< authority::AuthorityUpdateObserver > authority_update_observer_
Definition: babe_impl.hpp:180
std::shared_ptr< network::BlockAnnounceTransmitter > block_announce_transmitter_
Definition: babe_impl.hpp:173
std::unique_ptr< Registry > RegistryPtr
Definition: metrics.hpp:15
const application::AppConfiguration & app_config_
Definition: babe_impl.hpp:167
RegistryPtr createRegistry()
std::unique_ptr< clock::Timer > timer_
Definition: babe_impl.hpp:178
static outcome::result< Blob< size_ > > fromString(std::string_view data)
Definition: blob.hpp:169
std::shared_ptr< ChainSubscriptionEngine > ChainSubscriptionEnginePtr
libp2p::peer::PeerId PeerId
std::shared_ptr< primitives::events::ChainEventSubscriber > chain_sub_
Definition: babe_impl.hpp:184
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
const std::shared_ptr< crypto::Sr25519Keypair > & keypair_
Definition: babe_impl.hpp:174
metrics::Histogram * metric_block_proposal_time_
Definition: babe_impl.hpp:204
std::shared_ptr< runtime::Core > runtime_core_
Definition: babe_impl.hpp:187
std::shared_ptr< BabeLottery > lottery_
Definition: babe_impl.hpp:168
uint64_t BabeSlotNumber
slot number of the Babe production
Definition: common.hpp:24
std::shared_ptr< babe::ConsistencyKeeper > consistency_keeper_
Definition: babe_impl.hpp:188
const auto kUnsupportedInherentId_uncles00
Definition: babe_impl.hpp:62
std::shared_ptr< crypto::Hasher > hasher_
Definition: babe_impl.hpp:176
std::shared_ptr< crypto::Sr25519Provider > sr25519_provider_
Definition: babe_impl.hpp:177
std::shared_ptr< network::Synchronizer > synchronizer_
Definition: babe_impl.hpp:181
std::shared_ptr< TelemetryService > Telemetry
Definition: service.hpp:86
std::shared_ptr< blockchain::BlockTree > block_tree_
Definition: babe_impl.hpp:171
primitives::events::ChainSubscriptionEnginePtr chain_events_engine_
Definition: babe_impl.hpp:183