Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
impl SlashingHandler for Pallet for type-safety
Browse files Browse the repository at this point in the history
  • Loading branch information
ordian committed May 17, 2022
1 parent 30cbd5b commit 662503c
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 35 deletions.
2 changes: 1 addition & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,7 @@ impl parachains_initializer::Config for Runtime {

impl parachains_disputes::Config for Runtime {
type Event = Event;
type PunishValidators = ();
type SlashingHandler = ();
type WeightInfo = weights::runtime_parachains_disputes::WeightInfo<Runtime>;
}

Expand Down
27 changes: 22 additions & 5 deletions runtime/parachains/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub enum DisputeResult {
}

/// Punishment hooks for disputes.
pub trait PunishValidators {
pub trait SlashingHandler<BlockNumber> {
/// Punish a series of validators who were for an invalid parablock. This is expected to be a major
/// punishment.
fn punish_for_invalid(
Expand All @@ -80,9 +80,18 @@ pub trait PunishValidators {
losers: impl IntoIterator<Item = ValidatorIndex>,
winners: impl IntoIterator<Item = ValidatorIndex>,
);

/// Called by the initializer to initialize the slashing pallet.
fn initializer_initialize(now: BlockNumber) -> Weight;

/// Called by the initializer to finalize the slashing pallet.
fn initializer_finalize();

/// Called by the initializer to note that a new session has started.
fn initializer_on_new_session(notification: &SessionChangeNotification<BlockNumber>);
}

impl PunishValidators for () {
impl<BlockNumber> SlashingHandler<BlockNumber> for () {
fn punish_for_invalid(
_: SessionIndex,
_: CandidateHash,
Expand All @@ -98,6 +107,14 @@ impl PunishValidators for () {
_: impl IntoIterator<Item = ValidatorIndex>,
) {
}

fn initializer_initialize(_now: BlockNumber) -> Weight {
0
}

fn initializer_finalize() {}

fn initializer_on_new_session(_notification: &SessionChangeNotification<BlockNumber>) {}
}

/// Binary discriminator to determine if the expensive signature
Expand Down Expand Up @@ -408,7 +425,7 @@ pub mod pallet {
pub trait Config: frame_system::Config + configuration::Config + session_info::Config {
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

type PunishValidators: PunishValidators;
type SlashingHandler: SlashingHandler<Self::BlockNumber>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
Expand Down Expand Up @@ -1146,15 +1163,15 @@ impl<T: Config> Pallet<T> {
// Slash participants on a losing side.
{
// a valid candidate, according to 2/3. Punish those on the 'against' side.
T::PunishValidators::punish_against_valid(
T::SlashingHandler::punish_against_valid(
session,
candidate_hash,
summary.slash_against,
summary.state.validators_for.iter_ones().map(|i| ValidatorIndex(i as _)),
);

// an invalid candidate, according to 2/3. Punish those on the 'for' side.
T::PunishValidators::punish_for_invalid(
T::SlashingHandler::punish_for_invalid(
session,
candidate_hash,
summary.slash_for,
Expand Down
37 changes: 19 additions & 18 deletions runtime/parachains/src/disputes/slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
}

fn slash_fraction(_offenders: u32, _validator_set_count: u32) -> Perbill {
Perbill::from_percent(100) // TODO
Perbill::from_percent(100)
}
}

Expand Down Expand Up @@ -155,7 +155,7 @@ where
}

fn slash_fraction(_offenders: u32, _validator_set_count: u32) -> Perbill {
Perbill::from_percent(1) // TODO
Perbill::from_percent(1)
}
}

Expand All @@ -170,18 +170,9 @@ impl<C, R> Default for SlashValidatorsForDisputes<C, R> {
}
}

impl<T, R> SlashValidatorsForDisputes<T, R>
impl<T, R> SlashValidatorsForDisputes<Pallet<T>, R>
where
T: Config + crate::session_info::Config,
R: ReportOffence<
AccountId<T>,
IdentificationTuple<T>,
ForInvalidOffence<IdentificationTuple<T>>,
> + ReportOffence<
AccountId<T>,
IdentificationTuple<T>,
AgainstValidOffence<IdentificationTuple<T>>,
>,
{
/// If in the current session, returns the identified validators
/// along with the validator set count for that session. `None` otherwise.
Expand Down Expand Up @@ -212,7 +203,7 @@ where
}
}

impl<T, R> super::PunishValidators for SlashValidatorsForDisputes<T, R>
impl<T, R> super::SlashingHandler<T::BlockNumber> for SlashValidatorsForDisputes<Pallet<T>, R>
where
T: Config + crate::session_info::Config,
// TODO: use HandleReports instead?
Expand Down Expand Up @@ -299,6 +290,18 @@ where
<PendingAgainstValidLosers<T>>::insert(session_index, candidate_hash, losers);
<AgainstValidWinners<T>>::insert(session_index, candidate_hash, winners);
}

fn initializer_initialize(now: T::BlockNumber) -> Weight {
Pallet::<T>::initializer_initialize(now)
}

fn initializer_finalize() {
Pallet::<T>::initializer_finalize()
}

fn initializer_on_new_session(notification: &SessionChangeNotification<T::BlockNumber>) {
Pallet::<T>::initializer_on_new_session(notification)
}
}

#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, RuntimeDebug, TypeInfo)]
Expand Down Expand Up @@ -690,17 +693,15 @@ pub mod pallet {

impl<T: Config> Pallet<T> {
/// Called by the initializer to initialize the disputes slashing module.
pub(crate) fn initializer_initialize(_now: T::BlockNumber) -> Weight {
fn initializer_initialize(_now: T::BlockNumber) -> Weight {
0
}

/// Called by the initializer to finalize the disputes slashing pallet.
pub(crate) fn initializer_finalize() {}
fn initializer_finalize() {}

/// Called by the initializer to note a new session in the disputes slashing pallet.
pub(crate) fn initializer_on_new_session(
notification: &SessionChangeNotification<T::BlockNumber>,
) {
fn initializer_on_new_session(notification: &SessionChangeNotification<T::BlockNumber>) {
let config = <super::configuration::Pallet<T>>::config();
if notification.session_index <= config.dispute_period + 1 {
return
Expand Down
10 changes: 5 additions & 5 deletions runtime/parachains/src/initializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use crate::{
configuration::{self, HostConfiguration},
disputes::{self, DisputesHandler},
disputes::{self, DisputesHandler as _, SlashingHandler as _},
dmp, hrmp, inclusion, paras, scheduler, session_info, shared, ump,
};
use frame_support::{
Expand Down Expand Up @@ -109,7 +109,7 @@ pub mod pallet {
+ scheduler::Config
+ inclusion::Config
+ session_info::Config
+ disputes::slashing::Config
+ disputes::Config
+ dmp::Config
+ ump::Config
+ hrmp::Config
Expand Down Expand Up @@ -164,7 +164,7 @@ pub mod pallet {
inclusion::Pallet::<T>::initializer_initialize(now) +
session_info::Pallet::<T>::initializer_initialize(now) +
T::DisputesHandler::initializer_initialize(now) +
disputes::slashing::Pallet::<T>::initializer_initialize(now) +
T::SlashingHandler::initializer_initialize(now) +
dmp::Pallet::<T>::initializer_initialize(now) +
ump::Pallet::<T>::initializer_initialize(now) +
hrmp::Pallet::<T>::initializer_initialize(now);
Expand All @@ -179,7 +179,7 @@ pub mod pallet {
hrmp::Pallet::<T>::initializer_finalize();
ump::Pallet::<T>::initializer_finalize();
dmp::Pallet::<T>::initializer_finalize();
disputes::slashing::Pallet::<T>::initializer_finalize();
T::SlashingHandler::initializer_finalize();
T::DisputesHandler::initializer_finalize();
session_info::Pallet::<T>::initializer_finalize();
inclusion::Pallet::<T>::initializer_finalize();
Expand Down Expand Up @@ -263,7 +263,7 @@ impl<T: Config> Pallet<T> {
inclusion::Pallet::<T>::initializer_on_new_session(&notification);
session_info::Pallet::<T>::initializer_on_new_session(&notification);
T::DisputesHandler::initializer_on_new_session(&notification);
disputes::slashing::Pallet::<T>::initializer_on_new_session(&notification);
T::SlashingHandler::initializer_on_new_session(&notification);
dmp::Pallet::<T>::initializer_on_new_session(&notification, &outgoing_paras);
ump::Pallet::<T>::initializer_on_new_session(&notification, &outgoing_paras);
hrmp::Pallet::<T>::initializer_on_new_session(&notification, &outgoing_paras);
Expand Down
12 changes: 10 additions & 2 deletions runtime/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl crate::hrmp::Config for Test {

impl crate::disputes::Config for Test {
type Event = Event;
type PunishValidators = Self;
type SlashingHandler = Self;
type WeightInfo = crate::disputes::TestWeightInfo;
}

Expand All @@ -266,7 +266,7 @@ thread_local! {
pub static PUNISH_VALIDATORS_AGAINST: RefCell<Vec<(SessionIndex, Vec<ValidatorIndex>)>> = RefCell::new(Vec::new());
}

impl crate::disputes::PunishValidators for Test {
impl crate::disputes::SlashingHandler<BlockNumber> for Test {
fn punish_for_invalid(
session: SessionIndex,
_: CandidateHash,
Expand All @@ -285,6 +285,14 @@ impl crate::disputes::PunishValidators for Test {
PUNISH_VALIDATORS_AGAINST
.with(|r| r.borrow_mut().push((session, losers.into_iter().collect())))
}

fn initializer_initialize(_now: BlockNumber) -> Weight {
0
}

fn initializer_finalize() {}

fn initializer_on_new_session(_: &initializer::SessionChangeNotification<BlockNumber>) {}
}

impl crate::scheduler::Config for Test {}
Expand Down
2 changes: 1 addition & 1 deletion runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,7 @@ impl parachains_initializer::Config for Runtime {

impl parachains_disputes::Config for Runtime {
type Event = Event;
type PunishValidators = ();
type SlashingHandler = ();
type WeightInfo = weights::runtime_parachains_disputes::WeightInfo<Runtime>;
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl pallet_session::historical::Config for Runtime {

impl parachains_disputes::Config for Runtime {
type Event = Event;
type PunishValidators = ();
type SlashingHandler = ();
type WeightInfo = weights::runtime_parachains_disputes::WeightInfo<Runtime>;
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ impl parachains_inclusion::Config for Runtime {

impl parachains_disputes::Config for Runtime {
type Event = Event;
type PunishValidators = ();
type SlashingHandler = ();
type WeightInfo = parachains_disputes::TestWeightInfo;
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,7 @@ impl assigned_slots::Config for Runtime {

impl parachains_disputes::Config for Runtime {
type Event = Event;
type PunishValidators = parachains_slashing::SlashValidatorsForDisputes<Self, Offences>;
type SlashingHandler = parachains_slashing::SlashValidatorsForDisputes<ParasSlashing, Offences>;
type WeightInfo = weights::runtime_parachains_disputes::WeightInfo<Runtime>;
}

Expand Down

0 comments on commit 662503c

Please sign in to comment.