Skip to content

Commit

Permalink
Fix vote weights of ranked members in the Society pallet (paritytech#…
Browse files Browse the repository at this point in the history
…2758)

This PR fixes a bug in the tally accrual of approvals/rejections for
Candidates and Defender. The issue happened because:
- The `match maybe_old` is reducing `weight` from the tally:
```
Some(Vote { approve: true, weight }) => tally.approvals.saturating_reduce(weight),
Some(Vote { approve: false, weight }) => tally.rejections.saturating_reduce(weight),
```
- But `match approval` is accruing only `1` to the tally:
```
true => tally.approvals.saturating_accrue(1),
false => tally.rejections.saturating_accrue(1),
```

This way, if a member is rank 1 his vote is going to have weight 1 when
accruing but weight 4 when reducing from the tally. For example, let's
say:
- There's a Candidate with 0 approvals and 12 rejections;
- A ranked Member votes against the Candidate;
- The tally changes to 0 approvals and 13 rejections (should be 16);
- The Member changes his vote to an approval;
- Now tally changes to 1 approvals and 9 rejections, removing the
accrued approvals from other Members;
- If the Member keeps changing his vote, it wipes the tally clean.

So this PR changes `match approval` to accrue `weight` instead of just
`1` and changes the tests:
- Fixes `challenges_work`. This test started failing after the fix. The
reason is that the test assumes that all Members have equal weights to
their votes, but Member 10 is ranked, so his vote should have weight 4
against the Defender. So instead of using Member 10, I added Member 50
of rank 0 to keep the same logic;
- Improves `votes_are_working`. Added some assertions to check if the
tally is correct even after a ranked Member changes his vote a couple
times;
- Fixes `waive_repay_works`. Unrelated to the bug, but this test was
yielding a false positive. The test is ranking up Member 20, but
asserting the rank of Member 10, which is already ranked up.

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: command-bot <>
  • Loading branch information
laurogripa and bkchr authored Jan 4, 2024
1 parent 88fa08d commit 982a85b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
4 changes: 2 additions & 2 deletions substrate/frame/society/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,8 +1433,8 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
let weight_root = rank + 1;
let weight = weight_root * weight_root;
match approve {
true => tally.approvals.saturating_accrue(1),
false => tally.rejections.saturating_accrue(1),
true => tally.approvals.saturating_accrue(weight),
false => tally.rejections.saturating_accrue(weight),
}
Vote { approve, weight }
}
Expand Down
83 changes: 56 additions & 27 deletions substrate/frame/society/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ fn unvouch_works() {
// But their pick doesn't resign (yet).
conclude_intake(false, None);
// Voting still happening and voucher cannot unvouch.
assert_eq!(candidacies(), vec![(20, candidacy(1, 100, Vouch(10, 0), 0, 1))]);
assert_eq!(candidacies(), vec![(20, candidacy(1, 100, Vouch(10, 0), 0, 4))]);
assert_eq!(Members::<Test>::get(10).unwrap().vouching, Some(VouchingStatus::Vouching));

// Candidate gives in and resigns.
Expand Down Expand Up @@ -836,72 +836,72 @@ fn founder_and_head_cannot_be_removed() {
fn challenges_work() {
EnvBuilder::new().execute(|| {
// Add some members
place_members([20, 30, 40]);
place_members([20, 30, 40, 50]);
// Votes are empty
assert_eq!(DefenderVotes::<Test>::get(0, 10), None);
assert_eq!(DefenderVotes::<Test>::get(0, 20), None);
assert_eq!(DefenderVotes::<Test>::get(0, 30), None);
assert_eq!(DefenderVotes::<Test>::get(0, 40), None);
assert_eq!(DefenderVotes::<Test>::get(0, 50), None);
// Check starting point
assert_eq!(members(), vec![10, 20, 30, 40]);
assert_eq!(members(), vec![10, 20, 30, 40, 50]);
assert_eq!(Defending::<Test>::get(), None);

// 30 will be challenged during the challenge rotation
// 40 will be challenged during the challenge rotation
next_challenge();
assert_eq!(Defending::<Test>::get().unwrap().0, 30);
assert_eq!(Defending::<Test>::get().unwrap().0, 40);
// They can always free vote for themselves
assert_ok!(Society::defender_vote(Origin::signed(30), true));
assert_ok!(Society::defender_vote(Origin::signed(40), true));

// If no one else votes, nothing happens
next_challenge();
assert_eq!(members(), vec![10, 20, 30, 40]);
assert_eq!(members(), vec![10, 20, 30, 40, 50]);
// Reset votes for last challenge
assert_ok!(Society::cleanup_challenge(Origin::signed(0), 0, 10));
// New challenge period
assert_eq!(Defending::<Test>::get().unwrap().0, 30);
// New challenge period, 20 is challenged
assert_eq!(Defending::<Test>::get().unwrap().0, 20);
// Non-member cannot vote
assert_noop!(Society::defender_vote(Origin::signed(1), true), Error::<Test>::NotMember);
// 3 people say accept, 1 reject
assert_ok!(Society::defender_vote(Origin::signed(10), true));
assert_ok!(Society::defender_vote(Origin::signed(20), true));
assert_ok!(Society::defender_vote(Origin::signed(30), true));
assert_ok!(Society::defender_vote(Origin::signed(40), false));
assert_ok!(Society::defender_vote(Origin::signed(40), true));
assert_ok!(Society::defender_vote(Origin::signed(50), false));

next_challenge();
// 30 survives
assert_eq!(members(), vec![10, 20, 30, 40]);
// 20 survives
assert_eq!(members(), vec![10, 20, 30, 40, 50]);
// Reset votes for last challenge
assert_ok!(Society::cleanup_challenge(Origin::signed(0), 1, 10));
// Votes are reset
assert_eq!(DefenderVotes::<Test>::get(0, 10), None);
assert_eq!(DefenderVotes::<Test>::get(0, 20), None);
assert_eq!(DefenderVotes::<Test>::get(0, 30), None);
assert_eq!(DefenderVotes::<Test>::get(0, 40), None);
assert_eq!(DefenderVotes::<Test>::get(0, 50), None);

// One more time
assert_eq!(Defending::<Test>::get().unwrap().0, 30);
// One more time, 40 is challenged
assert_eq!(Defending::<Test>::get().unwrap().0, 40);
// 2 people say accept, 2 reject
assert_ok!(Society::defender_vote(Origin::signed(10), true));
assert_ok!(Society::defender_vote(Origin::signed(20), true));
assert_ok!(Society::defender_vote(Origin::signed(30), false));
assert_ok!(Society::defender_vote(Origin::signed(30), true));
assert_ok!(Society::defender_vote(Origin::signed(40), false));
assert_ok!(Society::defender_vote(Origin::signed(50), false));

next_challenge();
// 30 is suspended
assert_eq!(members(), vec![10, 20, 40]);
// 40 is suspended
assert_eq!(members(), vec![10, 20, 30, 50]);
assert_eq!(
SuspendedMembers::<Test>::get(30),
Some(MemberRecord { rank: 0, strikes: 0, vouching: None, index: 2 })
SuspendedMembers::<Test>::get(40),
Some(MemberRecord { rank: 0, strikes: 0, vouching: None, index: 3 })
);
// Reset votes for last challenge
assert_ok!(Society::cleanup_challenge(Origin::signed(0), 2, 10));
// New defender is chosen
assert_eq!(Defending::<Test>::get().unwrap().0, 20);
// New defender is chosen, 30 is challenged
assert_eq!(Defending::<Test>::get().unwrap().0, 30);
// Votes are reset
assert_eq!(DefenderVotes::<Test>::get(0, 10), None);
assert_eq!(DefenderVotes::<Test>::get(0, 20), None);
assert_eq!(DefenderVotes::<Test>::get(0, 30), None);
assert_eq!(DefenderVotes::<Test>::get(0, 40), None);
assert_eq!(DefenderVotes::<Test>::get(0, 50), None);
});
}

Expand Down Expand Up @@ -1044,6 +1044,9 @@ fn vouching_handles_removed_member_with_candidate() {
fn votes_are_working() {
EnvBuilder::new().execute(|| {
place_members([20]);
// Member 10 is rank 1 and Member 20 is rank 0
assert_eq!(Members::<Test>::get(10).unwrap().rank, 1);
assert_eq!(Members::<Test>::get(20).unwrap().rank, 0);
// Users make bids of various amounts
assert_ok!(Society::bid(RuntimeOrigin::signed(50), 500));
assert_ok!(Society::bid(RuntimeOrigin::signed(40), 400));
Expand All @@ -1061,6 +1064,32 @@ fn votes_are_working() {
assert_eq!(Votes::<Test>::get(30, 20), Some(Vote { approve: true, weight: 1 }));
assert_eq!(Votes::<Test>::get(40, 10), Some(Vote { approve: true, weight: 4 }));
assert_eq!(Votes::<Test>::get(50, 10), None);

// Votes have correct weights on the tally
assert_eq!(
Candidates::<Test>::get(30).unwrap().tally,
Tally { approvals: 5, rejections: 0 }
);
assert_eq!(
Candidates::<Test>::get(40).unwrap().tally,
Tally { approvals: 4, rejections: 0 }
);
// Member 10 changes his vote for Candidate 30
assert_ok!(Society::vote(Origin::signed(10), 30, false));
// Assert the tally calculation is correct
assert_eq!(
Candidates::<Test>::get(30).unwrap().tally,
Tally { approvals: 1, rejections: 4 }
);
// Member 10 changes his vote again
assert_ok!(Society::vote(Origin::signed(10), 30, true));
// Assert the tally is still correct
assert_eq!(
Candidates::<Test>::get(30).unwrap().tally,
Tally { approvals: 5, rejections: 0 }
);

// Finish intake
conclude_intake(false, None);
// Cleanup the candidacy
assert_ok!(Society::cleanup_candidacy(Origin::signed(0), 30, 10));
Expand Down Expand Up @@ -1240,7 +1269,7 @@ fn waive_repay_works() {
Payouts::<Test>::get(20),
PayoutRecord { paid: 0, payouts: vec![].try_into().unwrap() }
);
assert_eq!(Members::<Test>::get(10).unwrap().rank, 1);
assert_eq!(Members::<Test>::get(20).unwrap().rank, 1);
assert_eq!(Balances::free_balance(20), 50);
});
}
Expand Down

0 comments on commit 982a85b

Please sign in to comment.