frame_system/
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//! # System Pallet
19//!
20//! The System pallet provides low-level access to core types and cross-cutting utilities. It acts
21//! as the base layer for other pallets to interact with the Substrate framework components.
22//!
23//! - [`Config`]
24//!
25//! ## Overview
26//!
27//! The System pallet defines the core data types used in a Substrate runtime. It also provides
28//! several utility functions (see [`Pallet`]) for other FRAME pallets.
29//!
30//! In addition, it manages the storage items for extrinsic data, indices, event records, and digest
31//! items, among other things that support the execution of the current block.
32//!
33//! It also handles low-level tasks like depositing logs, basic set up and take down of temporary
34//! storage entries, and access to previous block hashes.
35//!
36//! ## Interface
37//!
38//! ### Dispatchable Functions
39//!
40//! The System pallet provides dispatchable functions that, with the exception of `remark`, manage
41//! low-level or privileged functionality of a Substrate-based runtime.
42//!
43//! - `remark`: Make some on-chain remark.
44//! - `set_heap_pages`: Set the number of pages in the WebAssembly environment's heap.
45//! - `set_code`: Set the new runtime code.
46//! - `set_code_without_checks`: Set the new runtime code without any checks.
47//! - `set_storage`: Set some items of storage.
48//! - `kill_storage`: Kill some items from storage.
49//! - `kill_prefix`: Kill all storage items with a key that starts with the given prefix.
50//! - `remark_with_event`: Make some on-chain remark and emit an event.
51//! - `do_task`: Do some specified task.
52//! - `authorize_upgrade`: Authorize new runtime code.
53//! - `authorize_upgrade_without_checks`: Authorize new runtime code and an upgrade sans
54//!   verification.
55//! - `apply_authorized_upgrade`: Provide new, already-authorized runtime code.
56//!
57//! #### A Note on Upgrades
58//!
59//! The pallet provides two primary means of upgrading the runtime, a single-phase means using
60//! `set_code` and a two-phase means using `authorize_upgrade` followed by
61//! `apply_authorized_upgrade`. The first will directly attempt to apply the provided `code`
62//! (application may have to be scheduled, depending on the context and implementation of the
63//! `OnSetCode` trait).
64//!
65//! The `authorize_upgrade` route allows the authorization of a runtime's code hash. Once
66//! authorized, anyone may upload the correct runtime to apply the code. This pattern is useful when
67//! providing the runtime ahead of time may be unwieldy, for example when a large preimage (the
68//! code) would need to be stored on-chain or sent over a message transport protocol such as a
69//! bridge.
70//!
71//! The `*_without_checks` variants do not perform any version checks, so using them runs the risk
72//! of applying a downgrade or entirely other chain specification. They will still validate that the
73//! `code` meets the authorized hash.
74//!
75//! ### Public Functions
76//!
77//! See the [`Pallet`] struct for details of publicly available functions.
78//!
79//! ### Signed Extensions
80//!
81//! The System pallet defines the following extensions:
82//!
83//!   - [`CheckWeight`]: Checks the weight and length of the block and ensure that it does not
84//!     exceed the limits.
85//!   - [`CheckNonce`]: Checks the nonce of the transaction. Contains a single payload of type
86//!     `T::Nonce`.
87//!   - [`CheckEra`]: Checks the era of the transaction. Contains a single payload of type `Era`.
88//!   - [`CheckGenesis`]: Checks the provided genesis hash of the transaction. Must be a part of the
89//!     signed payload of the transaction.
90//!   - [`CheckSpecVersion`]: Checks that the runtime version is the same as the one used to sign
91//!     the transaction.
92//!   - [`CheckTxVersion`]: Checks that the transaction version is the same as the one used to sign
93//!     the transaction.
94//!
95//! Look up the runtime aggregator file (e.g. `node/runtime`) to see the full list of signed
96//! extensions included in a chain.
97
98#![cfg_attr(not(feature = "std"), no_std)]
99
100extern crate alloc;
101
102use alloc::{borrow::Cow, boxed::Box, vec, vec::Vec};
103use core::{fmt::Debug, marker::PhantomData};
104use pallet_prelude::{BlockNumberFor, HeaderFor};
105#[cfg(feature = "std")]
106use serde::Serialize;
107use sp_io::hashing::blake2_256;
108#[cfg(feature = "runtime-benchmarks")]
109use sp_runtime::traits::TrailingZeroInput;
110use sp_runtime::{
111	generic,
112	traits::{
113		self, AtLeast32Bit, BadOrigin, BlockNumberProvider, Bounded, CheckEqual, Dispatchable,
114		Hash, Header, Lookup, LookupError, MaybeDisplay, MaybeSerializeDeserialize, Member, One,
115		Saturating, SimpleBitOps, StaticLookup, Zero,
116	},
117	transaction_validity::{
118		InvalidTransaction, TransactionLongevity, TransactionSource, TransactionValidity,
119		ValidTransaction,
120	},
121	DispatchError, RuntimeDebug,
122};
123use sp_version::RuntimeVersion;
124
125use codec::{Decode, DecodeWithMemTracking, Encode, EncodeLike, FullCodec, MaxEncodedLen};
126#[cfg(feature = "std")]
127use frame_support::traits::BuildGenesisConfig;
128use frame_support::{
129	dispatch::{
130		extract_actual_pays_fee, extract_actual_weight, DispatchClass, DispatchInfo,
131		DispatchResult, DispatchResultWithPostInfo, GetDispatchInfo, PerDispatchClass,
132		PostDispatchInfo,
133	},
134	ensure, impl_ensure_origin_with_arg_ignoring_arg,
135	migrations::MultiStepMigrator,
136	pallet_prelude::Pays,
137	storage::{self, StorageStreamIter},
138	traits::{
139		ConstU32, Contains, EnsureOrigin, EnsureOriginWithArg, Get, HandleLifetime,
140		OnKilledAccount, OnNewAccount, OnRuntimeUpgrade, OriginTrait, PalletInfo, SortedMembers,
141		StoredMap, TypedGet,
142	},
143	Parameter,
144};
145use scale_info::TypeInfo;
146use sp_core::storage::well_known_keys;
147use sp_runtime::{
148	traits::{DispatchInfoOf, PostDispatchInfoOf},
149	transaction_validity::TransactionValidityError,
150};
151use sp_weights::{RuntimeDbWeight, Weight};
152
153#[cfg(any(feature = "std", test))]
154use sp_io::TestExternalities;
155
156pub mod limits;
157#[cfg(test)]
158pub(crate) mod mock;
159
160pub mod offchain;
161
162mod extensions;
163#[cfg(feature = "std")]
164pub mod mocking;
165#[cfg(test)]
166mod tests;
167pub mod weights;
168
169pub mod migrations;
170
171pub use extensions::{
172	check_genesis::CheckGenesis, check_mortality::CheckMortality,
173	check_non_zero_sender::CheckNonZeroSender, check_nonce::CheckNonce,
174	check_spec_version::CheckSpecVersion, check_tx_version::CheckTxVersion,
175	check_weight::CheckWeight, weight_reclaim::WeightReclaim,
176	weights::SubstrateWeight as SubstrateExtensionsWeight, WeightInfo as ExtensionsWeightInfo,
177};
178// Backward compatible re-export.
179pub use extensions::check_mortality::CheckMortality as CheckEra;
180pub use frame_support::dispatch::RawOrigin;
181use frame_support::traits::{PostInherents, PostTransactions, PreInherents};
182use sp_core::storage::StateVersion;
183pub use weights::WeightInfo;
184
185const LOG_TARGET: &str = "runtime::system";
186
187/// Compute the trie root of a list of extrinsics.
188///
189/// The merkle proof is using the same trie as runtime state with
190/// `state_version` 0 or 1.
191pub fn extrinsics_root<H: Hash, E: codec::Encode>(
192	extrinsics: &[E],
193	state_version: StateVersion,
194) -> H::Output {
195	extrinsics_data_root::<H>(extrinsics.iter().map(codec::Encode::encode).collect(), state_version)
196}
197
198/// Compute the trie root of a list of extrinsics.
199///
200/// The merkle proof is using the same trie as runtime state with
201/// `state_version` 0 or 1.
202pub fn extrinsics_data_root<H: Hash>(xts: Vec<Vec<u8>>, state_version: StateVersion) -> H::Output {
203	H::ordered_trie_root(xts, state_version)
204}
205
206/// An object to track the currently used extrinsic weight in a block.
207pub type ConsumedWeight = PerDispatchClass<Weight>;
208
209pub use pallet::*;
210
211/// Do something when we should be setting the code.
212pub trait SetCode<T: Config> {
213	/// Set the code to the given blob.
214	fn set_code(code: Vec<u8>) -> DispatchResult;
215}
216
217impl<T: Config> SetCode<T> for () {
218	fn set_code(code: Vec<u8>) -> DispatchResult {
219		<Pallet<T>>::update_code_in_storage(&code);
220		Ok(())
221	}
222}
223
224/// Numeric limits over the ability to add a consumer ref using `inc_consumers`.
225pub trait ConsumerLimits {
226	/// The number of consumers over which `inc_consumers` will cease to work.
227	fn max_consumers() -> RefCount;
228	/// The maximum number of additional consumers expected to be over be added at once using
229	/// `inc_consumers_without_limit`.
230	///
231	/// Note: This is not enforced and it's up to the chain's author to ensure this reflects the
232	/// actual situation.
233	fn max_overflow() -> RefCount;
234}
235
236impl<const Z: u32> ConsumerLimits for ConstU32<Z> {
237	fn max_consumers() -> RefCount {
238		Z
239	}
240	fn max_overflow() -> RefCount {
241		Z
242	}
243}
244
245impl<MaxNormal: Get<u32>, MaxOverflow: Get<u32>> ConsumerLimits for (MaxNormal, MaxOverflow) {
246	fn max_consumers() -> RefCount {
247		MaxNormal::get()
248	}
249	fn max_overflow() -> RefCount {
250		MaxOverflow::get()
251	}
252}
253
254/// Information needed when a new runtime binary is submitted and needs to be authorized before
255/// replacing the current runtime.
256#[derive(Decode, Encode, Default, PartialEq, Eq, MaxEncodedLen, TypeInfo)]
257#[scale_info(skip_type_params(T))]
258pub struct CodeUpgradeAuthorization<T>
259where
260	T: Config,
261{
262	/// Hash of the new runtime binary.
263	code_hash: T::Hash,
264	/// Whether or not to carry out version checks.
265	check_version: bool,
266}
267
268/// Information about the dispatch of a call, to be displayed in the
269/// [`ExtrinsicSuccess`](Event::ExtrinsicSuccess) and [`ExtrinsicFailed`](Event::ExtrinsicFailed)
270/// events.
271#[derive(
272	Clone,
273	Copy,
274	Eq,
275	PartialEq,
276	Default,
277	RuntimeDebug,
278	Encode,
279	Decode,
280	DecodeWithMemTracking,
281	TypeInfo,
282)]
283pub struct DispatchEventInfo {
284	/// Weight of this transaction.
285	pub weight: Weight,
286	/// Class of this transaction.
287	pub class: DispatchClass,
288	/// Does this transaction pay fees.
289	pub pays_fee: Pays,
290}
291
292#[frame_support::pallet]
293pub mod pallet {
294	use crate::{self as frame_system, pallet_prelude::*, *};
295	use codec::HasCompact;
296	use frame_support::pallet_prelude::*;
297
298	/// Default implementations of [`DefaultConfig`], which can be used to implement [`Config`].
299	pub mod config_preludes {
300		use super::{inject_runtime_type, DefaultConfig};
301		use frame_support::{derive_impl, traits::Get};
302
303		/// A predefined adapter that covers `BlockNumberFor<T>` for `Config::Block::BlockNumber` of
304		/// the types `u32`, `u64`, and `u128`.
305		///
306		/// NOTE: Avoids overriding `BlockHashCount` when using `mocking::{MockBlock, MockBlockU32,
307		/// MockBlockU128}`.
308		pub struct TestBlockHashCount<C: Get<u32>>(core::marker::PhantomData<C>);
309		impl<I: From<u32>, C: Get<u32>> Get<I> for TestBlockHashCount<C> {
310			fn get() -> I {
311				C::get().into()
312			}
313		}
314
315		/// Provides a viable default config that can be used with
316		/// [`derive_impl`](`frame_support::derive_impl`) to derive a testing pallet config
317		/// based on this one.
318		///
319		/// See `Test` in the `default-config` example pallet's `test.rs` for an example of
320		/// a downstream user of this particular `TestDefaultConfig`
321		pub struct TestDefaultConfig;
322
323		#[frame_support::register_default_impl(TestDefaultConfig)]
324		impl DefaultConfig for TestDefaultConfig {
325			type Nonce = u32;
326			type Hash = sp_core::hash::H256;
327			type Hashing = sp_runtime::traits::BlakeTwo256;
328			type AccountId = u64;
329			type Lookup = sp_runtime::traits::IdentityLookup<Self::AccountId>;
330			type MaxConsumers = frame_support::traits::ConstU32<16>;
331			type AccountData = ();
332			type OnNewAccount = ();
333			type OnKilledAccount = ();
334			type SystemWeightInfo = ();
335			type ExtensionsWeightInfo = ();
336			type SS58Prefix = ();
337			type Version = ();
338			type BlockWeights = ();
339			type BlockLength = ();
340			type DbWeight = ();
341			#[inject_runtime_type]
342			type RuntimeEvent = ();
343			#[inject_runtime_type]
344			type RuntimeOrigin = ();
345			#[inject_runtime_type]
346			type RuntimeCall = ();
347			#[inject_runtime_type]
348			type PalletInfo = ();
349			#[inject_runtime_type]
350			type RuntimeTask = ();
351			type BaseCallFilter = frame_support::traits::Everything;
352			type BlockHashCount = TestBlockHashCount<frame_support::traits::ConstU32<10>>;
353			type OnSetCode = ();
354			type SingleBlockMigrations = ();
355			type MultiBlockMigrator = ();
356			type PreInherents = ();
357			type PostInherents = ();
358			type PostTransactions = ();
359		}
360
361		/// Default configurations of this pallet in a solochain environment.
362		///
363		/// ## Considerations:
364		///
365		/// By default, this type makes the following choices:
366		///
367		/// * Use a normal 32 byte account id, with a [`DefaultConfig::Lookup`] that implies no
368		///   'account-indexing' pallet is being used.
369		/// * Given that we don't know anything about the existence of a currency system in scope,
370		///   an [`DefaultConfig::AccountData`] is chosen that has no addition data. Overwrite this
371		///   if you use `pallet-balances` or similar.
372		/// * Make sure to overwrite [`DefaultConfig::Version`].
373		/// * 2s block time, and a default 5mb block size is used.
374		pub struct SolochainDefaultConfig;
375
376		#[frame_support::register_default_impl(SolochainDefaultConfig)]
377		impl DefaultConfig for SolochainDefaultConfig {
378			/// The default type for storing how many extrinsics an account has signed.
379			type Nonce = u32;
380
381			/// The default type for hashing blocks and tries.
382			type Hash = sp_core::hash::H256;
383
384			/// The default hashing algorithm used.
385			type Hashing = sp_runtime::traits::BlakeTwo256;
386
387			/// The default identifier used to distinguish between accounts.
388			type AccountId = sp_runtime::AccountId32;
389
390			/// The lookup mechanism to get account ID from whatever is passed in dispatchers.
391			type Lookup = sp_runtime::traits::AccountIdLookup<Self::AccountId, ()>;
392
393			/// The maximum number of consumers allowed on a single account. Using 128 as default.
394			type MaxConsumers = frame_support::traits::ConstU32<128>;
395
396			/// The default data to be stored in an account.
397			type AccountData = ();
398
399			/// What to do if a new account is created.
400			type OnNewAccount = ();
401
402			/// What to do if an account is fully reaped from the system.
403			type OnKilledAccount = ();
404
405			/// Weight information for the extrinsics of this pallet.
406			type SystemWeightInfo = ();
407
408			/// Weight information for the extensions of this pallet.
409			type ExtensionsWeightInfo = ();
410
411			/// This is used as an identifier of the chain.
412			type SS58Prefix = ();
413
414			/// Version of the runtime.
415			type Version = ();
416
417			/// Block & extrinsics weights: base values and limits.
418			type BlockWeights = ();
419
420			/// The maximum length of a block (in bytes).
421			type BlockLength = ();
422
423			/// The weight of database operations that the runtime can invoke.
424			type DbWeight = ();
425
426			/// The ubiquitous event type injected by `construct_runtime!`.
427			#[inject_runtime_type]
428			type RuntimeEvent = ();
429
430			/// The ubiquitous origin type injected by `construct_runtime!`.
431			#[inject_runtime_type]
432			type RuntimeOrigin = ();
433
434			/// The aggregated dispatch type available for extrinsics, injected by
435			/// `construct_runtime!`.
436			#[inject_runtime_type]
437			type RuntimeCall = ();
438
439			/// The aggregated Task type, injected by `construct_runtime!`.
440			#[inject_runtime_type]
441			type RuntimeTask = ();
442
443			/// Converts a module to the index of the module, injected by `construct_runtime!`.
444			#[inject_runtime_type]
445			type PalletInfo = ();
446
447			/// The basic call filter to use in dispatchable. Supports everything as the default.
448			type BaseCallFilter = frame_support::traits::Everything;
449
450			/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
451			/// Using 256 as default.
452			type BlockHashCount = TestBlockHashCount<frame_support::traits::ConstU32<256>>;
453
454			/// The set code logic, just the default since we're not a parachain.
455			type OnSetCode = ();
456			type SingleBlockMigrations = ();
457			type MultiBlockMigrator = ();
458			type PreInherents = ();
459			type PostInherents = ();
460			type PostTransactions = ();
461		}
462
463		/// Default configurations of this pallet in a relay-chain environment.
464		pub struct RelayChainDefaultConfig;
465
466		/// It currently uses the same configuration as `SolochainDefaultConfig`.
467		#[derive_impl(SolochainDefaultConfig as DefaultConfig, no_aggregated_types)]
468		#[frame_support::register_default_impl(RelayChainDefaultConfig)]
469		impl DefaultConfig for RelayChainDefaultConfig {}
470
471		/// Default configurations of this pallet in a parachain environment.
472		pub struct ParaChainDefaultConfig;
473
474		/// It currently uses the same configuration as `SolochainDefaultConfig`.
475		#[derive_impl(SolochainDefaultConfig as DefaultConfig, no_aggregated_types)]
476		#[frame_support::register_default_impl(ParaChainDefaultConfig)]
477		impl DefaultConfig for ParaChainDefaultConfig {}
478	}
479
480	/// System configuration trait. Implemented by runtime.
481	#[pallet::config(with_default)]
482	#[pallet::disable_frame_system_supertrait_check]
483	pub trait Config: 'static + Eq + Clone {
484		/// The aggregated event type of the runtime.
485		#[pallet::no_default_bounds]
486		type RuntimeEvent: Parameter
487			+ Member
488			+ From<Event<Self>>
489			+ Debug
490			+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
491
492		/// The basic call filter to use in Origin. All origins are built with this filter as base,
493		/// except Root.
494		///
495		/// This works as a filter for each incoming call. The call needs to pass this filter in
496		/// order to dispatch. Otherwise it will be rejected with `CallFiltered`. This can be
497		/// bypassed via `dispatch_bypass_filter` which should only be accessible by root. The
498		/// filter can be composed of sub-filters by nesting for example
499		/// [`frame_support::traits::InsideBoth`], [`frame_support::traits::TheseExcept`] or
500		/// [`frame_support::traits::EverythingBut`] et al. The default would be
501		/// [`frame_support::traits::Everything`].
502		#[pallet::no_default_bounds]
503		type BaseCallFilter: Contains<Self::RuntimeCall>;
504
505		/// Block & extrinsics weights: base values and limits.
506		#[pallet::constant]
507		type BlockWeights: Get<limits::BlockWeights>;
508
509		/// The maximum length of a block (in bytes).
510		#[pallet::constant]
511		type BlockLength: Get<limits::BlockLength>;
512
513		/// The `RuntimeOrigin` type used by dispatchable calls.
514		#[pallet::no_default_bounds]
515		type RuntimeOrigin: Into<Result<RawOrigin<Self::AccountId>, Self::RuntimeOrigin>>
516			+ From<RawOrigin<Self::AccountId>>
517			+ Clone
518			+ OriginTrait<Call = Self::RuntimeCall, AccountId = Self::AccountId>;
519
520		#[docify::export(system_runtime_call)]
521		/// The aggregated `RuntimeCall` type.
522		#[pallet::no_default_bounds]
523		type RuntimeCall: Parameter
524			+ Dispatchable<RuntimeOrigin = Self::RuntimeOrigin>
525			+ Debug
526			+ GetDispatchInfo
527			+ From<Call<Self>>;
528
529		/// The aggregated `RuntimeTask` type.
530		#[pallet::no_default_bounds]
531		type RuntimeTask: Task;
532
533		/// This stores the number of previous transactions associated with a sender account.
534		type Nonce: Parameter
535			+ HasCompact<Type: DecodeWithMemTracking>
536			+ Member
537			+ MaybeSerializeDeserialize
538			+ Debug
539			+ Default
540			+ MaybeDisplay
541			+ AtLeast32Bit
542			+ Copy
543			+ MaxEncodedLen;
544
545		/// The output of the `Hashing` function.
546		type Hash: Parameter
547			+ Member
548			+ MaybeSerializeDeserialize
549			+ Debug
550			+ MaybeDisplay
551			+ SimpleBitOps
552			+ Ord
553			+ Default
554			+ Copy
555			+ CheckEqual
556			+ core::hash::Hash
557			+ AsRef<[u8]>
558			+ AsMut<[u8]>
559			+ MaxEncodedLen;
560
561		/// The hashing system (algorithm) being used in the runtime (e.g. Blake2).
562		type Hashing: Hash<Output = Self::Hash> + TypeInfo;
563
564		/// The user account identifier type for the runtime.
565		type AccountId: Parameter
566			+ Member
567			+ MaybeSerializeDeserialize
568			+ Debug
569			+ MaybeDisplay
570			+ Ord
571			+ MaxEncodedLen;
572
573		/// Converting trait to take a source type and convert to `AccountId`.
574		///
575		/// Used to define the type and conversion mechanism for referencing accounts in
576		/// transactions. It's perfectly reasonable for this to be an identity conversion (with the
577		/// source type being `AccountId`), but other pallets (e.g. Indices pallet) may provide more
578		/// functional/efficient alternatives.
579		type Lookup: StaticLookup<Target = Self::AccountId>;
580
581		/// The Block type used by the runtime. This is used by `construct_runtime` to retrieve the
582		/// extrinsics or other block specific data as needed.
583		#[pallet::no_default]
584		type Block: Parameter + Member + traits::Block<Hash = Self::Hash>;
585
586		/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
587		#[pallet::constant]
588		#[pallet::no_default_bounds]
589		type BlockHashCount: Get<BlockNumberFor<Self>>;
590
591		/// The weight of runtime database operations the runtime can invoke.
592		#[pallet::constant]
593		type DbWeight: Get<RuntimeDbWeight>;
594
595		/// Get the chain's in-code version.
596		#[pallet::constant]
597		type Version: Get<RuntimeVersion>;
598
599		/// Provides information about the pallet setup in the runtime.
600		///
601		/// Expects the `PalletInfo` type that is being generated by `construct_runtime!` in the
602		/// runtime.
603		///
604		/// For tests it is okay to use `()` as type, however it will provide "useless" data.
605		#[pallet::no_default_bounds]
606		type PalletInfo: PalletInfo;
607
608		/// Data to be associated with an account (other than nonce/transaction counter, which this
609		/// pallet does regardless).
610		type AccountData: Member + FullCodec + Clone + Default + TypeInfo + MaxEncodedLen;
611
612		/// Handler for when a new account has just been created.
613		type OnNewAccount: OnNewAccount<Self::AccountId>;
614
615		/// A function that is invoked when an account has been determined to be dead.
616		///
617		/// All resources should be cleaned up associated with the given account.
618		type OnKilledAccount: OnKilledAccount<Self::AccountId>;
619
620		/// Weight information for the extrinsics of this pallet.
621		type SystemWeightInfo: WeightInfo;
622
623		/// Weight information for the transaction extensions of this pallet.
624		type ExtensionsWeightInfo: extensions::WeightInfo;
625
626		/// The designated SS58 prefix of this chain.
627		///
628		/// This replaces the "ss58Format" property declared in the chain spec. Reason is
629		/// that the runtime should know about the prefix in order to make use of it as
630		/// an identifier of the chain.
631		#[pallet::constant]
632		type SS58Prefix: Get<u16>;
633
634		/// What to do if the runtime wants to change the code to something new.
635		///
636		/// The default (`()`) implementation is responsible for setting the correct storage
637		/// entry and emitting corresponding event and log item. (see
638		/// [`Pallet::update_code_in_storage`]).
639		/// It's unlikely that this needs to be customized, unless you are writing a parachain using
640		/// `Cumulus`, where the actual code change is deferred.
641		#[pallet::no_default_bounds]
642		type OnSetCode: SetCode<Self>;
643
644		/// The maximum number of consumers allowed on a single account.
645		type MaxConsumers: ConsumerLimits;
646
647		/// All migrations that should run in the next runtime upgrade.
648		///
649		/// These used to be formerly configured in `Executive`. Parachains need to ensure that
650		/// running all these migrations in one block will not overflow the weight limit of a block.
651		/// The migrations are run *before* the pallet `on_runtime_upgrade` hooks, just like the
652		/// `OnRuntimeUpgrade` migrations.
653		type SingleBlockMigrations: OnRuntimeUpgrade;
654
655		/// The migrator that is used to run Multi-Block-Migrations.
656		///
657		/// Can be set to [`pallet-migrations`] or an alternative implementation of the interface.
658		/// The diagram in `frame_executive::block_flowchart` explains when it runs.
659		type MultiBlockMigrator: MultiStepMigrator;
660
661		/// A callback that executes in *every block* directly before all inherents were applied.
662		///
663		/// See `frame_executive::block_flowchart` for a in-depth explanation when it runs.
664		type PreInherents: PreInherents;
665
666		/// A callback that executes in *every block* directly after all inherents were applied.
667		///
668		/// See `frame_executive::block_flowchart` for a in-depth explanation when it runs.
669		type PostInherents: PostInherents;
670
671		/// A callback that executes in *every block* directly after all transactions were applied.
672		///
673		/// See `frame_executive::block_flowchart` for a in-depth explanation when it runs.
674		type PostTransactions: PostTransactions;
675	}
676
677	#[pallet::pallet]
678	pub struct Pallet<T>(_);
679
680	#[pallet::hooks]
681	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
682		#[cfg(feature = "std")]
683		fn integrity_test() {
684			T::BlockWeights::get().validate().expect("The weights are invalid.");
685		}
686	}
687
688	#[pallet::call]
689	impl<T: Config> Pallet<T> {
690		/// Make some on-chain remark.
691		///
692		/// Can be executed by every `origin`.
693		#[pallet::call_index(0)]
694		#[pallet::weight(T::SystemWeightInfo::remark(remark.len() as u32))]
695		pub fn remark(_origin: OriginFor<T>, remark: Vec<u8>) -> DispatchResultWithPostInfo {
696			let _ = remark; // No need to check the weight witness.
697			Ok(().into())
698		}
699
700		/// Set the number of pages in the WebAssembly environment's heap.
701		#[pallet::call_index(1)]
702		#[pallet::weight((T::SystemWeightInfo::set_heap_pages(), DispatchClass::Operational))]
703		pub fn set_heap_pages(origin: OriginFor<T>, pages: u64) -> DispatchResultWithPostInfo {
704			ensure_root(origin)?;
705			storage::unhashed::put_raw(well_known_keys::HEAP_PAGES, &pages.encode());
706			Self::deposit_log(generic::DigestItem::RuntimeEnvironmentUpdated);
707			Ok(().into())
708		}
709
710		/// Set the new runtime code.
711		#[pallet::call_index(2)]
712		#[pallet::weight((T::SystemWeightInfo::set_code(), DispatchClass::Operational))]
713		pub fn set_code(origin: OriginFor<T>, code: Vec<u8>) -> DispatchResultWithPostInfo {
714			ensure_root(origin)?;
715			Self::can_set_code(&code, true).into_result()?;
716			T::OnSetCode::set_code(code)?;
717			// consume the rest of the block to prevent further transactions
718			Ok(Some(T::BlockWeights::get().max_block).into())
719		}
720
721		/// Set the new runtime code without doing any checks of the given `code`.
722		///
723		/// Note that runtime upgrades will not run if this is called with a not-increasing spec
724		/// version!
725		#[pallet::call_index(3)]
726		#[pallet::weight((T::SystemWeightInfo::set_code(), DispatchClass::Operational))]
727		pub fn set_code_without_checks(
728			origin: OriginFor<T>,
729			code: Vec<u8>,
730		) -> DispatchResultWithPostInfo {
731			ensure_root(origin)?;
732			Self::can_set_code(&code, false).into_result()?;
733			T::OnSetCode::set_code(code)?;
734			Ok(Some(T::BlockWeights::get().max_block).into())
735		}
736
737		/// Set some items of storage.
738		#[pallet::call_index(4)]
739		#[pallet::weight((
740			T::SystemWeightInfo::set_storage(items.len() as u32),
741			DispatchClass::Operational,
742		))]
743		pub fn set_storage(
744			origin: OriginFor<T>,
745			items: Vec<KeyValue>,
746		) -> DispatchResultWithPostInfo {
747			ensure_root(origin)?;
748			for i in &items {
749				storage::unhashed::put_raw(&i.0, &i.1);
750			}
751			Ok(().into())
752		}
753
754		/// Kill some items from storage.
755		#[pallet::call_index(5)]
756		#[pallet::weight((
757			T::SystemWeightInfo::kill_storage(keys.len() as u32),
758			DispatchClass::Operational,
759		))]
760		pub fn kill_storage(origin: OriginFor<T>, keys: Vec<Key>) -> DispatchResultWithPostInfo {
761			ensure_root(origin)?;
762			for key in &keys {
763				storage::unhashed::kill(key);
764			}
765			Ok(().into())
766		}
767
768		/// Kill all storage items with a key that starts with the given prefix.
769		///
770		/// **NOTE:** We rely on the Root origin to provide us the number of subkeys under
771		/// the prefix we are removing to accurately calculate the weight of this function.
772		#[pallet::call_index(6)]
773		#[pallet::weight((
774			T::SystemWeightInfo::kill_prefix(subkeys.saturating_add(1)),
775			DispatchClass::Operational,
776		))]
777		pub fn kill_prefix(
778			origin: OriginFor<T>,
779			prefix: Key,
780			subkeys: u32,
781		) -> DispatchResultWithPostInfo {
782			ensure_root(origin)?;
783			let _ = storage::unhashed::clear_prefix(&prefix, Some(subkeys), None);
784			Ok(().into())
785		}
786
787		/// Make some on-chain remark and emit event.
788		#[pallet::call_index(7)]
789		#[pallet::weight(T::SystemWeightInfo::remark_with_event(remark.len() as u32))]
790		pub fn remark_with_event(
791			origin: OriginFor<T>,
792			remark: Vec<u8>,
793		) -> DispatchResultWithPostInfo {
794			let who = ensure_signed(origin)?;
795			let hash = T::Hashing::hash(&remark[..]);
796			Self::deposit_event(Event::Remarked { sender: who, hash });
797			Ok(().into())
798		}
799
800		#[cfg(feature = "experimental")]
801		#[pallet::call_index(8)]
802		#[pallet::weight(task.weight())]
803		pub fn do_task(_origin: OriginFor<T>, task: T::RuntimeTask) -> DispatchResultWithPostInfo {
804			if !task.is_valid() {
805				return Err(Error::<T>::InvalidTask.into())
806			}
807
808			Self::deposit_event(Event::TaskStarted { task: task.clone() });
809			if let Err(err) = task.run() {
810				Self::deposit_event(Event::TaskFailed { task, err });
811				return Err(Error::<T>::FailedTask.into())
812			}
813
814			// Emit a success event, if your design includes events for this pallet.
815			Self::deposit_event(Event::TaskCompleted { task });
816
817			// Return success.
818			Ok(().into())
819		}
820
821		/// Authorize an upgrade to a given `code_hash` for the runtime. The runtime can be supplied
822		/// later.
823		///
824		/// This call requires Root origin.
825		#[pallet::call_index(9)]
826		#[pallet::weight((T::SystemWeightInfo::authorize_upgrade(), DispatchClass::Operational))]
827		pub fn authorize_upgrade(origin: OriginFor<T>, code_hash: T::Hash) -> DispatchResult {
828			ensure_root(origin)?;
829			Self::do_authorize_upgrade(code_hash, true);
830			Ok(())
831		}
832
833		/// Authorize an upgrade to a given `code_hash` for the runtime. The runtime can be supplied
834		/// later.
835		///
836		/// WARNING: This authorizes an upgrade that will take place without any safety checks, for
837		/// example that the spec name remains the same and that the version number increases. Not
838		/// recommended for normal use. Use `authorize_upgrade` instead.
839		///
840		/// This call requires Root origin.
841		#[pallet::call_index(10)]
842		#[pallet::weight((T::SystemWeightInfo::authorize_upgrade(), DispatchClass::Operational))]
843		pub fn authorize_upgrade_without_checks(
844			origin: OriginFor<T>,
845			code_hash: T::Hash,
846		) -> DispatchResult {
847			ensure_root(origin)?;
848			Self::do_authorize_upgrade(code_hash, false);
849			Ok(())
850		}
851
852		/// Provide the preimage (runtime binary) `code` for an upgrade that has been authorized.
853		///
854		/// If the authorization required a version check, this call will ensure the spec name
855		/// remains unchanged and that the spec version has increased.
856		///
857		/// Depending on the runtime's `OnSetCode` configuration, this function may directly apply
858		/// the new `code` in the same block or attempt to schedule the upgrade.
859		///
860		/// All origins are allowed.
861		#[pallet::call_index(11)]
862		#[pallet::weight((T::SystemWeightInfo::apply_authorized_upgrade(), DispatchClass::Operational))]
863		pub fn apply_authorized_upgrade(
864			_: OriginFor<T>,
865			code: Vec<u8>,
866		) -> DispatchResultWithPostInfo {
867			let res = Self::validate_code_is_authorized(&code)?;
868			AuthorizedUpgrade::<T>::kill();
869
870			match Self::can_set_code(&code, res.check_version) {
871				CanSetCodeResult::Ok => {},
872				CanSetCodeResult::MultiBlockMigrationsOngoing =>
873					return Err(Error::<T>::MultiBlockMigrationsOngoing.into()),
874				CanSetCodeResult::InvalidVersion(error) => {
875					// The upgrade is invalid and there is no benefit in trying to apply this again.
876					Self::deposit_event(Event::RejectedInvalidAuthorizedUpgrade {
877						code_hash: res.code_hash,
878						error: error.into(),
879					});
880
881					// Not the fault of the caller of call.
882					return Ok(Pays::No.into())
883				},
884			};
885			T::OnSetCode::set_code(code)?;
886
887			Ok(PostDispatchInfo {
888				// consume the rest of the block to prevent further transactions
889				actual_weight: Some(T::BlockWeights::get().max_block),
890				// no fee for valid upgrade
891				pays_fee: Pays::No,
892			})
893		}
894	}
895
896	/// Event for the System pallet.
897	#[pallet::event]
898	pub enum Event<T: Config> {
899		/// An extrinsic completed successfully.
900		ExtrinsicSuccess { dispatch_info: DispatchEventInfo },
901		/// An extrinsic failed.
902		ExtrinsicFailed { dispatch_error: DispatchError, dispatch_info: DispatchEventInfo },
903		/// `:code` was updated.
904		CodeUpdated,
905		/// A new account was created.
906		NewAccount { account: T::AccountId },
907		/// An account was reaped.
908		KilledAccount { account: T::AccountId },
909		/// On on-chain remark happened.
910		Remarked { sender: T::AccountId, hash: T::Hash },
911		#[cfg(feature = "experimental")]
912		/// A [`Task`] has started executing
913		TaskStarted { task: T::RuntimeTask },
914		#[cfg(feature = "experimental")]
915		/// A [`Task`] has finished executing.
916		TaskCompleted { task: T::RuntimeTask },
917		#[cfg(feature = "experimental")]
918		/// A [`Task`] failed during execution.
919		TaskFailed { task: T::RuntimeTask, err: DispatchError },
920		/// An upgrade was authorized.
921		UpgradeAuthorized { code_hash: T::Hash, check_version: bool },
922		/// An invalid authorized upgrade was rejected while trying to apply it.
923		RejectedInvalidAuthorizedUpgrade { code_hash: T::Hash, error: DispatchError },
924	}
925
926	/// Error for the System pallet
927	#[pallet::error]
928	pub enum Error<T> {
929		/// The name of specification does not match between the current runtime
930		/// and the new runtime.
931		InvalidSpecName,
932		/// The specification version is not allowed to decrease between the current runtime
933		/// and the new runtime.
934		SpecVersionNeedsToIncrease,
935		/// Failed to extract the runtime version from the new runtime.
936		///
937		/// Either calling `Core_version` or decoding `RuntimeVersion` failed.
938		FailedToExtractRuntimeVersion,
939		/// Suicide called when the account has non-default composite data.
940		NonDefaultComposite,
941		/// There is a non-zero reference count preventing the account from being purged.
942		NonZeroRefCount,
943		/// The origin filter prevent the call to be dispatched.
944		CallFiltered,
945		/// A multi-block migration is ongoing and prevents the current code from being replaced.
946		MultiBlockMigrationsOngoing,
947		#[cfg(feature = "experimental")]
948		/// The specified [`Task`] is not valid.
949		InvalidTask,
950		#[cfg(feature = "experimental")]
951		/// The specified [`Task`] failed during execution.
952		FailedTask,
953		/// No upgrade authorized.
954		NothingAuthorized,
955		/// The submitted code is not authorized.
956		Unauthorized,
957	}
958
959	/// Exposed trait-generic origin type.
960	#[pallet::origin]
961	pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;
962
963	/// The full account information for a particular account ID.
964	#[pallet::storage]
965	#[pallet::getter(fn account)]
966	pub type Account<T: Config> = StorageMap<
967		_,
968		Blake2_128Concat,
969		T::AccountId,
970		AccountInfo<T::Nonce, T::AccountData>,
971		ValueQuery,
972	>;
973
974	/// Total extrinsics count for the current block.
975	#[pallet::storage]
976	pub(super) type ExtrinsicCount<T: Config> = StorageValue<_, u32>;
977
978	/// Whether all inherents have been applied.
979	#[pallet::storage]
980	pub type InherentsApplied<T: Config> = StorageValue<_, bool, ValueQuery>;
981
982	/// The current weight for the block.
983	#[pallet::storage]
984	#[pallet::whitelist_storage]
985	#[pallet::getter(fn block_weight)]
986	pub type BlockWeight<T: Config> = StorageValue<_, ConsumedWeight, ValueQuery>;
987
988	/// Total length (in bytes) for all extrinsics put together, for the current block.
989	#[pallet::storage]
990	#[pallet::whitelist_storage]
991	pub type AllExtrinsicsLen<T: Config> = StorageValue<_, u32>;
992
993	/// Map of block numbers to block hashes.
994	#[pallet::storage]
995	#[pallet::getter(fn block_hash)]
996	pub type BlockHash<T: Config> =
997		StorageMap<_, Twox64Concat, BlockNumberFor<T>, T::Hash, ValueQuery>;
998
999	/// Extrinsics data for the current block (maps an extrinsic's index to its data).
1000	#[pallet::storage]
1001	#[pallet::getter(fn extrinsic_data)]
1002	#[pallet::unbounded]
1003	pub(super) type ExtrinsicData<T: Config> =
1004		StorageMap<_, Twox64Concat, u32, Vec<u8>, ValueQuery>;
1005
1006	/// The current block number being processed. Set by `execute_block`.
1007	#[pallet::storage]
1008	#[pallet::whitelist_storage]
1009	#[pallet::getter(fn block_number)]
1010	pub(super) type Number<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
1011
1012	/// Hash of the previous block.
1013	#[pallet::storage]
1014	#[pallet::getter(fn parent_hash)]
1015	pub(super) type ParentHash<T: Config> = StorageValue<_, T::Hash, ValueQuery>;
1016
1017	/// Digest of the current block, also part of the block header.
1018	#[pallet::storage]
1019	#[pallet::whitelist_storage]
1020	#[pallet::unbounded]
1021	#[pallet::getter(fn digest)]
1022	pub(super) type Digest<T: Config> = StorageValue<_, generic::Digest, ValueQuery>;
1023
1024	/// Events deposited for the current block.
1025	///
1026	/// NOTE: The item is unbound and should therefore never be read on chain.
1027	/// It could otherwise inflate the PoV size of a block.
1028	///
1029	/// Events have a large in-memory size. Box the events to not go out-of-memory
1030	/// just in case someone still reads them from within the runtime.
1031	#[pallet::storage]
1032	#[pallet::whitelist_storage]
1033	#[pallet::disable_try_decode_storage]
1034	#[pallet::unbounded]
1035	pub(super) type Events<T: Config> =
1036		StorageValue<_, Vec<Box<EventRecord<T::RuntimeEvent, T::Hash>>>, ValueQuery>;
1037
1038	/// The number of events in the `Events<T>` list.
1039	#[pallet::storage]
1040	#[pallet::whitelist_storage]
1041	#[pallet::getter(fn event_count)]
1042	pub(super) type EventCount<T: Config> = StorageValue<_, EventIndex, ValueQuery>;
1043
1044	/// Mapping between a topic (represented by T::Hash) and a vector of indexes
1045	/// of events in the `<Events<T>>` list.
1046	///
1047	/// All topic vectors have deterministic storage locations depending on the topic. This
1048	/// allows light-clients to leverage the changes trie storage tracking mechanism and
1049	/// in case of changes fetch the list of events of interest.
1050	///
1051	/// The value has the type `(BlockNumberFor<T>, EventIndex)` because if we used only just
1052	/// the `EventIndex` then in case if the topic has the same contents on the next block
1053	/// no notification will be triggered thus the event might be lost.
1054	#[pallet::storage]
1055	#[pallet::unbounded]
1056	#[pallet::getter(fn event_topics)]
1057	pub(super) type EventTopics<T: Config> =
1058		StorageMap<_, Blake2_128Concat, T::Hash, Vec<(BlockNumberFor<T>, EventIndex)>, ValueQuery>;
1059
1060	/// Stores the `spec_version` and `spec_name` of when the last runtime upgrade happened.
1061	#[pallet::storage]
1062	#[pallet::unbounded]
1063	pub type LastRuntimeUpgrade<T: Config> = StorageValue<_, LastRuntimeUpgradeInfo>;
1064
1065	/// True if we have upgraded so that `type RefCount` is `u32`. False (default) if not.
1066	#[pallet::storage]
1067	pub(super) type UpgradedToU32RefCount<T: Config> = StorageValue<_, bool, ValueQuery>;
1068
1069	/// True if we have upgraded so that AccountInfo contains three types of `RefCount`. False
1070	/// (default) if not.
1071	#[pallet::storage]
1072	pub(super) type UpgradedToTripleRefCount<T: Config> = StorageValue<_, bool, ValueQuery>;
1073
1074	/// The execution phase of the block.
1075	#[pallet::storage]
1076	#[pallet::whitelist_storage]
1077	pub(super) type ExecutionPhase<T: Config> = StorageValue<_, Phase>;
1078
1079	/// `Some` if a code upgrade has been authorized.
1080	#[pallet::storage]
1081	#[pallet::getter(fn authorized_upgrade)]
1082	pub(super) type AuthorizedUpgrade<T: Config> =
1083		StorageValue<_, CodeUpgradeAuthorization<T>, OptionQuery>;
1084
1085	/// The weight reclaimed for the extrinsic.
1086	///
1087	/// This information is available until the end of the extrinsic execution.
1088	/// More precisely this information is removed in `note_applied_extrinsic`.
1089	///
1090	/// Logic doing some post dispatch weight reduction must update this storage to avoid duplicate
1091	/// reduction.
1092	#[pallet::storage]
1093	#[pallet::whitelist_storage]
1094	pub type ExtrinsicWeightReclaimed<T: Config> = StorageValue<_, Weight, ValueQuery>;
1095
1096	#[derive(frame_support::DefaultNoBound)]
1097	#[pallet::genesis_config]
1098	pub struct GenesisConfig<T: Config> {
1099		#[serde(skip)]
1100		pub _config: core::marker::PhantomData<T>,
1101	}
1102
1103	#[pallet::genesis_build]
1104	impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
1105		fn build(&self) {
1106			<BlockHash<T>>::insert::<_, T::Hash>(BlockNumberFor::<T>::zero(), hash69());
1107			<ParentHash<T>>::put::<T::Hash>(hash69());
1108			<LastRuntimeUpgrade<T>>::put(LastRuntimeUpgradeInfo::from(T::Version::get()));
1109			<UpgradedToU32RefCount<T>>::put(true);
1110			<UpgradedToTripleRefCount<T>>::put(true);
1111
1112			sp_io::storage::set(well_known_keys::EXTRINSIC_INDEX, &0u32.encode());
1113		}
1114	}
1115
1116	#[pallet::validate_unsigned]
1117	impl<T: Config> sp_runtime::traits::ValidateUnsigned for Pallet<T> {
1118		type Call = Call<T>;
1119		fn validate_unsigned(_source: TransactionSource, call: &Self::Call) -> TransactionValidity {
1120			if let Call::apply_authorized_upgrade { ref code } = call {
1121				if let Ok(res) = Self::validate_code_is_authorized(&code[..]) {
1122					if Self::can_set_code(&code, false).is_ok() {
1123						return Ok(ValidTransaction {
1124							priority: u64::max_value(),
1125							requires: Vec::new(),
1126							provides: vec![res.code_hash.encode()],
1127							longevity: TransactionLongevity::max_value(),
1128							propagate: true,
1129						})
1130					}
1131				}
1132			}
1133
1134			#[cfg(feature = "experimental")]
1135			if let Call::do_task { ref task } = call {
1136				if task.is_valid() {
1137					return Ok(ValidTransaction {
1138						priority: u64::max_value(),
1139						requires: Vec::new(),
1140						provides: vec![T::Hashing::hash_of(&task.encode()).as_ref().to_vec()],
1141						longevity: TransactionLongevity::max_value(),
1142						propagate: true,
1143					})
1144				}
1145			}
1146
1147			Err(InvalidTransaction::Call.into())
1148		}
1149	}
1150}
1151
1152pub type Key = Vec<u8>;
1153pub type KeyValue = (Vec<u8>, Vec<u8>);
1154
1155/// A phase of a block's execution.
1156#[derive(Encode, Decode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
1157#[cfg_attr(feature = "std", derive(Serialize, PartialEq, Eq, Clone))]
1158pub enum Phase {
1159	/// Applying an extrinsic.
1160	ApplyExtrinsic(u32),
1161	/// Finalizing the block.
1162	Finalization,
1163	/// Initializing the block.
1164	Initialization,
1165}
1166
1167impl Default for Phase {
1168	fn default() -> Self {
1169		Self::Initialization
1170	}
1171}
1172
1173/// Record of an event happening.
1174#[derive(Encode, Decode, RuntimeDebug, TypeInfo)]
1175#[cfg_attr(feature = "std", derive(Serialize, PartialEq, Eq, Clone))]
1176pub struct EventRecord<E: Parameter + Member, T> {
1177	/// The phase of the block it happened in.
1178	pub phase: Phase,
1179	/// The event itself.
1180	pub event: E,
1181	/// The list of the topics this event has.
1182	pub topics: Vec<T>,
1183}
1184
1185// Create a Hash with 69 for each byte,
1186// only used to build genesis config.
1187fn hash69<T: AsMut<[u8]> + Default>() -> T {
1188	let mut h = T::default();
1189	h.as_mut().iter_mut().for_each(|byte| *byte = 69);
1190	h
1191}
1192
1193/// This type alias represents an index of an event.
1194///
1195/// We use `u32` here because this index is used as index for `Events<T>`
1196/// which can't contain more than `u32::MAX` items.
1197type EventIndex = u32;
1198
1199/// Type used to encode the number of references an account has.
1200pub type RefCount = u32;
1201
1202/// Information of an account.
1203#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode, TypeInfo, MaxEncodedLen)]
1204pub struct AccountInfo<Nonce, AccountData> {
1205	/// The number of transactions this account has sent.
1206	pub nonce: Nonce,
1207	/// The number of other modules that currently depend on this account's existence. The account
1208	/// cannot be reaped until this is zero.
1209	pub consumers: RefCount,
1210	/// The number of other modules that allow this account to exist. The account may not be reaped
1211	/// until this and `sufficients` are both zero.
1212	pub providers: RefCount,
1213	/// The number of modules that allow this account to exist for their own purposes only. The
1214	/// account may not be reaped until this and `providers` are both zero.
1215	pub sufficients: RefCount,
1216	/// The additional data that belongs to this account. Used to store the balance(s) in a lot of
1217	/// chains.
1218	pub data: AccountData,
1219}
1220
1221/// Stores the `spec_version` and `spec_name` of when the last runtime upgrade
1222/// happened.
1223#[derive(RuntimeDebug, Encode, Decode, TypeInfo)]
1224#[cfg_attr(feature = "std", derive(PartialEq))]
1225pub struct LastRuntimeUpgradeInfo {
1226	pub spec_version: codec::Compact<u32>,
1227	pub spec_name: Cow<'static, str>,
1228}
1229
1230impl LastRuntimeUpgradeInfo {
1231	/// Returns if the runtime was upgraded in comparison of `self` and `current`.
1232	///
1233	/// Checks if either the `spec_version` increased or the `spec_name` changed.
1234	pub fn was_upgraded(&self, current: &RuntimeVersion) -> bool {
1235		current.spec_version > self.spec_version.0 || current.spec_name != self.spec_name
1236	}
1237}
1238
1239impl From<RuntimeVersion> for LastRuntimeUpgradeInfo {
1240	fn from(version: RuntimeVersion) -> Self {
1241		Self { spec_version: version.spec_version.into(), spec_name: version.spec_name }
1242	}
1243}
1244
1245/// Ensure the origin is Root.
1246pub struct EnsureRoot<AccountId>(core::marker::PhantomData<AccountId>);
1247impl<O: OriginTrait, AccountId> EnsureOrigin<O> for EnsureRoot<AccountId> {
1248	type Success = ();
1249	fn try_origin(o: O) -> Result<Self::Success, O> {
1250		match o.as_system_ref() {
1251			Some(RawOrigin::Root) => Ok(()),
1252			_ => Err(o),
1253		}
1254	}
1255
1256	#[cfg(feature = "runtime-benchmarks")]
1257	fn try_successful_origin() -> Result<O, ()> {
1258		Ok(O::root())
1259	}
1260}
1261
1262impl_ensure_origin_with_arg_ignoring_arg! {
1263	impl< { O: .., AccountId: Decode, T } >
1264		EnsureOriginWithArg<O, T> for EnsureRoot<AccountId>
1265	{}
1266}
1267
1268/// Ensure the origin is Root and return the provided `Success` value.
1269pub struct EnsureRootWithSuccess<AccountId, Success>(
1270	core::marker::PhantomData<(AccountId, Success)>,
1271);
1272impl<O: OriginTrait, AccountId, Success: TypedGet> EnsureOrigin<O>
1273	for EnsureRootWithSuccess<AccountId, Success>
1274{
1275	type Success = Success::Type;
1276	fn try_origin(o: O) -> Result<Self::Success, O> {
1277		match o.as_system_ref() {
1278			Some(RawOrigin::Root) => Ok(Success::get()),
1279			_ => Err(o),
1280		}
1281	}
1282
1283	#[cfg(feature = "runtime-benchmarks")]
1284	fn try_successful_origin() -> Result<O, ()> {
1285		Ok(O::root())
1286	}
1287}
1288
1289impl_ensure_origin_with_arg_ignoring_arg! {
1290	impl< { O: .., AccountId: Decode, Success: TypedGet, T } >
1291		EnsureOriginWithArg<O, T> for EnsureRootWithSuccess<AccountId, Success>
1292	{}
1293}
1294
1295/// Ensure the origin is provided `Ensure` origin and return the provided `Success` value.
1296pub struct EnsureWithSuccess<Ensure, AccountId, Success>(
1297	core::marker::PhantomData<(Ensure, AccountId, Success)>,
1298);
1299
1300impl<O: OriginTrait, Ensure: EnsureOrigin<O>, AccountId, Success: TypedGet> EnsureOrigin<O>
1301	for EnsureWithSuccess<Ensure, AccountId, Success>
1302{
1303	type Success = Success::Type;
1304
1305	fn try_origin(o: O) -> Result<Self::Success, O> {
1306		Ensure::try_origin(o).map(|_| Success::get())
1307	}
1308
1309	#[cfg(feature = "runtime-benchmarks")]
1310	fn try_successful_origin() -> Result<O, ()> {
1311		Ensure::try_successful_origin()
1312	}
1313}
1314
1315/// Ensure the origin is any `Signed` origin.
1316pub struct EnsureSigned<AccountId>(core::marker::PhantomData<AccountId>);
1317impl<O: OriginTrait<AccountId = AccountId>, AccountId: Decode + Clone> EnsureOrigin<O>
1318	for EnsureSigned<AccountId>
1319{
1320	type Success = AccountId;
1321	fn try_origin(o: O) -> Result<Self::Success, O> {
1322		match o.as_system_ref() {
1323			Some(RawOrigin::Signed(who)) => Ok(who.clone()),
1324			_ => Err(o),
1325		}
1326	}
1327
1328	#[cfg(feature = "runtime-benchmarks")]
1329	fn try_successful_origin() -> Result<O, ()> {
1330		let zero_account_id =
1331			AccountId::decode(&mut TrailingZeroInput::zeroes()).map_err(|_| ())?;
1332		Ok(O::signed(zero_account_id))
1333	}
1334}
1335
1336impl_ensure_origin_with_arg_ignoring_arg! {
1337	impl< { O: OriginTrait<AccountId = AccountId>, AccountId: Decode + Clone, T } >
1338		EnsureOriginWithArg<O, T> for EnsureSigned<AccountId>
1339	{}
1340}
1341
1342/// Ensure the origin is `Signed` origin from the given `AccountId`.
1343pub struct EnsureSignedBy<Who, AccountId>(core::marker::PhantomData<(Who, AccountId)>);
1344impl<
1345		O: OriginTrait<AccountId = AccountId>,
1346		Who: SortedMembers<AccountId>,
1347		AccountId: PartialEq + Clone + Ord + Decode,
1348	> EnsureOrigin<O> for EnsureSignedBy<Who, AccountId>
1349{
1350	type Success = AccountId;
1351	fn try_origin(o: O) -> Result<Self::Success, O> {
1352		match o.as_system_ref() {
1353			Some(RawOrigin::Signed(ref who)) if Who::contains(who) => Ok(who.clone()),
1354			_ => Err(o),
1355		}
1356	}
1357
1358	#[cfg(feature = "runtime-benchmarks")]
1359	fn try_successful_origin() -> Result<O, ()> {
1360		let first_member = match Who::sorted_members().first() {
1361			Some(account) => account.clone(),
1362			None => AccountId::decode(&mut TrailingZeroInput::zeroes()).map_err(|_| ())?,
1363		};
1364		Ok(O::signed(first_member))
1365	}
1366}
1367
1368impl_ensure_origin_with_arg_ignoring_arg! {
1369	impl< { O: OriginTrait<AccountId = AccountId>, Who: SortedMembers<AccountId>, AccountId: PartialEq + Clone + Ord + Decode, T } >
1370		EnsureOriginWithArg<O, T> for EnsureSignedBy<Who, AccountId>
1371	{}
1372}
1373
1374/// Ensure the origin is `None`. i.e. unsigned transaction.
1375pub struct EnsureNone<AccountId>(core::marker::PhantomData<AccountId>);
1376impl<O: OriginTrait<AccountId = AccountId>, AccountId> EnsureOrigin<O> for EnsureNone<AccountId> {
1377	type Success = ();
1378	fn try_origin(o: O) -> Result<Self::Success, O> {
1379		match o.as_system_ref() {
1380			Some(RawOrigin::None) => Ok(()),
1381			_ => Err(o),
1382		}
1383	}
1384
1385	#[cfg(feature = "runtime-benchmarks")]
1386	fn try_successful_origin() -> Result<O, ()> {
1387		Ok(O::none())
1388	}
1389}
1390
1391impl_ensure_origin_with_arg_ignoring_arg! {
1392	impl< { O: OriginTrait<AccountId = AccountId>, AccountId, T } >
1393		EnsureOriginWithArg<O, T> for EnsureNone<AccountId>
1394	{}
1395}
1396
1397/// Always fail.
1398pub struct EnsureNever<Success>(core::marker::PhantomData<Success>);
1399impl<O, Success> EnsureOrigin<O> for EnsureNever<Success> {
1400	type Success = Success;
1401	fn try_origin(o: O) -> Result<Self::Success, O> {
1402		Err(o)
1403	}
1404
1405	#[cfg(feature = "runtime-benchmarks")]
1406	fn try_successful_origin() -> Result<O, ()> {
1407		Err(())
1408	}
1409}
1410
1411impl_ensure_origin_with_arg_ignoring_arg! {
1412	impl< { O, Success, T } >
1413		EnsureOriginWithArg<O, T> for EnsureNever<Success>
1414	{}
1415}
1416
1417#[docify::export]
1418/// Ensure that the origin `o` represents a signed extrinsic (i.e. transaction).
1419/// Returns `Ok` with the account that signed the extrinsic or an `Err` otherwise.
1420pub fn ensure_signed<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<AccountId, BadOrigin>
1421where
1422	OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>,
1423{
1424	match o.into() {
1425		Ok(RawOrigin::Signed(t)) => Ok(t),
1426		_ => Err(BadOrigin),
1427	}
1428}
1429
1430/// Ensure that the origin `o` represents either a signed extrinsic (i.e. transaction) or the root.
1431/// Returns `Ok` with the account that signed the extrinsic, `None` if it was root,  or an `Err`
1432/// otherwise.
1433pub fn ensure_signed_or_root<OuterOrigin, AccountId>(
1434	o: OuterOrigin,
1435) -> Result<Option<AccountId>, BadOrigin>
1436where
1437	OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>,
1438{
1439	match o.into() {
1440		Ok(RawOrigin::Root) => Ok(None),
1441		Ok(RawOrigin::Signed(t)) => Ok(Some(t)),
1442		_ => Err(BadOrigin),
1443	}
1444}
1445
1446/// Ensure that the origin `o` represents the root. Returns `Ok` or an `Err` otherwise.
1447pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), BadOrigin>
1448where
1449	OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>,
1450{
1451	match o.into() {
1452		Ok(RawOrigin::Root) => Ok(()),
1453		_ => Err(BadOrigin),
1454	}
1455}
1456
1457/// Ensure that the origin `o` represents an unsigned extrinsic. Returns `Ok` or an `Err` otherwise.
1458pub fn ensure_none<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), BadOrigin>
1459where
1460	OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>,
1461{
1462	match o.into() {
1463		Ok(RawOrigin::None) => Ok(()),
1464		_ => Err(BadOrigin),
1465	}
1466}
1467
1468/// Reference status; can be either referenced or unreferenced.
1469#[derive(RuntimeDebug)]
1470pub enum RefStatus {
1471	Referenced,
1472	Unreferenced,
1473}
1474
1475/// Some resultant status relevant to incrementing a provider/self-sufficient reference.
1476#[derive(Eq, PartialEq, RuntimeDebug)]
1477pub enum IncRefStatus {
1478	/// Account was created.
1479	Created,
1480	/// Account already existed.
1481	Existed,
1482}
1483
1484/// Some resultant status relevant to decrementing a provider/self-sufficient reference.
1485#[derive(Eq, PartialEq, RuntimeDebug)]
1486pub enum DecRefStatus {
1487	/// Account was destroyed.
1488	Reaped,
1489	/// Account still exists.
1490	Exists,
1491}
1492
1493/// Result of [`Pallet::can_set_code`].
1494pub enum CanSetCodeResult<T: Config> {
1495	/// Everything is fine.
1496	Ok,
1497	/// Multi-block migrations are on-going.
1498	MultiBlockMigrationsOngoing,
1499	/// The runtime version is invalid or could not be fetched.
1500	InvalidVersion(Error<T>),
1501}
1502
1503impl<T: Config> CanSetCodeResult<T> {
1504	/// Convert `Self` into a result.
1505	pub fn into_result(self) -> Result<(), DispatchError> {
1506		match self {
1507			Self::Ok => Ok(()),
1508			Self::MultiBlockMigrationsOngoing =>
1509				Err(Error::<T>::MultiBlockMigrationsOngoing.into()),
1510			Self::InvalidVersion(err) => Err(err.into()),
1511		}
1512	}
1513
1514	/// Is this `Ok`?
1515	pub fn is_ok(&self) -> bool {
1516		matches!(self, Self::Ok)
1517	}
1518}
1519
1520impl<T: Config> Pallet<T> {
1521	/// Returns the `spec_version` of the last runtime upgrade.
1522	///
1523	/// This function is useful for writing guarded runtime migrations in the runtime. A runtime
1524	/// migration can use the `spec_version` to ensure that it isn't applied twice. This works
1525	/// similar as the storage version for pallets.
1526	///
1527	/// This functions returns the `spec_version` of the last runtime upgrade while executing the
1528	/// runtime migrations
1529	/// [`on_runtime_upgrade`](frame_support::traits::OnRuntimeUpgrade::on_runtime_upgrade)
1530	/// function. After all migrations are executed, this will return the `spec_version` of the
1531	/// current runtime until there is another runtime upgrade.
1532	///
1533	/// Example:
1534	#[doc = docify::embed!("src/tests.rs", last_runtime_upgrade_spec_version_usage)]
1535	pub fn last_runtime_upgrade_spec_version() -> u32 {
1536		LastRuntimeUpgrade::<T>::get().map_or(0, |l| l.spec_version.0)
1537	}
1538
1539	/// Returns true if the given account exists.
1540	pub fn account_exists(who: &T::AccountId) -> bool {
1541		Account::<T>::contains_key(who)
1542	}
1543
1544	/// Write code to the storage and emit related events and digest items.
1545	///
1546	/// Note this function almost never should be used directly. It is exposed
1547	/// for `OnSetCode` implementations that defer actual code being written to
1548	/// the storage (for instance in case of parachains).
1549	pub fn update_code_in_storage(code: &[u8]) {
1550		storage::unhashed::put_raw(well_known_keys::CODE, code);
1551		Self::deposit_log(generic::DigestItem::RuntimeEnvironmentUpdated);
1552		Self::deposit_event(Event::CodeUpdated);
1553	}
1554
1555	/// Whether all inherents have been applied.
1556	pub fn inherents_applied() -> bool {
1557		InherentsApplied::<T>::get()
1558	}
1559
1560	/// Note that all inherents have been applied.
1561	///
1562	/// Should be called immediately after all inherents have been applied. Must be called at least
1563	/// once per block.
1564	pub fn note_inherents_applied() {
1565		InherentsApplied::<T>::put(true);
1566	}
1567
1568	/// Increment the reference counter on an account.
1569	#[deprecated = "Use `inc_consumers` instead"]
1570	pub fn inc_ref(who: &T::AccountId) {
1571		let _ = Self::inc_consumers(who);
1572	}
1573
1574	/// Decrement the reference counter on an account. This *MUST* only be done once for every time
1575	/// you called `inc_consumers` on `who`.
1576	#[deprecated = "Use `dec_consumers` instead"]
1577	pub fn dec_ref(who: &T::AccountId) {
1578		let _ = Self::dec_consumers(who);
1579	}
1580
1581	/// The number of outstanding references for the account `who`.
1582	#[deprecated = "Use `consumers` instead"]
1583	pub fn refs(who: &T::AccountId) -> RefCount {
1584		Self::consumers(who)
1585	}
1586
1587	/// True if the account has no outstanding references.
1588	#[deprecated = "Use `!is_provider_required` instead"]
1589	pub fn allow_death(who: &T::AccountId) -> bool {
1590		!Self::is_provider_required(who)
1591	}
1592
1593	/// Increment the provider reference counter on an account.
1594	pub fn inc_providers(who: &T::AccountId) -> IncRefStatus {
1595		Account::<T>::mutate(who, |a| {
1596			if a.providers == 0 && a.sufficients == 0 {
1597				// Account is being created.
1598				a.providers = 1;
1599				Self::on_created_account(who.clone(), a);
1600				IncRefStatus::Created
1601			} else {
1602				a.providers = a.providers.saturating_add(1);
1603				IncRefStatus::Existed
1604			}
1605		})
1606	}
1607
1608	/// Decrement the provider reference counter on an account.
1609	///
1610	/// This *MUST* only be done once for every time you called `inc_providers` on `who`.
1611	pub fn dec_providers(who: &T::AccountId) -> Result<DecRefStatus, DispatchError> {
1612		Account::<T>::try_mutate_exists(who, |maybe_account| {
1613			if let Some(mut account) = maybe_account.take() {
1614				if account.providers == 0 {
1615					// Logic error - cannot decrement beyond zero.
1616					log::error!(
1617						target: LOG_TARGET,
1618						"Logic error: Unexpected underflow in reducing provider",
1619					);
1620					account.providers = 1;
1621				}
1622				match (account.providers, account.consumers, account.sufficients) {
1623					(1, 0, 0) => {
1624						// No providers left (and no consumers) and no sufficients. Account dead.
1625
1626						Pallet::<T>::on_killed_account(who.clone());
1627						Ok(DecRefStatus::Reaped)
1628					},
1629					(1, c, _) if c > 0 => {
1630						// Cannot remove last provider if there are consumers.
1631						Err(DispatchError::ConsumerRemaining)
1632					},
1633					(x, _, _) => {
1634						// Account will continue to exist as there is either > 1 provider or
1635						// > 0 sufficients.
1636						account.providers = x - 1;
1637						*maybe_account = Some(account);
1638						Ok(DecRefStatus::Exists)
1639					},
1640				}
1641			} else {
1642				log::error!(
1643					target: LOG_TARGET,
1644					"Logic error: Account already dead when reducing provider",
1645				);
1646				Ok(DecRefStatus::Reaped)
1647			}
1648		})
1649	}
1650
1651	/// Increment the self-sufficient reference counter on an account.
1652	pub fn inc_sufficients(who: &T::AccountId) -> IncRefStatus {
1653		Account::<T>::mutate(who, |a| {
1654			if a.providers + a.sufficients == 0 {
1655				// Account is being created.
1656				a.sufficients = 1;
1657				Self::on_created_account(who.clone(), a);
1658				IncRefStatus::Created
1659			} else {
1660				a.sufficients = a.sufficients.saturating_add(1);
1661				IncRefStatus::Existed
1662			}
1663		})
1664	}
1665
1666	/// Decrement the sufficients reference counter on an account.
1667	///
1668	/// This *MUST* only be done once for every time you called `inc_sufficients` on `who`.
1669	pub fn dec_sufficients(who: &T::AccountId) -> DecRefStatus {
1670		Account::<T>::mutate_exists(who, |maybe_account| {
1671			if let Some(mut account) = maybe_account.take() {
1672				if account.sufficients == 0 {
1673					// Logic error - cannot decrement beyond zero.
1674					log::error!(
1675						target: LOG_TARGET,
1676						"Logic error: Unexpected underflow in reducing sufficients",
1677					);
1678				}
1679				match (account.sufficients, account.providers) {
1680					(0, 0) | (1, 0) => {
1681						Pallet::<T>::on_killed_account(who.clone());
1682						DecRefStatus::Reaped
1683					},
1684					(x, _) => {
1685						account.sufficients = x.saturating_sub(1);
1686						*maybe_account = Some(account);
1687						DecRefStatus::Exists
1688					},
1689				}
1690			} else {
1691				log::error!(
1692					target: LOG_TARGET,
1693					"Logic error: Account already dead when reducing provider",
1694				);
1695				DecRefStatus::Reaped
1696			}
1697		})
1698	}
1699
1700	/// The number of outstanding provider references for the account `who`.
1701	pub fn providers(who: &T::AccountId) -> RefCount {
1702		Account::<T>::get(who).providers
1703	}
1704
1705	/// The number of outstanding sufficient references for the account `who`.
1706	pub fn sufficients(who: &T::AccountId) -> RefCount {
1707		Account::<T>::get(who).sufficients
1708	}
1709
1710	/// The number of outstanding provider and sufficient references for the account `who`.
1711	pub fn reference_count(who: &T::AccountId) -> RefCount {
1712		let a = Account::<T>::get(who);
1713		a.providers + a.sufficients
1714	}
1715
1716	/// Increment the reference counter on an account.
1717	///
1718	/// The account `who`'s `providers` must be non-zero and the current number of consumers must
1719	/// be less than `MaxConsumers::max_consumers()` or this will return an error.
1720	pub fn inc_consumers(who: &T::AccountId) -> Result<(), DispatchError> {
1721		Account::<T>::try_mutate(who, |a| {
1722			if a.providers > 0 {
1723				if a.consumers < T::MaxConsumers::max_consumers() {
1724					a.consumers = a.consumers.saturating_add(1);
1725					Ok(())
1726				} else {
1727					Err(DispatchError::TooManyConsumers)
1728				}
1729			} else {
1730				Err(DispatchError::NoProviders)
1731			}
1732		})
1733	}
1734
1735	/// Increment the reference counter on an account, ignoring the `MaxConsumers` limits.
1736	///
1737	/// The account `who`'s `providers` must be non-zero or this will return an error.
1738	pub fn inc_consumers_without_limit(who: &T::AccountId) -> Result<(), DispatchError> {
1739		Account::<T>::try_mutate(who, |a| {
1740			if a.providers > 0 {
1741				a.consumers = a.consumers.saturating_add(1);
1742				Ok(())
1743			} else {
1744				Err(DispatchError::NoProviders)
1745			}
1746		})
1747	}
1748
1749	/// Decrement the reference counter on an account. This *MUST* only be done once for every time
1750	/// you called `inc_consumers` on `who`.
1751	pub fn dec_consumers(who: &T::AccountId) {
1752		Account::<T>::mutate(who, |a| {
1753			if a.consumers > 0 {
1754				a.consumers -= 1;
1755			} else {
1756				log::error!(
1757					target: LOG_TARGET,
1758					"Logic error: Unexpected underflow in reducing consumer",
1759				);
1760			}
1761		})
1762	}
1763
1764	/// The number of outstanding references for the account `who`.
1765	pub fn consumers(who: &T::AccountId) -> RefCount {
1766		Account::<T>::get(who).consumers
1767	}
1768
1769	/// True if the account has some outstanding consumer references.
1770	pub fn is_provider_required(who: &T::AccountId) -> bool {
1771		Account::<T>::get(who).consumers != 0
1772	}
1773
1774	/// True if the account has no outstanding consumer references or more than one provider.
1775	pub fn can_dec_provider(who: &T::AccountId) -> bool {
1776		let a = Account::<T>::get(who);
1777		a.consumers == 0 || a.providers > 1
1778	}
1779
1780	/// True if the account has at least one provider reference and adding `amount` consumer
1781	/// references would not take it above the the maximum.
1782	pub fn can_accrue_consumers(who: &T::AccountId, amount: u32) -> bool {
1783		let a = Account::<T>::get(who);
1784		match a.consumers.checked_add(amount) {
1785			Some(c) => a.providers > 0 && c <= T::MaxConsumers::max_consumers(),
1786			None => false,
1787		}
1788	}
1789
1790	/// True if the account has at least one provider reference and fewer consumer references than
1791	/// the maximum.
1792	pub fn can_inc_consumer(who: &T::AccountId) -> bool {
1793		Self::can_accrue_consumers(who, 1)
1794	}
1795
1796	/// Deposits an event into this block's event record.
1797	///
1798	/// NOTE: Events not registered at the genesis block and quietly omitted.
1799	pub fn deposit_event(event: impl Into<T::RuntimeEvent>) {
1800		Self::deposit_event_indexed(&[], event.into());
1801	}
1802
1803	/// Deposits an event into this block's event record adding this event
1804	/// to the corresponding topic indexes.
1805	///
1806	/// This will update storage entries that correspond to the specified topics.
1807	/// It is expected that light-clients could subscribe to this topics.
1808	///
1809	/// NOTE: Events not registered at the genesis block and quietly omitted.
1810	pub fn deposit_event_indexed(topics: &[T::Hash], event: T::RuntimeEvent) {
1811		let block_number = Self::block_number();
1812
1813		// Don't populate events on genesis.
1814		if block_number.is_zero() {
1815			return
1816		}
1817
1818		let phase = ExecutionPhase::<T>::get().unwrap_or_default();
1819		let event = EventRecord { phase, event, topics: topics.to_vec() };
1820
1821		// Index of the event to be added.
1822		let event_idx = {
1823			let old_event_count = EventCount::<T>::get();
1824			let new_event_count = match old_event_count.checked_add(1) {
1825				// We've reached the maximum number of events at this block, just
1826				// don't do anything and leave the event_count unaltered.
1827				None => return,
1828				Some(nc) => nc,
1829			};
1830			EventCount::<T>::put(new_event_count);
1831			old_event_count
1832		};
1833
1834		Events::<T>::append(event);
1835
1836		for topic in topics {
1837			<EventTopics<T>>::append(topic, &(block_number, event_idx));
1838		}
1839	}
1840
1841	/// Gets the index of extrinsic that is currently executing.
1842	pub fn extrinsic_index() -> Option<u32> {
1843		storage::unhashed::get(well_known_keys::EXTRINSIC_INDEX)
1844	}
1845
1846	/// Gets extrinsics count.
1847	pub fn extrinsic_count() -> u32 {
1848		ExtrinsicCount::<T>::get().unwrap_or_default()
1849	}
1850
1851	pub fn all_extrinsics_len() -> u32 {
1852		AllExtrinsicsLen::<T>::get().unwrap_or_default()
1853	}
1854
1855	/// Inform the system pallet of some additional weight that should be accounted for, in the
1856	/// current block.
1857	///
1858	/// NOTE: use with extra care; this function is made public only be used for certain pallets
1859	/// that need it. A runtime that does not have dynamic calls should never need this and should
1860	/// stick to static weights. A typical use case for this is inner calls or smart contract calls.
1861	/// Furthermore, it only makes sense to use this when it is presumably  _cheap_ to provide the
1862	/// argument `weight`; In other words, if this function is to be used to account for some
1863	/// unknown, user provided call's weight, it would only make sense to use it if you are sure you
1864	/// can rapidly compute the weight of the inner call.
1865	///
1866	/// Even more dangerous is to note that this function does NOT take any action, if the new sum
1867	/// of block weight is more than the block weight limit. This is what the _unchecked_.
1868	///
1869	/// Another potential use-case could be for the `on_initialize` and `on_finalize` hooks.
1870	pub fn register_extra_weight_unchecked(weight: Weight, class: DispatchClass) {
1871		BlockWeight::<T>::mutate(|current_weight| {
1872			current_weight.accrue(weight, class);
1873		});
1874	}
1875
1876	/// Start the execution of a particular block.
1877	pub fn initialize(number: &BlockNumberFor<T>, parent_hash: &T::Hash, digest: &generic::Digest) {
1878		// populate environment
1879		ExecutionPhase::<T>::put(Phase::Initialization);
1880		storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &0u32);
1881		let entropy = (b"frame_system::initialize", parent_hash).using_encoded(blake2_256);
1882		storage::unhashed::put_raw(well_known_keys::INTRABLOCK_ENTROPY, &entropy[..]);
1883		<Number<T>>::put(number);
1884		<Digest<T>>::put(digest);
1885		<ParentHash<T>>::put(parent_hash);
1886		<BlockHash<T>>::insert(*number - One::one(), parent_hash);
1887		<InherentsApplied<T>>::kill();
1888
1889		// Remove previous block data from storage
1890		BlockWeight::<T>::kill();
1891	}
1892
1893	/// Remove temporary "environment" entries in storage, compute the storage root and return the
1894	/// resulting header for this block.
1895	pub fn finalize() -> HeaderFor<T> {
1896		log::debug!(
1897			target: LOG_TARGET,
1898			"[{:?}] {} extrinsics, length: {} (normal {}%, op: {}%, mandatory {}%) / normal weight:\
1899			 {} ({}%) op weight {} ({}%) / mandatory weight {} ({}%)",
1900			Self::block_number(),
1901			Self::extrinsic_count(),
1902			Self::all_extrinsics_len(),
1903			sp_runtime::Percent::from_rational(
1904				Self::all_extrinsics_len(),
1905				*T::BlockLength::get().max.get(DispatchClass::Normal)
1906			).deconstruct(),
1907			sp_runtime::Percent::from_rational(
1908				Self::all_extrinsics_len(),
1909				*T::BlockLength::get().max.get(DispatchClass::Operational)
1910			).deconstruct(),
1911			sp_runtime::Percent::from_rational(
1912				Self::all_extrinsics_len(),
1913				*T::BlockLength::get().max.get(DispatchClass::Mandatory)
1914			).deconstruct(),
1915			Self::block_weight().get(DispatchClass::Normal),
1916			sp_runtime::Percent::from_rational(
1917				Self::block_weight().get(DispatchClass::Normal).ref_time(),
1918				T::BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap_or(Bounded::max_value()).ref_time()
1919			).deconstruct(),
1920			Self::block_weight().get(DispatchClass::Operational),
1921			sp_runtime::Percent::from_rational(
1922				Self::block_weight().get(DispatchClass::Operational).ref_time(),
1923				T::BlockWeights::get().get(DispatchClass::Operational).max_total.unwrap_or(Bounded::max_value()).ref_time()
1924			).deconstruct(),
1925			Self::block_weight().get(DispatchClass::Mandatory),
1926			sp_runtime::Percent::from_rational(
1927				Self::block_weight().get(DispatchClass::Mandatory).ref_time(),
1928				T::BlockWeights::get().get(DispatchClass::Mandatory).max_total.unwrap_or(Bounded::max_value()).ref_time()
1929			).deconstruct(),
1930		);
1931		ExecutionPhase::<T>::kill();
1932		AllExtrinsicsLen::<T>::kill();
1933		storage::unhashed::kill(well_known_keys::INTRABLOCK_ENTROPY);
1934		InherentsApplied::<T>::kill();
1935
1936		// The following fields
1937		//
1938		// - <Events<T>>
1939		// - <EventCount<T>>
1940		// - <EventTopics<T>>
1941		// - <Number<T>>
1942		// - <ParentHash<T>>
1943		// - <Digest<T>>
1944		//
1945		// stay to be inspected by the client and will be cleared by `Self::initialize`.
1946		let number = <Number<T>>::get();
1947		let parent_hash = <ParentHash<T>>::get();
1948		let digest = <Digest<T>>::get();
1949
1950		let extrinsics = (0..ExtrinsicCount::<T>::take().unwrap_or_default())
1951			.map(ExtrinsicData::<T>::take)
1952			.collect();
1953		let extrinsics_root_state_version = T::Version::get().extrinsics_root_state_version();
1954		let extrinsics_root =
1955			extrinsics_data_root::<T::Hashing>(extrinsics, extrinsics_root_state_version);
1956
1957		// move block hash pruning window by one block
1958		let block_hash_count = T::BlockHashCount::get();
1959		let to_remove = number.saturating_sub(block_hash_count).saturating_sub(One::one());
1960
1961		// keep genesis hash
1962		if !to_remove.is_zero() {
1963			<BlockHash<T>>::remove(to_remove);
1964		}
1965
1966		let version = T::Version::get().state_version();
1967		let storage_root = T::Hash::decode(&mut &sp_io::storage::root(version)[..])
1968			.expect("Node is configured to use the same hash; qed");
1969
1970		HeaderFor::<T>::new(number, extrinsics_root, storage_root, parent_hash, digest)
1971	}
1972
1973	/// Deposits a log and ensures it matches the block's log data.
1974	pub fn deposit_log(item: generic::DigestItem) {
1975		<Digest<T>>::append(item);
1976	}
1977
1978	/// Get the basic externalities for this pallet, useful for tests.
1979	#[cfg(any(feature = "std", test))]
1980	pub fn externalities() -> TestExternalities {
1981		TestExternalities::new(sp_core::storage::Storage {
1982			top: [
1983				(<BlockHash<T>>::hashed_key_for(BlockNumberFor::<T>::zero()), [69u8; 32].encode()),
1984				(<Number<T>>::hashed_key().to_vec(), BlockNumberFor::<T>::one().encode()),
1985				(<ParentHash<T>>::hashed_key().to_vec(), [69u8; 32].encode()),
1986			]
1987			.into_iter()
1988			.collect(),
1989			children_default: Default::default(),
1990		})
1991	}
1992
1993	/// Get the current events deposited by the runtime.
1994	///
1995	/// NOTE: This should only be used in tests. Reading events from the runtime can have a large
1996	/// impact on the PoV size of a block. Users should use alternative and well bounded storage
1997	/// items for any behavior like this.
1998	///
1999	/// NOTE: Events not registered at the genesis block and quietly omitted.
2000	#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
2001	pub fn events() -> Vec<EventRecord<T::RuntimeEvent, T::Hash>> {
2002		// Dereferencing the events here is fine since we are not in the memory-restricted runtime.
2003		Self::read_events_no_consensus().map(|e| *e).collect()
2004	}
2005
2006	/// Get a single event at specified index.
2007	///
2008	/// Should only be called if you know what you are doing and outside of the runtime block
2009	/// execution else it can have a large impact on the PoV size of a block.
2010	pub fn event_no_consensus(index: usize) -> Option<T::RuntimeEvent> {
2011		Self::read_events_no_consensus().nth(index).map(|e| e.event.clone())
2012	}
2013
2014	/// Get the current events deposited by the runtime.
2015	///
2016	/// Should only be called if you know what you are doing and outside of the runtime block
2017	/// execution else it can have a large impact on the PoV size of a block.
2018	pub fn read_events_no_consensus(
2019	) -> impl Iterator<Item = Box<EventRecord<T::RuntimeEvent, T::Hash>>> {
2020		Events::<T>::stream_iter()
2021	}
2022
2023	/// Read and return the events of a specific pallet, as denoted by `E`.
2024	///
2025	/// This is useful for a pallet that wishes to read only the events it has deposited into
2026	/// `frame_system` using the standard `fn deposit_event`.
2027	pub fn read_events_for_pallet<E>() -> Vec<E>
2028	where
2029		T::RuntimeEvent: TryInto<E>,
2030	{
2031		Events::<T>::get()
2032			.into_iter()
2033			.map(|er| er.event)
2034			.filter_map(|e| e.try_into().ok())
2035			.collect::<_>()
2036	}
2037
2038	/// Simulate the execution of a block sequence up to a specified height, injecting the
2039	/// provided hooks at each block.
2040	///
2041	/// `on_finalize` is always called before `on_initialize` with the current block number.
2042	/// `on_initalize` is always called with the next block number.
2043	///
2044	/// These hooks allows custom logic to be executed at each block at specific location.
2045	/// For example, you might use one of them to set a timestamp for each block.
2046	#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
2047	pub fn run_to_block_with<AllPalletsWithSystem>(
2048		n: BlockNumberFor<T>,
2049		mut hooks: RunToBlockHooks<T>,
2050	) where
2051		AllPalletsWithSystem: frame_support::traits::OnInitialize<BlockNumberFor<T>>
2052			+ frame_support::traits::OnFinalize<BlockNumberFor<T>>,
2053	{
2054		let mut bn = Self::block_number();
2055
2056		while bn < n {
2057			// Skip block 0.
2058			if !bn.is_zero() {
2059				(hooks.before_finalize)(bn);
2060				AllPalletsWithSystem::on_finalize(bn);
2061				(hooks.after_finalize)(bn);
2062			}
2063
2064			bn += One::one();
2065
2066			Self::set_block_number(bn);
2067			(hooks.before_initialize)(bn);
2068			AllPalletsWithSystem::on_initialize(bn);
2069			(hooks.after_initialize)(bn);
2070		}
2071	}
2072
2073	/// Simulate the execution of a block sequence up to a specified height.
2074	#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
2075	pub fn run_to_block<AllPalletsWithSystem>(n: BlockNumberFor<T>)
2076	where
2077		AllPalletsWithSystem: frame_support::traits::OnInitialize<BlockNumberFor<T>>
2078			+ frame_support::traits::OnFinalize<BlockNumberFor<T>>,
2079	{
2080		Self::run_to_block_with::<AllPalletsWithSystem>(n, Default::default());
2081	}
2082
2083	/// Set the block number to something in particular. Can be used as an alternative to
2084	/// `initialize` for tests that don't need to bother with the other environment entries.
2085	#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
2086	pub fn set_block_number(n: BlockNumberFor<T>) {
2087		<Number<T>>::put(n);
2088	}
2089
2090	/// Sets the index of extrinsic that is currently executing.
2091	#[cfg(any(feature = "std", test))]
2092	pub fn set_extrinsic_index(extrinsic_index: u32) {
2093		storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &extrinsic_index)
2094	}
2095
2096	/// Set the parent hash number to something in particular. Can be used as an alternative to
2097	/// `initialize` for tests that don't need to bother with the other environment entries.
2098	#[cfg(any(feature = "std", test))]
2099	pub fn set_parent_hash(n: T::Hash) {
2100		<ParentHash<T>>::put(n);
2101	}
2102
2103	/// Set the current block weight. This should only be used in some integration tests.
2104	#[cfg(any(feature = "std", test))]
2105	pub fn set_block_consumed_resources(weight: Weight, len: usize) {
2106		BlockWeight::<T>::mutate(|current_weight| {
2107			current_weight.set(weight, DispatchClass::Normal)
2108		});
2109		AllExtrinsicsLen::<T>::put(len as u32);
2110	}
2111
2112	/// Reset events.
2113	///
2114	/// This needs to be used in prior calling [`initialize`](Self::initialize) for each new block
2115	/// to clear events from previous block.
2116	pub fn reset_events() {
2117		<Events<T>>::kill();
2118		EventCount::<T>::kill();
2119		let _ = <EventTopics<T>>::clear(u32::max_value(), None);
2120	}
2121
2122	/// Assert the given `event` exists.
2123	///
2124	/// NOTE: Events not registered at the genesis block and quietly omitted.
2125	#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
2126	#[track_caller]
2127	pub fn assert_has_event(event: T::RuntimeEvent) {
2128		let warn = if Self::block_number().is_zero() {
2129			"WARNING: block number is zero, and events are not registered at block number zero.\n"
2130		} else {
2131			""
2132		};
2133
2134		let events = Self::events();
2135		assert!(
2136			events.iter().any(|record| record.event == event),
2137			"{warn}expected event {event:?} not found in events {events:?}",
2138		);
2139	}
2140
2141	/// Assert the last event equal to the given `event`.
2142	///
2143	/// NOTE: Events not registered at the genesis block and quietly omitted.
2144	#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
2145	#[track_caller]
2146	pub fn assert_last_event(event: T::RuntimeEvent) {
2147		let warn = if Self::block_number().is_zero() {
2148			"WARNING: block number is zero, and events are not registered at block number zero.\n"
2149		} else {
2150			""
2151		};
2152
2153		let last_event = Self::events()
2154			.last()
2155			.expect(&alloc::format!("{warn}events expected"))
2156			.event
2157			.clone();
2158		assert_eq!(
2159			last_event, event,
2160			"{warn}expected event {event:?} is not equal to the last event {last_event:?}",
2161		);
2162	}
2163
2164	/// Return the chain's current runtime version.
2165	pub fn runtime_version() -> RuntimeVersion {
2166		T::Version::get()
2167	}
2168
2169	/// Retrieve the account transaction counter from storage.
2170	pub fn account_nonce(who: impl EncodeLike<T::AccountId>) -> T::Nonce {
2171		Account::<T>::get(who).nonce
2172	}
2173
2174	/// Increment a particular account's nonce by 1.
2175	pub fn inc_account_nonce(who: impl EncodeLike<T::AccountId>) {
2176		Account::<T>::mutate(who, |a| a.nonce += T::Nonce::one());
2177	}
2178
2179	/// Note what the extrinsic data of the current extrinsic index is.
2180	///
2181	/// This is required to be called before applying an extrinsic. The data will used
2182	/// in [`Self::finalize`] to calculate the correct extrinsics root.
2183	pub fn note_extrinsic(encoded_xt: Vec<u8>) {
2184		ExtrinsicData::<T>::insert(Self::extrinsic_index().unwrap_or_default(), encoded_xt);
2185	}
2186
2187	/// To be called immediately after an extrinsic has been applied.
2188	///
2189	/// Emits an `ExtrinsicSuccess` or `ExtrinsicFailed` event depending on the outcome.
2190	/// The emitted event contains the post-dispatch corrected weight including
2191	/// the base-weight for its dispatch class.
2192	pub fn note_applied_extrinsic(r: &DispatchResultWithPostInfo, info: DispatchInfo) {
2193		let weight = extract_actual_weight(r, &info)
2194			.saturating_add(T::BlockWeights::get().get(info.class).base_extrinsic);
2195		let class = info.class;
2196		let pays_fee = extract_actual_pays_fee(r, &info);
2197		let dispatch_event_info = DispatchEventInfo { weight, class, pays_fee };
2198
2199		Self::deposit_event(match r {
2200			Ok(_) => Event::ExtrinsicSuccess { dispatch_info: dispatch_event_info },
2201			Err(err) => {
2202				log::trace!(
2203					target: LOG_TARGET,
2204					"Extrinsic failed at block({:?}): {:?}",
2205					Self::block_number(),
2206					err,
2207				);
2208				Event::ExtrinsicFailed {
2209					dispatch_error: err.error,
2210					dispatch_info: dispatch_event_info,
2211				}
2212			},
2213		});
2214
2215		log::trace!(
2216			target: LOG_TARGET,
2217			"Used block weight: {:?}",
2218			BlockWeight::<T>::get(),
2219		);
2220
2221		log::trace!(
2222			target: LOG_TARGET,
2223			"Used block length: {:?}",
2224			Pallet::<T>::all_extrinsics_len(),
2225		);
2226
2227		let next_extrinsic_index = Self::extrinsic_index().unwrap_or_default() + 1u32;
2228
2229		storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &next_extrinsic_index);
2230		ExecutionPhase::<T>::put(Phase::ApplyExtrinsic(next_extrinsic_index));
2231		ExtrinsicWeightReclaimed::<T>::kill();
2232	}
2233
2234	/// To be called immediately after `note_applied_extrinsic` of the last extrinsic of the block
2235	/// has been called.
2236	pub fn note_finished_extrinsics() {
2237		let extrinsic_index: u32 =
2238			storage::unhashed::take(well_known_keys::EXTRINSIC_INDEX).unwrap_or_default();
2239		ExtrinsicCount::<T>::put(extrinsic_index);
2240		ExecutionPhase::<T>::put(Phase::Finalization);
2241	}
2242
2243	/// To be called immediately after finishing the initialization of the block
2244	/// (e.g., called `on_initialize` for all pallets).
2245	pub fn note_finished_initialize() {
2246		ExecutionPhase::<T>::put(Phase::ApplyExtrinsic(0))
2247	}
2248
2249	/// An account is being created.
2250	pub fn on_created_account(who: T::AccountId, _a: &mut AccountInfo<T::Nonce, T::AccountData>) {
2251		T::OnNewAccount::on_new_account(&who);
2252		Self::deposit_event(Event::NewAccount { account: who });
2253	}
2254
2255	/// Do anything that needs to be done after an account has been killed.
2256	fn on_killed_account(who: T::AccountId) {
2257		T::OnKilledAccount::on_killed_account(&who);
2258		Self::deposit_event(Event::KilledAccount { account: who });
2259	}
2260
2261	/// Determine whether or not it is possible to update the code.
2262	///
2263	/// - `check_version`: Should the runtime version be checked?
2264	pub fn can_set_code(code: &[u8], check_version: bool) -> CanSetCodeResult<T> {
2265		if T::MultiBlockMigrator::ongoing() {
2266			return CanSetCodeResult::MultiBlockMigrationsOngoing
2267		}
2268
2269		if check_version {
2270			let current_version = T::Version::get();
2271			let Some(new_version) = sp_io::misc::runtime_version(code)
2272				.and_then(|v| RuntimeVersion::decode(&mut &v[..]).ok())
2273			else {
2274				return CanSetCodeResult::InvalidVersion(Error::<T>::FailedToExtractRuntimeVersion)
2275			};
2276
2277			cfg_if::cfg_if! {
2278				if #[cfg(all(feature = "runtime-benchmarks", not(test)))] {
2279					// Let's ensure the compiler doesn't optimize our fetching of the runtime version away.
2280					core::hint::black_box((new_version, current_version));
2281				} else {
2282					if new_version.spec_name != current_version.spec_name {
2283						return CanSetCodeResult::InvalidVersion( Error::<T>::InvalidSpecName)
2284					}
2285
2286					if new_version.spec_version <= current_version.spec_version {
2287						return CanSetCodeResult::InvalidVersion(Error::<T>::SpecVersionNeedsToIncrease)
2288					}
2289				}
2290			}
2291		}
2292
2293		CanSetCodeResult::Ok
2294	}
2295
2296	/// Authorize the given `code_hash` as upgrade.
2297	pub fn do_authorize_upgrade(code_hash: T::Hash, check_version: bool) {
2298		AuthorizedUpgrade::<T>::put(CodeUpgradeAuthorization { code_hash, check_version });
2299		Self::deposit_event(Event::UpgradeAuthorized { code_hash, check_version });
2300	}
2301
2302	/// Check that provided `code` is authorized as an upgrade.
2303	///
2304	/// Returns the [`CodeUpgradeAuthorization`].
2305	fn validate_code_is_authorized(
2306		code: &[u8],
2307	) -> Result<CodeUpgradeAuthorization<T>, DispatchError> {
2308		let authorization = AuthorizedUpgrade::<T>::get().ok_or(Error::<T>::NothingAuthorized)?;
2309		let actual_hash = T::Hashing::hash(code);
2310		ensure!(actual_hash == authorization.code_hash, Error::<T>::Unauthorized);
2311		Ok(authorization)
2312	}
2313
2314	/// Reclaim the weight for the extrinsic given info and post info.
2315	///
2316	/// This function will check the already reclaimed weight, and reclaim more if the
2317	/// difference between pre dispatch and post dispatch weight is higher.
2318	pub fn reclaim_weight(
2319		info: &DispatchInfoOf<T::RuntimeCall>,
2320		post_info: &PostDispatchInfoOf<T::RuntimeCall>,
2321	) -> Result<(), TransactionValidityError>
2322	where
2323		T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
2324	{
2325		let already_reclaimed = crate::ExtrinsicWeightReclaimed::<T>::get();
2326		let unspent = post_info.calc_unspent(info);
2327		let accurate_reclaim = already_reclaimed.max(unspent);
2328		// Saturation never happens, we took the maximum above.
2329		let to_reclaim_more = accurate_reclaim.saturating_sub(already_reclaimed);
2330		if to_reclaim_more != Weight::zero() {
2331			crate::BlockWeight::<T>::mutate(|current_weight| {
2332				current_weight.reduce(to_reclaim_more, info.class);
2333			});
2334			crate::ExtrinsicWeightReclaimed::<T>::put(accurate_reclaim);
2335		}
2336
2337		Ok(())
2338	}
2339}
2340
2341/// Returns a 32 byte datum which is guaranteed to be universally unique. `entropy` is provided
2342/// as a facility to reduce the potential for precalculating results.
2343pub fn unique(entropy: impl Encode) -> [u8; 32] {
2344	let mut last = [0u8; 32];
2345	sp_io::storage::read(well_known_keys::INTRABLOCK_ENTROPY, &mut last[..], 0);
2346	let next = (b"frame_system::unique", entropy, last).using_encoded(blake2_256);
2347	sp_io::storage::set(well_known_keys::INTRABLOCK_ENTROPY, &next);
2348	next
2349}
2350
2351/// Event handler which registers a provider when created.
2352pub struct Provider<T>(PhantomData<T>);
2353impl<T: Config> HandleLifetime<T::AccountId> for Provider<T> {
2354	fn created(t: &T::AccountId) -> Result<(), DispatchError> {
2355		Pallet::<T>::inc_providers(t);
2356		Ok(())
2357	}
2358	fn killed(t: &T::AccountId) -> Result<(), DispatchError> {
2359		Pallet::<T>::dec_providers(t).map(|_| ())
2360	}
2361}
2362
2363/// Event handler which registers a self-sufficient when created.
2364pub struct SelfSufficient<T>(PhantomData<T>);
2365impl<T: Config> HandleLifetime<T::AccountId> for SelfSufficient<T> {
2366	fn created(t: &T::AccountId) -> Result<(), DispatchError> {
2367		Pallet::<T>::inc_sufficients(t);
2368		Ok(())
2369	}
2370	fn killed(t: &T::AccountId) -> Result<(), DispatchError> {
2371		Pallet::<T>::dec_sufficients(t);
2372		Ok(())
2373	}
2374}
2375
2376/// Event handler which registers a consumer when created.
2377pub struct Consumer<T>(PhantomData<T>);
2378impl<T: Config> HandleLifetime<T::AccountId> for Consumer<T> {
2379	fn created(t: &T::AccountId) -> Result<(), DispatchError> {
2380		Pallet::<T>::inc_consumers(t)
2381	}
2382	fn killed(t: &T::AccountId) -> Result<(), DispatchError> {
2383		Pallet::<T>::dec_consumers(t);
2384		Ok(())
2385	}
2386}
2387
2388impl<T: Config> BlockNumberProvider for Pallet<T> {
2389	type BlockNumber = BlockNumberFor<T>;
2390
2391	fn current_block_number() -> Self::BlockNumber {
2392		Pallet::<T>::block_number()
2393	}
2394
2395	#[cfg(feature = "runtime-benchmarks")]
2396	fn set_block_number(n: BlockNumberFor<T>) {
2397		Self::set_block_number(n)
2398	}
2399}
2400
2401/// Implement StoredMap for a simple single-item, provide-when-not-default system. This works fine
2402/// for storing a single item which allows the account to continue existing as long as it's not
2403/// empty/default.
2404///
2405/// Anything more complex will need more sophisticated logic.
2406impl<T: Config> StoredMap<T::AccountId, T::AccountData> for Pallet<T> {
2407	fn get(k: &T::AccountId) -> T::AccountData {
2408		Account::<T>::get(k).data
2409	}
2410
2411	fn try_mutate_exists<R, E: From<DispatchError>>(
2412		k: &T::AccountId,
2413		f: impl FnOnce(&mut Option<T::AccountData>) -> Result<R, E>,
2414	) -> Result<R, E> {
2415		let account = Account::<T>::get(k);
2416		let is_default = account.data == T::AccountData::default();
2417		let mut some_data = if is_default { None } else { Some(account.data) };
2418		let result = f(&mut some_data)?;
2419		if Self::providers(k) > 0 || Self::sufficients(k) > 0 {
2420			Account::<T>::mutate(k, |a| a.data = some_data.unwrap_or_default());
2421		} else {
2422			Account::<T>::remove(k)
2423		}
2424		Ok(result)
2425	}
2426}
2427
2428/// Split an `option` into two constituent options, as defined by a `splitter` function.
2429pub fn split_inner<T, R, S>(
2430	option: Option<T>,
2431	splitter: impl FnOnce(T) -> (R, S),
2432) -> (Option<R>, Option<S>) {
2433	match option {
2434		Some(inner) => {
2435			let (r, s) = splitter(inner);
2436			(Some(r), Some(s))
2437		},
2438		None => (None, None),
2439	}
2440}
2441
2442pub struct ChainContext<T>(PhantomData<T>);
2443impl<T> Default for ChainContext<T> {
2444	fn default() -> Self {
2445		ChainContext(PhantomData)
2446	}
2447}
2448
2449impl<T: Config> Lookup for ChainContext<T> {
2450	type Source = <T::Lookup as StaticLookup>::Source;
2451	type Target = <T::Lookup as StaticLookup>::Target;
2452
2453	fn lookup(&self, s: Self::Source) -> Result<Self::Target, LookupError> {
2454		<T::Lookup as StaticLookup>::lookup(s)
2455	}
2456}
2457
2458/// Hooks for the [`Pallet::run_to_block_with`] function.
2459#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
2460pub struct RunToBlockHooks<'a, T>
2461where
2462	T: 'a + Config,
2463{
2464	before_initialize: Box<dyn 'a + FnMut(BlockNumberFor<T>)>,
2465	after_initialize: Box<dyn 'a + FnMut(BlockNumberFor<T>)>,
2466	before_finalize: Box<dyn 'a + FnMut(BlockNumberFor<T>)>,
2467	after_finalize: Box<dyn 'a + FnMut(BlockNumberFor<T>)>,
2468}
2469
2470#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
2471impl<'a, T> RunToBlockHooks<'a, T>
2472where
2473	T: 'a + Config,
2474{
2475	/// Set the hook function logic before the initialization of the block.
2476	pub fn before_initialize<F>(mut self, f: F) -> Self
2477	where
2478		F: 'a + FnMut(BlockNumberFor<T>),
2479	{
2480		self.before_initialize = Box::new(f);
2481		self
2482	}
2483	/// Set the hook function logic after the initialization of the block.
2484	pub fn after_initialize<F>(mut self, f: F) -> Self
2485	where
2486		F: 'a + FnMut(BlockNumberFor<T>),
2487	{
2488		self.after_initialize = Box::new(f);
2489		self
2490	}
2491	/// Set the hook function logic before the finalization of the block.
2492	pub fn before_finalize<F>(mut self, f: F) -> Self
2493	where
2494		F: 'a + FnMut(BlockNumberFor<T>),
2495	{
2496		self.before_finalize = Box::new(f);
2497		self
2498	}
2499	/// Set the hook function logic after the finalization of the block.
2500	pub fn after_finalize<F>(mut self, f: F) -> Self
2501	where
2502		F: 'a + FnMut(BlockNumberFor<T>),
2503	{
2504		self.after_finalize = Box::new(f);
2505		self
2506	}
2507}
2508
2509#[cfg(any(feature = "std", feature = "runtime-benchmarks", test))]
2510impl<'a, T> Default for RunToBlockHooks<'a, T>
2511where
2512	T: Config,
2513{
2514	fn default() -> Self {
2515		Self {
2516			before_initialize: Box::new(|_| {}),
2517			after_initialize: Box::new(|_| {}),
2518			before_finalize: Box::new(|_| {}),
2519			after_finalize: Box::new(|_| {}),
2520		}
2521	}
2522}
2523
2524/// Prelude to be used alongside pallet macro, for ease of use.
2525pub mod pallet_prelude {
2526	pub use crate::{ensure_none, ensure_root, ensure_signed, ensure_signed_or_root};
2527
2528	/// Type alias for the `Origin` associated type of system config.
2529	pub type OriginFor<T> = <T as crate::Config>::RuntimeOrigin;
2530
2531	/// Type alias for the `Header`.
2532	pub type HeaderFor<T> =
2533		<<T as crate::Config>::Block as sp_runtime::traits::HeaderProvider>::HeaderT;
2534
2535	/// Type alias for the `BlockNumber` associated type of system config.
2536	pub type BlockNumberFor<T> = <HeaderFor<T> as sp_runtime::traits::Header>::Number;
2537
2538	/// Type alias for the `Extrinsic` associated type of system config.
2539	pub type ExtrinsicFor<T> =
2540		<<T as crate::Config>::Block as sp_runtime::traits::Block>::Extrinsic;
2541
2542	/// Type alias for the `RuntimeCall` associated type of system config.
2543	pub type RuntimeCallFor<T> = <T as crate::Config>::RuntimeCall;
2544}