Skip to content

Commit

Permalink
Add and test events in pallet-conviction-voting (#6544)
Browse files Browse the repository at this point in the history
# Description

#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 #6261 was, so
it is a trivial change.
  • Loading branch information
rockbmb authored Nov 20, 2024
1 parent 07a5933 commit ca8beae
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 19 deletions.
14 changes: 14 additions & 0 deletions prdoc/pr_6544.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0
# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json

title: Add and test events to conviction voting pallet

doc:
- audience: Runtime Dev
description: |
Add event for the unlocking of an expired conviction vote's funds, and test recently added
voting events.

crates:
- name: pallet-conviction-voting
bump: major
7 changes: 5 additions & 2 deletions substrate/frame/conviction-voting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ pub mod pallet {
Delegated(T::AccountId, T::AccountId),
/// An \[account\] has cancelled a previous delegation operation.
Undelegated(T::AccountId),
/// An account that has voted
/// An account has voted
Voted { who: T::AccountId, vote: AccountVote<BalanceOf<T, I>> },
/// A vote that been removed
/// A vote has been removed
VoteRemoved { who: T::AccountId, vote: AccountVote<BalanceOf<T, I>> },
/// The lockup period of a conviction vote expired, and the funds have been unlocked.
VoteUnlocked { who: T::AccountId, class: ClassOf<T, I> },
}

#[pallet::error]
Expand Down Expand Up @@ -315,6 +317,7 @@ pub mod pallet {
ensure_signed(origin)?;
let target = T::Lookup::lookup(target)?;
Self::update_lock(&class, &target);
Self::deposit_event(Event::VoteUnlocked { who: target, class });
Ok(())
}

Expand Down
85 changes: 75 additions & 10 deletions substrate/frame/conviction-voting/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,27 +238,52 @@ fn basic_stuff() {
fn basic_voting_works() {
new_test_ext().execute_with(|| {
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, aye(2, 5)));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
who: 1,
vote: aye(2, 5),
}));
assert_eq!(tally(3), Tally::from_parts(10, 0, 2));
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, nay(2, 5)));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
who: 1,
vote: nay(2, 5),
}));
assert_eq!(tally(3), Tally::from_parts(0, 10, 0));
assert_eq!(Balances::usable_balance(1), 8);

assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, aye(5, 1)));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
who: 1,
vote: aye(5, 1),
}));
assert_eq!(tally(3), Tally::from_parts(5, 0, 5));
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, nay(5, 1)));
assert_eq!(tally(3), Tally::from_parts(0, 5, 0));
assert_eq!(Balances::usable_balance(1), 5);

assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, aye(10, 0)));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
who: 1,
vote: aye(10, 0),
}));
assert_eq!(tally(3), Tally::from_parts(1, 0, 10));

assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, nay(10, 0)));
assert_eq!(tally(3), Tally::from_parts(0, 1, 0));
assert_eq!(Balances::usable_balance(1), 0);

assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), None, 3));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteRemoved {
who: 1,
vote: nay(10, 0),
}));
assert_eq!(tally(3), Tally::from_parts(0, 0, 0));

assert_ok!(Voting::unlock(RuntimeOrigin::signed(1), class(3), 1));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteUnlocked {
who: 1,
class: class(3),
}));
assert_eq!(Balances::usable_balance(1), 10);
});
}
Expand All @@ -267,15 +292,32 @@ fn basic_voting_works() {
fn split_voting_works() {
new_test_ext().execute_with(|| {
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, split(10, 0)));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
who: 1,
vote: split(10, 0),
}));
assert_eq!(tally(3), Tally::from_parts(1, 0, 10));

assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, split(5, 5)));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
who: 1,
vote: split(5, 5),
}));
assert_eq!(tally(3), Tally::from_parts(0, 0, 5));
assert_eq!(Balances::usable_balance(1), 0);

assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), None, 3));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteRemoved {
who: 1,
vote: split(5, 5),
}));
assert_eq!(tally(3), Tally::from_parts(0, 0, 0));

assert_ok!(Voting::unlock(RuntimeOrigin::signed(1), class(3), 1));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteUnlocked {
who: 1,
class: class(3),
}));
assert_eq!(Balances::usable_balance(1), 10);
});
}
Expand All @@ -284,25 +326,48 @@ fn split_voting_works() {
fn abstain_voting_works() {
new_test_ext().execute_with(|| {
assert_ok!(Voting::vote(RuntimeOrigin::signed(1), 3, split_abstain(0, 0, 10)));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
who: 1,
vote: split_abstain(0, 0, 10),
}));
assert_eq!(tally(3), Tally::from_parts(0, 0, 10));
assert_ok!(Voting::vote(RuntimeOrigin::signed(2), 3, split_abstain(0, 0, 20)));
assert_eq!(tally(3), Tally::from_parts(0, 0, 30));
assert_ok!(Voting::vote(RuntimeOrigin::signed(2), 3, split_abstain(10, 0, 10)));
assert_eq!(tally(3), Tally::from_parts(1, 0, 30));

assert_ok!(Voting::vote(RuntimeOrigin::signed(6), 3, split_abstain(10, 0, 20)));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
who: 6,
vote: split_abstain(10, 0, 20),
}));
assert_eq!(tally(3), Tally::from_parts(1, 0, 40));

assert_ok!(Voting::vote(RuntimeOrigin::signed(6), 3, split_abstain(0, 0, 40)));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::Voted {
who: 6,
vote: split_abstain(0, 0, 40),
}));

assert_eq!(tally(3), Tally::from_parts(0, 0, 50));
assert_eq!(Balances::usable_balance(1), 0);
assert_eq!(Balances::usable_balance(2), 0);
assert_eq!(Balances::usable_balance(6), 20);

assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(1), None, 3));
assert_eq!(tally(3), Tally::from_parts(1, 0, 20));

assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(2), None, 3));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteRemoved {
who: 1,
vote: split_abstain(0, 0, 10),
}));
assert_eq!(tally(3), Tally::from_parts(0, 0, 40));

assert_ok!(Voting::remove_vote(RuntimeOrigin::signed(6), Some(class(3)), 3));
System::assert_last_event(tests::RuntimeEvent::Voting(Event::VoteRemoved {
who: 6,
vote: split_abstain(0, 0, 40),
}));
assert_eq!(tally(3), Tally::from_parts(0, 0, 0));

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

assert_ok!(Voting::unlock(RuntimeOrigin::signed(2), class(3), 2));
assert_eq!(Balances::usable_balance(2), 20);
assert_ok!(Voting::unlock(RuntimeOrigin::signed(6), class(3), 6));
assert_eq!(Balances::usable_balance(6), 60);
});
}

Expand Down
9 changes: 2 additions & 7 deletions substrate/frame/conviction-voting/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,9 @@ impl<
pub fn from_parts(
ayes_with_conviction: Votes,
nays_with_conviction: Votes,
ayes: Votes,
support: Votes,
) -> Self {
Self {
ayes: ayes_with_conviction,
nays: nays_with_conviction,
support: ayes,
dummy: PhantomData,
}
Self { ayes: ayes_with_conviction, nays: nays_with_conviction, support, dummy: PhantomData }
}

/// Add an account's vote into the tally.
Expand Down

0 comments on commit ca8beae

Please sign in to comment.