Kagome
Polkadot Runtime Engine in C++17
offchain_worker_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_OFFCHAIN_OFFCHAINWORKERIMPL
7 #define KAGOME_OFFCHAIN_OFFCHAINWORKERIMPL
8 
10 
11 #include <boost/asio/io_context.hpp>
12 
14 #include "log/logger.hpp"
20 
21 namespace kagome::api {
22  class AuthorApi;
23 }
24 namespace kagome::application {
25  class AppConfiguration;
26 }
27 namespace kagome::crypto {
28  class Hasher;
29 }
30 namespace kagome::runtime {
31  class Executor;
32 }
33 using namespace std::chrono_literals;
34 
35 namespace kagome::offchain {
36  class OffchainWorkerPool;
37 
38  class OffchainWorkerImpl final
39  : public OffchainWorker,
40  public std::enable_shared_from_this<OffchainWorkerImpl> {
41  public:
42  // Duration of sleeping in loop of waiting.
43  // This value because all deadline quantized with milliseconds
44  static constexpr auto latency_of_waiting = 1ms;
45 
47  const application::AppConfiguration &app_config,
48  std::shared_ptr<clock::SystemClock> clock,
49  std::shared_ptr<crypto::Hasher> hasher,
50  std::shared_ptr<storage::BufferStorage> storage,
51  std::shared_ptr<crypto::CSPRNG> random_generator,
52  std::shared_ptr<api::AuthorApi> author_api,
53  const network::OwnPeerInfo &current_peer_info,
54  std::shared_ptr<OffchainPersistentStorage> persistent_storage,
55  std::shared_ptr<runtime::Executor> executor,
56  const primitives::BlockHeader &header,
57  std::shared_ptr<OffchainWorkerPool> ocw_pool);
58 
59  outcome::result<void> run() override;
60 
61  bool isValidator() const override;
62 
63  Result<Success, Failure> submitTransaction(
64  const primitives::Extrinsic &ext) override;
65 
66  Result<OpaqueNetworkState, Failure> networkState() override;
67 
68  Timestamp timestamp() override;
69 
70  void sleepUntil(Timestamp timestamp) override;
71 
72  RandomSeed randomSeed() override;
73 
74  void localStorageSet(StorageType storage_type,
75  const common::BufferView &key,
76  common::Buffer value) override;
77 
78  void localStorageClear(StorageType storage_type,
79  const common::BufferView &key) override;
80 
81  bool localStorageCompareAndSet(StorageType storage_type,
82  const common::BufferView &key,
83  std::optional<common::BufferView> expected,
84  common::Buffer value) override;
85 
86  outcome::result<common::Buffer> localStorageGet(
87  StorageType storage_type, const common::BufferView &key) override;
88 
89  Result<RequestId, Failure> httpRequestStart(HttpMethod method,
90  std::string_view uri,
91  common::Buffer meta) override;
92 
93  Result<Success, Failure> httpRequestAddHeader(
94  RequestId id, std::string_view name, std::string_view value) override;
95 
96  Result<Success, HttpError> httpRequestWriteBody(
97  RequestId id,
98  common::Buffer chunk,
99  std::optional<Timestamp> deadline) override;
100 
101  std::vector<HttpStatus> httpResponseWait(
102  const std::vector<RequestId> &ids,
103  std::optional<Timestamp> deadline) override;
104 
105  std::vector<std::pair<std::string, std::string>> httpResponseHeaders(
106  RequestId id) override;
107 
108  Result<uint32_t, HttpError> httpResponseReadBody(
109  RequestId id,
110  common::Buffer &chunk,
111  std::optional<Timestamp> deadline) override;
112 
113  void setAuthorizedNodes(std::vector<libp2p::peer::PeerId> nodes,
114  bool authorized_only) override;
115 
116  private:
117  OffchainStorage &getStorage(StorageType storage_type);
118 
120  std::shared_ptr<clock::SystemClock> clock_;
121  std::shared_ptr<crypto::Hasher> hasher_;
122  std::shared_ptr<crypto::CSPRNG> random_generator_;
123  std::shared_ptr<api::AuthorApi> author_api_;
125  std::shared_ptr<offchain::OffchainPersistentStorage> persistent_storage_;
126  std::shared_ptr<offchain::OffchainLocalStorage> local_storage_;
127  std::shared_ptr<runtime::Executor> executor_;
130  std::shared_ptr<OffchainWorkerPool> ocw_pool_;
132 
133  int16_t request_id_ = 0;
134  std::map<RequestId, std::shared_ptr<HttpRequest>> active_http_requests_;
135  };
136 
137 } // namespace kagome::offchain
138 
139 #endif // KAGOME_OFFCHAIN_OFFCHAINWORKERIMPL
Class represents arbitrary (including empty) byte buffer.
Definition: buffer.hpp:29
std::map< RequestId, std::shared_ptr< HttpRequest > > active_http_requests_
std::shared_ptr< api::AuthorApi > author_api_
std::shared_ptr< OffchainWorkerPool > ocw_pool_
const network::OwnPeerInfo & current_peer_info_
const primitives::BlockHeader header_
std::shared_ptr< soralog::Logger > Logger
Definition: logger.hpp:23
std::shared_ptr< crypto::Hasher > hasher_
int16_t RequestId
Definition: types.hpp:29
uint64_t Timestamp
Timestamp is milliseconds since UNIX Epoch.
Definition: types.hpp:21
std::shared_ptr< crypto::CSPRNG > random_generator_
std::shared_ptr< offchain::OffchainLocalStorage > local_storage_
std::shared_ptr< clock::SystemClock > clock_
Extrinsic class represents extrinsic.
Definition: extrinsic.hpp:24
const application::AppConfiguration & app_config_
std::shared_ptr< offchain::OffchainPersistentStorage > persistent_storage_
std::shared_ptr< runtime::Executor > executor_