referrerpolicy=no-referrer-when-downgrade

pallet_beefy/
equivocation.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! An opt-in utility module for reporting equivocations.
19//!
20//! This module defines an offence type for BEEFY equivocations
21//! and some utility traits to wire together:
22//! - a key ownership proof system (e.g. to prove that a given authority was part of a session);
23//! - a system for reporting offences;
24//! - a system for signing and submitting transactions;
25//! - a way to get the current block author;
26//!
27//! These can be used in an offchain context in order to submit equivocation
28//! reporting extrinsics (from the client that's running the BEEFY protocol).
29//! And in a runtime context, so that the BEEFY pallet can validate the
30//! equivocation proofs in the extrinsic and report the offences.
31//!
32//! IMPORTANT:
33//! When using this module for enabling equivocation reporting it is required
34//! that the `ValidateUnsigned` for the BEEFY pallet is used in the runtime
35//! definition.
36
37use alloc::{vec, vec::Vec};
38use codec::{self as codec, Decode, Encode};
39use frame_support::traits::{Get, KeyOwnerProofSystem};
40use frame_system::pallet_prelude::{BlockNumberFor, HeaderFor};
41use log::{error, info};
42use sp_consensus_beefy::{
43	check_commitment_signature, AncestryHelper, DoubleVotingProof, ForkVotingProof,
44	FutureBlockVotingProof, ValidatorSetId, KEY_TYPE as BEEFY_KEY_TYPE,
45};
46use sp_runtime::{
47	transaction_validity::{
48		InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity,
49		TransactionValidityError, ValidTransaction,
50	},
51	DispatchError, KeyTypeId, Perbill, RuntimeAppPublic,
52};
53use sp_session::{GetSessionNumber, GetValidatorCount};
54use sp_staking::{
55	offence::{Kind, Offence, OffenceReportSystem, ReportOffence},
56	SessionIndex,
57};
58
59use super::{Call, Config, Error, Pallet, LOG_TARGET};
60
61/// A round number and set id which point on the time of an offence.
62#[derive(Copy, Clone, PartialOrd, Ord, Eq, PartialEq, Encode, Decode)]
63pub struct TimeSlot<N: Copy + Clone + PartialOrd + Ord + Eq + PartialEq + Encode + Decode> {
64	// The order of these matters for `derive(Ord)`.
65	/// BEEFY Set ID.
66	pub set_id: ValidatorSetId,
67	/// Round number.
68	pub round: N,
69}
70
71/// BEEFY equivocation offence report.
72pub struct EquivocationOffence<Offender, N>
73where
74	N: Copy + Clone + PartialOrd + Ord + Eq + PartialEq + Encode + Decode,
75{
76	/// Time slot at which this incident happened.
77	pub time_slot: TimeSlot<N>,
78	/// The session index in which the incident happened.
79	pub session_index: SessionIndex,
80	/// The size of the validator set at the time of the offence.
81	pub validator_set_count: u32,
82	/// The authority which produced this equivocation.
83	pub offender: Offender,
84	/// Optional slash fraction
85	maybe_slash_fraction: Option<Perbill>,
86}
87
88impl<Offender: Clone, N> Offence<Offender> for EquivocationOffence<Offender, N>
89where
90	N: Copy + Clone + PartialOrd + Ord + Eq + PartialEq + Encode + Decode,
91{
92	const ID: Kind = *b"beefy:equivocati";
93	type TimeSlot = TimeSlot<N>;
94
95	fn offenders(&self) -> Vec<Offender> {
96		vec![self.offender.clone()]
97	}
98
99	fn session_index(&self) -> SessionIndex {
100		self.session_index
101	}
102
103	fn validator_set_count(&self) -> u32 {
104		self.validator_set_count
105	}
106
107	fn time_slot(&self) -> Self::TimeSlot {
108		self.time_slot
109	}
110
111	fn slash_fraction(&self, offenders_count: u32) -> Perbill {
112		if let Some(slash_fraction) = self.maybe_slash_fraction {
113			return slash_fraction;
114		}
115
116		// `Perbill` type domain is [0, 1] by definition
117		// The formula is min((3k / n)^2, 1)
118		// where k = offenders_number and n = validators_number
119		Perbill::from_rational(3 * offenders_count, self.validator_set_count).square()
120	}
121}
122
123/// BEEFY equivocation offence report system.
124///
125/// This type implements `OffenceReportSystem` such that:
126/// - Equivocation reports are published on-chain as unsigned extrinsic via
127///   `offchain::CreateTransactionBase`.
128/// - On-chain validity checks and processing are mostly delegated to the user provided generic
129///   types implementing `KeyOwnerProofSystem` and `ReportOffence` traits.
130/// - Offence reporter for unsigned transactions is fetched via the authorship pallet.
131pub struct EquivocationReportSystem<T, R, P, L>(core::marker::PhantomData<(T, R, P, L)>);
132
133/// Equivocation evidence convenience alias.
134pub enum EquivocationEvidenceFor<T: Config> {
135	DoubleVotingProof(
136		DoubleVotingProof<
137			BlockNumberFor<T>,
138			T::BeefyId,
139			<T::BeefyId as RuntimeAppPublic>::Signature,
140		>,
141		T::KeyOwnerProof,
142	),
143	ForkVotingProof(
144		ForkVotingProof<
145			HeaderFor<T>,
146			T::BeefyId,
147			<T::AncestryHelper as AncestryHelper<HeaderFor<T>>>::Proof,
148		>,
149		T::KeyOwnerProof,
150	),
151	FutureBlockVotingProof(FutureBlockVotingProof<BlockNumberFor<T>, T::BeefyId>, T::KeyOwnerProof),
152}
153
154impl<T: Config> EquivocationEvidenceFor<T> {
155	/// Returns the authority id of the equivocator.
156	fn offender_id(&self) -> &T::BeefyId {
157		match self {
158			EquivocationEvidenceFor::DoubleVotingProof(equivocation_proof, _) => {
159				equivocation_proof.offender_id()
160			},
161			EquivocationEvidenceFor::ForkVotingProof(equivocation_proof, _) => {
162				&equivocation_proof.vote.id
163			},
164			EquivocationEvidenceFor::FutureBlockVotingProof(equivocation_proof, _) => {
165				&equivocation_proof.vote.id
166			},
167		}
168	}
169
170	/// Returns the round number at which the equivocation occurred.
171	fn round_number(&self) -> &BlockNumberFor<T> {
172		match self {
173			EquivocationEvidenceFor::DoubleVotingProof(equivocation_proof, _) => {
174				equivocation_proof.round_number()
175			},
176			EquivocationEvidenceFor::ForkVotingProof(equivocation_proof, _) => {
177				&equivocation_proof.vote.commitment.block_number
178			},
179			EquivocationEvidenceFor::FutureBlockVotingProof(equivocation_proof, _) => {
180				&equivocation_proof.vote.commitment.block_number
181			},
182		}
183	}
184
185	/// Returns the set id at which the equivocation occurred.
186	fn set_id(&self) -> ValidatorSetId {
187		match self {
188			EquivocationEvidenceFor::DoubleVotingProof(equivocation_proof, _) => {
189				equivocation_proof.set_id()
190			},
191			EquivocationEvidenceFor::ForkVotingProof(equivocation_proof, _) => {
192				equivocation_proof.vote.commitment.validator_set_id
193			},
194			EquivocationEvidenceFor::FutureBlockVotingProof(equivocation_proof, _) => {
195				equivocation_proof.vote.commitment.validator_set_id
196			},
197		}
198	}
199
200	/// Returns the set id at which the equivocation occurred.
201	fn key_owner_proof(&self) -> &T::KeyOwnerProof {
202		match self {
203			EquivocationEvidenceFor::DoubleVotingProof(_, key_owner_proof) => key_owner_proof,
204			EquivocationEvidenceFor::ForkVotingProof(_, key_owner_proof) => key_owner_proof,
205			EquivocationEvidenceFor::FutureBlockVotingProof(_, key_owner_proof) => key_owner_proof,
206		}
207	}
208
209	fn checked_offender<P>(&self) -> Option<P::IdentificationTuple>
210	where
211		P: KeyOwnerProofSystem<(KeyTypeId, T::BeefyId), Proof = T::KeyOwnerProof>,
212	{
213		let key = (BEEFY_KEY_TYPE, self.offender_id().clone());
214		P::check_proof(key, self.key_owner_proof().clone())
215	}
216
217	fn check_equivocation_proof(self) -> Result<(), Error<T>> {
218		match self {
219			EquivocationEvidenceFor::DoubleVotingProof(equivocation_proof, _) => {
220				// Validate equivocation proof (check votes are different and signatures are valid).
221				if !sp_consensus_beefy::check_double_voting_proof(&equivocation_proof) {
222					return Err(Error::<T>::InvalidDoubleVotingProof);
223				}
224
225				Ok(())
226			},
227			EquivocationEvidenceFor::ForkVotingProof(equivocation_proof, _) => {
228				let ForkVotingProof { vote, ancestry_proof, header } = equivocation_proof;
229
230				if !<T::AncestryHelper as AncestryHelper<HeaderFor<T>>>::is_proof_optimal(
231					&ancestry_proof,
232				) {
233					return Err(Error::<T>::InvalidForkVotingProof);
234				}
235
236				let maybe_validation_context = <T::AncestryHelper as AncestryHelper<
237					HeaderFor<T>,
238				>>::extract_validation_context(header);
239				let validation_context = match maybe_validation_context {
240					Some(validation_context) => validation_context,
241					None => {
242						return Err(Error::<T>::InvalidForkVotingProof);
243					},
244				};
245
246				let is_non_canonical =
247					<T::AncestryHelper as AncestryHelper<HeaderFor<T>>>::is_non_canonical(
248						&vote.commitment,
249						ancestry_proof,
250						validation_context,
251					);
252				if !is_non_canonical {
253					return Err(Error::<T>::InvalidForkVotingProof);
254				}
255
256				let is_signature_valid =
257					check_commitment_signature(&vote.commitment, &vote.id, &vote.signature);
258				if !is_signature_valid {
259					return Err(Error::<T>::InvalidForkVotingProof);
260				}
261
262				Ok(())
263			},
264			EquivocationEvidenceFor::FutureBlockVotingProof(equivocation_proof, _) => {
265				let FutureBlockVotingProof { vote } = equivocation_proof;
266				// Check if the commitment actually targets a future block
267				if vote.commitment.block_number < frame_system::Pallet::<T>::block_number() {
268					return Err(Error::<T>::InvalidFutureBlockVotingProof);
269				}
270
271				let is_signature_valid =
272					check_commitment_signature(&vote.commitment, &vote.id, &vote.signature);
273				if !is_signature_valid {
274					return Err(Error::<T>::InvalidForkVotingProof);
275				}
276
277				Ok(())
278			},
279		}
280	}
281
282	fn slash_fraction(&self) -> Option<Perbill> {
283		match self {
284			EquivocationEvidenceFor::DoubleVotingProof(_, _) => None,
285			EquivocationEvidenceFor::ForkVotingProof(_, _) |
286			EquivocationEvidenceFor::FutureBlockVotingProof(_, _) => Some(Perbill::from_percent(50)),
287		}
288	}
289}
290
291impl<T, R, P, L> OffenceReportSystem<Option<T::AccountId>, EquivocationEvidenceFor<T>>
292	for EquivocationReportSystem<T, R, P, L>
293where
294	T: Config + pallet_authorship::Config + frame_system::offchain::CreateBare<Call<T>>,
295	R: ReportOffence<
296		T::AccountId,
297		P::IdentificationTuple,
298		EquivocationOffence<P::IdentificationTuple, BlockNumberFor<T>>,
299	>,
300	P: KeyOwnerProofSystem<(KeyTypeId, T::BeefyId), Proof = T::KeyOwnerProof>,
301	P::IdentificationTuple: Clone,
302	L: Get<u64>,
303{
304	type Longevity = L;
305
306	fn publish_evidence(evidence: EquivocationEvidenceFor<T>) -> Result<(), ()> {
307		use frame_system::offchain::SubmitTransaction;
308
309		let call: Call<T> = evidence.into();
310		let xt = T::create_bare(call.into());
311		let res = SubmitTransaction::<T, Call<T>>::submit_transaction(xt);
312		match res {
313			Ok(_) => info!(target: LOG_TARGET, "Submitted equivocation report."),
314			Err(e) => error!(target: LOG_TARGET, "Error submitting equivocation report: {:?}", e),
315		}
316		res
317	}
318
319	fn check_evidence(
320		evidence: EquivocationEvidenceFor<T>,
321	) -> Result<(), TransactionValidityError> {
322		let offender = evidence.checked_offender::<P>().ok_or(InvalidTransaction::BadProof)?;
323
324		// Check if the offence has already been reported, and if so then we can discard the report.
325		let time_slot = TimeSlot { set_id: evidence.set_id(), round: *evidence.round_number() };
326		if R::is_known_offence(&[offender], &time_slot) {
327			Err(InvalidTransaction::Stale.into())
328		} else {
329			Ok(())
330		}
331	}
332
333	fn process_evidence(
334		reporter: Option<T::AccountId>,
335		evidence: EquivocationEvidenceFor<T>,
336	) -> Result<(), DispatchError> {
337		let maybe_slash_fraction = evidence.slash_fraction();
338		let reporter = reporter.or_else(|| pallet_authorship::Pallet::<T>::author());
339
340		// We check the equivocation within the context of its set id (and associated session).
341		let set_id = evidence.set_id();
342		let round = *evidence.round_number();
343		let set_id_session_index = crate::SetIdSession::<T>::get(set_id)
344			.ok_or(Error::<T>::InvalidEquivocationProofSessionMember)?;
345
346		// Check that the session id for the membership proof is within the bounds
347		// of the set id reported in the equivocation.
348		let key_owner_proof = evidence.key_owner_proof();
349		let validator_count = key_owner_proof.validator_count();
350		let session_index = key_owner_proof.session();
351		if session_index != set_id_session_index {
352			return Err(Error::<T>::InvalidEquivocationProofSession.into());
353		}
354
355		// Validate the key ownership proof extracting the id of the offender.
356		let offender =
357			evidence.checked_offender::<P>().ok_or(Error::<T>::InvalidKeyOwnershipProof)?;
358
359		evidence.check_equivocation_proof()?;
360
361		let offence = EquivocationOffence {
362			time_slot: TimeSlot { set_id, round },
363			session_index,
364			validator_set_count: validator_count,
365			offender,
366			maybe_slash_fraction,
367		};
368		R::report_offence(reporter.into_iter().collect(), offence)
369			.map_err(|_| Error::<T>::DuplicateOffenceReport.into())
370	}
371}
372
373/// Methods for the `ValidateUnsigned` implementation:
374/// It restricts calls to `report_equivocation_unsigned` to local calls (i.e. extrinsics generated
375/// on this node) or that already in a block. This guarantees that only block authors can include
376/// unsigned equivocation reports.
377impl<T: Config> Pallet<T> {
378	pub fn validate_unsigned(source: TransactionSource, call: &Call<T>) -> TransactionValidity {
379		// discard equivocation report not coming from the local node
380		match source {
381			TransactionSource::Local | TransactionSource::InBlock => { /* allowed */ },
382			_ => {
383				log::warn!(
384					target: LOG_TARGET,
385					"rejecting unsigned report equivocation transaction because it is not local/in-block."
386				);
387				return InvalidTransaction::Call.into();
388			},
389		}
390
391		let evidence = call.to_equivocation_evidence_for().ok_or(InvalidTransaction::Call)?;
392		let tag = (evidence.offender_id().clone(), evidence.set_id(), *evidence.round_number());
393		T::EquivocationReportSystem::check_evidence(evidence)?;
394
395		let longevity =
396			<T::EquivocationReportSystem as OffenceReportSystem<_, _>>::Longevity::get();
397		ValidTransaction::with_tag_prefix("BeefyEquivocation")
398			// We assign the maximum priority for any equivocation report.
399			.priority(TransactionPriority::MAX)
400			// Only one equivocation report for the same offender at the same slot.
401			.and_provides(tag)
402			.longevity(longevity)
403			// We don't propagate this. This can never be included on a remote node.
404			.propagate(false)
405			.build()
406	}
407
408	pub fn pre_dispatch(call: &Call<T>) -> Result<(), TransactionValidityError> {
409		let evidence = call.to_equivocation_evidence_for().ok_or(InvalidTransaction::Call)?;
410		T::EquivocationReportSystem::check_evidence(evidence)
411	}
412}