polkadot_primitives/v8/
slashing.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17//! Primitives types used for dispute slashing.
18
19use crate::{CandidateHash, SessionIndex, ValidatorId, ValidatorIndex};
20use alloc::{collections::btree_map::BTreeMap, vec::Vec};
21use codec::{Decode, DecodeWithMemTracking, Encode};
22use scale_info::TypeInfo;
23
24/// The kind of the slashing offence (those come from disputes).
25///
26/// Notes:
27/// Will soon be fully eclipsed by the expanded `DisputeOffenceKind` enum.
28/// Only kept for backwards compatibility through old runtime apis.
29#[derive(PartialEq, Eq, Clone, Copy, Encode, Decode, DecodeWithMemTracking, TypeInfo, Debug)]
30pub enum SlashingOffenceKind {
31	/// A severe offence when a validator backed an invalid block.
32	#[codec(index = 0)]
33	ForInvalid,
34	/// A minor offence when a validator disputed a valid block.
35	#[codec(index = 1)]
36	AgainstValid,
37}
38
39/// Timeslots should uniquely identify offences and are used for the offence
40/// deduplication.
41#[derive(
42	Eq, PartialEq, Ord, PartialOrd, Clone, Encode, Decode, DecodeWithMemTracking, TypeInfo, Debug,
43)]
44pub struct DisputesTimeSlot {
45	// The order of the fields matters for `derive(Ord)`.
46	/// Session index when the candidate was backed/included.
47	pub session_index: SessionIndex,
48	/// Candidate hash of the disputed candidate.
49	pub candidate_hash: CandidateHash,
50}
51
52impl DisputesTimeSlot {
53	/// Create a new instance of `Self`.
54	pub fn new(session_index: SessionIndex, candidate_hash: CandidateHash) -> Self {
55		Self { session_index, candidate_hash }
56	}
57}
58
59/// We store most of the information about a lost dispute on chain. This struct
60/// is required to identify and verify it.
61///
62/// Notes:
63/// Will soon be fully eclipsed by the expanded vstaging `DisputeProof` struct
64/// that uses the newer `DisputeOffenceKind` enum instead.
65/// Only kept for backwards compatibility.
66#[derive(PartialEq, Eq, Clone, Encode, Decode, DecodeWithMemTracking, TypeInfo, Debug)]
67pub struct DisputeProof {
68	/// Time slot when the dispute occurred.
69	pub time_slot: DisputesTimeSlot,
70	/// The dispute outcome.
71	pub kind: SlashingOffenceKind,
72	/// The index of the validator who lost a dispute.
73	pub validator_index: ValidatorIndex,
74	/// The parachain session key of the validator.
75	pub validator_id: ValidatorId,
76}
77
78/// Slashes that are waiting to be applied once we have validator key
79/// identification.
80///
81/// Notes:
82/// Will soon be fully eclipsed by the expanded vstaging `PendingSlashes` struct
83/// that uses the newer `DisputeOffenceKind` enum instead.
84/// Only kept for backwards compatibility.
85#[derive(Encode, Decode, TypeInfo, Debug, Clone)]
86pub struct PendingSlashes {
87	/// Indices and keys of the validators who lost a dispute and are pending
88	/// slashes.
89	pub keys: BTreeMap<ValidatorIndex, ValidatorId>,
90	/// The dispute outcome.
91	pub kind: SlashingOffenceKind,
92}
93
94// TODO: can we reuse this type between BABE, GRANDPA and disputes?
95/// An opaque type used to represent the key ownership proof at the runtime API
96/// boundary. The inner value is an encoded representation of the actual key
97/// ownership proof which will be parameterized when defining the runtime. At
98/// the runtime API boundary this type is unknown and as such we keep this
99/// opaque representation, implementors of the runtime API will have to make
100/// sure that all usages of `OpaqueKeyOwnershipProof` refer to the same type.
101#[derive(Decode, Encode, PartialEq, Eq, Debug, Clone, TypeInfo)]
102pub struct OpaqueKeyOwnershipProof(Vec<u8>);
103impl OpaqueKeyOwnershipProof {
104	/// Create a new `OpaqueKeyOwnershipProof` using the given encoded
105	/// representation.
106	pub fn new(inner: Vec<u8>) -> OpaqueKeyOwnershipProof {
107		OpaqueKeyOwnershipProof(inner)
108	}
109
110	/// Try to decode this `OpaqueKeyOwnershipProof` into the given concrete key
111	/// ownership proof type.
112	pub fn decode<T: Decode>(self) -> Option<T> {
113		Decode::decode(&mut &self.0[..]).ok()
114	}
115
116	/// Length of the encoded proof.
117	pub fn len(&self) -> usize {
118		self.0.len()
119	}
120}