Skip to content

Commit

Permalink
pallet-core-fellowship: import an unimported member on approve (#2883)
Browse files Browse the repository at this point in the history
To align with the documentation of the approve call, we import an
unimported member on approval.

No changes have been made to the benchmarks as they already cover the
worst-case scenario.
  • Loading branch information
muharem authored Jan 9, 2024
1 parent 0ff3f4d commit d288c81
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
9 changes: 9 additions & 0 deletions prdoc/pr_2883.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
title: "pallet-core-fellowship: import an unimported on approve"

doc:
- audience: Runtime User
description: |
To align with the documentation of the approve call, we import an untracked member on approval.

crates:
- name: "pallet-core-fellowship"
26 changes: 18 additions & 8 deletions substrate/frame/core-fellowship/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ pub mod pallet {
ensure!(at_rank > 0, Error::<T, I>::InvalidRank);
let rank = T::Members::rank_of(&who).ok_or(Error::<T, I>::Unranked)?;
ensure!(rank == at_rank, Error::<T, I>::UnexpectedRank);
let mut member = Member::<T, I>::get(&who).ok_or(Error::<T, I>::NotTracked)?;
let mut member = if let Some(m) = Member::<T, I>::get(&who) {
m
} else {
Self::import_member(who.clone(), rank)
};

member.last_proof = frame_system::Pallet::<T>::block_number();
Member::<T, I>::insert(&who, &member);
Expand Down Expand Up @@ -518,13 +522,7 @@ pub mod pallet {
let who = ensure_signed(origin)?;
ensure!(!Member::<T, I>::contains_key(&who), Error::<T, I>::AlreadyInducted);
let rank = T::Members::rank_of(&who).ok_or(Error::<T, I>::Unranked)?;

let now = frame_system::Pallet::<T>::block_number();
Member::<T, I>::insert(
&who,
MemberStatus { is_active: true, last_promotion: 0u32.into(), last_proof: now },
);
Self::deposit_event(Event::<T, I>::Imported { who, rank });
let _ = Self::import_member(who, rank);

Ok(Pays::No.into())
}
Expand All @@ -548,6 +546,18 @@ pub mod pallet {
Self::deposit_event(e);
}
}

fn import_member(who: T::AccountId, rank: RankOf<T, I>) -> MemberStatusOf<T> {
let now = frame_system::Pallet::<T>::block_number();
let status = MemberStatus {
is_active: true,
last_promotion: BlockNumberFor::<T>::zero(),
last_proof: now,
};
Member::<T, I>::insert(&who, status.clone());
Self::deposit_event(Event::<T, I>::Imported { who, rank });
status
}
}

impl<T: Config<I>, I: 'static> GetSalary<RankOf<T, I>, T::AccountId, T::Balance> for Pallet<T, I> {
Expand Down
8 changes: 8 additions & 0 deletions substrate/frame/core-fellowship/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,11 @@ fn active_changing_get_salary_works() {
}
});
}

#[test]
fn approve_imports_not_tracked_member() {
new_test_ext().execute_with(|| {
set_rank(10, 5);
assert_ok!(CoreFellowship::approve(signed(5), 10, 5));
});
}

0 comments on commit d288c81

Please sign in to comment.