referrerpolicy=no-referrer-when-downgrade

frame_support_test/
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//! Minimal pallet without `frame_system::Config`-super trait.
19
20// Make sure we fail compilation on warnings
21#![warn(missing_docs)]
22#![deny(warnings)]
23
24pub use frame_support::dispatch::RawOrigin;
25use frame_system::pallet_prelude::BlockNumberFor;
26
27pub use self::pallet::*;
28
29#[frame_support::pallet(dev_mode)]
30pub mod pallet {
31	use super::*;
32	use crate::{self as frame_system, pallet_prelude::*};
33	use frame_support::pallet_prelude::*;
34
35	#[pallet::pallet]
36	pub struct Pallet<T>(_);
37
38	/// The configuration trait.
39	#[pallet::config(frame_system_config)]
40	#[pallet::disable_frame_system_supertrait_check]
41	pub trait Config: 'static + Eq + Clone {
42		/// The block number type.
43		type BlockNumber: Parameter + Member + Default + MaybeSerializeDeserialize + MaxEncodedLen;
44		/// The account type.
45		type AccountId: Parameter + Member + MaxEncodedLen;
46		/// The basic call filter to use in Origin.
47		type BaseCallFilter: frame_support::traits::Contains<Self::RuntimeCall>;
48		/// The runtime origin type.
49		type RuntimeOrigin: Into<Result<RawOrigin<Self::AccountId>, Self::RuntimeOrigin>>
50			+ From<RawOrigin<Self::AccountId>>;
51		/// The runtime call type.
52		type RuntimeCall;
53		/// Contains an aggregation of all tasks in this runtime.
54		type RuntimeTask;
55		/// The runtime event type.
56		type RuntimeEvent: Parameter
57			+ Member
58			+ IsType<<Self as frame_system::Config>::RuntimeEvent>
59			+ From<Event<Self>>;
60		/// The information about the pallet setup in the runtime.
61		type PalletInfo: frame_support::traits::PalletInfo;
62		/// The db weights.
63		type DbWeight: Get<frame_support::weights::RuntimeDbWeight>;
64	}
65
66	#[pallet::call]
67	impl<T: Config> Pallet<T> {
68		/// A noop call.
69		pub fn noop(_origin: OriginFor<T>) -> DispatchResult {
70			Ok(())
71		}
72	}
73
74	impl<T: Config> Pallet<T> {
75		/// A empty method.
76		pub fn deposit_event(_event: impl Into<T::RuntimeEvent>) {}
77	}
78
79	/// The origin type.
80	#[pallet::origin]
81	pub type Origin<T> = RawOrigin<<T as Config>::AccountId>;
82
83	/// The error type.
84	#[pallet::error]
85	pub enum Error<T> {
86		/// Test error documentation
87		TestError,
88		/// Error documentation
89		/// with multiple lines
90		AnotherError,
91		/// Required by construct_runtime
92		CallFiltered,
93	}
94
95	/// The event type.
96	#[pallet::event]
97	pub enum Event<T: Config> {
98		/// The extrinsic is successful
99		ExtrinsicSuccess,
100		/// The extrinsic is failed
101		ExtrinsicFailed,
102		/// The ignored error
103		Ignore(<T as Config>::BlockNumber),
104	}
105}
106
107/// Ensure that the origin `o` represents the root. Returns `Ok` or an `Err` otherwise.
108pub fn ensure_root<OuterOrigin, AccountId>(o: OuterOrigin) -> Result<(), &'static str>
109where
110	OuterOrigin: Into<Result<RawOrigin<AccountId>, OuterOrigin>>,
111{
112	o.into().map(|_| ()).map_err(|_| "bad origin: expected to be a root origin")
113}
114
115/// Same semantic as [`frame_system`].
116// Note: we cannot use [`frame_system`] here since the pallet does not depend on
117// [`frame_system::Config`].
118pub mod pallet_prelude {
119	pub use crate::ensure_root;
120
121	/// Type alias for the `Origin` associated type of system config.
122	pub type OriginFor<T> = <T as crate::Config>::RuntimeOrigin;
123
124	/// Type alias for the `BlockNumber` associated type of system config.
125	pub type BlockNumberFor<T> = <T as super::Config>::BlockNumber;
126}
127
128/// Provides an implementation of [`frame_support::traits::Randomness`] that should only be used in
129/// tests!
130pub struct TestRandomness<T>(core::marker::PhantomData<T>);
131
132impl<Output: codec::Decode + Default, T>
133	frame_support::traits::Randomness<Output, BlockNumberFor<T>> for TestRandomness<T>
134where
135	T: frame_system::Config,
136{
137	fn random(subject: &[u8]) -> (Output, BlockNumberFor<T>) {
138		use sp_runtime::traits::TrailingZeroInput;
139
140		(
141			Output::decode(&mut TrailingZeroInput::new(subject)).unwrap_or_default(),
142			frame_system::Pallet::<T>::block_number(),
143		)
144	}
145}