Skip to content

Commit

Permalink
extend Tally to include no-rank votes, update test
Browse files Browse the repository at this point in the history
  • Loading branch information
abdbee committed Dec 8, 2024
1 parent b262c7a commit 072f002
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 21 deletions.
51 changes: 40 additions & 11 deletions substrate/frame/ranked-collective/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,17 @@ pub type Votes = u32;
#[codec(mel_bound())]
pub struct Tally<T, I, M: GetMaxVoters> {
bare_ayes: MemberIndex,
out_of_rank_ayes: MemberIndex,
out_of_rank_nays: MemberIndex,
ayes: Votes,
nays: Votes,
dummy: PhantomData<(T, I, M)>,

}

impl<T: Config<I>, I: 'static, M: GetMaxVoters> Tally<T, I, M> {
pub fn from_parts(bare_ayes: MemberIndex, ayes: Votes, nays: Votes) -> Self {
Tally { bare_ayes, ayes, nays, dummy: PhantomData }
pub fn from_parts(bare_ayes: MemberIndex, out_of_rank_ayes: MemberIndex, out_of_rank_nays: MemberIndex, ayes: Votes, nays: Votes) -> Self {
Tally { bare_ayes, out_of_rank_ayes, out_of_rank_nays, ayes, nays, dummy: PhantomData }
}
}

Expand All @@ -123,7 +126,7 @@ impl<T: Config<I>, I: 'static, M: GetMaxVoters<Class = ClassOf<T, I>>>
VoteTally<Votes, ClassOf<T, I>> for Tally<T, I, M>
{
fn new(_: ClassOf<T, I>) -> Self {
Self { bare_ayes: 0, ayes: 0, nays: 0, dummy: PhantomData }
Self { bare_ayes: 0, out_of_rank_ayes: 0, out_of_rank_nays: 0, ayes: 0, nays: 0, dummy: PhantomData }
}
fn ayes(&self, _: ClassOf<T, I>) -> Votes {
self.bare_ayes
Expand Down Expand Up @@ -629,22 +632,48 @@ pub mod pallet {
PollStatus::Ongoing(ref mut tally, class) => {
match Voting::<T, I>::get(&poll, &who) {
Some(Aye(votes)) => {
tally.bare_ayes.saturating_dec();
tally.ayes.saturating_reduce(votes);
if votes > 0 {
tally.bare_ayes.saturating_dec();
tally.ayes.saturating_reduce(votes);
} else {
tally.out_of_rank_ayes.saturating_dec();
}

},
Some(Nay(votes)) => {
if votes > 0 {
tally.nays.saturating_reduce(votes)
} else {
tally.out_of_rank_nays.saturating_dec();
}
},
Some(Nay(votes)) => tally.nays.saturating_reduce(votes),
None => pays = Pays::No,
}
let min_rank = T::MinRankOfClass::convert(class);
let votes = Self::rank_to_votes(record.rank, min_rank).unwrap_or(0);
let vote = VoteRecord::from((aye, votes));
match aye {
true => {
tally.bare_ayes.saturating_inc();
tally.ayes.saturating_accrue(votes);
match votes {
0 => {
match aye {
true => {
tally.out_of_rank_ayes.saturating_inc();
},
false => {
tally.out_of_rank_nays.saturating_inc();
},
}
},
_ => {
match aye {
true => {
tally.bare_ayes.saturating_inc();
tally.ayes.saturating_accrue(votes);
},
false => tally.nays.saturating_accrue(votes),
}
},
false => tally.nays.saturating_accrue(votes),
}

Voting::<T, I>::insert(&poll, &who, &vote);
Ok((tally.clone(), vote))
},
Expand Down
22 changes: 12 additions & 10 deletions substrate/frame/ranked-collective/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ parameter_types! {
pub static Polls: BTreeMap<u8, TestPollState> = vec![
(1, Completed(1, true)),
(2, Completed(2, false)),
(3, Ongoing(Tally::from_parts(0, 0, 0), 1)),
(3, Ongoing(Tally::from_parts(0, 0, 0, 0, 0), 1)),
].into_iter().collect();
}

Expand Down Expand Up @@ -253,7 +253,7 @@ fn completed_poll_should_panic() {
#[test]
fn basic_stuff() {
ExtBuilder::default().build_and_execute(|| {
assert_eq!(tally(3), Tally::from_parts(0, 0, 0));
assert_eq!(tally(3), Tally::from_parts(0, 0, 0, 0, 0));
});
}

Expand Down Expand Up @@ -412,22 +412,24 @@ fn voting_works() {
assert_ok!(Club::promote_member(RuntimeOrigin::root(), 3));

assert_ok!(Club::vote(RuntimeOrigin::signed(0), 3, true));
assert_eq!(tally(3), Tally::from_parts(1, 0, 0));
assert_eq!(tally(3), Tally::from_parts(0, 1, 0, 0, 0));
assert_ok!(Club::vote(RuntimeOrigin::signed(0), 3, false));
assert_eq!(tally(3), Tally::from_parts(0, 0, 1, 0, 0));

assert_ok!(Club::vote(RuntimeOrigin::signed(1), 3, true));
assert_eq!(tally(3), Tally::from_parts(2, 1, 0));
assert_eq!(tally(3), Tally::from_parts(1, 0, 1, 1, 0));
assert_ok!(Club::vote(RuntimeOrigin::signed(1), 3, false));
assert_eq!(tally(3), Tally::from_parts(1, 0, 1));
assert_eq!(tally(3), Tally::from_parts(0, 0, 1, 0, 1));

assert_ok!(Club::vote(RuntimeOrigin::signed(2), 3, true));
assert_eq!(tally(3), Tally::from_parts(2, 3, 1));
assert_eq!(tally(3), Tally::from_parts(1, 0, 1, 3, 1));
assert_ok!(Club::vote(RuntimeOrigin::signed(2), 3, false));
assert_eq!(tally(3), Tally::from_parts(1, 0, 4));
assert_eq!(tally(3), Tally::from_parts(0, 0, 1, 0, 4));

assert_ok!(Club::vote(RuntimeOrigin::signed(3), 3, true));
assert_eq!(tally(3), Tally::from_parts(2, 6, 4));
assert_eq!(tally(3), Tally::from_parts(1, 0, 1, 6, 4));
assert_ok!(Club::vote(RuntimeOrigin::signed(3), 3, false));
assert_eq!(tally(3), Tally::from_parts(1, 0, 10));
assert_eq!(tally(3), Tally::from_parts(0, 0, 1, 0, 10));
});
}

Expand Down Expand Up @@ -588,7 +590,7 @@ fn tally_support_correct() {
assert_ok!(Club::promote_member(RuntimeOrigin::root(), 3));

// init tally with 1 aye vote.
let tally: TallyOf<Test> = Tally::from_parts(1, 1, 0);
let tally: TallyOf<Test> = Tally::from_parts(1, 0, 0, 1, 0);

// with minRank(Class) = Class
// for class 3, 100% support.
Expand Down

0 comments on commit 072f002

Please sign in to comment.