Kagome
Polkadot Runtime Engine in C++17
digest_tracker_impl.cpp
Go to the documentation of this file.
1 
7 
8 #include "common/visitor.hpp"
11 
12 namespace kagome::blockchain {
13 
15  std::shared_ptr<consensus::BabeDigestObserver> babe_update_observer,
16  std::shared_ptr<authority::AuthorityUpdateObserver>
17  authority_update_observer)
18  : babe_digest_observer_(std::move(babe_update_observer)),
19  authority_update_observer_(std::move(authority_update_observer)),
20  logger_(log::createLogger("DigestTracker", "digest_tracker")) {
21  BOOST_ASSERT(babe_digest_observer_ != nullptr);
22  BOOST_ASSERT(authority_update_observer_ != nullptr);
23  }
24 
25  outcome::result<void> DigestTrackerImpl::onDigest(
26  const primitives::BlockInfo &block, const primitives::Digest &digest) {
27  SL_TRACE(logger_, "Start process digest on block {}", block);
28  for (auto &item : digest) {
29  auto res = visit_in_place(
30  item,
31  [&](const primitives::Consensus &item) {
32  SL_TRACE(logger_,
33  "Consensus-digest on block {}, engine '{}'",
34  block,
36  return onConsensus(block, item);
37  },
38  [&](const primitives::Seal &item) {
39  SL_TRACE(logger_,
40  "Seal-digest on block {}, engine '{}'",
41  block,
43  return outcome::success(); // It does not processed by tracker
44  },
45  [&](const primitives::PreRuntime &item) {
46  SL_TRACE(logger_,
47  "PreRuntime-digest on block {}, engine '{}'",
48  block,
50  return onPreRuntime(block, item);
51  },
52  [&](const primitives::RuntimeEnvironmentUpdated &item) {
53  SL_TRACE(
54  logger_, "RuntimeEnvironmentUpdated-digest on block {}", block);
55  return outcome::success(); // It does not processed by tracker
56  },
57  [&](const auto &) {
58  SL_WARN(logger_,
59  "Unsupported digest on block {}: variant #{}",
60  block,
61  item.which());
62  return outcome::success();
63  });
64  OUTCOME_TRY(res);
65  }
66  SL_TRACE(logger_, "End process digest on block {}", block);
67  return outcome::success();
68  }
69 
71  // Cancel tracked babe digest
72  babe_digest_observer_->cancel(block);
73 
74  // Cancel tracked grandpa digest
75  authority_update_observer_->cancel(block);
76  }
77 
78  outcome::result<void> DigestTrackerImpl::onConsensus(
79  const primitives::BlockInfo &block,
80  const primitives::Consensus &message) {
82  return authority_update_observer_->onConsensus(block, message);
83 
84  // TODO(xDimon): Refactor AuthorityManager to accept grandpa-digest
85  // OUTCOME_TRY(digest,
86  // scale::decode<primitives::GrandpaDigest>(message.data));
87  //
88  // return authority_update_observer_->onDigest(block, digest);
89 
90  } else if (message.consensus_engine_id == primitives::kBabeEngineId) {
91  OUTCOME_TRY(digest, scale::decode<primitives::BabeDigest>(message.data));
92 
93  return babe_digest_observer_->onDigest(block, digest);
94 
95  } else if (message.consensus_engine_id
97  or message.consensus_engine_id
99  SL_DEBUG(logger_,
100  "Unsupported consensus engine id in block {}: {}",
101  block,
102  message.consensus_engine_id.toString());
103  return outcome::success();
104 
105  } else {
106  SL_WARN(logger_,
107  "Unknown consensus engine id in block {}: {}",
108  block,
109  message.consensus_engine_id.toString());
110  return outcome::success();
111  }
112  }
113 
114  outcome::result<void> DigestTrackerImpl::onPreRuntime(
115  const primitives::BlockInfo &block,
116  const primitives::PreRuntime &message) {
118  OUTCOME_TRY(digest,
119  scale::decode<consensus::BabeBlockHeader>(message.data));
120 
121  return babe_digest_observer_->onDigest(block, digest);
122  } else {
123  SL_WARN(logger_,
124  "Unknown consensus engine id in block {}: {}",
125  block,
126  message.consensus_engine_id.toString());
127  return outcome::success();
128  }
129  }
130 
131 } // namespace kagome::blockchain
Runtime code or heap pages updated.
Definition: digest.hpp:168
STL namespace.
std::shared_ptr< authority::AuthorityUpdateObserver > authority_update_observer_
outcome::result< void > onDigest(const primitives::BlockInfo &block, const primitives::Digest &digest) override
const auto kUnsupportedEngineId_POL1
Definition: digest.hpp:31
std::shared_ptr< consensus::BabeDigestObserver > babe_digest_observer_
const auto kUnsupportedEngineId_BEEF
Definition: digest.hpp:34
std::string toString() const noexcept
Definition: blob.hpp:153
outcome::result< void > onConsensus(const primitives::BlockInfo &block, const primitives::Consensus &consensus_message)
outcome::result< void > onPreRuntime(const primitives::BlockInfo &block, const primitives::PreRuntime &message)
common::SLBuffer< consensus::kMaxValidatorsNumber *1024 > data
Definition: digest.hpp:45
const auto kGrandpaEngineId
Definition: digest.hpp:28
const auto kBabeEngineId
Definition: digest.hpp:25
Logger createLogger(const std::string &tag)
Definition: logger.cpp:112
void cancel(const primitives::BlockInfo &block) override
DigestTrackerImpl(std::shared_ptr< consensus::BabeDigestObserver > babe_update_observer, std::shared_ptr< authority::AuthorityUpdateObserver > authority_update_observer)