sc_client_api/
execution_extensions.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Execution extensions for runtime calls.
20//!
21//! This module is responsible for defining the execution
22//! strategy for the runtime calls and provide the right `Externalities`
23//! extensions to support APIs for particular execution context & capabilities.
24
25use parking_lot::RwLock;
26use sp_core::traits::{ReadRuntimeVersion, ReadRuntimeVersionExt};
27use sp_externalities::{Extension, Extensions};
28use sp_runtime::traits::{Block as BlockT, NumberFor};
29use std::{marker::PhantomData, sync::Arc};
30
31/// Generate the starting set of [`Extensions`].
32///
33/// These [`Extensions`] are passed to the environment a runtime is executed in.
34pub trait ExtensionsFactory<Block: BlockT>: Send + Sync {
35	/// Create [`Extensions`] for the given input.
36	///
37	/// - `block_hash`: The hash of the block in the context that extensions will be used.
38	/// - `block_number`: The number of the block in the context that extensions will be used.
39	fn extensions_for(&self, block_hash: Block::Hash, block_number: NumberFor<Block>)
40		-> Extensions;
41}
42
43impl<Block: BlockT> ExtensionsFactory<Block> for () {
44	fn extensions_for(&self, _: Block::Hash, _: NumberFor<Block>) -> Extensions {
45		Extensions::new()
46	}
47}
48
49impl<Block: BlockT, T: ExtensionsFactory<Block>> ExtensionsFactory<Block> for Vec<T> {
50	fn extensions_for(
51		&self,
52		block_hash: Block::Hash,
53		block_number: NumberFor<Block>,
54	) -> Extensions {
55		let mut exts = Extensions::new();
56		exts.extend(self.iter().map(|e| e.extensions_for(block_hash, block_number)));
57		exts
58	}
59}
60
61/// An [`ExtensionsFactory`] that registers an [`Extension`] before a certain block.
62pub struct ExtensionBeforeBlock<Block: BlockT, Ext> {
63	before: NumberFor<Block>,
64	_marker: PhantomData<fn(Ext) -> Ext>,
65}
66
67impl<Block: BlockT, Ext> ExtensionBeforeBlock<Block, Ext> {
68	/// Create the extension factory.
69	///
70	/// - `before`: The block number until the extension should be registered.
71	pub fn new(before: NumberFor<Block>) -> Self {
72		Self { before, _marker: PhantomData }
73	}
74}
75
76impl<Block: BlockT, Ext: Default + Extension> ExtensionsFactory<Block>
77	for ExtensionBeforeBlock<Block, Ext>
78{
79	fn extensions_for(&self, _: Block::Hash, block_number: NumberFor<Block>) -> Extensions {
80		let mut exts = Extensions::new();
81
82		if block_number < self.before {
83			exts.register(Ext::default());
84		}
85
86		exts
87	}
88}
89
90/// A producer of execution extensions for offchain calls.
91///
92/// This crate aggregates extensions available for the offchain calls
93/// and is responsible for producing a correct `Extensions` object.
94pub struct ExecutionExtensions<Block: BlockT> {
95	extensions_factory: RwLock<Box<dyn ExtensionsFactory<Block>>>,
96	read_runtime_version: Arc<dyn ReadRuntimeVersion>,
97}
98
99impl<Block: BlockT> ExecutionExtensions<Block> {
100	/// Create new `ExecutionExtensions` given an `extensions_factory`.
101	pub fn new(
102		extensions_factory: Option<Box<dyn ExtensionsFactory<Block>>>,
103		read_runtime_version: Arc<dyn ReadRuntimeVersion>,
104	) -> Self {
105		Self {
106			extensions_factory: extensions_factory
107				.map(RwLock::new)
108				.unwrap_or_else(|| RwLock::new(Box::new(()))),
109			read_runtime_version,
110		}
111	}
112
113	/// Set the new extensions_factory
114	pub fn set_extensions_factory(&self, maker: impl ExtensionsFactory<Block> + 'static) {
115		*self.extensions_factory.write() = Box::new(maker);
116	}
117
118	/// Produces default extensions based on the input parameters.
119	pub fn extensions(
120		&self,
121		block_hash: Block::Hash,
122		block_number: NumberFor<Block>,
123	) -> Extensions {
124		let mut extensions =
125			self.extensions_factory.read().extensions_for(block_hash, block_number);
126
127		extensions.register(ReadRuntimeVersionExt::new(self.read_runtime_version.clone()));
128		extensions
129	}
130}