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

Commit 84360e1

Browse files
ascjonesgui1117
andauthored
Migrate pallet-sudo to pallet! (#8448)
* WIP convert sudo pallet to attribute macros * Fix up tests and migrate mock * Fix up genesis build * Migrate doc comment example * Update frame/sudo/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Update frame/sudo/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Update frame/sudo/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Update frame/sudo/src/lib.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Allow unused metadata call_functions Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
1 parent c983207 commit 84360e1

4 files changed

Lines changed: 178 additions & 101 deletions

File tree

frame/sudo/src/lib.rs

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

18-
//! # Sudo Module
18+
//! # Sudo Pallet
1919
//!
2020
//! - [`Config`]
2121
//! - [`Call`]
2222
//!
2323
//! ## Overview
2424
//!
25-
//! The Sudo module allows for a single account (called the "sudo key")
25+
//! The Sudo pallet allows for a single account (called the "sudo key")
2626
//! to execute dispatchable functions that require a `Root` call
2727
//! or designate a new account to replace them as the sudo key.
2828
//! Only one account can be the sudo key at a time.
@@ -31,7 +31,7 @@
3131
//!
3232
//! ### Dispatchable Functions
3333
//!
34-
//! Only the sudo key can call the dispatchable functions from the Sudo module.
34+
//! Only the sudo key can call the dispatchable functions from the Sudo pallet.
3535
//!
3636
//! * `sudo` - Make a `Root` call to a dispatchable function.
3737
//! * `set_key` - Assign a new account to be the sudo key.
@@ -40,44 +40,55 @@
4040
//!
4141
//! ### Executing Privileged Functions
4242
//!
43-
//! The Sudo module itself is not intended to be used within other modules.
44-
//! Instead, you can build "privileged functions" (i.e. functions that require `Root` origin) in other modules.
43+
//! The Sudo pallet itself is not intended to be used within other pallets.
44+
//! Instead, you can build "privileged functions" (i.e. functions that require `Root` origin) in other pallets.
4545
//! You can execute these privileged functions by calling `sudo` with the sudo key account.
4646
//! Privileged functions cannot be directly executed via an extrinsic.
4747
//!
4848
//! Learn more about privileged functions and `Root` origin in the [`Origin`] type documentation.
4949
//!
5050
//! ### Simple Code Snippet
5151
//!
52-
//! This is an example of a module that exposes a privileged function:
52+
//! This is an example of a pallet that exposes a privileged function:
5353
//!
5454
//! ```
55-
//! use frame_support::{decl_module, dispatch};
56-
//! use frame_system::ensure_root;
5755
//!
58-
//! pub trait Config: frame_system::Config {}
56+
//! #[frame_support::pallet]
57+
//! pub mod logger {
58+
//! use frame_support::pallet_prelude::*;
59+
//! use frame_system::pallet_prelude::*;
60+
//! use super::*;
5961
//!
60-
//! decl_module! {
61-
//! pub struct Module<T: Config> for enum Call where origin: T::Origin {
62-
//! #[weight = 0]
63-
//! pub fn privileged_function(origin) -> dispatch::DispatchResult {
62+
//! #[pallet::config]
63+
//! pub trait Config: frame_system::Config {}
64+
//!
65+
//! #[pallet::pallet]
66+
//! pub struct Pallet<T>(PhantomData<T>);
67+
//!
68+
//! #[pallet::hooks]
69+
//! impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
70+
//!
71+
//! #[pallet::call]
72+
//! impl<T: Config> Pallet<T> {
73+
//! #[pallet::weight(0)]
74+
//! pub fn privileged_function(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
6475
//! ensure_root(origin)?;
6576
//!
6677
//! // do something...
6778
//!
68-
//! Ok(())
79+
//! Ok(().into())
6980
//! }
70-
//! }
81+
//! }
7182
//! }
7283
//! # fn main() {}
7384
//! ```
7485
//!
7586
//! ## Genesis Config
7687
//!
77-
//! The Sudo module depends on the [`GenesisConfig`](./struct.GenesisConfig.html).
88+
//! The Sudo pallet depends on the [`GenesisConfig`].
7889
//! You need to set an initial superuser account as the sudo `key`.
7990
//!
80-
//! ## Related Modules
91+
//! ## Related Pallets
8192
//!
8293
//! * [Democracy](../pallet_democracy/index.html)
8394
//!
@@ -89,35 +100,41 @@ use sp_std::prelude::*;
89100
use sp_runtime::{DispatchResult, traits::StaticLookup};
90101

91102
use frame_support::{
92-
Parameter, decl_module, decl_event, decl_storage, decl_error, ensure,
103+
weights::GetDispatchInfo,
104+
traits::UnfilteredDispatchable,
93105
};
94-
use frame_support::{
95-
weights::{Weight, GetDispatchInfo, Pays},
96-
traits::{UnfilteredDispatchable, Get},
97-
dispatch::DispatchResultWithPostInfo,
98-
};
99-
use frame_system::ensure_signed;
100106

101107
#[cfg(test)]
102108
mod mock;
103109
#[cfg(test)]
104110
mod tests;
105111

106-
pub trait Config: frame_system::Config {
107-
/// The overarching event type.
108-
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
112+
pub use pallet::*;
109113

110-
/// A sudo-able call.
111-
type Call: Parameter + UnfilteredDispatchable<Origin=Self::Origin> + GetDispatchInfo;
112-
}
114+
#[frame_support::pallet]
115+
pub mod pallet {
116+
use frame_support::pallet_prelude::*;
117+
use frame_system::pallet_prelude::*;
118+
use super::{*, DispatchResult};
113119

114-
decl_module! {
115-
/// Sudo module declaration.
116-
pub struct Module<T: Config> for enum Call where origin: T::Origin {
117-
type Error = Error<T>;
120+
#[pallet::config]
121+
pub trait Config: frame_system::Config {
122+
/// The overarching event type.
123+
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
118124

119-
fn deposit_event() = default;
125+
/// A sudo-able call.
126+
type Call: Parameter + UnfilteredDispatchable<Origin=Self::Origin> + GetDispatchInfo;
127+
}
120128

129+
#[pallet::pallet]
130+
#[pallet::generate_store(pub(super) trait Store)]
131+
pub struct Pallet<T>(PhantomData<T>);
132+
133+
#[pallet::hooks]
134+
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
135+
136+
#[pallet::call]
137+
impl<T: Config> Pallet<T> {
121138
/// Authenticates the sudo key and dispatches a function call with `Root` origin.
122139
///
123140
/// The dispatch origin for this call must be _Signed_.
@@ -128,17 +145,20 @@ decl_module! {
128145
/// - One DB write (event).
129146
/// - Weight of derivative `call` execution + 10,000.
130147
/// # </weight>
131-
#[weight = {
148+
#[pallet::weight({
132149
let dispatch_info = call.get_dispatch_info();
133150
(dispatch_info.weight.saturating_add(10_000), dispatch_info.class)
134-
}]
135-
fn sudo(origin, call: Box<<T as Config>::Call>) -> DispatchResultWithPostInfo {
151+
})]
152+
pub(crate) fn sudo(
153+
origin: OriginFor<T>,
154+
call: Box<<T as Config>::Call>,
155+
) -> DispatchResultWithPostInfo {
136156
// This is a public call, so we ensure that the origin is some signed account.
137157
let sender = ensure_signed(origin)?;
138158
ensure!(sender == Self::key(), Error::<T>::RequireSudo);
139159

140160
let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into());
141-
Self::deposit_event(RawEvent::Sudid(res.map(|_| ()).map_err(|e| e.error)));
161+
Self::deposit_event(Event::Sudid(res.map(|_| ()).map_err(|e| e.error)));
142162
// Sudo user does not pay a fee.
143163
Ok(Pays::No.into())
144164
}
@@ -153,14 +173,18 @@ decl_module! {
153173
/// - O(1).
154174
/// - The weight of this call is defined by the caller.
155175
/// # </weight>
156-
#[weight = (*_weight, call.get_dispatch_info().class)]
157-
fn sudo_unchecked_weight(origin, call: Box<<T as Config>::Call>, _weight: Weight) -> DispatchResultWithPostInfo {
176+
#[pallet::weight((*_weight, call.get_dispatch_info().class))]
177+
pub(crate) fn sudo_unchecked_weight(
178+
origin: OriginFor<T>,
179+
call: Box<<T as Config>::Call>,
180+
_weight: Weight,
181+
) -> DispatchResultWithPostInfo {
158182
// This is a public call, so we ensure that the origin is some signed account.
159183
let sender = ensure_signed(origin)?;
160184
ensure!(sender == Self::key(), Error::<T>::RequireSudo);
161185

162186
let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into());
163-
Self::deposit_event(RawEvent::Sudid(res.map(|_| ()).map_err(|e| e.error)));
187+
Self::deposit_event(Event::Sudid(res.map(|_| ()).map_err(|e| e.error)));
164188
// Sudo user does not pay a fee.
165189
Ok(Pays::No.into())
166190
}
@@ -174,14 +198,17 @@ decl_module! {
174198
/// - Limited storage reads.
175199
/// - One DB change.
176200
/// # </weight>
177-
#[weight = 0]
178-
fn set_key(origin, new: <T::Lookup as StaticLookup>::Source) -> DispatchResultWithPostInfo {
201+
#[pallet::weight(0)]
202+
pub(crate) fn set_key(
203+
origin: OriginFor<T>,
204+
new: <T::Lookup as StaticLookup>::Source,
205+
) -> DispatchResultWithPostInfo {
179206
// This is a public call, so we ensure that the origin is some signed account.
180207
let sender = ensure_signed(origin)?;
181208
ensure!(sender == Self::key(), Error::<T>::RequireSudo);
182209
let new = T::Lookup::lookup(new)?;
183210

184-
Self::deposit_event(RawEvent::KeyChanged(Self::key()));
211+
Self::deposit_event(Event::KeyChanged(Self::key()));
185212
<Key<T>>::put(new);
186213
// Sudo user does not pay a fee.
187214
Ok(Pays::No.into())
@@ -198,7 +225,7 @@ decl_module! {
198225
/// - One DB write (event).
199226
/// - Weight of derivative `call` execution + 10,000.
200227
/// # </weight>
201-
#[weight = {
228+
#[pallet::weight({
202229
let dispatch_info = call.get_dispatch_info();
203230
(
204231
dispatch_info.weight
@@ -207,8 +234,9 @@ decl_module! {
207234
.saturating_add(T::DbWeight::get().reads_writes(1, 1)),
208235
dispatch_info.class,
209236
)
210-
}]
211-
fn sudo_as(origin,
237+
})]
238+
pub(crate) fn sudo_as(
239+
origin: OriginFor<T>,
212240
who: <T::Lookup as StaticLookup>::Source,
213241
call: Box<<T as Config>::Call>
214242
) -> DispatchResultWithPostInfo {
@@ -220,35 +248,55 @@ decl_module! {
220248

221249
let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Signed(who).into());
222250

223-
Self::deposit_event(RawEvent::SudoAsDone(res.map(|_| ()).map_err(|e| e.error)));
251+
Self::deposit_event(Event::SudoAsDone(res.map(|_| ()).map_err(|e| e.error)));
224252
// Sudo user does not pay a fee.
225253
Ok(Pays::No.into())
226254
}
227255
}
228-
}
229256

230-
decl_event!(
231-
pub enum Event<T> where AccountId = <T as frame_system::Config>::AccountId {
257+
#[pallet::event]
258+
#[pallet::generate_deposit(pub(super) fn deposit_event)]
259+
#[pallet::metadata(T::AccountId = "AccountId")]
260+
pub enum Event<T: Config> {
232261
/// A sudo just took place. \[result\]
233262
Sudid(DispatchResult),
234263
/// The \[sudoer\] just switched identity; the old key is supplied.
235-
KeyChanged(AccountId),
264+
KeyChanged(T::AccountId),
236265
/// A sudo just took place. \[result\]
237266
SudoAsDone(DispatchResult),
238267
}
239-
);
240268

241-
decl_storage! {
242-
trait Store for Module<T: Config> as Sudo {
269+
#[pallet::error]
270+
/// Error for the Sudo pallet
271+
pub enum Error<T> {
272+
/// Sender must be the Sudo account
273+
RequireSudo,
274+
}
275+
276+
/// The `AccountId` of the sudo key.
277+
#[pallet::storage]
278+
#[pallet::getter(fn key)]
279+
pub(super) type Key<T: Config> = StorageValue<_, T::AccountId, ValueQuery>;
280+
281+
#[pallet::genesis_config]
282+
pub struct GenesisConfig<T: Config> {
243283
/// The `AccountId` of the sudo key.
244-
Key get(fn key) config(): T::AccountId;
284+
pub key: T::AccountId,
245285
}
246-
}
247286

248-
decl_error! {
249-
/// Error for the Sudo module
250-
pub enum Error for Module<T: Config> {
251-
/// Sender must be the Sudo account
252-
RequireSudo,
287+
#[cfg(feature = "std")]
288+
impl<T: Config> Default for GenesisConfig<T> {
289+
fn default() -> Self {
290+
Self {
291+
key: Default::default(),
292+
}
293+
}
294+
}
295+
296+
#[pallet::genesis_build]
297+
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
298+
fn build(&self) {
299+
<Key<T>>::put(&self.key);
300+
}
253301
}
254302
}

0 commit comments

Comments
 (0)