Skip to content

Commit 9407752

Browse files
committed
feat(pallet-ranked-collective): added types
New associated types(`AddOrigin` & `RemoveOrigin`) have been added to `Config`.
1 parent 3bc1671 commit 9407752

5 files changed

Lines changed: 36 additions & 12 deletions

File tree

bin/node/runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,8 @@ impl pallet_referenda::Config<pallet_referenda::Instance2> for Runtime {
925925
impl pallet_ranked_collective::Config for Runtime {
926926
type WeightInfo = pallet_ranked_collective::weights::SubstrateWeight<Self>;
927927
type RuntimeEvent = RuntimeEvent;
928+
type AddOrigin = EnsureRoot<AccountId>;
929+
type RemoveOrigin = EnsureRootWithSuccess<AccountId, ConstU16<65535>>;
928930
type PromoteOrigin = EnsureRootWithSuccess<AccountId, ConstU16<65535>>;
929931
type DemoteOrigin = EnsureRootWithSuccess<AccountId, ConstU16<65535>>;
930932
type Polls = RankedPolls;

frame/ranked-collective/src/benchmarking.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ fn make_member<T: Config<I>, I: 'static>(rank: Rank) -> T::AccountId {
3737
let who = account::<T::AccountId>("member", MemberCount::<T, I>::get(0), SEED);
3838
let who_lookup = T::Lookup::unlookup(who.clone());
3939
assert_ok!(Pallet::<T, I>::add_member(
40-
T::PromoteOrigin::try_successful_origin()
41-
.expect("PromoteOrigin has no successful origin required for the benchmark"),
40+
T::AddOrigin::try_successful_origin()
41+
.expect("AddOrigin has no successful origin required for the benchmark"),
4242
who_lookup.clone(),
4343
));
4444
for _ in 0..rank {
@@ -56,7 +56,7 @@ benchmarks_instance_pallet! {
5656
let who = account::<T::AccountId>("member", 0, SEED);
5757
let who_lookup = T::Lookup::unlookup(who.clone());
5858
let origin =
59-
T::PromoteOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
59+
T::AddOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
6060
let call = Call::<T, I>::add_member { who: who_lookup };
6161
}: { call.dispatch_bypass_filter(origin)? }
6262
verify {
@@ -73,7 +73,7 @@ benchmarks_instance_pallet! {
7373
let last = make_member::<T, I>(rank);
7474
let last_index = (0..=rank).map(|r| IdToIndex::<T, I>::get(r, &last).unwrap()).collect::<Vec<_>>();
7575
let origin =
76-
T::DemoteOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
76+
T::RemoveOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
7777
let call = Call::<T, I>::remove_member { who: who_lookup, min_rank: rank };
7878
}: { call.dispatch_bypass_filter(origin)? }
7979
verify {
@@ -124,8 +124,8 @@ benchmarks_instance_pallet! {
124124
let caller: T::AccountId = whitelisted_caller();
125125
let caller_lookup = T::Lookup::unlookup(caller.clone());
126126
assert_ok!(Pallet::<T, I>::add_member(
127-
T::PromoteOrigin::try_successful_origin()
128-
.expect("PromoteOrigin has no successful origin required for the benchmark"),
127+
T::AddOrigin::try_successful_origin()
128+
.expect("AddOrigin has no successful origin required for the benchmark"),
129129
caller_lookup.clone(),
130130
));
131131
// Create a poll

frame/ranked-collective/src/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,19 @@ pub mod pallet {
382382
type RuntimeEvent: From<Event<Self, I>>
383383
+ IsType<<Self as frame_system::Config>::RuntimeEvent>;
384384

385-
/// The origin required to add or promote a mmember. The success value indicates the
385+
/// The origin required to add a member.
386+
type AddOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = ()>;
387+
388+
/// The origin required to remove a member. The success value indicates the
389+
/// maximum rank *from which* the removal may be.
390+
type RemoveOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Rank>;
391+
392+
/// The origin required to promote a member. The success value indicates the
386393
/// maximum rank *to which* the promotion may be.
387394
type PromoteOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Rank>;
388395

389-
/// The origin required to demote or remove a member. The success value indicates the
390-
/// maximum rank *from which* the demotion/removal may be.
396+
/// The origin required to demote a member. The success value indicates the
397+
/// maximum rank *from which* the demotion may be.
391398
type DemoteOrigin: EnsureOrigin<Self::RuntimeOrigin, Success = Rank>;
392399

393400
/// The polling system used for our voting.
@@ -490,7 +497,7 @@ pub mod pallet {
490497
#[pallet::call_index(0)]
491498
#[pallet::weight(T::WeightInfo::add_member())]
492499
pub fn add_member(origin: OriginFor<T>, who: AccountIdLookupOf<T>) -> DispatchResult {
493-
let _ = T::PromoteOrigin::ensure_origin(origin)?;
500+
T::AddOrigin::ensure_origin(origin)?;
494501
let who = T::Lookup::lookup(who)?;
495502
Self::do_add_member(who)
496503
}
@@ -538,7 +545,7 @@ pub mod pallet {
538545
who: AccountIdLookupOf<T>,
539546
min_rank: Rank,
540547
) -> DispatchResultWithPostInfo {
541-
let max_rank = T::DemoteOrigin::ensure_origin(origin)?;
548+
let max_rank = T::RemoveOrigin::ensure_origin(origin)?;
542549
let who = T::Lookup::lookup(who)?;
543550
let MemberRecord { rank, .. } = Self::ensure_member(&who)?;
544551
ensure!(min_rank >= rank, Error::<T, I>::InvalidWitness);

frame/ranked-collective/src/tests.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use frame_support::{
2828
use sp_core::{Get, H256};
2929
use sp_runtime::{
3030
testing::Header,
31-
traits::{BlakeTwo256, IdentityLookup, ReduceBy},
31+
traits::{BlakeTwo256, IdentityLookup, ReduceBy, ReplaceWithDefaultFor},
3232
BuildStorage,
3333
};
3434

@@ -182,6 +182,18 @@ parameter_types! {
182182
impl Config for Test {
183183
type WeightInfo = ();
184184
type RuntimeEvent = RuntimeEvent;
185+
type AddOrigin = EitherOf<
186+
// Root can add arbitrarily.
187+
frame_system::EnsureRoot<Self::AccountId>,
188+
// Members with rank 2 and above can add.
189+
MapSuccess<EnsureRanked<Test, (), 2>, ReplaceWithDefaultFor<()>>,
190+
>;
191+
type RemoveOrigin = EitherOf<
192+
// Root can remove arbitrarily.
193+
frame_system::EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>,
194+
// Members can remove up to the rank of 3 below them.
195+
MapSuccess<EnsureRanked<Test, (), 3>, ReduceBy<ConstU16<3>>>,
196+
>;
185197
type PromoteOrigin = EitherOf<
186198
// Root can promote arbitrarily.
187199
frame_system::EnsureRootWithSuccess<Self::AccountId, ConstU16<65535>>,

primitives/runtime/src/traits.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,9 @@ morph_types! {
540540
/// Morpher to disregard the source value and replace with another.
541541
pub type Replace<V: TypedGet> = |_| -> V::Type { V::get() };
542542

543+
/// Morpher to disregard the source value and replace with default for T.
544+
pub type ReplaceWithDefaultFor<T: Default> = |_| -> T { T::default() };
545+
543546
/// Mutator which reduces a scalar by a particular amount.
544547
pub type ReduceBy<N: TypedGet> = |r: N::Type| -> N::Type {
545548
r.checked_sub(&N::get()).unwrap_or(Zero::zero())

0 commit comments

Comments
 (0)