Kagome
Polkadot Runtime Engine in C++17
peer_manager_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_NETWORK_PEERMANAGERIMPL
7 #define KAGOME_NETWORK_PEERMANAGERIMPL
8 
10 
11 #include <memory>
12 #include <queue>
13 
14 #include <libp2p/basic/scheduler.hpp>
15 #include <libp2p/event/bus.hpp>
16 #include <libp2p/host/host.hpp>
17 #include <libp2p/protocol/identify/identify.hpp>
18 #include <libp2p/protocol/kademlia/kademlia.hpp>
19 
25 #include "clock/clock.hpp"
27 #include "crypto/hasher.hpp"
28 #include "log/logger.hpp"
29 #include "metrics/metrics.hpp"
36 #include "network/router.hpp"
40 #include "scale/libp2p_types.hpp"
42 
43 namespace kagome::network {
44 
45  enum class PeerType { PEER_TYPE_IN = 0, PEER_TYPE_OUT };
46 
47  struct PeerDescriptor {
50  };
51 
52  class PeerManagerImpl : public PeerManager,
53  public std::enable_shared_from_this<PeerManagerImpl> {
54  public:
55  static constexpr std::chrono::seconds kTimeoutForConnecting{15};
56 
57  enum class Error { UNDECLARED_COLLATOR = 1, OUT_OF_VIEW, DUPLICATE };
58 
60  std::shared_ptr<application::AppStateManager> app_state_manager,
61  libp2p::Host &host,
62  std::shared_ptr<libp2p::protocol::Identify> identify,
63  std::shared_ptr<libp2p::protocol::kademlia::Kademlia> kademlia,
64  std::shared_ptr<libp2p::basic::Scheduler> scheduler,
65  std::shared_ptr<StreamEngine> stream_engine,
66  const application::AppConfiguration &app_config,
67  std::shared_ptr<clock::SteadyClock> clock,
68  const BootstrapNodes &bootstrap_nodes,
69  const OwnPeerInfo &own_peer_info,
70  std::shared_ptr<network::Router> router,
71  std::shared_ptr<storage::BufferStorage> storage,
72  std::shared_ptr<crypto::Hasher> hasher,
73  std::shared_ptr<ReputationRepository> reputation_repository);
74 
76  bool prepare();
77 
79  bool start();
80 
82  void stop();
83 
85  void connectToPeer(const PeerInfo &peer_info) override;
86 
88  void reserveStreams(const PeerId &peer_id) const override;
89 
91  std::shared_ptr<StreamEngine> getStreamEngine() override;
92 
94  size_t activePeersNumber() const override;
95 
97  void setCollating(const PeerId &peer_id,
98  network::CollatorPublicKey const &collator_id,
99  network::ParachainId para_id) override;
100 
102  ParachainState &parachainState() override;
103 
104  outcome::result<
105  std::pair<network::CollatorPublicKey const &, network::ParachainId>>
106  insert_advertisement(PeerState &peer_state,
107  ParachainState &parachain_state,
108  primitives::BlockHash para_hash) override;
109 
111  void forEachPeer(std::function<void(const PeerId &)> func) const override;
112 
114  void forOnePeer(const PeerId &peer_id,
115  std::function<void(const PeerId &)> func) const override;
116 
118  void keepAlive(const PeerId &peer_id) override;
119 
121  void startPingingPeer(const PeerId &peer_id) override;
122 
124  void updatePeerState(const PeerId &peer_id, const Status &status) override;
125 
127  void updatePeerState(const PeerId &peer_id,
128  const BlockAnnounce &announce) override;
129 
131  void updatePeerState(
132  const PeerId &peer_id,
133  const GrandpaNeighborMessage &neighbor_message) override;
134 
136  std::optional<std::reference_wrapper<PeerState>> getPeerState(
137  const PeerId &peer_id) override;
138 
139  private:
141  bool isSelfPeer(const PeerId &peer_id) const;
142 
144  void align();
145 
146  void processDiscoveredPeer(const PeerId &peer_id);
147 
148  void processFullyConnectedPeer(const PeerId &peer_id);
149 
151  void connectToPeer(const PeerId &peer_id);
152 
154  void disconnectFromPeer(const PeerId &peer_id);
155 
156  std::vector<scale::PeerInfoSerializable> loadLastActivePeers();
157 
160  void storeActivePeers();
161 
162  void clearClosedPingingConnections();
163 
164  std::shared_ptr<application::AppStateManager> app_state_manager_;
166  std::shared_ptr<libp2p::protocol::Identify> identify_;
167  std::shared_ptr<libp2p::protocol::kademlia::Kademlia> kademlia_;
168  std::shared_ptr<libp2p::basic::Scheduler> scheduler_;
169  std::shared_ptr<StreamEngine> stream_engine_;
171  std::shared_ptr<clock::SteadyClock> clock_;
174  std::shared_ptr<network::Router> router_;
175  std::shared_ptr<storage::BufferStorage> storage_;
176  std::shared_ptr<crypto::Hasher> hasher_;
177  std::shared_ptr<ReputationRepository> reputation_repository_;
178 
179  libp2p::event::Handle add_peer_handle_;
180  libp2p::event::Handle peer_disconnected_handler_;
181  std::unordered_set<PeerId> peers_in_queue_;
182  std::deque<std::reference_wrapper<const PeerId>> queue_to_connect_;
183  std::unordered_set<PeerId> connecting_peers_;
184  std::unordered_set<libp2p::network::ConnectionManager::ConnectionSPtr>
186 
187  std::map<PeerId, PeerDescriptor> active_peers_;
188  std::unordered_map<PeerId, PeerState> peer_states_;
189  libp2p::basic::Scheduler::Handle align_timer_;
190  std::set<PeerId> recently_active_peers_;
191 
192  // metrics
195 
196  // parachain
198 
200  };
201 
202 } // namespace kagome::network
203 
205 
206 #endif // KAGOME_NETWORK_PEERMANAGERIMPL
const BootstrapNodes & bootstrap_nodes_
std::unordered_set< PeerId > peers_in_queue_
std::unordered_set< libp2p::network::ConnectionManager::ConnectionSPtr > pinging_connections_
std::shared_ptr< libp2p::basic::Scheduler > scheduler_
std::shared_ptr< application::AppStateManager > app_state_manager_
libp2p::peer::PeerId PeerId
std::unordered_set< PeerId > connecting_peers_
std::shared_ptr< StreamEngine > stream_engine_
std::deque< std::reference_wrapper< const PeerId > > queue_to_connect_
std::map< PeerId, PeerDescriptor > active_peers_
crypto::Sr25519PublicKey CollatorPublicKey
OUTCOME_HPP_DECLARE_ERROR(kagome::api, JRpcServerImpl::Error)
const application::AppConfiguration & app_config_
std::unique_ptr< Registry > RegistryPtr
Definition: metrics.hpp:15
RegistryPtr createRegistry()
libp2p::event::Handle add_peer_handle_
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::shared_ptr< ReputationRepository > reputation_repository_
std::shared_ptr< network::Router > router_
libp2p::peer::PeerInfo PeerInfo
std::shared_ptr< libp2p::protocol::kademlia::Kademlia > kademlia_
std::shared_ptr< clock::SteadyClock > clock_
std::unordered_map< PeerId, PeerState > peer_states_
libp2p::event::Handle peer_disconnected_handler_
libp2p::basic::Scheduler::Handle align_timer_
std::shared_ptr< crypto::Hasher > hasher_
std::shared_ptr< storage::BufferStorage > storage_
A gauge metric to represent a value that can arbitrarily go up and down.
Definition: metrics.hpp:49
std::shared_ptr< libp2p::protocol::Identify > identify_
typename ClockType::time_point TimePoint
Definition: clock.hpp:27
clock::SteadyClock::TimePoint time_point