-
Notifications
You must be signed in to change notification settings - Fork 2.7k
staking: avoid proportional slashing leak dust into chunks that should not be slashed #12058
staking: avoid proportional slashing leak dust into chunks that should not be slashed #12058
Conversation
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
@kianenigma PTAL |
The CI pipeline was cancelled due to failure one of the required jobs. |
frame/staking/src/lib.rs
Outdated
// zero otherwise. In both cases, we slash first `self.active`, and then | ||
// `slash_chunks_priority`. | ||
let (remaining_ratio, slash_chunks_priority) = { | ||
let mut affected_balance = BalanceOf::<T>::zero(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite agree with the changes here, like let mut first_chunk = None;
and assigning to it in the loop. This is actually not Rust-idiomatic at all, and the previous if let Some(_) = iterator {} else {}
is more preferred.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought using a single loop to get first_index
and affected_balance
was more efficient (less iteration) and cleaner (less if else
branch), but indeed hand code the functionality that the Iterator
API already provided is not Rust-idiomatic.
frame/staking/src/lib.rs
Outdated
@@ -611,21 +621,19 @@ impl<T: Config> StakingLedger<T> { | |||
slash_era, | |||
self, | |||
slash_chunks_priority, | |||
maybe_proportional, | |||
Perquintill::one() - remaining_ratio, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be safe math. Or, yet again, I want to complain that the refactor is more of step backwards, since Option
is the prefect type to show a maybe_it_is_proportional
, instead of some value being 0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The intention was to unify both cases (proportional or not) under a ratio, proportional or not just a matter of different ratio, but since it is important to get clear of these cases this indeed may not be a good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right direction and we can fix this, but please:
- The refactors are a bit opinionated, let's keep this PR limited only to fixing the rounding down (unless if you want to try a different approach at refactoring the code)
- As a simpler approach, please use the same logic as before, but use
Rounding::Up
when multiplying the Prequintill.
The combination of these two should make the diff of this PR very small, which makes it way easier to merge, since it is a very sensitive code. Thanks!
Thanks for the hint @kianenigma !
I found there may have two places need to change to substrate/frame/staking/src/lib.rs Line 595 in e7aa858
substrate/frame/staking/src/lib.rs Line 619 in e7aa858
the first place may have more impact to the result, it can changing to |
You should do what normal
|
Signed-off-by: linning <linningde25@gmail.com>
Signed-off-by: linning <linningde25@gmail.com>
The CI pipeline was cancelled due to failure one of the required jobs. |
The CI pipeline was cancelled due to failure one of the required jobs. |
Signed-off-by: linning <linningde25@gmail.com>
frame/staking/src/tests.rs
Outdated
@@ -5233,26 +5233,12 @@ fn proportional_ledger_slash_works() { | |||
ledger.active = unit; | |||
ledger.total = unit * 4 + value; | |||
// When | |||
assert_eq!(ledger.slash(slash, 0, 0), slash - 43); | |||
assert_eq!(ledger.slash(slash, 0, 2), slash); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add this test case and make sure it is reasonable as well: bdd1399
Ideally, existing tests should not be changed at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why you changed this test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry, I thought the original case was subject to the round down problem and changed it to meet the case not affected chunk should not be slashed
(like bdd1399), should not changing it and add a new case instead.
Signed-off-by: linning <linningde25@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, except the small question about tests.
Signed-off-by: linning <linningde25@gmail.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems good!
Signed-off-by: linning <linningde25@gmail.com> Co-authored-by: Roman Useinov <roman.useinov@gmail.com>
bot rebase |
Rebased |
bot merge |
…d not be slashed (paritytech#12058) * replace slash ratio with remaining ratio Signed-off-by: linning <linningde25@gmail.com> * little refactor Signed-off-by: linning <linningde25@gmail.com> * fix test Signed-off-by: linning <linningde25@gmail.com> * fix typo Signed-off-by: linning <linningde25@gmail.com> * revert refactor Signed-off-by: linning <linningde25@gmail.com> * rounding up instead of remaining ratio Signed-off-by: linning <linningde25@gmail.com> * address comment Signed-off-by: linning <linningde25@gmail.com> * Update frame/nomination-pools/test-staking/src/lib.rs Signed-off-by: linning <linningde25@gmail.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Update frame/nomination-pools/test-staking/src/lib.rs Signed-off-by: linning <linningde25@gmail.com> Co-authored-by: Roman Useinov <roman.useinov@gmail.com> Signed-off-by: linning <linningde25@gmail.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Roman Useinov <roman.useinov@gmail.com> Co-authored-by: parity-processbot <>
Attempt to fix #11810
This PR replaces the slash ratio with the remaining ratio, since calculations are rounded down, the
slash_from_target
calculated from the remaining ratio will be a bit more than the expected proportion instead of less, thus fixing the issue. This PR also comes with a little refactoring.polkadot address: 1CWyso9Zw46QdR54sTvKJXTUdmZznYZa2aJfNHdG6xQ6L71