referrerpolicy=no-referrer-when-downgrade

sp_externalities/
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#![cfg_attr(not(feature = "std"), no_std)]
19
20//! Substrate externalities abstraction
21//!
22//! The externalities mainly provide access to storage and to registered extensions. Extensions
23//! are for example the keystore or the offchain externalities. These externalities are used to
24//! access the node from the runtime via the runtime interfaces.
25//!
26//! This crate exposes the main [`Externalities`] trait.
27
28extern crate alloc;
29
30use alloc::{boxed::Box, vec::Vec};
31use core::any::{Any, TypeId};
32
33use sp_storage::{ChildInfo, StateVersion, TrackedStorageKey};
34
35pub use extensions::{Extension, ExtensionStore, Extensions, TransactionType};
36pub use scope_limited::{set_and_run_with_externalities, with_externalities};
37
38mod extensions;
39mod scope_limited;
40
41/// Externalities error.
42#[derive(Debug)]
43pub enum Error {
44	/// Same extension cannot be registered twice.
45	ExtensionAlreadyRegistered,
46	/// Extensions are not supported.
47	ExtensionsAreNotSupported,
48	/// Extension `TypeId` is not registered.
49	ExtensionIsNotRegistered(TypeId),
50	/// Failed to update storage,
51	StorageUpdateFailed(&'static str),
52}
53
54/// Results concerning an operation to remove many keys.
55#[derive(codec::Encode, codec::Decode)]
56#[must_use]
57pub struct MultiRemovalResults {
58	/// A continuation cursor which, if `Some` must be provided to the subsequent removal call.
59	/// If `None` then all removals are complete and no further calls are needed.
60	pub maybe_cursor: Option<Vec<u8>>,
61	/// The number of items removed from the backend database.
62	pub backend: u32,
63	/// The number of unique keys removed, taking into account both the backend and the overlay.
64	pub unique: u32,
65	/// The number of iterations (each requiring a storage seek/read) which were done.
66	pub loops: u32,
67}
68
69impl MultiRemovalResults {
70	/// Deconstruct into the internal components.
71	///
72	/// Returns `(maybe_cursor, backend, unique, loops)`.
73	pub fn deconstruct(self) -> (Option<Vec<u8>>, u32, u32, u32) {
74		(self.maybe_cursor, self.backend, self.unique, self.loops)
75	}
76}
77
78/// The Substrate externalities.
79///
80/// Provides access to the storage and to other registered extensions.
81pub trait Externalities: ExtensionStore {
82	/// Write a key value pair to the offchain storage database.
83	fn set_offchain_storage(&mut self, key: &[u8], value: Option<&[u8]>);
84
85	/// Read runtime storage.
86	fn storage(&mut self, key: &[u8]) -> Option<Vec<u8>>;
87
88	/// Get storage value hash.
89	///
90	/// This may be optimized for large values.
91	fn storage_hash(&mut self, key: &[u8]) -> Option<Vec<u8>>;
92
93	/// Get child storage value hash.
94	///
95	/// This may be optimized for large values.
96	///
97	/// Returns an `Option` that holds the SCALE encoded hash.
98	fn child_storage_hash(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
99
100	/// Read child runtime storage.
101	///
102	/// Returns an `Option` that holds the SCALE encoded hash.
103	fn child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
104
105	/// Set storage entry `key` of current contract being called (effective immediately).
106	fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
107		self.place_storage(key, Some(value));
108	}
109
110	/// Set child storage entry `key` of current contract being called (effective immediately).
111	fn set_child_storage(&mut self, child_info: &ChildInfo, key: Vec<u8>, value: Vec<u8>) {
112		self.place_child_storage(child_info, key, Some(value))
113	}
114
115	/// Clear a storage entry (`key`) of current contract being called (effective immediately).
116	fn clear_storage(&mut self, key: &[u8]) {
117		self.place_storage(key.to_vec(), None);
118	}
119
120	/// Clear a child storage entry (`key`) of current contract being called (effective
121	/// immediately).
122	fn clear_child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) {
123		self.place_child_storage(child_info, key.to_vec(), None)
124	}
125
126	/// Whether a storage entry exists.
127	fn exists_storage(&mut self, key: &[u8]) -> bool {
128		self.storage(key).is_some()
129	}
130
131	/// Whether a child storage entry exists.
132	fn exists_child_storage(&mut self, child_info: &ChildInfo, key: &[u8]) -> bool {
133		self.child_storage(child_info, key).is_some()
134	}
135
136	/// Returns the key immediately following the given key, if it exists.
137	fn next_storage_key(&mut self, key: &[u8]) -> Option<Vec<u8>>;
138
139	/// Returns the key immediately following the given key, if it exists, in child storage.
140	fn next_child_storage_key(&mut self, child_info: &ChildInfo, key: &[u8]) -> Option<Vec<u8>>;
141
142	/// Clear an entire child storage.
143	///
144	/// Deletes all keys from the overlay and up to `maybe_limit` keys from the backend. No
145	/// limit is applied if `maybe_limit` is `None`. Returns the cursor for the next call as `Some`
146	/// if the child trie deletion operation is incomplete. In this case, it should be passed into
147	/// the next call to avoid unaccounted iterations on the backend. Returns also the the number
148	/// of keys that were removed from the backend, the number of unique keys removed in total
149	/// (including from the overlay) and the number of backend iterations done.
150	///
151	/// As long as `maybe_cursor` is passed from the result of the previous call, then the number of
152	/// iterations done will only ever be one more than the number of keys removed.
153	///
154	/// # Note
155	///
156	/// An implementation is free to delete more keys than the specified limit as long as
157	/// it is able to do that in constant time.
158	fn kill_child_storage(
159		&mut self,
160		child_info: &ChildInfo,
161		maybe_limit: Option<u32>,
162		maybe_cursor: Option<&[u8]>,
163	) -> MultiRemovalResults;
164
165	/// Clear storage entries which keys are start with the given prefix.
166	///
167	/// `maybe_limit`, `maybe_cursor` and result works as for `kill_child_storage`.
168	fn clear_prefix(
169		&mut self,
170		prefix: &[u8],
171		maybe_limit: Option<u32>,
172		maybe_cursor: Option<&[u8]>,
173	) -> MultiRemovalResults;
174
175	/// Clear child storage entries which keys are start with the given prefix.
176	///
177	/// `maybe_limit`, `maybe_cursor` and result works as for `kill_child_storage`.
178	fn clear_child_prefix(
179		&mut self,
180		child_info: &ChildInfo,
181		prefix: &[u8],
182		maybe_limit: Option<u32>,
183		maybe_cursor: Option<&[u8]>,
184	) -> MultiRemovalResults;
185
186	/// Set or clear a storage entry (`key`) of current contract being called (effective
187	/// immediately).
188	fn place_storage(&mut self, key: Vec<u8>, value: Option<Vec<u8>>);
189
190	/// Set or clear a child storage entry.
191	fn place_child_storage(&mut self, child_info: &ChildInfo, key: Vec<u8>, value: Option<Vec<u8>>);
192
193	/// Get the trie root of the current storage map.
194	///
195	/// This will also update all child storage keys in the top-level storage map.
196	///
197	/// The returned hash is defined by the `Block` and is SCALE encoded. The trie layout follows
198	/// the state version the externalities were configured with.
199	fn storage_root(&mut self) -> Vec<u8>;
200
201	/// Get the trie root of the current storage map using the given `state_version`.
202	///
203	/// Same as [`Self::storage_root`], but the trie layout follows `state_version` instead of
204	/// the configured state version. Used by the legacy host functions that pass the state
205	/// version explicitly.
206	fn storage_root_with_version(&mut self, state_version: StateVersion) -> Vec<u8>;
207
208	/// Get the trie root of a child storage map.
209	///
210	/// This will also update the value of the child storage keys in the top-level storage map.
211	///
212	/// If the storage root equals the default hash as defined by the trie, the key in the top-level
213	/// storage map will be removed.
214	///
215	/// The trie layout follows the state version the externalities were configured with.
216	fn child_storage_root(&mut self, child_info: &ChildInfo) -> Vec<u8>;
217
218	/// Get the trie root of a child storage map using the given `state_version`.
219	///
220	/// Same as [`Self::child_storage_root`], but the trie layout follows `state_version` instead
221	/// of the configured state version. Used by the legacy host functions that pass the state
222	/// version explicitly.
223	fn child_storage_root_with_version(
224		&mut self,
225		child_info: &ChildInfo,
226		state_version: StateVersion,
227	) -> Vec<u8>;
228
229	/// Append storage item.
230	///
231	/// This assumes specific format of the storage item. Also there is no way to undo this
232	/// operation.
233	fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>);
234
235	/// Start a new nested transaction.
236	///
237	/// This allows to either commit or roll back all changes made after this call to the
238	/// top changes or the default child changes. For every transaction there cam be a
239	/// matching call to either `storage_rollback_transaction` or `storage_commit_transaction`.
240	/// Any transactions that are still open after returning from runtime are committed
241	/// automatically.
242	///
243	/// Changes made without any open transaction are committed immediately.
244	fn storage_start_transaction(&mut self);
245
246	/// Rollback the last transaction started by `storage_start_transaction`.
247	///
248	/// Any changes made during that storage transaction are discarded. Returns an error when
249	/// no transaction is open that can be closed.
250	fn storage_rollback_transaction(&mut self) -> Result<(), ()>;
251
252	/// Commit the last transaction started by `storage_start_transaction`.
253	///
254	/// Any changes made during that storage transaction are committed. Returns an error when
255	/// no transaction is open that can be closed.
256	fn storage_commit_transaction(&mut self) -> Result<(), ()>;
257
258	/// Index specified transaction slice and store it.
259	fn storage_index_transaction(&mut self, _index: u32, _hash: &[u8], _size: u32) {
260		unimplemented!("storage_index_transaction");
261	}
262
263	/// Renew existing piece of transaction storage.
264	fn storage_renew_transaction_index(&mut self, _index: u32, _hash: &[u8]) {
265		unimplemented!("storage_renew_transaction_index");
266	}
267
268	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
269	/// Benchmarking related functionality and shouldn't be used anywhere else!
270	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
271	///
272	/// Wipes all changes from caches and the database.
273	///
274	/// The state will be reset to genesis.
275	fn wipe(&mut self);
276
277	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
278	/// Benchmarking related functionality and shouldn't be used anywhere else!
279	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
280	///
281	/// Commits all changes to the database and clears all caches.
282	fn commit(&mut self);
283
284	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
285	/// Benchmarking related functionality and shouldn't be used anywhere else!
286	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
287	///
288	/// Gets the current read/write count for the benchmarking process.
289	fn read_write_count(&self) -> (u32, u32, u32, u32);
290
291	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
292	/// Benchmarking related functionality and shouldn't be used anywhere else!
293	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
294	///
295	/// Resets read/write count for the benchmarking process.
296	fn reset_read_write_count(&mut self);
297
298	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
299	/// Benchmarking related functionality and shouldn't be used anywhere else!
300	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
301	///
302	/// Gets the current DB tracking whitelist.
303	fn get_whitelist(&self) -> Vec<TrackedStorageKey>;
304
305	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
306	/// Benchmarking related functionality and shouldn't be used anywhere else!
307	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
308	///
309	/// Adds new storage keys to the DB tracking whitelist.
310	fn set_whitelist(&mut self, new: Vec<TrackedStorageKey>);
311
312	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
313	/// Benchmarking related functionality and shouldn't be used anywhere else!
314	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
315	///
316	/// Returns estimated proof size for the state queries so far.
317	/// Proof is reset on commit and wipe.
318	fn proof_size(&self) -> Option<u32> {
319		None
320	}
321
322	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
323	/// Benchmarking related functionality and shouldn't be used anywhere else!
324	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
325	///
326	/// Get all the keys that have been read or written to during the benchmark.
327	fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)>;
328}
329
330/// Extension for the [`Externalities`] trait.
331pub trait ExternalitiesExt {
332	/// Tries to find a registered extension and returns a mutable reference.
333	fn extension<T: Any + Extension>(&mut self) -> Option<&mut T>;
334
335	/// Register extension `ext`.
336	///
337	/// Should return error if extension is already registered or extensions are not supported.
338	fn register_extension<T: Extension>(&mut self, ext: T) -> Result<(), Error>;
339
340	/// Deregister and drop extension of `T` type.
341	///
342	/// Should return error if extension of type `T` is not registered or
343	/// extensions are not supported.
344	fn deregister_extension<T: Extension>(&mut self) -> Result<(), Error>;
345}
346
347impl ExternalitiesExt for &mut dyn Externalities {
348	fn extension<T: Any + Extension>(&mut self) -> Option<&mut T> {
349		self.extension_by_type_id(TypeId::of::<T>()).and_then(<dyn Any>::downcast_mut)
350	}
351
352	fn register_extension<T: Extension>(&mut self, ext: T) -> Result<(), Error> {
353		self.register_extension_with_type_id(TypeId::of::<T>(), Box::new(ext))
354	}
355
356	fn deregister_extension<T: Extension>(&mut self) -> Result<(), Error> {
357		self.deregister_extension_by_type_id(TypeId::of::<T>())
358	}
359}