Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 91f4559

Browse files
authored
Migrate pallet-utility to pallet attribute macro. (#8326)
* Migrate pallet-utility to pallet attribute macro. * Replace 'Module' with 'Pallet' in benchmarking.
1 parent 189d079 commit 91f4559

File tree

3 files changed

+73
-53
lines changed

3 files changed

+73
-53
lines changed

frame/utility/src/benchmarking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ benchmarks! {
7070
}
7171

7272
impl_benchmark_test_suite!(
73-
Module,
73+
Pallet,
7474
crate::tests::new_test_ext(),
7575
crate::tests::Test,
7676
);

frame/utility/src/lib.rs

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717

18-
//! # Utility Module
19-
//! A stateless module with helpers for dispatch management which does no re-authentication.
18+
//! # Utility Pallet
19+
//! A stateless pallet with helpers for dispatch management which does no re-authentication.
2020
//!
2121
//! - [`Config`]
2222
//! - [`Call`]
2323
//!
2424
//! ## Overview
2525
//!
26-
//! This module contains two basic pieces of functionality:
26+
//! This pallet contains two basic pieces of functionality:
2727
//! - Batch dispatch: A stateless operation, allowing any origin to execute multiple calls in a
2828
//! single dispatch. This can be useful to amalgamate proposals, combining `set_code` with
2929
//! corresponding `set_storage`s, for efficient multiple payouts with just a single signature
@@ -34,9 +34,9 @@
3434
//! need multiple distinct accounts (e.g. as controllers for many staking accounts), but where
3535
//! it's perfectly fine to have each of them controlled by the same underlying keypair.
3636
//! Derivative accounts are, for the purposes of proxy filtering considered exactly the same as
37-
//! the oigin and are thus hampered with the origin's filters.
37+
//! the origin and are thus hampered with the origin's filters.
3838
//!
39-
//! Since proxy filters are respected in all dispatches of this module, it should never need to be
39+
//! Since proxy filters are respected in all dispatches of this pallet, it should never need to be
4040
//! filtered by any proxy.
4141
//!
4242
//! ## Interface
@@ -60,58 +60,58 @@ use sp_std::prelude::*;
6060
use codec::{Encode, Decode};
6161
use sp_core::TypeId;
6262
use sp_io::hashing::blake2_256;
63-
use frame_support::{decl_module, decl_event, decl_storage, Parameter, transactional};
6463
use frame_support::{
65-
traits::{OriginTrait, UnfilteredDispatchable, Get},
66-
weights::{Weight, GetDispatchInfo, DispatchClass, extract_actual_weight},
67-
dispatch::{PostDispatchInfo, DispatchResultWithPostInfo},
64+
transactional,
65+
traits::{OriginTrait, UnfilteredDispatchable},
66+
weights::{GetDispatchInfo, extract_actual_weight},
67+
dispatch::PostDispatchInfo,
6868
};
69-
use frame_system::{ensure_signed, ensure_root};
70-
use sp_runtime::{DispatchError, traits::Dispatchable};
69+
use sp_runtime::traits::Dispatchable;
7170
pub use weights::WeightInfo;
7271

73-
/// Configuration trait.
74-
pub trait Config: frame_system::Config {
75-
/// The overarching event type.
76-
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>;
72+
pub use pallet::*;
7773

78-
/// The overarching call type.
79-
type Call: Parameter + Dispatchable<Origin=Self::Origin, PostInfo=PostDispatchInfo>
80-
+ GetDispatchInfo + From<frame_system::Call<Self>>
81-
+ UnfilteredDispatchable<Origin=Self::Origin>;
74+
#[frame_support::pallet]
75+
pub mod pallet {
76+
use frame_support::pallet_prelude::*;
77+
use frame_system::pallet_prelude::*;
78+
use super::*;
8279

83-
/// Weight information for extrinsics in this pallet.
84-
type WeightInfo: WeightInfo;
85-
}
80+
#[pallet::pallet]
81+
#[pallet::generate_store(pub(super) trait Store)]
82+
pub struct Pallet<T>(_);
8683

87-
decl_storage! {
88-
trait Store for Module<T: Config> as Utility {}
89-
}
9084

91-
decl_event! {
92-
/// Events type.
85+
/// Configuration trait.
86+
#[pallet::config]
87+
pub trait Config: frame_system::Config {
88+
/// The overarching event type.
89+
type Event: From<Event> + IsType<<Self as frame_system::Config>::Event>;
90+
91+
/// The overarching call type.
92+
type Call: Parameter + Dispatchable<Origin=Self::Origin, PostInfo=PostDispatchInfo>
93+
+ GetDispatchInfo + From<frame_system::Call<Self>>
94+
+ UnfilteredDispatchable<Origin=Self::Origin>;
95+
96+
/// Weight information for extrinsics in this pallet.
97+
type WeightInfo: WeightInfo;
98+
}
99+
100+
#[pallet::event]
101+
#[pallet::generate_deposit(pub(super) fn deposit_event)]
93102
pub enum Event {
94103
/// Batch of dispatches did not complete fully. Index of first failing dispatch given, as
95104
/// well as the error. \[index, error\]
96105
BatchInterrupted(u32, DispatchError),
97106
/// Batch of dispatches completed fully with no error.
98107
BatchCompleted,
99108
}
100-
}
101-
102-
/// A module identifier. These are per module and should be stored in a registry somewhere.
103-
#[derive(Clone, Copy, Eq, PartialEq, Encode, Decode)]
104-
struct IndexedUtilityModuleId(u16);
105109

106-
impl TypeId for IndexedUtilityModuleId {
107-
const TYPE_ID: [u8; 4] = *b"suba";
108-
}
109-
110-
decl_module! {
111-
pub struct Module<T: Config> for enum Call where origin: T::Origin {
112-
/// Deposit one of this module's events by using the default implementation.
113-
fn deposit_event() = default;
110+
#[pallet::hooks]
111+
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
114112

113+
#[pallet::call]
114+
impl<T: Config> Pallet<T> {
115115
/// Send a batch of dispatch calls.
116116
///
117117
/// May be called from any origin.
@@ -130,7 +130,7 @@ decl_module! {
130130
/// `BatchInterrupted` event is deposited, along with the number of successful calls made
131131
/// and the error of the failed call. If all were successful, then the `BatchCompleted`
132132
/// event is deposited.
133-
#[weight = {
133+
#[pallet::weight({
134134
let dispatch_infos = calls.iter().map(|call| call.get_dispatch_info()).collect::<Vec<_>>();
135135
let dispatch_weight = dispatch_infos.iter()
136136
.map(|di| di.weight)
@@ -147,8 +147,11 @@ decl_module! {
147147
}
148148
};
149149
(dispatch_weight, dispatch_class)
150-
}]
151-
fn batch(origin, calls: Vec<<T as Config>::Call>) -> DispatchResultWithPostInfo {
150+
})]
151+
pub fn batch(
152+
origin: OriginFor<T>,
153+
calls: Vec<<T as Config>::Call>,
154+
) -> DispatchResultWithPostInfo {
152155
let is_root = ensure_root(origin.clone()).is_ok();
153156
let calls_len = calls.len();
154157
// Track the actual weight of each of the batch calls.
@@ -189,7 +192,7 @@ decl_module! {
189192
/// NOTE: Prior to version *12, this was called `as_limited_sub`.
190193
///
191194
/// The dispatch origin for this call must be _Signed_.
192-
#[weight = {
195+
#[pallet::weight({
193196
let dispatch_info = call.get_dispatch_info();
194197
(
195198
T::WeightInfo::as_derivative()
@@ -198,8 +201,12 @@ decl_module! {
198201
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
199202
dispatch_info.class,
200203
)
201-
}]
202-
fn as_derivative(origin, index: u16, call: Box<<T as Config>::Call>) -> DispatchResultWithPostInfo {
204+
})]
205+
pub fn as_derivative(
206+
origin: OriginFor<T>,
207+
index: u16,
208+
call: Box<<T as Config>::Call>,
209+
) -> DispatchResultWithPostInfo {
203210
let mut origin = origin;
204211
let who = ensure_signed(origin.clone())?;
205212
let pseudonym = Self::derivative_account_id(who, index);
@@ -229,7 +236,7 @@ decl_module! {
229236
/// # <weight>
230237
/// - Complexity: O(C) where C is the number of calls to be batched.
231238
/// # </weight>
232-
#[weight = {
239+
#[pallet::weight({
233240
let dispatch_infos = calls.iter().map(|call| call.get_dispatch_info()).collect::<Vec<_>>();
234241
let dispatch_weight = dispatch_infos.iter()
235242
.map(|di| di.weight)
@@ -246,9 +253,12 @@ decl_module! {
246253
}
247254
};
248255
(dispatch_weight, dispatch_class)
249-
}]
256+
})]
250257
#[transactional]
251-
fn batch_all(origin, calls: Vec<<T as Config>::Call>) -> DispatchResultWithPostInfo {
258+
pub fn batch_all(
259+
origin: OriginFor<T>,
260+
calls: Vec<<T as Config>::Call>,
261+
) -> DispatchResultWithPostInfo {
252262
let is_root = ensure_root(origin.clone()).is_ok();
253263
let calls_len = calls.len();
254264
// Track the actual weight of each of the batch calls.
@@ -276,9 +286,18 @@ decl_module! {
276286
Ok(Some(base_weight + weight).into())
277287
}
278288
}
289+
290+
}
291+
292+
/// A pallet identifier. These are per pallet and should be stored in a registry somewhere.
293+
#[derive(Clone, Copy, Eq, PartialEq, Encode, Decode)]
294+
struct IndexedUtilityPalletId(u16);
295+
296+
impl TypeId for IndexedUtilityPalletId {
297+
const TYPE_ID: [u8; 4] = *b"suba";
279298
}
280299

281-
impl<T: Config> Module<T> {
300+
impl<T: Config> Pallet<T> {
282301
/// Derive a derivative account ID from the owner account and the sub-account index.
283302
pub fn derivative_account_id(who: T::AccountId, index: u16) -> T::AccountId {
284303
let entropy = (b"modlpy/utilisuba", who, index).using_encoded(blake2_256);

frame/utility/src/tests.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use super::*;
2323

2424
use frame_support::{
25-
assert_ok, assert_noop, parameter_types, assert_err_ignore_postinfo,
25+
assert_ok, assert_noop, parameter_types, assert_err_ignore_postinfo, decl_module,
2626
weights::{Weight, Pays},
2727
dispatch::{DispatchError, DispatchErrorWithPostInfo, Dispatchable},
2828
traits::Filter,
@@ -35,7 +35,8 @@ use crate as utility;
3535
// example module to test behaviors.
3636
pub mod example {
3737
use super::*;
38-
use frame_support::dispatch::WithPostDispatchInfo;
38+
use frame_system::ensure_signed;
39+
use frame_support::dispatch::{DispatchResultWithPostInfo, WithPostDispatchInfo};
3940
pub trait Config: frame_system::Config { }
4041

4142
decl_module! {

0 commit comments

Comments
 (0)