mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 14:37:57 +00:00
5e7b27e98c
* Initial setup * Adds node block * Uses UncheckedExtrinsic and removes Where section * Updates frame_system to use Block * Adds deprecation warning * Fixes pallet-timestamp * Removes Header and BlockNumber * Addresses review comments * Addresses review comments * Adds comment about compiler bug * Removes where clause * Refactors code * Fixes errors in cargo check * Fixes errors in cargo check * Fixes warnings in cargo check * Formatting * Fixes construct_runtime tests * Uses import instead of full path for BlockNumber * Uses import instead of full path for Header * Formatting * Fixes construct_runtime tests * Fixes imports in benchmarks * Formatting * Fixes construct_runtime tests * Formatting * Minor updates * Fixes construct_runtime ui tests * Fixes construct_runtime ui tests with 1.70 * Fixes docs * Fixes docs * Adds u128 mock block type * Fixes split example * fixes for cumulus * ".git/.scripts/commands/fmt/fmt.sh" * Updates new tests * Fixes fully-qualified path in few places * Formatting * Update frame/examples/default-config/src/lib.rs Co-authored-by: Juan <juangirini@gmail.com> * Update frame/support/procedural/src/construct_runtime/mod.rs Co-authored-by: Juan <juangirini@gmail.com> * ".git/.scripts/commands/fmt/fmt.sh" * Addresses some review comments * Fixes build * ".git/.scripts/commands/fmt/fmt.sh" * Update frame/democracy/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update frame/democracy/src/lib.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update frame/support/procedural/src/construct_runtime/mod.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Update frame/support/procedural/src/construct_runtime/mod.rs Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * Addresses review comments * Updates trait bounds * Minor fix * ".git/.scripts/commands/fmt/fmt.sh" * Removes unnecessary bound * ".git/.scripts/commands/fmt/fmt.sh" * Updates test * Fixes build * Adds a bound for header * ".git/.scripts/commands/fmt/fmt.sh" * Removes where block * Minor fix * Minor fix * Fixes tests * ".git/.scripts/commands/update-ui/update-ui.sh" 1.70 * Updates test * Update primitives/runtime/src/traits.rs Co-authored-by: Bastian Köcher <git@kchr.de> * Update primitives/runtime/src/traits.rs Co-authored-by: Bastian Köcher <git@kchr.de> * Updates doc * Updates doc --------- Co-authored-by: command-bot <> Co-authored-by: Juan <juangirini@gmail.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Bastian Köcher <git@kchr.de>
250 lines
9.0 KiB
Rust
250 lines
9.0 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//! An opt-in utility module for reporting equivocations.
|
|
//!
|
|
//! This module defines an offence type for BABE equivocations
|
|
//! and some utility traits to wire together:
|
|
//! - a system for reporting offences;
|
|
//! - a system for submitting unsigned transactions;
|
|
//! - a way to get the current block author;
|
|
//!
|
|
//! These can be used in an offchain context in order to submit equivocation
|
|
//! reporting extrinsics (from the client that's import BABE blocks).
|
|
//! And in a runtime context, so that the BABE pallet can validate the
|
|
//! equivocation proofs in the extrinsic and report the offences.
|
|
//!
|
|
//! IMPORTANT:
|
|
//! When using this module for enabling equivocation reporting it is required
|
|
//! that the `ValidateUnsigned` for the BABE pallet is used in the runtime
|
|
//! definition.
|
|
|
|
use frame_support::traits::{Get, KeyOwnerProofSystem};
|
|
use frame_system::pallet_prelude::HeaderFor;
|
|
use log::{error, info};
|
|
|
|
use sp_consensus_babe::{AuthorityId, EquivocationProof, Slot, KEY_TYPE};
|
|
use sp_runtime::{
|
|
transaction_validity::{
|
|
InvalidTransaction, TransactionPriority, TransactionSource, TransactionValidity,
|
|
TransactionValidityError, ValidTransaction,
|
|
},
|
|
DispatchError, KeyTypeId, Perbill,
|
|
};
|
|
use sp_session::{GetSessionNumber, GetValidatorCount};
|
|
use sp_staking::{
|
|
offence::{Kind, Offence, OffenceReportSystem, ReportOffence},
|
|
SessionIndex,
|
|
};
|
|
use sp_std::prelude::*;
|
|
|
|
use crate::{Call, Config, Error, Pallet, LOG_TARGET};
|
|
|
|
/// BABE equivocation offence report.
|
|
///
|
|
/// When a validator released two or more blocks at the same slot.
|
|
pub struct EquivocationOffence<Offender> {
|
|
/// A babe slot in which this incident happened.
|
|
pub slot: Slot,
|
|
/// The session index in which the incident happened.
|
|
pub session_index: SessionIndex,
|
|
/// The size of the validator set at the time of the offence.
|
|
pub validator_set_count: u32,
|
|
/// The authority that produced the equivocation.
|
|
pub offender: Offender,
|
|
}
|
|
|
|
impl<Offender: Clone> Offence<Offender> for EquivocationOffence<Offender> {
|
|
const ID: Kind = *b"babe:equivocatio";
|
|
type TimeSlot = Slot;
|
|
|
|
fn offenders(&self) -> Vec<Offender> {
|
|
vec![self.offender.clone()]
|
|
}
|
|
|
|
fn session_index(&self) -> SessionIndex {
|
|
self.session_index
|
|
}
|
|
|
|
fn validator_set_count(&self) -> u32 {
|
|
self.validator_set_count
|
|
}
|
|
|
|
fn time_slot(&self) -> Self::TimeSlot {
|
|
self.slot
|
|
}
|
|
|
|
// The formula is min((3k / n)^2, 1)
|
|
// where k = offenders_number and n = validators_number
|
|
fn slash_fraction(&self, offenders_count: u32) -> Perbill {
|
|
// Perbill type domain is [0, 1] by definition
|
|
Perbill::from_rational(3 * offenders_count, self.validator_set_count).square()
|
|
}
|
|
}
|
|
|
|
/// BABE equivocation offence report system.
|
|
///
|
|
/// This type implements `OffenceReportSystem` such that:
|
|
/// - Equivocation reports are published on-chain as unsigned extrinsic via
|
|
/// `offchain::SendTransactionTypes`.
|
|
/// - On-chain validity checks and processing are mostly delegated to the user provided generic
|
|
/// types implementing `KeyOwnerProofSystem` and `ReportOffence` traits.
|
|
/// - Offence reporter for unsigned transactions is fetched via the the authorship pallet.
|
|
pub struct EquivocationReportSystem<T, R, P, L>(sp_std::marker::PhantomData<(T, R, P, L)>);
|
|
|
|
impl<T, R, P, L>
|
|
OffenceReportSystem<Option<T::AccountId>, (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof)>
|
|
for EquivocationReportSystem<T, R, P, L>
|
|
where
|
|
T: Config + pallet_authorship::Config + frame_system::offchain::SendTransactionTypes<Call<T>>,
|
|
R: ReportOffence<
|
|
T::AccountId,
|
|
P::IdentificationTuple,
|
|
EquivocationOffence<P::IdentificationTuple>,
|
|
>,
|
|
P: KeyOwnerProofSystem<(KeyTypeId, AuthorityId), Proof = T::KeyOwnerProof>,
|
|
P::IdentificationTuple: Clone,
|
|
L: Get<u64>,
|
|
{
|
|
type Longevity = L;
|
|
|
|
fn publish_evidence(
|
|
evidence: (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof),
|
|
) -> Result<(), ()> {
|
|
use frame_system::offchain::SubmitTransaction;
|
|
let (equivocation_proof, key_owner_proof) = evidence;
|
|
|
|
let call = Call::report_equivocation_unsigned {
|
|
equivocation_proof: Box::new(equivocation_proof),
|
|
key_owner_proof,
|
|
};
|
|
let res = SubmitTransaction::<T, Call<T>>::submit_unsigned_transaction(call.into());
|
|
match res {
|
|
Ok(_) => info!(target: LOG_TARGET, "Submitted equivocation report"),
|
|
Err(e) => error!(target: LOG_TARGET, "Error submitting equivocation report: {:?}", e),
|
|
}
|
|
res
|
|
}
|
|
|
|
fn check_evidence(
|
|
evidence: (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof),
|
|
) -> Result<(), TransactionValidityError> {
|
|
let (equivocation_proof, key_owner_proof) = evidence;
|
|
|
|
// Check the membership proof to extract the offender's id
|
|
let key = (sp_consensus_babe::KEY_TYPE, equivocation_proof.offender.clone());
|
|
let offender =
|
|
P::check_proof(key, key_owner_proof.clone()).ok_or(InvalidTransaction::BadProof)?;
|
|
|
|
// Check if the offence has already been reported, and if so then we can discard the report.
|
|
if R::is_known_offence(&[offender], &equivocation_proof.slot) {
|
|
Err(InvalidTransaction::Stale.into())
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn process_evidence(
|
|
reporter: Option<T::AccountId>,
|
|
evidence: (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof),
|
|
) -> Result<(), DispatchError> {
|
|
let (equivocation_proof, key_owner_proof) = evidence;
|
|
let reporter = reporter.or_else(|| <pallet_authorship::Pallet<T>>::author());
|
|
let offender = equivocation_proof.offender.clone();
|
|
let slot = equivocation_proof.slot;
|
|
|
|
// Validate the equivocation proof (check votes are different and signatures are valid)
|
|
if !sp_consensus_babe::check_equivocation_proof(equivocation_proof) {
|
|
return Err(Error::<T>::InvalidEquivocationProof.into())
|
|
}
|
|
|
|
let validator_set_count = key_owner_proof.validator_count();
|
|
let session_index = key_owner_proof.session();
|
|
|
|
let epoch_index =
|
|
*slot.saturating_sub(crate::GenesisSlot::<T>::get()) / T::EpochDuration::get();
|
|
|
|
// Check that the slot number is consistent with the session index
|
|
// in the key ownership proof (i.e. slot is for that epoch)
|
|
if Pallet::<T>::session_index_for_epoch(epoch_index) != session_index {
|
|
return Err(Error::<T>::InvalidKeyOwnershipProof.into())
|
|
}
|
|
|
|
// Check the membership proof and extract the offender's id
|
|
let offender = P::check_proof((KEY_TYPE, offender), key_owner_proof)
|
|
.ok_or(Error::<T>::InvalidKeyOwnershipProof)?;
|
|
|
|
let offence = EquivocationOffence { slot, validator_set_count, offender, session_index };
|
|
|
|
R::report_offence(reporter.into_iter().collect(), offence)
|
|
.map_err(|_| Error::<T>::DuplicateOffenceReport)?;
|
|
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// Methods for the `ValidateUnsigned` implementation:
|
|
/// It restricts calls to `report_equivocation_unsigned` to local calls (i.e. extrinsics generated
|
|
/// on this node) or that already in a block. This guarantees that only block authors can include
|
|
/// unsigned equivocation reports.
|
|
impl<T: Config> Pallet<T> {
|
|
pub fn validate_unsigned(source: TransactionSource, call: &Call<T>) -> TransactionValidity {
|
|
if let Call::report_equivocation_unsigned { equivocation_proof, key_owner_proof } = call {
|
|
// discard equivocation report not coming from the local node
|
|
match source {
|
|
TransactionSource::Local | TransactionSource::InBlock => { /* allowed */ },
|
|
_ => {
|
|
log::warn!(
|
|
target: LOG_TARGET,
|
|
"rejecting unsigned report equivocation transaction because it is not local/in-block.",
|
|
);
|
|
|
|
return InvalidTransaction::Call.into()
|
|
},
|
|
}
|
|
|
|
// Check report validity
|
|
let evidence = (*equivocation_proof.clone(), key_owner_proof.clone());
|
|
T::EquivocationReportSystem::check_evidence(evidence)?;
|
|
|
|
let longevity =
|
|
<T::EquivocationReportSystem as OffenceReportSystem<_, _>>::Longevity::get();
|
|
|
|
ValidTransaction::with_tag_prefix("BabeEquivocation")
|
|
// We assign the maximum priority for any equivocation report.
|
|
.priority(TransactionPriority::max_value())
|
|
// Only one equivocation report for the same offender at the same slot.
|
|
.and_provides((equivocation_proof.offender.clone(), *equivocation_proof.slot))
|
|
.longevity(longevity)
|
|
// We don't propagate this. This can never be included on a remote node.
|
|
.propagate(false)
|
|
.build()
|
|
} else {
|
|
InvalidTransaction::Call.into()
|
|
}
|
|
}
|
|
|
|
pub fn pre_dispatch(call: &Call<T>) -> Result<(), TransactionValidityError> {
|
|
if let Call::report_equivocation_unsigned { equivocation_proof, key_owner_proof } = call {
|
|
let evidence = (*equivocation_proof.clone(), key_owner_proof.clone());
|
|
T::EquivocationReportSystem::check_evidence(evidence)
|
|
} else {
|
|
Err(InvalidTransaction::Call.into())
|
|
}
|
|
}
|
|
}
|