referrerpolicy=no-referrer-when-downgrade

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