Kagome
Polkadot Runtime Engine in C++17
authority_manager_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CONSENSUS_AUTHORITIES_MANAGER_IMPL
7 #define KAGOME_CONSENSUS_AUTHORITIES_MANAGER_IMPL
8 
11 
12 #include "crypto/hasher.hpp"
13 #include "log/logger.hpp"
14 #include "primitives/authority.hpp"
17 
18 namespace kagome::application {
19  class AppStateManager;
20 }
21 namespace kagome::authority {
22  class ScheduleNode;
23 }
24 namespace kagome::blockchain {
25  class BlockTree;
26  class BlockHeaderRepository;
27 } // namespace kagome::blockchain
28 
29 namespace kagome::primitives {
30  struct BabeConfiguration;
31 } // namespace kagome::primitives
32 
33 namespace kagome::runtime {
34  class GrandpaApi;
35  class Executor;
36 } // namespace kagome::runtime
37 
38 namespace kagome::storage::trie {
39  class TrieStorage;
40 }
41 
42 namespace kagome::authority {
43 
46  public:
47  inline static const std::vector<primitives::ConsensusEngineId>
49 
50  struct Config {
51  // Whether OnDisabled digest message should be processed.
52  // It is disabled in Polkadot.
53  // It is enabled in Kusama, but some blocks (recognized in 530k-550k)
54  // fail to finalize and syncing gets stuck
55  bool on_disable_enabled = false;
56  };
57 
59  Config config,
60  std::shared_ptr<application::AppStateManager> app_state_manager,
61  std::shared_ptr<blockchain::BlockTree> block_tree,
62  std::shared_ptr<storage::trie::TrieStorage> trie_storage,
63  std::shared_ptr<runtime::GrandpaApi> grandpa_api,
64  std::shared_ptr<crypto::Hasher> hash,
65  std::shared_ptr<storage::BufferStorage> persistent_storage,
66  std::shared_ptr<blockchain::BlockHeaderRepository> header_repo);
67 
68  ~AuthorityManagerImpl() override;
69 
70  outcome::result<void> recalculateStoredState(
71  primitives::BlockNumber last_finalized_number) override;
72 
73  bool prepare();
74 
75  primitives::BlockInfo base() const override;
76 
77  std::optional<std::shared_ptr<const primitives::AuthoritySet>> authorities(
78  const primitives::BlockInfo &target_block,
79  IsBlockFinalized finalized) const override;
80 
81  outcome::result<void> applyScheduledChange(
82  const primitives::BlockInfo &block,
83  const primitives::AuthorityList &authorities,
84  primitives::BlockNumber activate_at) override;
85 
86  outcome::result<void> applyForcedChange(
87  const primitives::BlockInfo &block,
88  const primitives::AuthorityList &authorities,
89  primitives::BlockNumber delay_start,
90  size_t delay) override;
91 
92  outcome::result<void> applyOnDisabled(const primitives::BlockInfo &block,
93  uint64_t authority_index) override;
94 
95  outcome::result<void> applyPause(
96  const primitives::BlockInfo &block,
97  primitives::BlockNumber activate_at) override;
98 
99  outcome::result<void> applyResume(
100  const primitives::BlockInfo &block,
101  primitives::BlockNumber activate_at) override;
102 
103  outcome::result<void> onConsensus(
104  const primitives::BlockInfo &block,
105  const primitives::Consensus &message) override;
106 
107  void cancel(const primitives::BlockInfo &block) override;
108 
109  void prune(const primitives::BlockInfo &block) override;
110 
111  private:
112  outcome::result<void> initializeAt(const primitives::BlockInfo &root_block);
113 
119  std::shared_ptr<ScheduleNode> getAppropriateAncestor(
120  const primitives::BlockInfo &block) const;
121 
122  outcome::result<std::optional<primitives::AuthoritySetId>>
123  readSetIdFromRuntime(primitives::BlockHeader const &targetBlock) const;
124 
131  bool directChainExists(const primitives::BlockInfo &ancestor,
132  const primitives::BlockInfo &descendant) const;
133 
134  void reorganize(std::shared_ptr<ScheduleNode> node,
135  std::shared_ptr<ScheduleNode> new_node);
136 
138  std::shared_ptr<const blockchain::BlockTree> block_tree_;
139  std::shared_ptr<storage::trie::TrieStorage> trie_storage_;
140  std::shared_ptr<runtime::GrandpaApi> grandpa_api_;
141  std::shared_ptr<crypto::Hasher> hasher_;
142  std::shared_ptr<storage::BufferStorage> persistent_storage_;
143  std::shared_ptr<blockchain::BlockHeaderRepository> header_repo_;
144 
145  std::shared_ptr<ScheduleNode> root_;
147  };
148 } // namespace kagome::authority
149 
150 #endif // KAGOME_CONSENSUS_AUTHORITIES_MANAGER_IMPL
std::shared_ptr< storage::trie::TrieStorage > trie_storage_
uint32_t BlockNumber
Definition: common.hpp:18
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::shared_ptr< crypto::Hasher > hasher_
std::shared_ptr< runtime::GrandpaApi > grandpa_api_
const auto kGrandpaEngineId
Definition: digest.hpp:28
std::shared_ptr< storage::BufferStorage > persistent_storage_
const auto kBabeEngineId
Definition: digest.hpp:25
std::shared_ptr< const blockchain::BlockTree > block_tree_
std::shared_ptr< blockchain::BlockHeaderRepository > header_repo_