Kagome
Polkadot Runtime Engine in C++17
service.cpp
Go to the documentation of this file.
1 
6 #include "telemetry/service.hpp"
7 
8 #include <boost/assert.hpp>
9 
10 namespace kagome::telemetry {
11 
12  namespace {
13 
14  class TelemetryInstanceProxy : public TelemetryService {
15  public:
16  void setGenesisBlockHash(const primitives::BlockHash &hash) override {
17  if (service_) {
18  service_->setGenesisBlockHash(hash);
19  }
20  genesis_hash_ = hash;
21  }
22  void notifyBlockImported(const primitives::BlockInfo &info,
23  BlockOrigin origin) override {
24  if (service_) {
25  service_->notifyBlockImported(info, origin);
26  }
27  }
28  void notifyBlockFinalized(const primitives::BlockInfo &info) override {
29  if (service_) {
30  service_->notifyBlockFinalized(info);
31  }
32  }
33  void notifyWasSynchronized() override {
34  if (service_) {
35  service_->notifyWasSynchronized();
36  }
37  was_synchronized_ = true;
38  }
39  bool isEnabled() const override {
40  return false;
41  }
42 
43  void setActualImplementation(Telemetry service) {
44  service_ = std::move(service);
45  if (was_synchronized_) {
46  service_->notifyWasSynchronized();
47  }
48  service_->setGenesisBlockHash(genesis_hash_);
49  }
50 
51  private:
52  bool was_synchronized_ = false;
53  primitives::BlockHash genesis_hash_{};
54  Telemetry service_;
55  };
56 
57  // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
58  std::shared_ptr<TelemetryInstanceProxy> telemetry_service_;
59 
60  } // namespace
61 
62  void setTelemetryService(std::shared_ptr<TelemetryService> service) {
63  if (not telemetry_service_) {
64  telemetry_service_ = std::make_shared<TelemetryInstanceProxy>();
65  }
66  telemetry_service_->setActualImplementation(std::move(service));
67  }
68 
69  std::shared_ptr<TelemetryService> createTelemetryService() {
70  if (not telemetry_service_) {
71  telemetry_service_ = std::make_shared<TelemetryInstanceProxy>();
72  }
73  return telemetry_service_;
74  }
75 
76 } // namespace kagome::telemetry
std::shared_ptr< TelemetryService > createTelemetryService()
Returns preliminary initialized instance of telemetry service.
Definition: service.cpp:69
common::Hash256 BlockHash
Definition: block_id.hpp:15
detail::BlockInfoT< struct BlockInfoTag > BlockInfo
Definition: common.hpp:63
void setTelemetryService(std::shared_ptr< TelemetryService > service)
Definition: service.cpp:62
std::shared_ptr< TelemetryService > Telemetry
Definition: service.hpp:86