This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
safe multi-era slashing for NPoS #3846
Merged
Merged
Changes from 26 commits
Commits
Show all changes
56 commits
Select commit
Hold shift + click to select a range
c6bdd50
define slashing spans
rphmeier fef2d4c
tests and pruning for slashing-spans record
rphmeier 59ea56a
validators get slashed before nominators
rphmeier c6c9da8
apply slash to nominators as well
rphmeier 8427efb
chill and end slashing spans
rphmeier 4288736
actually perform slashes
rphmeier 372a4ce
integration (tests failing)
rphmeier 06af8d0
prune metadata
rphmeier 2468334
fix compilation
rphmeier d0854ae
some tests for slashing and metadata garbage collection
rphmeier 47f3c54
correctly pass session index to slash handler
rphmeier 3145ac3
test span-max property for nominators and validators
rphmeier 13f3083
test that slashes are summed correctly
rphmeier 187b3f6
reward value computation
rphmeier 52ff078
implement rewarding
rphmeier e4d9864
add comment about rewards
rphmeier 459ebe7
do not adjust slash fraction in offences module
rphmeier 2d10e2c
fix offences tests
rphmeier ababe08
remove unused new_offenders field
rphmeier a682448
update runtime version
rphmeier d593569
fix up some docs
rphmeier 036fdbd
fix some CI failures
rphmeier 0d9e4da
Merge branch 'master' into rh-npos-slashing
rphmeier 0d3f17f
remove no-std incompatible vec! invocation
rphmeier 177e7e6
try to fix span-max rounding error
rphmeier ee189aa
Update srml/staking/src/slashing.rs
rphmeier e673db0
Merge branch 'master' into rh-npos-slashing
rphmeier f8e7fab
slashes from prior spans don't kick validator again
rphmeier 8a24768
more information for nominators, suppression
rphmeier 8aca6f4
ensure ledger is consistent with itself post-slash
rphmeier b165b27
implement slash out of unlocking funds also
rphmeier 76608ab
slashing: create records to be applied after-the-fact
rphmeier 9c30d7d
queue slashes for a few eras later
rphmeier d3ac5a2
method for canceling deferred slashes
rphmeier 8c245b3
Merge branch 'rh-npos-slashing' of github.com:paritytech/substrate in…
rphmeier a62d08b
Merge branch 'master' into rh-npos-slashing
rphmeier a5a8e99
attempt to fix test in CI
rphmeier 5bbdafe
Merge branch 'master' into rh-npos-slashing
rphmeier 5ba349e
storage migration for `Nominators`
rphmeier 36f3b34
update node-runtime to use SlashDeferDuration
rphmeier fd8c70f
Merge branch 'master' into rh-npos-slashing
rphmeier 11b71f3
adjust migration entry-points somewhat
rphmeier 74087ba
fix migration compilation
rphmeier b823286
add manual Vec import to migration
rphmeier 1866a50
enable migrations feature in node-runtime
rphmeier 7106425
bump runtime version
rphmeier c44ee91
Merge branch 'master' into rh-npos-slashing
rphmeier 7988052
Merge branch 'master' into rh-npos-slashing
rphmeier deda336
update to latest master crate renames
rphmeier aee412c
update to use ensure-origin
rphmeier d578e2e
Apply suggestions from code review
rphmeier 4e4c254
Merge branch 'master' into rh-npos-slashing
rphmeier a94d403
fix multi-slash removal
rphmeier 4fc80f7
initialize storage version to current in genesis
rphmeier b7599d7
add test for version initialization
rphmeier 8f9a743
Merge branch 'master' into rh-npos-slashing
rphmeier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,17 +24,11 @@ | |
| mod mock; | ||
| mod tests; | ||
|
|
||
| use rstd::{ | ||
| vec::Vec, | ||
| collections::btree_set::BTreeSet, | ||
| }; | ||
| use rstd::vec::Vec; | ||
| use support::{ | ||
| decl_module, decl_event, decl_storage, Parameter, | ||
| }; | ||
| use sr_primitives::{ | ||
| Perbill, | ||
| traits::{Hash, Saturating}, | ||
| }; | ||
| use sr_primitives::traits::Hash; | ||
| use sr_staking_primitives::{ | ||
| offence::{Offence, ReportOffence, Kind, OnOffenceHandler, OffenceDetails}, | ||
| }; | ||
|
|
@@ -100,10 +94,11 @@ where | |
|
|
||
| // Go through all offenders in the offence report and find all offenders that was spotted | ||
| // in unique reports. | ||
| let TriageOutcome { | ||
| new_offenders, | ||
| concurrent_offenders, | ||
| } = match Self::triage_offence_report::<O>(reporters, &time_slot, offenders) { | ||
| let TriageOutcome { concurrent_offenders } = match Self::triage_offence_report::<O>( | ||
| reporters, | ||
| &time_slot, | ||
| offenders, | ||
| ) { | ||
| Some(triage) => triage, | ||
| // The report contained only duplicates, so there is no need to slash again. | ||
| None => return, | ||
|
|
@@ -113,44 +108,18 @@ where | |
| Self::deposit_event(Event::Offence(O::ID, time_slot.encode())); | ||
|
|
||
| let offenders_count = concurrent_offenders.len() as u32; | ||
| let previous_offenders_count = offenders_count - new_offenders.len() as u32; | ||
|
|
||
| // The amount new offenders are slashed | ||
| let new_fraction = O::slash_fraction(offenders_count, validator_set_count); | ||
|
|
||
| // The amount previous offenders are slashed additionally. | ||
| // | ||
| // Since they were slashed in the past, we slash by: | ||
| // x = (new - prev) / (1 - prev) | ||
| // because: | ||
| // Y = X * (1 - prev) | ||
| // Z = Y * (1 - x) | ||
| // Z = X * (1 - new) | ||
| let old_fraction = if previous_offenders_count > 0 { | ||
| let previous_fraction = O::slash_fraction( | ||
| offenders_count.saturating_sub(previous_offenders_count), | ||
| validator_set_count, | ||
| ); | ||
| let numerator = new_fraction.saturating_sub(previous_fraction); | ||
| let denominator = Perbill::one().saturating_sub(previous_fraction); | ||
| denominator.saturating_mul(numerator) | ||
| } else { | ||
| new_fraction.clone() | ||
| }; | ||
| let slash_perbill: Vec<_> = (0..concurrent_offenders.len()) | ||
| .map(|_| new_fraction.clone()).collect(); | ||
|
|
||
| // calculate how much to slash | ||
| let slash_perbill = concurrent_offenders | ||
| .iter() | ||
| .map(|details| { | ||
| if previous_offenders_count > 0 && new_offenders.contains(&details.offender) { | ||
| new_fraction.clone() | ||
| } else { | ||
| old_fraction.clone() | ||
| } | ||
| }) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| T::OnOffenceHandler::on_offence(&concurrent_offenders, &slash_perbill); | ||
| T::OnOffenceHandler::on_offence( | ||
| &concurrent_offenders, | ||
| &slash_perbill, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: from the way that |
||
| offence.session_index(), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -173,13 +142,13 @@ impl<T: Trait> Module<T> { | |
| offenders: Vec<T::IdentificationTuple>, | ||
| ) -> Option<TriageOutcome<T>> { | ||
| let mut storage = ReportIndexStorage::<T, O>::load(time_slot); | ||
| let mut new_offenders = BTreeSet::new(); | ||
|
|
||
| let mut any_new = false; | ||
| for offender in offenders { | ||
| let report_id = Self::report_id::<O>(time_slot, &offender); | ||
|
|
||
| if !<Reports<T>>::exists(&report_id) { | ||
| new_offenders.insert(offender.clone()); | ||
| any_new = true; | ||
| <Reports<T>>::insert( | ||
| &report_id, | ||
| OffenceDetails { | ||
|
|
@@ -192,7 +161,7 @@ impl<T: Trait> Module<T> { | |
| } | ||
| } | ||
|
|
||
| if !new_offenders.is_empty() { | ||
| if any_new { | ||
| // Load report details for the all reports happened at the same time. | ||
| let concurrent_offenders = storage.concurrent_reports | ||
| .iter() | ||
|
|
@@ -202,7 +171,6 @@ impl<T: Trait> Module<T> { | |
| storage.save(); | ||
|
|
||
| Some(TriageOutcome { | ||
| new_offenders, | ||
| concurrent_offenders, | ||
| }) | ||
| } else { | ||
|
|
@@ -212,8 +180,6 @@ impl<T: Trait> Module<T> { | |
| } | ||
|
|
||
| struct TriageOutcome<T: Trait> { | ||
| /// Offenders that was spotted in the unique reports. | ||
| new_offenders: BTreeSet<T::IdentificationTuple>, | ||
| /// Other reports for the same report kinds. | ||
| concurrent_offenders: Vec<OffenceDetails<T::AccountId, T::IdentificationTuple>>, | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why is this logic gone? So now we slash the previous offenders again, and they will be slashed more in total, no?
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.
Just saw that in the
slashingmodule we implement a rolling window approach and only slash for maximum (not sum) value anyway.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 - accounting for the prior slash is done now in the slashing module