Kagome
Polkadot Runtime Engine in C++17
block_announce_protocol.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_NETWORK_BLOCKANNOUNCEPROTOCOL
7 #define KAGOME_NETWORK_BLOCKANNOUNCEPROTOCOL
8 
10 
11 #include <memory>
12 
13 #include <libp2p/connection/stream.hpp>
14 #include <libp2p/host/host.hpp>
15 
21 #include "crypto/hasher.hpp"
22 #include "log/logger.hpp"
26 #include "network/peer_manager.hpp"
28 #include "network/types/status.hpp"
29 #include "utils/non_copyable.hpp"
30 
31 namespace kagome::network {
32 
33  KAGOME_DECLARE_CACHE(BlockAnnounceProtocol, KAGOME_CACHE_UNIT(BlockAnnounce));
34 
36  : public ProtocolBase,
37  public std::enable_shared_from_this<BlockAnnounceProtocol>,
39  NonMovable {
40  public:
41  BlockAnnounceProtocol() = delete;
42  ~BlockAnnounceProtocol() override = default;
43 
45  const application::AppConfiguration &app_config,
46  const application::ChainSpec &chain_spec,
47  std::shared_ptr<StreamEngine> stream_engine,
48  std::shared_ptr<blockchain::BlockTree> block_tree,
49  std::shared_ptr<BlockAnnounceObserver> observer,
50  std::shared_ptr<PeerManager> peer_manager);
51 
52  bool start() override;
53  bool stop() override;
54 
55  const std::string &protocolName() const override {
57  }
58 
59  void onIncomingStream(std::shared_ptr<Stream> stream) override;
60  void newOutgoingStream(
61  const PeerInfo &peer_info,
62  std::function<void(outcome::result<std::shared_ptr<Stream>>)> &&cb)
63  override;
64 
65  void blockAnnounce(BlockAnnounce &&announce);
66 
67  private:
68  outcome::result<Status> createStatus() const;
69 
70  enum class Direction { INCOMING, OUTGOING };
71  void readStatus(std::shared_ptr<Stream> stream,
72  Direction direction,
73  std::function<void(outcome::result<void>)> &&cb);
74 
75  void writeStatus(std::shared_ptr<Stream> stream,
76  Direction direction,
77  std::function<void(outcome::result<void>)> &&cb);
78 
79  void readAnnounce(std::shared_ptr<Stream> stream);
80 
81  const static inline auto kBlockAnnounceProtocolName =
82  "BlockAnnounceProtocol"s;
85  std::shared_ptr<StreamEngine> stream_engine_;
86  std::shared_ptr<blockchain::BlockTree> block_tree_;
87  std::shared_ptr<BlockAnnounceObserver> observer_;
88  std::shared_ptr<PeerManager> peer_manager_;
89  };
90 
91 } // namespace kagome::network
92 
93 #endif // KAGOME_NETWORK_BLOCKANNOUNCEPROTOCOL
KAGOME_DECLARE_CACHE(BlockAnnounceProtocol, KAGOME_CACHE_UNIT(BlockAnnounce))
const application::AppConfiguration & app_config_
std::shared_ptr< blockchain::BlockTree > block_tree_
void onIncomingStream(std::shared_ptr< Stream > stream) override
outcome::result< Status > createStatus() const
#define KAGOME_CACHE_UNIT(type)
void newOutgoingStream(const PeerInfo &peer_info, std::function< void(outcome::result< std::shared_ptr< Stream >>)> &&cb) override
libp2p::peer::PeerInfo PeerInfo
void readAnnounce(std::shared_ptr< Stream > stream)
void blockAnnounce(BlockAnnounce &&announce)
void writeStatus(std::shared_ptr< Stream > stream, Direction direction, std::function< void(outcome::result< void >)> &&cb)
std::shared_ptr< StreamEngine > stream_engine_
std::shared_ptr< PeerManager > peer_manager_
const std::string & protocolName() const override
void readStatus(std::shared_ptr< Stream > stream, Direction direction, std::function< void(outcome::result< void >)> &&cb)
std::shared_ptr< BlockAnnounceObserver > observer_