From 74254e9c58fbe99afec0da3c7a68149c0f5b69c9 Mon Sep 17 00:00:00 2001 From: PraetorP Date: Thu, 13 Jul 2023 20:32:45 +0700 Subject: [PATCH 1/7] feat(pallet-ranked-collective): added types New associated types(`AddOrigin` & `RemoveOrigin`) have been added to `Config`. --- bin/node/runtime/src/lib.rs | 2 ++ frame/ranked-collective/src/benchmarking.rs | 12 ++++++------ frame/ranked-collective/src/lib.rs | 17 ++++++++++++----- frame/ranked-collective/src/tests.rs | 4 +++- primitives/runtime/src/traits.rs | 3 +++ 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 058f43dad00b1..b1c034512af92 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -925,6 +925,8 @@ impl pallet_referenda::Config for Runtime { impl pallet_ranked_collective::Config for Runtime { type WeightInfo = pallet_ranked_collective::weights::SubstrateWeight; type RuntimeEvent = RuntimeEvent; + type AddOrigin = EnsureRoot; + type RemoveOrigin = EnsureRootWithSuccess>; type PromoteOrigin = EnsureRootWithSuccess>; type DemoteOrigin = EnsureRootWithSuccess>; type Polls = RankedPolls; diff --git a/frame/ranked-collective/src/benchmarking.rs b/frame/ranked-collective/src/benchmarking.rs index b610d10009a08..74bf32b937769 100644 --- a/frame/ranked-collective/src/benchmarking.rs +++ b/frame/ranked-collective/src/benchmarking.rs @@ -37,8 +37,8 @@ fn make_member, I: 'static>(rank: Rank) -> T::AccountId { let who = account::("member", MemberCount::::get(0), SEED); let who_lookup = T::Lookup::unlookup(who.clone()); assert_ok!(Pallet::::add_member( - T::PromoteOrigin::try_successful_origin() - .expect("PromoteOrigin has no successful origin required for the benchmark"), + T::AddOrigin::try_successful_origin() + .expect("AddOrigin has no successful origin required for the benchmark"), who_lookup.clone(), )); for _ in 0..rank { @@ -56,7 +56,7 @@ benchmarks_instance_pallet! { let who = account::("member", 0, SEED); let who_lookup = T::Lookup::unlookup(who.clone()); let origin = - T::PromoteOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?; + T::AddOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?; let call = Call::::add_member { who: who_lookup }; }: { call.dispatch_bypass_filter(origin)? } verify { @@ -73,7 +73,7 @@ benchmarks_instance_pallet! { let last = make_member::(rank); let last_index = (0..=rank).map(|r| IdToIndex::::get(r, &last).unwrap()).collect::>(); let origin = - T::DemoteOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?; + T::RemoveOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?; let call = Call::::remove_member { who: who_lookup, min_rank: rank }; }: { call.dispatch_bypass_filter(origin)? } verify { @@ -124,8 +124,8 @@ benchmarks_instance_pallet! { let caller: T::AccountId = whitelisted_caller(); let caller_lookup = T::Lookup::unlookup(caller.clone()); assert_ok!(Pallet::::add_member( - T::PromoteOrigin::try_successful_origin() - .expect("PromoteOrigin has no successful origin required for the benchmark"), + T::AddOrigin::try_successful_origin() + .expect("AddOrigin has no successful origin required for the benchmark"), caller_lookup.clone(), )); // Create a poll diff --git a/frame/ranked-collective/src/lib.rs b/frame/ranked-collective/src/lib.rs index 6fe9fc221a7df..400c32024f1e1 100644 --- a/frame/ranked-collective/src/lib.rs +++ b/frame/ranked-collective/src/lib.rs @@ -382,12 +382,19 @@ pub mod pallet { type RuntimeEvent: From> + IsType<::RuntimeEvent>; - /// The origin required to add or promote a mmember. The success value indicates the + /// The origin required to add a member. + type AddOrigin: EnsureOrigin; + + /// The origin required to remove a member. The success value indicates the + /// maximum rank *from which* the removal may be. + type RemoveOrigin: EnsureOrigin; + + /// The origin required to promote a member. The success value indicates the /// maximum rank *to which* the promotion may be. type PromoteOrigin: EnsureOrigin; - /// The origin required to demote or remove a member. The success value indicates the - /// maximum rank *from which* the demotion/removal may be. + /// The origin required to demote a member. The success value indicates the + /// maximum rank *from which* the demotion may be. type DemoteOrigin: EnsureOrigin; /// The polling system used for our voting. @@ -490,7 +497,7 @@ pub mod pallet { #[pallet::call_index(0)] #[pallet::weight(T::WeightInfo::add_member())] pub fn add_member(origin: OriginFor, who: AccountIdLookupOf) -> DispatchResult { - let _ = T::PromoteOrigin::ensure_origin(origin)?; + T::AddOrigin::ensure_origin(origin)?; let who = T::Lookup::lookup(who)?; Self::do_add_member(who) } @@ -538,7 +545,7 @@ pub mod pallet { who: AccountIdLookupOf, min_rank: Rank, ) -> DispatchResultWithPostInfo { - let max_rank = T::DemoteOrigin::ensure_origin(origin)?; + let max_rank = T::RemoveOrigin::ensure_origin(origin)?; let who = T::Lookup::lookup(who)?; let MemberRecord { rank, .. } = Self::ensure_member(&who)?; ensure!(min_rank >= rank, Error::::InvalidWitness); diff --git a/frame/ranked-collective/src/tests.rs b/frame/ranked-collective/src/tests.rs index fb8dc00e76551..365854bf1f7fa 100644 --- a/frame/ranked-collective/src/tests.rs +++ b/frame/ranked-collective/src/tests.rs @@ -28,7 +28,7 @@ use frame_support::{ use sp_core::{Get, H256}; use sp_runtime::{ testing::Header, - traits::{BlakeTwo256, IdentityLookup, ReduceBy}, + traits::{BlakeTwo256, IdentityLookup, ReduceBy, ReplaceWithDefaultFor}, BuildStorage, }; @@ -182,6 +182,8 @@ parameter_types! { impl Config for Test { type WeightInfo = (); type RuntimeEvent = RuntimeEvent; + type AddOrigin = MapSuccess>; + type RemoveOrigin = Self::DemoteOrigin; type PromoteOrigin = EitherOf< // Root can promote arbitrarily. frame_system::EnsureRootWithSuccess>, diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 3aa27234fbce2..c0cef83ed289b 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -540,6 +540,9 @@ morph_types! { /// Morpher to disregard the source value and replace with another. pub type Replace = |_| -> V::Type { V::get() }; + /// Morpher to disregard the source value and replace with default for T. + pub type ReplaceWithDefaultFor = |_| -> T { T::default() }; + /// Mutator which reduces a scalar by a particular amount. pub type ReduceBy = |r: N::Type| -> N::Type { r.checked_sub(&N::get()).unwrap_or(Zero::zero()) From 4d4711b21abeeda77a1a0b7bf1c8f4bdf8ca3a76 Mon Sep 17 00:00:00 2001 From: PraetorP Date: Fri, 14 Jul 2023 18:24:06 +0700 Subject: [PATCH 2/7] doc(pallet-ranked-collective): update fn docs --- frame/ranked-collective/src/lib.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/frame/ranked-collective/src/lib.rs b/frame/ranked-collective/src/lib.rs index 7514d36ee04a0..00f96b906285a 100644 --- a/frame/ranked-collective/src/lib.rs +++ b/frame/ranked-collective/src/lib.rs @@ -489,9 +489,8 @@ pub mod pallet { impl, I: 'static> Pallet { /// Introduce a new member. /// - /// - `origin`: Must be the `AdminOrigin`. + /// - `origin`: Must be the `AddOrigin`. /// - `who`: Account of non-member which will become a member. - /// - `rank`: The rank to give the new member. /// /// Weight: `O(1)` #[pallet::call_index(0)] @@ -504,7 +503,7 @@ pub mod pallet { /// Increment the rank of an existing member by one. /// - /// - `origin`: Must be the `AdminOrigin`. + /// - `origin`: Must be the `PromoteOrigin`. /// - `who`: Account of existing member. /// /// Weight: `O(1)` @@ -519,7 +518,7 @@ pub mod pallet { /// Decrement the rank of an existing member by one. If the member is already at rank zero, /// then they are removed entirely. /// - /// - `origin`: Must be the `AdminOrigin`. + /// - `origin`: Must be the `DemoteOrigin`. /// - `who`: Account of existing member of rank greater than zero. /// /// Weight: `O(1)`, less if the member's index is highest in its rank. @@ -533,7 +532,7 @@ pub mod pallet { /// Remove the member entirely. /// - /// - `origin`: Must be the `AdminOrigin`. + /// - `origin`: Must be the `RemoveOrigin`. /// - `who`: Account of existing member of rank greater than zero. /// - `min_rank`: The rank of the member or greater. /// From b2ac5d1e6483e655b712749111285387846ff641 Mon Sep 17 00:00:00 2001 From: Pavel Orlov <45266194+PraetorP@users.noreply.github.com> Date: Fri, 14 Jul 2023 19:07:30 +0700 Subject: [PATCH 3/7] Update primitives/runtime/src/traits.rs Co-authored-by: Oliver Tale-Yazdi --- primitives/runtime/src/traits.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index b522aec6b3d68..90e26bf93c29f 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -540,8 +540,8 @@ morph_types! { /// Morpher to disregard the source value and replace with another. pub type Replace = |_| -> V::Type { V::get() }; - /// Morpher to disregard the source value and replace with default for T. - pub type ReplaceWithDefaultFor = |_| -> T { T::default() }; + /// Morpher to disregard the source value. + pub type Ignore = |_| -> () { () }; /// Mutator which reduces a scalar by a particular amount. pub type ReduceBy = |r: N::Type| -> N::Type { From 5d1b180a768f93b5f1e67ac7a93b49c4081fbc02 Mon Sep 17 00:00:00 2001 From: PraetorP Date: Fri, 14 Jul 2023 19:18:38 +0700 Subject: [PATCH 4/7] test(pallet-ranked-collective): switch to `Ignore` --- frame/ranked-collective/src/tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frame/ranked-collective/src/tests.rs b/frame/ranked-collective/src/tests.rs index 7504531edabe9..98644033e7cfb 100644 --- a/frame/ranked-collective/src/tests.rs +++ b/frame/ranked-collective/src/tests.rs @@ -27,7 +27,7 @@ use frame_support::{ }; use sp_core::{Get, H256}; use sp_runtime::{ - traits::{BlakeTwo256, IdentityLookup, ReduceBy, ReplaceWithDefaultFor}, + traits::{BlakeTwo256, IdentityLookup, Ignore, ReduceBy}, BuildStorage, }; @@ -176,7 +176,7 @@ parameter_types! { impl Config for Test { type WeightInfo = (); type RuntimeEvent = RuntimeEvent; - type AddOrigin = MapSuccess>; + type AddOrigin = MapSuccess; type RemoveOrigin = Self::DemoteOrigin; type PromoteOrigin = EitherOf< // Root can promote arbitrarily. From e1f8af40162f728d59993ab5f0c70fb1812195f6 Mon Sep 17 00:00:00 2001 From: Pavel Orlov <45266194+PraetorP@users.noreply.github.com> Date: Sat, 19 Aug 2023 19:18:50 +0700 Subject: [PATCH 5/7] Update primitives/runtime/src/traits.rs Co-authored-by: Keith Yeung --- primitives/runtime/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 90e26bf93c29f..59c011330e62a 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -541,7 +541,7 @@ morph_types! { pub type Replace = |_| -> V::Type { V::get() }; /// Morpher to disregard the source value. - pub type Ignore = |_| -> () { () }; + pub type Ignore = |_| {}; /// Mutator which reduces a scalar by a particular amount. pub type ReduceBy = |r: N::Type| -> N::Type { From 2261fb086420c6dcc4437e5ee8c4f213a7e1d2b0 Mon Sep 17 00:00:00 2001 From: PraetorP Date: Sat, 19 Aug 2023 13:21:51 +0000 Subject: [PATCH 6/7] Revert "Update primitives/runtime/src/traits.rs" This reverts commit e1f8af40162f728d59993ab5f0c70fb1812195f6. --- primitives/runtime/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 59c011330e62a..90e26bf93c29f 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -541,7 +541,7 @@ morph_types! { pub type Replace = |_| -> V::Type { V::get() }; /// Morpher to disregard the source value. - pub type Ignore = |_| {}; + pub type Ignore = |_| -> () { () }; /// Mutator which reduces a scalar by a particular amount. pub type ReduceBy = |r: N::Type| -> N::Type { From 9137fb30f42e5d4685af816f91d69b437ef8c08d Mon Sep 17 00:00:00 2001 From: Pavel Orlov <45266194+PraetorP@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:09:55 +0700 Subject: [PATCH 7/7] Update primitives/runtime/src/traits.rs Co-authored-by: Keith Yeung --- primitives/runtime/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/primitives/runtime/src/traits.rs b/primitives/runtime/src/traits.rs index 90e26bf93c29f..def3ea65a1167 100644 --- a/primitives/runtime/src/traits.rs +++ b/primitives/runtime/src/traits.rs @@ -541,7 +541,7 @@ morph_types! { pub type Replace = |_| -> V::Type { V::get() }; /// Morpher to disregard the source value. - pub type Ignore = |_| -> () { () }; + pub type Ignore = |_| -> () {}; /// Mutator which reduces a scalar by a particular amount. pub type ReduceBy = |r: N::Type| -> N::Type {