mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-15 03:15:44 +00:00
6cc4495700
* Remove offline slashing logic from staking. * Initial version of reworked offence module, can report offences * Clean up staking example. * Commit SlashingOffence * Force new era on slash. * Add offenders in the SlashingOffence trait. * Introduce the ReportOffence trait. * Rename `Offence`. * Add on_before_session_ending handler. * Move offence related stuff under sr-primitives. * Fix cargo check. * Import new im-online implementation. * Adding validator count to historical session storage as it's needed for slash calculations * Add a comment about offence. * Add BabeEquivocationOffence * GrandpaEquivocationOffence * slash_fraction and fix * current_era_start_session_index * UnresponsivnessOffence * Finalise OnOffenceHandler traits, and stub impl for staking. * slash_fraction doesn't really need &self * Note that offenders count is greater than 0 * Add a test to ensure that I got the math right * Use FullIdentification in offences. * Use FullIndentification. * Hook up the offences module. * Report unresponsive validators * Make sure eras have the same length. * Slashing and rewards. * Fix compilation. * Distribute rewards. * Supply validators_count * Use identificationTuple in Unresponsivness report * Fix merge. * Make sure we don't slash if amount is zero. * We don't return an error from report_offence anymo * We actually can use vec! * Prevent division by zero if the reporters is empty * offence_forces_new_era/nominators_also_get_slashed * advance_session * Fix tests. * Update srml/staking/src/lib.rs Co-Authored-By: Robert Habermeier <rphmeier@gmail.com> * slashing_performed_according_exposure * Check that reporters receive their slice. * Small clean-up. * invulnerables_are_not_slashed * Minor clean ups. * Improve docs. * dont_slash_if_fraction_is_zero * Remove session dependency from offences. * Introduce sr-staking-primitives * Move offence under sr_staking_primitives * rename session_index * Resolves todos re using SessionIndex * Fix staking tests. * Properly scale denominator. * Fix UnresponsivnessOffence * Fix compilation. * Tests for offences. * Clean offences tests. * Fix staking doc test. * Bump spec version * Fix aura tests. * Fix node_executor * Deposit an event on offence. * Fix compilation of node-runtime * Remove aura slashing logic. * Remove HandleReport * Update docs for timeslot. * rename with_on_offence_fractions * Add should_properly_count_offences * Replace ValidatorIdByIndex with CurrentElectedSet ValidatorIdByIndex was querying the current_elected set in each call, doing loading (even though its from cache), deserializing and cloning of element. Instead of this it is more efficient to use `CurrentElectedSet`. As a small bonus, the invariant became a little bit easier: now we just rely on the fact that `keys` and `current_elected` set are of the same length rather than relying on the fact that `validator_id_by_index` would work similar to `<[T]>::get`. * Clarify babe equivocation * Fix offences. * Rename validators_count to validator_set_count * Fix squaring. * Update core/sr-staking-primitives/src/offence.rs Co-Authored-By: Gavin Wood <gavin@parity.io> * Docs for CurrentElectedSet. * Don't punish only invulnerables * Use `get/insert` instead of `mutate`. * Fix compilation * Update core/sr-staking-primitives/src/offence.rs Co-Authored-By: Gavin Wood <gavin@parity.io> * Update srml/offences/src/lib.rs Co-Authored-By: Robert Habermeier <rphmeier@gmail.com> * Update srml/im-online/src/lib.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Update srml/im-online/src/lib.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Update srml/im-online/src/lib.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Update srml/babe/src/lib.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Update core/sr-staking-primitives/src/offence.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Update core/sr-staking-primitives/src/offence.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Update core/sr-staking-primitives/src/offence.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Update core/sr-staking-primitives/src/offence.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Update core/sr-staking-primitives/src/offence.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Add aura todo. * Allow multiple reports for single offence report. * Fix slash_fraction calculation. * Fix typos. * Fix compilation and tests. * Fix staking tests. * Update srml/im-online/src/lib.rs Co-Authored-By: Logan Saether <x@logansaether.com> * Fix doc on time_slot * Allow slashing only on current era (#3411) * only slash in current era * prune journal for last era * comment own_slash * emit an event when old slashing events are discarded * Pave the way for pruning * Address issues. * Try to refactor collect_offence_reports * Other fixes. * More fixes.
313 lines
9.5 KiB
Rust
313 lines
9.5 KiB
Rust
// Copyright 2017-2019 Parity Technologies (UK) Ltd.
|
|
// This file is part of Substrate.
|
|
|
|
// Substrate is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
|
|
// Substrate is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//! Tests for the module.
|
|
|
|
#![cfg(test)]
|
|
|
|
use sr_primitives::testing::Digest;
|
|
use sr_primitives::traits::{Header, OnFinalize};
|
|
use runtime_io::with_externalities;
|
|
use crate::mock::*;
|
|
use system::{EventRecord, Phase};
|
|
use codec::{Decode, Encode};
|
|
use fg_primitives::ScheduledChange;
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn authorities_change_logged() {
|
|
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
|
|
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
|
|
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 0, None).unwrap();
|
|
|
|
System::note_finished_extrinsics();
|
|
Grandpa::on_finalize(1);
|
|
|
|
let header = System::finalize();
|
|
assert_eq!(header.digest, Digest {
|
|
logs: vec![
|
|
grandpa_log(ConsensusLog::ScheduledChange(
|
|
ScheduledChange { delay: 0, next_authorities: to_authorities(vec![(4, 1), (5, 1), (6, 1)]) }
|
|
)),
|
|
],
|
|
});
|
|
|
|
assert_eq!(System::events(), vec![
|
|
EventRecord {
|
|
phase: Phase::Finalization,
|
|
event: Event::NewAuthorities(to_authorities(vec![(4, 1), (5, 1), (6, 1)])).into(),
|
|
topics: vec![],
|
|
},
|
|
]);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn authorities_change_logged_after_delay() {
|
|
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
|
|
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
|
|
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 1, None).unwrap();
|
|
Grandpa::on_finalize(1);
|
|
let header = System::finalize();
|
|
assert_eq!(header.digest, Digest {
|
|
logs: vec![
|
|
grandpa_log(ConsensusLog::ScheduledChange(
|
|
ScheduledChange { delay: 1, next_authorities: to_authorities(vec![(4, 1), (5, 1), (6, 1)]) }
|
|
)),
|
|
],
|
|
});
|
|
|
|
// no change at this height.
|
|
assert_eq!(System::events(), vec![]);
|
|
|
|
System::initialize(&2, &header.hash(), &Default::default(), &Default::default());
|
|
System::note_finished_extrinsics();
|
|
Grandpa::on_finalize(2);
|
|
|
|
let _header = System::finalize();
|
|
assert_eq!(System::events(), vec![
|
|
EventRecord {
|
|
phase: Phase::Finalization,
|
|
event: Event::NewAuthorities(to_authorities(vec![(4, 1), (5, 1), (6, 1)])).into(),
|
|
topics: vec![],
|
|
},
|
|
]);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn cannot_schedule_change_when_one_pending() {
|
|
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
|
|
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
|
|
Grandpa::schedule_change(to_authorities(vec![(4, 1), (5, 1), (6, 1)]), 1, None).unwrap();
|
|
assert!(<PendingChange<Test>>::exists());
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_err());
|
|
|
|
Grandpa::on_finalize(1);
|
|
let header = System::finalize();
|
|
|
|
System::initialize(&2, &header.hash(), &Default::default(), &Default::default());
|
|
assert!(<PendingChange<Test>>::exists());
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_err());
|
|
|
|
Grandpa::on_finalize(2);
|
|
let header = System::finalize();
|
|
|
|
System::initialize(&3, &header.hash(), &Default::default(), &Default::default());
|
|
assert!(!<PendingChange<Test>>::exists());
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_ok());
|
|
|
|
Grandpa::on_finalize(3);
|
|
let _header = System::finalize();
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn new_decodes_from_old() {
|
|
let old = OldStoredPendingChange {
|
|
scheduled_at: 5u32,
|
|
delay: 100u32,
|
|
next_authorities: to_authorities(vec![(1, 5), (2, 10), (3, 2)]),
|
|
};
|
|
|
|
let encoded = old.encode();
|
|
let new = StoredPendingChange::<u32>::decode(&mut &encoded[..]).unwrap();
|
|
assert!(new.forced.is_none());
|
|
assert_eq!(new.scheduled_at, old.scheduled_at);
|
|
assert_eq!(new.delay, old.delay);
|
|
assert_eq!(new.next_authorities, old.next_authorities);
|
|
}
|
|
|
|
#[test]
|
|
fn dispatch_forced_change() {
|
|
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
|
|
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
|
|
Grandpa::schedule_change(
|
|
to_authorities(vec![(4, 1), (5, 1), (6, 1)]),
|
|
5,
|
|
Some(0),
|
|
).unwrap();
|
|
|
|
assert!(<PendingChange<Test>>::exists());
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, Some(0)).is_err());
|
|
|
|
Grandpa::on_finalize(1);
|
|
let mut header = System::finalize();
|
|
|
|
for i in 2..7 {
|
|
System::initialize(&i, &header.hash(), &Default::default(), &Default::default());
|
|
assert!(<PendingChange<Test>>::get().unwrap().forced.is_some());
|
|
assert_eq!(Grandpa::next_forced(), Some(11));
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_err());
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, Some(0)).is_err());
|
|
|
|
Grandpa::on_finalize(i);
|
|
header = System::finalize();
|
|
}
|
|
|
|
// change has been applied at the end of block 6.
|
|
// add a normal change.
|
|
{
|
|
System::initialize(&7, &header.hash(), &Default::default(), &Default::default());
|
|
assert!(!<PendingChange<Test>>::exists());
|
|
assert_eq!(Grandpa::grandpa_authorities(), to_authorities(vec![(4, 1), (5, 1), (6, 1)]));
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_ok());
|
|
Grandpa::on_finalize(7);
|
|
header = System::finalize();
|
|
}
|
|
|
|
// run the normal change.
|
|
{
|
|
System::initialize(&8, &header.hash(), &Default::default(), &Default::default());
|
|
assert!(<PendingChange<Test>>::exists());
|
|
assert_eq!(Grandpa::grandpa_authorities(), to_authorities(vec![(4, 1), (5, 1), (6, 1)]));
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1)]), 1, None).is_err());
|
|
Grandpa::on_finalize(8);
|
|
header = System::finalize();
|
|
}
|
|
|
|
// normal change applied. but we can't apply a new forced change for some
|
|
// time.
|
|
for i in 9..11 {
|
|
System::initialize(&i, &header.hash(), &Default::default(), &Default::default());
|
|
assert!(!<PendingChange<Test>>::exists());
|
|
assert_eq!(Grandpa::grandpa_authorities(), to_authorities(vec![(5, 1)]));
|
|
assert_eq!(Grandpa::next_forced(), Some(11));
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1), (6, 1)]), 5, Some(0)).is_err());
|
|
Grandpa::on_finalize(i);
|
|
header = System::finalize();
|
|
}
|
|
|
|
{
|
|
System::initialize(&11, &header.hash(), &Default::default(), &Default::default());
|
|
assert!(!<PendingChange<Test>>::exists());
|
|
assert!(Grandpa::schedule_change(to_authorities(vec![(5, 1), (6, 1), (7, 1)]), 5, Some(0)).is_ok());
|
|
assert_eq!(Grandpa::next_forced(), Some(21));
|
|
Grandpa::on_finalize(11);
|
|
header = System::finalize();
|
|
}
|
|
let _ = header;
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn schedule_pause_only_when_live() {
|
|
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
|
|
// we schedule a pause at block 1 with delay of 1
|
|
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
|
|
Grandpa::schedule_pause(1).unwrap();
|
|
|
|
// we've switched to the pending pause state
|
|
assert_eq!(
|
|
Grandpa::state(),
|
|
StoredState::PendingPause {
|
|
scheduled_at: 1u64,
|
|
delay: 1,
|
|
},
|
|
);
|
|
|
|
Grandpa::on_finalize(1);
|
|
let _ = System::finalize();
|
|
|
|
System::initialize(&2, &Default::default(), &Default::default(), &Default::default());
|
|
|
|
// signaling a pause now should fail
|
|
assert!(Grandpa::schedule_pause(1).is_err());
|
|
|
|
Grandpa::on_finalize(2);
|
|
let _ = System::finalize();
|
|
|
|
// after finalizing block 2 the set should have switched to paused state
|
|
assert_eq!(
|
|
Grandpa::state(),
|
|
StoredState::Paused,
|
|
);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn schedule_resume_only_when_paused() {
|
|
with_externalities(&mut new_test_ext(vec![(1, 1), (2, 1), (3, 1)]), || {
|
|
System::initialize(&1, &Default::default(), &Default::default(), &Default::default());
|
|
|
|
// the set is currently live, resuming it is an error
|
|
assert!(Grandpa::schedule_resume(1).is_err());
|
|
|
|
assert_eq!(
|
|
Grandpa::state(),
|
|
StoredState::Live,
|
|
);
|
|
|
|
// we schedule a pause to be applied instantly
|
|
Grandpa::schedule_pause(0).unwrap();
|
|
Grandpa::on_finalize(1);
|
|
let _ = System::finalize();
|
|
|
|
assert_eq!(
|
|
Grandpa::state(),
|
|
StoredState::Paused,
|
|
);
|
|
|
|
// we schedule the set to go back live in 2 blocks
|
|
System::initialize(&2, &Default::default(), &Default::default(), &Default::default());
|
|
Grandpa::schedule_resume(2).unwrap();
|
|
Grandpa::on_finalize(2);
|
|
let _ = System::finalize();
|
|
|
|
System::initialize(&3, &Default::default(), &Default::default(), &Default::default());
|
|
Grandpa::on_finalize(3);
|
|
let _ = System::finalize();
|
|
|
|
System::initialize(&4, &Default::default(), &Default::default(), &Default::default());
|
|
Grandpa::on_finalize(4);
|
|
let _ = System::finalize();
|
|
|
|
// it should be live at block 4
|
|
assert_eq!(
|
|
Grandpa::state(),
|
|
StoredState::Live,
|
|
);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn time_slot_have_sane_ord() {
|
|
// Ensure that `Ord` implementation is sane.
|
|
const FIXTURE: &[GrandpaTimeSlot] = &[
|
|
GrandpaTimeSlot {
|
|
set_id: 0,
|
|
round: 0,
|
|
},
|
|
GrandpaTimeSlot {
|
|
set_id: 0,
|
|
round: 1,
|
|
},
|
|
GrandpaTimeSlot {
|
|
set_id: 1,
|
|
round: 0,
|
|
},
|
|
GrandpaTimeSlot {
|
|
set_id: 1,
|
|
round: 1,
|
|
},
|
|
GrandpaTimeSlot {
|
|
set_id: 1,
|
|
round: 2,
|
|
}
|
|
];
|
|
assert!(FIXTURE.windows(2).all(|f| f[0] < f[1]));
|
|
}
|