From e3830f1ef447db653c2f002d911d63c99c82b0c2 Mon Sep 17 00:00:00 2001 From: Shaun Wang Date: Tue, 4 May 2021 21:33:57 +1200 Subject: [PATCH 1/2] Migrate pallet-nicks to pallet attribute macro. --- frame/nicks/src/lib.rs | 142 ++++++++++++++++++++++------------------- 1 file changed, 75 insertions(+), 67 deletions(-) diff --git a/frame/nicks/src/lib.rs b/frame/nicks/src/lib.rs index 1afe55756777a..56528d3e362d9 100644 --- a/frame/nicks/src/lib.rs +++ b/frame/nicks/src/lib.rs @@ -15,16 +15,16 @@ // See the License for the specific language governing permissions and // limitations under the License. -//! # Nicks Module +//! # Nicks Pallet //! //! - [`Config`] //! - [`Call`] //! //! ## Overview //! -//! Nicks is an example module for keeping track of account names on-chain. It makes no effort to +//! Nicks is an example pallet for keeping track of account names on-chain. It makes no effort to //! create a name hierarchy, be a DNS replacement or provide reverse lookups. Furthermore, the -//! weights attached to this module's dispatchable functions are for demonstration purposes only and +//! weights attached to this pallet's dispatchable functions are for demonstration purposes only and //! have not been designed to be economically secure. Do not use this pallet as-is in production. //! //! ## Interface @@ -45,63 +45,64 @@ use sp_std::prelude::*; use sp_runtime::{ traits::{StaticLookup, Zero} }; -use frame_support::{ - decl_module, decl_event, decl_storage, ensure, decl_error, - traits::{Currency, EnsureOrigin, ReservableCurrency, OnUnbalanced, Get}, -}; -use frame_system::ensure_signed; +use frame_support::traits::{Currency, ReservableCurrency, OnUnbalanced}; +pub use pallet::*; type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; type NegativeImbalanceOf = <::Currency as Currency<::AccountId>>::NegativeImbalance; -pub trait Config: frame_system::Config { - /// The overarching event type. - type Event: From> + Into<::Event>; +#[frame_support::pallet] +pub mod pallet { + use frame_system::{ensure_signed, pallet_prelude::*}; + use frame_support::{ensure, pallet_prelude::*, traits::{EnsureOrigin, Get}}; + use super::*; - /// The currency trait. - type Currency: ReservableCurrency; + #[pallet::config] + pub trait Config: frame_system::Config { + /// The overarching event type. + type Event: From> + IsType<::Event>; - /// Reservation fee. - type ReservationFee: Get>; + /// The currency trait. + type Currency: ReservableCurrency; - /// What to do with slashed funds. - type Slashed: OnUnbalanced>; + /// Reservation fee. + #[pallet::constant] + type ReservationFee: Get>; - /// The origin which may forcibly set or remove a name. Root can always do this. - type ForceOrigin: EnsureOrigin; + /// What to do with slashed funds. + type Slashed: OnUnbalanced>; - /// The minimum length a name may be. - type MinLength: Get; + /// The origin which may forcibly set or remove a name. Root can always do this. + type ForceOrigin: EnsureOrigin; - /// The maximum length a name may be. - type MaxLength: Get; -} + /// The minimum length a name may be. + #[pallet::constant] + type MinLength: Get; -decl_storage! { - trait Store for Module as Nicks { - /// The lookup table for names. - NameOf: map hasher(twox_64_concat) T::AccountId => Option<(Vec, BalanceOf)>; + /// The maximum length a name may be. + #[pallet::constant] + type MaxLength: Get; } -} -decl_event!( - pub enum Event where AccountId = ::AccountId, Balance = BalanceOf { + #[pallet::event] + #[pallet::generate_deposit(pub(super) fn deposit_event)] + #[pallet::metadata(T::AccountId = "AccountId", BalanceOf = "Balance")] + pub enum Event { /// A name was set. \[who\] - NameSet(AccountId), + NameSet(T::AccountId), /// A name was forcibly set. \[target\] - NameForced(AccountId), + NameForced(T::AccountId), /// A name was changed. \[who\] - NameChanged(AccountId), + NameChanged(T::AccountId), /// A name was cleared, and the given balance returned. \[who, deposit\] - NameCleared(AccountId, Balance), + NameCleared(T::AccountId, BalanceOf), /// A name was removed and the given balance slashed. \[target, deposit\] - NameKilled(AccountId, Balance), + NameKilled(T::AccountId, BalanceOf), } -); -decl_error! { - /// Error for the nicks module. - pub enum Error for Module { + /// Error for the nicks pallet. + #[pallet::error] + pub enum Error { /// A name is too short. TooShort, /// A name is too long. @@ -109,24 +110,20 @@ decl_error! { /// An account isn't named. Unnamed, } -} - -decl_module! { - /// Nicks module declaration. - pub struct Module for enum Call where origin: T::Origin { - type Error = Error; - - fn deposit_event() = default; - /// Reservation fee. - const ReservationFee: BalanceOf = T::ReservationFee::get(); + /// The lookup table for names. + #[pallet::storage] + pub(super) type NameOf = StorageMap<_, Twox64Concat, T::AccountId, (Vec, BalanceOf)>; - /// The minimum length a name may be. - const MinLength: u32 = T::MinLength::get() as u32; + #[pallet::pallet] + #[pallet::generate_store(pub(super) trait Store)] + pub struct Pallet(_); - /// The maximum length a name may be. - const MaxLength: u32 = T::MaxLength::get() as u32; + #[pallet::hooks] + impl Hooks> for Pallet {} + #[pallet::call] + impl Pallet { /// Set an account's name. The name should be a UTF-8-encoded string by convention, though /// we don't check it. /// @@ -143,24 +140,25 @@ decl_module! { /// - One storage read/write. /// - One event. /// # - #[weight = 50_000_000] - fn set_name(origin, name: Vec) { + #[pallet::weight(50_000_000)] + fn set_name(origin: OriginFor, name: Vec) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; ensure!(name.len() >= T::MinLength::get(), Error::::TooShort); ensure!(name.len() <= T::MaxLength::get(), Error::::TooLong); let deposit = if let Some((_, deposit)) = >::get(&sender) { - Self::deposit_event(RawEvent::NameChanged(sender.clone())); + Self::deposit_event(Event::::NameChanged(sender.clone())); deposit } else { let deposit = T::ReservationFee::get(); T::Currency::reserve(&sender, deposit.clone())?; - Self::deposit_event(RawEvent::NameSet(sender.clone())); + Self::deposit_event(Event::::NameSet(sender.clone())); deposit }; >::insert(&sender, (name, deposit)); + Ok(().into()) } /// Clear an account's name and return the deposit. Fails if the account was not named. @@ -173,8 +171,8 @@ decl_module! { /// - One storage read/write. /// - One event. /// # - #[weight = 70_000_000] - fn clear_name(origin) { + #[pallet::weight(70_000_000)] + fn clear_name(origin: OriginFor) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; let deposit = >::take(&sender).ok_or(Error::::Unnamed)?.1; @@ -182,7 +180,8 @@ decl_module! { let err_amount = T::Currency::unreserve(&sender, deposit.clone()); debug_assert!(err_amount.is_zero()); - Self::deposit_event(RawEvent::NameCleared(sender, deposit)); + Self::deposit_event(Event::::NameCleared(sender, deposit)); + Ok(().into()) } /// Remove an account's name and take charge of the deposit. @@ -198,8 +197,11 @@ decl_module! { /// - One storage read/write. /// - One event. /// # - #[weight = 70_000_000] - fn kill_name(origin, target: ::Source) { + #[pallet::weight(70_000_000)] + fn kill_name( + origin: OriginFor, + target: ::Source + ) -> DispatchResultWithPostInfo { T::ForceOrigin::ensure_origin(origin)?; // Figure out who we're meant to be clearing. @@ -209,7 +211,8 @@ decl_module! { // Slash their deposit from them. T::Slashed::on_unbalanced(T::Currency::slash_reserved(&target, deposit.clone()).0); - Self::deposit_event(RawEvent::NameKilled(target, deposit)); + Self::deposit_event(Event::::NameKilled(target, deposit)); + Ok(().into()) } /// Set a third-party account's name with no deposit. @@ -224,15 +227,20 @@ decl_module! { /// - One storage read/write. /// - One event. /// # - #[weight = 70_000_000] - fn force_name(origin, target: ::Source, name: Vec) { + #[pallet::weight(70_000_000)] + fn force_name( + origin: OriginFor, + target: ::Source, + name: Vec + ) -> DispatchResultWithPostInfo { T::ForceOrigin::ensure_origin(origin)?; let target = T::Lookup::lookup(target)?; let deposit = >::get(&target).map(|x| x.1).unwrap_or_else(Zero::zero); >::insert(&target, (name, deposit)); - Self::deposit_event(RawEvent::NameForced(target)); + Self::deposit_event(Event::::NameForced(target)); + Ok(().into()) } } } From b3ceb003477221a3b34f967b66618fbc27a33cd9 Mon Sep 17 00:00:00 2001 From: Shaun Wang Date: Tue, 4 May 2021 22:07:04 +1200 Subject: [PATCH 2/2] Fix constants. --- frame/nicks/src/lib.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/frame/nicks/src/lib.rs b/frame/nicks/src/lib.rs index 56528d3e362d9..a6d2415ab96ef 100644 --- a/frame/nicks/src/lib.rs +++ b/frame/nicks/src/lib.rs @@ -77,11 +77,11 @@ pub mod pallet { /// The minimum length a name may be. #[pallet::constant] - type MinLength: Get; + type MinLength: Get; /// The maximum length a name may be. #[pallet::constant] - type MaxLength: Get; + type MaxLength: Get; } #[pallet::event] @@ -141,11 +141,11 @@ pub mod pallet { /// - One event. /// # #[pallet::weight(50_000_000)] - fn set_name(origin: OriginFor, name: Vec) -> DispatchResultWithPostInfo { + pub(super) fn set_name(origin: OriginFor, name: Vec) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; - ensure!(name.len() >= T::MinLength::get(), Error::::TooShort); - ensure!(name.len() <= T::MaxLength::get(), Error::::TooLong); + ensure!(name.len() >= T::MinLength::get() as usize, Error::::TooShort); + ensure!(name.len() <= T::MaxLength::get() as usize, Error::::TooLong); let deposit = if let Some((_, deposit)) = >::get(&sender) { Self::deposit_event(Event::::NameChanged(sender.clone())); @@ -172,7 +172,7 @@ pub mod pallet { /// - One event. /// # #[pallet::weight(70_000_000)] - fn clear_name(origin: OriginFor) -> DispatchResultWithPostInfo { + pub(super) fn clear_name(origin: OriginFor) -> DispatchResultWithPostInfo { let sender = ensure_signed(origin)?; let deposit = >::take(&sender).ok_or(Error::::Unnamed)?.1; @@ -198,7 +198,7 @@ pub mod pallet { /// - One event. /// # #[pallet::weight(70_000_000)] - fn kill_name( + pub(super) fn kill_name( origin: OriginFor, target: ::Source ) -> DispatchResultWithPostInfo { @@ -228,7 +228,7 @@ pub mod pallet { /// - One event. /// # #[pallet::weight(70_000_000)] - fn force_name( + pub(super) fn force_name( origin: OriginFor, target: ::Source, name: Vec @@ -316,8 +316,8 @@ mod tests { } parameter_types! { pub const ReservationFee: u64 = 2; - pub const MinLength: usize = 3; - pub const MaxLength: usize = 16; + pub const MinLength: u32 = 3; + pub const MaxLength: u32 = 16; } ord_parameter_types! { pub const One: u64 = 1;