From 94a0c89064ed3a84dd49c4e91927e8001bc38c86 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Thu, 28 Dec 2023 15:49:28 +0100 Subject: [PATCH 1/9] wip --- substrate/frame/identity/src/lib.rs | 107 +++++++++++++++++++--------- 1 file changed, 74 insertions(+), 33 deletions(-) diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index 8588612cd5bc..16c414a35f93 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -83,7 +83,11 @@ use codec::Encode; use frame_support::{ ensure, pallet_prelude::{DispatchError, DispatchResult}, - traits::{BalanceStatus, Currency, Get, OnUnbalanced, ReservableCurrency}, + traits::{ + fungible::{Inspect, MutateHold}, + tokens::Precision, + BalanceStatus, Get, OnUnbalanced, + }, }; use sp_runtime::traits::{AppendZerosInput, Hash, Saturating, StaticLookup, Zero}; use sp_std::prelude::*; @@ -95,10 +99,9 @@ pub use types::{ }; type BalanceOf = - <::Currency as Currency<::AccountId>>::Balance; -type NegativeImbalanceOf = <::Currency as Currency< - ::AccountId, ->>::NegativeImbalance; + <::Currency as Inspect<::AccountId>>::Balance; +type NegativeImbalanceOf = + <::Currency as Inspect<::AccountId>>::NegativeImbalance; type AccountIdLookupOf = <::Lookup as StaticLookup>::Source; #[frame_support::pallet] @@ -113,7 +116,7 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The currency trait. - type Currency: ReservableCurrency; + type Currency: MutateHold; /// The amount held on deposit for a registered identity #[pallet::constant] @@ -152,6 +155,16 @@ pub mod pallet { /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; + + /// The overarching hold reason. + type RuntimeHoldReason: From; + } + + /// The reasons for the pallet identity placing holds on funds. + #[pallet::composite_enum] + pub enum HoldReason { + IdentityDeposit, + RegistrarFee, } #[pallet::pallet] @@ -352,11 +365,20 @@ pub mod pallet { let old_deposit = id.deposit; id.deposit = T::BasicDeposit::get().saturating_add(byte_deposit); if id.deposit > old_deposit { - T::Currency::reserve(&sender, id.deposit - old_deposit)?; + T::Currency::hold( + &HoldReason::IdentityDeposit.into(), + &sender, + id.deposit - old_deposit, + )?; } if old_deposit > id.deposit { - let err_amount = T::Currency::unreserve(&sender, old_deposit - id.deposit); - debug_assert!(err_amount.is_zero()); + T::Currency::release( + &HoldReason::IdentityDeposit.into(), + &sender, + old_deposit - id.deposit, + Precision::Exact, + ) + .unwrap(); } let judgements = id.judgements.len(); @@ -404,10 +426,19 @@ pub mod pallet { ensure!(not_other_sub, Error::::AlreadyClaimed); if old_deposit < new_deposit { - T::Currency::reserve(&sender, new_deposit - old_deposit)?; + T::Currency::hold( + &HoldReason::IdentityDeposit.into(), + &sender, + new_deposit - old_deposit, + )?; } else if old_deposit > new_deposit { - let err_amount = T::Currency::unreserve(&sender, old_deposit - new_deposit); - debug_assert!(err_amount.is_zero()); + T::Currency::release( + &HoldReason::IdentityDeposit.into(), + &sender, + old_deposit - new_deposit, + Precision::Exact, + ) + .unwrap(); } // do nothing if they're equal. @@ -458,8 +489,13 @@ pub mod pallet { >::remove(sub); } - let err_amount = T::Currency::unreserve(&sender, deposit); - debug_assert!(err_amount.is_zero()); + T::Currency::release( + &HoldReason::IdentityDeposit.into(), + &sender, + deposit, + Precision::Exact, + ) + .unwrap(); Self::deposit_event(Event::IdentityCleared { who: sender, deposit }); @@ -515,7 +551,7 @@ pub mod pallet { id.judgements.try_insert(i, item).map_err(|_| Error::::TooManyRegistrars)?, } - T::Currency::reserve(&sender, registrar.fee)?; + T::Currency::hold(&HoldReason::RegistrarFee.into(), &sender, registrar.fee)?; let judgements = id.judgements.len(); >::insert(&sender, id); @@ -557,8 +593,8 @@ pub mod pallet { return Err(Error::::JudgementGiven.into()) }; - let err_amount = T::Currency::unreserve(&sender, fee); - debug_assert!(err_amount.is_zero()); + T::Currency::release(&HoldReason::RegistrarFee.into(), &sender, fee, Precision::Exact) + .unwrap(); let judgements = id.judgements.len(); >::insert(&sender, id); @@ -707,13 +743,8 @@ pub mod pallet { match id.judgements.binary_search_by_key(®_index, |x| x.0) { Ok(position) => { if let Judgement::FeePaid(fee) = id.judgements[position].1 { - T::Currency::repatriate_reserved( - &target, - &sender, - fee, - BalanceStatus::Free, - ) - .map_err(|_| Error::::JudgementPaymentFailed)?; + T::Currency::transfer_on_hold(&target, &sender, fee, BalanceStatus::Free) + .map_err(|_| Error::::JudgementPaymentFailed)?; } id.judgements[position] = item }, @@ -800,7 +831,7 @@ pub mod pallet { Error::::TooManySubAccounts ); let deposit = T::SubAccountDeposit::get(); - T::Currency::reserve(&sender, deposit)?; + T::Currency::hold(&HoldReason::IdentityDeposit.into(), &sender, deposit)?; SuperOf::::insert(&sub, (sender.clone(), data)); sub_ids.try_push(sub.clone()).expect("sub ids length checked above; qed"); @@ -850,8 +881,13 @@ pub mod pallet { sub_ids.retain(|x| x != &sub); let deposit = T::SubAccountDeposit::get().min(*subs_deposit); *subs_deposit -= deposit; - let err_amount = T::Currency::unreserve(&sender, deposit); - debug_assert!(err_amount.is_zero()); + T::Currency::release( + &HoldReason::IdentityDeposit.into(), + &sender, + deposit, + Precision::Exact, + ) + .unwrap(); Self::deposit_event(Event::SubIdentityRemoved { sub, main: sender, deposit }); }); Ok(()) @@ -911,10 +947,15 @@ impl Pallet { new: BalanceOf, ) -> DispatchResult { if new > current { - T::Currency::reserve(who, new - current)?; + T::Currency::hold(&HoldReason::IdentityDeposit.into(), who, new - current)?; } else if new < current { - let err_amount = T::Currency::unreserve(who, current - new); - debug_assert!(err_amount.is_zero()); + T::Currency::release( + &HoldReason::IdentityDeposit.into(), + who, + current - new, + Precision::Exact, + ) + .unwrap(); } Ok(()) } @@ -954,10 +995,10 @@ impl Pallet { >::remove(sub); } - // unreserve any deposits + // release any deposits let deposit = id.total_deposit().saturating_add(subs_deposit); - let err_amount = T::Currency::unreserve(&who, deposit); - debug_assert!(err_amount.is_zero()); + T::Currency::release(&HoldReason::IdentityDeposit.into(), &who, deposit, Precision::Exact) + .unwrap(); Ok((registrars, encoded_byte_size, actual_subs)) } From 30f0e6af3bc7e1b40658adcd01924f763538d7ae Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Tue, 2 Jan 2024 09:24:47 -0300 Subject: [PATCH 2/9] wip --- substrate/frame/identity/src/lib.rs | 48 +++++++++++++++++------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index 16c414a35f93..774d13e676ce 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -372,13 +372,13 @@ pub mod pallet { )?; } if old_deposit > id.deposit { - T::Currency::release( + let result = T::Currency::release( &HoldReason::IdentityDeposit.into(), &sender, old_deposit - id.deposit, Precision::Exact, - ) - .unwrap(); + ); + debug_assert!(result.is_ok()); } let judgements = id.judgements.len(); @@ -432,13 +432,13 @@ pub mod pallet { new_deposit - old_deposit, )?; } else if old_deposit > new_deposit { - T::Currency::release( + let result = T::Currency::release( &HoldReason::IdentityDeposit.into(), &sender, old_deposit - new_deposit, Precision::Exact, - ) - .unwrap(); + ); + debug_assert!(result.is_ok()); } // do nothing if they're equal. @@ -489,13 +489,13 @@ pub mod pallet { >::remove(sub); } - T::Currency::release( + let result = T::Currency::release( &HoldReason::IdentityDeposit.into(), &sender, deposit, Precision::Exact, - ) - .unwrap(); + ); + debug_assert!(result.is_ok()); Self::deposit_event(Event::IdentityCleared { who: sender, deposit }); @@ -593,8 +593,13 @@ pub mod pallet { return Err(Error::::JudgementGiven.into()) }; - T::Currency::release(&HoldReason::RegistrarFee.into(), &sender, fee, Precision::Exact) - .unwrap(); + let result = T::Currency::release( + &HoldReason::RegistrarFee.into(), + &sender, + fee, + Precision::Exact, + ); + debug_assert!(result.is_ok()); let judgements = id.judgements.len(); >::insert(&sender, id); @@ -881,13 +886,13 @@ pub mod pallet { sub_ids.retain(|x| x != &sub); let deposit = T::SubAccountDeposit::get().min(*subs_deposit); *subs_deposit -= deposit; - T::Currency::release( + let result = T::Currency::release( &HoldReason::IdentityDeposit.into(), &sender, deposit, Precision::Exact, - ) - .unwrap(); + ); + debug_assert!(result.is_ok()); Self::deposit_event(Event::SubIdentityRemoved { sub, main: sender, deposit }); }); Ok(()) @@ -949,13 +954,13 @@ impl Pallet { if new > current { T::Currency::hold(&HoldReason::IdentityDeposit.into(), who, new - current)?; } else if new < current { - T::Currency::release( + let result = T::Currency::release( &HoldReason::IdentityDeposit.into(), who, current - new, Precision::Exact, - ) - .unwrap(); + ); + debug_assert!(result.is_ok()); } Ok(()) } @@ -997,8 +1002,13 @@ impl Pallet { // release any deposits let deposit = id.total_deposit().saturating_add(subs_deposit); - T::Currency::release(&HoldReason::IdentityDeposit.into(), &who, deposit, Precision::Exact) - .unwrap(); + let result = T::Currency::release( + &HoldReason::IdentityDeposit.into(), + &who, + deposit, + Precision::Exact, + ); + debug_assert!(result.is_ok()); Ok((registrars, encoded_byte_size, actual_subs)) } From 2f2dc7c66e4f84764160a184f0bb7b24ee23aaf1 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Thu, 4 Jan 2024 06:30:51 -0300 Subject: [PATCH 3/9] refactor pallet --- substrate/frame/identity/src/lib.rs | 48 +++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index 774d13e676ce..9fdcd5e174ae 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -84,9 +84,9 @@ use frame_support::{ ensure, pallet_prelude::{DispatchError, DispatchResult}, traits::{ - fungible::{Inspect, MutateHold}, + fungible::{Credit, Inspect, MutateHold}, tokens::Precision, - BalanceStatus, Get, OnUnbalanced, + Get, Imbalance, OnUnbalanced, }, }; use sp_runtime::traits::{AppendZerosInput, Hash, Saturating, StaticLookup, Zero}; @@ -100,14 +100,19 @@ pub use types::{ type BalanceOf = <::Currency as Inspect<::AccountId>>::Balance; -type NegativeImbalanceOf = - <::Currency as Inspect<::AccountId>>::NegativeImbalance; +type CreditOf = Credit<::AccountId, ::Currency>; type AccountIdLookupOf = <::Lookup as StaticLookup>::Source; #[frame_support::pallet] pub mod pallet { use super::*; - use frame_support::pallet_prelude::*; + use frame_support::{ + pallet_prelude::*, + traits::{ + fungible::BalancedHold, + tokens::{Fortitude, Restriction}, + }, + }; use frame_system::pallet_prelude::*; #[pallet::config] @@ -116,7 +121,9 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The currency trait. - type Currency: MutateHold; + type Currency: MutateHold + + BalancedHold + + Imbalance>; /// The amount held on deposit for a registered identity #[pallet::constant] @@ -145,7 +152,7 @@ pub mod pallet { type MaxRegistrars: Get; /// What to do with slashed funds. - type Slashed: OnUnbalanced>; + type Slashed: OnUnbalanced>; /// The origin which may forcibly set or remove a name. Root can always do this. type ForceOrigin: EnsureOrigin; @@ -748,8 +755,16 @@ pub mod pallet { match id.judgements.binary_search_by_key(®_index, |x| x.0) { Ok(position) => { if let Judgement::FeePaid(fee) = id.judgements[position].1 { - T::Currency::transfer_on_hold(&target, &sender, fee, BalanceStatus::Free) - .map_err(|_| Error::::JudgementPaymentFailed)?; + T::Currency::transfer_on_hold( + &HoldReason::RegistrarFee.into(), + &target, + &sender, + fee, + Precision::BestEffort, + Restriction::Free, + Fortitude::Polite, + ) + .map_err(|_| Error::::JudgementPaymentFailed)?; } id.judgements[position] = item }, @@ -799,7 +814,9 @@ pub mod pallet { >::remove(sub); } // Slash their deposit from them. - T::Slashed::on_unbalanced(T::Currency::slash_reserved(&target, deposit).0); + T::Slashed::on_unbalanced( + T::Currency::slash(&HoldReason::IdentityDeposit.into(), &target, deposit).0, + ); Self::deposit_event(Event::IdentityKilled { who: target, deposit }); @@ -917,8 +934,15 @@ pub mod pallet { sub_ids.retain(|x| x != &sender); let deposit = T::SubAccountDeposit::get().min(*subs_deposit); *subs_deposit -= deposit; - let _ = - T::Currency::repatriate_reserved(&sup, &sender, deposit, BalanceStatus::Free); + let _ = T::Currency::transfer_on_hold( + &HoldReason::IdentityDeposit.into(), + &sup, + &sender, + deposit, + Precision::BestEffort, + Restriction::Free, + Fortitude::Polite, + ); Self::deposit_event(Event::SubIdentityRevoked { sub: sender, main: sup.clone(), From e052e4270c78f04f5e6f274e8e06c402cb36d619 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Thu, 4 Jan 2024 06:57:32 -0300 Subject: [PATCH 4/9] make tests compile --- substrate/frame/identity/src/lib.rs | 5 ++--- substrate/frame/identity/src/tests.rs | 9 +++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index 9fdcd5e174ae..685cfa0ae8a4 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -86,7 +86,7 @@ use frame_support::{ traits::{ fungible::{Credit, Inspect, MutateHold}, tokens::Precision, - Get, Imbalance, OnUnbalanced, + Get, OnUnbalanced, }, }; use sp_runtime::traits::{AppendZerosInput, Hash, Saturating, StaticLookup, Zero}; @@ -122,8 +122,7 @@ pub mod pallet { /// The currency trait. type Currency: MutateHold - + BalancedHold - + Imbalance>; + + BalancedHold; /// The amount held on deposit for a registered identity #[pallet::constant] diff --git a/substrate/frame/identity/src/tests.rs b/substrate/frame/identity/src/tests.rs index 8ac7b4d66cb6..26e54fd9e9ff 100644 --- a/substrate/frame/identity/src/tests.rs +++ b/substrate/frame/identity/src/tests.rs @@ -26,7 +26,7 @@ use crate::{ use codec::{Decode, Encode}; use frame_support::{ assert_noop, assert_ok, derive_impl, ord_parameter_types, parameter_types, - traits::{ConstU32, ConstU64, EitherOfDiverse, Get}, + traits::{fungible::Mutate, ConstU32, ConstU64, EitherOfDiverse, Get}, BoundedVec, }; use frame_system::{EnsureRoot, EnsureSignedBy}; @@ -43,7 +43,7 @@ frame_support::construct_runtime!( { System: frame_system::{Pallet, Call, Config, Storage, Event}, Balances: pallet_balances::{Pallet, Call, Storage, Config, Event}, - Identity: pallet_identity::{Pallet, Call, Storage, Event}, + Identity: pallet_identity::{Pallet, Call, Storage, Event, HoldReason}, } ); @@ -86,7 +86,7 @@ impl pallet_balances::Config for Test { type WeightInfo = (); type FreezeIdentifier = (); type MaxFreezes = (); - type RuntimeHoldReason = (); + type RuntimeHoldReason = RuntimeHoldReason; type RuntimeFreezeReason = (); type MaxHolds = (); } @@ -115,6 +115,7 @@ impl pallet_identity::Config for Test { type RegistrarOrigin = EnsureOneOrRoot; type ForceOrigin = EnsureTwoOrRoot; type WeightInfo = (); + type RuntimeHoldReason = RuntimeHoldReason; } pub fn new_test_ext() -> sp_io::TestExternalities { @@ -641,7 +642,7 @@ fn provide_judgement_should_return_judgement_payment_failed_error() { assert_eq!(Balances::free_balance(10), 1000 - id_deposit - 10); // This forces judgement payment failed error - Balances::make_free_balance_be(&3, 0); + Balances::set_balance(&3, 0); assert_noop!( Identity::provide_judgement( RuntimeOrigin::signed(3), From 35ad039b2a21dbdd37cd2dd61514cdd6d8fa9b46 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Thu, 4 Jan 2024 07:31:47 -0300 Subject: [PATCH 5/9] fix tests --- substrate/frame/identity/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substrate/frame/identity/src/tests.rs b/substrate/frame/identity/src/tests.rs index 26e54fd9e9ff..e5a969602b14 100644 --- a/substrate/frame/identity/src/tests.rs +++ b/substrate/frame/identity/src/tests.rs @@ -88,7 +88,7 @@ impl pallet_balances::Config for Test { type MaxFreezes = (); type RuntimeHoldReason = RuntimeHoldReason; type RuntimeFreezeReason = (); - type MaxHolds = (); + type MaxHolds = ConstU32<2>; } parameter_types! { From 549eaf4b18af08c159929b4b4c413d21e0309e4e Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Fri, 5 Jan 2024 07:07:25 -0300 Subject: [PATCH 6/9] fix tests --- substrate/frame/identity/src/tests.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/substrate/frame/identity/src/tests.rs b/substrate/frame/identity/src/tests.rs index e5a969602b14..0709647ef6d4 100644 --- a/substrate/frame/identity/src/tests.rs +++ b/substrate/frame/identity/src/tests.rs @@ -26,7 +26,7 @@ use crate::{ use codec::{Decode, Encode}; use frame_support::{ assert_noop, assert_ok, derive_impl, ord_parameter_types, parameter_types, - traits::{fungible::Mutate, ConstU32, ConstU64, EitherOfDiverse, Get}, + traits::{ConstU32, ConstU64, EitherOfDiverse, Get, StoredMap}, BoundedVec, }; use frame_system::{EnsureRoot, EnsureSignedBy}; @@ -641,8 +641,12 @@ fn provide_judgement_should_return_judgement_payment_failed_error() { // 10 for the judgement request and the deposit for the identity. assert_eq!(Balances::free_balance(10), 1000 - id_deposit - 10); - // This forces judgement payment failed error - Balances::set_balance(&3, 0); + // This forces judgement payment failed error, as `transfer_on_hold` will fail with + // `CannotCreate` when the attempt to transfer to `3` overflows. + let _ = ::AccountStore::mutate(&3, |account| { + account.free = ::Balance::max_value(); + Ok::<(), ()>(()) + }); assert_noop!( Identity::provide_judgement( RuntimeOrigin::signed(3), From 985bae2c0012e489c02667854123a8807e55996d Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Fri, 5 Jan 2024 10:06:44 -0300 Subject: [PATCH 7/9] make benchmarks compile --- substrate/frame/identity/src/benchmarking.rs | 28 ++++++++++---------- substrate/frame/identity/src/lib.rs | 7 +++++ 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/substrate/frame/identity/src/benchmarking.rs b/substrate/frame/identity/src/benchmarking.rs index 3d976bd6c881..72e110a3a379 100644 --- a/substrate/frame/identity/src/benchmarking.rs +++ b/substrate/frame/identity/src/benchmarking.rs @@ -43,7 +43,7 @@ fn add_registrars(r: u32) -> Result<(), &'static str> { for i in 0..r { let registrar: T::AccountId = account("registrar", i, SEED); let registrar_lookup = T::Lookup::unlookup(registrar.clone()); - let _ = T::Currency::make_free_balance_be(®istrar, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(®istrar, BalanceOf::::max_value()); let registrar_origin = T::RegistrarOrigin::try_successful_origin() .expect("RegistrarOrigin has no successful origin required for the benchmark"); Identity::::add_registrar(registrar_origin, registrar_lookup)?; @@ -73,7 +73,7 @@ fn create_sub_accounts( // Set identity so `set_subs` does not fail. if IdentityOf::::get(who).is_none() { - let _ = T::Currency::make_free_balance_be(who, BalanceOf::::max_value() / 2u32.into()); + let _ = T::Currency::set_balance(who, BalanceOf::::max_value() / 2u32.into()); let info = T::IdentityInformation::create_identity_info(); Identity::::set_identity(who_origin.into(), Box::new(info))?; } @@ -122,7 +122,7 @@ mod benchmarks { let caller_lookup = T::Lookup::unlookup(caller.clone()); let caller_origin: ::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); - let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); // Add an initial identity let initial_info = T::IdentityInformation::create_identity_info(); @@ -133,7 +133,7 @@ mod benchmarks { let registrar: T::AccountId = account("registrar", i, SEED); let _ = T::Lookup::unlookup(registrar.clone()); let balance_to_use = T::Currency::minimum_balance() * 10u32.into(); - let _ = T::Currency::make_free_balance_be(®istrar, balance_to_use); + let _ = T::Currency::set_balance(®istrar, balance_to_use); Identity::::request_judgement(caller_origin.clone(), i, 10u32.into())?; Identity::::provide_judgement( @@ -200,7 +200,7 @@ mod benchmarks { let caller_origin = ::RuntimeOrigin::from(RawOrigin::Signed(caller.clone())); let caller_lookup = ::unlookup(caller.clone()); - let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); // Register the registrars add_registrars::(r)?; @@ -216,7 +216,7 @@ mod benchmarks { for i in 0..r { let registrar: T::AccountId = account("registrar", i, SEED); let balance_to_use = T::Currency::minimum_balance() * 10u32.into(); - let _ = T::Currency::make_free_balance_be(®istrar, balance_to_use); + let _ = T::Currency::set_balance(®istrar, balance_to_use); Identity::::request_judgement(caller_origin.clone(), i, 10u32.into())?; Identity::::provide_judgement( @@ -240,7 +240,7 @@ mod benchmarks { #[benchmark] fn request_judgement(r: Linear<1, { T::MaxRegistrars::get() }>) -> Result<(), BenchmarkError> { let caller: T::AccountId = whitelisted_caller(); - let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); // Register the registrars add_registrars::(r)?; @@ -264,7 +264,7 @@ mod benchmarks { #[benchmark] fn cancel_request(r: Linear<1, { T::MaxRegistrars::get() }>) -> Result<(), BenchmarkError> { let caller: T::AccountId = whitelisted_caller(); - let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); // Register the registrars add_registrars::(r)?; @@ -317,7 +317,7 @@ mod benchmarks { fn set_account_id(r: Linear<1, { T::MaxRegistrars::get() - 1 }>) -> Result<(), BenchmarkError> { let caller: T::AccountId = whitelisted_caller(); let caller_lookup = T::Lookup::unlookup(caller.clone()); - let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); add_registrars::(r)?; @@ -346,7 +346,7 @@ mod benchmarks { fn set_fields(r: Linear<1, { T::MaxRegistrars::get() - 1 }>) -> Result<(), BenchmarkError> { let caller: T::AccountId = whitelisted_caller(); let caller_lookup = T::Lookup::unlookup(caller.clone()); - let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); add_registrars::(r)?; @@ -382,11 +382,11 @@ mod benchmarks { let user_origin = ::RuntimeOrigin::from(RawOrigin::Signed(user.clone())); let user_lookup = ::unlookup(user.clone()); - let _ = T::Currency::make_free_balance_be(&user, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&user, BalanceOf::::max_value()); let caller: T::AccountId = whitelisted_caller(); let caller_lookup = T::Lookup::unlookup(caller.clone()); - let _ = T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); add_registrars::(r)?; @@ -420,7 +420,7 @@ mod benchmarks { let target_origin: ::RuntimeOrigin = RawOrigin::Signed(target.clone()).into(); let target_lookup = T::Lookup::unlookup(target.clone()); - let _ = T::Currency::make_free_balance_be(&target, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&target, BalanceOf::::max_value()); let info = T::IdentityInformation::create_identity_info(); Identity::::set_identity(target_origin.clone(), Box::new(info.clone()))?; @@ -430,7 +430,7 @@ mod benchmarks { for i in 0..r { let registrar: T::AccountId = account("registrar", i, SEED); let balance_to_use = T::Currency::minimum_balance() * 10u32.into(); - let _ = T::Currency::make_free_balance_be(®istrar, balance_to_use); + let _ = T::Currency::set_balance(®istrar, balance_to_use); Identity::::request_judgement(target_origin.clone(), i, 10u32.into())?; Identity::::provide_judgement( diff --git a/substrate/frame/identity/src/lib.rs b/substrate/frame/identity/src/lib.rs index 685cfa0ae8a4..dedd7ac1a828 100644 --- a/substrate/frame/identity/src/lib.rs +++ b/substrate/frame/identity/src/lib.rs @@ -80,6 +80,8 @@ mod types; pub mod weights; use codec::Encode; +#[cfg(feature = "runtime-benchmarks")] +use frame_support::traits::fungible::Mutate; use frame_support::{ ensure, pallet_prelude::{DispatchError, DispatchResult}, @@ -121,8 +123,13 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; /// The currency trait. + #[cfg(not(feature = "runtime-benchmarks"))] type Currency: MutateHold + BalancedHold; + #[cfg(feature = "runtime-benchmarks")] + type Currency: MutateHold + + BalancedHold + + Mutate; /// The amount held on deposit for a registered identity #[pallet::constant] From fe9d80112dbdbc53c177255a7d1cc31105fe2947 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Mon, 8 Jan 2024 07:43:07 -0300 Subject: [PATCH 8/9] fix benchamrk tests --- substrate/frame/identity/src/benchmarking.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/substrate/frame/identity/src/benchmarking.rs b/substrate/frame/identity/src/benchmarking.rs index 72e110a3a379..73eedd4ab65b 100644 --- a/substrate/frame/identity/src/benchmarking.rs +++ b/substrate/frame/identity/src/benchmarking.rs @@ -122,7 +122,7 @@ mod benchmarks { let caller_lookup = T::Lookup::unlookup(caller.clone()); let caller_origin: ::RuntimeOrigin = RawOrigin::Signed(caller.clone()).into(); - let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value() / 2u32.into()); // Add an initial identity let initial_info = T::IdentityInformation::create_identity_info(); @@ -240,7 +240,7 @@ mod benchmarks { #[benchmark] fn request_judgement(r: Linear<1, { T::MaxRegistrars::get() }>) -> Result<(), BenchmarkError> { let caller: T::AccountId = whitelisted_caller(); - let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value() / 2u32.into()); // Register the registrars add_registrars::(r)?; @@ -264,7 +264,7 @@ mod benchmarks { #[benchmark] fn cancel_request(r: Linear<1, { T::MaxRegistrars::get() }>) -> Result<(), BenchmarkError> { let caller: T::AccountId = whitelisted_caller(); - let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value() / 2u32.into()); // Register the registrars add_registrars::(r)?; @@ -382,11 +382,11 @@ mod benchmarks { let user_origin = ::RuntimeOrigin::from(RawOrigin::Signed(user.clone())); let user_lookup = ::unlookup(user.clone()); - let _ = T::Currency::set_balance(&user, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&user, BalanceOf::::max_value() / 2u32.into()); let caller: T::AccountId = whitelisted_caller(); let caller_lookup = T::Lookup::unlookup(caller.clone()); - let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&caller, BalanceOf::::max_value() / 2u32.into()); add_registrars::(r)?; @@ -420,7 +420,7 @@ mod benchmarks { let target_origin: ::RuntimeOrigin = RawOrigin::Signed(target.clone()).into(); let target_lookup = T::Lookup::unlookup(target.clone()); - let _ = T::Currency::set_balance(&target, BalanceOf::::max_value()); + let _ = T::Currency::set_balance(&target, BalanceOf::::max_value() / 2u32.into()); let info = T::IdentityInformation::create_identity_info(); Identity::::set_identity(target_origin.clone(), Box::new(info.clone()))?; From b454b3aca66327285368cc5bb64222fbd4f910f2 Mon Sep 17 00:00:00 2001 From: Juan Girini Date: Mon, 8 Jan 2024 08:06:57 -0300 Subject: [PATCH 9/9] add holdreason to pallet impl --- cumulus/parachains/runtimes/people/people-rococo/src/people.rs | 1 + cumulus/parachains/runtimes/people/people-westend/src/people.rs | 1 + polkadot/runtime/common/src/integration_tests.rs | 1 + polkadot/runtime/rococo/src/lib.rs | 1 + polkadot/runtime/westend/src/lib.rs | 1 + substrate/bin/node/runtime/src/lib.rs | 1 + substrate/frame/alliance/src/mock.rs | 1 + 7 files changed, 7 insertions(+) diff --git a/cumulus/parachains/runtimes/people/people-rococo/src/people.rs b/cumulus/parachains/runtimes/people/people-rococo/src/people.rs index 95b14a544d55..9586ed52cf18 100644 --- a/cumulus/parachains/runtimes/people/people-rococo/src/people.rs +++ b/cumulus/parachains/runtimes/people/people-rococo/src/people.rs @@ -52,6 +52,7 @@ impl pallet_identity::Config for Runtime { type ForceOrigin = EnsureRoot; type RegistrarOrigin = EnsureRoot; type WeightInfo = weights::pallet_identity::WeightInfo; + type RuntimeHoldReason = RuntimeHoldReason; } /// The fields that we use to identify the owner of an account with. Each corresponds to a field diff --git a/cumulus/parachains/runtimes/people/people-westend/src/people.rs b/cumulus/parachains/runtimes/people/people-westend/src/people.rs index 202e85bb62bc..ce98363b6604 100644 --- a/cumulus/parachains/runtimes/people/people-westend/src/people.rs +++ b/cumulus/parachains/runtimes/people/people-westend/src/people.rs @@ -52,6 +52,7 @@ impl pallet_identity::Config for Runtime { type ForceOrigin = EnsureRoot; type RegistrarOrigin = EnsureRoot; type WeightInfo = weights::pallet_identity::WeightInfo; + type RuntimeHoldReason = RuntimeHoldReason; } /// The fields that we use to identify the owner of an account with. Each corresponds to a field diff --git a/polkadot/runtime/common/src/integration_tests.rs b/polkadot/runtime/common/src/integration_tests.rs index 4870432d22f9..1ce37f973efa 100644 --- a/polkadot/runtime/common/src/integration_tests.rs +++ b/polkadot/runtime/common/src/integration_tests.rs @@ -294,6 +294,7 @@ impl pallet_identity::Config for Test { type RegistrarOrigin = EnsureRoot; type ForceOrigin = EnsureRoot; type WeightInfo = (); + type RuntimeHoldReason = RuntimeHoldReason; } impl identity_migrator::Config for Test { diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 4d0e537f6dbd..4759a975fe5d 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -669,6 +669,7 @@ impl pallet_identity::Config for Runtime { type ForceOrigin = EitherOf, GeneralAdmin>; type RegistrarOrigin = EitherOf, GeneralAdmin>; type WeightInfo = weights::pallet_identity::WeightInfo; + type RuntimeHoldReason = RuntimeHoldReason; } impl pallet_utility::Config for Runtime { diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index fb54bec509b3..6c0485c53547 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -892,6 +892,7 @@ impl pallet_identity::Config for Runtime { type ForceOrigin = EitherOf, GeneralAdmin>; type RegistrarOrigin = EitherOf, GeneralAdmin>; type WeightInfo = weights::pallet_identity::WeightInfo; + type RuntimeHoldReason = RuntimeHoldReason; } impl pallet_utility::Config for Runtime { diff --git a/substrate/bin/node/runtime/src/lib.rs b/substrate/bin/node/runtime/src/lib.rs index 4d409a791ba2..d895daad0f79 100644 --- a/substrate/bin/node/runtime/src/lib.rs +++ b/substrate/bin/node/runtime/src/lib.rs @@ -1496,6 +1496,7 @@ impl pallet_identity::Config for Runtime { type ForceOrigin = EnsureRootOrHalfCouncil; type RegistrarOrigin = EnsureRootOrHalfCouncil; type WeightInfo = pallet_identity::weights::SubstrateWeight; + type RuntimeHoldReason = RuntimeHoldReason; } parameter_types! { diff --git a/substrate/frame/alliance/src/mock.rs b/substrate/frame/alliance/src/mock.rs index 01e0e01fe7ec..9261c917e7b1 100644 --- a/substrate/frame/alliance/src/mock.rs +++ b/substrate/frame/alliance/src/mock.rs @@ -125,6 +125,7 @@ impl pallet_identity::Config for Test { type RegistrarOrigin = EnsureOneOrRoot; type ForceOrigin = EnsureTwoOrRoot; type WeightInfo = (); + type RuntimeHoldReason = RuntimeHoldReason; } pub struct AllianceIdentityVerifier;