Skip to content
lib.rs 53.6 KiB
Newer Older
// This file is part of Substrate.
Gav Wood's avatar
Gav Wood committed

// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
Gav Wood's avatar
Gav Wood committed

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Gav Wood's avatar
Gav Wood committed

//! The System pallet provides low-level access to core types and cross-cutting utilities.
//! It acts as the base layer for other pallets to interact with the Substrate framework components.
//! ## Overview
//! The System pallet defines the core data types used in a Substrate runtime.
//! It also provides several utility functions (see [`Pallet`]) for other FRAME pallets.
//! In addition, it manages the storage items for extrinsics data, indexes, event records, and
//! digest items, among other things that support the execution of the current block.
//! It also handles low-level tasks like depositing logs, basic set up and take down of
//! temporary storage entries, and access to previous block hashes.
//! ## Interface
//! ### Dispatchable Functions
//! The System pallet does not implement any dispatchable functions.
//! ### Public Functions
//! See the [`Pallet`] struct for details of publicly available functions.
//! The System pallet defines the following extensions:
//!
//!   - [`CheckWeight`]: Checks the weight and length of the block and ensure that it does not
//!     exceed the limits.
yjh's avatar
yjh committed
//!   - [`CheckNonce`]: Checks the nonce of the transaction. Contains a single payload of type
//!     `T::Index`.
//!   - [`CheckEra`]: Checks the era of the transaction. Contains a single payload of type `Era`.
//!   - [`CheckGenesis`]: Checks the provided genesis hash of the transaction. Must be a part of the
//!     signed payload of the transaction.
//!   - [`CheckSpecVersion`]: Checks that the runtime version is the same as the one used to sign
//!     the transaction.
//!   - [`CheckTxVersion`]: Checks that the transaction version is the same as the one used to sign
//!     the transaction.
//!
//! Lookup the runtime aggregator file (e.g. `node/runtime`) to see the full list of signed
//! extensions included in a chain.
Gav Wood's avatar
Gav Wood committed

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "std")]
use serde::Serialize;
use sp_runtime::{
		self, AtLeast32Bit, AtLeast32BitUnsigned, BadOrigin, BlockNumberProvider, Bounded,
		CheckEqual, Dispatchable, Hash, Lookup, LookupError, MaybeDisplay, MaybeMallocSizeOf,
		MaybeSerializeDeserialize, Member, One, Saturating, SimpleBitOps, StaticLookup, Zero,
#[cfg(any(feature = "std", test))]
use sp_std::map;
use sp_std::{fmt::Debug, marker::PhantomData, prelude::*};
use sp_version::RuntimeVersion;
use codec::{Decode, Encode, EncodeLike, FullCodec, MaxEncodedLen};
use frame_support::{
	dispatch::{DispatchResult, DispatchResultWithPostInfo},
	storage,
Gavin Wood's avatar
Gavin Wood committed
	traits::{
Gavin Wood's avatar
Gavin Wood committed
		ConstU32, Contains, EnsureOrigin, Get, HandleLifetime, OnKilledAccount, OnNewAccount,
		OriginTrait, PalletInfo, SortedMembers, StoredMap,
Gavin Wood's avatar
Gavin Wood committed
	},
	weights::{
		extract_actual_weight, DispatchClass, DispatchInfo, PerDispatchClass, RuntimeDbWeight,
		Weight,
use scale_info::TypeInfo;
use sp_core::storage::well_known_keys;
Gav Wood's avatar
Gav Wood committed

#[cfg(feature = "std")]
use frame_support::traits::GenesisBuild;
Gav Wood's avatar
Gav Wood committed
#[cfg(any(feature = "std", test))]
use sp_io::TestExternalities;
pub mod offchain;
#[cfg(feature = "std")]
pub mod mocking;
#[cfg(test)]
mod tests;
pub mod weights;
	check_genesis::CheckGenesis, check_mortality::CheckMortality,
	check_non_zero_sender::CheckNonZeroSender, check_nonce::CheckNonce,
	check_spec_version::CheckSpecVersion, check_tx_version::CheckTxVersion,
	check_weight::CheckWeight,
};
// Backward compatible re-export.
pub use extensions::check_mortality::CheckMortality as CheckEra;
pub use weights::WeightInfo;
/// Compute the trie root of a list of extrinsics.
///
/// The merkle proof is using the same trie as runtime state with
/// `state_version` 0.
pub fn extrinsics_root<H: Hash, E: codec::Encode>(extrinsics: &[E]) -> H::Output {
	extrinsics_data_root::<H>(extrinsics.iter().map(codec::Encode::encode).collect())
/// Compute the trie root of a list of extrinsics.
///
/// The merkle proof is using the same trie as runtime state with
/// `state_version` 0.
pub fn extrinsics_data_root<H: Hash>(xts: Vec<Vec<u8>>) -> H::Output {
	H::ordered_trie_root(xts, sp_core::storage::StateVersion::V0)
/// An object to track the currently used extrinsic weight in a block.
pub type ConsumedWeight = PerDispatchClass<Weight>;

/// Do something when we should be setting the code.
pub trait SetCode<T: Config> {
	/// Set the code to the given blob.
	fn set_code(code: Vec<u8>) -> DispatchResult;
impl<T: Config> SetCode<T> for () {
	fn set_code(code: Vec<u8>) -> DispatchResult {
		<Pallet<T>>::update_code_in_storage(&code)?;
Gavin Wood's avatar
Gavin Wood committed
/// Numeric limits over the ability to add a consumer ref using `inc_consumers`.
pub trait ConsumerLimits {
	/// The number of consumers over which `inc_consumers` will cease to work.
	fn max_consumers() -> RefCount;
	/// The maximum number of additional consumers expected to be over be added at once using
	/// `inc_consumers_without_limit`.
	///
	/// Note: This is not enforced and it's up to the chain's author to ensure this reflects the
	/// actual situation.
	fn max_overflow() -> RefCount;
}

impl<const Z: u32> ConsumerLimits for ConstU32<Z> {
	fn max_consumers() -> RefCount {
		Z
	}
	fn max_overflow() -> RefCount {
		Z
	}
}

impl<MaxNormal: Get<u32>, MaxOverflow: Get<u32>> ConsumerLimits for (MaxNormal, MaxOverflow) {
	fn max_consumers() -> RefCount {
		MaxNormal::get()
	}
	fn max_overflow() -> RefCount {
		MaxOverflow::get()
	}
}

#[frame_support::pallet]
pub mod pallet {
	use crate::{self as frame_system, pallet_prelude::*, *};
	use frame_support::pallet_prelude::*;

	/// System configuration trait. Implemented by runtime.
	#[pallet::config]
	#[pallet::disable_frame_system_supertrait_check]
Loading full blame...