Skip to main content

sp_io/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! # Substrate Primitives: IO
19//!
20//! This crate contains interfaces for the runtime to communicate with the outside world, ergo `io`.
21//! In other context, such interfaces are referred to as "**host functions**".
22//!
23//! Each set of host functions are defined with an instance of the
24//! [`sp_runtime_interface::runtime_interface`] macro.
25//!
26//! Most notably, this crate contains host functions for:
27//!
28//! - [`hashing`]
29//! - [`crypto`]
30//! - [`trie`]
31//! - [`offchain`]
32//! - [`storage`]
33//! - [`allocator`]
34//! - [`logging`]
35//!
36//! All of the default host functions provided by this crate, and by default contained in all
37//! substrate-based clients are amalgamated in [`SubstrateHostFunctions`].
38//!
39//! ## Externalities
40//!
41//! Host functions go hand in hand with the concept of externalities. Externalities are an
42//! environment in which host functions are provided, and thus can be accessed. Some host functions
43//! are only accessible in an externality environment that provides it.
44//!
45//! A typical error for substrate developers is the following:
46//!
47//! ```should_panic
48//! use sp_io::storage::get;
49//! # fn main() {
50//! let data = get(b"hello world");
51//! # }
52//! ```
53//!
54//! This code will panic with the following error:
55//!
56//! ```no_compile
57//! thread 'main' panicked at '`get_version_1` called outside of an Externalities-provided environment.'
58//! ```
59//!
60//! Such error messages should always be interpreted as "code accessing host functions accessed
61//! outside of externalities".
62//!
63//! An externality is any type that implements [`sp_externalities::Externalities`]. A simple example
64//! of which is [`TestExternalities`], which is commonly used in tests and is exported from this
65//! crate.
66//!
67//! ```
68//! use sp_io::{storage::get, TestExternalities};
69//! # fn main() {
70//! TestExternalities::default().execute_with(|| {
71//! 	let data = get(b"hello world");
72//! });
73//! # }
74//! ```
75
76#![warn(missing_docs)]
77#![cfg_attr(not(feature = "std"), no_std)]
78#![cfg_attr(enable_alloc_error_handler, feature(alloc_error_handler))]
79
80extern crate alloc;
81
82use alloc::vec::Vec;
83
84#[cfg(not(substrate_runtime))]
85use tracing;
86
87#[cfg(not(substrate_runtime))]
88use sp_core::{
89	crypto::Pair,
90	hexdisplay::HexDisplay,
91	offchain::{OffchainDbExt, OffchainWorkerExt, TransactionPoolExt},
92	storage::ChildInfo,
93};
94#[cfg(not(substrate_runtime))]
95use sp_keystore::KeystoreExt;
96
97#[cfg(feature = "bandersnatch-experimental")]
98use sp_core::bandersnatch;
99use sp_core::{
100	crypto::KeyTypeId,
101	ecdsa, ed25519,
102	offchain::{
103		HttpError, HttpRequestId, HttpRequestStatus, OpaqueNetworkState, StorageKind, Timestamp,
104	},
105	sr25519,
106	storage::StateVersion,
107	LogLevelFilter, OpaquePeerId, RuntimeInterfaceLogLevel, H256,
108};
109
110#[cfg(feature = "bls-experimental")]
111use sp_core::{bls381, ecdsa_bls381};
112
113#[cfg(not(substrate_runtime))]
114use sp_trie::{LayoutV0, LayoutV1, TrieConfiguration};
115
116use sp_runtime_interface::{
117	pass_by::{
118		AllocateAndReturnByCodec, AllocateAndReturnFatPointer, AllocateAndReturnPointer, PassAs,
119		PassFatPointerAndDecode, PassFatPointerAndDecodeSlice, PassFatPointerAndRead,
120		PassFatPointerAndReadWrite, PassPointerAndRead, PassPointerAndReadCopy, ReturnAs,
121	},
122	runtime_interface, Pointer,
123};
124
125use codec::{Decode, Encode};
126
127#[cfg(not(substrate_runtime))]
128use secp256k1::{
129	ecdsa::{RecoverableSignature, RecoveryId},
130	Message,
131};
132
133#[cfg(not(substrate_runtime))]
134use sp_externalities::{Externalities, ExternalitiesExt};
135
136pub use sp_externalities::MultiRemovalResults;
137
138#[cfg(all(not(feature = "disable_allocator"), substrate_runtime))]
139mod global_alloc;
140
141#[cfg(not(substrate_runtime))]
142const LOG_TARGET: &str = "runtime::io";
143
144/// Error verifying ECDSA signature
145#[derive(Encode, Decode)]
146pub enum EcdsaVerifyError {
147	/// Incorrect value of R or S
148	BadRS,
149	/// Incorrect value of V
150	BadV,
151	/// Invalid signature
152	BadSignature,
153}
154
155/// The outcome of calling `storage_kill`. Returned value is the number of storage items
156/// removed from the backend from making the `storage_kill` call.
157#[derive(Encode, Decode)]
158pub enum KillStorageResult {
159	/// All keys to remove were removed, return number of iterations performed during the
160	/// operation.
161	AllRemoved(u32),
162	/// Not all key to remove were removed, return number of iterations performed during the
163	/// operation.
164	SomeRemaining(u32),
165}
166
167impl From<MultiRemovalResults> for KillStorageResult {
168	fn from(r: MultiRemovalResults) -> Self {
169		// We use `loops` here rather than `backend` because that's the same as the original
170		// functionality pre-#11490. This won't matter once we switch to the new host function
171		// since we won't be using the `KillStorageResult` type in the runtime any more.
172		match r.maybe_cursor {
173			None => Self::AllRemoved(r.loops),
174			Some(..) => Self::SomeRemaining(r.loops),
175		}
176	}
177}
178
179/// Interface for accessing the storage from within the runtime.
180#[runtime_interface]
181pub trait Storage {
182	/// Returns the data for `key` in the storage or `None` if the key can not be found.
183	fn get(
184		&mut self,
185		key: PassFatPointerAndRead<&[u8]>,
186	) -> AllocateAndReturnByCodec<Option<bytes::Bytes>> {
187		self.storage(key).map(|s| bytes::Bytes::from(s.to_vec()))
188	}
189
190	/// Get `key` from storage, placing the value into `value_out` and return the number of
191	/// bytes that the entry in storage has beyond the offset or `None` if the storage entry
192	/// doesn't exist at all.
193	/// If `value_out` length is smaller than the returned length, only `value_out` length bytes
194	/// are copied into `value_out`.
195	fn read(
196		&mut self,
197		key: PassFatPointerAndRead<&[u8]>,
198		value_out: PassFatPointerAndReadWrite<&mut [u8]>,
199		value_offset: u32,
200	) -> AllocateAndReturnByCodec<Option<u32>> {
201		self.storage(key).map(|value| {
202			let value_offset = value_offset as usize;
203			let data = &value[value_offset.min(value.len())..];
204			let written = core::cmp::min(data.len(), value_out.len());
205			value_out[..written].copy_from_slice(&data[..written]);
206			data.len() as u32
207		})
208	}
209
210	/// Set `key` to `value` in the storage.
211	fn set(&mut self, key: PassFatPointerAndRead<&[u8]>, value: PassFatPointerAndRead<&[u8]>) {
212		self.set_storage(key.to_vec(), value.to_vec());
213	}
214
215	/// Clear the storage of the given `key` and its value.
216	fn clear(&mut self, key: PassFatPointerAndRead<&[u8]>) {
217		self.clear_storage(key)
218	}
219
220	/// Check whether the given `key` exists in storage.
221	fn exists(&mut self, key: PassFatPointerAndRead<&[u8]>) -> bool {
222		self.exists_storage(key)
223	}
224
225	/// Clear the storage of each key-value pair where the key starts with the given `prefix`.
226	fn clear_prefix(&mut self, prefix: PassFatPointerAndRead<&[u8]>) {
227		let _ = Externalities::clear_prefix(*self, prefix, None, None);
228	}
229
230	/// Clear the storage of each key-value pair where the key starts with the given `prefix`.
231	///
232	/// # Limit
233	///
234	/// Deletes all keys from the overlay and up to `limit` keys from the backend if
235	/// it is set to `Some`. No limit is applied when `limit` is set to `None`.
236	///
237	/// The limit can be used to partially delete a prefix storage in case it is too large
238	/// to delete in one go (block).
239	///
240	/// Returns [`KillStorageResult`] to inform about the result.
241	///
242	/// # Note
243	///
244	/// Please note that keys that are residing in the overlay for that prefix when
245	/// issuing this call are all deleted without counting towards the `limit`. Only keys
246	/// written during the current block are part of the overlay. Deleting with a `limit`
247	/// mostly makes sense with an empty overlay for that prefix.
248	///
249	/// Calling this function multiple times per block for the same `prefix` does
250	/// not make much sense because it is not cumulative when called inside the same block.
251	/// The deletion would always start from `prefix` resulting in the same keys being deleted
252	/// every time this function is called with the exact same arguments per block. This happens
253	/// because the keys in the overlay are not taken into account when deleting keys in the
254	/// backend.
255	#[version(2)]
256	fn clear_prefix(
257		&mut self,
258		prefix: PassFatPointerAndRead<&[u8]>,
259		limit: PassFatPointerAndDecode<Option<u32>>,
260	) -> AllocateAndReturnByCodec<KillStorageResult> {
261		Externalities::clear_prefix(*self, prefix, limit, None).into()
262	}
263
264	/// Partially clear the storage of each key-value pair where the key starts with the given
265	/// prefix.
266	///
267	/// # Limit
268	///
269	/// A *limit* should always be provided through `maybe_limit`. This is one fewer than the
270	/// maximum number of backend iterations which may be done by this operation and as such
271	/// represents the maximum number of backend deletions which may happen. A *limit* of zero
272	/// implies that no keys will be deleted, though there may be a single iteration done.
273	///
274	/// The limit can be used to partially delete a prefix storage in case it is too large or costly
275	/// to delete in a single operation.
276	///
277	/// # Cursor
278	///
279	/// A *cursor* may be passed in to this operation with `maybe_cursor`. `None` should only be
280	/// passed once (in the initial call) for any given `maybe_prefix` value. Subsequent calls
281	/// operating on the same prefix should always pass `Some`, and this should be equal to the
282	/// previous call result's `maybe_cursor` field.
283	///
284	/// Returns [`MultiRemovalResults`](sp_io::MultiRemovalResults) to inform about the result. Once
285	/// the resultant `maybe_cursor` field is `None`, then no further items remain to be deleted.
286	///
287	/// NOTE: After the initial call for any given prefix, it is important that no keys further
288	/// keys under the same prefix are inserted. If so, then they may or may not be deleted by
289	/// subsequent calls.
290	///
291	/// # Note
292	///
293	/// Please note that keys which are residing in the overlay for that prefix when
294	/// issuing this call are deleted without counting towards the `limit`.
295	#[version(3, register_only)]
296	fn clear_prefix(
297		&mut self,
298		maybe_prefix: PassFatPointerAndRead<&[u8]>,
299		maybe_limit: PassFatPointerAndDecode<Option<u32>>,
300		maybe_cursor: PassFatPointerAndDecode<Option<Vec<u8>>>, /* TODO Make work or just
301		                                                         * Option<Vec<u8>>? */
302	) -> AllocateAndReturnByCodec<MultiRemovalResults> {
303		Externalities::clear_prefix(
304			*self,
305			maybe_prefix,
306			maybe_limit,
307			maybe_cursor.as_ref().map(|x| &x[..]),
308		)
309		.into()
310	}
311
312	/// Append the encoded `value` to the storage item at `key`.
313	///
314	/// The storage item needs to implement [`EncodeAppend`](codec::EncodeAppend).
315	///
316	/// # Warning
317	///
318	/// If the storage item does not support [`EncodeAppend`](codec::EncodeAppend) or
319	/// something else fails at appending, the storage item will be set to `[value]`.
320	fn append(&mut self, key: PassFatPointerAndRead<&[u8]>, value: PassFatPointerAndRead<Vec<u8>>) {
321		self.storage_append(key.to_vec(), value);
322	}
323
324	/// "Commit" all existing operations and compute the resulting storage root.
325	///
326	/// The hashing algorithm is defined by the `Block`.
327	///
328	/// Returns a `Vec<u8>` that holds the SCALE encoded hash.
329	fn root(&mut self) -> AllocateAndReturnFatPointer<Vec<u8>> {
330		self.storage_root(StateVersion::V0)
331	}
332
333	/// "Commit" all existing operations and compute the resulting storage root.
334	///
335	/// The hashing algorithm is defined by the `Block`.
336	///
337	/// Returns a `Vec<u8>` that holds the SCALE encoded hash.
338	#[version(2)]
339	fn root(&mut self, version: PassAs<StateVersion, u8>) -> AllocateAndReturnFatPointer<Vec<u8>> {
340		self.storage_root(version)
341	}
342
343	/// Always returns `None`. This function exists for compatibility reasons.
344	fn changes_root(
345		&mut self,
346		_parent_hash: PassFatPointerAndRead<&[u8]>,
347	) -> AllocateAndReturnByCodec<Option<Vec<u8>>> {
348		None
349	}
350
351	/// Get the next key in storage after the given one in lexicographic order.
352	fn next_key(
353		&mut self,
354		key: PassFatPointerAndRead<&[u8]>,
355	) -> AllocateAndReturnByCodec<Option<Vec<u8>>> {
356		self.next_storage_key(key)
357	}
358
359	/// Start a new nested transaction.
360	///
361	/// This allows to either commit or roll back all changes that are made after this call.
362	/// For every transaction there must be a matching call to either `rollback_transaction`
363	/// or `commit_transaction`. This is also effective for all values manipulated using the
364	/// `DefaultChildStorage` API.
365	///
366	/// # Warning
367	///
368	/// This is a low level API that is potentially dangerous as it can easily result
369	/// in unbalanced transactions. For example, FRAME users should use high level storage
370	/// abstractions.
371	fn start_transaction(&mut self) {
372		self.storage_start_transaction();
373	}
374
375	/// Rollback the last transaction started by `start_transaction`.
376	///
377	/// Any changes made during that transaction are discarded.
378	///
379	/// # Panics
380	///
381	/// Will panic if there is no open transaction.
382	fn rollback_transaction(&mut self) {
383		self.storage_rollback_transaction()
384			.expect("No open transaction that can be rolled back.");
385	}
386
387	/// Commit the last transaction started by `start_transaction`.
388	///
389	/// Any changes made during that transaction are committed.
390	///
391	/// # Panics
392	///
393	/// Will panic if there is no open transaction.
394	fn commit_transaction(&mut self) {
395		self.storage_commit_transaction()
396			.expect("No open transaction that can be committed.");
397	}
398}
399
400/// Interface for accessing the child storage for default child trie,
401/// from within the runtime.
402#[runtime_interface]
403pub trait DefaultChildStorage {
404	/// Get a default child storage value for a given key.
405	///
406	/// Parameter `storage_key` is the unprefixed location of the root of the child trie in the
407	/// parent trie. Result is `None` if the value for `key` in the child storage can not be found.
408	fn get(
409		&mut self,
410		storage_key: PassFatPointerAndRead<&[u8]>,
411		key: PassFatPointerAndRead<&[u8]>,
412	) -> AllocateAndReturnByCodec<Option<Vec<u8>>> {
413		let child_info = ChildInfo::new_default(storage_key);
414		self.child_storage(&child_info, key).map(|s| s.to_vec())
415	}
416
417	/// Allocation efficient variant of `get`.
418	///
419	/// Get `key` from child storage, placing the value into `value_out` and return the number
420	/// of bytes that the entry in storage has beyond the offset or `None` if the storage entry
421	/// doesn't exist at all.
422	/// If `value_out` length is smaller than the returned length, only `value_out` length bytes
423	/// are copied into `value_out`.
424	fn read(
425		&mut self,
426		storage_key: PassFatPointerAndRead<&[u8]>,
427		key: PassFatPointerAndRead<&[u8]>,
428		value_out: PassFatPointerAndReadWrite<&mut [u8]>,
429		value_offset: u32,
430	) -> AllocateAndReturnByCodec<Option<u32>> {
431		let child_info = ChildInfo::new_default(storage_key);
432		self.child_storage(&child_info, key).map(|value| {
433			let value_offset = value_offset as usize;
434			let data = &value[value_offset.min(value.len())..];
435			let written = core::cmp::min(data.len(), value_out.len());
436			value_out[..written].copy_from_slice(&data[..written]);
437			data.len() as u32
438		})
439	}
440
441	/// Set a child storage value.
442	///
443	/// Set `key` to `value` in the child storage denoted by `storage_key`.
444	fn set(
445		&mut self,
446		storage_key: PassFatPointerAndRead<&[u8]>,
447		key: PassFatPointerAndRead<&[u8]>,
448		value: PassFatPointerAndRead<&[u8]>,
449	) {
450		let child_info = ChildInfo::new_default(storage_key);
451		self.set_child_storage(&child_info, key.to_vec(), value.to_vec());
452	}
453
454	/// Clear a child storage key.
455	///
456	/// For the default child storage at `storage_key`, clear value at `key`.
457	fn clear(
458		&mut self,
459		storage_key: PassFatPointerAndRead<&[u8]>,
460		key: PassFatPointerAndRead<&[u8]>,
461	) {
462		let child_info = ChildInfo::new_default(storage_key);
463		self.clear_child_storage(&child_info, key);
464	}
465
466	/// Clear an entire child storage.
467	///
468	/// If it exists, the child storage for `storage_key`
469	/// is removed.
470	fn storage_kill(&mut self, storage_key: PassFatPointerAndRead<&[u8]>) {
471		let child_info = ChildInfo::new_default(storage_key);
472		let _ = self.kill_child_storage(&child_info, None, None);
473	}
474
475	/// Clear a child storage key.
476	///
477	/// See `Storage` module `clear_prefix` documentation for `limit` usage.
478	#[version(2)]
479	fn storage_kill(
480		&mut self,
481		storage_key: PassFatPointerAndRead<&[u8]>,
482		limit: PassFatPointerAndDecode<Option<u32>>,
483	) -> bool {
484		let child_info = ChildInfo::new_default(storage_key);
485		let r = self.kill_child_storage(&child_info, limit, None);
486		r.maybe_cursor.is_none()
487	}
488
489	/// Clear a child storage key.
490	///
491	/// See `Storage` module `clear_prefix` documentation for `limit` usage.
492	#[version(3)]
493	fn storage_kill(
494		&mut self,
495		storage_key: PassFatPointerAndRead<&[u8]>,
496		limit: PassFatPointerAndDecode<Option<u32>>,
497	) -> AllocateAndReturnByCodec<KillStorageResult> {
498		let child_info = ChildInfo::new_default(storage_key);
499		self.kill_child_storage(&child_info, limit, None).into()
500	}
501
502	/// Clear a child storage key.
503	///
504	/// See `Storage` module `clear_prefix` documentation for `limit` usage.
505	#[version(4, register_only)]
506	fn storage_kill(
507		&mut self,
508		storage_key: PassFatPointerAndRead<&[u8]>,
509		maybe_limit: PassFatPointerAndDecode<Option<u32>>,
510		maybe_cursor: PassFatPointerAndDecode<Option<Vec<u8>>>,
511	) -> AllocateAndReturnByCodec<MultiRemovalResults> {
512		let child_info = ChildInfo::new_default(storage_key);
513		self.kill_child_storage(&child_info, maybe_limit, maybe_cursor.as_ref().map(|x| &x[..]))
514			.into()
515	}
516
517	/// Check a child storage key.
518	///
519	/// Check whether the given `key` exists in default child defined at `storage_key`.
520	fn exists(
521		&mut self,
522		storage_key: PassFatPointerAndRead<&[u8]>,
523		key: PassFatPointerAndRead<&[u8]>,
524	) -> bool {
525		let child_info = ChildInfo::new_default(storage_key);
526		self.exists_child_storage(&child_info, key)
527	}
528
529	/// Clear child default key by prefix.
530	///
531	/// Clear the child storage of each key-value pair where the key starts with the given `prefix`.
532	fn clear_prefix(
533		&mut self,
534		storage_key: PassFatPointerAndRead<&[u8]>,
535		prefix: PassFatPointerAndRead<&[u8]>,
536	) {
537		let child_info = ChildInfo::new_default(storage_key);
538		let _ = self.clear_child_prefix(&child_info, prefix, None, None);
539	}
540
541	/// Clear the child storage of each key-value pair where the key starts with the given `prefix`.
542	///
543	/// See `Storage` module `clear_prefix` documentation for `limit` usage.
544	#[version(2)]
545	fn clear_prefix(
546		&mut self,
547		storage_key: PassFatPointerAndRead<&[u8]>,
548		prefix: PassFatPointerAndRead<&[u8]>,
549		limit: PassFatPointerAndDecode<Option<u32>>,
550	) -> AllocateAndReturnByCodec<KillStorageResult> {
551		let child_info = ChildInfo::new_default(storage_key);
552		self.clear_child_prefix(&child_info, prefix, limit, None).into()
553	}
554
555	/// Clear the child storage of each key-value pair where the key starts with the given `prefix`.
556	///
557	/// See `Storage` module `clear_prefix` documentation for `limit` usage.
558	#[version(3, register_only)]
559	fn clear_prefix(
560		&mut self,
561		storage_key: PassFatPointerAndRead<&[u8]>,
562		prefix: PassFatPointerAndRead<&[u8]>,
563		maybe_limit: PassFatPointerAndDecode<Option<u32>>,
564		maybe_cursor: PassFatPointerAndDecode<Option<Vec<u8>>>,
565	) -> AllocateAndReturnByCodec<MultiRemovalResults> {
566		let child_info = ChildInfo::new_default(storage_key);
567		self.clear_child_prefix(
568			&child_info,
569			prefix,
570			maybe_limit,
571			maybe_cursor.as_ref().map(|x| &x[..]),
572		)
573		.into()
574	}
575
576	/// Default child root calculation.
577	///
578	/// "Commit" all existing operations and compute the resulting child storage root.
579	/// The hashing algorithm is defined by the `Block`.
580	///
581	/// Returns a `Vec<u8>` that holds the SCALE encoded hash.
582	fn root(
583		&mut self,
584		storage_key: PassFatPointerAndRead<&[u8]>,
585	) -> AllocateAndReturnFatPointer<Vec<u8>> {
586		let child_info = ChildInfo::new_default(storage_key);
587		self.child_storage_root(&child_info, StateVersion::V0)
588	}
589
590	/// Default child root calculation.
591	///
592	/// "Commit" all existing operations and compute the resulting child storage root.
593	/// The hashing algorithm is defined by the `Block`.
594	///
595	/// Returns a `Vec<u8>` that holds the SCALE encoded hash.
596	#[version(2)]
597	fn root(
598		&mut self,
599		storage_key: PassFatPointerAndRead<&[u8]>,
600		version: PassAs<StateVersion, u8>,
601	) -> AllocateAndReturnFatPointer<Vec<u8>> {
602		let child_info = ChildInfo::new_default(storage_key);
603		self.child_storage_root(&child_info, version)
604	}
605
606	/// Child storage key iteration.
607	///
608	/// Get the next key in storage after the given one in lexicographic order in child storage.
609	fn next_key(
610		&mut self,
611		storage_key: PassFatPointerAndRead<&[u8]>,
612		key: PassFatPointerAndRead<&[u8]>,
613	) -> AllocateAndReturnByCodec<Option<Vec<u8>>> {
614		let child_info = ChildInfo::new_default(storage_key);
615		self.next_child_storage_key(&child_info, key)
616	}
617}
618
619/// Interface that provides trie related functionality.
620#[runtime_interface]
621pub trait Trie {
622	/// A trie root formed from the iterated items.
623	fn blake2_256_root(
624		input: PassFatPointerAndDecode<Vec<(Vec<u8>, Vec<u8>)>>,
625	) -> AllocateAndReturnPointer<H256, 32> {
626		LayoutV0::<sp_core::Blake2Hasher>::trie_root(input)
627	}
628
629	/// A trie root formed from the iterated items.
630	#[version(2)]
631	fn blake2_256_root(
632		input: PassFatPointerAndDecode<Vec<(Vec<u8>, Vec<u8>)>>,
633		version: PassAs<StateVersion, u8>,
634	) -> AllocateAndReturnPointer<H256, 32> {
635		match version {
636			StateVersion::V0 => LayoutV0::<sp_core::Blake2Hasher>::trie_root(input),
637			StateVersion::V1 => LayoutV1::<sp_core::Blake2Hasher>::trie_root(input),
638		}
639	}
640
641	/// A trie root formed from the enumerated items.
642	fn blake2_256_ordered_root(
643		input: PassFatPointerAndDecode<Vec<Vec<u8>>>,
644	) -> AllocateAndReturnPointer<H256, 32> {
645		LayoutV0::<sp_core::Blake2Hasher>::ordered_trie_root(input)
646	}
647
648	/// A trie root formed from the enumerated items.
649	#[version(2)]
650	fn blake2_256_ordered_root(
651		input: PassFatPointerAndDecode<Vec<Vec<u8>>>,
652		version: PassAs<StateVersion, u8>,
653	) -> AllocateAndReturnPointer<H256, 32> {
654		match version {
655			StateVersion::V0 => LayoutV0::<sp_core::Blake2Hasher>::ordered_trie_root(input),
656			StateVersion::V1 => LayoutV1::<sp_core::Blake2Hasher>::ordered_trie_root(input),
657		}
658	}
659
660	/// A trie root formed from the iterated items.
661	fn keccak_256_root(
662		input: PassFatPointerAndDecode<Vec<(Vec<u8>, Vec<u8>)>>,
663	) -> AllocateAndReturnPointer<H256, 32> {
664		LayoutV0::<sp_core::KeccakHasher>::trie_root(input)
665	}
666
667	/// A trie root formed from the iterated items.
668	#[version(2)]
669	fn keccak_256_root(
670		input: PassFatPointerAndDecode<Vec<(Vec<u8>, Vec<u8>)>>,
671		version: PassAs<StateVersion, u8>,
672	) -> AllocateAndReturnPointer<H256, 32> {
673		match version {
674			StateVersion::V0 => LayoutV0::<sp_core::KeccakHasher>::trie_root(input),
675			StateVersion::V1 => LayoutV1::<sp_core::KeccakHasher>::trie_root(input),
676		}
677	}
678
679	/// A trie root formed from the enumerated items.
680	fn keccak_256_ordered_root(
681		input: PassFatPointerAndDecode<Vec<Vec<u8>>>,
682	) -> AllocateAndReturnPointer<H256, 32> {
683		LayoutV0::<sp_core::KeccakHasher>::ordered_trie_root(input)
684	}
685
686	/// A trie root formed from the enumerated items.
687	#[version(2)]
688	fn keccak_256_ordered_root(
689		input: PassFatPointerAndDecode<Vec<Vec<u8>>>,
690		version: PassAs<StateVersion, u8>,
691	) -> AllocateAndReturnPointer<H256, 32> {
692		match version {
693			StateVersion::V0 => LayoutV0::<sp_core::KeccakHasher>::ordered_trie_root(input),
694			StateVersion::V1 => LayoutV1::<sp_core::KeccakHasher>::ordered_trie_root(input),
695		}
696	}
697
698	/// Verify trie proof
699	fn blake2_256_verify_proof(
700		root: PassPointerAndReadCopy<H256, 32>,
701		proof: PassFatPointerAndDecodeSlice<&[Vec<u8>]>,
702		key: PassFatPointerAndRead<&[u8]>,
703		value: PassFatPointerAndRead<&[u8]>,
704	) -> bool {
705		sp_trie::verify_trie_proof::<LayoutV0<sp_core::Blake2Hasher>, _, _, _>(
706			&root,
707			proof,
708			&[(key, Some(value))],
709		)
710		.is_ok()
711	}
712
713	/// Verify trie proof
714	#[version(2)]
715	fn blake2_256_verify_proof(
716		root: PassPointerAndReadCopy<H256, 32>,
717		proof: PassFatPointerAndDecodeSlice<&[Vec<u8>]>,
718		key: PassFatPointerAndRead<&[u8]>,
719		value: PassFatPointerAndRead<&[u8]>,
720		version: PassAs<StateVersion, u8>,
721	) -> bool {
722		match version {
723			StateVersion::V0 => sp_trie::verify_trie_proof::<
724				LayoutV0<sp_core::Blake2Hasher>,
725				_,
726				_,
727				_,
728			>(&root, proof, &[(key, Some(value))])
729			.is_ok(),
730			StateVersion::V1 => sp_trie::verify_trie_proof::<
731				LayoutV1<sp_core::Blake2Hasher>,
732				_,
733				_,
734				_,
735			>(&root, proof, &[(key, Some(value))])
736			.is_ok(),
737		}
738	}
739
740	/// Verify trie proof
741	fn keccak_256_verify_proof(
742		root: PassPointerAndReadCopy<H256, 32>,
743		proof: PassFatPointerAndDecodeSlice<&[Vec<u8>]>,
744		key: PassFatPointerAndRead<&[u8]>,
745		value: PassFatPointerAndRead<&[u8]>,
746	) -> bool {
747		sp_trie::verify_trie_proof::<LayoutV0<sp_core::KeccakHasher>, _, _, _>(
748			&root,
749			proof,
750			&[(key, Some(value))],
751		)
752		.is_ok()
753	}
754
755	/// Verify trie proof
756	#[version(2)]
757	fn keccak_256_verify_proof(
758		root: PassPointerAndReadCopy<H256, 32>,
759		proof: PassFatPointerAndDecodeSlice<&[Vec<u8>]>,
760		key: PassFatPointerAndRead<&[u8]>,
761		value: PassFatPointerAndRead<&[u8]>,
762		version: PassAs<StateVersion, u8>,
763	) -> bool {
764		match version {
765			StateVersion::V0 => sp_trie::verify_trie_proof::<
766				LayoutV0<sp_core::KeccakHasher>,
767				_,
768				_,
769				_,
770			>(&root, proof, &[(key, Some(value))])
771			.is_ok(),
772			StateVersion::V1 => sp_trie::verify_trie_proof::<
773				LayoutV1<sp_core::KeccakHasher>,
774				_,
775				_,
776				_,
777			>(&root, proof, &[(key, Some(value))])
778			.is_ok(),
779		}
780	}
781}
782
783/// Interface that provides miscellaneous functions for communicating between the runtime and the
784/// node.
785#[runtime_interface]
786pub trait Misc {
787	// NOTE: We use the target 'runtime' for messages produced by general printing functions,
788	// instead of LOG_TARGET.
789
790	/// Print a number.
791	fn print_num(val: u64) {
792		log::debug!(target: "runtime", "{}", val);
793	}
794
795	/// Print any valid `utf8` buffer.
796	fn print_utf8(utf8: PassFatPointerAndRead<&[u8]>) {
797		if let Ok(data) = core::str::from_utf8(utf8) {
798			log::debug!(target: "runtime", "{}", data)
799		}
800	}
801
802	/// Print any `u8` slice as hex.
803	fn print_hex(data: PassFatPointerAndRead<&[u8]>) {
804		log::debug!(target: "runtime", "{}", HexDisplay::from(&data));
805	}
806
807	/// Extract the runtime version of the given wasm blob by calling `Core_version`.
808	///
809	/// Returns `None` if calling the function failed for any reason or `Some(Vec<u8>)` where
810	/// the `Vec<u8>` holds the SCALE encoded runtime version.
811	///
812	/// # Performance
813	///
814	/// This function may be very expensive to call depending on the wasm binary. It may be
815	/// relatively cheap if the wasm binary contains version information. In that case,
816	/// uncompression of the wasm blob is the dominating factor.
817	///
818	/// If the wasm binary does not have the version information attached, then a legacy mechanism
819	/// may be involved. This means that a runtime call will be performed to query the version.
820	///
821	/// Calling into the runtime may be incredible expensive and should be approached with care.
822	fn runtime_version(
823		&mut self,
824		wasm: PassFatPointerAndRead<&[u8]>,
825	) -> AllocateAndReturnByCodec<Option<Vec<u8>>> {
826		use sp_core::traits::ReadRuntimeVersionExt;
827
828		let mut ext = sp_state_machine::BasicExternalities::default();
829
830		match self
831			.extension::<ReadRuntimeVersionExt>()
832			.expect("No `ReadRuntimeVersionExt` associated for the current context!")
833			.read_runtime_version(wasm, &mut ext)
834		{
835			Ok(v) => Some(v),
836			Err(err) => {
837				log::debug!(
838					target: LOG_TARGET,
839					"cannot read version from the given runtime: {}",
840					err,
841				);
842				None
843			},
844		}
845	}
846}
847
848#[cfg(not(substrate_runtime))]
849sp_externalities::decl_extension! {
850	/// Extension to signal to [`crypt::ed25519_verify`] to use the dalek crate.
851	///
852	/// The switch from `ed25519-dalek` to `ed25519-zebra` was a breaking change.
853	/// `ed25519-zebra` is more permissive when it comes to the verification of signatures.
854	/// This means that some chains may fail to sync from genesis when using `ed25519-zebra`.
855	/// So, this extension can be registered to the runtime execution environment to signal
856	/// that `ed25519-dalek` should be used for verification. The extension can be registered
857	/// in the following way:
858	///
859	/// ```nocompile
860	/// client.execution_extensions().set_extensions_factory(
861	/// 	// Let the `UseDalekExt` extension being registered for each runtime invocation
862	/// 	// until the execution happens in the context of block `1000`.
863	/// 	sc_client_api::execution_extensions::ExtensionBeforeBlock::<Block, UseDalekExt>::new(1000)
864	/// );
865	/// ```
866	pub struct UseDalekExt;
867}
868
869#[cfg(not(substrate_runtime))]
870impl Default for UseDalekExt {
871	fn default() -> Self {
872		Self
873	}
874}
875
876/// Interfaces for working with crypto related types from within the runtime.
877#[runtime_interface]
878pub trait Crypto {
879	/// Returns all `ed25519` public keys for the given key id from the keystore.
880	fn ed25519_public_keys(
881		&mut self,
882		id: PassPointerAndReadCopy<KeyTypeId, 4>,
883	) -> AllocateAndReturnByCodec<Vec<ed25519::Public>> {
884		self.extension::<KeystoreExt>()
885			.expect("No `keystore` associated for the current context!")
886			.ed25519_public_keys(id)
887	}
888
889	/// Generate an `ed22519` key for the given key type using an optional `seed` and
890	/// store it in the keystore.
891	///
892	/// The `seed` needs to be a valid utf8.
893	///
894	/// Returns the public key.
895	fn ed25519_generate(
896		&mut self,
897		id: PassPointerAndReadCopy<KeyTypeId, 4>,
898		seed: PassFatPointerAndDecode<Option<Vec<u8>>>,
899	) -> AllocateAndReturnPointer<ed25519::Public, 32> {
900		let seed = seed.as_ref().map(|s| core::str::from_utf8(s).expect("Seed is valid utf8!"));
901		self.extension::<KeystoreExt>()
902			.expect("No `keystore` associated for the current context!")
903			.ed25519_generate_new(id, seed)
904			.expect("`ed25519_generate` failed")
905	}
906
907	/// Sign the given `msg` with the `ed25519` key that corresponds to the given public key and
908	/// key type in the keystore.
909	///
910	/// Returns the signature.
911	fn ed25519_sign(
912		&mut self,
913		id: PassPointerAndReadCopy<KeyTypeId, 4>,
914		pub_key: PassPointerAndRead<&ed25519::Public, 32>,
915		msg: PassFatPointerAndRead<&[u8]>,
916	) -> AllocateAndReturnByCodec<Option<ed25519::Signature>> {
917		self.extension::<KeystoreExt>()
918			.expect("No `keystore` associated for the current context!")
919			.ed25519_sign(id, pub_key, msg)
920			.ok()
921			.flatten()
922	}
923
924	/// Verify `ed25519` signature.
925	///
926	/// Returns `true` when the verification was successful.
927	fn ed25519_verify(
928		sig: PassPointerAndRead<&ed25519::Signature, 64>,
929		msg: PassFatPointerAndRead<&[u8]>,
930		pub_key: PassPointerAndRead<&ed25519::Public, 32>,
931	) -> bool {
932		// We don't want to force everyone needing to call the function in an externalities context.
933		// So, we assume that we should not use dalek when we are not in externalities context.
934		// Otherwise, we check if the extension is present.
935		if sp_externalities::with_externalities(|mut e| e.extension::<UseDalekExt>().is_some())
936			.unwrap_or_default()
937		{
938			use ed25519_dalek::Verifier;
939
940			let Ok(public_key) = ed25519_dalek::VerifyingKey::from_bytes(&pub_key.0) else {
941				return false;
942			};
943
944			let sig = ed25519_dalek::Signature::from_bytes(&sig.0);
945
946			public_key.verify(msg, &sig).is_ok()
947		} else {
948			ed25519::Pair::verify(sig, msg, pub_key)
949		}
950	}
951
952	/// Register a `ed25519` signature for batch verification.
953	///
954	/// Batch verification must be enabled by calling [`start_batch_verify`].
955	/// If batch verification is not enabled, the signature will be verified immediately.
956	/// To get the result of the batch verification, [`finish_batch_verify`]
957	/// needs to be called.
958	///
959	/// Returns `true` when the verification is either successful or batched.
960	///
961	/// NOTE: Is tagged with `register_only` to keep the functions around for backwards
962	/// compatibility with old runtimes, but it should not be used anymore by new runtimes.
963	/// The implementation emulates the old behavior, but isn't doing any batch verification
964	/// anymore.
965	#[version(1, register_only)]
966	fn ed25519_batch_verify(
967		&mut self,
968		sig: PassPointerAndRead<&ed25519::Signature, 64>,
969		msg: PassFatPointerAndRead<&[u8]>,
970		pub_key: PassPointerAndRead<&ed25519::Public, 32>,
971	) -> bool {
972		let res = ed25519_verify(sig, msg, pub_key);
973
974		if let Some(ext) = self.extension::<VerificationExtDeprecated>() {
975			ext.0 &= res;
976		}
977
978		res
979	}
980
981	/// Verify `sr25519` signature.
982	///
983	/// Returns `true` when the verification was successful.
984	#[version(2)]
985	fn sr25519_verify(
986		sig: PassPointerAndRead<&sr25519::Signature, 64>,
987		msg: PassFatPointerAndRead<&[u8]>,
988		pub_key: PassPointerAndRead<&sr25519::Public, 32>,
989	) -> bool {
990		sr25519::Pair::verify(sig, msg, pub_key)
991	}
992
993	/// Register a `sr25519` signature for batch verification.
994	///
995	/// Batch verification must be enabled by calling [`start_batch_verify`].
996	/// If batch verification is not enabled, the signature will be verified immediately.
997	/// To get the result of the batch verification, [`finish_batch_verify`]
998	/// needs to be called.
999	///
1000	/// Returns `true` when the verification is either successful or batched.
1001	///
1002	/// NOTE: Is tagged with `register_only` to keep the functions around for backwards
1003	/// compatibility with old runtimes, but it should not be used anymore by new runtimes.
1004	/// The implementation emulates the old behavior, but isn't doing any batch verification
1005	/// anymore.
1006	#[version(1, register_only)]
1007	fn sr25519_batch_verify(
1008		&mut self,
1009		sig: PassPointerAndRead<&sr25519::Signature, 64>,
1010		msg: PassFatPointerAndRead<&[u8]>,
1011		pub_key: PassPointerAndRead<&sr25519::Public, 32>,
1012	) -> bool {
1013		let res = sr25519_verify(sig, msg, pub_key);
1014
1015		if let Some(ext) = self.extension::<VerificationExtDeprecated>() {
1016			ext.0 &= res;
1017		}
1018
1019		res
1020	}
1021
1022	/// Start verification extension.
1023	///
1024	/// NOTE: Is tagged with `register_only` to keep the functions around for backwards
1025	/// compatibility with old runtimes, but it should not be used anymore by new runtimes.
1026	/// The implementation emulates the old behavior, but isn't doing any batch verification
1027	/// anymore.
1028	#[version(1, register_only)]
1029	fn start_batch_verify(&mut self) {
1030		self.register_extension(VerificationExtDeprecated(true))
1031			.expect("Failed to register required extension: `VerificationExt`");
1032	}
1033
1034	/// Finish batch-verification of signatures.
1035	///
1036	/// Verify or wait for verification to finish for all signatures which were previously
1037	/// deferred by `sr25519_verify`/`ed25519_verify`.
1038	///
1039	/// Will panic if no `VerificationExt` is registered (`start_batch_verify` was not called).
1040	///
1041	/// NOTE: Is tagged with `register_only` to keep the functions around for backwards
1042	/// compatibility with old runtimes, but it should not be used anymore by new runtimes.
1043	/// The implementation emulates the old behavior, but isn't doing any batch verification
1044	/// anymore.
1045	#[version(1, register_only)]
1046	fn finish_batch_verify(&mut self) -> bool {
1047		let result = self
1048			.extension::<VerificationExtDeprecated>()
1049			.expect("`finish_batch_verify` should only be called after `start_batch_verify`")
1050			.0;
1051
1052		self.deregister_extension::<VerificationExtDeprecated>()
1053			.expect("No verification extension in current context!");
1054
1055		result
1056	}
1057
1058	/// Returns all `sr25519` public keys for the given key id from the keystore.
1059	fn sr25519_public_keys(
1060		&mut self,
1061		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1062	) -> AllocateAndReturnByCodec<Vec<sr25519::Public>> {
1063		self.extension::<KeystoreExt>()
1064			.expect("No `keystore` associated for the current context!")
1065			.sr25519_public_keys(id)
1066	}
1067
1068	/// Generate an `sr22519` key for the given key type using an optional seed and
1069	/// store it in the keystore.
1070	///
1071	/// The `seed` needs to be a valid utf8.
1072	///
1073	/// Returns the public key.
1074	fn sr25519_generate(
1075		&mut self,
1076		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1077		seed: PassFatPointerAndDecode<Option<Vec<u8>>>,
1078	) -> AllocateAndReturnPointer<sr25519::Public, 32> {
1079		let seed = seed.as_ref().map(|s| core::str::from_utf8(s).expect("Seed is valid utf8!"));
1080		self.extension::<KeystoreExt>()
1081			.expect("No `keystore` associated for the current context!")
1082			.sr25519_generate_new(id, seed)
1083			.expect("`sr25519_generate` failed")
1084	}
1085
1086	/// Sign the given `msg` with the `sr25519` key that corresponds to the given public key and
1087	/// key type in the keystore.
1088	///
1089	/// Returns the signature.
1090	fn sr25519_sign(
1091		&mut self,
1092		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1093		pub_key: PassPointerAndRead<&sr25519::Public, 32>,
1094		msg: PassFatPointerAndRead<&[u8]>,
1095	) -> AllocateAndReturnByCodec<Option<sr25519::Signature>> {
1096		self.extension::<KeystoreExt>()
1097			.expect("No `keystore` associated for the current context!")
1098			.sr25519_sign(id, pub_key, msg)
1099			.ok()
1100			.flatten()
1101	}
1102
1103	/// Verify an `sr25519` signature.
1104	///
1105	/// Returns `true` when the verification in successful regardless of
1106	/// signature version.
1107	fn sr25519_verify(
1108		sig: PassPointerAndRead<&sr25519::Signature, 64>,
1109		msg: PassFatPointerAndRead<&[u8]>,
1110		pubkey: PassPointerAndRead<&sr25519::Public, 32>,
1111	) -> bool {
1112		sr25519::Pair::verify_deprecated(sig, msg, pubkey)
1113	}
1114
1115	/// Returns all `ecdsa` public keys for the given key id from the keystore.
1116	fn ecdsa_public_keys(
1117		&mut self,
1118		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1119	) -> AllocateAndReturnByCodec<Vec<ecdsa::Public>> {
1120		self.extension::<KeystoreExt>()
1121			.expect("No `keystore` associated for the current context!")
1122			.ecdsa_public_keys(id)
1123	}
1124
1125	/// Generate an `ecdsa` key for the given key type using an optional `seed` and
1126	/// store it in the keystore.
1127	///
1128	/// The `seed` needs to be a valid utf8.
1129	///
1130	/// Returns the public key.
1131	fn ecdsa_generate(
1132		&mut self,
1133		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1134		seed: PassFatPointerAndDecode<Option<Vec<u8>>>,
1135	) -> AllocateAndReturnPointer<ecdsa::Public, 33> {
1136		let seed = seed.as_ref().map(|s| core::str::from_utf8(s).expect("Seed is valid utf8!"));
1137		self.extension::<KeystoreExt>()
1138			.expect("No `keystore` associated for the current context!")
1139			.ecdsa_generate_new(id, seed)
1140			.expect("`ecdsa_generate` failed")
1141	}
1142
1143	/// Sign the given `msg` with the `ecdsa` key that corresponds to the given public key and
1144	/// key type in the keystore.
1145	///
1146	/// Returns the signature.
1147	fn ecdsa_sign(
1148		&mut self,
1149		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1150		pub_key: PassPointerAndRead<&ecdsa::Public, 33>,
1151		msg: PassFatPointerAndRead<&[u8]>,
1152	) -> AllocateAndReturnByCodec<Option<ecdsa::Signature>> {
1153		self.extension::<KeystoreExt>()
1154			.expect("No `keystore` associated for the current context!")
1155			.ecdsa_sign(id, pub_key, msg)
1156			.ok()
1157			.flatten()
1158	}
1159
1160	/// Sign the given a pre-hashed `msg` with the `ecdsa` key that corresponds to the given public
1161	/// key and key type in the keystore.
1162	///
1163	/// Returns the signature.
1164	fn ecdsa_sign_prehashed(
1165		&mut self,
1166		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1167		pub_key: PassPointerAndRead<&ecdsa::Public, 33>,
1168		msg: PassPointerAndRead<&[u8; 32], 32>,
1169	) -> AllocateAndReturnByCodec<Option<ecdsa::Signature>> {
1170		self.extension::<KeystoreExt>()
1171			.expect("No `keystore` associated for the current context!")
1172			.ecdsa_sign_prehashed(id, pub_key, msg)
1173			.ok()
1174			.flatten()
1175	}
1176
1177	/// Verify `ecdsa` signature.
1178	///
1179	/// Returns `true` when the verification was successful.
1180	/// This version is able to handle, non-standard, overflowing signatures.
1181	fn ecdsa_verify(
1182		sig: PassPointerAndRead<&ecdsa::Signature, 65>,
1183		msg: PassFatPointerAndRead<&[u8]>,
1184		pub_key: PassPointerAndRead<&ecdsa::Public, 33>,
1185	) -> bool {
1186		#[allow(deprecated)]
1187		ecdsa::Pair::verify_deprecated(sig, msg, pub_key)
1188	}
1189
1190	/// Verify `ecdsa` signature.
1191	///
1192	/// Returns `true` when the verification was successful.
1193	#[version(2)]
1194	fn ecdsa_verify(
1195		sig: PassPointerAndRead<&ecdsa::Signature, 65>,
1196		msg: PassFatPointerAndRead<&[u8]>,
1197		pub_key: PassPointerAndRead<&ecdsa::Public, 33>,
1198	) -> bool {
1199		ecdsa::Pair::verify(sig, msg, pub_key)
1200	}
1201
1202	/// Verify `ecdsa` signature with pre-hashed `msg`.
1203	///
1204	/// Returns `true` when the verification was successful.
1205	fn ecdsa_verify_prehashed(
1206		sig: PassPointerAndRead<&ecdsa::Signature, 65>,
1207		msg: PassPointerAndRead<&[u8; 32], 32>,
1208		pub_key: PassPointerAndRead<&ecdsa::Public, 33>,
1209	) -> bool {
1210		ecdsa::Pair::verify_prehashed(sig, msg, pub_key)
1211	}
1212
1213	/// Register a `ecdsa` signature for batch verification.
1214	///
1215	/// Batch verification must be enabled by calling [`start_batch_verify`].
1216	/// If batch verification is not enabled, the signature will be verified immediately.
1217	/// To get the result of the batch verification, [`finish_batch_verify`]
1218	/// needs to be called.
1219	///
1220	/// Returns `true` when the verification is either successful or batched.
1221	///
1222	/// NOTE: Is tagged with `register_only` to keep the functions around for backwards
1223	/// compatibility with old runtimes, but it should not be used anymore by new runtimes.
1224	/// The implementation emulates the old behavior, but isn't doing any batch verification
1225	/// anymore.
1226	#[version(1, register_only)]
1227	fn ecdsa_batch_verify(
1228		&mut self,
1229		sig: PassPointerAndRead<&ecdsa::Signature, 65>,
1230		msg: PassFatPointerAndRead<&[u8]>,
1231		pub_key: PassPointerAndRead<&ecdsa::Public, 33>,
1232	) -> bool {
1233		let res = ecdsa_verify(sig, msg, pub_key);
1234
1235		if let Some(ext) = self.extension::<VerificationExtDeprecated>() {
1236			ext.0 &= res;
1237		}
1238
1239		res
1240	}
1241
1242	/// Verify and recover a SECP256k1 ECDSA signature.
1243	///
1244	/// - `sig` is passed in RSV format. V should be either `0/1` or `27/28`.
1245	/// - `msg` is the blake2-256 hash of the message.
1246	///
1247	/// Returns `Err` if the signature is bad, otherwise the 64-byte pubkey
1248	/// (doesn't include the 0x04 prefix).
1249	/// This version is able to handle, non-standard, overflowing signatures.
1250	fn secp256k1_ecdsa_recover(
1251		sig: PassPointerAndRead<&[u8; 65], 65>,
1252		msg: PassPointerAndRead<&[u8; 32], 32>,
1253	) -> AllocateAndReturnByCodec<Result<[u8; 64], EcdsaVerifyError>> {
1254		let rid = libsecp256k1::RecoveryId::parse(
1255			if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as u8,
1256		)
1257		.map_err(|_| EcdsaVerifyError::BadV)?;
1258		let sig = libsecp256k1::Signature::parse_overflowing_slice(&sig[..64])
1259			.map_err(|_| EcdsaVerifyError::BadRS)?;
1260		let msg = libsecp256k1::Message::parse(msg);
1261		let pubkey =
1262			libsecp256k1::recover(&msg, &sig, &rid).map_err(|_| EcdsaVerifyError::BadSignature)?;
1263		let mut res = [0u8; 64];
1264		res.copy_from_slice(&pubkey.serialize()[1..65]);
1265		Ok(res)
1266	}
1267
1268	/// Verify and recover a SECP256k1 ECDSA signature.
1269	///
1270	/// - `sig` is passed in RSV format. V should be either `0/1` or `27/28`.
1271	/// - `msg` is the blake2-256 hash of the message.
1272	///
1273	/// Returns `Err` if the signature is bad, otherwise the 64-byte pubkey
1274	/// (doesn't include the 0x04 prefix).
1275	#[version(2)]
1276	fn secp256k1_ecdsa_recover(
1277		sig: PassPointerAndRead<&[u8; 65], 65>,
1278		msg: PassPointerAndRead<&[u8; 32], 32>,
1279	) -> AllocateAndReturnByCodec<Result<[u8; 64], EcdsaVerifyError>> {
1280		let rid = RecoveryId::from_i32(if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as i32)
1281			.map_err(|_| EcdsaVerifyError::BadV)?;
1282		let sig = RecoverableSignature::from_compact(&sig[..64], rid)
1283			.map_err(|_| EcdsaVerifyError::BadRS)?;
1284		let msg = Message::from_digest_slice(msg).expect("Message is 32 bytes; qed");
1285		#[cfg(feature = "std")]
1286		let ctx = secp256k1::SECP256K1;
1287		#[cfg(not(feature = "std"))]
1288		let ctx = secp256k1::Secp256k1::<secp256k1::VerifyOnly>::gen_new();
1289		let pubkey = ctx.recover_ecdsa(&msg, &sig).map_err(|_| EcdsaVerifyError::BadSignature)?;
1290		let mut res = [0u8; 64];
1291		res.copy_from_slice(&pubkey.serialize_uncompressed()[1..]);
1292		Ok(res)
1293	}
1294
1295	/// Verify and recover a SECP256k1 ECDSA signature.
1296	///
1297	/// - `sig` is passed in RSV format. V should be either `0/1` or `27/28`.
1298	/// - `msg` is the blake2-256 hash of the message.
1299	///
1300	/// Returns `Err` if the signature is bad, otherwise the 33-byte compressed pubkey.
1301	fn secp256k1_ecdsa_recover_compressed(
1302		sig: PassPointerAndRead<&[u8; 65], 65>,
1303		msg: PassPointerAndRead<&[u8; 32], 32>,
1304	) -> AllocateAndReturnByCodec<Result<[u8; 33], EcdsaVerifyError>> {
1305		let rid = libsecp256k1::RecoveryId::parse(
1306			if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as u8,
1307		)
1308		.map_err(|_| EcdsaVerifyError::BadV)?;
1309		let sig = libsecp256k1::Signature::parse_overflowing_slice(&sig[0..64])
1310			.map_err(|_| EcdsaVerifyError::BadRS)?;
1311		let msg = libsecp256k1::Message::parse(msg);
1312		let pubkey =
1313			libsecp256k1::recover(&msg, &sig, &rid).map_err(|_| EcdsaVerifyError::BadSignature)?;
1314		Ok(pubkey.serialize_compressed())
1315	}
1316
1317	/// Verify and recover a SECP256k1 ECDSA signature.
1318	///
1319	/// - `sig` is passed in RSV format. V should be either `0/1` or `27/28`.
1320	/// - `msg` is the blake2-256 hash of the message.
1321	///
1322	/// Returns `Err` if the signature is bad, otherwise the 33-byte compressed pubkey.
1323	#[version(2)]
1324	fn secp256k1_ecdsa_recover_compressed(
1325		sig: PassPointerAndRead<&[u8; 65], 65>,
1326		msg: PassPointerAndRead<&[u8; 32], 32>,
1327	) -> AllocateAndReturnByCodec<Result<[u8; 33], EcdsaVerifyError>> {
1328		let rid = RecoveryId::from_i32(if sig[64] > 26 { sig[64] - 27 } else { sig[64] } as i32)
1329			.map_err(|_| EcdsaVerifyError::BadV)?;
1330		let sig = RecoverableSignature::from_compact(&sig[..64], rid)
1331			.map_err(|_| EcdsaVerifyError::BadRS)?;
1332		let msg = Message::from_digest_slice(msg).expect("Message is 32 bytes; qed");
1333		#[cfg(feature = "std")]
1334		let ctx = secp256k1::SECP256K1;
1335		#[cfg(not(feature = "std"))]
1336		let ctx = secp256k1::Secp256k1::<secp256k1::VerifyOnly>::gen_new();
1337		let pubkey = ctx.recover_ecdsa(&msg, &sig).map_err(|_| EcdsaVerifyError::BadSignature)?;
1338		Ok(pubkey.serialize())
1339	}
1340
1341	/// Generate an `bls12-381` key for the given key type using an optional `seed` and
1342	/// store it in the keystore.
1343	///
1344	/// The `seed` needs to be a valid utf8.
1345	///
1346	/// Returns the public key.
1347	#[cfg(feature = "bls-experimental")]
1348	fn bls381_generate(
1349		&mut self,
1350		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1351		seed: PassFatPointerAndDecode<Option<Vec<u8>>>,
1352	) -> AllocateAndReturnPointer<bls381::Public, 144> {
1353		let seed = seed.as_ref().map(|s| core::str::from_utf8(s).expect("Seed is valid utf8!"));
1354		self.extension::<KeystoreExt>()
1355			.expect("No `keystore` associated for the current context!")
1356			.bls381_generate_new(id, seed)
1357			.expect("`bls381_generate` failed")
1358	}
1359
1360	/// Generate a 'bls12-381' Proof Of Possession for the corresponding public key.
1361	///
1362	/// Returns the Proof Of Possession as an option of the ['bls381::Signature'] type
1363	/// or 'None' if an error occurs.
1364	#[cfg(feature = "bls-experimental")]
1365	fn bls381_generate_proof_of_possession(
1366		&mut self,
1367		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1368		pub_key: PassPointerAndRead<&bls381::Public, 144>,
1369		owner: PassFatPointerAndRead<&[u8]>,
1370	) -> AllocateAndReturnByCodec<Option<bls381::ProofOfPossession>> {
1371		self.extension::<KeystoreExt>()
1372			.expect("No `keystore` associated for the current context!")
1373			.bls381_generate_proof_of_possession(id, pub_key, owner)
1374			.ok()
1375			.flatten()
1376	}
1377
1378	/// Generate combination `ecdsa & bls12-381` key for the given key type using an optional `seed`
1379	/// and store it in the keystore.
1380	///
1381	/// The `seed` needs to be a valid utf8.
1382	///
1383	/// Returns the public key.
1384	#[cfg(feature = "bls-experimental")]
1385	fn ecdsa_bls381_generate(
1386		&mut self,
1387		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1388		seed: PassFatPointerAndDecode<Option<Vec<u8>>>,
1389	) -> AllocateAndReturnPointer<ecdsa_bls381::Public, { 144 + 33 }> {
1390		let seed = seed.as_ref().map(|s| core::str::from_utf8(s).expect("Seed is valid utf8!"));
1391		self.extension::<KeystoreExt>()
1392			.expect("No `keystore` associated for the current context!")
1393			.ecdsa_bls381_generate_new(id, seed)
1394			.expect("`ecdsa_bls381_generate` failed")
1395	}
1396
1397	/// Generate a `bandersnatch` key pair for the given key type using an optional
1398	/// `seed` and store it in the keystore.
1399	///
1400	/// The `seed` needs to be a valid utf8.
1401	///
1402	/// Returns the public key.
1403	#[cfg(feature = "bandersnatch-experimental")]
1404	fn bandersnatch_generate(
1405		&mut self,
1406		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1407		seed: PassFatPointerAndDecode<Option<Vec<u8>>>,
1408	) -> AllocateAndReturnPointer<bandersnatch::Public, 32> {
1409		let seed = seed.as_ref().map(|s| core::str::from_utf8(s).expect("Seed is valid utf8!"));
1410		self.extension::<KeystoreExt>()
1411			.expect("No `keystore` associated for the current context!")
1412			.bandersnatch_generate_new(id, seed)
1413			.expect("`bandernatch_generate` failed")
1414	}
1415
1416	/// Sign the given `msg` with the `bandersnatch` key that corresponds to the given public key
1417	/// and key type in the keystore.
1418	///
1419	/// Returns the signature or `None` if an error occurred.
1420	#[cfg(feature = "bandersnatch-experimental")]
1421	fn bandersnatch_sign(
1422		&mut self,
1423		id: PassPointerAndReadCopy<KeyTypeId, 4>,
1424		pub_key: PassPointerAndRead<&bandersnatch::Public, 32>,
1425		msg: PassFatPointerAndRead<&[u8]>,
1426	) -> AllocateAndReturnByCodec<Option<bandersnatch::Signature>> {
1427		self.extension::<KeystoreExt>()
1428			.expect("No `keystore` associated for the current context!")
1429			.bandersnatch_sign(id, pub_key, msg)
1430			.ok()
1431			.flatten()
1432	}
1433}
1434
1435/// Interface that provides functions for hashing with different algorithms.
1436#[runtime_interface]
1437pub trait Hashing {
1438	/// Conduct a 256-bit Keccak hash.
1439	fn keccak_256(data: PassFatPointerAndRead<&[u8]>) -> AllocateAndReturnPointer<[u8; 32], 32> {
1440		sp_crypto_hashing::keccak_256(data)
1441	}
1442
1443	/// Conduct a 512-bit Keccak hash.
1444	fn keccak_512(data: PassFatPointerAndRead<&[u8]>) -> AllocateAndReturnPointer<[u8; 64], 64> {
1445		sp_crypto_hashing::keccak_512(data)
1446	}
1447
1448	/// Conduct a 256-bit Sha2 hash.
1449	fn sha2_256(data: PassFatPointerAndRead<&[u8]>) -> AllocateAndReturnPointer<[u8; 32], 32> {
1450		sp_crypto_hashing::sha2_256(data)
1451	}
1452
1453	/// Conduct a 128-bit Blake2 hash.
1454	fn blake2_128(data: PassFatPointerAndRead<&[u8]>) -> AllocateAndReturnPointer<[u8; 16], 16> {
1455		sp_crypto_hashing::blake2_128(data)
1456	}
1457
1458	/// Conduct a 256-bit Blake2 hash.
1459	fn blake2_256(data: PassFatPointerAndRead<&[u8]>) -> AllocateAndReturnPointer<[u8; 32], 32> {
1460		sp_crypto_hashing::blake2_256(data)
1461	}
1462
1463	/// Conduct four XX hashes to give a 256-bit result.
1464	fn twox_256(data: PassFatPointerAndRead<&[u8]>) -> AllocateAndReturnPointer<[u8; 32], 32> {
1465		sp_crypto_hashing::twox_256(data)
1466	}
1467
1468	/// Conduct two XX hashes to give a 128-bit result.
1469	fn twox_128(data: PassFatPointerAndRead<&[u8]>) -> AllocateAndReturnPointer<[u8; 16], 16> {
1470		sp_crypto_hashing::twox_128(data)
1471	}
1472
1473	/// Conduct two XX hashes to give a 64-bit result.
1474	fn twox_64(data: PassFatPointerAndRead<&[u8]>) -> AllocateAndReturnPointer<[u8; 8], 8> {
1475		sp_crypto_hashing::twox_64(data)
1476	}
1477}
1478
1479/// Interface that provides transaction indexing API.
1480#[runtime_interface]
1481pub trait TransactionIndex {
1482	/// Indexes the specified transaction for the given `extrinsic` and `context_hash`.
1483	fn index(
1484		&mut self,
1485		extrinsic: u32,
1486		size: u32,
1487		context_hash: PassPointerAndReadCopy<[u8; 32], 32>,
1488	) {
1489		self.storage_index_transaction(extrinsic, &context_hash, size);
1490	}
1491
1492	/// Renews the transaction index entry for the given `extrinsic` using the provided
1493	/// `context_hash`.
1494	fn renew(&mut self, extrinsic: u32, context_hash: PassPointerAndReadCopy<[u8; 32], 32>) {
1495		self.storage_renew_transaction_index(extrinsic, &context_hash);
1496	}
1497}
1498
1499/// Interface that provides functions to access the Offchain DB.
1500#[runtime_interface]
1501pub trait OffchainIndex {
1502	/// Write a key value pair to the Offchain DB database in a buffered fashion.
1503	fn set(&mut self, key: PassFatPointerAndRead<&[u8]>, value: PassFatPointerAndRead<&[u8]>) {
1504		self.set_offchain_storage(key, Some(value));
1505	}
1506
1507	/// Remove a key and its associated value from the Offchain DB.
1508	fn clear(&mut self, key: PassFatPointerAndRead<&[u8]>) {
1509		self.set_offchain_storage(key, None);
1510	}
1511}
1512
1513#[cfg(not(substrate_runtime))]
1514sp_externalities::decl_extension! {
1515	/// Deprecated verification context.
1516	///
1517	/// Stores the combined result of all verifications that are done in the same context.
1518	struct VerificationExtDeprecated(bool);
1519}
1520
1521/// Interface that provides functions to access the offchain functionality.
1522///
1523/// These functions are being made available to the runtime and are called by the runtime.
1524#[runtime_interface]
1525pub trait Offchain {
1526	/// Returns if the local node is a potential validator.
1527	///
1528	/// Even if this function returns `true`, it does not mean that any keys are configured
1529	/// and that the validator is registered in the chain.
1530	fn is_validator(&mut self) -> bool {
1531		self.extension::<OffchainWorkerExt>()
1532			.expect("is_validator can be called only in the offchain worker context")
1533			.is_validator()
1534	}
1535
1536	/// Submit an encoded transaction to the pool.
1537	///
1538	/// The transaction will end up in the pool.
1539	fn submit_transaction(
1540		&mut self,
1541		data: PassFatPointerAndRead<Vec<u8>>,
1542	) -> AllocateAndReturnByCodec<Result<(), ()>> {
1543		self.extension::<TransactionPoolExt>()
1544			.expect(
1545				"submit_transaction can be called only in the offchain call context with
1546				TransactionPool capabilities enabled",
1547			)
1548			.submit_transaction(data)
1549	}
1550
1551	/// Returns information about the local node's network state.
1552	fn network_state(&mut self) -> AllocateAndReturnByCodec<Result<OpaqueNetworkState, ()>> {
1553		self.extension::<OffchainWorkerExt>()
1554			.expect("network_state can be called only in the offchain worker context")
1555			.network_state()
1556	}
1557
1558	/// Returns current UNIX timestamp (in millis)
1559	fn timestamp(&mut self) -> ReturnAs<Timestamp, u64> {
1560		self.extension::<OffchainWorkerExt>()
1561			.expect("timestamp can be called only in the offchain worker context")
1562			.timestamp()
1563	}
1564
1565	/// Pause the execution until `deadline` is reached.
1566	fn sleep_until(&mut self, deadline: PassAs<Timestamp, u64>) {
1567		self.extension::<OffchainWorkerExt>()
1568			.expect("sleep_until can be called only in the offchain worker context")
1569			.sleep_until(deadline)
1570	}
1571
1572	/// Returns a random seed.
1573	///
1574	/// This is a truly random, non-deterministic seed generated by host environment.
1575	/// Obviously fine in the off-chain worker context.
1576	fn random_seed(&mut self) -> AllocateAndReturnPointer<[u8; 32], 32> {
1577		self.extension::<OffchainWorkerExt>()
1578			.expect("random_seed can be called only in the offchain worker context")
1579			.random_seed()
1580	}
1581
1582	/// Sets a value in the local storage.
1583	///
1584	/// Note this storage is not part of the consensus, it's only accessible by
1585	/// offchain worker tasks running on the same machine. It IS persisted between runs.
1586	fn local_storage_set(
1587		&mut self,
1588		kind: PassAs<StorageKind, u32>,
1589		key: PassFatPointerAndRead<&[u8]>,
1590		value: PassFatPointerAndRead<&[u8]>,
1591	) {
1592		self.extension::<OffchainDbExt>()
1593			.expect(
1594				"local_storage_set can be called only in the offchain call context with
1595				OffchainDb extension",
1596			)
1597			.local_storage_set(kind, key, value)
1598	}
1599
1600	/// Remove a value from the local storage.
1601	///
1602	/// Note this storage is not part of the consensus, it's only accessible by
1603	/// offchain worker tasks running on the same machine. It IS persisted between runs.
1604	fn local_storage_clear(
1605		&mut self,
1606		kind: PassAs<StorageKind, u32>,
1607		key: PassFatPointerAndRead<&[u8]>,
1608	) {
1609		self.extension::<OffchainDbExt>()
1610			.expect(
1611				"local_storage_clear can be called only in the offchain call context with
1612				OffchainDb extension",
1613			)
1614			.local_storage_clear(kind, key)
1615	}
1616
1617	/// Sets a value in the local storage if it matches current value.
1618	///
1619	/// Since multiple offchain workers may be running concurrently, to prevent
1620	/// data races use CAS to coordinate between them.
1621	///
1622	/// Returns `true` if the value has been set, `false` otherwise.
1623	///
1624	/// Note this storage is not part of the consensus, it's only accessible by
1625	/// offchain worker tasks running on the same machine. It IS persisted between runs.
1626	fn local_storage_compare_and_set(
1627		&mut self,
1628		kind: PassAs<StorageKind, u32>,
1629		key: PassFatPointerAndRead<&[u8]>,
1630		old_value: PassFatPointerAndDecode<Option<Vec<u8>>>,
1631		new_value: PassFatPointerAndRead<&[u8]>,
1632	) -> bool {
1633		self.extension::<OffchainDbExt>()
1634			.expect(
1635				"local_storage_compare_and_set can be called only in the offchain call context
1636				with OffchainDb extension",
1637			)
1638			.local_storage_compare_and_set(kind, key, old_value.as_deref(), new_value)
1639	}
1640
1641	/// Gets a value from the local storage.
1642	///
1643	/// If the value does not exist in the storage `None` will be returned.
1644	/// Note this storage is not part of the consensus, it's only accessible by
1645	/// offchain worker tasks running on the same machine. It IS persisted between runs.
1646	fn local_storage_get(
1647		&mut self,
1648		kind: PassAs<StorageKind, u32>,
1649		key: PassFatPointerAndRead<&[u8]>,
1650	) -> AllocateAndReturnByCodec<Option<Vec<u8>>> {
1651		self.extension::<OffchainDbExt>()
1652			.expect(
1653				"local_storage_get can be called only in the offchain call context with
1654				OffchainDb extension",
1655			)
1656			.local_storage_get(kind, key)
1657	}
1658
1659	/// Initiates a http request given HTTP verb and the URL.
1660	///
1661	/// Meta is a future-reserved field containing additional, parity-scale-codec encoded
1662	/// parameters. Returns the id of newly started request.
1663	fn http_request_start(
1664		&mut self,
1665		method: PassFatPointerAndRead<&str>,
1666		uri: PassFatPointerAndRead<&str>,
1667		meta: PassFatPointerAndRead<&[u8]>,
1668	) -> AllocateAndReturnByCodec<Result<HttpRequestId, ()>> {
1669		self.extension::<OffchainWorkerExt>()
1670			.expect("http_request_start can be called only in the offchain worker context")
1671			.http_request_start(method, uri, meta)
1672	}
1673
1674	/// Append header to the request.
1675	fn http_request_add_header(
1676		&mut self,
1677		request_id: PassAs<HttpRequestId, u16>,
1678		name: PassFatPointerAndRead<&str>,
1679		value: PassFatPointerAndRead<&str>,
1680	) -> AllocateAndReturnByCodec<Result<(), ()>> {
1681		self.extension::<OffchainWorkerExt>()
1682			.expect("http_request_add_header can be called only in the offchain worker context")
1683			.http_request_add_header(request_id, name, value)
1684	}
1685
1686	/// Write a chunk of request body.
1687	///
1688	/// Writing an empty chunks finalizes the request.
1689	/// Passing `None` as deadline blocks forever.
1690	///
1691	/// Returns an error in case deadline is reached or the chunk couldn't be written.
1692	fn http_request_write_body(
1693		&mut self,
1694		request_id: PassAs<HttpRequestId, u16>,
1695		chunk: PassFatPointerAndRead<&[u8]>,
1696		deadline: PassFatPointerAndDecode<Option<Timestamp>>,
1697	) -> AllocateAndReturnByCodec<Result<(), HttpError>> {
1698		self.extension::<OffchainWorkerExt>()
1699			.expect("http_request_write_body can be called only in the offchain worker context")
1700			.http_request_write_body(request_id, chunk, deadline)
1701	}
1702
1703	/// Block and wait for the responses for given requests.
1704	///
1705	/// Returns a vector of request statuses (the len is the same as ids).
1706	/// Note that if deadline is not provided the method will block indefinitely,
1707	/// otherwise unready responses will produce `DeadlineReached` status.
1708	///
1709	/// Passing `None` as deadline blocks forever.
1710	fn http_response_wait(
1711		&mut self,
1712		ids: PassFatPointerAndDecodeSlice<&[HttpRequestId]>,
1713		deadline: PassFatPointerAndDecode<Option<Timestamp>>,
1714	) -> AllocateAndReturnByCodec<Vec<HttpRequestStatus>> {
1715		self.extension::<OffchainWorkerExt>()
1716			.expect("http_response_wait can be called only in the offchain worker context")
1717			.http_response_wait(ids, deadline)
1718	}
1719
1720	/// Read all response headers.
1721	///
1722	/// Returns a vector of pairs `(HeaderKey, HeaderValue)`.
1723	/// NOTE: response headers have to be read before response body.
1724	fn http_response_headers(
1725		&mut self,
1726		request_id: PassAs<HttpRequestId, u16>,
1727	) -> AllocateAndReturnByCodec<Vec<(Vec<u8>, Vec<u8>)>> {
1728		self.extension::<OffchainWorkerExt>()
1729			.expect("http_response_headers can be called only in the offchain worker context")
1730			.http_response_headers(request_id)
1731	}
1732
1733	/// Read a chunk of body response to given buffer.
1734	///
1735	/// Returns the number of bytes written or an error in case a deadline
1736	/// is reached or server closed the connection.
1737	/// If `0` is returned it means that the response has been fully consumed
1738	/// and the `request_id` is now invalid.
1739	/// NOTE: this implies that response headers must be read before draining the body.
1740	/// Passing `None` as a deadline blocks forever.
1741	fn http_response_read_body(
1742		&mut self,
1743		request_id: PassAs<HttpRequestId, u16>,
1744		buffer: PassFatPointerAndReadWrite<&mut [u8]>,
1745		deadline: PassFatPointerAndDecode<Option<Timestamp>>,
1746	) -> AllocateAndReturnByCodec<Result<u32, HttpError>> {
1747		self.extension::<OffchainWorkerExt>()
1748			.expect("http_response_read_body can be called only in the offchain worker context")
1749			.http_response_read_body(request_id, buffer, deadline)
1750			.map(|r| r as u32)
1751	}
1752
1753	/// Set the authorized nodes and authorized_only flag.
1754	fn set_authorized_nodes(
1755		&mut self,
1756		nodes: PassFatPointerAndDecode<Vec<OpaquePeerId>>,
1757		authorized_only: bool,
1758	) {
1759		self.extension::<OffchainWorkerExt>()
1760			.expect("set_authorized_nodes can be called only in the offchain worker context")
1761			.set_authorized_nodes(nodes, authorized_only)
1762	}
1763}
1764
1765/// Wasm only interface that provides functions for calling into the allocator.
1766#[runtime_interface(wasm_only)]
1767pub trait Allocator {
1768	/// Malloc the given number of bytes and return the pointer to the allocated memory location.
1769	fn malloc(&mut self, size: u32) -> Pointer<u8> {
1770		self.allocate_memory(size).expect("Failed to allocate memory")
1771	}
1772
1773	/// Free the given pointer.
1774	fn free(&mut self, ptr: Pointer<u8>) {
1775		self.deallocate_memory(ptr).expect("Failed to deallocate memory")
1776	}
1777}
1778
1779/// WASM-only interface which allows for aborting the execution in case
1780/// of an unrecoverable error.
1781#[runtime_interface(wasm_only)]
1782pub trait PanicHandler {
1783	/// Aborts the current execution with the given error message.
1784	#[trap_on_return]
1785	fn abort_on_panic(&mut self, message: PassFatPointerAndRead<&str>) {
1786		self.register_panic_error_message(message);
1787	}
1788}
1789
1790/// Interface that provides functions for logging from within the runtime.
1791#[runtime_interface]
1792pub trait Logging {
1793	/// Request to print a log message on the host.
1794	///
1795	/// Note that this will be only displayed if the host is enabled to display log messages with
1796	/// given level and target.
1797	///
1798	/// Instead of using directly, prefer setting up `RuntimeLogger` and using `log` macros.
1799	fn log(
1800		level: PassAs<RuntimeInterfaceLogLevel, u8>,
1801		target: PassFatPointerAndRead<&str>,
1802		message: PassFatPointerAndRead<&[u8]>,
1803	) {
1804		if let Ok(message) = core::str::from_utf8(message) {
1805			log::log!(target: target, log::Level::from(level), "{}", message)
1806		}
1807	}
1808
1809	/// Returns the max log level used by the host.
1810	fn max_level() -> ReturnAs<LogLevelFilter, u8> {
1811		log::max_level().into()
1812	}
1813}
1814
1815/// Interface to provide tracing facilities for wasm. Modelled after tokios `tracing`-crate
1816/// interfaces. See `sp-tracing` for more information.
1817#[runtime_interface(wasm_only, no_tracing)]
1818pub trait WasmTracing {
1819	/// Whether the span described in `WasmMetadata` should be traced wasm-side
1820	/// On the host converts into a static Metadata and checks against the global `tracing`
1821	/// dispatcher.
1822	///
1823	/// When returning false the calling code should skip any tracing-related execution. In general
1824	/// within the same block execution this is not expected to change and it doesn't have to be
1825	/// checked more than once per metadata. This exists for optimisation purposes but is still not
1826	/// cheap as it will jump the wasm-native-barrier every time it is called. So an implementation
1827	/// might chose to cache the result for the execution of the entire block.
1828	fn enabled(&mut self, metadata: PassFatPointerAndDecode<sp_tracing::WasmMetadata>) -> bool {
1829		let metadata: &tracing_core::metadata::Metadata<'static> = (&metadata).into();
1830		tracing::dispatcher::get_default(|d| d.enabled(metadata))
1831	}
1832
1833	/// Open a new span with the given attributes. Return the u64 Id of the span.
1834	///
1835	/// On the native side this goes through the default `tracing` dispatcher to register the span
1836	/// and then calls `clone_span` with the ID to signal that we are keeping it around on the wasm-
1837	/// side even after the local span is dropped. The resulting ID is then handed over to the wasm-
1838	/// side.
1839	fn enter_span(
1840		&mut self,
1841		span: PassFatPointerAndDecode<sp_tracing::WasmEntryAttributes>,
1842	) -> u64 {
1843		let span: tracing::Span = span.into();
1844		match span.id() {
1845			Some(id) => tracing::dispatcher::get_default(|d| {
1846				// inform dispatch that we'll keep the ID around
1847				// then enter it immediately
1848				let final_id = d.clone_span(&id);
1849				d.enter(&final_id);
1850				final_id.into_u64()
1851			}),
1852			_ => 0,
1853		}
1854	}
1855
1856	/// Emit the given event to the global tracer on the native side
1857	fn event(&mut self, event: PassFatPointerAndDecode<sp_tracing::WasmEntryAttributes>) {
1858		event.emit();
1859	}
1860
1861	/// Signal that a given span-id has been exited. On native, this directly
1862	/// proxies the span to the global dispatcher.
1863	fn exit(&mut self, span: u64) {
1864		tracing::dispatcher::get_default(|d| {
1865			let id = tracing_core::span::Id::from_u64(span);
1866			d.exit(&id);
1867		});
1868	}
1869}
1870
1871#[cfg(all(substrate_runtime, feature = "with-tracing"))]
1872mod tracing_setup {
1873	use super::wasm_tracing;
1874	use core::sync::atomic::{AtomicBool, Ordering};
1875	use tracing_core::{
1876		dispatcher::{set_global_default, Dispatch},
1877		span::{Attributes, Id, Record},
1878		Event, Metadata,
1879	};
1880
1881	static TRACING_SET: AtomicBool = AtomicBool::new(false);
1882
1883	/// The PassingTracingSubscriber implements `tracing_core::Subscriber`
1884	/// and pushes the information across the runtime interface to the host
1885	struct PassingTracingSubscriber;
1886
1887	impl tracing_core::Subscriber for PassingTracingSubscriber {
1888		fn enabled(&self, metadata: &Metadata<'_>) -> bool {
1889			wasm_tracing::enabled(metadata.into())
1890		}
1891		fn new_span(&self, attrs: &Attributes<'_>) -> Id {
1892			Id::from_u64(wasm_tracing::enter_span(attrs.into()))
1893		}
1894		fn enter(&self, _: &Id) {
1895			// Do nothing, we already entered the span previously
1896		}
1897		/// Not implemented! We do not support recording values later
1898		/// Will panic when used.
1899		fn record(&self, _: &Id, _: &Record<'_>) {
1900			unimplemented! {} // this usage is not supported
1901		}
1902		/// Not implemented! We do not support recording values later
1903		/// Will panic when used.
1904		fn record_follows_from(&self, _: &Id, _: &Id) {
1905			unimplemented! {} // this usage is not supported
1906		}
1907		fn event(&self, event: &Event<'_>) {
1908			wasm_tracing::event(event.into())
1909		}
1910		fn exit(&self, span: &Id) {
1911			wasm_tracing::exit(span.into_u64())
1912		}
1913	}
1914
1915	/// Initialize tracing of sp_tracing on wasm with `with-tracing` enabled.
1916	/// Can be called multiple times from within the same process and will only
1917	/// set the global bridging subscriber once.
1918	pub fn init_tracing() {
1919		if TRACING_SET.load(Ordering::Relaxed) == false {
1920			set_global_default(Dispatch::new(PassingTracingSubscriber {}))
1921				.expect("We only ever call this once");
1922			TRACING_SET.store(true, Ordering::Relaxed);
1923		}
1924	}
1925}
1926
1927#[cfg(not(all(substrate_runtime, feature = "with-tracing")))]
1928mod tracing_setup {
1929	/// Initialize tracing of sp_tracing not necessary – noop. To enable build
1930	/// when not both `substrate_runtime` and `with-tracing`-feature.
1931	pub fn init_tracing() {}
1932}
1933
1934pub use tracing_setup::init_tracing;
1935
1936/// Crashes the execution of the program.
1937///
1938/// Equivalent to the WASM `unreachable` instruction, RISC-V `unimp` instruction,
1939/// or just the `unreachable!()` macro everywhere else.
1940pub fn unreachable() -> ! {
1941	#[cfg(target_family = "wasm")]
1942	{
1943		core::arch::wasm32::unreachable();
1944	}
1945
1946	#[cfg(any(target_arch = "riscv32", target_arch = "riscv64"))]
1947	unsafe {
1948		core::arch::asm!("unimp", options(noreturn));
1949	}
1950
1951	#[cfg(not(any(target_arch = "riscv32", target_arch = "riscv64", target_family = "wasm")))]
1952	unreachable!();
1953}
1954
1955/// A default panic handler for the runtime environment.
1956#[cfg(all(not(feature = "disable_panic_handler"), substrate_runtime))]
1957#[panic_handler]
1958pub fn panic(info: &core::panic::PanicInfo) -> ! {
1959	let message = alloc::format!("{}", info);
1960	#[cfg(feature = "improved_panic_error_reporting")]
1961	{
1962		panic_handler::abort_on_panic(&message);
1963	}
1964	#[cfg(not(feature = "improved_panic_error_reporting"))]
1965	{
1966		logging::log(RuntimeInterfaceLogLevel::Error, "runtime", message.as_bytes());
1967		unreachable();
1968	}
1969}
1970
1971/// A default OOM handler for the runtime environment.
1972#[cfg(all(not(feature = "disable_oom"), enable_alloc_error_handler))]
1973#[alloc_error_handler]
1974pub fn oom(_: core::alloc::Layout) -> ! {
1975	#[cfg(feature = "improved_panic_error_reporting")]
1976	{
1977		panic_handler::abort_on_panic("Runtime memory exhausted.");
1978	}
1979	#[cfg(not(feature = "improved_panic_error_reporting"))]
1980	{
1981		logging::log(
1982			RuntimeInterfaceLogLevel::Error,
1983			"runtime",
1984			b"Runtime memory exhausted. Aborting",
1985		);
1986		unreachable();
1987	}
1988}
1989
1990/// Type alias for Externalities implementation used in tests.
1991#[cfg(feature = "std")] // NOTE: Deliberately isn't `not(substrate_runtime)`.
1992pub type TestExternalities = sp_state_machine::TestExternalities<sp_core::Blake2Hasher>;
1993
1994/// The host functions Substrate provides for the Wasm runtime environment.
1995///
1996/// All these host functions will be callable from inside the Wasm environment.
1997#[docify::export]
1998#[cfg(not(substrate_runtime))]
1999pub type SubstrateHostFunctions = (
2000	storage::HostFunctions,
2001	default_child_storage::HostFunctions,
2002	misc::HostFunctions,
2003	wasm_tracing::HostFunctions,
2004	offchain::HostFunctions,
2005	crypto::HostFunctions,
2006	hashing::HostFunctions,
2007	allocator::HostFunctions,
2008	panic_handler::HostFunctions,
2009	logging::HostFunctions,
2010	crate::trie::HostFunctions,
2011	offchain_index::HostFunctions,
2012	transaction_index::HostFunctions,
2013);
2014
2015#[cfg(test)]
2016mod tests {
2017	use super::*;
2018	use sp_core::{crypto::UncheckedInto, map, storage::Storage};
2019	use sp_state_machine::BasicExternalities;
2020
2021	#[test]
2022	fn storage_works() {
2023		let mut t = BasicExternalities::default();
2024		t.execute_with(|| {
2025			assert_eq!(storage::get(b"hello"), None);
2026			storage::set(b"hello", b"world");
2027			assert_eq!(storage::get(b"hello"), Some(b"world".to_vec().into()));
2028			assert_eq!(storage::get(b"foo"), None);
2029			storage::set(b"foo", &[1, 2, 3][..]);
2030		});
2031
2032		t = BasicExternalities::new(Storage {
2033			top: map![b"foo".to_vec() => b"bar".to_vec()],
2034			children_default: map![],
2035		});
2036
2037		t.execute_with(|| {
2038			assert_eq!(storage::get(b"hello"), None);
2039			assert_eq!(storage::get(b"foo"), Some(b"bar".to_vec().into()));
2040		});
2041
2042		let value = vec![7u8; 35];
2043		let storage =
2044			Storage { top: map![b"foo00".to_vec() => value.clone()], children_default: map![] };
2045		t = BasicExternalities::new(storage);
2046
2047		t.execute_with(|| {
2048			assert_eq!(storage::get(b"hello"), None);
2049			assert_eq!(storage::get(b"foo00"), Some(value.clone().into()));
2050		});
2051	}
2052
2053	#[test]
2054	fn read_storage_works() {
2055		let value = b"\x0b\0\0\0Hello world".to_vec();
2056		let mut t = BasicExternalities::new(Storage {
2057			top: map![b":test".to_vec() => value.clone()],
2058			children_default: map![],
2059		});
2060
2061		t.execute_with(|| {
2062			let mut v = [0u8; 4];
2063			assert_eq!(storage::read(b":test", &mut v[..], 0).unwrap(), value.len() as u32);
2064			assert_eq!(v, [11u8, 0, 0, 0]);
2065			let mut w = [0u8; 11];
2066			assert_eq!(storage::read(b":test", &mut w[..], 4).unwrap(), value.len() as u32 - 4);
2067			assert_eq!(&w, b"Hello world");
2068		});
2069	}
2070
2071	#[test]
2072	fn clear_prefix_works() {
2073		let mut t = BasicExternalities::new(Storage {
2074			top: map![
2075				b":a".to_vec() => b"\x0b\0\0\0Hello world".to_vec(),
2076				b":abcd".to_vec() => b"\x0b\0\0\0Hello world".to_vec(),
2077				b":abc".to_vec() => b"\x0b\0\0\0Hello world".to_vec(),
2078				b":abdd".to_vec() => b"\x0b\0\0\0Hello world".to_vec()
2079			],
2080			children_default: map![],
2081		});
2082
2083		t.execute_with(|| {
2084			// We can switch to this once we enable v3 of the `clear_prefix`.
2085			// assert!(matches!(
2086			// 	storage::clear_prefix(b":abc", None),
2087			// 	MultiRemovalResults::NoneLeft { db: 2, total: 2 }
2088			//));
2089			assert!(matches!(
2090				storage::clear_prefix(b":abc", None),
2091				KillStorageResult::AllRemoved(2),
2092			));
2093
2094			assert!(storage::get(b":a").is_some());
2095			assert!(storage::get(b":abdd").is_some());
2096			assert!(storage::get(b":abcd").is_none());
2097			assert!(storage::get(b":abc").is_none());
2098
2099			// We can switch to this once we enable v3 of the `clear_prefix`.
2100			// assert!(matches!(
2101			// 	storage::clear_prefix(b":abc", None),
2102			// 	MultiRemovalResults::NoneLeft { db: 0, total: 0 }
2103			//));
2104			assert!(matches!(
2105				storage::clear_prefix(b":abc", None),
2106				KillStorageResult::AllRemoved(0),
2107			));
2108		});
2109	}
2110
2111	fn zero_ed_pub() -> ed25519::Public {
2112		[0u8; 32].unchecked_into()
2113	}
2114
2115	fn zero_ed_sig() -> ed25519::Signature {
2116		ed25519::Signature::from_raw([0u8; 64])
2117	}
2118
2119	#[test]
2120	fn use_dalek_ext_works() {
2121		let mut ext = BasicExternalities::default();
2122		ext.register_extension(UseDalekExt);
2123
2124		// With dalek the zero signature should fail to verify.
2125		ext.execute_with(|| {
2126			assert!(!crypto::ed25519_verify(&zero_ed_sig(), &Vec::new(), &zero_ed_pub()));
2127		});
2128
2129		// But with zebra it should work.
2130		BasicExternalities::default().execute_with(|| {
2131			assert!(crypto::ed25519_verify(&zero_ed_sig(), &Vec::new(), &zero_ed_pub()));
2132		})
2133	}
2134
2135	#[test]
2136	fn dalek_should_not_panic_on_invalid_signature() {
2137		let mut ext = BasicExternalities::default();
2138		ext.register_extension(UseDalekExt);
2139
2140		ext.execute_with(|| {
2141			let mut bytes = [0u8; 64];
2142			// Make it invalid
2143			bytes[63] = 0b1110_0000;
2144
2145			assert!(!crypto::ed25519_verify(
2146				&ed25519::Signature::from_raw(bytes),
2147				&Vec::new(),
2148				&zero_ed_pub()
2149			));
2150		});
2151	}
2152}