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

Adding try_state hook for Core Fellowship #3276

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
89 changes: 88 additions & 1 deletion substrate/frame/core-fellowship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;
use sp_arithmetic::traits::{Saturating, Zero};
use sp_runtime::RuntimeDebug;
use sp_runtime::{RuntimeDebug, TryRuntimeError};
use sp_std::{marker::PhantomData, prelude::*};

use frame_support::{
Expand Down Expand Up @@ -279,6 +279,14 @@ pub mod pallet {
TooSoon,
}

#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
#[cfg(feature = "try-runtime")]
fn try_state(_n: BlockNumberFor<T>) -> Result<(), TryRuntimeError> {
Self::do_try_state()
}
}

#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Bump the state of a member.
Expand Down Expand Up @@ -551,6 +559,85 @@ pub mod pallet {
Self::deposit_event(e);
}
}

///
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
/// even though the offboard timeout has elapsed, members can still call promote.
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
///
///
#[cfg(any(feature = "try-runtime", test))]
pub fn do_try_state() -> Result<(), TryRuntimeError> {
// Check invariants for each member tracked by the pallet
let params = Params::<T, I>::get();

for who in Member::<T,I>::iter_keys() {
if let Some(member_status) = Member::<T,I>::get(who.clone()) {
// Ensure the member's rank is consistent with their status in the pallet
let rank = T::Members::rank_of(&who).ok_or("Member not found in rank registry")?;

if rank > T::Members::min_rank() {
Self::ranked_member(rank, &who, member_status.clone(), params.clone())?;
} else {
Self::unranked_member(rank, &who, member_status.clone(), params.clone())?;
}
}
}
Ok(())
}

fn ranked_member(rank: RankOf<T, I>, who: &T::AccountId, member_status: MemberStatusOf<T>, params: ParamsOf<T, I>) -> Result<(), TryRuntimeError> {
// If member has evidence submitted, ensure it aligns with their rank and wish
if let Some((wish, _)) = MemberEvidence::<T, I>::get(&who) {
match wish {
Wish::Retention => {

ensure!(rank < 7, "Member at exclusive rank cannot retain further");
// propose the addition of T::Members::exclusive_rank()
},
Wish::Promotion => {

ensure!(rank < 9, "Member at max rank cannot be promoted");
// propose the addition of T::Members::max_rank()
},
}
}

let now = frame_system::Pallet::<T>::block_number();
let rank_index = Self::rank_to_index(rank).ok_or(Error::<T, I>::InvalidRank)?;
let demotion_period = if params.demotion_period[rank_index] != Zero::zero() {
Some(params.demotion_period[rank_index])
} else {
None
};

if let Some(period) = demotion_period {
let demotion_due = member_status.last_proof.saturating_add(period);
ensure!(now < demotion_due, TryRuntimeError::Other("Member is outside the demotion period, indicating potential rank inconsistency"));
}
Ok(())
}

fn unranked_member(rank: RankOf<T, I>, who: &T::AccountId, member_status: MemberStatusOf<T>, params: ParamsOf<T, I>) -> Result<(), TryRuntimeError>{
if let Some((wish, _evidence)) = MemberEvidence::<T, I>::get(&who) {
if wish == Wish::Retention {

return Err(TryRuntimeError::Other("Rentention disallowed for Rank < 1"));
}
}

let now = frame_system::Pallet::<T>::block_number();
let offboard_timeout = if params.offboard_timeout != Zero::zero() {
Some(params.offboard_timeout)
} else {
None
};

if let Some(timeout) = offboard_timeout {
let offboard_due = member_status.last_proof.saturating_add(timeout);
ensure!(now < offboard_due, TryRuntimeError::Other("Member has exceeded offboard timeout, indicating potential rank inconsistency"));
}

Ok(())
}
}

impl<T: Config<I>, I: 'static> GetSalary<RankOf<T, I>, T::AccountId, T::Balance> for Pallet<T, I> {
Expand Down
54 changes: 41 additions & 13 deletions substrate/frame/core-fellowship/src/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
ext
}

/// Run the function pointer inside externalities and asserts the try_state hook at the end.
pub fn build_and_execute(test: impl FnOnce() -> ()) {
new_test_ext().execute_with(|| {
test();
CoreFellowship::do_try_state().expect("All invariants must hold after a test");
});
}

fn next_block() {
System::set_block_number(System::block_number() + 1);
}
Expand All @@ -155,9 +163,16 @@ fn next_demotion(who: u64) -> u64 {
member.last_proof + demotion_period[TestClub::rank_of(&who).unwrap() as usize - 1]
}

// helper function for offboard timeout.
fn offboard_timeout(who: u64) -> u64 {
let member = Member::<Test>::get(who).unwrap();
let offboard_timeout = Params::<Test>::get().offboard_timeout;
member.last_proof + offboard_timeout
}

#[test]
fn basic_stuff() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
assert_eq!(CoreFellowship::rank_to_index(0), None);
assert_eq!(CoreFellowship::rank_to_index(1), Some(0));
assert_eq!(CoreFellowship::rank_to_index(9), Some(8));
Expand Down Expand Up @@ -186,7 +201,7 @@ fn set_params_works() {

#[test]
fn induct_works() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
set_rank(0, 0);
assert_ok!(CoreFellowship::import(signed(0)));
set_rank(1, 1);
Expand All @@ -195,24 +210,36 @@ fn induct_works() {
assert_noop!(CoreFellowship::induct(signed(10), 10), DispatchError::BadOrigin);
assert_noop!(CoreFellowship::induct(signed(0), 10), DispatchError::BadOrigin);
assert_ok!(CoreFellowship::induct(signed(1), 10));
assert_eq!(offboard_timeout(10), 2);
assert_noop!(CoreFellowship::induct(signed(1), 10), Error::<Test>::AlreadyInducted);
});
}

#[test]
fn promote_works() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
set_rank(1, 1);
set_rank(2, 2);
assert_ok!(CoreFellowship::import(signed(1)));
assert_noop!(CoreFellowship::promote(signed(1), 10, 1), Error::<Test>::Unranked);

assert_ok!(CoreFellowship::induct(signed(1), 10));
assert_noop!(CoreFellowship::promote(signed(10), 10, 1), DispatchError::BadOrigin);
assert_noop!(CoreFellowship::promote(signed(0), 10, 1), Error::<Test>::NoPermission);
assert_noop!(CoreFellowship::promote(signed(3), 10, 2), Error::<Test>::UnexpectedRank);
run_to(3);

// Member(1) demotion period will elapse, a call to approve / bump is required.
Doordashcon marked this conversation as resolved.
Show resolved Hide resolved
assert_ok!(CoreFellowship::approve(signed(2), 1, 1));
run_to(2);

assert_noop!(CoreFellowship::promote(signed(1), 10, 1), Error::<Test>::TooSoon);

assert_ok!(CoreFellowship::approve(signed(2), 1, 1));
run_to(3);

assert_ok!(CoreFellowship::approve(signed(2), 1, 1));
run_to(4);

assert_ok!(CoreFellowship::promote(signed(1), 10, 1));
set_rank(11, 0);
assert_noop!(CoreFellowship::promote(signed(1), 11, 1), Error::<Test>::NotTracked);
Expand All @@ -221,7 +248,7 @@ fn promote_works() {

#[test]
fn sync_works() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
set_rank(10, 5);
assert_noop!(CoreFellowship::approve(signed(4), 10, 5), Error::<Test>::NoPermission);
assert_noop!(CoreFellowship::approve(signed(6), 10, 6), Error::<Test>::UnexpectedRank);
Expand All @@ -233,7 +260,7 @@ fn sync_works() {

#[test]
fn auto_demote_works() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
set_rank(10, 5);
assert_ok!(CoreFellowship::import(signed(10)));

Expand All @@ -249,7 +276,7 @@ fn auto_demote_works() {

#[test]
fn auto_demote_offboard_works() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
set_rank(10, 1);
assert_ok!(CoreFellowship::import(signed(10)));

Expand All @@ -265,7 +292,7 @@ fn auto_demote_offboard_works() {

#[test]
fn offboard_works() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
assert_noop!(CoreFellowship::offboard(signed(0), 10), Error::<Test>::NotTracked);
set_rank(10, 0);
assert_noop!(CoreFellowship::offboard(signed(0), 10), Error::<Test>::Ranked);
Expand All @@ -282,7 +309,7 @@ fn offboard_works() {

#[test]
fn infinite_demotion_period_works() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
let params = ParamsType {
active_salary: [10; 9],
passive_salary: [10; 9],
Expand All @@ -295,6 +322,7 @@ fn infinite_demotion_period_works() {
set_rank(0, 0);
assert_ok!(CoreFellowship::import(signed(0)));
set_rank(1, 1);

assert_ok!(CoreFellowship::import(signed(1)));

assert_noop!(CoreFellowship::bump(signed(0), 0), Error::<Test>::NothingDoing);
Expand All @@ -304,7 +332,7 @@ fn infinite_demotion_period_works() {

#[test]
fn proof_postpones_auto_demote() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
set_rank(10, 5);
assert_ok!(CoreFellowship::import(signed(10)));

Expand All @@ -317,7 +345,7 @@ fn proof_postpones_auto_demote() {

#[test]
fn promote_postpones_auto_demote() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
set_rank(10, 5);
assert_ok!(CoreFellowship::import(signed(10)));

Expand All @@ -330,7 +358,7 @@ fn promote_postpones_auto_demote() {

#[test]
fn get_salary_works() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
for i in 1..=9u64 {
set_rank(10 + i, i as u16);
assert_ok!(CoreFellowship::import(signed(10 + i)));
Expand All @@ -341,7 +369,7 @@ fn get_salary_works() {

#[test]
fn active_changing_get_salary_works() {
new_test_ext().execute_with(|| {
build_and_execute(|| {
for i in 1..=9u64 {
set_rank(10 + i, i as u16);
assert_ok!(CoreFellowship::import(signed(10 + i)));
Expand Down
Loading