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

Commit

Permalink
Society: Ensure all votes are removed after tally (#4666)
Browse files Browse the repository at this point in the history
* Ensure all votes are removed after tally

* Fix comment
  • Loading branch information
shawntabrizi authored and gavofyork committed Jan 17, 2020
1 parent f0c1852 commit 8de5340
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pallet-transaction-payment = { version = "2.0.0", path = "../../../frame/transac
frame-support = { version = "2.0.0", default-features = false, path = "../../../frame/support" }
pallet-im-online = { version = "2.0.0", default-features = false, path = "../../../frame/im-online" }
pallet-authority-discovery = { version = "2.0.0", path = "../../../frame/authority-discovery" }
pallet-society = { version = "2.0.0", path = "../../../frame/society" }

# node-specific dependencies
node-runtime = { version = "2.0.0", path = "../runtime" }
Expand Down
8 changes: 7 additions & 1 deletion frame/society/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,9 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
}
}).collect::<Vec<_>>();

// Clean up all votes.
<Votes<T, I>>::remove_all();

// Reward one of the voters who voted the right way.
if !total_slash.is_zero() {
if let Some(winner) = pick_item(&mut rng, &rewardees) {
Expand Down Expand Up @@ -1472,7 +1475,7 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
let mut rejection_count = 0;
// Tallies total number of approve and reject votes for the defender.
members.iter()
.filter_map(|m| <DefenderVotes<T, I>>::get(m))
.filter_map(|m| <DefenderVotes<T, I>>::take(m))
.for_each(|v| {
match v {
Vote::Approve => approval_count += 1,
Expand All @@ -1485,6 +1488,9 @@ impl<T: Trait<I>, I: Instance> Module<T, I> {
Self::suspend_member(&defender);
*members = Self::members();
}

// Clean up all votes.
<DefenderVotes<T, I>>::remove_all();
}

// Start a new defender rotation
Expand Down
15 changes: 15 additions & 0 deletions frame/society/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,11 @@ fn challenges_work() {
assert_ok!(Society::add_member(&20));
assert_ok!(Society::add_member(&30));
assert_ok!(Society::add_member(&40));
// Votes are empty
assert_eq!(<DefenderVotes<Test>>::get(10), None);
assert_eq!(<DefenderVotes<Test>>::get(20), None);
assert_eq!(<DefenderVotes<Test>>::get(30), None);
assert_eq!(<DefenderVotes<Test>>::get(40), None);
// Check starting point
assert_eq!(Society::members(), vec![10, 20, 30, 40]);
assert_eq!(Society::defender(), None);
Expand All @@ -561,6 +566,11 @@ fn challenges_work() {
run_to_block(24);
// 20 survives
assert_eq!(Society::members(), vec![10, 20, 30, 40]);
// Votes are reset
assert_eq!(<DefenderVotes<Test>>::get(10), None);
assert_eq!(<DefenderVotes<Test>>::get(20), None);
assert_eq!(<DefenderVotes<Test>>::get(30), None);
assert_eq!(<DefenderVotes<Test>>::get(40), None);
// One more time
assert_eq!(Society::defender(), Some(20));
// 2 people say accept, 2 reject
Expand All @@ -574,6 +584,11 @@ fn challenges_work() {
assert_eq!(Society::suspended_member(20), true);
// New defender is chosen
assert_eq!(Society::defender(), Some(40));
// Votes are reset
assert_eq!(<DefenderVotes<Test>>::get(10), None);
assert_eq!(<DefenderVotes<Test>>::get(20), None);
assert_eq!(<DefenderVotes<Test>>::get(30), None);
assert_eq!(<DefenderVotes<Test>>::get(40), None);
});
}

Expand Down

0 comments on commit 8de5340

Please sign in to comment.