Newer
Older
new_test_ext().execute_with(|| {
assert_eq!(Map::iter().collect::<Vec<_>>().sorted(), vec![(15, 42)]);
// insert / remove
let key = 17u32;
Map::insert(key, 4u64);
assert_eq!(Map::iter().collect::<Vec<_>>().sorted(), vec![(15, 42), (key, 4)]);
assert_eq!(Map::take(&15), 42u64);
assert_eq!(Map::take(&key), 4u64);
assert_eq!(Map::iter().collect::<Vec<_>>().sorted(), vec![]);
// Add couple of more elements
Map::insert(key, 42u64);
assert_eq!(Map::iter().collect::<Vec<_>>().sorted(), vec![(key, 42)]);
assert_eq!(Map::iter().collect::<Vec<_>>().sorted(), vec![(key, 42), (key + 1, 43)]);
// mutate
let key = key + 2;
Map::mutate(&key, |val| {
*val = 15;
});
assert_eq!(
Map::iter().collect::<Vec<_>>().sorted(),
vec![(key - 2, 42), (key - 1, 43), (key, 15)]
);
Map::mutate(&key, |val| {
*val = 17;
});
assert_eq!(
Map::iter().collect::<Vec<_>>().sorted(),
vec![(key - 2, 42), (key - 1, 43), (key, 17)]
);
// remove first
Map::remove(&key);
assert_eq!(
Map::iter().collect::<Vec<_>>().sorted(),
vec![(key - 2, 42), (key - 1, 43)]
);
// remove last from the list
Map::remove(&(key - 2));
assert_eq!(Map::iter().collect::<Vec<_>>().sorted(), vec![(key - 1, 43)]);
// remove the last element
Map::remove(&(key - 1));
assert_eq!(Map::iter().collect::<Vec<_>>().sorted(), vec![]);
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
#[test]
fn double_map_basic_insert_remove_remove_prefix_with_commit_should_work() {
let key1 = 17u32;
let key2 = 18u32;
type DoubleMap = DataDM;
let mut e = new_test_ext();
e.execute_with(|| {
// initialized during genesis
assert_eq!(DoubleMap::get(&15u32, &16u32), 42u64);
// get / insert / take
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
DoubleMap::insert(&key1, &key2, &4u64);
assert_eq!(DoubleMap::get(&key1, &key2), 4u64);
assert_eq!(DoubleMap::take(&key1, &key2), 4u64);
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
// mutate
DoubleMap::mutate(&key1, &key2, |val| *val = 15);
assert_eq!(DoubleMap::get(&key1, &key2), 15u64);
// remove
DoubleMap::remove(&key1, &key2);
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
// remove prefix
DoubleMap::insert(&key1, &key2, &4u64);
DoubleMap::insert(&key1, &(key2 + 1), &4u64);
DoubleMap::insert(&(key1 + 1), &key2, &4u64);
DoubleMap::insert(&(key1 + 1), &(key2 + 1), &4u64);
});
e.commit_all().unwrap();
e.execute_with(|| {
assert!(matches!(
DoubleMap::clear_prefix(&key1, u32::max_value(), None),
MultiRemovalResults { maybe_cursor: None, backend: 2, unique: 2, loops: 2 }
));
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
assert_eq!(DoubleMap::get(&key1, &(key2 + 1)), 0u64);
assert_eq!(DoubleMap::get(&(key1 + 1), &key2), 4u64);
assert_eq!(DoubleMap::get(&(key1 + 1), &(key2 + 1)), 4u64);
});
}
#[test]
fn double_map_basic_insert_remove_remove_prefix_should_work() {
new_test_ext().execute_with(|| {
let key1 = 17u32;
let key2 = 18u32;
type DoubleMap = DataDM;
assert_eq!(DoubleMap::get(&15u32, &16u32), 42u64);
// get / insert / take
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
DoubleMap::insert(&key1, &key2, &4u64);
assert_eq!(DoubleMap::get(&key1, &key2), 4u64);
assert_eq!(DoubleMap::take(&key1, &key2), 4u64);
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
DoubleMap::mutate(&key1, &key2, |val| *val = 15);
assert_eq!(DoubleMap::get(&key1, &key2), 15u64);
DoubleMap::remove(&key1, &key2);
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
DoubleMap::insert(&key1, &key2, &4u64);
DoubleMap::insert(&key1, &(key2 + 1), &4u64);
DoubleMap::insert(&(key1 + 1), &key2, &4u64);
DoubleMap::insert(&(key1 + 1), &(key2 + 1), &4u64);
// all in overlay
assert!(matches!(
DoubleMap::clear_prefix(&key1, u32::max_value(), None),
MultiRemovalResults { maybe_cursor: None, backend: 0, unique: 0, loops: 0 }
));
// Note this is the incorrect answer (for now), since we are using v2 of
// `clear_prefix`.
// When we switch to v3, then this will become:
// MultiRemovalResults:: { maybe_cursor: None, backend: 0, unique: 2, loops: 2 },
DoubleMap::clear_prefix(&key1, u32::max_value(), None),
MultiRemovalResults { maybe_cursor: None, backend: 0, unique: 0, loops: 0 }
assert_eq!(DoubleMap::get(&key1, &key2), 0u64);
assert_eq!(DoubleMap::get(&key1, &(key2 + 1)), 0u64);
assert_eq!(DoubleMap::get(&(key1 + 1), &key2), 4u64);
assert_eq!(DoubleMap::get(&(key1 + 1), &(key2 + 1)), 4u64);
});
}
#[test]
fn double_map_append_should_work() {
new_test_ext().execute_with(|| {
type DoubleMap = AppendableDM<Test>;
let key1 = 17u32;
let key2 = 18u32;
DoubleMap::insert(&key1, &key2, &vec![1]);
DoubleMap::append(&key1, &key2, 2);
assert_eq!(DoubleMap::get(&key1, &key2), &[1, 2]);
#[test]
fn double_map_mutate_exists_should_work() {
new_test_ext().execute_with(|| {
type DoubleMap = DataDM;
let (key1, key2) = (11, 13);
// mutated
DoubleMap::mutate_exists(key1, key2, |v| *v = Some(1));
assert_eq!(DoubleMap::get(&key1, key2), 1);
// removed if mutated to `None`
DoubleMap::mutate_exists(key1, key2, |v| *v = None);
assert!(!DoubleMap::contains_key(&key1, key2));
});
}
#[test]
fn double_map_try_mutate_exists_should_work() {
new_test_ext().execute_with(|| {
type DoubleMap = DataDM;
type TestResult = result::Result<(), &'static str>;
let (key1, key2) = (11, 13);
// mutated if `Ok`
assert_ok!(DoubleMap::try_mutate_exists(key1, key2, |v| -> TestResult {
*v = Some(1);
Ok(())
}));
assert_eq!(DoubleMap::get(&key1, key2), 1);
// no-op if `Err`
assert_noop!(
DoubleMap::try_mutate_exists(key1, key2, |v| -> TestResult {
*v = Some(2);
Err("nah")
}),
"nah"
);
// removed if mutated to`None`
assert_ok!(DoubleMap::try_mutate_exists(key1, key2, |v| -> TestResult {
*v = None;
Ok(())
}));
assert!(!DoubleMap::contains_key(&key1, key2));
});
fn expected_metadata() -> PalletStorageMetadata {
PalletStorageMetadata {
prefix: "Test",
entries: vec![
StorageEntryMetadata {
name: "Data",
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::Map {
hashers: vec![StorageHasher::Twox64Concat],
key: scale_info::meta_type::<u32>(),
value: scale_info::meta_type::<u64>(),
},
default: vec![0, 0, 0, 0, 0, 0, 0, 0],
docs: vec![],
StorageEntryMetadata {
name: "OptionLinkedMap",
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Map {
hashers: vec![StorageHasher::Blake2_128Concat],
key: scale_info::meta_type::<u32>(),
value: scale_info::meta_type::<u32>(),
},
default: vec![0],
docs: vec![],
StorageEntryMetadata {
name: "GenericData",
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::Map {
hashers: vec![StorageHasher::Identity],
key: scale_info::meta_type::<u32>(),
value: scale_info::meta_type::<u32>(),
},
default: vec![0, 0, 0, 0],
docs: vec![],
StorageEntryMetadata {
name: "GenericData2",
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Map {
hashers: vec![StorageHasher::Blake2_128Concat],
key: scale_info::meta_type::<u32>(),
value: scale_info::meta_type::<u32>(),
},
default: vec![0],
docs: vec![],
StorageEntryMetadata {
name: "DataDM",
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::Map {
hashers: vec![StorageHasher::Twox64Concat, StorageHasher::Blake2_128Concat],
key: scale_info::meta_type::<(u32, u32)>(),
value: scale_info::meta_type::<u64>(),
},
default: vec![0, 0, 0, 0, 0, 0, 0, 0],
docs: vec![],
StorageEntryMetadata {
name: "GenericDataDM",
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::Map {
hashers: vec![StorageHasher::Blake2_128Concat, StorageHasher::Identity],
key: scale_info::meta_type::<(u32, u32)>(),
value: scale_info::meta_type::<u32>(),
},
default: vec![0, 0, 0, 0],
docs: vec![],
StorageEntryMetadata {
name: "GenericData2DM",
modifier: StorageEntryModifier::Optional,
ty: StorageEntryType::Map {
hashers: vec![StorageHasher::Blake2_128Concat, StorageHasher::Twox64Concat],
key: scale_info::meta_type::<(u32, u32)>(),
value: scale_info::meta_type::<u32>(),
},
default: vec![0],
docs: vec![],
StorageEntryMetadata {
name: "AppendableDM",
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::Map {
hashers: vec![
StorageHasher::Blake2_128Concat,
StorageHasher::Blake2_128Concat,
],
key: scale_info::meta_type::<(u32, u32)>(),
value: scale_info::meta_type::<Vec<u32>>(),
},
default: vec![0],
docs: vec![],
#[test]
fn store_metadata() {
let metadata = Module::<Test>::storage_metadata();
pretty_assertions::assert_eq!(expected_metadata(), metadata);
parameter_types! {
storage StorageParameter: u64 = 10;
}
#[test]
fn check_storage_parameter_type_works() {
TestExternalities::default().execute_with(|| {
assert_eq!(sp_io::hashing::twox_128(b":StorageParameter:"), StorageParameter::key());
assert_eq!(10, StorageParameter::get());
StorageParameter::set(&300);
assert_eq!(300, StorageParameter::get());
})
}
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub static Members: Vec<u64> = vec![];
pub const Foo: Option<u64> = None;
}
/// Prelude to be used alongside pallet macro, for ease of use.
pub mod pallet_prelude {
#[cfg(feature = "std")]
pub use crate::traits::GenesisBuild;
pub use crate::{
dispatch::{DispatchError, DispatchResult, DispatchResultWithPostInfo, Parameter},
ensure,
inherent::{InherentData, InherentIdentifier, ProvideInherent},
storage,
storage::{
bounded_vec::BoundedVec,
types::{
CountedStorageMap, Key as NMapKey, OptionQuery, StorageDoubleMap, StorageMap,
StorageNMap, StorageValue, ValueQuery,
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
traits::{
ConstU32, EnsureOrigin, Get, GetDefault, GetStorageVersion, Hooks, IsType,
PalletInfoAccess, StorageInfoTrait, StorageVersion, TypedGet,
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
},
weights::{DispatchClass, Pays, Weight},
Blake2_128, Blake2_128Concat, Blake2_256, CloneNoBound, DebugNoBound, EqNoBound, Identity,
PartialEqNoBound, RuntimeDebug, RuntimeDebugNoBound, Twox128, Twox256, Twox64Concat,
pub use codec::{Decode, Encode, MaxEncodedLen};
pub use scale_info::TypeInfo;
pub use sp_runtime::{
traits::{MaybeSerializeDeserialize, Member, ValidateUnsigned},
transaction_validity::{
InvalidTransaction, TransactionLongevity, TransactionPriority, TransactionSource,
TransactionTag, TransactionValidity, TransactionValidityError, UnknownTransaction,
ValidTransaction,
MAX_MODULE_ERROR_ENCODED_SIZE,
pub use sp_std::marker::PhantomData;
}
/// `pallet` attribute macro allows to define a pallet to be used in `construct_runtime!`.
///
/// It is define by a module item:
/// ```ignore
/// #[pallet]
/// pub mod pallet {
/// ...
/// }
/// ```
///
/// Inside the module the macro will parse item with the attribute: `#[pallet::*]`, some
/// attributes are mandatory, some other optional.
/// The attribute are explained with the syntax of non instantiable pallets, to see how pallet
/// with instance work see below example.
///
/// Note various type can be automatically imported using pallet_prelude in frame_support and
/// frame_system:
/// ```ignore
/// #[pallet]
/// pub mod pallet {
/// use frame_support::pallet_prelude::*;
/// use frame_system::pallet_prelude::*;
/// ...
/// }
/// ```
///
/// # Config trait: `#[pallet::config]` mandatory
///
/// The trait defining generics of the pallet.
///
/// Item must be defined as
/// ```ignore
/// #[pallet::config]
/// pub trait Config: frame_system::Config + $optionally_some_other_supertraits
/// $optional_where_clause
/// {
/// ...
/// }
/// ```
/// I.e. a regular trait definition named `Config`, with supertrait `frame_system::Config`,
/// optionally other supertrait and where clause.
///
/// The associated type `Event` is reserved, if defined it must bounds `From<Event>` and
/// `IsType<<Self as frame_system::Config>::Event>`, see `#[pallet::event]` for more
/// information.
///
/// To put `Get` associated type into metadatas, use the attribute `#[pallet::constant]`, e.g.:
/// ```ignore
/// #[pallet::config]
/// pub trait Config: frame_system::Config {
/// #[pallet::constant]
/// type Foo: Get<u32>;
/// }
/// ```
///
/// To bypass the `frame_system::Config` supertrait check, use the attribute
/// `#[pallet::disable_frame_system_supertrait_check]`, e.g.:
/// ```ignore
/// #[pallet::config]
/// #[pallet::disable_frame_system_supertrait_check]
/// pub trait Config: pallet_timestamp::Config {}
/// ```
///
/// ### Macro expansion:
///
/// The macro expand pallet constant metadata with the information given by
/// `#[pallet::constant]`.
///
/// # Pallet struct placeholder: `#[pallet::pallet]` mandatory
///
/// The placeholder struct, on which is implemented pallet informations.
///
/// Item must be defined as followed:
/// ```ignore
/// #[pallet::pallet]
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
/// pub struct Pallet<T>(_);
/// ```
/// I.e. a regular struct definition named `Pallet`, with generic T and no where clause.
///
/// To generate a `Store` trait associating all storages, use the attribute
/// `#[pallet::generate_store($vis trait Store)]`, e.g.:
/// ```ignore
/// #[pallet::pallet]
/// #[pallet::generate_store(pub(super) trait Store)]
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
/// pub struct Pallet<T>(_);
/// More precisely the store trait contains an associated type for each storage. It is
/// implemented for `Pallet` allowing to access the storage from pallet struct.
///
/// Thus when defining a storage named `Foo`, it can later be accessed from `Pallet` using
/// `<Pallet as Store>::Foo`.
///
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
/// To generate the full storage info (used for PoV calculation) use the attribute
/// `#[pallet::generate_storage_info]`, e.g.:
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
/// ```ignore
/// #[pallet::pallet]
/// #[pallet::generate_storage_info]
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
/// pub struct Pallet<T>(_);
/// ```
///
/// This require all storage to implement the trait [`traits::StorageInfoTrait`], thus all keys
/// and value types must bound [`pallet_prelude::MaxEncodedLen`].
/// Some individual storage can opt-out from this constraint by using `#[pallet::unbounded]`,
/// see `#[pallet::storage]` documentation.
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
///
/// As the macro implements [`traits::GetStorageVersion`], the current storage version needs to
/// be communicated to the macro. This can be done by using the `storage_version` attribute:
///
/// ```ignore
/// const STORAGE_VERSION: StorageVersion = StorageVersion::new(5);
///
/// #[pallet::pallet]
/// #[pallet::storage_version(STORAGE_VERSION)]
/// pub struct Pallet<T>(_);
/// ```
///
/// If not present, the current storage version is set to the default value.
///
/// ### Macro expansion:
///
/// The macro add this attribute to the struct definition:
/// ```ignore
/// #[derive(
/// frame_support::CloneNoBound,
/// frame_support::EqNoBound,
/// frame_support::PartialEqNoBound,
/// frame_support::RuntimeDebugNoBound,
/// )]
/// ```
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
/// and replace the type `_` by `PhantomData<T>`.
///
/// It implements on pallet:
/// * [`traits::GetStorageVersion`]
/// * [`traits::OnGenesis`]: contains some logic to write pallet version into storage.
/// * `PalletErrorTypeInfo`: provides the type information for the pallet error, if defined.
/// It declares `type Module` type alias for `Pallet`, used by [`construct_runtime`].
/// It implements [`traits::PalletInfoAccess`] on `Pallet` to ease access to pallet
/// informations given by [`frame_support::traits::PalletInfo`].
/// (The implementation uses the associated type `frame_system::Config::PalletInfo`).
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
///
/// It implements [`traits::StorageInfoTrait`] on `Pallet` which give information about all
/// storages.
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
///
/// If the attribute generate_store is set then the macro creates the trait `Store` and
/// implements it on `Pallet`.
![thiolliere thiolliere's avatar](/assets/no_avatar-849f9c04a3a0d0cea2424ae97b27447dc64a7dbfae83c036c45b403392f0e8ba.png)
thiolliere
committed
///
/// If the attribute set_storage_max_encoded_len is set then the macro call
/// [`traits::StorageInfoTrait`] for each storage in the implementation of
/// [`traits::StorageInfoTrait`] for the pallet.
/// Otherwise it implements [`traits::StorageInfoTrait`] for the pallet using the
/// [`traits::PartialStorageInfoTrait`] implementation of storages.
/// # Hooks: `#[pallet::hooks]` optional
///
/// Implementation of `Hooks` on `Pallet` allowing to define some specific pallet logic.
///
/// Item must be defined as
/// ```ignore
/// #[pallet::hooks]
/// impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> $optional_where_clause {
/// }
/// ```
/// I.e. a regular trait implementation with generic bound: `T: Config`, for the trait
/// `Hooks<BlockNumberFor<T>>` (they are defined in preludes), for the type `Pallet<T>`
/// and with an optional where clause.
///
/// If no `#[pallet::hooks]` exists, then a default implementation corresponding to the
/// following code is automatically generated:
/// ```ignore
/// #[pallet::hooks]
/// impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
/// ```
///
/// ### Macro expansion:
///
/// The macro implements the traits `OnInitialize`, `OnIdle`, `OnFinalize`, `OnRuntimeUpgrade`,
/// `OffchainWorker`, `IntegrityTest` using `Hooks` implementation.
///
/// NOTE: OnRuntimeUpgrade is implemented with `Hooks::on_runtime_upgrade` and some additional
/// logic. E.g. logic to write pallet version into storage.
///
/// NOTE: The macro also adds some tracing logic when implementing the above traits. The
/// following hooks emit traces: `on_initialize`, `on_finalize` and `on_runtime_upgrade`.
/// # Call: `#[pallet::call]` optional
///
/// Implementation of pallet dispatchables.
///
/// Item must be defined as:
/// ```ignore
/// #[pallet::call]
/// impl<T: Config> Pallet<T> {
/// /// $some_doc
/// #[pallet::weight($ExpressionResultingInWeight)]
/// origin: OriginFor<T>,
/// $some_arg: $some_type,
/// // or with compact attribute: #[pallet::compact] $some_arg: $some_type,
/// ...
/// ) -> DispatchResultWithPostInfo { // or `-> DispatchResult`
/// ...
/// }
/// ...
/// }
/// ```
/// I.e. a regular type implementation, with generic `T: Config`, on type `Pallet<T>`, with
/// optional where clause.
///
/// Each dispatchable needs to define a weight with `#[pallet::weight($expr)]` attribute,
/// the first argument must be `origin: OriginFor<T>`, compact encoding for argument can be
/// used using `#[pallet::compact]`, function must return `DispatchResultWithPostInfo` or
/// `DispatchResult`.
/// Each dispatchable may also be annotated with the `#[pallet::call_index($idx)]` attribute,
/// which defines and sets the codec index for the dispatchable function in the `Call` enum.
///
/// All call indexes start from 0, until it encounters a dispatchable function with a defined
/// call index. The dispatchable function that lexically follows the function with a defined
/// call index will have that call index, but incremented by 1, e.g. if there are 3
/// dispatchable functions `fn foo`, `fn bar` and `fn qux` in that order, and only `fn bar` has
/// a call index of 10, then `fn qux` will have an index of 11, instead of 1.
///
/// All arguments must implement `Debug`, `PartialEq`, `Eq`, `Decode`, `Encode`, `Clone`. For
/// ease of use, bound the trait `Member` available in frame_support::pallet_prelude.
/// If no `#[pallet::call]` exists, then a default implementation corresponding to the
/// following code is automatically generated:
/// ```ignore
/// #[pallet::call]
/// impl<T: Config> Pallet<T> {}
/// ```
///
/// **WARNING**: modifying dispatchables, changing their order, removing some must be done with
/// care. Indeed this will change the outer runtime call type (which is an enum with one
/// variant per pallet), this outer runtime call can be stored on-chain (e.g. in
/// pallet-scheduler). Thus migration might be needed. To mitigate against some of this, the
/// `#[pallet::call_index($idx)]` attribute can be used to fix the order of the dispatchable so
/// that the `Call` enum encoding does not change after modification.
///
/// ### Macro expansion
///
/// The macro creates an enum `Call` with one variant per dispatchable. This enum implements:
/// `Clone`, `Eq`, `PartialEq`, `Debug` (with stripped implementation in `not("std")`),
/// `Encode`, `Decode`, `GetDispatchInfo`, `GetCallName`, `UnfilteredDispatchable`.
/// The macro implement the `Callable` trait on `Pallet` and a function `call_functions` which
/// returns the dispatchable metadata.
///
/// # Extra constants: `#[pallet::extra_constants]` optional
///
/// Allow to define some extra constants to put into constant metadata.
///
/// Item must be defined as:
/// ```ignore
/// #[pallet::extra_constants]
/// impl<T: Config> Pallet<T> where $optional_where_clause {
/// /// $some_doc
/// $vis fn $fn_name() -> $some_return_type {
/// ...
/// }
/// ...
/// }
/// ```
/// I.e. a regular rust implement block with some optional where clause and functions with 0
/// args, 0 generics, and some return type.
///
/// ### Macro expansion
///
/// The macro add some extra constant to pallet constant metadata.
///
/// # Error: `#[pallet::error]` optional
///
/// Allow to define an error type to be return from dispatchable on error.
/// This error type informations are put into metadata.
///
/// Item must be defined as:
/// ```ignore
/// #[pallet::error]
/// pub enum Error<T> {
/// /// $some_optional_doc
/// $SomeFieldLessVariant,
/// /// $some_more_optional_doc
/// $SomeVariantWithOneField(FieldType),
/// ...
/// }
/// ```
/// I.e. a regular rust enum named `Error`, with generic `T` and fieldless or multiple-field
/// variants.
///
/// Any field type in the enum variants must implement [`scale_info::TypeInfo`] in order to be
/// properly used in the metadata, and its encoded size should be as small as possible,
/// preferably 1 byte in size in order to reduce storage size. The error enum itself has an
/// absolute maximum encoded size specified by [`MAX_MODULE_ERROR_ENCODED_SIZE`].
///
/// Field types in enum variants must also implement [`PalletError`](traits::PalletError),
/// otherwise the pallet will fail to compile. Rust primitive types have already implemented
/// the [`PalletError`](traits::PalletError) trait along with some commonly used stdlib types
/// such as `Option` and `PhantomData`, and hence in most use cases, a manual implementation is
/// not necessary and is discouraged.
///
/// The generic `T` mustn't bound anything and where clause is not allowed. But bounds and
/// where clause shouldn't be needed for any usecase.
///
/// ### Macro expansion
///
/// The macro implements `Debug` trait and functions `as_u8` using variant position, and
/// `as_str` using variant doc.
///
/// The macro implements `From<Error<T>>` for `&'static str`.
/// The macro implements `From<Error<T>>` for `DispatchError`.
///
/// # Event: `#[pallet::event]` optional
///
/// Allow to define pallet events, pallet events are stored in the block when they deposited
/// (and removed in next block).
///
/// Item is defined as:
/// ```ignore
/// #[pallet::event]
/// #[pallet::generate_deposit($visibility fn deposit_event)] // Optional
/// pub enum Event<$some_generic> $optional_where_clause {
/// /// Some doc
/// $SomeName($SomeType, $YetanotherType, ...),
/// ...
/// }
/// ```
/// I.e. an enum (with named or unnamed fields variant), named Event, with generic: none or `T`
/// or `T: Config`, and optional where clause.
/// Each field must implement `Clone`, `Eq`, `PartialEq`, `Encode`, `Decode`, and `Debug` (on
/// std only).
/// For ease of use, bound the trait `Member` available in frame_support::pallet_prelude.
/// The attribute `#[pallet::generate_deposit($visibility fn deposit_event)]` generate a helper
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
/// function on `Pallet` to deposit event.
///
/// NOTE: For instantiable pallet, event must be generic over T and I.
///
/// ### Macro expansion:
///
/// Macro will add on enum `Event` the attributes:
/// * `#[derive(frame_support::CloneNoBound)]`,
/// * `#[derive(frame_support::EqNoBound)]`,
/// * `#[derive(frame_support::PartialEqNoBound)]`,
/// * `#[derive(codec::Encode)]`,
/// * `#[derive(codec::Decode)]`,
/// * `#[derive(frame_support::RuntimeDebugNoBound)]`
///
/// Macro implements `From<Event<..>>` for ().
///
/// Macro implements metadata function on `Event` returning the `EventMetadata`.
///
/// If `#[pallet::generate_deposit]` then macro implement `fn deposit_event` on `Pallet`.
///
/// # Storage: `#[pallet::storage]` optional
///
/// Allow to define some abstract storage inside runtime storage and also set its metadata.
/// This attribute can be used multiple times.
///
/// Item is defined as:
/// ```ignore
/// #[pallet::storage]
/// #[pallet::getter(fn $getter_name)] // optional
/// $vis type $StorageName<$some_generic> $optional_where_clause
/// = $StorageType<$generic_name = $some_generics, $other_name = $some_other, ...>;
/// ```
/// or with unnamed generic
/// ```ignore
/// #[pallet::storage]
/// #[pallet::getter(fn $getter_name)] // optional
/// $vis type $StorageName<$some_generic> $optional_where_clause
/// = $StorageType<_, $some_generics, ...>;
/// ```
/// I.e. it must be a type alias, with generics: `T` or `T: Config`, aliased type must be one
/// of `StorageValue`, `StorageMap` or `StorageDoubleMap` (defined in frame_support).
/// The generic arguments of the storage type can be given in two manner: named and unnamed.
/// For named generic argument: the name for each argument is the one as define on the storage
/// struct:
/// * [`pallet_prelude::StorageValue`] expect `Value` and optionally `QueryKind` and `OnEmpty`,
/// * [`pallet_prelude::StorageMap`] expect `Hasher`, `Key`, `Value` and optionally `QueryKind`
/// and `OnEmpty`,
/// * [`pallet_prelude::CountedStorageMap`] expect `Hasher`, `Key`, `Value` and optionally
/// `QueryKind` and `OnEmpty`,
/// * [`pallet_prelude::StorageDoubleMap`] expect `Hasher1`, `Key1`, `Hasher2`, `Key2`, `Value`
/// and optionally `QueryKind` and `OnEmpty`.
///
/// For unnamed generic argument: Their first generic must be `_` as it is replaced by the
/// macro and other generic must declared as a normal declaration of type generic in rust.
/// The Prefix generic written by the macro is generated using
/// `PalletInfo::name::<Pallet<..>>()` and the name of the storage type.
/// E.g. if runtime names the pallet "MyExample" then the storage `type Foo<T> = ...` use the
/// prefix: `Twox128(b"MyExample") ++ Twox128(b"Foo")`.
///
/// For the `CountedStorageMap` variant, the Prefix also implements
/// `CountedStorageMapInstance`. It associate a `CounterPrefix`, which is implemented same as
/// above, but the storage prefix is prepend with `"CounterFor"`.
/// E.g. if runtime names the pallet "MyExample" then the storage
/// `type Foo<T> = CountedStorageaMap<...>` will store its counter at the prefix:
/// `Twox128(b"MyExample") ++ Twox128(b"CounterForFoo")`.
///
/// E.g:
/// ```ignore
/// #[pallet::storage]
/// pub(super) type MyStorage<T> = StorageMap<Hasher = Blake2_128Concat, Key = u32, Value = u32>;
/// ```
/// In this case the final prefix used by the map is
/// `Twox128(b"MyExample") ++ Twox128(b"OtherName")`.
///
/// The optional attribute `#[pallet::getter(fn $my_getter_fn_name)]` allows to define a
/// getter function on `Pallet`.
///
/// The optional attribute `#[pallet::storage_prefix = "SomeName"]` allow to define the storage
/// prefix to use, see how `Prefix` generic is implemented above.
///
/// E.g:
/// ```ignore
/// #[pallet::storage]
/// #[pallet::storage_prefix = "foo"]
/// #[pallet::getter(fn my_storage)]
/// pub(super) type MyStorage<T> = StorageMap<Hasher = Blake2_128Concat, Key = u32, Value = u32>;
/// ```
/// or
/// ```ignore
/// #[pallet::storage]
/// #[pallet::getter(fn my_storage)]
/// pub(super) type MyStorage<T> = StorageMap<_, Blake2_128Concat, u32, u32>;
/// ```
///
/// The optional attribute `#[pallet::unbounded]` allows to declare the storage as unbounded.
/// When implementating the storage info (when `#[pallet::generate_storage_info]` is specified
/// on the pallet struct placeholder), the size of the storage will be declared as unbounded.
/// This can be useful for storage which can never go into PoV (Proof of Validity).
///
/// The optional attributes `#[cfg(..)]` allow conditional compilation for the storage.
///
/// E.g:
/// ```ignore
/// #[cfg(feature = "my-feature")]
/// #[pallet::storage]
/// pub(super) type MyStorage<T> = StorageValue<Value = u32>;
/// All the `cfg` attributes are automatically copied to the items generated for the storage,
/// i.e. the getter, storage prefix, and the metadata element etc.
/// NOTE: If the `QueryKind` generic parameter is still generic at this stage or is using some
/// type alias then the generation of the getter might fail. In this case the getter can be
/// implemented manually.
/// NOTE: The generic `Hasher` must implement the [`StorageHasher`] trait (or the type is not
/// usable at all). We use [`StorageHasher::METADATA`] for the metadata of the hasher of the
/// storage item. Thus generic hasher is supported.
///
/// ### Macro expansion
///
/// For each storage item the macro generates a struct named
/// `_GeneratedPrefixForStorage$NameOfStorage`, and implements
/// [`StorageInstance`](traits::StorageInstance) on it using the pallet and storage name. It
/// then uses it as the first generic of the aliased type.
/// For `CountedStorageMap`, `CountedStorageMapInstance` is implemented, and another similar
/// struct is generated.
/// For named generic, the macro will reorder the generics, and remove the names.
/// The macro implements the function `storage_metadata` on `Pallet` implementing the metadata
/// for all storage items based on their kind:
/// * for a storage value, the type of the value is copied into the metadata
/// * for a storage map, the type of the values and the key's type is copied into the metadata
/// * for a storage double map, the type of the values, and the types of key1 and key2 are
/// copied into the metadata.
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
///
/// # Type value: `#[pallet::type_value]` optional
///
/// Helper to define a struct implementing `Get` trait. To ease use of storage types.
/// This attribute can be used multiple time.
///
/// Item is defined as
/// ```ignore
/// #[pallet::type_value]
/// fn $MyDefaultName<$some_generic>() -> $default_type $optional_where_clause { $expr }
/// ```
/// I.e.: a function definition with generics none or `T: Config` and a returned type.
///
/// E.g.:
/// ```ignore
/// #[pallet::type_value]
/// fn MyDefault<T: Config>() -> T::Balance { 3.into() }
/// ```
///
/// NOTE: This attribute is meant to be used alongside `#[pallet::storage]` to defined some
/// specific default value in storage.
///
/// ### Macro expansion
///
/// Macro renames the function to some internal name, generate a struct with the original name
/// of the function and its generic, and implement `Get<$ReturnType>` by calling the user
/// defined function.
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
///
/// # Genesis config: `#[pallet::genesis_config]` optional
///
/// Allow to define the genesis configuration of the pallet.
///
/// Item is defined as either an enum or a struct.
/// It needs to be public and implement trait GenesisBuild with `#[pallet::genesis_build]`.
/// The type generics is constrained to be either none, or `T` or `T: Config`.
///
/// E.g:
/// ```ignore
/// #[pallet::genesis_config]
/// pub struct GenesisConfig<T: Config> {
/// _myfield: BalanceOf<T>,
/// }
/// ```
///
/// ### Macro expansion
///
/// Macro will add the following attribute on it:
/// * `#[cfg(feature = "std")]`
/// * `#[derive(Serialize, Deserialize)]`
/// * `#[serde(rename_all = "camelCase")]`
/// * `#[serde(deny_unknown_fields)]`
/// * `#[serde(bound(serialize = ""))]`
/// * `#[serde(bound(deserialize = ""))]`
///
/// # Genesis build: `#[pallet::genesis_build]` optional
///
/// Allow to define how genesis_configuration is built.
///
/// Item is defined as
/// ```ignore
/// #[pallet::genesis_build]
/// impl<T: Config> GenesisBuild<T> for GenesisConfig<$maybe_generics> {
/// fn build(&self) { $expr }
/// }
/// ```
/// I.e. a rust trait implementation with generic `T: Config`, of trait `GenesisBuild<T>` on
/// type `GenesisConfig` with generics none or `T`.
///
/// E.g.:
/// ```ignore
/// #[pallet::genesis_build]
/// impl<T: Config> GenesisBuild<T> for GenesisConfig {
/// fn build(&self) {}
/// }
/// ```
///
/// ### Macro expansion
///
/// Macro will add the following attribute on it:
/// * `#[cfg(feature = "std")]`
///
/// Macro will implement `sp_runtime::BuildModuleGenesisStorage` using `()` as second generic
/// for non-instantiable pallets.
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
///
/// # Inherent: `#[pallet::inherent]` optional
///
/// Allow the pallet to provide some inherent:
///
/// Item is defined as:
/// ```ignore
/// #[pallet::inherent]
/// impl<T: Config> ProvideInherent for Pallet<T> {
/// // ... regular trait implementation
/// }
/// ```
/// I.e. a trait implementation with bound `T: Config`, of trait `ProvideInherent` for type
/// `Pallet<T>`, and some optional where clause.
///
/// ### Macro expansion
///
/// Macro make currently no use of this information, but it might use this information in the
/// future to give information directly to construct_runtime.
///
/// # Validate unsigned: `#[pallet::validate_unsigned]` optional
///
/// Allow the pallet to validate some unsigned transaction:
///
/// Item is defined as:
/// ```ignore
/// #[pallet::validate_unsigned]
/// impl<T: Config> ValidateUnsigned for Pallet<T> {
/// // ... regular trait implementation
/// }
/// ```
/// I.e. a trait implementation with bound `T: Config`, of trait `ValidateUnsigned` for type
/// `Pallet<T>`, and some optional where clause.
///
/// NOTE: There is also `sp_runtime::traits::SignedExtension` that can be used to add some
/// specific logic for transaction validation.
///
/// ### Macro expansion
///
/// Macro make currently no use of this information, but it might use this information in the
/// future to give information directly to construct_runtime.
///
/// # Origin: `#[pallet::origin]` optional
///
/// Allow to define some origin for the pallet.
///
/// Item must be either a type alias or an enum or a struct. It needs to be public.
///
/// E.g.:
/// ```ignore
/// #[pallet::origin]
/// pub struct Origin<T>(PhantomData<(T)>);
/// ```
///
/// **WARNING**: modifying origin changes the outer runtime origin. This outer runtime origin
/// can be stored on-chain (e.g. in pallet-scheduler), thus any change must be done with care
/// as it might require some migration.
///
/// NOTE: for instantiable pallet, origin must be generic over T and I.
///
/// # General notes on instantiable pallet
///
/// An instantiable pallet is one where Config is generic, i.e. `Config<I>`. This allow runtime
/// to implement multiple instance of the pallet, by using different type for the generic.
/// This is the sole purpose of the generic `I`.