polkadot_primitives/v9/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, DisputeOffenceKind, 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#[derive(PartialEq, Eq, Clone, Encode, Decode, DecodeWithMemTracking, TypeInfo, Debug)]
62pub struct DisputeProof {
63 /// Time slot when the dispute occurred.
64 pub time_slot: DisputesTimeSlot,
65 /// The dispute outcome.
66 pub kind: DisputeOffenceKind,
67 /// The index of the validator who lost a dispute.
68 pub validator_index: ValidatorIndex,
69 /// The parachain session key of the validator.
70 pub validator_id: ValidatorId,
71}
72
73/// Slashes that are waiting to be applied once we have validator key
74/// identification.
75#[derive(Encode, Decode, TypeInfo, Debug, Clone)]
76pub struct PendingSlashes {
77 /// Indices and keys of the validators who lost a dispute and are pending
78 /// slashes.
79 pub keys: BTreeMap<ValidatorIndex, ValidatorId>,
80 /// The dispute outcome.
81 pub kind: DisputeOffenceKind,
82}
83
84// TODO: can we reuse this type between BABE, GRANDPA and disputes?
85/// An opaque type used to represent the key ownership proof at the runtime API
86/// boundary. The inner value is an encoded representation of the actual key
87/// ownership proof which will be parameterized when defining the runtime. At
88/// the runtime API boundary this type is unknown and as such we keep this
89/// opaque representation, implementors of the runtime API will have to make
90/// sure that all usages of `OpaqueKeyOwnershipProof` refer to the same type.
91#[derive(Decode, Encode, PartialEq, Eq, Debug, Clone, TypeInfo)]
92pub struct OpaqueKeyOwnershipProof(Vec<u8>);
93impl OpaqueKeyOwnershipProof {
94 /// Create a new `OpaqueKeyOwnershipProof` using the given encoded
95 /// representation.
96 pub fn new(inner: Vec<u8>) -> OpaqueKeyOwnershipProof {
97 OpaqueKeyOwnershipProof(inner)
98 }
99
100 /// Try to decode this `OpaqueKeyOwnershipProof` into the given concrete key
101 /// ownership proof type.
102 pub fn decode<T: Decode>(self) -> Option<T> {
103 Decode::decode(&mut &self.0[..]).ok()
104 }
105
106 /// Length of the encoded proof.
107 pub fn len(&self) -> usize {
108 self.0.len()
109 }
110}