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

Commit

Permalink
runtime/disputes: slashing (#5535)
Browse files Browse the repository at this point in the history
* disputes: runtime part of slashing

* disputes: reward winners

* disputes/slashing: validate_unsigned impl

* fmt

* disputes/slashing: report_dispute_lost_unsigned

* disputes/slashing: separate winners from losers and report winners

* disputes/slashing: refactoring

* impl HandleReports

* enable on Wenstend

* fmt

* add slashing pallet to the mock and test runtimes

* fix a bug in report_dispute_lost_unsigned

* fmt

* disputes: remove new_participants from summary

* disputes: remove punish_inconclusive

* impl SlashingHandler for Pallet for type-safety

* do not impl slashing::Config on mainnets yet

* teach spellcheck deduplication

* simplify interfaces and resolve some TODOs

* resolve some more TODOs

* minor typos

* move slashing into a folder

* remove unnecessary clone

* fix validator_set_count calculation

* introduce ValidatorSetCount

* store ValidatorSetCount

* fmt

* add the benchmark

* fmt

* unflatten slashing

* post-rebase fixes

* remove winners eagerly

* use real slashing weights for westend

* remove bench test suite

* zombinet: modify disputes test to check for an offence report

* zombinet: add a timeout

* add slashing pallet to Rococo

* zombienet: revert back to rococo-local

* fmt

* remove TODOs

* revert some accidental changes

* slashing is submodule of disputes

* Change the log target

Co-authored-by: Kian Paimani <[email protected]>

* wrap comments with rustfmt, more docs, constants

* use Defensive trait

* cargo update -p sp-io

* merge offence types, remove rewards for now

* cargo update -p sp-io

* benchmark fixes

* fmt

* unused var

* fix block_author impl

* ressurect RewardValidators trait

* remove outdated comment

* more module docs

* introduce BenchmarkingConfig

* typo fix

* teach spellcheck unapplied

* use Weight::new()

* fix mocking rewards

* use RefTimeWeight

* ".git/.scripts/bench-bot.sh" runtime westend-dev runtime_parachains::disputes::slashing

* refactor maybe_identify_validators

* no more ticket in disguise

* remove outdated comments

* lower against valid to 0.1%

* bump zombienet version for debug

* use from_perthousand

* post-merge fixes

* another day, another Weight changes

* Revert "bump zombienet version for debug"

This reverts commit 0d99787.

* do not reward block authors

* fix outdated comment

* use Pays from frame_support::dispatch::Pays

* add timeout to is up

Co-authored-by: Kian Paimani <[email protected]>
Co-authored-by: command-bot <>
Co-authored-by: Javier Viola <[email protected]>
Co-authored-by: Javier Viola <[email protected]>
  • Loading branch information
4 people authored Sep 20, 2022
1 parent 57f7cab commit 7ed7345
Show file tree
Hide file tree
Showing 18 changed files with 1,105 additions and 73 deletions.
2 changes: 1 addition & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,7 +1173,7 @@ impl parachains_initializer::Config for Runtime {
impl parachains_disputes::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type RewardValidators = parachains_reward_points::RewardValidatorsWithEraPoints<Runtime>;
type PunishValidators = ();
type SlashingHandler = ();
type WeightInfo = weights::runtime_parachains_disputes::WeightInfo<Runtime>;
}

Expand Down
1 change: 1 addition & 0 deletions runtime/parachains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"primitives/runtime-benchmarks",
"static_assertions",
"sp-application-crypto",
Expand Down
78 changes: 49 additions & 29 deletions runtime/parachains/src/disputes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use sp_std::{cmp::Ordering, prelude::*};
#[allow(unused_imports)]
pub(crate) use self::tests::run_to_block;

pub mod slashing;
#[cfg(test)]
mod tests;

Expand Down Expand Up @@ -73,35 +74,55 @@ impl RewardValidators for () {
}

/// Punishment hooks for disputes.
pub trait PunishValidators {
/// Punish a series of validators who were for an invalid parablock. This is expected to be a major
/// punishment.
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(
session: SessionIndex,
validators: impl IntoIterator<Item = ValidatorIndex>,
candidate_hash: CandidateHash,
losers: impl IntoIterator<Item = ValidatorIndex>,
);

/// Punish a series of validators who were against a valid parablock. This is expected to be a minor
/// punishment.
/// Punish a series of validators who were against a valid parablock. This
/// is expected to be a minor punishment.
fn punish_against_valid(
session: SessionIndex,
validators: impl IntoIterator<Item = ValidatorIndex>,
candidate_hash: CandidateHash,
losers: impl IntoIterator<Item = ValidatorIndex>,
);

/// Punish a series of validators who were part of a dispute which never concluded. This is expected
/// to be a minor punishment.
fn punish_inconclusive(
session: SessionIndex,
validators: 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(session_index: SessionIndex);
}

impl PunishValidators for () {
fn punish_for_invalid(_: SessionIndex, _: impl IntoIterator<Item = ValidatorIndex>) {}
impl<BlockNumber> SlashingHandler<BlockNumber> for () {
fn punish_for_invalid(
_: SessionIndex,
_: CandidateHash,
_: impl IntoIterator<Item = ValidatorIndex>,
) {
}

fn punish_against_valid(
_: SessionIndex,
_: CandidateHash,
_: impl IntoIterator<Item = ValidatorIndex>,
) {
}

fn punish_against_valid(_: SessionIndex, _: impl IntoIterator<Item = ValidatorIndex>) {}
fn initializer_initialize(_now: BlockNumber) -> Weight {
Weight::zero()
}

fn initializer_finalize() {}

fn punish_inconclusive(_: SessionIndex, _: impl IntoIterator<Item = ValidatorIndex>) {}
fn initializer_on_new_session(_: SessionIndex) {}
}

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

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
Expand Down Expand Up @@ -831,14 +852,7 @@ impl<T: Config> Pallet<T> {
// it would be unexpected for any change here to occur when the dispute has not concluded
// in time, as a dispute guaranteed to have at least one honest participant should
// conclude quickly.
let participating = decrement_spam(spam_slots, &dispute);

// Slight punishment as these validators have failed to make data available to
// others in a timely manner.
T::PunishValidators::punish_inconclusive(
session_index,
participating.iter_ones().map(|i| ValidatorIndex(i as _)),
);
let _participating = decrement_spam(spam_slots, &dispute);
});

weight += T::DbWeight::get().reads_writes(2, 2);
Expand Down Expand Up @@ -1187,7 +1201,9 @@ impl<T: Config> Pallet<T> {
Error::<T>::SingleSidedDispute,
);

let DisputeStatementSet { session, candidate_hash, .. } = set.clone();
let DisputeStatementSet { ref session, ref candidate_hash, .. } = set;
let session = *session;
let candidate_hash = *candidate_hash;

// we can omit spam slot checks, `fn filter_disputes_data` is
// always called before calling this `fn`.
Expand Down Expand Up @@ -1227,10 +1243,14 @@ 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(session, summary.slash_against);
T::SlashingHandler::punish_against_valid(
session,
candidate_hash,
summary.slash_against,
);

// an invalid candidate, according to 2/3. Punish those on the 'for' side.
T::PunishValidators::punish_for_invalid(session, summary.slash_for);
T::SlashingHandler::punish_for_invalid(session, candidate_hash, summary.slash_for);
}

<Disputes<T>>::insert(&session, &candidate_hash, &summary.state);
Expand Down
Loading

0 comments on commit 7ed7345

Please sign in to comment.