Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vote weights of ranked members in the Society pallet #2758

Merged
merged 8 commits into from
Jan 4, 2024

Conversation

laurogripa
Copy link
Contributor

@laurogripa laurogripa commented Dec 19, 2023

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.

@laurogripa laurogripa requested review from a team December 19, 2023 16:44
@cla-bot-2021
Copy link

cla-bot-2021 bot commented Dec 19, 2023

User @laurogripa, please sign the CLA here.

@paritytech-review-bot paritytech-review-bot bot requested a review from a team December 19, 2023 16:45
@bkchr
Copy link
Member

bkchr commented Dec 21, 2023

@laurogripa can you please write a prdoc?

@bkchr bkchr added the T1-FRAME This PR/Issue is related to core FRAME, the framework. label Dec 21, 2023
@bkchr
Copy link
Member

bkchr commented Dec 22, 2023

bot fmt

@command-bot
Copy link

command-bot bot commented Dec 22, 2023

@bkchr https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/4792269 was started for your command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh". Check out https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/pipelines?page=1&scope=all&username=group_605_bot to know what else is being executed currently.

Comment bot cancel 6-8cae3fd7-0ea1-4941-b21d-78984c25bfaf to cancel this command or bot cancel to cancel all commands in this pull request.

@command-bot
Copy link

command-bot bot commented Dec 22, 2023

@bkchr Command "$PIPELINE_SCRIPTS_DIR/commands/fmt/fmt.sh" has finished. Result: https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/4792269 has finished. If any artifacts were generated, you can download them from https://gitlab.parity.io/parity/mirrors/polkadot-sdk/-/jobs/4792269/artifacts/download.

@laurogripa
Copy link
Contributor Author

@laurogripa can you please write a prdoc?

Done. Let me know if it makes sense.

prdoc/pr_2758.prdoc Outdated Show resolved Hide resolved
@bkchr bkchr merged commit 924089f into paritytech:master Jan 4, 2024
119 of 120 checks passed
bgallois pushed a commit to duniter/duniter-polkadot-sdk that referenced this pull request Mar 25, 2024
…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 <>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T1-FRAME This PR/Issue is related to core FRAME, the framework.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants