Skip to content

Commit

Permalink
Autofinalise dispute where jury size == 1 (#279)
Browse files Browse the repository at this point in the history
* autocomplete dispute where jury size = 1

* refactor tests to include jury

* fmt

* give briefs a jury!

* fmt

* fix tests from merge

* remove it (#285)

---------

Co-authored-by: Sam Elamin <[email protected]>
  • Loading branch information
f-gate and samelamin authored Dec 8, 2023
1 parent 5c64d8b commit bfa42bf
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 68 deletions.
6 changes: 5 additions & 1 deletion pallets/briefs/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ pub static BOB: AccountId = 126;
pub static CHARLIE: AccountId = 127;
pub static FREELANCER: AccountId = 1270;
pub static TREASURY: AccountId = 200;
pub static JURY_1: AccountId = 1002;
pub static JURY_2: AccountId = 1001;

pub(crate) fn build_test_externality() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::<Test>::default()
Expand Down Expand Up @@ -340,7 +342,9 @@ pub struct MockJurySelector;
impl pallet_fellowship::traits::SelectJury<AccountId> for MockJurySelector {
type JurySize = MaxJuryMembers;
fn select_jury() -> BoundedVec<AccountId, Self::JurySize> {
BoundedVec::new()
vec![JURY_1, JURY_2]
.try_into()
.expect("should be below bound.")
}
}

Expand Down
2 changes: 1 addition & 1 deletion pallets/disputes/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use frame_support::BoundedVec;
use sp_runtime::DispatchError;

use crate::pallet::{AccountIdOf, Config, Dispute};
use traits::DisputeRaiser;
use crate::traits::{DisputeHooks, DisputeRaiser};

impl<T: Config> DisputeRaiser<AccountIdOf<T>> for Pallet<T> {
type DisputeKey = T::DisputeKey;
Expand Down
39 changes: 32 additions & 7 deletions pallets/proposals/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ mod benchmarks {
create_funded_user::<T>("contributor", 1, 1_000_000_000_000_000_000u128);
let bob: T::AccountId =
create_funded_user::<T>("initiator", 1, 1_000_000_000_000_000_000u128);
let jury = get_funded_jury::<T>(10);

let contributions = get_contributions::<T>(vec![alice], 100_000_000_000_000_000u128);
let prop_milestones = get_max_milestones::<T>();
let project_key = create_and_fund_project::<T>(
bob.clone(),
contributions,
prop_milestones,
CurrencyId::Native,
jury,
)
.unwrap();

Expand All @@ -50,6 +53,8 @@ mod benchmarks {
create_funded_user::<T>("initiator", 1, 1_000_000_000_000_000_000u128);
let bob: T::AccountId =
create_funded_user::<T>("contributor", 1, 1_000_000_000_000_000_000u128);
let jury = get_funded_jury::<T>(10);

// TODO: should update the contributors list to have maximum available length
let contributions = get_contributions::<T>(vec![bob.clone()], 1_000_000_000_000u128);
let prop_milestones = get_max_milestones::<T>();
Expand All @@ -58,6 +63,7 @@ mod benchmarks {
contributions,
prop_milestones,
CurrencyId::Native,
jury,
)
.unwrap();

Expand All @@ -81,9 +87,9 @@ mod benchmarks {
create_funded_user::<T>("initiator", 1, 1_000_000_000_000_000_000u128);
let bob: T::AccountId =
create_funded_user::<T>("contributor", 1, 1_000_000_000_000_000_000u128);
let jury = get_funded_jury::<T>(10);
let contributions = get_contributions::<T>(vec![bob.clone()], 100_000_000_000_000_000u128);
let raised_funds = 100_000_000_000_000_000u128.saturated_into();

let milestone_count = <T as Config>::MaxMilestonesPerProject::get();
let prop_milestones = get_milestones(milestone_count as u8);

Expand All @@ -92,6 +98,7 @@ mod benchmarks {
contributions,
prop_milestones,
CurrencyId::Native,
jury,
)
.unwrap();

Expand Down Expand Up @@ -145,6 +152,7 @@ mod benchmarks {
create_funded_user::<T>("initiator", 1, 1_000_000_000_000_000_000u128);
let bob: T::AccountId =
create_funded_user::<T>("contributor", 0, 1_000_000_000_000_000_000u128);
let jury = get_funded_jury::<T>(10);

let contributors: Vec<T::AccountId> = (0
..<T as Config>::MaximumContributorsPerProject::get())
Expand All @@ -160,9 +168,14 @@ mod benchmarks {
.try_into()
.unwrap();

let project_key =
create_and_fund_project::<T>(alice, contributions, prop_milestones, CurrencyId::Native)
.unwrap();
let project_key = create_and_fund_project::<T>(
alice,
contributions,
prop_milestones,
CurrencyId::Native,
jury,
)
.unwrap();

#[extrinsic_call]
raise_dispute(RawOrigin::Signed(bob), project_key, milestone_keys);
Expand All @@ -175,6 +188,7 @@ mod benchmarks {
create_funded_user::<T>("initiator", 1, 1_000_000_000_000_000_000u128);
let bob: T::AccountId =
create_funded_user::<T>("contributor", 0, 1_000_000_000_000_000_000u128);
let jury = get_funded_jury::<T>(10);

let contributors: Vec<T::AccountId> = (0
..<T as Config>::MaximumContributorsPerProject::get())
Expand All @@ -192,9 +206,14 @@ mod benchmarks {
.try_into()
.unwrap();

let project_key =
create_and_fund_project::<T>(alice, contributions, prop_milestones, CurrencyId::Native)
.unwrap();
let project_key = create_and_fund_project::<T>(
alice,
contributions,
prop_milestones,
CurrencyId::Native,
jury,
)
.unwrap();

assert_ok!(crate::Pallet::<T>::raise_dispute(
RawOrigin::Signed(bob.clone()).into(),
Expand Down Expand Up @@ -224,3 +243,9 @@ mod benchmarks {
crate::mock::Test
);
}

fn get_funded_jury<T: Config>(n: u32) -> Vec<AccountIdOf<T>> {
(0..n)
.map(|n| create_funded_user::<T>("jury member", n, 1_000_000_000_000_000_000u128))
.collect::<Vec<AccountIdOf<T>>>()
}
22 changes: 15 additions & 7 deletions pallets/proposals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ pub mod pallet {
OnlyContributorsCanInitiateRefund,
/// Only the ForeignAssetSigner can mint tokens
RequireForeignAssetSigner,
/// A Jury is required to create a project.
JuryRequired,
}

#[pallet::hooks]
Expand Down Expand Up @@ -468,13 +470,18 @@ pub mod pallet {
Error::<T>::CannotRaiseDisputeOnApprovedMilestone
);

<T as Config>::DisputeRaiser::raise_dispute(
project_key,
who,
project.jury,
milestone_keys.clone(),
)?;
ProjectsInDispute::<T>::insert(project_key, milestone_keys);
if project.jury.len() == 1 {
// https://github.com/ImbueNetwork/imbue/issues/270
let _ = <Self as pallet_disputes::traits::DisputeHooks<ProjectKey, MilestoneKey>>::on_dispute_complete(project_key, milestone_keys.to_vec(), pallet_disputes::DisputeResult::Success);
} else {
<T as Config>::DisputeRaiser::raise_dispute(
project_key,
who,
project.jury,
milestone_keys.clone(),
)?;
ProjectsInDispute::<T>::insert(project_key, milestone_keys);
}

Ok(())
}
Expand Down Expand Up @@ -641,6 +648,7 @@ pub mod pallet {
jury: BoundedVec<AccountIdOf<T>, Self::MaxJuryMembers>,
on_creation_funding: FundingPath,
) -> Result<(), DispatchError> {
ensure!(jury.len() > 0, Error::<T>::JuryRequired);
let project_key = crate::ProjectCount::<T>::get().saturating_add(1);

// Take storage deposit only for a Project.
Expand Down
12 changes: 9 additions & 3 deletions pallets/proposals/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,9 +1161,15 @@ mod test {
build_test_externality().execute_with(|| {
let cont = get_contributions::<Test>(vec![BOB, DAVE], 100_000);
let prop_milestones = get_milestones(10);
let project_key =
create_and_fund_project::<Test>(ALICE, cont, prop_milestones, CurrencyId::Native)
.expect("project wasnt created!");
let jury = vec![JURY_1, JURY_2];
let project_key = create_and_fund_project::<Test>(
ALICE,
cont,
prop_milestones,
CurrencyId::Native,
jury,
)
.expect("project wasnt created!");
let milestone_key: MilestoneKey = 0;
let expiry_block: BlockNumber = 10;
let rounds_expiring: BoundedProjectKeysPerBlock<Test> =
Expand Down
3 changes: 3 additions & 0 deletions pallets/proposals/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ pub static DAVE: AccountId = 128;
pub static TREASURY: AccountId = 222;
pub static JOHN: AccountId = 255;

pub static JURY_1: AccountId = 1000;
pub static JURY_2: AccountId = 1001;

pub(crate) fn build_test_externality() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::<Test>::default()
.build_storage()
Expand Down
3 changes: 2 additions & 1 deletion pallets/proposals/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub fn create_and_fund_project<T: Config>(
contributions: ContributionsFor<T>,
proposed_milestones: Vec<ProposedMilestone>,
currency_id: CurrencyId,
jury: Vec<AccountIdOf<T>>,
) -> Result<ProjectKey, DispatchError> {
contributions.iter().for_each(|(acc, c)| {
<T as Config>::MultiCurrency::reserve(currency_id, acc, c.value).unwrap();
Expand All @@ -86,7 +87,7 @@ pub fn create_and_fund_project<T: Config>(
beneficiary,
proposed_milestones.try_into().map_err(|_|Error::<T>::TooManyMilestones)?,
refund_locations,
BoundedVec::new(),
jury.try_into().expect("Too many Jury members."),
FundingPath::TakeFromReserved,
)?;

Expand Down
Loading

0 comments on commit bfa42bf

Please sign in to comment.