Skip to content

Commit

Permalink
Optional fellowship switch (#280)
Browse files Browse the repository at this point in the history
* add test for approved applicant

* fmt

* add ensure role to runtime

---------

Co-authored-by: Sam Elamin <hussam.elamin@gmail.com>
  • Loading branch information
f-gate and samelamin authored Dec 5, 2023
1 parent 40e7f54 commit 210ccc3
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 9 deletions.
10 changes: 7 additions & 3 deletions pallets/briefs/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ mod benchmarks {
brief_id,
CurrencyId::Native,
milestones,
false,
);
assert_last_event::<T>(Event::<T>::BriefSubmitted(caller, brief_id).into());
}
Expand All @@ -63,7 +64,8 @@ mod benchmarks {
initial_contribution,
brief_id,
CurrencyId::Native,
milestones
milestones,
false,
));
let brief_owner: T::AccountId = brief_owners[0].clone();
// (brief_owner, brief_id, contribution)
Expand Down Expand Up @@ -93,7 +95,8 @@ mod benchmarks {
initial_contribution,
brief_id,
CurrencyId::Native,
milestones
milestones,
false,
));
// (origin, brief_id)
#[extrinsic_call]
Expand All @@ -118,7 +121,8 @@ mod benchmarks {
initial_contribution,
brief_id,
CurrencyId::Native,
milestones
milestones,
false,
));
// (origin, brief_id)
#[extrinsic_call]
Expand Down
1 change: 1 addition & 0 deletions pallets/briefs/src/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn create_proposal_from_brief() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
);

assert_ok!(BriefsMod::commence_work(
Expand Down
9 changes: 9 additions & 0 deletions pallets/briefs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod pallet {
use frame_system::pallet_prelude::*;
use orml_traits::{MultiCurrency, MultiReservableCurrency};
use pallet_deposits::traits::DepositHandler;
use pallet_fellowship::traits::EnsureRole;
use pallet_fellowship::traits::SelectJury;
use pallet_proposals::traits::IntoProposal;
use pallet_proposals::{Contribution, FundingPath, ProposedMilestone};
Expand Down Expand Up @@ -82,9 +83,12 @@ pub mod pallet {
type MaxMilestonesPerBrief: Get<u32>;
/// Storage deposits.
type BriefStorageItem: Get<StorageItemOf<Self>>;
/// Handler for deposits.
type DepositHandler: DepositHandler<BalanceOf<Self>, AccountIdOf<Self>>;
/// The type that selects a list of jury members.
type JurySelector: SelectJury<AccountIdOf<Self>>;
/// Type for ensuring an account is of a given fellowship role.
type EnsureRole: pallet_fellowship::traits::EnsureRole<AccountIdOf<Self>>;
/// The weight info for the extrinsics.
type WeightInfo: WeightInfoT;
}
Expand Down Expand Up @@ -170,9 +174,14 @@ pub mod pallet {
brief_id: BriefHash,
currency_id: CurrencyId,
milestones: BoundedProposedMilestones<T>,
require_fellowship: bool,
) -> DispatchResult {
let who = ensure_signed(origin)?;

if require_fellowship {
T::EnsureRole::ensure_role(&applicant, pallet_fellowship::Role::Freelancer, None)?;
}

ensure!(
Briefs::<T>::get(brief_id).is_none(),
Error::<T>::BriefAlreadyExists
Expand Down
30 changes: 29 additions & 1 deletion pallets/briefs/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use sp_runtime::{
};

use pallet_deposits::traits::DepositHandler;
use pallet_fellowship::traits::FellowshipHandle;
use pallet_fellowship::Role;
use sp_std::{
convert::{TryFrom, TryInto},
str,
Expand Down Expand Up @@ -57,6 +59,7 @@ frame_support::construct_runtime!(
BriefsMod: pallet_briefs::{Pallet, Call, Storage, Event<T>},
Proposals: pallet_proposals::{Pallet, Call, Storage, Event<T>},
Identity: pallet_identity::{Pallet, Call, Storage, Event<T>},
Fellowship: pallet_fellowship::{Pallet, Call, Storage, Event<T>},
}
);

Expand Down Expand Up @@ -209,6 +212,7 @@ impl pallet_briefs::Config for Test {
type DepositHandler = MockDepositHandler;
type WeightInfo = pallet_briefs::WeightInfo<Self>;
type JurySelector = MockJurySelector;
type EnsureRole = pallet_fellowship::impls::EnsureFellowshipRole<Self>;
}

parameter_types! {
Expand Down Expand Up @@ -273,13 +277,36 @@ impl pallet_identity::Config for Test {
type WeightInfo = ();
}

parameter_types! {
pub MaxCandidatesPerShortlist: u32 = 100;
pub ShortlistPeriod: BlockNumber = 100;
pub MembershipDeposit: Balance = 50_000_000;
pub SlashAccount: AccountId = 1;
pub DepositCurrencyId: CurrencyId = CurrencyId::Native;
}

impl pallet_fellowship::Config for Test {
type RuntimeEvent = RuntimeEvent;
type MultiCurrency = Tokens;
type ForceAuthority = EnsureRoot<AccountId>;
type MaxCandidatesPerShortlist = MaxCandidatesPerShortlist;
type ShortlistPeriod = ShortlistPeriod;
type MembershipDeposit = MembershipDeposit;
type DepositCurrencyId = DepositCurrencyId;
type SlashAccount = SlashAccount;
type Permissions = pallet_fellowship::impls::VetterAndFreelancerAllPermissions;
type WeightInfo = pallet_fellowship::weights::WeightInfo<Test>;
}

parameter_types! {
pub const UnitWeightCost: u64 = 10;
pub const MaxInstructions: u32 = 100;
}

pub static ALICE: AccountId = 125;
pub static BOB: AccountId = 126;
pub static CHARLIE: AccountId = 127;
pub static FREELANCER: AccountId = 1270;
pub static TREASURY: AccountId = 200;

pub(crate) fn build_test_externality() -> sp_io::TestExternalities {
Expand All @@ -292,7 +319,7 @@ pub(crate) fn build_test_externality() -> sp_io::TestExternalities {
.unwrap();
orml_tokens::GenesisConfig::<Test> {
balances: {
vec![ALICE, BOB, CHARLIE]
vec![ALICE, BOB, CHARLIE, FREELANCER]
.into_iter()
.map(|id| (id, CurrencyId::Native, 1000000))
.collect::<Vec<_>>()
Expand All @@ -303,6 +330,7 @@ pub(crate) fn build_test_externality() -> sp_io::TestExternalities {

let mut ext = sp_io::TestExternalities::new(t);
ext.execute_with(|| {
pallet_fellowship::Roles::<Test>::insert(&FREELANCER, (Role::Freelancer, 10));
System::set_block_number(1);
});
ext
Expand Down
53 changes: 51 additions & 2 deletions pallets/briefs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,53 @@ use crate::*;
use common_types::CurrencyId;
use frame_support::{assert_noop, assert_ok, pallet_prelude::*};
use orml_traits::{MultiCurrency, MultiReservableCurrency};
use pallet_fellowship::traits::EnsureRole;
use pallet_proposals::{BoundedProposedMilestones, Projects, ProposedMilestone};
use sp_arithmetic::per_things::Percent;
use sp_runtime::DispatchError::BadOrigin;

use std::convert::TryInto;

#[test]
fn create_brief_not_approved_applicant() {
build_test_externality().execute_with(|| {
// TODO:
// Only accounts in the fellowship can apply for work
assert_noop!(
BriefsMod::create_brief(
RuntimeOrigin::signed(BOB),
get_brief_owners(u32::MAX),
ALICE,
100000,
10000,
gen_hash(1),
CurrencyId::Native,
get_milestones(10),
true,
),
BadOrigin
);
});
}

#[test]
fn create_brief_approved_applicant() {
build_test_externality().execute_with(|| {
assert_ok!(<Test as Config>::EnsureRole::ensure_role(
&FREELANCER,
pallet_fellowship::Role::Freelancer,
None
));

assert_ok!(BriefsMod::create_brief(
RuntimeOrigin::signed(BOB),
get_brief_owners(10),
FREELANCER,
100000,
10000,
gen_hash(1),
CurrencyId::Native,
get_milestones(10),
true,
));
});
}

Expand All @@ -32,6 +69,7 @@ fn create_brief_brief_owner_overflow() {
gen_hash(1),
CurrencyId::Native,
get_milestones(10),
false,
),
Error::<Test>::TooManyBriefOwners
);
Expand All @@ -50,6 +88,7 @@ fn create_brief_with_no_contribution_ok() {
gen_hash(1),
CurrencyId::Native,
get_milestones(10),
false,
));
});
}
Expand All @@ -70,6 +109,7 @@ fn create_brief_no_contribution_and_contribute() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
));

(0..5).for_each(|_| {
Expand Down Expand Up @@ -111,6 +151,7 @@ fn contribute_to_brief_not_brief_owner() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
));

assert_noop!(
Expand Down Expand Up @@ -139,6 +180,7 @@ fn contribute_to_brief_more_than_total_ok() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
));
assert_ok!(BriefsMod::contribute_to_brief(
RuntimeOrigin::signed(BOB),
Expand All @@ -163,6 +205,7 @@ fn create_brief_already_exists() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
));

assert_noop!(
Expand All @@ -175,6 +218,7 @@ fn create_brief_already_exists() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
),
Error::<Test>::BriefAlreadyExists
);
Expand All @@ -196,6 +240,7 @@ fn only_applicant_can_start_work() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
));

assert_noop!(
Expand Down Expand Up @@ -225,6 +270,7 @@ fn initial_contribution_and_extra_contribution_aggregates() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
));

assert_ok!(BriefsMod::contribute_to_brief(
Expand Down Expand Up @@ -261,6 +307,7 @@ fn reserved_funds_are_transferred_to_project_kitty() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
);

assert_ok!(BriefsMod::commence_work(
Expand Down Expand Up @@ -293,6 +340,7 @@ fn cancel_brief_works() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
));

assert_ok!(BriefsMod::contribute_to_brief(
Expand Down Expand Up @@ -359,6 +407,7 @@ fn cancel_brief_not_brief_owner() {
brief_id,
CurrencyId::Native,
get_milestones(10),
false,
));

assert_noop!(
Expand Down
2 changes: 1 addition & 1 deletion pallets/fellowship/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use sp_std::{vec, vec::Vec};
/// Ensure that a account is of a given role.
/// Used in other pallets like an ensure origin.
pub struct EnsureFellowshipRole<T>(T);
impl<T: Config> EnsureRole<AccountIdOf<T>, Role> for EnsureFellowshipRole<T> {
impl<T: Config> EnsureRole<AccountIdOf<T>> for EnsureFellowshipRole<T> {
type Success = ();

fn ensure_role(
Expand Down
4 changes: 2 additions & 2 deletions pallets/fellowship/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::Rank;
use crate::{Rank, Role};
use codec::{FullCodec, FullEncode};
use frame_support::{pallet_prelude::*, weights::Weight};
use sp_runtime::DispatchError;
Expand All @@ -19,7 +19,7 @@ pub trait FellowshipHandle<AccountId> {
fn revoke_fellowship(who: &AccountId, slash_deposit: bool) -> Result<(), DispatchError>;
}

pub trait EnsureRole<AccountId, Role> {
pub trait EnsureRole<AccountId> {
type Success;
fn ensure_role(
acc: &AccountId,
Expand Down
1 change: 1 addition & 0 deletions runtime/imbue-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,7 @@ impl pallet_briefs::Config for Runtime {
type BriefStorageItem = BriefStorageItem;
type DepositHandler = Deposits;
type JurySelector = PointerBasedJurySelector<Runtime>;
type EnsureRole = pallet_fellowship::impls::EnsureFellowshipRole<Runtime>;
}

parameter_types! {
Expand Down

0 comments on commit 210ccc3

Please sign in to comment.