Offences report system rework (#13425)

* Experiments with common equivocation trait

* Improved equivocation trait

* Fix grandpa equivocation implementation

* Remove some cruft

* Remove some more cruft

* More generic naming

* Simplification of offences manipilation

* More refactory

* Some prograss with the encapsulation of offence report system

* Finally unit type works as a universal null report system

* Align substrate node code

* Further simplification

* Fix test utils

* Remove not required associated type

* Fix benches

* Rollback to prev field name

* Box big params

* Fix typo

* Remove new tag computation

* Remove default implementations

* Better docs

* Return 'Result' instead of bool

* Change offence report system return types

* Some renaming and documentation

* Improve documentation

* More abstract offence report system

* Rename 'consume_evidence' to 'process_evidence'

* Further docs refinements

* Doc for dummy offence report

* Fix rustdoc

* Fix after master merge

* Apply code review suggestions

* Improve docs
This commit is contained in:
Davide Galassi
2023-03-07 21:25:55 +01:00
committed by GitHub
parent e16f15119f
commit 9dd10d131d
18 changed files with 499 additions and 741 deletions
@@ -240,6 +240,9 @@ pub struct EquivocationProof<H, N> {
equivocation: Equivocation<H, N>,
}
// Don't bother the grandpa crate...
impl<H: PartialEq, N: PartialEq> Eq for EquivocationProof<H, N> {}
impl<H, N> EquivocationProof<H, N> {
/// Create a new `EquivocationProof` for the given set id and using the
/// given equivocation as proof.
@@ -130,7 +130,7 @@ impl SlotDuration {
/// produces more than one block on the same slot. The proof of equivocation
/// are the given distinct headers that were signed by the validator and which
/// include the slot number.
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo)]
#[derive(Clone, Debug, Decode, Encode, PartialEq, TypeInfo, Eq)]
pub struct EquivocationProof<Header, Id> {
/// Returns the authority id of the equivocator.
pub offender: Id,
+68 -3
View File
@@ -18,10 +18,10 @@
//! Common traits and types that are useful for describing offences for usage in environments
//! that use staking.
use sp_std::vec::Vec;
use codec::{Decode, Encode};
use sp_runtime::Perbill;
use sp_core::Get;
use sp_runtime::{transaction_validity::TransactionValidityError, DispatchError, Perbill};
use sp_std::vec::Vec;
use crate::SessionIndex;
@@ -209,3 +209,68 @@ pub struct OffenceDetails<Reporter, Offender> {
/// particular reporters.
pub reporters: Vec<Reporter>,
}
/// An abstract system to publish, check and process offence evidences.
///
/// Implementation details are left opaque and we don't assume any specific usage
/// scenario for this trait at this level. The main goal is to group together some
/// common actions required during a typical offence report flow.
///
/// Even though this trait doesn't assume too much, this is a general guideline
/// for a typical usage scenario:
///
/// 1. An offence is detected and an evidence is submitted on-chain via the
/// [`OffenceReportSystem::publish_evidence`] method. This will construct
/// and submit an extrinsic transaction containing the offence evidence.
///
/// 2. If the extrinsic is unsigned then the transaction receiver may want to
/// perform some preliminary checks before further processing. This is a good
/// place to call the [`OffenceReportSystem::check_evidence`] method.
///
/// 3. Finally the report extrinsic is executed on-chain. This is where the user
/// calls the [`OffenceReportSystem::process_evidence`] to consume the offence
/// report and enact any required action.
pub trait OffenceReportSystem<Reporter, Evidence> {
/// Longevity, in blocks, for the evidence report validity.
///
/// For example, when using the staking pallet this should be set equal
/// to the bonding duration in blocks, not eras.
type Longevity: Get<u64>;
/// Publish an offence evidence.
///
/// Common usage: submit the evidence on-chain via some kind of extrinsic.
fn publish_evidence(evidence: Evidence) -> Result<(), ()>;
/// Check an offence evidence.
///
/// Common usage: preliminary validity check before execution
/// (e.g. for unsigned extrinsic quick checks).
fn check_evidence(evidence: Evidence) -> Result<(), TransactionValidityError>;
/// Process an offence evidence.
///
/// Common usage: enact some form of slashing directly or by forwarding
/// the evidence to a lower level specialized subsystem (e.g. a handler
/// implementing `ReportOffence` trait).
fn process_evidence(reporter: Reporter, evidence: Evidence) -> Result<(), DispatchError>;
}
/// Dummy offence report system.
///
/// Doesn't do anything special and returns `Ok(())` for all the actions.
impl<Reporter, Evidence> OffenceReportSystem<Reporter, Evidence> for () {
type Longevity = ();
fn publish_evidence(_evidence: Evidence) -> Result<(), ()> {
Ok(())
}
fn check_evidence(_evidence: Evidence) -> Result<(), TransactionValidityError> {
Ok(())
}
fn process_evidence(_reporter: Reporter, _evidence: Evidence) -> Result<(), DispatchError> {
Ok(())
}
}