pallet_utility/
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//! # Utility Pallet
19//! A stateless pallet with helpers for dispatch management which does no re-authentication.
20//!
21//! - [`Config`]
22//! - [`Call`]
23//!
24//! ## Overview
25//!
26//! This pallet contains two basic pieces of functionality:
27//! - Batch dispatch: A stateless operation, allowing any origin to execute multiple calls in a
28//!   single dispatch. This can be useful to amalgamate proposals, combining `set_code` with
29//!   corresponding `set_storage`s, for efficient multiple payouts with just a single signature
30//!   verify, or in combination with one of the other two dispatch functionality.
31//! - Pseudonymal dispatch: A stateless operation, allowing a signed origin to execute a call from
32//!   an alternative signed origin. Each account has 2 * 2**16 possible "pseudonyms" (alternative
33//!   account IDs) and these can be stacked. This can be useful as a key management tool, where you
34//!   need multiple distinct accounts (e.g. as controllers for many staking accounts), but where
35//!   it's perfectly fine to have each of them controlled by the same underlying keypair. Derivative
36//!   accounts are, for the purposes of proxy filtering considered exactly the same as the origin
37//!   and are thus hampered with the origin's filters.
38//!
39//! Since proxy filters are respected in all dispatches of this pallet, it should never need to be
40//! filtered by any proxy.
41//!
42//! ## Interface
43//!
44//! ### Dispatchable Functions
45//!
46//! #### For batch dispatch
47//! * `batch` - Dispatch multiple calls from the sender's origin.
48//!
49//! #### For pseudonymal dispatch
50//! * `as_derivative` - Dispatch a call from a derivative signed origin.
51
52// Ensure we're `no_std` when compiling for Wasm.
53#![cfg_attr(not(feature = "std"), no_std)]
54
55mod benchmarking;
56mod tests;
57pub mod weights;
58
59extern crate alloc;
60
61use alloc::{boxed::Box, vec::Vec};
62use codec::{Decode, Encode};
63use frame_support::{
64	dispatch::{extract_actual_weight, GetDispatchInfo, PostDispatchInfo},
65	traits::{IsSubType, OriginTrait, UnfilteredDispatchable},
66};
67use sp_core::TypeId;
68use sp_io::hashing::blake2_256;
69use sp_runtime::traits::{BadOrigin, Dispatchable, TrailingZeroInput};
70pub use weights::WeightInfo;
71
72pub use pallet::*;
73
74#[frame_support::pallet]
75pub mod pallet {
76	use super::*;
77	use frame_support::{dispatch::DispatchClass, pallet_prelude::*};
78	use frame_system::pallet_prelude::*;
79
80	#[pallet::pallet]
81	pub struct Pallet<T>(_);
82
83	/// Configuration trait.
84	#[pallet::config]
85	pub trait Config: frame_system::Config {
86		/// The overarching event type.
87		type RuntimeEvent: From<Event> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
88
89		/// The overarching call type.
90		type RuntimeCall: Parameter
91			+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin, PostInfo = PostDispatchInfo>
92			+ GetDispatchInfo
93			+ From<frame_system::Call<Self>>
94			+ UnfilteredDispatchable<RuntimeOrigin = Self::RuntimeOrigin>
95			+ IsSubType<Call<Self>>
96			+ IsType<<Self as frame_system::Config>::RuntimeCall>;
97
98		/// The caller origin, overarching type of all pallets origins.
99		type PalletsOrigin: Parameter +
100			Into<<Self as frame_system::Config>::RuntimeOrigin> +
101			IsType<<<Self as frame_system::Config>::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin>;
102
103		/// Weight information for extrinsics in this pallet.
104		type WeightInfo: WeightInfo;
105	}
106
107	#[pallet::event]
108	#[pallet::generate_deposit(pub(super) fn deposit_event)]
109	pub enum Event {
110		/// Batch of dispatches did not complete fully. Index of first failing dispatch given, as
111		/// well as the error.
112		BatchInterrupted { index: u32, error: DispatchError },
113		/// Batch of dispatches completed fully with no error.
114		BatchCompleted,
115		/// Batch of dispatches completed but has errors.
116		BatchCompletedWithErrors,
117		/// A single item within a Batch of dispatches has completed with no error.
118		ItemCompleted,
119		/// A single item within a Batch of dispatches has completed with error.
120		ItemFailed { error: DispatchError },
121		/// A call was dispatched.
122		DispatchedAs { result: DispatchResult },
123	}
124
125	// Align the call size to 1KB. As we are currently compiling the runtime for native/wasm
126	// the `size_of` of the `Call` can be different. To ensure that this don't leads to
127	// mismatches between native/wasm or to different metadata for the same runtime, we
128	// algin the call size. The value is chosen big enough to hopefully never reach it.
129	const CALL_ALIGN: u32 = 1024;
130
131	#[pallet::extra_constants]
132	impl<T: Config> Pallet<T> {
133		/// The limit on the number of batched calls.
134		fn batched_calls_limit() -> u32 {
135			let allocator_limit = sp_core::MAX_POSSIBLE_ALLOCATION;
136			let call_size = ((core::mem::size_of::<<T as Config>::RuntimeCall>() as u32 +
137				CALL_ALIGN - 1) /
138				CALL_ALIGN) * CALL_ALIGN;
139			// The margin to take into account vec doubling capacity.
140			let margin_factor = 3;
141
142			allocator_limit / margin_factor / call_size
143		}
144	}
145
146	#[pallet::hooks]
147	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
148		fn integrity_test() {
149			// If you hit this error, you need to try to `Box` big dispatchable parameters.
150			assert!(
151				core::mem::size_of::<<T as Config>::RuntimeCall>() as u32 <= CALL_ALIGN,
152				"Call enum size should be smaller than {} bytes.",
153				CALL_ALIGN,
154			);
155		}
156	}
157
158	#[pallet::error]
159	pub enum Error<T> {
160		/// Too many calls batched.
161		TooManyCalls,
162	}
163
164	#[pallet::call]
165	impl<T: Config> Pallet<T> {
166		/// Send a batch of dispatch calls.
167		///
168		/// May be called from any origin except `None`.
169		///
170		/// - `calls`: The calls to be dispatched from the same origin. The number of call must not
171		///   exceed the constant: `batched_calls_limit` (available in constant metadata).
172		///
173		/// If origin is root then the calls are dispatched without checking origin filter. (This
174		/// includes bypassing `frame_system::Config::BaseCallFilter`).
175		///
176		/// ## Complexity
177		/// - O(C) where C is the number of calls to be batched.
178		///
179		/// This will return `Ok` in all circumstances. To determine the success of the batch, an
180		/// event is deposited. If a call failed and the batch was interrupted, then the
181		/// `BatchInterrupted` event is deposited, along with the number of successful calls made
182		/// and the error of the failed call. If all were successful, then the `BatchCompleted`
183		/// event is deposited.
184		#[pallet::call_index(0)]
185		#[pallet::weight({
186			let (dispatch_weight, dispatch_class) = Pallet::<T>::weight_and_dispatch_class(&calls);
187			let dispatch_weight = dispatch_weight.saturating_add(T::WeightInfo::batch(calls.len() as u32));
188			(dispatch_weight, dispatch_class)
189		})]
190		pub fn batch(
191			origin: OriginFor<T>,
192			calls: Vec<<T as Config>::RuntimeCall>,
193		) -> DispatchResultWithPostInfo {
194			// Do not allow the `None` origin.
195			if ensure_none(origin.clone()).is_ok() {
196				return Err(BadOrigin.into())
197			}
198
199			let is_root = ensure_root(origin.clone()).is_ok();
200			let calls_len = calls.len();
201			ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::<T>::TooManyCalls);
202
203			// Track the actual weight of each of the batch calls.
204			let mut weight = Weight::zero();
205			for (index, call) in calls.into_iter().enumerate() {
206				let info = call.get_dispatch_info();
207				// If origin is root, don't apply any dispatch filters; root can call anything.
208				let result = if is_root {
209					call.dispatch_bypass_filter(origin.clone())
210				} else {
211					call.dispatch(origin.clone())
212				};
213				// Add the weight of this call.
214				weight = weight.saturating_add(extract_actual_weight(&result, &info));
215				if let Err(e) = result {
216					Self::deposit_event(Event::BatchInterrupted {
217						index: index as u32,
218						error: e.error,
219					});
220					// Take the weight of this function itself into account.
221					let base_weight = T::WeightInfo::batch(index.saturating_add(1) as u32);
222					// Return the actual used weight + base_weight of this call.
223					return Ok(Some(base_weight.saturating_add(weight)).into())
224				}
225				Self::deposit_event(Event::ItemCompleted);
226			}
227			Self::deposit_event(Event::BatchCompleted);
228			let base_weight = T::WeightInfo::batch(calls_len as u32);
229			Ok(Some(base_weight.saturating_add(weight)).into())
230		}
231
232		/// Send a call through an indexed pseudonym of the sender.
233		///
234		/// Filter from origin are passed along. The call will be dispatched with an origin which
235		/// use the same filter as the origin of this call.
236		///
237		/// NOTE: If you need to ensure that any account-based filtering is not honored (i.e.
238		/// because you expect `proxy` to have been used prior in the call stack and you do not want
239		/// the call restrictions to apply to any sub-accounts), then use `as_multi_threshold_1`
240		/// in the Multisig pallet instead.
241		///
242		/// NOTE: Prior to version *12, this was called `as_limited_sub`.
243		///
244		/// The dispatch origin for this call must be _Signed_.
245		#[pallet::call_index(1)]
246		#[pallet::weight({
247			let dispatch_info = call.get_dispatch_info();
248			(
249				T::WeightInfo::as_derivative()
250					// AccountData for inner call origin accountdata.
251					.saturating_add(T::DbWeight::get().reads_writes(1, 1))
252					.saturating_add(dispatch_info.call_weight),
253				dispatch_info.class,
254			)
255		})]
256		pub fn as_derivative(
257			origin: OriginFor<T>,
258			index: u16,
259			call: Box<<T as Config>::RuntimeCall>,
260		) -> DispatchResultWithPostInfo {
261			let mut origin = origin;
262			let who = ensure_signed(origin.clone())?;
263			let pseudonym = Self::derivative_account_id(who, index);
264			origin.set_caller_from(frame_system::RawOrigin::Signed(pseudonym));
265			let info = call.get_dispatch_info();
266			let result = call.dispatch(origin);
267			// Always take into account the base weight of this call.
268			let mut weight = T::WeightInfo::as_derivative()
269				.saturating_add(T::DbWeight::get().reads_writes(1, 1));
270			// Add the real weight of the dispatch.
271			weight = weight.saturating_add(extract_actual_weight(&result, &info));
272			result
273				.map_err(|mut err| {
274					err.post_info = Some(weight).into();
275					err
276				})
277				.map(|_| Some(weight).into())
278		}
279
280		/// Send a batch of dispatch calls and atomically execute them.
281		/// The whole transaction will rollback and fail if any of the calls failed.
282		///
283		/// May be called from any origin except `None`.
284		///
285		/// - `calls`: The calls to be dispatched from the same origin. The number of call must not
286		///   exceed the constant: `batched_calls_limit` (available in constant metadata).
287		///
288		/// If origin is root then the calls are dispatched without checking origin filter. (This
289		/// includes bypassing `frame_system::Config::BaseCallFilter`).
290		///
291		/// ## Complexity
292		/// - O(C) where C is the number of calls to be batched.
293		#[pallet::call_index(2)]
294		#[pallet::weight({
295			let (dispatch_weight, dispatch_class) = Pallet::<T>::weight_and_dispatch_class(&calls);
296			let dispatch_weight = dispatch_weight.saturating_add(T::WeightInfo::batch_all(calls.len() as u32));
297			(dispatch_weight, dispatch_class)
298		})]
299		pub fn batch_all(
300			origin: OriginFor<T>,
301			calls: Vec<<T as Config>::RuntimeCall>,
302		) -> DispatchResultWithPostInfo {
303			// Do not allow the `None` origin.
304			if ensure_none(origin.clone()).is_ok() {
305				return Err(BadOrigin.into())
306			}
307
308			let is_root = ensure_root(origin.clone()).is_ok();
309			let calls_len = calls.len();
310			ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::<T>::TooManyCalls);
311
312			// Track the actual weight of each of the batch calls.
313			let mut weight = Weight::zero();
314			for (index, call) in calls.into_iter().enumerate() {
315				let info = call.get_dispatch_info();
316				// If origin is root, bypass any dispatch filter; root can call anything.
317				let result = if is_root {
318					call.dispatch_bypass_filter(origin.clone())
319				} else {
320					let mut filtered_origin = origin.clone();
321					// Don't allow users to nest `batch_all` calls.
322					filtered_origin.add_filter(
323						move |c: &<T as frame_system::Config>::RuntimeCall| {
324							let c = <T as Config>::RuntimeCall::from_ref(c);
325							!matches!(c.is_sub_type(), Some(Call::batch_all { .. }))
326						},
327					);
328					call.dispatch(filtered_origin)
329				};
330				// Add the weight of this call.
331				weight = weight.saturating_add(extract_actual_weight(&result, &info));
332				result.map_err(|mut err| {
333					// Take the weight of this function itself into account.
334					let base_weight = T::WeightInfo::batch_all(index.saturating_add(1) as u32);
335					// Return the actual used weight + base_weight of this call.
336					err.post_info = Some(base_weight.saturating_add(weight)).into();
337					err
338				})?;
339				Self::deposit_event(Event::ItemCompleted);
340			}
341			Self::deposit_event(Event::BatchCompleted);
342			let base_weight = T::WeightInfo::batch_all(calls_len as u32);
343			Ok(Some(base_weight.saturating_add(weight)).into())
344		}
345
346		/// Dispatches a function call with a provided origin.
347		///
348		/// The dispatch origin for this call must be _Root_.
349		///
350		/// ## Complexity
351		/// - O(1).
352		#[pallet::call_index(3)]
353		#[pallet::weight({
354			let dispatch_info = call.get_dispatch_info();
355			(
356				T::WeightInfo::dispatch_as()
357					.saturating_add(dispatch_info.call_weight),
358				dispatch_info.class,
359			)
360		})]
361		pub fn dispatch_as(
362			origin: OriginFor<T>,
363			as_origin: Box<T::PalletsOrigin>,
364			call: Box<<T as Config>::RuntimeCall>,
365		) -> DispatchResult {
366			ensure_root(origin)?;
367
368			let res = call.dispatch_bypass_filter((*as_origin).into());
369
370			Self::deposit_event(Event::DispatchedAs {
371				result: res.map(|_| ()).map_err(|e| e.error),
372			});
373			Ok(())
374		}
375
376		/// Send a batch of dispatch calls.
377		/// Unlike `batch`, it allows errors and won't interrupt.
378		///
379		/// May be called from any origin except `None`.
380		///
381		/// - `calls`: The calls to be dispatched from the same origin. The number of call must not
382		///   exceed the constant: `batched_calls_limit` (available in constant metadata).
383		///
384		/// If origin is root then the calls are dispatch without checking origin filter. (This
385		/// includes bypassing `frame_system::Config::BaseCallFilter`).
386		///
387		/// ## Complexity
388		/// - O(C) where C is the number of calls to be batched.
389		#[pallet::call_index(4)]
390		#[pallet::weight({
391			let (dispatch_weight, dispatch_class) = Pallet::<T>::weight_and_dispatch_class(&calls);
392			let dispatch_weight = dispatch_weight.saturating_add(T::WeightInfo::force_batch(calls.len() as u32));
393			(dispatch_weight, dispatch_class)
394		})]
395		pub fn force_batch(
396			origin: OriginFor<T>,
397			calls: Vec<<T as Config>::RuntimeCall>,
398		) -> DispatchResultWithPostInfo {
399			// Do not allow the `None` origin.
400			if ensure_none(origin.clone()).is_ok() {
401				return Err(BadOrigin.into())
402			}
403
404			let is_root = ensure_root(origin.clone()).is_ok();
405			let calls_len = calls.len();
406			ensure!(calls_len <= Self::batched_calls_limit() as usize, Error::<T>::TooManyCalls);
407
408			// Track the actual weight of each of the batch calls.
409			let mut weight = Weight::zero();
410			// Track failed dispatch occur.
411			let mut has_error: bool = false;
412			for call in calls.into_iter() {
413				let info = call.get_dispatch_info();
414				// If origin is root, don't apply any dispatch filters; root can call anything.
415				let result = if is_root {
416					call.dispatch_bypass_filter(origin.clone())
417				} else {
418					call.dispatch(origin.clone())
419				};
420				// Add the weight of this call.
421				weight = weight.saturating_add(extract_actual_weight(&result, &info));
422				if let Err(e) = result {
423					has_error = true;
424					Self::deposit_event(Event::ItemFailed { error: e.error });
425				} else {
426					Self::deposit_event(Event::ItemCompleted);
427				}
428			}
429			if has_error {
430				Self::deposit_event(Event::BatchCompletedWithErrors);
431			} else {
432				Self::deposit_event(Event::BatchCompleted);
433			}
434			let base_weight = T::WeightInfo::batch(calls_len as u32);
435			Ok(Some(base_weight.saturating_add(weight)).into())
436		}
437
438		/// Dispatch a function call with a specified weight.
439		///
440		/// This function does not check the weight of the call, and instead allows the
441		/// Root origin to specify the weight of the call.
442		///
443		/// The dispatch origin for this call must be _Root_.
444		#[pallet::call_index(5)]
445		#[pallet::weight((*weight, call.get_dispatch_info().class))]
446		pub fn with_weight(
447			origin: OriginFor<T>,
448			call: Box<<T as Config>::RuntimeCall>,
449			weight: Weight,
450		) -> DispatchResult {
451			ensure_root(origin)?;
452			let _ = weight; // Explicitly don't check the the weight witness.
453
454			let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into());
455			res.map(|_| ()).map_err(|e| e.error)
456		}
457	}
458
459	impl<T: Config> Pallet<T> {
460		/// Get the accumulated `weight` and the dispatch class for the given `calls`.
461		fn weight_and_dispatch_class(
462			calls: &[<T as Config>::RuntimeCall],
463		) -> (Weight, DispatchClass) {
464			let dispatch_infos = calls.iter().map(|call| call.get_dispatch_info());
465			let (dispatch_weight, dispatch_class) = dispatch_infos.fold(
466				(Weight::zero(), DispatchClass::Operational),
467				|(total_weight, dispatch_class): (Weight, DispatchClass), di| {
468					(
469						total_weight.saturating_add(di.call_weight),
470						// If not all are `Operational`, we want to use `DispatchClass::Normal`.
471						if di.class == DispatchClass::Normal { di.class } else { dispatch_class },
472					)
473				},
474			);
475
476			(dispatch_weight, dispatch_class)
477		}
478	}
479}
480
481/// A pallet identifier. These are per pallet and should be stored in a registry somewhere.
482#[derive(Clone, Copy, Eq, PartialEq, Encode, Decode)]
483struct IndexedUtilityPalletId(u16);
484
485impl TypeId for IndexedUtilityPalletId {
486	const TYPE_ID: [u8; 4] = *b"suba";
487}
488
489impl<T: Config> Pallet<T> {
490	/// Derive a derivative account ID from the owner account and the sub-account index.
491	pub fn derivative_account_id(who: T::AccountId, index: u16) -> T::AccountId {
492		let entropy = (b"modlpy/utilisuba", who, index).using_encoded(blake2_256);
493		Decode::decode(&mut TrailingZeroInput::new(entropy.as_ref()))
494			.expect("infinite length input; no invalid inputs for type; qed")
495	}
496}