Skip to content

Commit c552fb5

Browse files
ggwpezPraetorP
andauthored
Ranked collective Add+Remove origins (#3212)
Superseeds #1245 This PR is a migration of the paritytech/substrate#14577. The PR added associated types (`AddOrigin` & `RemoveOrigin`) to `Config`. It allows you to decouple types and areas of responsibility, since at the moment the same types are responsible for adding and promoting(removing and demoting). This will improve the flexibility of the pallet configuration. ``` /// The origin required to add a member. type AddOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = ()>; /// The origin required to remove a member. The success value indicates the /// maximum rank *from which* the removal may be. type RemoveOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Rank>; ``` To achieve the backward compatibility, the users of the pallet can use the old type via the new morph: ``` type AddOrigin = MapSuccess<Self::PromoteOrigin, Ignore>; type RemoveOrigin = Self::DemoteOrigin; ``` --------- Signed-off-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: PraetorP <[email protected]> Co-authored-by: Pavel Orlov <[email protected]>
1 parent 7df1ae3 commit c552fb5

File tree

11 files changed

+72
-36
lines changed

11 files changed

+72
-36
lines changed

cumulus/parachains/runtimes/collectives/collectives-westend/src/ambassador/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use origins::pallet_origins::{
4040
EnsureAmbassadorsVoice, EnsureAmbassadorsVoiceFrom, EnsureHeadAmbassadorsVoice, Origin,
4141
};
4242
use sp_core::ConstU128;
43-
use sp_runtime::traits::{CheckedReduceBy, ConstU16, ConvertToValue, Replace};
43+
use sp_runtime::traits::{CheckedReduceBy, ConstU16, ConvertToValue, Replace, ReplaceWithDefault};
4444
use xcm::prelude::*;
4545
use xcm_builder::{AliasesIntoAccountId32, PayOverXcm};
4646

@@ -108,8 +108,10 @@ pub type ExchangeOrigin = EitherOf<EnsureRootWithSuccess<AccountId, ConstU16<655
108108
impl pallet_ranked_collective::Config<AmbassadorCollectiveInstance> for Runtime {
109109
type WeightInfo = weights::pallet_ranked_collective_ambassador_collective::WeightInfo<Runtime>;
110110
type RuntimeEvent = RuntimeEvent;
111+
type AddOrigin = MapSuccess<Self::PromoteOrigin, ReplaceWithDefault<()>>;
111112
type PromoteOrigin = PromoteOrigin;
112113
type DemoteOrigin = DemoteOrigin;
114+
type RemoveOrigin = Self::DemoteOrigin;
113115
type ExchangeOrigin = ExchangeOrigin;
114116
type Polls = AmbassadorReferenda;
115117
type MinRankOfClass = sp_runtime::traits::Identity;

cumulus/parachains/runtimes/collectives/collectives-westend/src/fellowship/mod.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,17 @@ impl pallet_ranked_collective::Config<FellowshipCollectiveInstance> for Runtime
113113
type WeightInfo = weights::pallet_ranked_collective_fellowship_collective::WeightInfo<Runtime>;
114114
type RuntimeEvent = RuntimeEvent;
115115

116-
#[cfg(not(feature = "runtime-benchmarks"))]
117116
// Promotions and the induction of new members are serviced by `FellowshipCore` pallet instance.
118-
type PromoteOrigin = frame_system::EnsureNever<pallet_ranked_collective::Rank>;
117+
#[cfg(not(feature = "runtime-benchmarks"))]
118+
type AddOrigin = frame_system::EnsureNever<()>;
119119
#[cfg(feature = "runtime-benchmarks")]
120+
type AddOrigin = frame_system::EnsureRoot<Self::AccountId>;
121+
120122
// The maximum value of `u16` set as a success value for the root to ensure the benchmarks will
121123
// pass.
124+
#[cfg(not(feature = "runtime-benchmarks"))]
125+
type PromoteOrigin = frame_system::EnsureNever<pallet_ranked_collective::Rank>;
126+
#[cfg(feature = "runtime-benchmarks")]
122127
type PromoteOrigin = EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>;
123128

124129
// Demotion is by any of:
@@ -127,6 +132,7 @@ impl pallet_ranked_collective::Config<FellowshipCollectiveInstance> for Runtime
127132
//
128133
// The maximum value of `u16` set as a success value for the root to ensure the benchmarks will
129134
// pass.
135+
type RemoveOrigin = Self::DemoteOrigin;
130136
type DemoteOrigin = EitherOf<
131137
EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>,
132138
MapSuccess<

polkadot/runtime/rococo/src/governance/fellowship.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//! Elements of governance concerning the Rococo Fellowship.
1818
1919
use frame_support::traits::{MapSuccess, TryMapSuccess};
20-
use sp_runtime::traits::{CheckedReduceBy, ConstU16, Replace};
20+
use sp_runtime::traits::{CheckedReduceBy, ConstU16, Replace, ReplaceWithDefault};
2121

2222
use super::*;
2323
use crate::{CENTS, DAYS};
@@ -315,6 +315,11 @@ pub type FellowshipCollectiveInstance = pallet_ranked_collective::Instance1;
315315
impl pallet_ranked_collective::Config<FellowshipCollectiveInstance> for Runtime {
316316
type WeightInfo = weights::pallet_ranked_collective::WeightInfo<Self>;
317317
type RuntimeEvent = RuntimeEvent;
318+
// Adding is by any of:
319+
// - Root.
320+
// - the FellowshipAdmin origin.
321+
// - a Fellowship origin.
322+
type AddOrigin = MapSuccess<Self::PromoteOrigin, ReplaceWithDefault<()>>;
318323
// Promotion is by any of:
319324
// - Root can demote arbitrarily.
320325
// - the FellowshipAdmin origin (i.e. token holder referendum);
@@ -326,6 +331,11 @@ impl pallet_ranked_collective::Config<FellowshipCollectiveInstance> for Runtime
326331
TryMapSuccess<origins::EnsureFellowship, CheckedReduceBy<ConstU16<1>>>,
327332
>,
328333
>;
334+
// Removing is by any of:
335+
// - Root can remove arbitrarily.
336+
// - the FellowshipAdmin origin (i.e. token holder referendum);
337+
// - a vote by the rank two above the current rank.
338+
type RemoveOrigin = Self::DemoteOrigin;
329339
// Demotion is by any of:
330340
// - Root can demote arbitrarily.
331341
// - the FellowshipAdmin origin (i.e. token holder referendum);

prdoc/pr_3212.prdoc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
title: "Ranked collective introduce `Add` and `Remove` origins"
2+
3+
doc:
4+
- audience: Runtime Dev
5+
description: |
6+
Add two new origins to the ranked-collective pallet. One to add new members and one to remove members, named `AddOrigin` and `RemoveOrigin` respectively.
7+
8+
crates:
9+
- name: pallet-ranked-collective

substrate/bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,8 @@ impl pallet_referenda::Config<pallet_referenda::Instance2> for Runtime {
10131013
impl pallet_ranked_collective::Config for Runtime {
10141014
type WeightInfo = pallet_ranked_collective::weights::SubstrateWeight<Self>;
10151015
type RuntimeEvent = RuntimeEvent;
1016+
type AddOrigin = EnsureRoot<AccountId>;
1017+
type RemoveOrigin = Self::DemoteOrigin;
10161018
type PromoteOrigin = EnsureRootWithSuccess<AccountId, ConstU16<65535>>;
10171019
type DemoteOrigin = EnsureRootWithSuccess<AccountId, ConstU16<65535>>;
10181020
type ExchangeOrigin = EnsureRootWithSuccess<AccountId, ConstU16<65535>>;

substrate/frame/core-fellowship/src/tests/integration.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use frame_system::EnsureSignedBy;
2727
use pallet_ranked_collective::{EnsureRanked, Geometric, Rank, TallyOf, Votes};
2828
use sp_core::Get;
2929
use sp_runtime::{
30-
traits::{Convert, ReduceBy, TryMorphInto},
30+
traits::{Convert, ReduceBy, ReplaceWithDefault, TryMorphInto},
3131
BuildStorage, DispatchError,
3232
};
3333
type Class = Rank;
@@ -137,12 +137,14 @@ impl pallet_ranked_collective::Config for Test {
137137
// Members can promote up to the rank of 2 below them.
138138
MapSuccess<EnsureRanked<Test, (), 2>, ReduceBy<ConstU16<2>>>,
139139
>;
140+
type AddOrigin = MapSuccess<Self::PromoteOrigin, ReplaceWithDefault<()>>;
140141
type DemoteOrigin = EitherOf<
141142
// Root can demote arbitrarily.
142143
frame_system::EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>,
143144
// Members can demote up to the rank of 3 below them.
144145
MapSuccess<EnsureRanked<Test, (), 3>, ReduceBy<ConstU16<3>>>,
145146
>;
147+
type RemoveOrigin = Self::DemoteOrigin;
146148
type ExchangeOrigin = EitherOf<
147149
// Root can exchange arbitrarily.
148150
frame_system::EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>,

substrate/frame/ranked-collective/src/benchmarking.rs

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ fn make_member<T: Config<I>, I: 'static>(rank: Rank) -> T::AccountId {
4141
let who = account::<T::AccountId>("member", MemberCount::<T, I>::get(0), SEED);
4242
let who_lookup = T::Lookup::unlookup(who.clone());
4343
assert_ok!(Pallet::<T, I>::add_member(
44-
T::PromoteOrigin::try_successful_origin()
45-
.expect("PromoteOrigin has no successful origin required for the benchmark"),
44+
T::AddOrigin::try_successful_origin()
45+
.expect("AddOrigin has no successful origin required for the benchmark"),
4646
who_lookup.clone(),
4747
));
4848
for _ in 0..rank {
@@ -60,7 +60,7 @@ benchmarks_instance_pallet! {
6060
let who = account::<T::AccountId>("member", 0, SEED);
6161
let who_lookup = T::Lookup::unlookup(who.clone());
6262
let origin =
63-
T::PromoteOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
63+
T::AddOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
6464
let call = Call::<T, I>::add_member { who: who_lookup };
6565
}: { call.dispatch_bypass_filter(origin)? }
6666
verify {
@@ -77,7 +77,7 @@ benchmarks_instance_pallet! {
7777
let last = make_member::<T, I>(rank);
7878
let last_index = (0..=rank).map(|r| IdToIndex::<T, I>::get(r, &last).unwrap()).collect::<Vec<_>>();
7979
let origin =
80-
T::DemoteOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
80+
T::RemoveOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
8181
let call = Call::<T, I>::remove_member { who: who_lookup, min_rank: rank };
8282
}: { call.dispatch_bypass_filter(origin)? }
8383
verify {
@@ -125,23 +125,11 @@ benchmarks_instance_pallet! {
125125
}
126126

127127
vote {
128-
let caller: T::AccountId = whitelisted_caller();
129-
let caller_lookup = T::Lookup::unlookup(caller.clone());
130-
assert_ok!(Pallet::<T, I>::add_member(
131-
T::PromoteOrigin::try_successful_origin()
132-
.expect("PromoteOrigin has no successful origin required for the benchmark"),
133-
caller_lookup.clone(),
134-
));
135-
// Create a poll
136128
let class = T::Polls::classes().into_iter().next().unwrap();
137129
let rank = T::MinRankOfClass::convert(class.clone());
138-
for _ in 0..rank {
139-
assert_ok!(Pallet::<T, I>::promote_member(
140-
T::PromoteOrigin::try_successful_origin()
141-
.expect("PromoteOrigin has no successful origin required for the benchmark"),
142-
caller_lookup.clone(),
143-
));
144-
}
130+
131+
let caller = make_member::<T, I>(rank);
132+
let caller_lookup = T::Lookup::unlookup(caller.clone());
145133

146134
let poll = T::Polls::create_ongoing(class).expect("Must always be able to create a poll for rank 0");
147135

substrate/frame/ranked-collective/src/lib.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,20 @@ pub mod pallet {
393393
type RuntimeEvent: From<Event<Self, I>>
394394
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
395395

396-
/// The origin required to add or promote a mmember. The success value indicates the
396+
/// The origin required to add a member.
397+
type AddOrigin: EnsureOrigin<Self::RuntimeOrigin>;
398+
399+
/// The origin required to remove a member.
400+
///
401+
/// The success value indicates the maximum rank *from which* the removal may be.
402+
type RemoveOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Rank>;
403+
404+
/// The origin required to promote a member. The success value indicates the
397405
/// maximum rank *to which* the promotion may be.
398406
type PromoteOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Rank>;
399407

400-
/// The origin required to demote or remove a member. The success value indicates the
401-
/// maximum rank *from which* the demotion/removal may be.
408+
/// The origin required to demote a member. The success value indicates the
409+
/// maximum rank *from which* the demotion may be.
402410
type DemoteOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Rank>;
403411

404412
/// The origin that can swap the account of a member.
@@ -510,22 +518,21 @@ pub mod pallet {
510518
impl<T: Config<I>, I: 'static> Pallet<T, I> {
511519
/// Introduce a new member.
512520
///
513-
/// - `origin`: Must be the `AdminOrigin`.
521+
/// - `origin`: Must be the `AddOrigin`.
514522
/// - `who`: Account of non-member which will become a member.
515-
/// - `rank`: The rank to give the new member.
516523
///
517524
/// Weight: `O(1)`
518525
#[pallet::call_index(0)]
519526
#[pallet::weight(T::WeightInfo::add_member())]
520527
pub fn add_member(origin: OriginFor<T>, who: AccountIdLookupOf<T>) -> DispatchResult {
521-
let _ = T::PromoteOrigin::ensure_origin(origin)?;
528+
T::AddOrigin::ensure_origin(origin)?;
522529
let who = T::Lookup::lookup(who)?;
523530
Self::do_add_member(who, true)
524531
}
525532

526533
/// Increment the rank of an existing member by one.
527534
///
528-
/// - `origin`: Must be the `AdminOrigin`.
535+
/// - `origin`: Must be the `PromoteOrigin`.
529536
/// - `who`: Account of existing member.
530537
///
531538
/// Weight: `O(1)`
@@ -540,7 +547,7 @@ pub mod pallet {
540547
/// Decrement the rank of an existing member by one. If the member is already at rank zero,
541548
/// then they are removed entirely.
542549
///
543-
/// - `origin`: Must be the `AdminOrigin`.
550+
/// - `origin`: Must be the `DemoteOrigin`.
544551
/// - `who`: Account of existing member of rank greater than zero.
545552
///
546553
/// Weight: `O(1)`, less if the member's index is highest in its rank.
@@ -554,7 +561,7 @@ pub mod pallet {
554561

555562
/// Remove the member entirely.
556563
///
557-
/// - `origin`: Must be the `AdminOrigin`.
564+
/// - `origin`: Must be the `RemoveOrigin`.
558565
/// - `who`: Account of existing member of rank greater than zero.
559566
/// - `min_rank`: The rank of the member or greater.
560567
///
@@ -566,7 +573,7 @@ pub mod pallet {
566573
who: AccountIdLookupOf<T>,
567574
min_rank: Rank,
568575
) -> DispatchResultWithPostInfo {
569-
let max_rank = T::DemoteOrigin::ensure_origin(origin)?;
576+
let max_rank = T::RemoveOrigin::ensure_origin(origin)?;
570577
let who = T::Lookup::lookup(who)?;
571578
let MemberRecord { rank, .. } = Self::ensure_member(&who)?;
572579
ensure!(min_rank >= rank, Error::<T, I>::InvalidWitness);

substrate/frame/ranked-collective/src/tests.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ use frame_support::{
2626
traits::{ConstU16, EitherOf, MapSuccess, Polling},
2727
};
2828
use sp_core::Get;
29-
use sp_runtime::{traits::ReduceBy, BuildStorage};
29+
use sp_runtime::{
30+
traits::{ReduceBy, ReplaceWithDefault},
31+
BuildStorage,
32+
};
3033

3134
use super::*;
3235
use crate as pallet_ranked_collective;
@@ -152,6 +155,8 @@ parameter_types! {
152155
impl Config for Test {
153156
type WeightInfo = ();
154157
type RuntimeEvent = RuntimeEvent;
158+
type AddOrigin = MapSuccess<Self::PromoteOrigin, ReplaceWithDefault<()>>;
159+
type RemoveOrigin = Self::DemoteOrigin;
155160
type PromoteOrigin = EitherOf<
156161
// Root can promote arbitrarily.
157162
frame_system::EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>,

substrate/frame/salary/src/tests/integration.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use frame_support::{
2626
use pallet_ranked_collective::{EnsureRanked, Geometric, TallyOf, Votes};
2727
use sp_core::{ConstU16, Get};
2828
use sp_runtime::{
29-
traits::{Convert, ReduceBy},
29+
traits::{Convert, ReduceBy, ReplaceWithDefault},
3030
BuildStorage, DispatchError,
3131
};
3232

@@ -162,12 +162,14 @@ impl pallet_ranked_collective::Config for Test {
162162
// Members can promote up to the rank of 2 below them.
163163
MapSuccess<EnsureRanked<Test, (), 2>, ReduceBy<ConstU16<2>>>,
164164
>;
165+
type AddOrigin = MapSuccess<Self::PromoteOrigin, ReplaceWithDefault<()>>;
165166
type DemoteOrigin = EitherOf<
166167
// Root can demote arbitrarily.
167168
frame_system::EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>,
168169
// Members can demote up to the rank of 3 below them.
169170
MapSuccess<EnsureRanked<Test, (), 3>, ReduceBy<ConstU16<3>>>,
170171
>;
172+
type RemoveOrigin = Self::DemoteOrigin;
171173
type ExchangeOrigin = EitherOf<
172174
// Root can exchange arbitrarily.
173175
frame_system::EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>,

0 commit comments

Comments
 (0)