diff --git a/pallets/pallet-web3-names/src/benchmarking.rs b/pallets/pallet-web3-names/src/benchmarking.rs index 0039b6390..ae208147c 100644 --- a/pallets/pallet-web3-names/src/benchmarking.rs +++ b/pallets/pallet-web3-names/src/benchmarking.rs @@ -17,7 +17,7 @@ // If you feel like getting in touch with us, you can do so at info@botlabs.org #![cfg(feature = "runtime-benchmarks")] -use frame_benchmarking::{account, benchmarks}; +use frame_benchmarking::{account, benchmarks_instance_pallet}; use frame_support::{ pallet_prelude::EnsureOrigin, sp_runtime::{traits::Zero, SaturatedConversion}, @@ -41,163 +41,165 @@ use crate::{ const CALLER_SEED: u32 = 0; const OWNER_SEED: u32 = 1; -fn make_free_for_did(account: &AccountIdOf) +fn make_free_for_did(account: &AccountIdOf) where - ::Currency: Mutate, + T: Config, + I: 'static, + >::Currency: Mutate, { - let balance = as Inspect>>::minimum_balance() - + ::Deposit::get() - + ::Deposit::get(); - CurrencyOf::::set_balance(account, balance); + let balance = as Inspect>>::minimum_balance() + + >::Deposit::get() + + >::Deposit::get(); + CurrencyOf::::set_balance(account, balance); } fn generate_web3_name_input(length: usize) -> Vec { vec![b'1'; length] } -benchmarks! { +benchmarks_instance_pallet! { where_clause { where T::AccountId: From, T::Web3NameOwner: From, T::OwnerOrigin: GenerateBenchmarkOrigin, T::BanOrigin: EnsureOrigin, - ::Currency: Mutate, + >::Currency: Mutate, } claim { let n in (T::MinNameLength::get()) .. (T::MaxNameLength::get()); let caller: AccountIdOf = account("caller", 0, CALLER_SEED); - let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); + let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); let web3_name_input: BoundedVec = BoundedVec::try_from(generate_web3_name_input(n.saturated_into())).expect("BoundedVec creation should not fail."); let web3_name_input_clone = web3_name_input.clone(); let origin = T::OwnerOrigin::generate_origin(caller.clone(), owner.clone()); - make_free_for_did::(&caller); + make_free_for_did::(&caller); }: _(origin, web3_name_input_clone) verify { - let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); - assert!(Names::::get(&owner).is_some()); - assert!(Owner::::get(&web3_name).is_some()); + let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); + assert!(Names::::get(&owner).is_some()); + assert!(Owner::::get(&web3_name).is_some()); } release_by_owner { let caller: AccountIdOf = account("caller", 0, CALLER_SEED); - let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); + let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); let web3_name_input: BoundedVec = BoundedVec::try_from(generate_web3_name_input(T::MaxNameLength::get().saturated_into())).expect("BoundedVec creation should not fail."); let origin = T::OwnerOrigin::generate_origin(caller.clone(), owner.clone()); - make_free_for_did::(&caller); - Pallet::::claim(origin.clone(), web3_name_input.clone()).expect("Should register the claimed web3 name."); + make_free_for_did::(&caller); + Pallet::::claim(origin.clone(), web3_name_input.clone()).expect("Should register the claimed web3 name."); }: _(origin) verify { - let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); - assert!(Names::::get(&owner).is_none()); - assert!(Owner::::get(&web3_name).is_none()); + let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); + assert!(Names::::get(&owner).is_none()); + assert!(Owner::::get(&web3_name).is_none()); } reclaim_deposit { let n in (T::MinNameLength::get()) .. (T::MaxNameLength::get()); let caller: AccountIdOf = account("caller", 0, CALLER_SEED); - let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); + let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); let web3_name_input: BoundedVec = BoundedVec::try_from(generate_web3_name_input(n.saturated_into())).expect("BoundedVec creation should not fail."); let web3_name_input_clone = web3_name_input.clone(); let did_origin = T::OwnerOrigin::generate_origin(caller.clone(), owner.clone()); let signed_origin = RawOrigin::Signed(caller.clone()); - make_free_for_did::(&caller); - Pallet::::claim(did_origin, web3_name_input.clone()).expect("Should register the claimed web3 name."); + make_free_for_did::(&caller); + Pallet::::claim(did_origin, web3_name_input.clone()).expect("Should register the claimed web3 name."); }: _(signed_origin, web3_name_input_clone) verify { - let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); - assert!(Names::::get(&owner).is_none()); - assert!(Owner::::get(&web3_name).is_none()); + let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); + assert!(Names::::get(&owner).is_none()); + assert!(Owner::::get(&web3_name).is_none()); } ban { let n in (T::MinNameLength::get()) .. (T::MaxNameLength::get()); let caller: AccountIdOf = account("caller", 0, CALLER_SEED); - let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); + let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); let web3_name_input: BoundedVec = BoundedVec::try_from(generate_web3_name_input(n.saturated_into())).expect("BoundedVec creation should not fail."); let web3_name_input_clone = web3_name_input.clone(); let did_origin = T::OwnerOrigin::generate_origin(caller.clone(), owner.clone()); let ban_origin = RawOrigin::Root; - make_free_for_did::(&caller); - Pallet::::claim(did_origin, web3_name_input.clone()).expect("Should register the claimed web3 name."); + make_free_for_did::(&caller); + Pallet::::claim(did_origin, web3_name_input.clone()).expect("Should register the claimed web3 name."); }: _(ban_origin, web3_name_input_clone) verify { - let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); - assert!(Names::::get(&owner).is_none()); - assert!(Owner::::get(&web3_name).is_none()); - assert!(Banned::::get(&web3_name).is_some()); + let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); + assert!(Names::::get(&owner).is_none()); + assert!(Owner::::get(&web3_name).is_none()); + assert!(Banned::::get(&web3_name).is_some()); } unban { let n in (T::MinNameLength::get()) .. (T::MaxNameLength::get()); let caller: AccountIdOf = account("caller", 0, CALLER_SEED); - let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); + let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); let web3_name_input: BoundedVec = BoundedVec::try_from(generate_web3_name_input(n.saturated_into())).expect("BoundedVec creation should not fail."); let web3_name_input_clone = web3_name_input.clone(); let ban_origin = RawOrigin::Root; - make_free_for_did::(&caller); - Pallet::::ban(ban_origin.clone().into(), web3_name_input.clone()).expect("Should ban the web3 name."); + make_free_for_did::(&caller); + Pallet::::ban(ban_origin.clone().into(), web3_name_input.clone()).expect("Should ban the web3 name."); }: _(ban_origin, web3_name_input_clone) verify { - let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); - assert!(Names::::get(&owner).is_none()); - assert!(Owner::::get(&web3_name).is_none()); - assert!(Banned::::get(&web3_name).is_none()); + let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); + assert!(Names::::get(&owner).is_none()); + assert!(Owner::::get(&web3_name).is_none()); + assert!(Banned::::get(&web3_name).is_none()); } change_deposit_owner { let deposit_owner_old: AccountIdOf = account("caller", 0, CALLER_SEED); let deposit_owner_new: AccountIdOf = account("caller", 1, CALLER_SEED); - let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); + let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); let web3_name_input: BoundedVec = BoundedVec::try_from( generate_web3_name_input(T::MaxNameLength::get().saturated_into()) ).expect("BoundedVec creation should not fail."); let web3_name_input_clone = web3_name_input.clone(); let origin_create = T::OwnerOrigin::generate_origin(deposit_owner_old.clone(), owner.clone()); - make_free_for_did::(&deposit_owner_old); - make_free_for_did::(&deposit_owner_new); - Pallet::::claim(origin_create, web3_name_input.clone()).expect("Should register the claimed web3 name."); + make_free_for_did::(&deposit_owner_old); + make_free_for_did::(&deposit_owner_new); + Pallet::::claim(origin_create, web3_name_input.clone()).expect("Should register the claimed web3 name."); let origin = T::OwnerOrigin::generate_origin(deposit_owner_new.clone(), owner); }: _(origin) verify { - let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); - assert_eq!(Owner::::get(&web3_name).expect("w3n should exists").deposit, Deposit { + let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); + assert_eq!(Owner::::get(&web3_name).expect("w3n should exists").deposit, Deposit { owner: deposit_owner_new, - amount: ::Deposit::get(), + amount: >::Deposit::get(), }); } update_deposit { let deposit_owner: AccountIdOf = account("caller", 0, CALLER_SEED); - let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); + let owner: Web3NameOwnerOf = account("owner", 0, OWNER_SEED); let web3_name_input: BoundedVec = BoundedVec::try_from( generate_web3_name_input(T::MaxNameLength::get().saturated_into()) ).expect("BoundedVec creation should not fail."); - let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); + let web3_name = Web3NameOf::::try_from(web3_name_input.to_vec()).unwrap(); - make_free_for_did::(&deposit_owner); - insert_raw_w3n::( + make_free_for_did::(&deposit_owner); + insert_raw_w3n::( deposit_owner.clone(), owner, web3_name.clone(), BlockNumberFor::::zero(), - ::Deposit::get() + ::Deposit::get() + >::Deposit::get() + >::Deposit::get() ); let origin = RawOrigin::Signed(deposit_owner.clone()); }: _(origin, web3_name_input) verify { - assert_eq!(Owner::::get(&web3_name).expect("w3n should exists").deposit, Deposit { + assert_eq!(Owner::::get(&web3_name).expect("w3n should exists").deposit, Deposit { owner: deposit_owner, - amount: ::Deposit::get(), + amount: >::Deposit::get(), }); } diff --git a/pallets/pallet-web3-names/src/lib.rs b/pallets/pallet-web3-names/src/lib.rs index 68cdf9f57..18dc220e3 100644 --- a/pallets/pallet-web3-names/src/lib.rs +++ b/pallets/pallet-web3-names/src/lib.rs @@ -77,59 +77,61 @@ pub mod pallet { const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); pub type AccountIdOf = ::AccountId; - pub type Web3NameOwnerOf = ::Web3NameOwner; - pub type Web3NameInput = BoundedVec::MaxNameLength>; - pub type Web3NameOf = ::Web3Name; - pub type Web3OwnershipOf = - Web3NameOwnership, Deposit, BalanceOf>, BlockNumberFor>; + pub type BalanceOf = as Inspect>>::Balance; + pub type Web3NameOwnerOf = >::Web3NameOwner; + pub type Web3NameInput = BoundedVec>::MaxNameLength>; + pub type Web3NameOf = >::Web3Name; + pub type Web3OwnershipOf = + Web3NameOwnership, Deposit, BalanceOf>, BlockNumberFor>; - pub(crate) type BalanceMigrationManagerOf = ::BalanceMigrationManager; - pub(crate) type CurrencyOf = ::Currency; - pub type BalanceOf = as Inspect>>::Balance; + pub(crate) type BalanceMigrationManagerOf = >::BalanceMigrationManager; + pub(crate) type CurrencyOf = >::Currency; #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] - pub struct Pallet(_); + pub struct Pallet(_); /// Map of name -> ownership details. #[pallet::storage] #[pallet::getter(fn owner)] - pub type Owner = StorageMap<_, Blake2_128Concat, Web3NameOf, Web3OwnershipOf>; + pub type Owner, I: 'static = ()> = + StorageMap<_, Blake2_128Concat, Web3NameOf, Web3OwnershipOf>; /// Map of owner -> name. #[pallet::storage] #[pallet::getter(fn names)] - pub type Names = StorageMap<_, Blake2_128Concat, Web3NameOwnerOf, Web3NameOf>; + pub type Names, I: 'static = ()> = + StorageMap<_, Blake2_128Concat, Web3NameOwnerOf, Web3NameOf>; /// Map of name -> (). /// /// If a name key is present, the name is currently banned. #[pallet::storage] #[pallet::getter(fn is_banned)] - pub type Banned = StorageMap<_, Blake2_128Concat, Web3NameOf, ()>; + pub type Banned, I: 'static = ()> = StorageMap<_, Blake2_128Concat, Web3NameOf, ()>; #[pallet::composite_enum] - pub enum HoldReason { + pub enum HoldReason { Deposit, } #[pallet::config] - pub trait Config: frame_system::Config { + pub trait Config: frame_system::Config { /// The origin allowed to ban names. type BanOrigin: EnsureOrigin; /// The origin allowed to perform regular operations. type OwnerOrigin: EnsureOrigin<::RuntimeOrigin, Success = Self::OriginSuccess>; /// The type of origin after a successful origin check. - type OriginSuccess: CallSources, Web3NameOwnerOf>; + type OriginSuccess: CallSources, Web3NameOwnerOf>; /// Aggregated hold reason. - type RuntimeHoldReason: From; + type RuntimeHoldReason: From>; /// The currency type to reserve and release deposits. type Currency: MutateHold, Reason = Self::RuntimeHoldReason>; /// The amount of KILT to deposit to claim a name. #[pallet::constant] - type Deposit: Get>; + type Deposit: Get>; /// The overarching event type. - type RuntimeEvent: From> + IsType<::RuntimeEvent>; + type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The min encoded length of a name. #[pallet::constant] type MinNameLength: Get; @@ -144,7 +146,7 @@ pub mod pallet { + PartialEq + Clone + TypeInfo - + TryFrom, Error = Error> + + TryFrom, Error = Error> + MaxEncodedLen + Ord; /// The type of a name owner. @@ -153,30 +155,30 @@ pub mod pallet { type WeightInfo: WeightInfo; /// Migration manager to handle new created entries - type BalanceMigrationManager: BalanceMigrationManager, BalanceOf>; + type BalanceMigrationManager: BalanceMigrationManager, BalanceOf>; } #[pallet::event] #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { + pub enum Event, I: 'static = ()> { /// A new name has been claimed. Web3NameClaimed { - owner: Web3NameOwnerOf, - name: Web3NameOf, + owner: Web3NameOwnerOf, + name: Web3NameOf, }, /// A name has been released. Web3NameReleased { - owner: Web3NameOwnerOf, - name: Web3NameOf, + owner: Web3NameOwnerOf, + name: Web3NameOf, }, /// A name has been banned. - Web3NameBanned { name: Web3NameOf }, + Web3NameBanned { name: Web3NameOf }, /// A name has been unbanned. - Web3NameUnbanned { name: Web3NameOf }, + Web3NameUnbanned { name: Web3NameOf }, /// The deposit for a web3name has changed owner. DepositOwnerChanged { /// The web3name whose deposit owner changed. - id: Web3NameOf, + id: Web3NameOf, /// The old deposit owner. from: AccountIdOf, /// The new deposit owner. @@ -186,7 +188,7 @@ pub mod pallet { #[pallet::error] #[derive(PartialEq, Eq)] - pub enum Error { + pub enum Error { /// The tx submitter does not have enough funds to pay for the deposit. InsufficientFunds, /// The specified name has already been previously claimed. @@ -215,15 +217,15 @@ pub mod pallet { } #[pallet::hooks] - impl Hooks> for Pallet { + impl, I: 'static> Hooks> for Pallet { #[cfg(feature = "try-runtime")] fn try_state(_n: BlockNumberFor) -> Result<(), sp_runtime::TryRuntimeError> { - crate::try_state::do_try_state::() + crate::try_state::do_try_state::() } } #[pallet::call] - impl Pallet { + impl, I: 'static> Pallet { /// Assign the specified name to the owner as specified in the /// origin. /// @@ -240,8 +242,8 @@ pub mod pallet { /// - Writes: Names, Owner storage entries + currency deposit reserve /// # #[pallet::call_index(0)] - #[pallet::weight(::WeightInfo::claim(name.len().saturated_into()))] - pub fn claim(origin: OriginFor, name: Web3NameInput) -> DispatchResult { + #[pallet::weight(>::WeightInfo::claim(name.len().saturated_into()))] + pub fn claim(origin: OriginFor, name: Web3NameInput) -> DispatchResult { let runtime_origin = T::OwnerOrigin::ensure_origin(origin)?; let payer = runtime_origin.sender(); let owner = runtime_origin.subject(); @@ -266,7 +268,7 @@ pub mod pallet { /// - Writes: Names, Owner storage entries + currency deposit release /// # #[pallet::call_index(1)] - #[pallet::weight(::WeightInfo::release_by_owner())] + #[pallet::weight(>::WeightInfo::release_by_owner())] pub fn release_by_owner(origin: OriginFor) -> DispatchResult { let runtime_origin = T::OwnerOrigin::ensure_origin(origin)?; let owner = runtime_origin.subject(); @@ -291,8 +293,8 @@ pub mod pallet { /// - Writes: Names, Owner storage entries + currency deposit release /// # #[pallet::call_index(2)] - #[pallet::weight(::WeightInfo::reclaim_deposit(name.len().saturated_into()))] - pub fn reclaim_deposit(origin: OriginFor, name: Web3NameInput) -> DispatchResult { + #[pallet::weight(>::WeightInfo::reclaim_deposit(name.len().saturated_into()))] + pub fn reclaim_deposit(origin: OriginFor, name: Web3NameInput) -> DispatchResult { let caller = ensure_signed(origin)?; let decoded_name = Self::check_reclaim_deposit_preconditions(name, &caller)?; @@ -319,8 +321,8 @@ pub mod pallet { /// release /// # #[pallet::call_index(3)] - #[pallet::weight(::WeightInfo::ban(name.len().saturated_into()))] - pub fn ban(origin: OriginFor, name: Web3NameInput) -> DispatchResult { + #[pallet::weight(>::WeightInfo::ban(name.len().saturated_into()))] + pub fn ban(origin: OriginFor, name: Web3NameInput) -> DispatchResult { T::BanOrigin::ensure_origin(origin)?; let (decoded_name, is_claimed) = Self::check_banning_preconditions(name)?; @@ -330,7 +332,7 @@ pub mod pallet { } Self::ban_name(&decoded_name); - Self::deposit_event(Event::::Web3NameBanned { name: decoded_name }); + Self::deposit_event(Event::::Web3NameBanned { name: decoded_name }); Ok(()) } @@ -350,14 +352,14 @@ pub mod pallet { /// - Writes: Banned storage entry deposit release /// # #[pallet::call_index(4)] - #[pallet::weight(::WeightInfo::unban(name.len().saturated_into()))] - pub fn unban(origin: OriginFor, name: Web3NameInput) -> DispatchResult { + #[pallet::weight(>::WeightInfo::unban(name.len().saturated_into()))] + pub fn unban(origin: OriginFor, name: Web3NameInput) -> DispatchResult { T::BanOrigin::ensure_origin(origin)?; let decoded_name = Self::check_unbanning_preconditions(name)?; Self::unban_name(&decoded_name); - Self::deposit_event(Event::::Web3NameUnbanned { name: decoded_name }); + Self::deposit_event(Event::::Web3NameUnbanned { name: decoded_name }); Ok(()) } @@ -370,18 +372,18 @@ pub mod pallet { /// The subject of the call must be the owner of the web3name. /// The sender of the call will be the new deposit owner. #[pallet::call_index(5)] - #[pallet::weight(::WeightInfo::change_deposit_owner())] + #[pallet::weight(>::WeightInfo::change_deposit_owner())] pub fn change_deposit_owner(origin: OriginFor) -> DispatchResult { - let source = ::OwnerOrigin::ensure_origin(origin)?; + let source = >::OwnerOrigin::ensure_origin(origin)?; let w3n_owner = source.subject(); let sender = source.sender(); - let name = Names::::get(&w3n_owner).ok_or(Error::::NotFound)?; + let name = Names::::get(&w3n_owner).ok_or(Error::::NotFound)?; - let old_deposit_owner = Web3NameStorageDepositCollector::::change_deposit_owner::< - BalanceMigrationManagerOf, + let old_deposit_owner = Web3NameStorageDepositCollector::::change_deposit_owner::< + BalanceMigrationManagerOf, >(&name, sender.clone())?; - Self::deposit_event(Event::::DepositOwnerChanged { + Self::deposit_event(Event::::DepositOwnerChanged { id: name, from: old_deposit_owner, to: sender, @@ -394,20 +396,20 @@ pub mod pallet { /// /// The sender must be the deposit owner. #[pallet::call_index(6)] - #[pallet::weight(::WeightInfo::update_deposit())] - pub fn update_deposit(origin: OriginFor, name_input: Web3NameInput) -> DispatchResult { + #[pallet::weight(>::WeightInfo::update_deposit())] + pub fn update_deposit(origin: OriginFor, name_input: Web3NameInput) -> DispatchResult { let source = ensure_signed(origin)?; - let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; - let w3n_entry = Owner::::get(&name).ok_or(Error::::NotFound)?; - ensure!(w3n_entry.deposit.owner == source, Error::::NotAuthorized); + let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; + let w3n_entry = Owner::::get(&name).ok_or(Error::::NotFound)?; + ensure!(w3n_entry.deposit.owner == source, Error::::NotAuthorized); - Web3NameStorageDepositCollector::::update_deposit::>(&name)?; + Web3NameStorageDepositCollector::::update_deposit::>(&name)?; Ok(()) } } - impl Pallet { + impl, I: 'static> Pallet { /// Verify that the claiming preconditions are verified. Specifically: /// - The name input data can be decoded as a valid name /// - The name does not already exist @@ -415,15 +417,15 @@ pub mod pallet { /// - The name has not been banned /// - The tx submitter has enough funds to pay the deposit fn check_claiming_preconditions( - name_input: Web3NameInput, - owner: &Web3NameOwnerOf, + name_input: Web3NameInput, + owner: &Web3NameOwnerOf, deposit_payer: &AccountIdOf, - ) -> Result, DispatchError> { - let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; + ) -> Result, DispatchError> { + let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; - ensure!(!Names::::contains_key(owner), Error::::OwnerAlreadyExists); - ensure!(!Owner::::contains_key(&name), Error::::AlreadyExists); - ensure!(!Banned::::contains_key(&name), Error::::Banned); + ensure!(!Names::::contains_key(owner), Error::::OwnerAlreadyExists); + ensure!(!Owner::::contains_key(&name), Error::::AlreadyExists); + ensure!(!Banned::::contains_key(&name), Error::::Banned); ensure!( >>::can_hold( @@ -431,7 +433,7 @@ pub mod pallet { deposit_payer, T::Deposit::get() ), - Error::::InsufficientFunds + Error::::InsufficientFunds ); Ok(name) @@ -442,34 +444,36 @@ pub mod pallet { /// `check_claiming_preconditions` as it does not verify all the /// preconditions again. pub fn register_name( - name: Web3NameOf, - owner: Web3NameOwnerOf, + name: Web3NameOf, + owner: Web3NameOwnerOf, deposit_payer: AccountIdOf, ) -> DispatchResult { let block_number = frame_system::Pallet::::block_number(); - let deposit = Web3NameStorageDepositCollector::::create_deposit(deposit_payer, T::Deposit::get())?; - ::BalanceMigrationManager::exclude_key_from_migration(&Owner::::hashed_key_for(&name)); + let deposit = Web3NameStorageDepositCollector::::create_deposit(deposit_payer, T::Deposit::get())?; + >::BalanceMigrationManager::exclude_key_from_migration(&Owner::::hashed_key_for( + &name, + )); - Names::::insert(&owner, name.clone()); - Owner::::insert( + Names::::insert(&owner, name.clone()); + Owner::::insert( &name, - Web3OwnershipOf:: { + Web3OwnershipOf:: { owner: owner.clone(), claimed_at: block_number, deposit, }, ); - Self::deposit_event(Event::::Web3NameClaimed { owner, name }); + Self::deposit_event(Event::::Web3NameClaimed { owner, name }); Ok(()) } /// Verify that the releasing preconditions for an owner are verified. /// Specifically: /// - The owner has a previously claimed name - fn check_releasing_preconditions(owner: &Web3NameOwnerOf) -> Result, DispatchError> { - let name = Names::::get(owner).ok_or(Error::::OwnerNotFound)?; + fn check_releasing_preconditions(owner: &Web3NameOwnerOf) -> Result, DispatchError> { + let name = Names::::get(owner).ok_or(Error::::OwnerNotFound)?; Ok(name) } @@ -480,13 +484,13 @@ pub mod pallet { /// - The name exists (i.e., it has been previous claimed) /// - The caller owns the name's deposit fn check_reclaim_deposit_preconditions( - name_input: Web3NameInput, + name_input: Web3NameInput, caller: &AccountIdOf, - ) -> Result, DispatchError> { - let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; - let Web3NameOwnership { deposit, .. } = Owner::::get(&name).ok_or(Error::::NotFound)?; + ) -> Result, DispatchError> { + let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; + let Web3NameOwnership { deposit, .. } = Owner::::get(&name).ok_or(Error::::NotFound)?; - ensure!(caller == &deposit.owner, Error::::NotAuthorized); + ensure!(caller == &deposit.owner, Error::::NotAuthorized); Ok(name) } @@ -495,23 +499,23 @@ pub mod pallet { /// original payer. This function must be called after /// `check_releasing_preconditions` as it does not verify all the /// preconditions again. - fn unregister_name(name: &Web3NameOf) -> Result, DispatchError> { - let name_ownership = Owner::::take(name).ok_or(Error::::OwnerNotFound)?; - Names::::remove(&name_ownership.owner); + fn unregister_name(name: &Web3NameOf) -> Result, DispatchError> { + let name_ownership = Owner::::take(name).ok_or(Error::::OwnerNotFound)?; + Names::::remove(&name_ownership.owner); let is_key_migrated = - ::BalanceMigrationManager::is_key_migrated(&Owner::::hashed_key_for(name)); + >::BalanceMigrationManager::is_key_migrated(&Owner::::hashed_key_for(name)); if is_key_migrated { - Web3NameStorageDepositCollector::::free_deposit(name_ownership.clone().deposit)?; + Web3NameStorageDepositCollector::::free_deposit(name_ownership.clone().deposit)?; } else { - ::BalanceMigrationManager::release_reserved_deposit( + >::BalanceMigrationManager::release_reserved_deposit( &name_ownership.deposit.owner, &name_ownership.deposit.amount, ) } - Self::deposit_event(Event::::Web3NameReleased { + Self::deposit_event(Event::::Web3NameReleased { owner: name_ownership.owner.clone(), name: name.clone(), }); @@ -527,12 +531,14 @@ pub mod pallet { /// If the preconditions are verified, return /// a tuple containing the parsed name value and whether the name /// being banned is currently assigned to someone or not. - fn check_banning_preconditions(name_input: Web3NameInput) -> Result<(Web3NameOf, bool), DispatchError> { - let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; + fn check_banning_preconditions( + name_input: Web3NameInput, + ) -> Result<(Web3NameOf, bool), DispatchError> { + let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; - ensure!(!Banned::::contains_key(&name), Error::::AlreadyBanned); + ensure!(!Banned::::contains_key(&name), Error::::AlreadyBanned); - let is_claimed = Owner::::contains_key(&name); + let is_claimed = Owner::::contains_key(&name); Ok((name, is_claimed)) } @@ -540,18 +546,18 @@ pub mod pallet { /// Ban the provided name. This function must be called after /// `check_banning_preconditions` as it does not verify all the /// preconditions again. - pub(crate) fn ban_name(name: &Web3NameOf) { - Banned::::insert(name, ()); + pub(crate) fn ban_name(name: &Web3NameOf) { + Banned::::insert(name, ()); } /// Verify that the unbanning preconditions are verified. /// Specifically: /// - The name input data can be decoded as a valid name /// - The name must have already been banned - fn check_unbanning_preconditions(name_input: Web3NameInput) -> Result, DispatchError> { - let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; + fn check_unbanning_preconditions(name_input: Web3NameInput) -> Result, DispatchError> { + let name = Web3NameOf::::try_from(name_input.into_inner()).map_err(DispatchError::from)?; - ensure!(Banned::::contains_key(&name), Error::::NotBanned); + ensure!(Banned::::contains_key(&name), Error::::NotBanned); Ok(name) } @@ -559,20 +565,21 @@ pub mod pallet { /// Unban the provided name. This function must be called after /// `check_unbanning_preconditions` as it does not verify all the /// preconditions again. - fn unban_name(name: &Web3NameOf) { - Banned::::remove(name); + fn unban_name(name: &Web3NameOf) { + Banned::::remove(name); } } - pub(crate) struct Web3NameStorageDepositCollector(PhantomData); - impl StorageDepositCollector, T::Web3Name, T::RuntimeHoldReason> - for Web3NameStorageDepositCollector + pub(crate) struct Web3NameStorageDepositCollector(PhantomData<(T, I)>); + impl, I: 'static> + StorageDepositCollector, >::Web3Name, >::RuntimeHoldReason> + for Web3NameStorageDepositCollector { type Currency = T::Currency; - type Reason = HoldReason; + type Reason = HoldReason; fn get_hashed_key(key: &T::Web3Name) -> Result, DispatchError> { - Ok(Owner::::hashed_key_for(key)) + Ok(Owner::::hashed_key_for(key)) } fn reason() -> Self::Reason { @@ -581,7 +588,7 @@ pub mod pallet { fn deposit( key: &T::Web3Name, ) -> Result, >>::Balance>, DispatchError> { - let w3n_entry = Owner::::get(key).ok_or(Error::::NotFound)?; + let w3n_entry = Owner::::get(key).ok_or(Error::::NotFound)?; Ok(w3n_entry.deposit) } @@ -594,8 +601,8 @@ pub mod pallet { key: &T::Web3Name, deposit: Deposit, >>::Balance>, ) -> Result<(), DispatchError> { - let w3n_entry = Owner::::get(key).ok_or(Error::::NotFound)?; - Owner::::insert(key, Web3OwnershipOf:: { deposit, ..w3n_entry }); + let w3n_entry = Owner::::get(key).ok_or(Error::::NotFound)?; + Owner::::insert(key, Web3OwnershipOf:: { deposit, ..w3n_entry }); Ok(()) } diff --git a/pallets/pallet-web3-names/src/migrations.rs b/pallets/pallet-web3-names/src/migrations.rs index bdc75b20d..40aeeda41 100644 --- a/pallets/pallet-web3-names/src/migrations.rs +++ b/pallets/pallet-web3-names/src/migrations.rs @@ -26,11 +26,10 @@ use crate::{AccountIdOf, Config, CurrencyOf, Error, HoldReason, Owner, Web3NameO pub fn update_balance_for_w3n(key: &Web3NameOf) -> DispatchResult where - ::Currency: - ReservableCurrency::Currency as Inspect>>::Balance>, + T::Currency: ReservableCurrency>>::Balance>, { let details = Owner::::get(key).ok_or(Error::::NotFound)?; - switch_reserved_to_hold::, CurrencyOf>( + switch_reserved_to_hold::, CurrencyOf>( &details.deposit.owner, &HoldReason::Deposit.into(), details.deposit.amount, diff --git a/pallets/pallet-web3-names/src/mock.rs b/pallets/pallet-web3-names/src/mock.rs index a1d4baff3..7c69773c3 100644 --- a/pallets/pallet-web3-names/src/mock.rs +++ b/pallets/pallet-web3-names/src/mock.rs @@ -23,20 +23,20 @@ use crate::{ AccountIdOf, BalanceOf, Config, CurrencyOf, HoldReason, Names, Owner, Web3NameOf, Web3NameOwnerOf, Web3OwnershipOf, }; -pub(crate) fn insert_raw_w3n( +pub(crate) fn insert_raw_w3n, I: 'static>( payer: AccountIdOf, - owner: Web3NameOwnerOf, - name: Web3NameOf, + owner: Web3NameOwnerOf, + name: Web3NameOf, block_number: BlockNumberFor, - deposit: BalanceOf, + deposit: BalanceOf, ) { - CurrencyOf::::hold(&HoldReason::Deposit.into(), &payer, deposit) + CurrencyOf::::hold(&HoldReason::Deposit.into(), &payer, deposit) .expect("Payer should have enough funds for deposit"); - Names::::insert(&owner, name.clone()); - Owner::::insert( + Names::::insert(&owner, name.clone()); + Owner::::insert( &name, - Web3OwnershipOf:: { + Web3OwnershipOf:: { owner, claimed_at: block_number, deposit: Deposit { @@ -244,7 +244,7 @@ pub(crate) mod runtime { pub fn build_and_execute_with_sanity_tests(self, test: impl FnOnce()) { self.build().execute_with(|| { test(); - crate::try_state::do_try_state::().expect("Sanity test for w3n failed."); + crate::try_state::do_try_state::().expect("Sanity test for w3n failed."); }) } diff --git a/pallets/pallet-web3-names/src/tests/deposit.rs b/pallets/pallet-web3-names/src/tests/deposit.rs index e31fc1e03..e30bd2fe0 100644 --- a/pallets/pallet-web3-names/src/tests/deposit.rs +++ b/pallets/pallet-web3-names/src/tests/deposit.rs @@ -95,7 +95,7 @@ fn test_update_deposit() { ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, initial_balance)]) .build_and_execute_with_sanity_tests(|| { - insert_raw_w3n::( + insert_raw_w3n::( ACCOUNT_00, DID_00, web3_name_00.clone(), @@ -133,7 +133,7 @@ fn test_update_deposit_unauthorized() { ExtBuilder::default() .with_balances(vec![(ACCOUNT_00, initial_balance)]) .build_and_execute_with_sanity_tests(|| { - insert_raw_w3n::( + insert_raw_w3n::( ACCOUNT_00, DID_00, web3_name_00.clone(), diff --git a/pallets/pallet-web3-names/src/try_state.rs b/pallets/pallet-web3-names/src/try_state.rs index be4fc58d7..71b1bf177 100644 --- a/pallets/pallet-web3-names/src/try_state.rs +++ b/pallets/pallet-web3-names/src/try_state.rs @@ -23,12 +23,12 @@ use sp_runtime::TryRuntimeError; use crate::{Banned, Config, Names, Owner, Web3NameOf, Web3NameOwnerOf, Web3OwnershipOf}; -pub fn do_try_state() -> Result<(), TryRuntimeError> { +pub fn do_try_state, I: 'static>() -> Result<(), TryRuntimeError> { // check if for each owner there is a name stored. - Owner::::iter().try_for_each( - |(w3n, ownership): (Web3NameOf, Web3OwnershipOf)| -> Result<(), TryRuntimeError> { + Owner::::iter().try_for_each( + |(w3n, ownership): (Web3NameOf, Web3OwnershipOf)| -> Result<(), TryRuntimeError> { ensure!( - Names::::get(&ownership.owner) == Some(w3n.clone()), + Names::::get(&ownership.owner) == Some(w3n.clone()), log_and_return_error_message(format!( "Owned w3n from owner {:?} does not match with saved w3n {:?}", ownership.owner, w3n @@ -39,19 +39,19 @@ pub fn do_try_state() -> Result<(), TryRuntimeError> { )?; // check for each name there is an owner. - Names::::iter().try_for_each( - |(w3n_owner, w3n): (Web3NameOwnerOf, Web3NameOf)| -> Result<(), TryRuntimeError> { + Names::::iter().try_for_each( + |(w3n_owner, w3n): (Web3NameOwnerOf, Web3NameOf)| -> Result<(), TryRuntimeError> { ensure!( - Owner::::get(&w3n).expect("Unknown w3n").owner == w3n_owner, + Owner::::get(&w3n).expect("Unknown w3n").owner == w3n_owner, log_and_return_error_message(format!("Owner {:?} with w3n {:?} not found", w3n_owner, w3n)) ); Ok(()) }, )?; // a banned name should have no owner. - Banned::::iter_keys().try_for_each(|banned_w3n| -> Result<(), TryRuntimeError> { + Banned::::iter_keys().try_for_each(|banned_w3n| -> Result<(), TryRuntimeError> { ensure!( - !Owner::::contains_key(&banned_w3n), + !Owner::::contains_key(&banned_w3n), log_and_return_error_message(format!("Owner contains banned name {:?}", banned_w3n)) ); Ok(()) diff --git a/support/src/traits.rs b/support/src/traits.rs index c35ea5acf..2c6861f17 100644 --- a/support/src/traits.rs +++ b/support/src/traits.rs @@ -149,10 +149,8 @@ impl BalanceMigrationManager for () { pub trait StorageDepositCollector { type Currency: MutateHold; - // TODO: This could also be replaced with a `Borrow` or an - // `AsRef`, but not sure what trait the runtime composite - // enum implements. - type Reason: Into + Clone; + + type Reason: Into; /// Returns the hold reason for deposits taken by the deposit collector; fn reason() -> Self::Reason; @@ -216,7 +214,7 @@ pub trait StorageDepositCollector { let reason = Self::reason(); if is_key_migrated { - free_deposit::(&deposit, &reason.clone().into())?; + free_deposit::(&deposit, &Self::reason().into())?; } else { DepositBalanceMigrationManager::release_reserved_deposit(&deposit.owner, &deposit.amount); DepositBalanceMigrationManager::exclude_key_from_migration(&hashed_key); @@ -257,7 +255,7 @@ pub trait StorageDepositCollector { let is_key_migrated = DepositBalanceMigrationManager::is_key_migrated(&hashed_key); if is_key_migrated { - free_deposit::(&deposit, &reason.clone().into())?; + free_deposit::(&deposit, &Self::reason().into())?; } else { DepositBalanceMigrationManager::release_reserved_deposit(&deposit.owner, &deposit.amount); DepositBalanceMigrationManager::exclude_key_from_migration(&hashed_key);