Kagome
Polkadot Runtime Engine in C++17
signer.cpp
Go to the documentation of this file.
1 
7 
8 #include "log/logger.hpp"
10 
11 namespace kagome::parachain {
12  constexpr std::chrono::milliseconds kDelay{1500};
13 
14  namespace {
15  inline auto log() {
16  return log::createLogger("BitfieldSigner");
17  }
18  } // namespace
19 
21  std::shared_ptr<crypto::Hasher> hasher,
22  std::shared_ptr<ValidatorSignerFactory> signer_factory,
23  std::shared_ptr<libp2p::basic::Scheduler> scheduler,
24  std::shared_ptr<runtime::ParachainHost> parachain_api,
25  std::shared_ptr<AvailabilityStore> store,
26  std::shared_ptr<BitfieldStore> bitfield_store)
27  : hasher_{std::move(hasher)},
28  signer_factory_{std::move(signer_factory)},
29  scheduler_{std::move(scheduler)},
30  parachain_api_{std::move(parachain_api)},
31  store_{std::move(store)},
32  bitfield_store_{std::move(bitfield_store)} {}
33 
35  std::shared_ptr<primitives::events::ChainSubscriptionEngine>
36  chain_sub_engine) {
37  chain_sub_ = std::make_shared<primitives::events::ChainEventSubscriber>(
38  chain_sub_engine);
39  chain_sub_->subscribe(chain_sub_->generateSubscriptionSetId(),
41  chain_sub_->setCallback(
42  [weak = weak_from_this()](
44  auto &&,
47  if (auto self = weak.lock()) {
48  auto r = self->onBlock(self->hasher_->blake2b_256(
49  scale::encode(
50  boost::get<primitives::events::HeadsEventParams>(event))
51  .value()));
52  if (r.has_error()) {
53  SL_WARN(log(), "onBlock error {}", r.error());
54  }
55  }
56  });
57  }
58 
59  outcome::result<void> BitfieldSigner::sign(const ValidatorSigner &signer) {
60  auto &relay_parent = signer.relayParent();
61  scale::BitVec bitfield;
62  OUTCOME_TRY(cores, parachain_api_->availability_cores(relay_parent));
63  bitfield.bits.reserve(cores.size());
64  for (auto &core : cores) {
65  auto occupied = boost::get<runtime::OccupiedCore>(&core);
66  bitfield.bits.push_back(occupied != nullptr
67  && store_->hasChunk(occupied->candidate_hash,
68  signer.validatorIndex()));
69  }
70 
71  OUTCOME_TRY(signed_bitfield, signer.sign(bitfield));
72  bitfield_store_->putBitfield(relay_parent, signed_bitfield);
73  // TODO(turuslan): broadcast
74  return outcome::success();
75  }
76 
77  outcome::result<void> BitfieldSigner::onBlock(const BlockHash &relay_parent) {
78  OUTCOME_TRY(signer, signer_factory_->at(relay_parent));
79  if (not signer.has_value()) {
80  return outcome::success();
81  }
82  scheduler_->schedule(
83  [weak = weak_from_this(), relay_parent, signer{std::move(*signer)}]() {
84  if (auto self = weak.lock()) {
85  auto r = self->sign(signer);
86  if (r.has_error()) {
87  SL_WARN(log(), "sign error {}", r.error());
88  }
89  }
90  },
91  kDelay);
92  return outcome::success();
93  }
94 } // namespace kagome::parachain
std::shared_ptr< runtime::ParachainHost > parachain_api_
Definition: signer.hpp:44
std::shared_ptr< BitfieldStore > bitfield_store_
Definition: signer.hpp:46
outcome::result< void > sign(const ValidatorSigner &signer)
Sign bitfield for given block.
Definition: signer.cpp:59
ValidatorIndex validatorIndex() const
Get validator index.
Definition: signer.cpp:29
std::shared_ptr< libp2p::basic::Scheduler > scheduler_
Definition: signer.hpp:43
outcome::result< void > onBlock(const BlockHash &relay_parent)
Definition: signer.cpp:77
std::shared_ptr< primitives::events::ChainEventSubscriber > chain_sub_
Definition: signer.hpp:47
boost::variant< std::nullopt_t, HeadsEventParams, RuntimeVersionEventParams, NewRuntimeEventParams > ChainEventParams
Definition: event_types.hpp:51
std::shared_ptr< AvailabilityStore > store_
Definition: signer.hpp:45
const primitives::BlockHash & relayParent() const
Get relay parent hash.
Definition: signer.cpp:33
BitfieldSigner(std::shared_ptr< crypto::Hasher > hasher, std::shared_ptr< ValidatorSignerFactory > signer_factory, std::shared_ptr< libp2p::basic::Scheduler > scheduler, std::shared_ptr< runtime::ParachainHost > parachain_api, std::shared_ptr< AvailabilityStore > store, std::shared_ptr< BitfieldStore > bitfield_store)
Definition: signer.cpp:20
constexpr std::chrono::milliseconds kDelay
Definition: signer.cpp:12
Signs payload with signing context and validator keypair.
Definition: signer.hpp:52
outcome::result< network::Signed< T > > sign(T payload) const
Sign payload.
Definition: signer.hpp:64
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
std::shared_ptr< ValidatorSignerFactory > signer_factory_
Definition: signer.hpp:42
void start(std::shared_ptr< primitives::events::ChainSubscriptionEngine > chain_sub_engine)
Subscribes to new heads.
Definition: signer.cpp:34