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.7k
New slashing logic #570
Merged
Merged
New slashing logic #570
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b06b022
New slashing mechanism (#554)
gavofyork 8d85ecb
Remove accidental merge files.
gavofyork f46c603
Merge remote-tracking branch 'origin/master' into gav-new-pos
gavofyork 5d12d61
Version bump, fixes (#572)
gavofyork fd04ad7
Fix merge
gavofyork 15315de
Fix accidental merge bug
gavofyork bd579f1
Fixes.
gavofyork 42c5856
Staking failsafes
gavofyork dc6de6c
Make minimum validator count dynamic.
gavofyork b85cb38
test fixes
gavofyork 7192001
Fix tests.
gavofyork 5a19b7b
Fix tests
gavofyork bc9f55b
Fix tests, update readme.
gavofyork b6069c7
Merge remote-tracking branch 'origin/master' into gav-new-pos
gavofyork 97ed7b9
Test with release.
gavofyork 03631c6
Use safe math when dealing with total stake
gavofyork 94bebf6
Fix test again.
gavofyork 5d8bc21
Fix grumbles.
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
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
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
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 |
|---|---|---|
|
|
@@ -54,21 +54,21 @@ use runtime_support::dispatch::Result; | |
| use std::collections::HashMap; | ||
|
|
||
| /// A session has changed. | ||
| pub trait OnSessionChange<T, A> { | ||
| pub trait OnSessionChange<T> { | ||
| /// Session has changed. | ||
| fn on_session_change(time_elapsed: T, bad_validators: Vec<A>); | ||
| fn on_session_change(time_elapsed: T, should_reward: bool); | ||
| } | ||
|
|
||
| impl<T, A> OnSessionChange<T, A> for () { | ||
| fn on_session_change(_: T, _: Vec<A>) {} | ||
| impl<T> OnSessionChange<T> for () { | ||
| fn on_session_change(_: T, _: bool) {} | ||
| } | ||
|
|
||
| pub trait Trait: timestamp::Trait { | ||
| // the position of the required timestamp-set extrinsic. | ||
| const NOTE_OFFLINE_POSITION: u32; | ||
| const NOTE_MISSED_PROPOSAL_POSITION: u32; | ||
|
|
||
| type ConvertAccountIdToSessionKey: Convert<Self::AccountId, Self::SessionKey>; | ||
| type OnSessionChange: OnSessionChange<Self::Moment, Self::AccountId>; | ||
| type OnSessionChange: OnSessionChange<Self::Moment>; | ||
| } | ||
|
|
||
| decl_module! { | ||
|
|
@@ -83,7 +83,7 @@ decl_module! { | |
| #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] | ||
| pub enum PrivCall { | ||
| fn set_length(new: T::BlockNumber) -> Result = 0; | ||
| fn force_new_session(normal_rotation: bool) -> Result = 1; | ||
| fn force_new_session(apply_rewards: bool) -> Result = 1; | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -142,18 +142,18 @@ impl<T: Trait> Module<T> { | |
| } | ||
|
|
||
| /// Forces a new session. | ||
| pub fn force_new_session(normal_rotation: bool) -> Result { | ||
| <ForcingNewSession<T>>::put(normal_rotation); | ||
| pub fn force_new_session(apply_rewards: bool) -> Result { | ||
| <ForcingNewSession<T>>::put(apply_rewards); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Notes which of the validators appear to be online from the point of the view of the block author. | ||
| pub fn note_offline(aux: &T::PublicAux, offline_val_indices: Vec<u32>) -> Result { | ||
| assert!(aux.is_empty()); | ||
| assert!( | ||
| <system::Module<T>>::extrinsic_index() == T::NOTE_OFFLINE_POSITION, | ||
| <system::Module<T>>::extrinsic_index() == T::NOTE_MISSED_PROPOSAL_POSITION, | ||
| "note_offline extrinsic must be at position {} in the block", | ||
| T::NOTE_OFFLINE_POSITION | ||
| T::NOTE_MISSED_PROPOSAL_POSITION | ||
|
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. Could you update error message? |
||
| ); | ||
|
|
||
| let vs = Self::validators(); | ||
|
|
@@ -181,15 +181,15 @@ impl<T: Trait> Module<T> { | |
| // check block number and call next_session if necessary. | ||
| let block_number = <system::Module<T>>::block_number(); | ||
| let is_final_block = ((block_number - Self::last_length_change()) % Self::length()).is_zero(); | ||
| let bad_validators = <BadValidators<T>>::take().unwrap_or_default(); | ||
| let should_end_session = <ForcingNewSession<T>>::take().is_some() || !bad_validators.is_empty() || is_final_block; | ||
| let (should_end_session, apply_rewards) = <ForcingNewSession<T>>::take() | ||
| .map_or((is_final_block, is_final_block), |apply_rewards| (true, apply_rewards)); | ||
| if should_end_session { | ||
| Self::rotate_session(is_final_block, bad_validators); | ||
| Self::rotate_session(is_final_block, apply_rewards); | ||
| } | ||
| } | ||
|
|
||
| /// Move onto next session: register the new authority set. | ||
| pub fn rotate_session(is_final_block: bool, bad_validators: Vec<T::AccountId>) { | ||
| pub fn rotate_session(is_final_block: bool, apply_rewards: bool) { | ||
| let now = <timestamp::Module<T>>::get(); | ||
| let time_elapsed = now.clone() - Self::current_start(); | ||
|
|
||
|
|
@@ -209,7 +209,7 @@ impl<T: Trait> Module<T> { | |
| <LastLengthChange<T>>::put(block_number); | ||
| } | ||
|
|
||
| T::OnSessionChange::on_session_change(time_elapsed, bad_validators); | ||
| T::OnSessionChange::on_session_change(time_elapsed, apply_rewards); | ||
|
|
||
| // Update any changes in session keys. | ||
| Self::validators().iter().enumerate().for_each(|(i, v)| { | ||
|
|
@@ -314,7 +314,7 @@ mod tests { | |
| type Moment = u64; | ||
| } | ||
| impl Trait for Test { | ||
| const NOTE_OFFLINE_POSITION: u32 = 1; | ||
| const NOTE_MISSED_PROPOSAL_POSITION: u32 = 1; | ||
| type ConvertAccountIdToSessionKey = Identity; | ||
| type OnSessionChange = (); | ||
| } | ||
|
|
@@ -350,34 +350,6 @@ mod tests { | |
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_rotate_on_bad_validators() { | ||
| with_externalities(&mut new_test_ext(), || { | ||
| System::set_block_number(2); | ||
| assert_eq!(Session::blocks_remaining(), 0); | ||
| Timestamp::set_timestamp(0); | ||
| assert_ok!(Session::set_length(3)); | ||
| Session::check_rotate_session(); | ||
| assert_eq!(Session::current_index(), 1); | ||
| assert_eq!(Session::length(), 3); | ||
| assert_eq!(Session::current_start(), 0); | ||
| assert_eq!(Session::ideal_session_duration(), 15); | ||
| // ideal end = 0 + 15 * 3 = 15 | ||
|
|
||
| System::set_block_number(3); | ||
| assert_eq!(Session::blocks_remaining(), 2); | ||
| Timestamp::set_timestamp(9); // no bad validators. session not rotated. | ||
| Session::check_rotate_session(); | ||
|
|
||
| System::set_block_number(4); | ||
| ::system::ExtrinsicIndex::<Test>::put(1); | ||
| assert_eq!(Session::blocks_remaining(), 1); | ||
| Session::note_offline(&0, vec![1]).unwrap(); // bad validator -> session rotate | ||
| Session::check_rotate_session(); | ||
| assert_eq!(Session::current_index(), 2); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn should_work_with_early_exit() { | ||
| with_externalities(&mut new_test_ext(), || { | ||
|
|
||
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.
Could you update a comment above?