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

Commit 404b8c9

Browse files
authored
OpenGov: Abstentions (#12842)
* OpenGov: Abstentions * Tests
1 parent 05ebde1 commit 404b8c9

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

frame/conviction-voting/src/tests.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,14 @@ fn nay(amount: u64, conviction: u8) -> AccountVote<u64> {
237237
AccountVote::Standard { vote, balance: amount }
238238
}
239239

240+
fn split(aye: u64, nay: u64) -> AccountVote<u64> {
241+
AccountVote::Split { aye, nay }
242+
}
243+
244+
fn split_abstain(aye: u64, nay: u64, abstain: u64) -> AccountVote<u64> {
245+
AccountVote::SplitAbstain { aye, nay, abstain }
246+
}
247+
240248
fn tally(index: u8) -> TallyOf<Test> {
241249
<TestPolls as Polling<TallyOf<Test>>>::as_ongoing(index).expect("No poll").0
242250
}
@@ -295,6 +303,49 @@ fn basic_voting_works() {
295303
});
296304
}
297305

306+
#[test]
307+
fn split_voting_works() {
308+
new_test_ext().execute_with(|| {
309+
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, split(10, 0)));
310+
assert_eq!(tally(3), Tally::from_parts(1, 0, 10));
311+
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, split(5, 5)));
312+
assert_eq!(tally(3), Tally::from_parts(0, 0, 5));
313+
assert_eq!(Balances::usable_balance(1), 0);
314+
315+
assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), None, 3));
316+
assert_eq!(tally(3), Tally::from_parts(0, 0, 0));
317+
318+
assert_ok!(Voting::unlock(RuntimeOrigin::signed(1), class(3), 1));
319+
assert_eq!(Balances::usable_balance(1), 10);
320+
});
321+
}
322+
323+
#[test]
324+
fn abstain_voting_works() {
325+
new_test_ext().execute_with(|| {
326+
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, split_abstain(0, 0, 10)));
327+
assert_eq!(tally(3), Tally::from_parts(0, 0, 10));
328+
assert_ok!(Voting::vote(RuntimeOrigin::signed(2), 3, split_abstain(0, 0, 20)));
329+
assert_eq!(tally(3), Tally::from_parts(0, 0, 30));
330+
assert_ok!(Voting::vote(RuntimeOrigin::signed(2), 3, split_abstain(10, 0, 10)));
331+
assert_eq!(tally(3), Tally::from_parts(1, 0, 30));
332+
assert_eq!(Balances::usable_balance(1), 0);
333+
assert_eq!(Balances::usable_balance(2), 0);
334+
335+
assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), None, 3));
336+
assert_eq!(tally(3), Tally::from_parts(1, 0, 20));
337+
338+
assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(2), None, 3));
339+
assert_eq!(tally(3), Tally::from_parts(0, 0, 0));
340+
341+
assert_ok!(Voting::unlock(RuntimeOrigin::signed(1), class(3), 1));
342+
assert_eq!(Balances::usable_balance(1), 10);
343+
344+
assert_ok!(Voting::unlock(RuntimeOrigin::signed(2), class(3), 2));
345+
assert_eq!(Balances::usable_balance(2), 20);
346+
});
347+
}
348+
298349
#[test]
299350
fn voting_balance_gets_locked() {
300351
new_test_ext().execute_with(|| {

frame/conviction-voting/src/types.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ impl<
147147
self.ayes = self.ayes.checked_add(&aye.votes)?;
148148
self.nays = self.nays.checked_add(&nay.votes)?;
149149
},
150+
AccountVote::SplitAbstain { aye, nay, abstain } => {
151+
let aye = Conviction::None.votes(aye);
152+
let nay = Conviction::None.votes(nay);
153+
let abstain = Conviction::None.votes(abstain);
154+
self.support =
155+
self.support.checked_add(&aye.capital)?.checked_add(&abstain.capital)?;
156+
self.ayes = self.ayes.checked_add(&aye.votes)?;
157+
self.nays = self.nays.checked_add(&nay.votes)?;
158+
},
150159
}
151160
Some(())
152161
}
@@ -171,6 +180,15 @@ impl<
171180
self.ayes = self.ayes.checked_sub(&aye.votes)?;
172181
self.nays = self.nays.checked_sub(&nay.votes)?;
173182
},
183+
AccountVote::SplitAbstain { aye, nay, abstain } => {
184+
let aye = Conviction::None.votes(aye);
185+
let nay = Conviction::None.votes(nay);
186+
let abstain = Conviction::None.votes(abstain);
187+
self.support =
188+
self.support.checked_sub(&aye.capital)?.checked_sub(&abstain.capital)?;
189+
self.ayes = self.ayes.checked_sub(&aye.votes)?;
190+
self.nays = self.nays.checked_sub(&nay.votes)?;
191+
},
174192
}
175193
Some(())
176194
}

frame/conviction-voting/src/vote.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ pub enum AccountVote<Balance> {
7474
/// A split vote with balances given for both ways, and with no conviction, useful for
7575
/// parachains when voting.
7676
Split { aye: Balance, nay: Balance },
77+
/// A split vote with balances given for both ways as well as abstentions, and with no
78+
/// conviction, useful for parachains when voting, other off-chain aggregate accounts and
79+
/// individuals who wish to abstain.
80+
SplitAbstain { aye: Balance, nay: Balance, abstain: Balance },
7781
}
7882

7983
impl<Balance: Saturating> AccountVote<Balance> {
@@ -94,6 +98,8 @@ impl<Balance: Saturating> AccountVote<Balance> {
9498
match self {
9599
AccountVote::Standard { balance, .. } => balance,
96100
AccountVote::Split { aye, nay } => aye.saturating_add(nay),
101+
AccountVote::SplitAbstain { aye, nay, abstain } =>
102+
aye.saturating_add(nay).saturating_add(abstain),
97103
}
98104
}
99105

0 commit comments

Comments
 (0)