This repository has been 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
New slashing mechanism #554
Merged
Merged
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
417c6c5
Slashing improvements
gavofyork 611e1ca
Fix warning
gavofyork 2072502
Cleanups and ensure slash_count decays
gavofyork 07a4128
Bump authoring version and introduce needed authoring stub
gavofyork e59650c
Rename
gavofyork 09f0cda
Fix offline tracker
gavofyork 7df0fbe
Fix offline tracker
gavofyork eb9f94b
Renames
gavofyork 0cbbf67
Add test
gavofyork 3592d44
Tests
gavofyork 60dd126
Tests.
gavofyork 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 |
---|---|---|
|
@@ -21,14 +21,18 @@ use polkadot_primitives::AccountId; | |
use std::collections::HashMap; | ||
use std::time::{Instant, Duration}; | ||
|
||
// time before we report a validator. | ||
const REPORT_TIME: Duration = Duration::from_secs(60 * 5); | ||
|
||
struct Observed { | ||
last_round_end: Instant, | ||
offline_since: Instant, | ||
} | ||
|
||
#[derive(Eq, PartialEq)] | ||
enum Activity { | ||
Offline, | ||
StillOffline(Duration), | ||
Online, | ||
} | ||
|
||
impl Observed { | ||
fn new() -> Observed { | ||
let now = Instant::now(); | ||
|
@@ -38,31 +42,32 @@ impl Observed { | |
} | ||
} | ||
|
||
fn note_round_end(&mut self, was_online: bool) { | ||
let now = Instant::now(); | ||
|
||
fn note_round_end(&mut self, now: Instant, was_online: Option<bool>) { | ||
self.last_round_end = now; | ||
if was_online { | ||
if let Some(false) = was_online { | ||
self.offline_since = now; | ||
} | ||
} | ||
|
||
fn is_active(&self) -> bool { | ||
/// Returns what we have observed about the online/offline state of the validator. | ||
fn activity(&self) -> Activity { | ||
// can happen if clocks are not monotonic | ||
if self.offline_since > self.last_round_end { return true } | ||
self.last_round_end.duration_since(self.offline_since) < REPORT_TIME | ||
if self.offline_since > self.last_round_end { return Activity::Online } | ||
if self.offline_since == self.last_round_end { return Activity::Offline } | ||
Activity::StillOffline(self.last_round_end.duration_since(self.offline_since)) | ||
} | ||
} | ||
|
||
/// Tracks offline validators and can issue a report for those offline. | ||
pub struct OfflineTracker { | ||
observed: HashMap<AccountId, Observed>, | ||
block_instant: Instant, | ||
} | ||
|
||
impl OfflineTracker { | ||
/// Create a new tracker. | ||
pub fn new() -> Self { | ||
OfflineTracker { observed: HashMap::new() } | ||
OfflineTracker { observed: HashMap::new(), block_instant: Instant::now() } | ||
} | ||
|
||
/// Note new consensus is starting with the given set of validators. | ||
|
@@ -71,23 +76,33 @@ impl OfflineTracker { | |
|
||
let set: HashSet<_> = validators.iter().cloned().collect(); | ||
self.observed.retain(|k, _| set.contains(k)); | ||
|
||
self.block_instant = Instant::now(); | ||
} | ||
|
||
/// Note that a round has ended. | ||
pub fn note_round_end(&mut self, validator: AccountId, was_online: bool) { | ||
self.observed.entry(validator) | ||
.or_insert_with(Observed::new) | ||
.note_round_end(was_online); | ||
self.observed.entry(validator).or_insert_with(Observed::new); | ||
for (val, obs) in self.observed.iter_mut() { | ||
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. all validators are updated with the current time so that we can see which of them were inactive in specifially the previous block. |
||
obs.note_round_end( | ||
self.block_instant, | ||
if val == &validator { | ||
Some(was_online) | ||
} else { | ||
None | ||
} | ||
) | ||
} | ||
} | ||
|
||
/// Generate a vector of indices for offline account IDs. | ||
pub fn reports(&self, validators: &[AccountId]) -> Vec<u32> { | ||
validators.iter() | ||
.enumerate() | ||
.filter_map(|(i, v)| if self.is_online(v) { | ||
None | ||
} else { | ||
.filter_map(|(i, v)| if self.is_known_offline_now(v) { | ||
Some(i as u32) | ||
} else { | ||
None | ||
}) | ||
.collect() | ||
} | ||
|
@@ -101,13 +116,15 @@ impl OfflineTracker { | |
}; | ||
|
||
// we must think all validators reported externally are offline. | ||
let thinks_online = self.is_online(v); | ||
!thinks_online | ||
self.is_known_offline_now(v) | ||
}) | ||
} | ||
|
||
fn is_online(&self, v: &AccountId) -> bool { | ||
self.observed.get(v).map(Observed::is_active).unwrap_or(true) | ||
/// Rwturns true only if we have seen the validator miss the last round. For further | ||
/// rounds where we can't say for sure that they're still offline, we give them the | ||
/// benefit of the doubt. | ||
fn is_known_offline_now(&self, v: &AccountId) -> bool { | ||
self.observed.get(v).map(|o| o.activity() == Activity::Offline).unwrap_or(false) | ||
} | ||
} | ||
|
||
|
@@ -121,17 +138,30 @@ mod tests { | |
let v = [0; 32].into(); | ||
let v2 = [1; 32].into(); | ||
let v3 = [2; 32].into(); | ||
tracker.note_new_block(&[v, v2, v3]); | ||
tracker.note_round_end(v, true); | ||
tracker.note_round_end(v2, true); | ||
tracker.note_round_end(v3, true); | ||
assert_eq!(tracker.reports(&[v, v2, v3]), vec![0u32; 0]); | ||
|
||
tracker.note_new_block(&[v, v2, v3]); | ||
tracker.note_round_end(v, true); | ||
tracker.note_round_end(v2, false); | ||
tracker.note_round_end(v3, true); | ||
assert_eq!(tracker.reports(&[v, v2, v3]), vec![1]); | ||
|
||
let slash_time = REPORT_TIME + Duration::from_secs(5); | ||
tracker.observed.get_mut(&v).unwrap().offline_since -= slash_time; | ||
tracker.observed.get_mut(&v2).unwrap().offline_since -= slash_time; | ||
tracker.note_new_block(&[v, v2, v3]); | ||
tracker.note_round_end(v, false); | ||
assert_eq!(tracker.reports(&[v, v2, v3]), vec![0]); | ||
|
||
assert_eq!(tracker.reports(&[v, v2, v3]), vec![0, 1]); | ||
tracker.note_new_block(&[v, v2, v3]); | ||
tracker.note_round_end(v, false); | ||
tracker.note_round_end(v2, true); | ||
tracker.note_round_end(v3, false); | ||
assert_eq!(tracker.reports(&[v, v2, v3]), vec![0, 2]); | ||
|
||
tracker.note_new_block(&[v, v3]); | ||
tracker.note_new_block(&[v, v2]); | ||
tracker.note_round_end(v, false); | ||
assert_eq!(tracker.reports(&[v, v2, v3]), vec![0]); | ||
} | ||
} |
This file contains 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 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 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 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 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.
we provide
now
in order to ensure that all rounds in the same block are considered contemporaneous, which lets us group which validators were offline for the previous block specifically.