Kagome
Polkadot Runtime Engine in C++17
host_api_impl.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_RUNTIME_HOSTAPIIMPL
7 #define KAGOME_RUNTIME_HOSTAPIIMPL
8 
9 #include "host_api/host_api.hpp"
10 
19 
20 namespace kagome::runtime {
21  class Core;
22  class MemoryProvider;
23  class CoreApiFactory;
24 } // namespace kagome::runtime
25 
26 namespace kagome::host_api {
27 
28  class OffchainExtension;
29 
33  class HostApiImpl : public HostApi {
34  public:
35  HostApiImpl() = delete;
37  const OffchainExtensionConfig &offchain_config,
38  std::shared_ptr<const runtime::MemoryProvider> memory_provider,
39  std::shared_ptr<const runtime::CoreApiFactory> core_provider,
40  std::shared_ptr<runtime::TrieStorageProvider> storage_provider,
41  std::shared_ptr<const crypto::Sr25519Provider> sr25519_provider,
42  std::shared_ptr<const crypto::EcdsaProvider> ecdsa_provider,
43  std::shared_ptr<const crypto::Ed25519Provider> ed25519_provider,
44  std::shared_ptr<const crypto::Secp256k1Provider> secp256k1_provider,
45  std::shared_ptr<const crypto::Hasher> hasher,
46  std::shared_ptr<crypto::CryptoStore> crypto_store,
47  std::shared_ptr<const crypto::Bip39Provider> bip39_provider,
48  std::shared_ptr<offchain::OffchainPersistentStorage>
49  offchain_persistent_storage,
50  std::shared_ptr<offchain::OffchainWorkerPool> offchain_worker_pool);
51 
52  ~HostApiImpl() override = default;
53 
54  void reset() override;
55 
56  // ------------------------ Storage extensions v1 ------------------------
57 
58  runtime::WasmSpan ext_storage_read_version_1(
60  runtime::WasmSpan value_out,
61  runtime::WasmOffset offset) override;
62 
63  void ext_storage_set_version_1(runtime::WasmSpan key,
64  runtime::WasmSpan value) override;
65 
66  runtime::WasmSpan ext_storage_get_version_1(runtime::WasmSpan key) override;
67 
68  void ext_storage_clear_version_1(runtime::WasmSpan key_data) override;
69 
70  runtime::WasmSize ext_storage_exists_version_1(
71  runtime::WasmSpan key_data) const override;
72 
73  void ext_storage_clear_prefix_version_1(runtime::WasmSpan prefix) override;
74 
75  runtime::WasmSpan ext_storage_clear_prefix_version_2(
76  runtime::WasmSpan prefix, runtime::WasmSpan limit) override;
77 
78  runtime::WasmSpan ext_storage_root_version_1() override;
79 
80  runtime::WasmSpan ext_storage_root_version_2(
81  runtime::WasmI32 state_version) override;
82 
83  runtime::WasmSpan ext_storage_changes_root_version_1(
84  runtime::WasmSpan parent_hash) override;
85 
86  void ext_storage_start_transaction_version_1() override;
87 
88  void ext_storage_rollback_transaction_version_1() override;
89 
90  void ext_storage_commit_transaction_version_1() override;
91 
92  runtime::WasmSpan ext_storage_next_key_version_1(
93  runtime::WasmSpan key) const override;
94 
95  void ext_storage_append_version_1(runtime::WasmSpan key,
96  runtime::WasmSpan value) const override;
97 
98  runtime::WasmPointer ext_trie_blake2_256_root_version_1(
99  runtime::WasmSpan values_data) override;
100 
101  runtime::WasmPointer ext_trie_blake2_256_ordered_root_version_1(
102  runtime::WasmSpan values_data) override;
103 
104  runtime::WasmPointer ext_trie_blake2_256_ordered_root_version_2(
105  runtime::WasmSpan values_data, runtime::WasmI32 state_version) override;
106 
107  // ------------------------Memory extensions v1-------------------------
108  runtime::WasmPointer ext_allocator_malloc_version_1(
109  runtime::WasmSize size) override;
110 
111  void ext_allocator_free_version_1(runtime::WasmPointer ptr) override;
112 
113  // -------------------------Crypto extensions v1---------------------
114 
115  void ext_crypto_start_batch_verify_version_1() override;
116 
117  [[nodiscard]] int32_t ext_crypto_finish_batch_verify_version_1() override;
118 
119  runtime::WasmSpan ext_crypto_secp256k1_ecdsa_recover_version_1(
120  runtime::WasmPointer sig, runtime::WasmPointer msg) override;
121 
122  runtime::WasmSpan ext_crypto_secp256k1_ecdsa_recover_version_2(
123  runtime::WasmPointer sig, runtime::WasmPointer msg) override;
124 
125  runtime::WasmSpan ext_crypto_secp256k1_ecdsa_recover_compressed_version_1(
126  runtime::WasmPointer sig, runtime::WasmPointer msg) override;
127 
128  runtime::WasmSpan ext_crypto_secp256k1_ecdsa_recover_compressed_version_2(
129  runtime::WasmPointer sig, runtime::WasmPointer msg) override;
130 
131  runtime::WasmSpan ext_crypto_ed25519_public_keys_version_1(
132  runtime::WasmSize key_type) override;
133 
134  runtime::WasmPointer ext_crypto_ed25519_generate_version_1(
135  runtime::WasmSize key_type, runtime::WasmSpan seed) override;
136 
137  runtime::WasmSpan ext_crypto_ed25519_sign_version_1(
138  runtime::WasmSize key_type,
140  runtime::WasmSpan msg_data) override;
141 
142  runtime::WasmSize ext_crypto_ed25519_verify_version_1(
143  runtime::WasmPointer sig_data,
144  runtime::WasmSpan msg,
145  runtime::WasmPointer pubkey_data) override;
146 
147  runtime::WasmSpan ext_crypto_sr25519_public_keys_version_1(
148  runtime::WasmSize key_type) override;
149 
150  runtime::WasmPointer ext_crypto_sr25519_generate_version_1(
151  runtime::WasmSize key_type, runtime::WasmSpan seed) override;
152 
153  runtime::WasmSpan ext_crypto_sr25519_sign_version_1(
154  runtime::WasmSize key_type,
156  runtime::WasmSpan msg_data) override;
157 
158  int32_t ext_crypto_sr25519_verify_version_1(
159  runtime::WasmPointer sig_data,
160  runtime::WasmSpan msg,
161  runtime::WasmPointer pubkey_data) override;
162 
163  int32_t ext_crypto_sr25519_verify_version_2(runtime::WasmPointer,
165  runtime::WasmPointer) override;
166 
167  runtime::WasmSpan ext_crypto_ecdsa_public_keys_version_1(
168  runtime::WasmSize key_type) override;
169 
170  runtime::WasmSpan ext_crypto_ecdsa_sign_version_1(
171  runtime::WasmSize key_type,
173  runtime::WasmSpan msg_data) override;
174 
175  runtime::WasmSpan ext_crypto_ecdsa_sign_prehashed_version_1(
176  runtime::WasmSize key_type,
178  runtime::WasmSpan msg_data) override;
179 
180  runtime::WasmPointer ext_crypto_ecdsa_generate_version_1(
181  runtime::WasmSize key_type_id, runtime::WasmSpan seed) override;
182 
183  int32_t ext_crypto_ecdsa_verify_version_1(
185  runtime::WasmSpan msg,
186  runtime::WasmPointer key) override;
187 
188  int32_t ext_crypto_ecdsa_verify_prehashed_version_1(
190  runtime::WasmSpan msg,
191  runtime::WasmPointer key) override;
192 
193  // ------------------------- Hashing extension/crypto ---------------
194 
195  runtime::WasmPointer ext_hashing_keccak_256_version_1(
196  runtime::WasmSpan data) override;
197 
198  runtime::WasmPointer ext_hashing_sha2_256_version_1(
199  runtime::WasmSpan data) override;
200 
201  runtime::WasmPointer ext_hashing_blake2_128_version_1(
202  runtime::WasmSpan data) override;
203 
204  runtime::WasmPointer ext_hashing_blake2_256_version_1(
205  runtime::WasmSpan data) override;
206 
207  runtime::WasmPointer ext_hashing_twox_64_version_1(
208  runtime::WasmSpan data) override;
209 
210  runtime::WasmPointer ext_hashing_twox_128_version_1(
211  runtime::WasmSpan data) override;
212 
213  runtime::WasmPointer ext_hashing_twox_256_version_1(
214  runtime::WasmSpan data) override;
215 
216  // -------------------------Misc extensions--------------------------
217 
218  void ext_logging_log_version_1(runtime::WasmEnum level,
219  runtime::WasmSpan target,
220  runtime::WasmSpan message) override;
221 
222  runtime::WasmEnum ext_logging_max_level_version_1() override;
223 
224  runtime::WasmSpan ext_misc_runtime_version_version_1(
225  runtime::WasmSpan data) const override;
226 
227  void ext_misc_print_hex_version_1(runtime::WasmSpan data) const override;
228 
229  void ext_misc_print_num_version_1(uint64_t value) const override;
230 
231  void ext_misc_print_utf8_version_1(runtime::WasmSpan data) const override;
232 
233  // -------------------------- Offchain extension ---------------------------
234 
235  runtime::WasmI32 ext_offchain_is_validator_version_1() override;
236 
237  runtime::WasmSpan ext_offchain_submit_transaction_version_1(
238  runtime::WasmSpan data) override;
239 
240  runtime::WasmSpan ext_offchain_network_state_version_1() override;
241 
242  runtime::WasmU64 ext_offchain_timestamp_version_1() override;
243 
244  void ext_offchain_sleep_until_version_1(runtime::WasmU64 deadline) override;
245 
246  runtime::WasmPointer ext_offchain_random_seed_version_1() override;
247 
248  void ext_offchain_local_storage_set_version_1(
249  runtime::WasmI32 kind,
250  runtime::WasmSpan key,
251  runtime::WasmSpan value) override;
252 
253  void ext_offchain_local_storage_clear_version_1(
254  runtime::WasmI32 kind, runtime::WasmSpan key) override;
255 
256  runtime::WasmI32 ext_offchain_local_storage_compare_and_set_version_1(
257  runtime::WasmI32 kind,
258  runtime::WasmSpan key,
259  runtime::WasmSpan expected,
260  runtime::WasmSpan value) override;
261 
262  runtime::WasmSpan ext_offchain_local_storage_get_version_1(
263  runtime::WasmI32 kind, runtime::WasmSpan key) override;
264 
265  runtime::WasmSpan ext_offchain_http_request_start_version_1(
266  runtime::WasmSpan method,
267  runtime::WasmSpan uri,
268  runtime::WasmSpan meta) override;
269 
270  runtime::WasmSpan ext_offchain_http_request_add_header_version_1(
271  runtime::WasmI32 request_id,
272  runtime::WasmSpan name,
273  runtime::WasmSpan value) override;
274 
275  runtime::WasmSpan ext_offchain_http_request_write_body_version_1(
276  runtime::WasmI32 request_id,
277  runtime::WasmSpan chunk,
278  runtime::WasmSpan deadline) override;
279 
280  runtime::WasmSpan ext_offchain_http_response_wait_version_1(
281  runtime::WasmSpan ids, runtime::WasmSpan deadline) override;
282 
283  runtime::WasmSpan ext_offchain_http_response_headers_version_1(
284  runtime::WasmI32 request_id) override;
285 
286  runtime::WasmSpan ext_offchain_http_response_read_body_version_1(
287  runtime::WasmI32 request_id,
288  runtime::WasmSpan buffer,
289  runtime::WasmSpan deadline) override;
290 
291  void ext_offchain_set_authorized_nodes_version_1(
292  runtime::WasmSpan nodes, runtime::WasmI32 authorized_only) override;
293 
294  void ext_offchain_index_set_version_1(runtime::WasmSpan key,
295  runtime::WasmSpan value) override;
296 
297  void ext_offchain_index_clear_version_1(runtime::WasmSpan key) override;
298 
299  // ---------------------------- Child Storage ----------------------------
300 
301  void ext_default_child_storage_set_version_1(
302  runtime::WasmSpan child_storage_key,
303  runtime::WasmSpan key,
304  runtime::WasmSpan value) override;
305 
306  runtime::WasmSpan ext_default_child_storage_get_version_1(
307  runtime::WasmSpan child_storage_key,
308  runtime::WasmSpan key) const override;
309 
310  void ext_default_child_storage_clear_version_1(
311  runtime::WasmSpan child_storage_key, runtime::WasmSpan key) override;
312 
313  runtime::WasmSpan ext_default_child_storage_next_key_version_1(
314  runtime::WasmSpan child_storage_key,
315  runtime::WasmSpan key) const override;
316 
317  runtime::WasmSpan ext_default_child_storage_root_version_1(
318  runtime::WasmSpan child_storage_key) const override;
319 
320  void ext_default_child_storage_clear_prefix_version_1(
321  runtime::WasmSpan child_storage_key, runtime::WasmSpan prefix) override;
322 
323  virtual runtime::WasmSpan ext_default_child_storage_read_version_1(
324  runtime::WasmSpan child_storage_key,
325  runtime::WasmSpan key,
326  runtime::WasmSpan value_out,
327  runtime::WasmOffset offset) const override;
328 
329  virtual uint32_t ext_default_child_storage_exists_version_1(
330  runtime::WasmSpan child_storage_key,
331  runtime::WasmSpan key) const override;
332 
333  virtual void ext_default_child_storage_storage_kill_version_1(
334  runtime::WasmSpan child_storage_key) override;
335 
336  private:
337  static constexpr uint64_t DEFAULT_CHAIN_ID = 42;
338 
339  std::shared_ptr<const runtime::MemoryProvider> memory_provider_;
340  std::shared_ptr<runtime::TrieStorageProvider> storage_provider_;
341 
349  };
350 } // namespace kagome::host_api
351 
352 #endif // KAGOME_RUNTIME_HOSTAPIIMPL
uint64_t WasmU64
Definition: types.hpp:47
uint32_t WasmSize
Size type is uint32_t because we are working in 32 bit address space.
Definition: types.hpp:35
std::shared_ptr< runtime::TrieStorageProvider > storage_provider_
uint32_t WasmOffset
Offset type is uint32_t because we are working in 32 bit address space.
Definition: types.hpp:44
int32_t WasmI32
Definition: types.hpp:46
std::shared_ptr< const runtime::MemoryProvider > memory_provider_
uint64_t WasmSpan
combination of pointer and size, where less significant part represents wasm pointer, and most significant represents size
Definition: types.hpp:31
uint32_t WasmEnum
Enum value is uint32_t.
Definition: types.hpp:39
ChildStorageExtension child_storage_ext_
uint32_t WasmPointer
type of wasm memory is 32 bit integer
Definition: types.hpp:26