Skip to content

Commit ef4797d

Browse files
rockbmbdudo50
authored andcommitted
Add and test events in pallet-conviction-voting (paritytech#6544)
# Description paritytech#4613 introduced events for `pallet_conviction_voting::{vote, remove_vote, remove_other_vote}`. However: 1. it did not include `unlock` 2. the pallet's unit tests were missing an update ## Integration N/A ## Review Notes This is as paritytech#6261 was, so it is a trivial change.
1 parent 00ac1d3 commit ef4797d

4 files changed

Lines changed: 96 additions & 19 deletions

File tree

prdoc/pr_6544.prdoc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
2+
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json
3+
4+
title: Add and test events to conviction voting pallet
5+
6+
doc:
7+
- audience: Runtime Dev
8+
description: |
9+
Add event for the unlocking of an expired conviction vote's funds, and test recently added
10+
voting events.
11+
12+
crates:
13+
- name: pallet-conviction-voting
14+
bump: major

substrate/frame/conviction-voting/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,12 @@ pub mod pallet {
171171
Delegated(T::AccountId, T::AccountId),
172172
/// An \[account\] has cancelled a previous delegation operation.
173173
Undelegated(T::AccountId),
174-
/// An account that has voted
174+
/// An account has voted
175175
Voted { who: T::AccountId, vote: AccountVote<BalanceOf<T, I>> },
176-
/// A vote that been removed
176+
/// A vote has been removed
177177
VoteRemoved { who: T::AccountId, vote: AccountVote<BalanceOf<T, I>> },
178+
/// The lockup period of a conviction vote expired, and the funds have been unlocked.
179+
VoteUnlocked { who: T::AccountId, class: ClassOf<T, I> },
178180
}
179181

180182
#[pallet::error]
@@ -315,6 +317,7 @@ pub mod pallet {
315317
ensure_signed(origin)?;
316318
let target = T::Lookup::lookup(target)?;
317319
Self::update_lock(&class, &target);
320+
Self::deposit_event(Event::VoteUnlocked { who: target, class });
318321
Ok(())
319322
}
320323

substrate/frame/conviction-voting/src/tests.rs

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -238,27 +238,52 @@ fn basic_stuff() {
238238
fn basic_voting_works() {
239239
new_test_ext().execute_with(|| {
240240
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, aye(2, 5)));
241+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
242+
who: 1,
243+
vote: aye(2, 5),
244+
}));
241245
assert_eq!(tally(3), Tally::from_parts(10, 0, 2));
242246
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, nay(2, 5)));
247+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
248+
who: 1,
249+
vote: nay(2, 5),
250+
}));
243251
assert_eq!(tally(3), Tally::from_parts(0, 10, 0));
244252
assert_eq!(Balances::usable_balance(1), 8);
245253

246254
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, aye(5, 1)));
255+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
256+
who: 1,
257+
vote: aye(5, 1),
258+
}));
247259
assert_eq!(tally(3), Tally::from_parts(5, 0, 5));
248260
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, nay(5, 1)));
249261
assert_eq!(tally(3), Tally::from_parts(0, 5, 0));
250262
assert_eq!(Balances::usable_balance(1), 5);
251263

252264
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, aye(10, 0)));
265+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
266+
who: 1,
267+
vote: aye(10, 0),
268+
}));
253269
assert_eq!(tally(3), Tally::from_parts(1, 0, 10));
270+
254271
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, nay(10, 0)));
255272
assert_eq!(tally(3), Tally::from_parts(0, 1, 0));
256273
assert_eq!(Balances::usable_balance(1), 0);
257274

258275
assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), None, 3));
276+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteRemoved {
277+
who: 1,
278+
vote: nay(10, 0),
279+
}));
259280
assert_eq!(tally(3), Tally::from_parts(0, 0, 0));
260281

261282
assert_ok!(Voting::unlock(RuntimeOrigin::signed(1), class(3), 1));
283+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteUnlocked {
284+
who: 1,
285+
class: class(3),
286+
}));
262287
assert_eq!(Balances::usable_balance(1), 10);
263288
});
264289
}
@@ -267,15 +292,32 @@ fn basic_voting_works() {
267292
fn split_voting_works() {
268293
new_test_ext().execute_with(|| {
269294
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, split(10, 0)));
295+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
296+
who: 1,
297+
vote: split(10, 0),
298+
}));
270299
assert_eq!(tally(3), Tally::from_parts(1, 0, 10));
300+
271301
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, split(5, 5)));
302+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
303+
who: 1,
304+
vote: split(5, 5),
305+
}));
272306
assert_eq!(tally(3), Tally::from_parts(0, 0, 5));
273307
assert_eq!(Balances::usable_balance(1), 0);
274308

275309
assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), None, 3));
310+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteRemoved {
311+
who: 1,
312+
vote: split(5, 5),
313+
}));
276314
assert_eq!(tally(3), Tally::from_parts(0, 0, 0));
277315

278316
assert_ok!(Voting::unlock(RuntimeOrigin::signed(1), class(3), 1));
317+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteUnlocked {
318+
who: 1,
319+
class: class(3),
320+
}));
279321
assert_eq!(Balances::usable_balance(1), 10);
280322
});
281323
}
@@ -284,25 +326,48 @@ fn split_voting_works() {
284326
fn abstain_voting_works() {
285327
new_test_ext().execute_with(|| {
286328
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, split_abstain(0, 0, 10)));
329+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
330+
who: 1,
331+
vote: split_abstain(0, 0, 10),
332+
}));
287333
assert_eq!(tally(3), Tally::from_parts(0, 0, 10));
288-
assert_ok!(Voting::vote(RuntimeOrigin::signed(2), 3, split_abstain(0, 0, 20)));
289-
assert_eq!(tally(3), Tally::from_parts(0, 0, 30));
290-
assert_ok!(Voting::vote(RuntimeOrigin::signed(2), 3, split_abstain(10, 0, 10)));
291-
assert_eq!(tally(3), Tally::from_parts(1, 0, 30));
334+
335+
assert_ok!(Voting::vote(RuntimeOrigin::signed(6), 3, split_abstain(10, 0, 20)));
336+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
337+
who: 6,
338+
vote: split_abstain(10, 0, 20),
339+
}));
340+
assert_eq!(tally(3), Tally::from_parts(1, 0, 40));
341+
342+
assert_ok!(Voting::vote(RuntimeOrigin::signed(6), 3, split_abstain(0, 0, 40)));
343+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
344+
who: 6,
345+
vote: split_abstain(0, 0, 40),
346+
}));
347+
348+
assert_eq!(tally(3), Tally::from_parts(0, 0, 50));
292349
assert_eq!(Balances::usable_balance(1), 0);
293-
assert_eq!(Balances::usable_balance(2), 0);
350+
assert_eq!(Balances::usable_balance(6), 20);
294351

295352
assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), None, 3));
296-
assert_eq!(tally(3), Tally::from_parts(1, 0, 20));
297-
298-
assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(2), None, 3));
353+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteRemoved {
354+
who: 1,
355+
vote: split_abstain(0, 0, 10),
356+
}));
357+
assert_eq!(tally(3), Tally::from_parts(0, 0, 40));
358+
359+
assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(6), Some(class(3)), 3));
360+
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteRemoved {
361+
who: 6,
362+
vote: split_abstain(0, 0, 40),
363+
}));
299364
assert_eq!(tally(3), Tally::from_parts(0, 0, 0));
300365

301366
assert_ok!(Voting::unlock(RuntimeOrigin::signed(1), class(3), 1));
302367
assert_eq!(Balances::usable_balance(1), 10);
303368

304-
assert_ok!(Voting::unlock(RuntimeOrigin::signed(2), class(3), 2));
305-
assert_eq!(Balances::usable_balance(2), 20);
369+
assert_ok!(Voting::unlock(RuntimeOrigin::signed(6), class(3), 6));
370+
assert_eq!(Balances::usable_balance(6), 60);
306371
});
307372
}
308373

substrate/frame/conviction-voting/src/types.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,9 @@ impl<
117117
pub fn from_parts(
118118
ayes_with_conviction: Votes,
119119
nays_with_conviction: Votes,
120-
ayes: Votes,
120+
support: Votes,
121121
) -> Self {
122-
Self {
123-
ayes: ayes_with_conviction,
124-
nays: nays_with_conviction,
125-
support: ayes,
126-
dummy: PhantomData,
127-
}
122+
Self { ayes: ayes_with_conviction, nays: nays_with_conviction, support, dummy: PhantomData }
128123
}
129124

130125
/// Add an account's vote into the tally.

0 commit comments

Comments
 (0)