lib.rs 77.1 KiB
Newer Older
Gav's avatar
Gav committed

Bastian Köcher's avatar
Bastian Köcher committed
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
Gav's avatar
Gav 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's avatar
Gav committed

//! Support code for the runtime.

#![cfg_attr(not(feature = "std"), no_std)]
/// Export ourself as `frame_support` to make tests happy.
extern crate self as frame_support;
Bastian Köcher's avatar
Bastian Köcher committed
#[doc(hidden)]
pub use sp_tracing;
#[doc(hidden)]
pub use frame_metadata as metadata;
#[doc(hidden)]
pub use log;
#[cfg(feature = "std")]
#[doc(hidden)]
pub use once_cell;
#[doc(hidden)]
pub use paste;
#[cfg(feature = "std")]
pub use serde;
pub use sp_core::Void;
#[doc(hidden)]
pub use sp_io::{self, storage::root as storage_root};
#[doc(hidden)]
pub use sp_runtime::RuntimeDebug;
#[cfg(feature = "std")]
pub use sp_state_machine::BasicExternalities;
#[macro_use]
Gav Wood's avatar
Gav Wood committed
pub mod dispatch;
mod hash;
pub mod error;
pub mod instances;
pub mod traits;
#[doc(hidden)]
pub mod unsigned {
	#[doc(hidden)]
	pub use crate::sp_runtime::traits::ValidateUnsigned;
	#[doc(hidden)]
	pub use crate::sp_runtime::transaction_validity::{
		TransactionSource, TransactionValidity, TransactionValidityError, UnknownTransaction,
pub use self::{
	dispatch::{Callable, Parameter},
	hash::{
		Blake2_128, Blake2_128Concat, Blake2_256, Hashable, Identity, ReversibleStorageHasher,
		StorageHasher, Twox128, Twox256, Twox64Concat,
	},
	storage::{
		bounded_vec::{BoundedSlice, BoundedVec},
		migration,
		weak_bounded_vec::WeakBoundedVec,
		IterableStorageDoubleMap, IterableStorageMap, IterableStorageNMap, StorageDoubleMap,
		StorageMap, StorageNMap, StoragePrefixedMap, StorageValue,
	},
pub use sp_runtime::{self, print, traits::Printable, ConsensusEngineId};
use codec::{Decode, Encode};
use sp_runtime::TypeId;

/// A unified log target for support operations.
pub const LOG_TARGET: &'static str = "runtime::frame-support";

/// A type that cannot be instantiated.
#[derive(Debug, PartialEq, Eq, Clone)]
/// A pallet identifier. These are per pallet and should be stored in a registry somewhere.
#[derive(Clone, Copy, Eq, PartialEq, Encode, Decode)]
pub struct PalletId(pub [u8; 8]);

impl TypeId for PalletId {
	const TYPE_ID: [u8; 4] = *b"modl";
}

thiolliere's avatar
thiolliere committed
/// Generate a new type alias for [`storage::types::StorageValue`],
/// [`storage::types::StorageMap`], [`storage::types::StorageDoubleMap`]
/// and [`storage::types::StorageNMap`].
///
/// Useful for creating a *storage-like* struct for test and migrations.
///
/// # use frame_support::generate_storage_alias;
/// use frame_support::codec;
/// use frame_support::Twox64Concat;
/// // generate a storage value with type u32.
/// generate_storage_alias!(Prefix, StorageName => Value<u32>);
///
/// // generate a double map from `(u32, u32)` (with hashers `Twox64Concat` for each key)
/// // to `Vec<u8>`
/// generate_storage_alias!(
/// 	OtherPrefix, OtherStorageName => DoubleMap<
/// 		(u32, Twox64Concat),
/// 		(u32, Twox64Concat),
/// );
///
/// // generate a map from `Config::AccountId` (with hasher `Twox64Concat`) to `Vec<u8>`
/// trait Config { type AccountId: codec::FullCodec; }
/// generate_storage_alias!(
/// 	Prefix, GenericStorage<T: Config> => Map<(T::AccountId, Twox64Concat), Vec<u8>>
#[macro_export]
macro_rules! generate_storage_alias {
	// without generic for $name.
	($pallet:ident, $name:ident => Map<($key:ty, $hasher:ty), $value:ty>) => {
		$crate::paste::paste! {
			$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
			type $name = $crate::storage::types::StorageMap<
				[<$name Instance>],
				$hasher,
				$key,
				$value,
			>;
		}
	};
	($pallet:ident, $name:ident => DoubleMap<($key1:ty, $hasher1:ty), ($key2:ty, $hasher2:ty), $value:ty>) => {
		$crate::paste::paste! {
			$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
			type $name = $crate::storage::types::StorageDoubleMap<
				[<$name Instance>],
				$hasher1,
				$key1,
				$hasher2,
				$key2,
				$value,
			>;
		}
	};
	($pallet:ident, $name:ident => NMap<Key<$(($key:ty, $hasher:ty)),+>, $value:ty>) => {
		$crate::paste::paste! {
			$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
			type $name = $crate::storage::types::StorageNMap<
				[<$name Instance>],
				(
					$( $crate::storage::types::Key<$hasher, $key>, )+
				),
				$value,
			>;
		}
	};
	($pallet:ident, $name:ident => Value<$value:ty>) => {
		$crate::paste::paste! {
			$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
			type $name = $crate::storage::types::StorageValue<
				[<$name Instance>],
				$value,
			>;
		}
	};
	// with generic for $name.
	($pallet:ident, $name:ident<$t:ident : $bounds:tt> => Map<($key:ty, $hasher:ty), $value:ty>) => {
		$crate::paste::paste! {
			$crate::generate_storage_alias!(@GENERATE_INSTANCE_STRUCT $pallet, $name);
			#[allow(type_alias_bounds)]
			type $name<$t : $bounds> = $crate::storage::types::StorageMap<
				[<$name Instance>],
				$key,
				$hasher,
				$value,
			>;
		}
	};
Loading full blame...