Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds MaxRank Config in pallet-core-fellowship #3393

Merged
merged 45 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
2e527a3
adds max_rank()
Doordashcon Feb 19, 2024
28cdd10
modify original test cases
Doordashcon Feb 26, 2024
277345e
includes prdoc
Doordashcon Feb 26, 2024
696aafb
add higher rank
Doordashcon Feb 27, 2024
5a6a64f
fix benchmark
Doordashcon Feb 28, 2024
6f78f77
Merge branch 'master' into ddc-max-rank
Doordashcon Feb 28, 2024
4117b94
xcm & salary
Mar 11, 2024
26e9a15
Merge branch 'master' into ddc-max-rank
Doordashcon Mar 11, 2024
1352159
benchmaek: set_params
Doordashcon Mar 12, 2024
d72a5bd
adjust promote benchmark
Doordashcon Mar 14, 2024
1cd019b
Merge branch 'master' into ddc-max-rank
Doordashcon Mar 14, 2024
72ecddb
revised implementation
Doordashcon Apr 8, 2024
083669a
Merge branch 'master' into ddc-max-rank
Doordashcon Apr 8, 2024
8a5c550
westend + fmt
Doordashcon Apr 8, 2024
abc2092
fix benchmarks
Doordashcon Apr 8, 2024
6d77c6c
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Apr 12, 2024
dd0e5a9
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Apr 12, 2024
afe16a5
init migration
Doordashcon Apr 15, 2024
4e53b70
Merge branch 'ddc-max-rank' of https://github.com/Doordashcon/polkado…
Doordashcon Apr 15, 2024
a746913
remove migration test
Doordashcon Apr 15, 2024
23a0d06
no truncate
Doordashcon Apr 15, 2024
e4fc2a1
remove nesting
Doordashcon Apr 18, 2024
548b0f7
Merge branch 'master' into ddc-max-rank
Doordashcon Apr 18, 2024
11c73be
adds header
Doordashcon Apr 19, 2024
9eb769f
Merge branch 'master' into ddc-max-rank
Doordashcon Apr 22, 2024
a93fdca
MigrateToV1>>>
Doordashcon Apr 22, 2024
7da28ff
doc update Migration to MigrateToV1
Doordashcon Apr 22, 2024
252d152
westend core pallet migration
Doordashcon Apr 26, 2024
fb1a240
Merge branch 'master' into ddc-max-rank
Doordashcon Apr 26, 2024
137b5f4
Merge branch 'master' into ddc-max-rank
Doordashcon Apr 26, 2024
2aca81a
doc add westend-collective
Doordashcon Apr 26, 2024
8cebd55
Merge branch 'master' into ddc-max-rank
Doordashcon Apr 26, 2024
4d798a4
nit
Doordashcon Apr 27, 2024
fc72258
max_rank in Ranked Member not needed
Doordashcon Apr 30, 2024
093232f
remove Config salary pallet test
Doordashcon Apr 30, 2024
3f21602
ranked remove MaxRank in test
Doordashcon Apr 30, 2024
6c76c06
Merge branch 'master' into ddc-max-rank
Doordashcon Apr 30, 2024
f9724a2
clippy
Doordashcon Apr 30, 2024
105109c
Merge branch 'master' into ddc-max-rank
Doordashcon Apr 30, 2024
25f0d06
similar import for benchmarks
Doordashcon Apr 30, 2024
2afaa8f
sp_runtime::bounded_vec
Doordashcon Apr 30, 2024
ee9f047
sp_core
Doordashcon May 1, 2024
4a31d0a
back to BoundedVec::try_from..
Doordashcon May 3, 2024
5071cb9
Update substrate/frame/core-fellowship/src/lib.rs
bkchr May 16, 2024
9af1d88
Merge branch 'master' into ddc-max-rank
bkchr May 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions polkadot/xcm/xcm-builder/src/tests/pay/salary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ impl RankedMembers for TestClub {
},
})
}
fn max_rank() -> Self::Rank {
CLUB.with(|club| club.borrow().values().max().cloned().unwrap_or(Self::min_rank()))
}
}

fn set_rank(who: AccountId, rank: u128) {
Expand Down
13 changes: 13 additions & 0 deletions prdoc/pr_3393.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Add `max_rank()` method to `RankedMember`

doc:
- audience: Runtime User
description: |
This PR adds a new method named max_rank to the RankedMember trait. This method facilitates the expansion of rank numbers within a collective. Initially, the maximum rank was set to IX (Grand Master) on the core-fellowship pallet, corresponding to the establishment of the Technical Fellowship and setting the default member count to nine. However, with the introduction of new collectives, this maximum rank is expected to evolve.

crates:
- name: pallet-core-fellowship
- name: pallet-ranked-collective
45 changes: 30 additions & 15 deletions substrate/frame/core-fellowship/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ mod benchmarks {
}

fn set_benchmark_params<T: Config<I>, I: 'static>() -> Result<(), BenchmarkError> {
let max_rank: usize = T::Members::max_rank().into();
let params = ParamsType {
active_salary: [100u32.into(); 9],
passive_salary: [10u32.into(); 9],
demotion_period: [100u32.into(); 9],
min_promotion_period: [100u32.into(); 9],
active_salary: vec![100u32.into(); max_rank],
passive_salary: vec![10u32.into(); max_rank],
demotion_period: vec![100u32.into(); max_rank],
min_promotion_period: vec![100u32.into(); max_rank],
offboard_timeout: 1u32.into(),
};

Expand All @@ -68,11 +69,12 @@ mod benchmarks {

#[benchmark]
fn set_params() -> Result<(), BenchmarkError> {
make_member::<T, I>(9)?;
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
let params = ParamsType {
active_salary: [100u32.into(); 9],
passive_salary: [10u32.into(); 9],
demotion_period: [100u32.into(); 9],
min_promotion_period: [100u32.into(); 9],
active_salary: [100u32.into(); 9].to_vec(),
passive_salary: [10u32.into(); 9].to_vec(),
demotion_period: [100u32.into(); 9].to_vec(),
min_promotion_period: [100u32.into(); 9].to_vec(),
offboard_timeout: 1u32.into(),
};

Expand Down Expand Up @@ -104,10 +106,10 @@ mod benchmarks {

#[benchmark]
fn bump_demote() -> Result<(), BenchmarkError> {
set_benchmark_params::<T, I>()?;

let member = make_member::<T, I>(2)?;

set_benchmark_params::<T, I>()?;

// Set it to the max value to ensure that any possible auto-demotion period has passed.
frame_system::Pallet::<T>::set_block_number(BlockNumberFor::<T>::max_value());
ensure_evidence::<T, I>(&member)?;
Expand Down Expand Up @@ -149,14 +151,27 @@ mod benchmarks {

#[benchmark]
fn promote() -> Result<(), BenchmarkError> {
let member = make_member::<T, I>(1)?;
ensure_evidence::<T, I>(&member)?;
let candidate: T::AccountId = account("candidate", 0, SEED);

T::Members::induct(&candidate)?;

CoreFellowship::<T, I>::import(RawOrigin::Signed(candidate.clone()).into())?;

T::Members::promote(&candidate)?;

make_member::<T, I>(2)?;

set_benchmark_params::<T, I>()?;

ensure_evidence::<T, I>(&candidate)?;

frame_system::Pallet::<T>::set_block_number(BlockNumberFor::<T>::max_value());

#[extrinsic_call]
_(RawOrigin::Root, member.clone(), 2u8.into());
_(RawOrigin::Root, candidate.clone(), 2u8.into());

assert_eq!(T::Members::rank_of(&member), Some(2));
assert!(!MemberEvidence::<T, I>::contains_key(&member));
assert_eq!(T::Members::rank_of(&candidate), Some(2));
assert!(!MemberEvidence::<T, I>::contains_key(&candidate));
Ok(())
}

Expand Down
45 changes: 30 additions & 15 deletions substrate/frame/core-fellowship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,28 +101,28 @@ pub type Evidence<T, I> = BoundedVec<u8, <T as Config<I>>::EvidenceSize>;

/// The status of the pallet instance.
#[derive(Encode, Decode, Eq, PartialEq, Clone, TypeInfo, MaxEncodedLen, RuntimeDebug)]
pub struct ParamsType<Balance, BlockNumber, const RANKS: usize> {
pub struct ParamsType<Balance, BlockNumber> {
/// The amounts to be paid when a member of a given rank (-1) is active.
active_salary: [Balance; RANKS],
active_salary: Vec<Balance>,
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
/// The amounts to be paid when a member of a given rank (-1) is passive.
passive_salary: [Balance; RANKS],
passive_salary: Vec<Balance>,
/// The period between which unproven members become demoted.
demotion_period: [BlockNumber; RANKS],
demotion_period: Vec<BlockNumber>,
/// The period between which members must wait before they may proceed to this rank.
min_promotion_period: [BlockNumber; RANKS],
min_promotion_period: Vec<BlockNumber>,
/// Amount by which an account can remain at rank 0 (candidate before being offboard entirely).
offboard_timeout: BlockNumber,
}

impl<Balance: Default + Copy, BlockNumber: Default + Copy, const RANKS: usize> Default
for ParamsType<Balance, BlockNumber, RANKS>
impl<Balance: Default + Copy, BlockNumber: Default + Copy> Default
for ParamsType<Balance, BlockNumber>
{
fn default() -> Self {
Self {
active_salary: [Balance::default(); RANKS],
passive_salary: [Balance::default(); RANKS],
demotion_period: [BlockNumber::default(); RANKS],
min_promotion_period: [BlockNumber::default(); RANKS],
active_salary: Vec::new(),
passive_salary: Vec::new(),
demotion_period: Vec::new(),
min_promotion_period: Vec::new(),
offboard_timeout: BlockNumber::default(),
}
}
Expand All @@ -149,9 +149,8 @@ pub mod pallet {
};
use frame_system::{ensure_root, pallet_prelude::*};

const RANK_COUNT: usize = 9;

#[pallet::pallet]
#[pallet::without_storage_info]
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);

#[pallet::config]
Expand Down Expand Up @@ -195,7 +194,7 @@ pub mod pallet {
type EvidenceSize: Get<u32>;
}

pub type ParamsOf<T, I> = ParamsType<<T as Config<I>>::Balance, BlockNumberFor<T>, RANK_COUNT>;
pub type ParamsOf<T, I> = ParamsType<<T as Config<I>>::Balance, BlockNumberFor<T>>;
pub type MemberStatusOf<T> = MemberStatus<BlockNumberFor<T>>;
pub type RankOf<T, I> = <<T as Config<I>>::Members as RankedMembers>::Rank;

Expand Down Expand Up @@ -276,6 +275,8 @@ pub mod pallet {
NotTracked,
/// Operation cannot be done yet since not enough time has passed.
TooSoon,
/// Length of parameters are incompatible.
IncorrectParamsLength,
}

#[pallet::call]
Expand Down Expand Up @@ -337,8 +338,22 @@ pub mod pallet {
#[pallet::call_index(1)]
pub fn set_params(origin: OriginFor<T>, params: Box<ParamsOf<T, I>>) -> DispatchResult {
T::ParamsOrigin::ensure_origin_or_root(origin)?;
// Retrieves the current max rank allowed.
let max_rank = T::Members::max_rank();

// Validate the lengths of the vectors in params are equal to max_rank.
let expected_length = max_rank as usize; // Convert max_rank to usize for comparison.
ensure!(
params.active_salary.len() == expected_length &&
params.passive_salary.len() == expected_length &&
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
params.demotion_period.len() == expected_length &&
params.min_promotion_period.len() == expected_length,
Error::<T, I>::IncorrectParamsLength
);

Params::<T, I>::put(params.as_ref());
Self::deposit_event(Event::<T, I>::ParamsChanged { params: *params });

Ok(())
}

Expand Down Expand Up @@ -539,7 +554,7 @@ pub mod pallet {
/// in the range `1..=RANK_COUNT` is `None`.
pub(crate) fn rank_to_index(rank: RankOf<T, I>) -> Option<usize> {
match TryInto::<usize>::try_into(rank) {
Ok(r) if r <= RANK_COUNT && r > 0 => Some(r - 1),
Ok(r) if r <= T::Members::max_rank().into() && r > 0 => Some(r - 1),
_ => return None,
}
}
Expand Down
47 changes: 42 additions & 5 deletions substrate/frame/core-fellowship/src/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use frame_support::{
traits::{ConstU16, EitherOf, IsInVec, MapSuccess, PollStatus, Polling, TryMapSuccess},
};
use frame_system::EnsureSignedBy;
use pallet_ranked_collective::{EnsureRanked, Geometric, Rank, TallyOf, Votes};
use pallet_ranked_collective::{EnsureRanked, Geometric, MemberCount, Rank, TallyOf, Votes};
use sp_core::Get;
use sp_runtime::{
traits::{Convert, ReduceBy, ReplaceWithDefault, TryMorphInto},
Expand Down Expand Up @@ -163,11 +163,13 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
assert_ok!(Club::add_member(RuntimeOrigin::root(), 100));
promote_n_times(100, 9);
let params = ParamsType {
active_salary: [10, 20, 30, 40, 50, 60, 70, 80, 90],
passive_salary: [1, 2, 3, 4, 5, 6, 7, 8, 9],
demotion_period: [2, 4, 6, 8, 10, 12, 14, 16, 18],
min_promotion_period: [3, 6, 9, 12, 15, 18, 21, 24, 27],
active_salary: [10, 20, 30, 40, 50, 60, 70, 80, 90].to_vec(),
passive_salary: [1, 2, 3, 4, 5, 6, 7, 8, 9].to_vec(),
demotion_period: [2, 4, 6, 8, 10, 12, 14, 16, 18].to_vec(),
min_promotion_period: [3, 6, 9, 12, 15, 18, 21, 24, 27].to_vec(),
offboard_timeout: 1,
};
assert_ok!(CoreFellowship::set_params(signed(1), Box::new(params)));
Expand Down Expand Up @@ -278,3 +280,38 @@ fn swap_bad_noops() {
);
});
}

#[test]
fn add_higher_rank() {
new_test_ext().execute_with(|| {
assert_ok!(Club::add_member(RuntimeOrigin::root(), 7));
assert_ok!(Club::add_member(RuntimeOrigin::root(), 5));
promote_n_times(7, 9);
promote_n_times(5, 9);

System::set_block_number(3);
assert_ok!(CoreFellowship::import(signed(7)));
assert_ok!(CoreFellowship::import(signed(5)));

// MemberCount still reads previous max rank as 9
assert_noop!(
CoreFellowship::promote(RuntimeOrigin::root(), 7, 10),
Error::<Test>::InvalidRank
);

assert_ok!(Club::promote_member(RuntimeOrigin::root(), 7));
assert_eq!(MemberCount::<Test>::get(10), 1);

let params = ParamsType {
active_salary: [10, 20, 30, 40, 50, 60, 70, 80, 90, 100].to_vec(),
passive_salary: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].to_vec(),
demotion_period: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20].to_vec(),
min_promotion_period: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30].to_vec(),
offboard_timeout: 1,
};
assert_ok!(CoreFellowship::set_params(signed(1), Box::new(params)));

System::set_block_number(30);
assert_ok!(CoreFellowship::promote(RuntimeOrigin::root(), 5, 10));
})
}
29 changes: 17 additions & 12 deletions substrate/frame/core-fellowship/src/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ impl RankedMembers for TestClub {
},
})
}
fn max_rank() -> Self::Rank {
CLUB.with(|club| club.borrow().values().max().cloned().unwrap_or(Self::min_rank()))
}
}

fn set_rank(who: u64, rank: u16) {
Expand Down Expand Up @@ -122,13 +125,15 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
set_rank(100, 9);
let params = ParamsType {
active_salary: [10, 20, 30, 40, 50, 60, 70, 80, 90],
passive_salary: [1, 2, 3, 4, 5, 6, 7, 8, 9],
demotion_period: [2, 4, 6, 8, 10, 12, 14, 16, 18],
min_promotion_period: [3, 6, 9, 12, 15, 18, 21, 24, 27],
active_salary: [10, 20, 30, 40, 50, 60, 70, 80, 90].to_vec(),
passive_salary: [1, 2, 3, 4, 5, 6, 7, 8, 9].to_vec(),
demotion_period: [2, 4, 6, 8, 10, 12, 14, 16, 18].to_vec(),
min_promotion_period: [3, 6, 9, 12, 15, 18, 21, 24, 27].to_vec(),
offboard_timeout: 1,
};

assert_ok!(CoreFellowship::set_params(signed(1), Box::new(params)));
System::set_block_number(1);
});
Expand Down Expand Up @@ -170,10 +175,10 @@ fn basic_stuff() {
fn set_params_works() {
new_test_ext().execute_with(|| {
let params = ParamsType {
active_salary: [10, 20, 30, 40, 50, 60, 70, 80, 90],
passive_salary: [1, 2, 3, 4, 5, 6, 7, 8, 9],
demotion_period: [1, 2, 3, 4, 5, 6, 7, 8, 9],
min_promotion_period: [1, 2, 3, 4, 5, 10, 15, 20, 30],
active_salary: [10, 20, 30, 40, 50, 60, 70, 80, 90].to_vec(),
passive_salary: [1, 2, 3, 4, 5, 6, 7, 8, 9].to_vec(),
demotion_period: [1, 2, 3, 4, 5, 6, 7, 8, 9].to_vec(),
min_promotion_period: [1, 2, 3, 4, 5, 10, 15, 20, 30].to_vec(),
offboard_timeout: 1,
};
assert_noop!(
Expand Down Expand Up @@ -284,10 +289,10 @@ fn offboard_works() {
fn infinite_demotion_period_works() {
new_test_ext().execute_with(|| {
let params = ParamsType {
active_salary: [10; 9],
passive_salary: [10; 9],
min_promotion_period: [10; 9],
demotion_period: [0; 9],
active_salary: [10; 9].to_vec(),
passive_salary: [10; 9].to_vec(),
min_promotion_period: [10; 9].to_vec(),
demotion_period: [0; 9].to_vec(),
offboard_timeout: 0,
};
assert_ok!(CoreFellowship::set_params(signed(1), Box::new(params)));
Expand Down
7 changes: 7 additions & 0 deletions substrate/frame/ranked-collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1003,5 +1003,12 @@ pub mod pallet {
fn demote(who: &Self::AccountId) -> DispatchResult {
Self::do_demote_member(who.clone(), None)
}

fn max_rank() -> Self::Rank {
MemberCount::<T, I>::iter()
.map(|(rank, _)| rank)
.max()
.unwrap_or(Self::min_rank())
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
3 changes: 3 additions & 0 deletions substrate/frame/salary/src/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ impl RankedMembers for TestClub {
},
})
}
fn max_rank() -> Self::Rank {
CLUB.with(|club| club.borrow().values().max().cloned().unwrap_or(Self::min_rank()))
}
}

fn set_rank(who: u64, rank: u64) {
Expand Down
3 changes: 3 additions & 0 deletions substrate/frame/support/src/traits/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,9 @@ pub trait RankedMembers {
/// Demote a member to the next lower rank; demoting beyond the `min_rank` removes the
/// member entirely.
fn demote(who: &Self::AccountId) -> DispatchResult;

/// The maximum rank possible in this membership organisation.
fn max_rank() -> Self::Rank;
}

/// Handler that can deal with the swap of two members.
Expand Down
Loading