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

            
28
extern crate alloc;
29

            
30
use alloc::{boxed::Box, vec::Vec};
31
use core::any::{Any, TypeId};
32

            
33
use sp_storage::{ChildInfo, StateVersion, TrackedStorageKey};
34

            
35
pub use extensions::{Extension, ExtensionStore, Extensions};
36
pub use scope_limited::{set_and_run_with_externalities, with_externalities};
37

            
38
mod extensions;
39
mod scope_limited;
40

            
41
/// Externalities error.
42
#[derive(Debug)]
43
pub 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]
57
pub 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

            
69
impl 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.
81
pub 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
15378909
	fn set_storage(&mut self, key: Vec<u8>, value: Vec<u8>) {
107
15378909
		self.place_storage(key, Some(value));
108
15378909
	}
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
5878035
	fn clear_storage(&mut self, key: &[u8]) {
117
5878035
		self.place_storage(key.to_vec(), None);
118
5878035
	}
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
1753755
	fn exists_storage(&mut self, key: &[u8]) -> bool {
128
1753755
		self.storage(key).is_some()
129
1753755
	}
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.
198
	fn storage_root(&mut self, state_version: StateVersion) -> Vec<u8>;
199

            
200
	/// Get the trie root of a child storage map.
201
	///
202
	/// This will also update the value of the child storage keys in the top-level storage map.
203
	///
204
	/// If the storage root equals the default hash as defined by the trie, the key in the top-level
205
	/// storage map will be removed.
206
	fn child_storage_root(
207
		&mut self,
208
		child_info: &ChildInfo,
209
		state_version: StateVersion,
210
	) -> Vec<u8>;
211

            
212
	/// Append storage item.
213
	///
214
	/// This assumes specific format of the storage item. Also there is no way to undo this
215
	/// operation.
216
	fn storage_append(&mut self, key: Vec<u8>, value: Vec<u8>);
217

            
218
	/// Start a new nested transaction.
219
	///
220
	/// This allows to either commit or roll back all changes made after this call to the
221
	/// top changes or the default child changes. For every transaction there cam be a
222
	/// matching call to either `storage_rollback_transaction` or `storage_commit_transaction`.
223
	/// Any transactions that are still open after returning from runtime are committed
224
	/// automatically.
225
	///
226
	/// Changes made without any open transaction are committed immediately.
227
	fn storage_start_transaction(&mut self);
228

            
229
	/// Rollback the last transaction started by `storage_start_transaction`.
230
	///
231
	/// Any changes made during that storage transaction are discarded. Returns an error when
232
	/// no transaction is open that can be closed.
233
	fn storage_rollback_transaction(&mut self) -> Result<(), ()>;
234

            
235
	/// Commit the last transaction started by `storage_start_transaction`.
236
	///
237
	/// Any changes made during that storage transaction are committed. Returns an error when
238
	/// no transaction is open that can be closed.
239
	fn storage_commit_transaction(&mut self) -> Result<(), ()>;
240

            
241
	/// Index specified transaction slice and store it.
242
	fn storage_index_transaction(&mut self, _index: u32, _hash: &[u8], _size: u32) {
243
		unimplemented!("storage_index_transaction");
244
	}
245

            
246
	/// Renew existing piece of transaction storage.
247
	fn storage_renew_transaction_index(&mut self, _index: u32, _hash: &[u8]) {
248
		unimplemented!("storage_renew_transaction_index");
249
	}
250

            
251
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
252
	/// Benchmarking related functionality and shouldn't be used anywhere else!
253
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
254
	///
255
	/// Wipes all changes from caches and the database.
256
	///
257
	/// The state will be reset to genesis.
258
	fn wipe(&mut self);
259

            
260
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
261
	/// Benchmarking related functionality and shouldn't be used anywhere else!
262
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
263
	///
264
	/// Commits all changes to the database and clears all caches.
265
	fn commit(&mut self);
266

            
267
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
268
	/// Benchmarking related functionality and shouldn't be used anywhere else!
269
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
270
	///
271
	/// Gets the current read/write count for the benchmarking process.
272
	fn read_write_count(&self) -> (u32, u32, u32, u32);
273

            
274
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
275
	/// Benchmarking related functionality and shouldn't be used anywhere else!
276
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
277
	///
278
	/// Resets read/write count for the benchmarking process.
279
	fn reset_read_write_count(&mut self);
280

            
281
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
282
	/// Benchmarking related functionality and shouldn't be used anywhere else!
283
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
284
	///
285
	/// Gets the current DB tracking whitelist.
286
	fn get_whitelist(&self) -> Vec<TrackedStorageKey>;
287

            
288
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
289
	/// Benchmarking related functionality and shouldn't be used anywhere else!
290
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
291
	///
292
	/// Adds new storage keys to the DB tracking whitelist.
293
	fn set_whitelist(&mut self, new: Vec<TrackedStorageKey>);
294

            
295
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
296
	/// Benchmarking related functionality and shouldn't be used anywhere else!
297
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
298
	///
299
	/// Returns estimated proof size for the state queries so far.
300
	/// Proof is reset on commit and wipe.
301
	fn proof_size(&self) -> Option<u32> {
302
		None
303
	}
304

            
305
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
306
	/// Benchmarking related functionality and shouldn't be used anywhere else!
307
	/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
308
	///
309
	/// Get all the keys that have been read or written to during the benchmark.
310
	fn get_read_and_written_keys(&self) -> Vec<(Vec<u8>, u32, u32, bool)>;
311
}
312

            
313
/// Extension for the [`Externalities`] trait.
314
pub trait ExternalitiesExt {
315
	/// Tries to find a registered extension and returns a mutable reference.
316
	fn extension<T: Any + Extension>(&mut self) -> Option<&mut T>;
317

            
318
	/// Register extension `ext`.
319
	///
320
	/// Should return error if extension is already registered or extensions are not supported.
321
	fn register_extension<T: Extension>(&mut self, ext: T) -> Result<(), Error>;
322

            
323
	/// Deregister and drop extension of `T` type.
324
	///
325
	/// Should return error if extension of type `T` is not registered or
326
	/// extensions are not supported.
327
	fn deregister_extension<T: Extension>(&mut self) -> Result<(), Error>;
328
}
329

            
330
impl ExternalitiesExt for &mut dyn Externalities {
331
1876
	fn extension<T: Any + Extension>(&mut self) -> Option<&mut T> {
332
1876
		self.extension_by_type_id(TypeId::of::<T>()).and_then(<dyn Any>::downcast_mut)
333
1876
	}
334

            
335
	fn register_extension<T: Extension>(&mut self, ext: T) -> Result<(), Error> {
336
		self.register_extension_with_type_id(TypeId::of::<T>(), Box::new(ext))
337
	}
338

            
339
	fn deregister_extension<T: Extension>(&mut self) -> Result<(), Error> {
340
		self.deregister_extension_by_type_id(TypeId::of::<T>())
341
	}
342
}