referrerpolicy=no-referrer-when-downgrade

sp_externalities/
extensions.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//! Externalities extensions storage.
19//!
20//! Externalities support to register a wide variety custom extensions. The [`Extensions`] provides
21//! some convenience functionality to store and retrieve these extensions.
22//!
23//! It is required that each extension implements the [`Extension`] trait.
24
25use crate::Error;
26use alloc::{
27	boxed::Box,
28	collections::btree_map::{BTreeMap, Entry},
29};
30use core::{
31	any::{Any, TypeId},
32	iter::FromIterator,
33	ops::DerefMut,
34};
35
36/// Informs [`Extension`] about what type of transaction is started, committed or rolled back.
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub enum TransactionType {
39	/// A transaction started by the host.
40	Host,
41	/// A transaction started by the runtime.
42	Runtime,
43}
44
45impl TransactionType {
46	/// Is `self` set to [`Self::Host`].
47	pub fn is_host(self) -> bool {
48		matches!(self, Self::Host)
49	}
50
51	/// Is `self` set to [`Self::Runtime`].
52	pub fn is_runtime(self) -> bool {
53		matches!(self, Self::Runtime)
54	}
55}
56
57/// Marker trait for types that should be registered as [`Externalities`](crate::Externalities)
58/// extension.
59///
60/// As extensions are stored as `Box<Any>`, this trait should give more confidence that the correct
61/// type is registered and requested.
62pub trait Extension: Send + 'static {
63	/// Return the extension as `&mut dyn Any`.
64	///
65	/// This is a trick to make the trait type castable into an [`Any`].
66	fn as_mut_any(&mut self) -> &mut dyn Any;
67
68	/// Get the [`TypeId`] of this `Extension`.
69	fn type_id(&self) -> TypeId;
70
71	/// Start a transaction of type `ty`.
72	fn start_transaction(&mut self, ty: TransactionType) {
73		let _ty = ty;
74	}
75
76	/// Commit a transaction of type `ty`.
77	fn commit_transaction(&mut self, ty: TransactionType) {
78		let _ty = ty;
79	}
80
81	/// Rollback a transaction of type `ty`.
82	fn rollback_transaction(&mut self, ty: TransactionType) {
83		let _ty = ty;
84	}
85}
86
87impl Extension for Box<dyn Extension> {
88	fn as_mut_any(&mut self) -> &mut dyn Any {
89		(**self).as_mut_any()
90	}
91
92	fn type_id(&self) -> TypeId {
93		(**self).type_id()
94	}
95
96	fn start_transaction(&mut self, ty: TransactionType) {
97		(**self).start_transaction(ty);
98	}
99
100	fn commit_transaction(&mut self, ty: TransactionType) {
101		(**self).commit_transaction(ty);
102	}
103
104	fn rollback_transaction(&mut self, ty: TransactionType) {
105		(**self).rollback_transaction(ty);
106	}
107}
108
109/// Macro for declaring an extension that usable with [`Extensions`].
110///
111/// The extension will be an unit wrapper struct that implements [`Extension`], `Deref` and
112/// `DerefMut`. The wrapped type is given by the user.
113///
114/// # Example
115/// ```
116/// # use sp_externalities::decl_extension;
117/// decl_extension! {
118///     /// Some test extension
119///     struct TestExt(String);
120/// }
121/// ```
122///
123/// The [`Extension`] trait provides hooks that are called when starting, committing or rolling back
124/// a transaction. These can be implemented with the macro as well:
125/// ```
126/// # use sp_externalities::{decl_extension, TransactionType};
127/// decl_extension! {
128///     /// Some test extension
129///     struct TestExtWithCallback(String);
130///
131///     impl TestExtWithCallback {
132///         fn start_transaction(&mut self, ty: TransactionType) {
133///             // do something cool
134///         }
135///
136///         // The other methods `commit_transaction` and `rollback_transaction` can also
137///         // be implemented in the same way.
138///     }
139/// }
140/// ```
141#[macro_export]
142macro_rules! decl_extension {
143	(
144		$( #[ $attr:meta ] )*
145		$vis:vis struct $ext_name:ident ($inner:ty);
146		$(
147			impl $ext_name_impl:ident {
148				$(
149					$impls:tt
150				)*
151			}
152		)*
153	) => {
154		$( #[ $attr ] )*
155		$vis struct $ext_name (pub $inner);
156
157		impl $crate::Extension for $ext_name {
158			fn as_mut_any(&mut self) -> &mut dyn core::any::Any {
159				self
160			}
161
162			fn type_id(&self) -> core::any::TypeId {
163				core::any::Any::type_id(self)
164			}
165
166			$(
167				$(
168					$impls
169				)*
170			)*
171		}
172
173		impl $ext_name {
174			/// Returns the `TypeId` of this extension.
175			#[allow(dead_code)]
176			pub fn type_id() -> core::any::TypeId {
177				core::any::TypeId::of::<Self>()
178			}
179		}
180
181		impl core::ops::Deref for $ext_name {
182			type Target = $inner;
183
184			fn deref(&self) -> &Self::Target {
185				&self.0
186			}
187		}
188
189		impl core::ops::DerefMut for $ext_name {
190			fn deref_mut(&mut self) -> &mut Self::Target {
191				&mut self.0
192			}
193		}
194
195		impl From<$inner> for $ext_name {
196			fn from(inner: $inner) -> Self {
197				Self(inner)
198			}
199 		}
200	};
201	(
202		$( #[ $attr:meta ] )*
203		$vis:vis struct $ext_name:ident;
204	) => {
205		$( #[ $attr ] )*
206		$vis struct $ext_name;
207
208		impl $crate::Extension for $ext_name {
209			fn as_mut_any(&mut self) -> &mut dyn core::any::Any {
210				self
211			}
212
213			fn type_id(&self) -> core::any::TypeId {
214				core::any::Any::type_id(self)
215			}
216		}
217
218		impl $ext_name {
219			/// Returns the `TypeId` of this extension.
220			#[allow(dead_code)]
221			pub fn type_id() -> core::any::TypeId {
222				core::any::TypeId::of::<Self>()
223			}
224		}
225	}
226}
227
228/// Something that provides access to the [`Extensions`] store.
229///
230/// This is a super trait of the [`Externalities`](crate::Externalities).
231pub trait ExtensionStore {
232	/// Tries to find a registered extension by the given `type_id` and returns it as a `&mut dyn
233	/// Any`.
234	///
235	/// It is advised to use [`ExternalitiesExt::extension`](crate::ExternalitiesExt::extension)
236	/// instead of this function to get type system support and automatic type downcasting.
237	fn extension_by_type_id(&mut self, type_id: TypeId) -> Option<&mut dyn Any>;
238
239	/// Register extension `extension` with specified `type_id`.
240	///
241	/// It should return error if extension is already registered.
242	fn register_extension_with_type_id(
243		&mut self,
244		type_id: TypeId,
245		extension: Box<dyn Extension>,
246	) -> Result<(), Error>;
247
248	/// Deregister extension with specified 'type_id' and drop it.
249	///
250	/// It should return error if extension is not registered.
251	fn deregister_extension_by_type_id(&mut self, type_id: TypeId) -> Result<(), Error>;
252}
253
254/// Stores extensions that should be made available through the externalities.
255#[derive(Default)]
256pub struct Extensions {
257	extensions: BTreeMap<TypeId, Box<dyn Extension>>,
258}
259
260impl core::fmt::Debug for Extensions {
261	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
262		write!(f, "Extensions: ({})", self.extensions.len())
263	}
264}
265
266impl Extensions {
267	/// Create new instance of `Self`.
268	pub fn new() -> Self {
269		Self::default()
270	}
271
272	/// Register the given extension.
273	pub fn register<E: Extension>(&mut self, ext: E) {
274		let type_id = ext.type_id();
275		self.extensions.insert(type_id, Box::new(ext));
276	}
277
278	/// Returns `true` if an extension for the given `type_id` is already registered.
279	pub fn is_registered(&self, type_id: TypeId) -> bool {
280		self.extensions.contains_key(&type_id)
281	}
282
283	/// Register extension `extension` using the given `type_id`.
284	pub fn register_with_type_id(
285		&mut self,
286		type_id: TypeId,
287		extension: Box<dyn Extension>,
288	) -> Result<(), Error> {
289		match self.extensions.entry(type_id) {
290			Entry::Vacant(vacant) => {
291				vacant.insert(extension);
292				Ok(())
293			},
294			Entry::Occupied(_) => Err(Error::ExtensionAlreadyRegistered),
295		}
296	}
297
298	/// Return a mutable reference to the requested extension.
299	pub fn get_mut(&mut self, ext_type_id: TypeId) -> Option<&mut dyn Any> {
300		self.extensions
301			.get_mut(&ext_type_id)
302			.map(DerefMut::deref_mut)
303			.map(Extension::as_mut_any)
304	}
305
306	/// Deregister extension for the given `type_id`.
307	///
308	/// Returns `true` when the extension was registered.
309	pub fn deregister(&mut self, type_id: TypeId) -> bool {
310		self.extensions.remove(&type_id).is_some()
311	}
312
313	/// Returns a mutable iterator over all extensions.
314	pub fn iter_mut(&mut self) -> impl Iterator<Item = (&TypeId, &mut Box<dyn Extension>)> {
315		self.extensions.iter_mut()
316	}
317
318	/// Merge `other` into `self`.
319	///
320	/// If both contain the same extension, the extension instance of `other` will overwrite the
321	/// instance found in `self`.
322	pub fn merge(&mut self, other: Self) {
323		self.extensions.extend(other.extensions);
324	}
325
326	/// Start a transaction of type `ty`.
327	pub fn start_transaction(&mut self, ty: TransactionType) {
328		self.extensions.values_mut().for_each(|e| e.start_transaction(ty));
329	}
330
331	/// Commit a transaction of type `ty`.
332	pub fn commit_transaction(&mut self, ty: TransactionType) {
333		self.extensions.values_mut().for_each(|e| e.commit_transaction(ty));
334	}
335
336	/// Rollback a transaction of type `ty`.
337	pub fn rollback_transaction(&mut self, ty: TransactionType) {
338		self.extensions.values_mut().for_each(|e| e.rollback_transaction(ty));
339	}
340
341	/// Returns an iterator that returns all stored extensions.
342	pub fn into_extensions(self) -> impl Iterator<Item = Box<dyn Extension>> {
343		self.extensions.into_values()
344	}
345}
346
347impl Extend<Extensions> for Extensions {
348	fn extend<T: IntoIterator<Item = Extensions>>(&mut self, iter: T) {
349		iter.into_iter()
350			.for_each(|ext| self.extensions.extend(ext.extensions.into_iter()));
351	}
352}
353
354impl<A: Extension> From<A> for Extensions {
355	fn from(ext: A) -> Self {
356		Self {
357			extensions: FromIterator::from_iter(
358				[(Extension::type_id(&ext), Box::new(ext) as Box<dyn Extension>)].into_iter(),
359			),
360		}
361	}
362}
363
364impl<A: Extension, B: Extension> From<(A, B)> for Extensions {
365	fn from((ext, ext2): (A, B)) -> Self {
366		Self {
367			extensions: FromIterator::from_iter(
368				[
369					(Extension::type_id(&ext), Box::new(ext) as Box<dyn Extension>),
370					(Extension::type_id(&ext2), Box::new(ext2) as Box<dyn Extension>),
371				]
372				.into_iter(),
373			),
374		}
375	}
376}
377
378impl<A: Extension, B: Extension, C: Extension> From<(A, B, C)> for Extensions {
379	fn from((ext, ext2, ext3): (A, B, C)) -> Self {
380		Self {
381			extensions: FromIterator::from_iter(
382				[
383					(Extension::type_id(&ext), Box::new(ext) as Box<dyn Extension>),
384					(Extension::type_id(&ext2), Box::new(ext2) as Box<dyn Extension>),
385					(Extension::type_id(&ext3), Box::new(ext3) as Box<dyn Extension>),
386				]
387				.into_iter(),
388			),
389		}
390	}
391}
392
393#[cfg(test)]
394mod tests {
395	use super::*;
396
397	decl_extension! {
398		struct DummyExt(u32);
399	}
400	decl_extension! {
401		struct DummyExt2(u32);
402	}
403
404	#[test]
405	fn register_and_retrieve_extension() {
406		let mut exts = Extensions::new();
407		exts.register(DummyExt(1));
408		exts.register(DummyExt2(2));
409
410		let ext = exts.get_mut(TypeId::of::<DummyExt>()).expect("Extension is registered");
411		let ext_ty = ext.downcast_mut::<DummyExt>().expect("Downcasting works");
412
413		assert_eq!(ext_ty.0, 1);
414	}
415
416	#[test]
417	fn register_box_extension() {
418		let mut exts = Extensions::new();
419		let box1: Box<dyn Extension> = Box::new(DummyExt(1));
420		let box2: Box<dyn Extension> = Box::new(DummyExt2(2));
421		exts.register(box1);
422		exts.register(box2);
423
424		{
425			let ext = exts.get_mut(TypeId::of::<DummyExt>()).expect("Extension 1 is registered");
426			let ext_ty = ext.downcast_mut::<DummyExt>().expect("Downcasting works for Extension 1");
427			assert_eq!(ext_ty.0, 1);
428		}
429		{
430			let ext2 = exts.get_mut(TypeId::of::<DummyExt2>()).expect("Extension 2 is registered");
431			let ext_ty2 =
432				ext2.downcast_mut::<DummyExt2>().expect("Downcasting works for Extension 2");
433			assert_eq!(ext_ty2.0, 2);
434		}
435	}
436
437	#[test]
438	fn from_boxed_extensions() {
439		let exts = Extensions::from((DummyExt(1), DummyExt2(2)));
440
441		assert!(exts.is_registered(DummyExt::type_id()));
442		assert!(exts.is_registered(DummyExt2::type_id()));
443	}
444}