Kagome
Polkadot Runtime Engine in C++17
babe_config_repository_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CONSENSUS_BABE_BABECONFIGREPOSITORYIMPL
7 #define KAGOME_CONSENSUS_BABE_BABECONFIGREPOSITORYIMPL
8 
12 
14 #include "log/logger.hpp"
17 
18 namespace kagome::application {
19  class AppStateManager;
20 }
21 namespace kagome::blockchain {
22  class BlockTree;
23  class BlockHeaderRepository;
24 } // namespace kagome::blockchain
25 namespace kagome::crypto {
26  class Hasher;
27 }
28 namespace kagome::runtime {
29  class BabeApi;
30 }
31 
32 namespace kagome::consensus::babe {
33 
35  : public BabeConfigRepository,
36  public BabeDigestObserver,
37  public BabeUtil,
38  public std::enable_shared_from_this<BabeConfigRepositoryImpl> {
39  static const primitives::BlockNumber kSavepointEachSuchBlock = 100000;
40 
41  public:
43  const std::shared_ptr<application::AppStateManager> &app_state_manager,
44  std::shared_ptr<storage::BufferStorage> persistent_storage,
45  std::shared_ptr<blockchain::BlockTree> block_tree,
46  std::shared_ptr<blockchain::BlockHeaderRepository> header_repo,
47  std::shared_ptr<runtime::BabeApi> babe_api,
48  std::shared_ptr<crypto::Hasher> hasher,
50  const primitives::GenesisBlockHeader &genesis_block_header,
51  const BabeClock &clock);
52 
53  bool prepare();
54 
55  // BabeDigestObserver
56 
57  outcome::result<void> onDigest(
58  const primitives::BlockInfo &block,
59  const consensus::BabeBlockHeader &digest) override;
60 
61  outcome::result<void> onDigest(
62  const primitives::BlockInfo &block,
63  const primitives::BabeDigest &digest) override;
64 
65  void cancel(const primitives::BlockInfo &block) override;
66 
67  // BabeConfigRepository
68 
69  BabeDuration slotDuration() const override;
70 
71  EpochLength epochLength() const override;
72 
73  std::shared_ptr<const primitives::BabeConfiguration> config(
74  const primitives::BlockInfo &parent_block,
75  consensus::EpochNumber epoch_number) override;
76 
77  // BabeUtil
78 
79  BabeSlotNumber syncEpoch(
80  std::function<std::tuple<BabeSlotNumber, bool>()> &&f) override;
81 
82  BabeSlotNumber getCurrentSlot() const override;
83 
84  BabeTimePoint slotStartTime(BabeSlotNumber slot) const override;
85  BabeDuration remainToStartOfSlot(BabeSlotNumber slot) const override;
86  BabeTimePoint slotFinishTime(BabeSlotNumber slot) const override;
87  BabeDuration remainToFinishOfSlot(BabeSlotNumber slot) const override;
88 
89  EpochNumber slotToEpoch(BabeSlotNumber slot) const override;
90  BabeSlotNumber slotInEpoch(BabeSlotNumber slot) const override;
91 
92  private:
93  outcome::result<void> load();
94  outcome::result<void> save();
95 
96  void prune(const primitives::BlockInfo &block);
97 
98  outcome::result<void> onNextEpochData(const primitives::BlockInfo &block,
99  const primitives::NextEpochData &msg);
100 
101  outcome::result<void> onNextConfigData(
102  const primitives::BlockInfo &block,
103  const primitives::NextConfigDataV1 &msg);
104 
110  std::shared_ptr<BabeConfigNode> getNode(
111  const primitives::BlockInfo &block) const;
112 
119  bool directChainExists(const primitives::BlockInfo &ancestor,
120  const primitives::BlockInfo &descendant) const;
121 
122  BabeSlotNumber getFirstBlockSlotNumber();
123 
124  std::shared_ptr<storage::BufferStorage> persistent_storage_;
125  std::shared_ptr<blockchain::BlockTree> block_tree_;
126  std::shared_ptr<blockchain::BlockHeaderRepository> header_repo_;
127  std::shared_ptr<runtime::BabeApi> babe_api_;
128  std::shared_ptr<crypto::Hasher> hasher_;
129  std::shared_ptr<primitives::events::ChainEventSubscriber> chain_sub_;
131 
132  const BabeDuration slot_duration_{};
133  const EpochLength epoch_length_{};
134 
135  std::shared_ptr<BabeConfigNode> root_;
136  primitives::BlockNumber last_saved_state_block_ = 0;
137 
139  std::optional<BabeSlotNumber> first_block_slot_number_;
140  bool is_first_block_finalized_ = false;
141 
143  };
144 
145 } // namespace kagome::consensus::babe
146 
147 #endif // KAGOME_CONSENSUS_BABE_BABECONFIGREPOSITORYIMPL
std::shared_ptr< storage::BufferStorage > persistent_storage_
uint32_t BlockNumber
Definition: common.hpp:18
std::shared_ptr< ChainSubscriptionEngine > ChainSubscriptionEnginePtr
std::shared_ptr< blockchain::BlockHeaderRepository > header_repo_
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
BabeClock::TimePoint BabeTimePoint
BABE uses system clock&#39;s time points.
Definition: common.hpp:18
std::shared_ptr< primitives::events::ChainEventSubscriber > chain_sub_
uint64_t BabeSlotNumber
slot number of the Babe production
Definition: common.hpp:24
EpochNumber EpochLength
Definition: common.hpp:30
boost::variant< Unused< 0 >, NextEpochData, OnDisabled, NextConfigData > BabeDigest
https://github.com/paritytech/substrate/blob/polkadot-v0.9.8/primitives/consensus/babe/src/lib.rs#L130
Definition: digest.hpp:63
uint64_t EpochNumber
number of the epoch in the Babe production
Definition: common.hpp:27
BabeClock::Duration BabeDuration
Definition: common.hpp:21