Kagome
Polkadot Runtime Engine in C++17
peers.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_API_SYSTEM_REQUEST_PEERS
7 #define KAGOME_API_SYSTEM_REQUEST_PEERS
8 
10 
11 #include "account_next_index.hpp"
13 #include "chain.hpp"
14 #include "chain_type.hpp"
15 #include "health.hpp"
16 
18 
24  struct Peers final : details::RequestType<jsonrpc::Value::Array> {
25  explicit Peers(std::shared_ptr<SystemApi> &api) : api_(api) {
26  BOOST_ASSERT(api_);
27  }
28 
29  outcome::result<Return> execute() override {
30  auto &peer_manager = *api_->getPeerManager();
31 
32  jsonrpc::Value::Array result;
33  result.reserve(peer_manager.activePeersNumber());
34 
35  peer_manager.forEachPeer([&](auto &peer_id) {
36  auto status_opt = peer_manager.getPeerState(peer_id);
37  if (status_opt.has_value()) {
38  auto &status = status_opt.value();
39 
40  jsonrpc::Value::Struct peer;
41  peer.emplace("PeerId", peer_id.toBase58());
42  peer.emplace(
43  "roles",
44  status.get().roles.flags.authority
45  ? "AUTHORITY"
46  : status.get().roles.flags.full
47  ? "FULL"
48  : status.get().roles.flags.light ? "LIGHT" : "NONE");
49  peer.emplace("bestHash", makeValue(status.get().best_block.hash));
50  peer.emplace("bestNumber", makeValue(status.get().best_block.number));
51 
52  result.emplace_back(std::move(peer));
53  }
54  });
55 
56  return result;
57  }
58 
59  private:
60  std::shared_ptr<SystemApi> api_;
61  };
62 
63 } // namespace kagome::api::system::request
64 
65 #endif // KAGOME_API_SYSTEM_REQUEST_PEERS
Peers(std::shared_ptr< SystemApi > &api)
Definition: peers.hpp:25
std::shared_ptr< SystemApi > api_
Definition: peers.hpp:60
Returns currently connected peers.
Definition: peers.hpp:24
outcome::result< Return > execute() override
Definition: peers.hpp:29
jsonrpc::Value makeValue(const uint32_t &)