Force new era only if 1/3 validators is disabled. (#3533)

* Force new era only when over third validators is disabled.

* Update srml/session/src/lib.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Update srml/staking/src/lib.rs

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Parametrize the threshold.

* Bump runtime version.

* Update node/runtime/src/lib.rs

* Fix build.

* Fix im-online test.
This commit is contained in:
Tomasz Drwięga
2019-09-19 12:04:48 +02:00
committed by Gavin Wood
parent bfe240d1b0
commit 668acca9ed
7 changed files with 95 additions and 12 deletions
+11 -6
View File
@@ -454,7 +454,11 @@ type MomentOf<T>= <<T as Trait>::Time as Time>::Moment;
/// This is needed because `Staking` sets the `ValidatorIdOf` of the `session::Trait`
pub trait SessionInterface<AccountId>: system::Trait {
/// Disable a given validator by stash ID.
fn disable_validator(validator: &AccountId) -> Result<(), ()>;
///
/// Returns `true` if new era should be forced at the end of this session.
/// This allows preventing a situation where there is too many validators
/// disabled and block production stalls.
fn disable_validator(validator: &AccountId) -> Result<bool, ()>;
/// Get the validators from session.
fn validators() -> Vec<AccountId>;
/// Prune historical session tries up to but not including the given index.
@@ -472,7 +476,7 @@ impl<T: Trait> SessionInterface<<T as system::Trait>::AccountId> for T where
T::SelectInitialValidators: session::SelectInitialValidators<<T as system::Trait>::AccountId>,
T::ValidatorIdOf: Convert<<T as system::Trait>::AccountId, Option<<T as system::Trait>::AccountId>>
{
fn disable_validator(validator: &<T as system::Trait>::AccountId) -> Result<(), ()> {
fn disable_validator(validator: &<T as system::Trait>::AccountId) -> Result<bool, ()> {
<session::Module<T>>::disable(validator)
}
@@ -1531,10 +1535,11 @@ impl <T: Trait> OnOffenceHandler<T::AccountId, session::historical::Identificati
continue;
}
// make sure to disable validator in next sessions
let _ = T::SessionInterface::disable_validator(stash);
// force a new era, to select a new validator set
ForceEra::put(Forcing::ForceNew);
// make sure to disable validator till the end of this session
if T::SessionInterface::disable_validator(stash).unwrap_or(false) {
// force a new era, to select a new validator set
ForceEra::put(Forcing::ForceNew);
}
// actually slash the validator
let slashed_amount = Self::slash_validator(stash, amount, exposure, &mut journal);
+2
View File
@@ -150,6 +150,7 @@ parameter_types! {
pub const Period: BlockNumber = 1;
pub const Offset: BlockNumber = 0;
pub const UncleGenerations: u64 = 0;
pub const DisabledValidatorsThreshold: Perbill = Perbill::from_percent(33);
}
impl session::Trait for Test {
type OnSessionEnding = session::historical::NoteHistoricalRoot<Test, Staking>;
@@ -160,6 +161,7 @@ impl session::Trait for Test {
type ValidatorId = AccountId;
type ValidatorIdOf = crate::StashOf<Test>;
type SelectInitialValidators = Staking;
type DisabledValidatorsThreshold = DisabledValidatorsThreshold;
}
impl session::historical::Trait for Test {