Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
feat: add event SkepticsChosen event in society (#13291)
Browse files Browse the repository at this point in the history
* feat: add event SkepticsChosen event in society

* feat: add test for SkepticsChosen event
  • Loading branch information
gitofdeepanshu authored and Ank4n committed Feb 5, 2023
1 parent 6453a4a commit ff8a71f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
10 changes: 7 additions & 3 deletions frame/society/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ pub mod pallet {
Unfounded { founder: T::AccountId },
/// Some funds were deposited into the society account.
Deposit { value: BalanceOf<T, I> },
/// A group of members has been choosen as Skeptics
SkepticsChosen { skeptics: Vec<T::AccountId> },
}

/// The first member.
Expand Down Expand Up @@ -1610,12 +1612,14 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {

// Select sqrt(n) random members from the society and make them skeptics.
let pick_member =
|_| pick_item(&mut rng, &members[..]).expect("exited if members empty; qed");
for skeptic in (0..members.len().integer_sqrt()).map(pick_member) {
|_| pick_item(&mut rng, &members[..]).expect("exited if members empty; qed").clone();
let skeptics = (0..members.len().integer_sqrt()).map(pick_member).collect::<Vec<_>>();
skeptics.iter().for_each(|skeptic| {
for Bid { who: c, .. } in candidates.iter() {
<Votes<T, I>>::insert(c, skeptic, Vote::Skeptic);
}
}
});
Self::deposit_event(Event::<T, I>::SkepticsChosen { skeptics });
}

/// Attempt to slash the payout of some member. Return the total amount that was deducted.
Expand Down
7 changes: 7 additions & 0 deletions frame/society/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,20 @@ fn payout_works() {
#[test]
fn basic_new_member_skeptic_works() {
EnvBuilder::new().execute(|| {
// NOTE: events are not deposited in the genesis event
System::set_block_number(1);

assert_eq!(Strikes::<Test>::get(10), 0);
assert_ok!(Society::bid(RuntimeOrigin::signed(20), 0));
run_to_block(4);
assert_eq!(Society::candidates(), vec![create_bid(0, 20, BidKind::Deposit(25))]);
run_to_block(8);
assert_eq!(Society::members(), vec![10]);
assert_eq!(Strikes::<Test>::get(10), 1);

System::assert_last_event(mock::RuntimeEvent::Society(crate::Event::SkepticsChosen {
skeptics: vec![10],
}));
});
}

Expand Down

0 comments on commit ff8a71f

Please sign in to comment.