Kagome
Polkadot Runtime Engine in C++17
grandpa_protocol.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_NETWORK_GRANDPAROTOCOL
7 #define KAGOME_NETWORK_GRANDPAROTOCOL
8 
10 
11 #include <memory>
12 
13 #include <libp2p/basic/scheduler.hpp>
14 #include <libp2p/connection/stream.hpp>
15 #include <libp2p/host/host.hpp>
16 
20 #include "log/logger.hpp"
23 #include "network/peer_manager.hpp"
25 #include "utils/non_copyable.hpp"
26 
27 namespace kagome::blockchain {
28  class BlockTree;
29 }
30 
31 namespace kagome::network {
32 
34 
35  class GrandpaProtocol final
36  : public ProtocolBase,
37  public std::enable_shared_from_this<GrandpaProtocol>,
39  NonMovable {
40  public:
41  GrandpaProtocol() = delete;
42  ~GrandpaProtocol() override = default;
43 
45  libp2p::Host &host,
46  std::shared_ptr<boost::asio::io_context> io_context,
47  const application::AppConfiguration &app_config,
48  std::shared_ptr<consensus::grandpa::GrandpaObserver> grandpa_observer,
49  const OwnPeerInfo &own_info,
50  std::shared_ptr<StreamEngine> stream_engine,
51  std::shared_ptr<PeerManager> peer_manager,
52  const primitives::BlockHash &genesis_hash,
53  std::shared_ptr<libp2p::basic::Scheduler> scheduler);
54 
59  bool start() override;
60  bool stop() override;
61 
62  const std::string &protocolName() const override {
63  return kGrandpaProtocolName;
64  }
65 
66  void onIncomingStream(std::shared_ptr<Stream> stream) override;
67  void newOutgoingStream(
68  const PeerInfo &peer_info,
69  std::function<void(outcome::result<std::shared_ptr<Stream>>)> &&cb)
70  override;
71 
72  void vote(network::GrandpaVote &&vote_message,
73  std::optional<const libp2p::peer::PeerId> peer_id);
74  void neighbor(GrandpaNeighborMessage &&msg);
75  void finalize(FullCommitMessage &&msg,
76  std::optional<const libp2p::peer::PeerId> peer_id);
77  void catchUpRequest(const libp2p::peer::PeerId &peer_id,
78  CatchUpRequest &&catch_up_request);
79  void catchUpResponse(const libp2p::peer::PeerId &peer_id,
80  CatchUpResponse &&catch_up_response);
81 
82  private:
83  const static inline auto kGrandpaProtocolName = "GrandpaProtocol"s;
84  enum class Direction { INCOMING, OUTGOING };
85  void readHandshake(std::shared_ptr<Stream> stream,
86  Direction direction,
87  std::function<void(outcome::result<void>)> &&cb);
88 
89  void writeHandshake(std::shared_ptr<Stream> stream,
90  Direction direction,
91  std::function<void(outcome::result<void>)> &&cb);
92 
93  void read(std::shared_ptr<Stream> stream);
94 
95  void write(
96  std::shared_ptr<Stream> stream,
97  const int &msg,
98  std::function<void(outcome::result<std::shared_ptr<Stream>>)> &&cb);
99 
103  static constexpr std::chrono::milliseconds kRecentnessDuration =
104  std::chrono::seconds(300);
105 
107  std::shared_ptr<boost::asio::io_context> io_context_;
109  std::shared_ptr<consensus::grandpa::GrandpaObserver> grandpa_observer_;
111  std::shared_ptr<StreamEngine> stream_engine_;
112  std::shared_ptr<PeerManager> peer_manager_;
113  std::shared_ptr<libp2p::basic::Scheduler> scheduler_;
114 
115  std::set<std::tuple<consensus::grandpa::RoundNumber,
118 
119  std::set<libp2p::peer::PeerId> recent_catchup_requests_by_peer_;
120  };
121 
122 } // namespace kagome::network
123 
124 #endif // KAGOME_NETWORK_GRANDPAROTOCOL
boost::variant< GrandpaVote, FullCommitMessage, GrandpaNeighborMessage, CatchUpRequest, CatchUpResponse > GrandpaMessage
const std::string & protocolName() const override
std::shared_ptr< boost::asio::io_context > io_context_
#define KAGOME_CACHE_UNIT(type)
std::shared_ptr< PeerManager > peer_manager_
libp2p::peer::PeerInfo PeerInfo
std::set< libp2p::peer::PeerId > recent_catchup_requests_by_peer_
libp2p::peer::PeerId PeerId
std::shared_ptr< consensus::grandpa::GrandpaObserver > grandpa_observer_
std::set< std::tuple< consensus::grandpa::RoundNumber, consensus::grandpa::VoterSetId > > recent_catchup_requests_by_round_
#define KAGOME_DECLARE_CACHE(prefix,...)
const application::AppConfiguration & app_config_
std::shared_ptr< StreamEngine > stream_engine_
std::shared_ptr< libp2p::basic::Scheduler > scheduler_