Kagome
Polkadot Runtime Engine in C++17
state_protocol_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_NETWORK_STATEPROTOCOLIMPL
7 #define KAGOME_NETWORK_STATEPROTOCOLIMPL
8 
10 
11 #include <memory>
12 
13 #include <libp2p/connection/stream.hpp>
14 #include <libp2p/host/host.hpp>
15 
17 #include "log/logger.hpp"
19 
20 namespace kagome::network {
21 
26 
27  class StateProtocolImpl final
28  : public StateProtocol,
29  public std::enable_shared_from_this<StateProtocolImpl> {
30  public:
32  const application::ChainSpec &chain_spec,
33  std::shared_ptr<StateProtocolObserver> state_observer);
34 
35  bool start() override;
36  bool stop() override;
37 
38  const std::string &protocolName() const override {
39  return kStateProtocolName;
40  }
41 
42  void onIncomingStream(std::shared_ptr<Stream> stream) override;
43  void newOutgoingStream(
44  const PeerInfo &peer_info,
45  std::function<void(outcome::result<std::shared_ptr<Stream>>)> &&cb)
46  override;
47 
48  void request(const PeerId &peer_id,
49  StateRequest state_request,
50  std::function<void(outcome::result<StateResponse>)>
51  &&response_handler) override;
52 
53  void readRequest(std::shared_ptr<Stream> stream);
54 
55  void writeResponse(std::shared_ptr<Stream> stream,
56  StateResponse state_response);
57 
58  void writeRequest(std::shared_ptr<Stream> stream,
59  StateRequest state_request,
60  std::function<void(outcome::result<void>)> &&cb);
61 
62  void readResponse(
63  std::shared_ptr<Stream> stream,
64  std::function<void(outcome::result<StateResponse>)> &&response_handler);
65 
66  private:
67  const static inline auto kStateProtocolName = "StateProtocol"s;
69  std::shared_ptr<StateProtocolObserver> state_observer_;
71  log::Logger log_ = log::createLogger("StateProtocol", "state_protocol");
72  };
73 
74 } // namespace kagome::network
75 
76 #endif // KAGOME_NETWORK_STATEPROTOCOLIMPL
StateProtocolImpl(libp2p::Host &host, const application::ChainSpec &chain_spec, std::shared_ptr< StateProtocolObserver > state_observer)
void writeResponse(std::shared_ptr< Stream > stream, StateResponse state_response)
void writeRequest(std::shared_ptr< Stream > stream, StateRequest state_request, std::function< void(outcome::result< void >)> &&cb)
const libp2p::peer::Protocol protocol_
libp2p::peer::PeerInfo PeerInfo
libp2p::peer::Protocol Protocol
libp2p::peer::PeerId PeerId
void newOutgoingStream(const PeerInfo &peer_info, std::function< void(outcome::result< std::shared_ptr< Stream >>)> &&cb) override
Class for communication via /{chainType}/state/2 according to state protocol specification https://sp...
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
void request(const PeerId &peer_id, StateRequest state_request, std::function< void(outcome::result< StateResponse >)> &&response_handler) override
Make async request to peer and return response in callback.
libp2p::connection::Stream Stream
void readRequest(std::shared_ptr< Stream > stream)
void onIncomingStream(std::shared_ptr< Stream > stream) override
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
std::shared_ptr< StateProtocolObserver > state_observer_
const std::string & protocolName() const override
void readResponse(std::shared_ptr< Stream > stream, std::function< void(outcome::result< StateResponse >)> &&response_handler)