Kagome
Polkadot Runtime Engine in C++17
grandpa_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_CONSENSUS_GRANDPA_GRANDPAIMPL
7 #define KAGOME_CONSENSUS_GRANDPA_GRANDPAIMPL
8 
11 
12 #include <boost/operators.hpp>
13 
23 #include "crypto/hasher.hpp"
24 #include "log/logger.hpp"
25 #include "metrics/metrics.hpp"
26 #include "network/peer_manager.hpp"
28 #include "network/synchronizer.hpp"
30 
32 
33  // clang-format off
55  // clang-format on
56 
57  class GrandpaImpl : public Grandpa,
58  public GrandpaObserver,
59  public std::enable_shared_from_this<GrandpaImpl> {
60  public:
63  static const size_t kCatchUpThreshold = 2;
64 
66  static const size_t kKeepRecentRounds = 3;
67 
70  std::chrono::milliseconds(45'000);
71 
72  ~GrandpaImpl() override = default;
73 
74  GrandpaImpl(
75  std::shared_ptr<application::AppStateManager> app_state_manager,
76  std::shared_ptr<Environment> environment,
77  std::shared_ptr<crypto::Ed25519Provider> crypto_provider,
78  std::shared_ptr<runtime::GrandpaApi> grandpa_api,
79  const std::shared_ptr<crypto::Ed25519Keypair> &keypair,
80  const application::ChainSpec &chain_spec,
81  std::shared_ptr<Clock> clock,
82  std::shared_ptr<libp2p::basic::Scheduler> scheduler,
83  std::shared_ptr<authority::AuthorityManager> authority_manager,
84  std::shared_ptr<network::Synchronizer> synchronizer,
85  std::shared_ptr<network::PeerManager> peer_manager,
86  std::shared_ptr<blockchain::BlockTree> block_tree,
87  std::shared_ptr<network::ReputationRepository> reputation_repository);
88 
95  bool prepare();
96 
105  bool start();
106 
111  void stop();
112 
125  void onNeighborMessage(const libp2p::peer::PeerId &peer_id,
126  const network::GrandpaNeighborMessage &msg) override;
127 
128  // Catch-up methods
129 
140  void onCatchUpRequest(const libp2p::peer::PeerId &peer_id,
141  const network::CatchUpRequest &msg) override;
142 
156  void onCatchUpResponse(const libp2p::peer::PeerId &peer_id,
157  const network::CatchUpResponse &msg) override;
158 
159  // Voting methods
160 
171  void onVoteMessage(const libp2p::peer::PeerId &peer_id,
172  const network::VoteMessage &msg) override;
173 
183  void onCommitMessage(const libp2p::peer::PeerId &peer_id,
184  const network::FullCommitMessage &msg) override;
185 
196  outcome::result<void> applyJustification(
197  const BlockInfo &block_info,
198  const GrandpaJustification &justification) override;
199 
200  // Round processing method
201 
210  void tryExecuteNextRound(
211  const std::shared_ptr<VotingRound> &round) override;
212 
219  void updateNextRound(RoundNumber round_number) override;
220 
221  private:
229  std::optional<std::shared_ptr<VotingRound>> selectRound(
230  RoundNumber round_number, std::optional<VoterSetId> voter_set_id);
231 
235  outcome::result<MovableRoundState> getLastCompletedRound() const;
236 
245  std::shared_ptr<VotingRound> makeInitialRound(
246  const MovableRoundState &round_state, std::shared_ptr<VoterSet> voters);
247 
253  std::shared_ptr<VotingRound> makeNextRound(
254  const std::shared_ptr<VotingRound> &previous_round);
255 
260  void loadMissingBlocks();
261 
262  const Clock::Duration round_time_factor_;
263 
264  std::shared_ptr<Environment> environment_;
265  std::shared_ptr<crypto::Ed25519Provider> crypto_provider_;
266  std::shared_ptr<runtime::GrandpaApi> grandpa_api_;
267  const std::shared_ptr<crypto::Ed25519Keypair> &keypair_;
268  std::shared_ptr<Clock> clock_;
269  std::shared_ptr<libp2p::basic::Scheduler> scheduler_;
270  std::shared_ptr<authority::AuthorityManager> authority_manager_;
271  std::shared_ptr<network::Synchronizer> synchronizer_;
272  std::shared_ptr<network::PeerManager> peer_manager_;
273  std::shared_ptr<blockchain::BlockTree> block_tree_;
274  std::shared_ptr<network::ReputationRepository> reputation_repository_;
275 
276  std::shared_ptr<VotingRound> current_round_;
277  std::optional<
278  const std::tuple<libp2p::peer::PeerId, network::CatchUpRequest>>
279  pending_catchup_request_;
280  libp2p::basic::Scheduler::Handle catchup_request_timer_handle_;
281  libp2p::basic::Scheduler::Handle fallback_timer_handle_;
282 
283  // Metrics
284  metrics::RegistryPtr metrics_registry_ = metrics::createRegistry();
285  metrics::Gauge *metric_highest_round_;
286 
287  log::Logger logger_ = log::createLogger("Grandpa", "grandpa");
288  };
289 
290 } // namespace kagome::consensus::grandpa
291 
292 #endif // KAGOME_CONSENSUS_GRANDPA_GRANDPAIMPL
static constexpr Clock::Duration kCatchupRequestTimeout
Timeout of waiting catchup response for request.
typename ClockType::duration Duration
Definition: clock.hpp:23
static const size_t kKeepRecentRounds
Maximum number of rounds we are keep to communication.