runtime: past session slashing runtime API (#6667)

* runtime/vstaging: unapplied_slashes runtime API

* runtime/vstaging: key_ownership_proof runtime API

* runtime/ParachainHost: submit_report_dispute_lost

* fix key_ownership_proof API

* runtime: submit_report_dispute_lost runtime API

* nits

* Update node/subsystem-types/src/messages.rs

Co-authored-by: Marcin S. <marcin@bytedude.com>

* revert unrelated fmt changes

* post merge fixes

* fix compilation

---------

Co-authored-by: Marcin S. <marcin@bytedude.com>
This commit is contained in:
ordian
2023-05-26 11:35:46 +02:00
committed by GitHub
parent 035b24866c
commit 9bc4f62eff
12 changed files with 441 additions and 96 deletions
+22 -4
View File
@@ -111,10 +111,10 @@
//! from the stable primitives.
use crate::{
BlockNumber, CandidateCommitments, CandidateEvent, CandidateHash, CommittedCandidateReceipt,
CoreState, DisputeState, ExecutorParams, GroupRotationInfo, OccupiedCoreAssumption,
PersistedValidationData, PvfCheckStatement, ScrapedOnChainVotes, SessionIndex, SessionInfo,
ValidatorId, ValidatorIndex, ValidatorSignature,
vstaging, BlockNumber, CandidateCommitments, CandidateEvent, CandidateHash,
CommittedCandidateReceipt, CoreState, DisputeState, ExecutorParams, GroupRotationInfo,
OccupiedCoreAssumption, PersistedValidationData, PvfCheckStatement, ScrapedOnChainVotes,
SessionIndex, SessionInfo, ValidatorId, ValidatorIndex, ValidatorSignature,
};
use parity_scale_codec::{Decode, Encode};
use polkadot_core_primitives as pcp;
@@ -218,5 +218,23 @@ sp_api::decl_runtime_apis! {
/// Returns execution parameters for the session.
fn session_executor_params(session_index: SessionIndex) -> Option<ExecutorParams>;
/// Returns a list of validators that lost a past session dispute and need to be slashed.
#[api_version(5)]
fn unapplied_slashes() -> Vec<(SessionIndex, CandidateHash, vstaging::slashing::PendingSlashes)>;
/// Returns a merkle proof of a validator session key.
#[api_version(5)]
fn key_ownership_proof(
validator_id: ValidatorId,
) -> Option<vstaging::slashing::OpaqueKeyOwnershipProof>;
/// Submit an unsigned extrinsic to slash validators who lost a dispute about
/// a candidate of a past session.
#[api_version(5)]
fn submit_report_dispute_lost(
dispute_proof: vstaging::slashing::DisputeProof,
key_ownership_proof: vstaging::slashing::OpaqueKeyOwnershipProof,
) -> Option<()>;
}
}
+1
View File
@@ -18,6 +18,7 @@
// Put any primitives used by staging APIs functions here
pub use crate::v4::*;
pub mod slashing;
use sp_std::prelude::*;
use parity_scale_codec::{Decode, Encode};
@@ -0,0 +1,99 @@
// Copyright 2017-2023 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Primitives types used for dispute slashing.
use crate::v4::{CandidateHash, SessionIndex, ValidatorId, ValidatorIndex};
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};
/// The kind of the dispute offence.
#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, TypeInfo, Debug)]
pub enum SlashingOffenceKind {
/// A severe offence when a validator backed an invalid block.
#[codec(index = 0)]
ForInvalid,
/// A minor offence when a validator disputed a valid block.
#[codec(index = 1)]
AgainstValid,
}
/// Timeslots should uniquely identify offences and are used for the offence
/// deduplication.
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Encode, Decode, TypeInfo, Debug)]
pub struct DisputesTimeSlot {
// The order of the fields matters for `derive(Ord)`.
/// Session index when the candidate was backed/included.
pub session_index: SessionIndex,
/// Candidate hash of the disputed candidate.
pub candidate_hash: CandidateHash,
}
impl DisputesTimeSlot {
/// Create a new instance of `Self`.
pub fn new(session_index: SessionIndex, candidate_hash: CandidateHash) -> Self {
Self { session_index, candidate_hash }
}
}
/// We store most of the information about a lost dispute on chain. This struct
/// is required to identify and verify it.
#[derive(PartialEq, Eq, Clone, Encode, Decode, TypeInfo, Debug)]
pub struct DisputeProof {
/// Time slot when the dispute occured.
pub time_slot: DisputesTimeSlot,
/// The dispute outcome.
pub kind: SlashingOffenceKind,
/// The index of the validator who lost a dispute.
pub validator_index: ValidatorIndex,
/// The parachain session key of the validator.
pub validator_id: ValidatorId,
}
/// Slashes that are waiting to be applied once we have validator key
/// identification.
#[derive(Encode, Decode, TypeInfo, Debug, Clone)]
pub struct PendingSlashes {
/// Indices and keys of the validators who lost a dispute and are pending
/// slashes.
pub keys: BTreeMap<ValidatorIndex, ValidatorId>,
/// The dispute outcome.
pub kind: SlashingOffenceKind,
}
// TODO: can we reuse this type between BABE, GRANDPA and disputes?
/// An opaque type used to represent the key ownership proof at the runtime API
/// boundary. The inner value is an encoded representation of the actual key
/// ownership proof which will be parameterized when defining the runtime. At
/// the runtime API boundary this type is unknown and as such we keep this
/// opaque representation, implementors of the runtime API will have to make
/// sure that all usages of `OpaqueKeyOwnershipProof` refer to the same type.
#[derive(Decode, Encode, PartialEq, Eq, Debug, Clone, TypeInfo)]
pub struct OpaqueKeyOwnershipProof(Vec<u8>);
impl OpaqueKeyOwnershipProof {
/// Create a new `OpaqueKeyOwnershipProof` using the given encoded
/// representation.
pub fn new(inner: Vec<u8>) -> OpaqueKeyOwnershipProof {
OpaqueKeyOwnershipProof(inner)
}
/// Try to decode this `OpaqueKeyOwnershipProof` into the given concrete key
/// ownership proof type.
pub fn decode<T: Decode>(self) -> Option<T> {
Decode::decode(&mut &self.0[..]).ok()
}
}