Kagome
Polkadot Runtime Engine in C++17
parachain_observer.cpp
Go to the documentation of this file.
1 
7 
8 #include <gsl/span>
9 
11 #include "network/common.hpp"
14 #include "network/peer_manager.hpp"
16 
17 namespace kagome::observers {
18 
21  std::shared_ptr<network::PeerManager> pm,
22  std::shared_ptr<crypto::Sr25519Provider> crypto_provider,
23  std::shared_ptr<parachain::ParachainProcessorImpl> processor)
24  : pm_{std::move(pm)},
25  crypto_provider_{std::move(crypto_provider)},
26  processor_{std::move(processor)} {
27  BOOST_ASSERT_MSG(crypto_provider_,
28  "Crypto provider must be initialised!");
29  BOOST_ASSERT_MSG(pm_, "Peer manager must be initialised!");
30  BOOST_ASSERT_MSG(processor_, "Parachain processor must be initialised!");
31  }
32  ~CollationObserverImpl() override = default;
33 
34  void onAdvertise(libp2p::peer::PeerId const &peer_id,
35  primitives::BlockHash relay_parent) override {
36  auto &parachain_state = pm_->parachainState();
37  bool const contains_para_hash =
38  (parachain_state.our_view.count(relay_parent) != 0);
39 
40  if (!contains_para_hash) {
41  logger_->warn("Advertise collation out of view from peer {}", peer_id);
42  return;
43  }
44 
45  auto const peer_state = pm_->getPeerState(peer_id);
46  if (!peer_state) {
47  logger_->warn("Received collation advertise from unknown peer {}",
48  peer_id);
49  return;
50  }
51 
52  auto result = pm_->insert_advertisement(
53  peer_state->get(), parachain_state, std::move(relay_parent));
54  if (!result) {
55  logger_->warn("Insert advertisement from {} failed: {}",
56  peer_id,
57  result.error().message());
58  return;
59  }
60 
61  processor_->requestCollations(
62  network::PendingCollation{.para_id = result.value().second,
63  .relay_parent = relay_parent,
64  .peer_id = peer_id});
65  }
66 
67  void onDeclare(libp2p::peer::PeerId const &peer_id,
69  network::ParachainId para_id,
70  network::Signature signature) override {
71  auto const peer_state = pm_->getPeerState(peer_id);
72  if (!peer_state) {
73  logger_->warn("Received collation declaration from unknown peer {}:{}",
74  peer_id,
75  para_id);
76  return;
77  }
78 
79  if (peer_state->get().collator_state) {
80  logger_->warn("Peer is in collating state {}:{}", peer_id, para_id);
81  return;
82  }
83 
84  auto payload{peer_id.toVector()};
85  payload.insert(payload.end(), {'C', 'O', 'L', 'L'});
87 
88  if (auto result = crypto_provider_->verify(
89  signature, gsl::span<uint8_t>(payload), pubkey);
90  !result) {
91  logger_->warn("Received incorrect collation declaration from {}:{}",
92  peer_id,
93  para_id);
94  return;
95  }
96 
98  pm_->setCollating(peer_id, pubkey, para_id);
99  }
100 
101  private:
102  std::shared_ptr<network::PeerManager> pm_;
103  std::shared_ptr<crypto::Sr25519Provider> crypto_provider_;
104  std::shared_ptr<parachain::ParachainProcessorImpl> processor_;
105  log::Logger logger_ = log::createLogger("CollationObserver", "parachain");
106  };
107 
109  ReqCollationObserverImpl(std::shared_ptr<network::PeerManager> pm)
110  : pm_{std::move(pm)} {
111  BOOST_ASSERT_MSG(pm_, "Peer manager must be initialised!");
112  }
113  ~ReqCollationObserverImpl() override = default;
114 
115  outcome::result<network::CollationFetchingResponse> OnCollationRequest(
116  network::CollationFetchingRequest request) override {
119  }
120 
121  private:
122  std::shared_ptr<network::PeerManager> pm_;
123  };
124 
125 } // namespace kagome::observers
126 
127 namespace kagome::parachain {
128 
129  ParachainObserverImpl::ParachainObserverImpl(
130  std::shared_ptr<network::PeerManager> pm,
131  std::shared_ptr<crypto::Sr25519Provider> crypto_provider,
132  std::shared_ptr<parachain::ParachainProcessorImpl> processor)
133  : collation_observer_impl_{std::make_shared<
135  pm, std::move(crypto_provider), processor)},
137  std::make_shared<observers::ReqCollationObserverImpl>(pm)},
138  processor_{processor} {
139  BOOST_ASSERT_MSG(collation_observer_impl_,
140  "Collation observer must be initialised!");
141  BOOST_ASSERT_MSG(req_collation_observer_impl_,
142  "Fetch collation observer must be initialised!");
143  BOOST_ASSERT_MSG(processor_, "Parachain processor must be initialised!");
144  }
145 
147  primitives::BlockHash relay_parent) {
148  collation_observer_impl_->onAdvertise(peer_id, std::move(relay_parent));
149  }
150 
153  network::ParachainId para_id,
154  network::Signature signature) {
155  collation_observer_impl_->onDeclare(
156  peer_id, std::move(pubkey), std::move(para_id), std::move(signature));
157  }
158 
159  outcome::result<network::CollationFetchingResponse>
162  return req_collation_observer_impl_->OnCollationRequest(std::move(request));
163  }
164 
165 } // namespace kagome::parachain
void onDeclare(libp2p::peer::PeerId const &peer_id, network::CollatorPublicKey pubkey, network::ParachainId para_id, network::Signature signature) override
std::shared_ptr< network::PeerManager > pm_
outcome::result< network::CollationFetchingResponse > OnCollationRequest(network::CollationFetchingRequest request) override
fetch collation protocol observer
std::shared_ptr< crypto::Sr25519Provider > crypto_provider_
crypto::Sr25519PublicKey CollatorPublicKey
libp2p::peer::PeerId PeerId
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::shared_ptr< observers::ReqCollationObserverImpl > req_collation_observer_impl_
outcome::result< network::CollationFetchingResponse > OnCollationRequest(network::CollationFetchingRequest request) override
CollationObserverImpl(std::shared_ptr< network::PeerManager > pm, std::shared_ptr< crypto::Sr25519Provider > crypto_provider, std::shared_ptr< parachain::ParachainProcessorImpl > processor)
std::shared_ptr< network::PeerManager > pm_
void onDeclare(libp2p::peer::PeerId const &peer_id, network::CollatorPublicKey pubkey, network::ParachainId para_id, network::Signature signature) override
std::shared_ptr< parachain::ParachainProcessorImpl > processor_
ReqCollationObserverImpl(std::shared_ptr< network::PeerManager > pm)
std::shared_ptr< parachain::ParachainProcessorImpl > processor_
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
crypto::Sr25519Signature Signature
void onAdvertise(libp2p::peer::PeerId const &peer_id, primitives::BlockHash para_hash) override
collation protocol observer
void onAdvertise(libp2p::peer::PeerId const &peer_id, primitives::BlockHash relay_parent) override
std::shared_ptr< observers::CollationObserverImpl > collation_observer_impl_