Kagome
Polkadot Runtime Engine in C++17
peer_manager.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_NETWORK_PEERMANAGER
7 #define KAGOME_NETWORK_PEERMANAGER
8 
9 #include <optional>
10 #include <unordered_set>
11 
12 #include <libp2p/peer/peer_id.hpp>
13 #include <libp2p/peer/peer_info.hpp>
14 
18 #include "network/types/status.hpp"
19 #include "outcome/outcome.hpp"
20 #include "primitives/common.hpp"
21 #include "utils/non_copyable.hpp"
22 
23 namespace kagome::network {
24 
25  struct CollatorState {
28  std::unordered_set<BlockHash> advertisements;
29  };
30 
35  };
36 
37  /*
38  * Parachain state view.
39  */
40  struct ParachainState {
41  std::unordered_map<BlockHash, bool> our_view;
42  };
43 
44  struct PeerState {
46  Roles roles = 0;
47  BlockInfo best_block = {0, {}};
48  std::optional<RoundNumber> round_number = std::nullopt;
49  std::optional<VoterSetId> set_id = std::nullopt;
50  BlockNumber last_finalized = 0;
51  std::optional<CollatorState> collator_state = std::nullopt;
52  };
53 
54  struct StreamEngine;
55 
63  class PeerManager {
64  public:
68  using AdvResult = outcome::result<
69  std::pair<network::CollatorPublicKey const &, network::ParachainId>>;
70 
71  virtual ~PeerManager() = default;
72 
76  virtual void connectToPeer(const PeerInfo &peer_info) = 0;
77 
81  virtual void reserveStreams(const PeerId &peer_id) const = 0;
82 
86  virtual std::shared_ptr<StreamEngine> getStreamEngine() = 0;
87 
91  virtual void keepAlive(const PeerId &peer_id) = 0;
92 
96  virtual void startPingingPeer(const PeerId &peer_id) = 0;
97 
101  virtual void updatePeerState(const PeerId &peer_id,
102  const Status &status) = 0;
103 
107  virtual void updatePeerState(const PeerId &peer_id,
108  const BlockAnnounce &announce) = 0;
109 
113  virtual outcome::result<
114  std::pair<network::CollatorPublicKey const &, network::ParachainId>>
115  insert_advertisement(PeerState &peer_state,
116  ParachainState &parachain_state,
117  primitives::BlockHash para_hash) = 0;
118 
122  virtual ParachainState &parachainState() = 0;
123 
128  virtual void setCollating(const PeerId &peer_id,
130  network::ParachainId para_id) = 0;
131 
136  virtual void updatePeerState(
137  const PeerId &peer_id,
138  const GrandpaNeighborMessage &neighbor_message) = 0;
139 
143  virtual std::optional<std::reference_wrapper<PeerState>> getPeerState(
144  const PeerId &peer_id) = 0;
145 
149  virtual size_t activePeersNumber() const = 0;
150 
154  virtual void forEachPeer(
155  std::function<void(const PeerId &)> func) const = 0;
156 
160  virtual void forOnePeer(const PeerId &peer_id,
161  std::function<void(const PeerId &)> func) const = 0;
162  };
163 } // namespace kagome::network
164 
165 #endif // KAGOME_NETWORK_PEERMANAGER
clock::SteadyClock::TimePoint time
libp2p::peer::PeerId PeerId
network::ParachainId parachain_id
std::unordered_set< BlockHash > advertisements
crypto::Sr25519PublicKey CollatorPublicKey
network::CollatorPublicKey collator_id
primitives::BlockInfo BlockInfo
Definition: structs.hpp:29
std::unordered_map< BlockHash, bool > our_view
libp2p::peer::PeerInfo PeerInfo
libp2p::peer::PeerId PeerId
primitives::BlockNumber BlockNumber
Definition: common.hpp:24
libp2p::peer::PeerInfo PeerInfo
libp2p::peer::PeerId const & peer_id
primitives::BlockHash BlockHash
Definition: common.hpp:23
detail::BlockInfoT< struct BlockInfoTag > BlockInfo
Definition: common.hpp:63
outcome::result< std::pair< network::CollatorPublicKey const &, network::ParachainId >> AdvResult
typename ClockType::time_point TimePoint
Definition: clock.hpp:27