Migrate pallet-offences to pallet attribute macro (#8763)

* update to pallet macro

* fixes

* fix tests

* remove unwanted generic

* fix conflict

* Fix storage and tabs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
stanly-johnson
2021-05-12 22:08:46 +05:30
committed by GitHub
parent 8e2b7b7c25
commit d48243344c
3 changed files with 83 additions and 55 deletions
@@ -38,7 +38,7 @@ use pallet_balances::Config as BalancesConfig;
use pallet_babe::BabeEquivocationOffence; use pallet_babe::BabeEquivocationOffence;
use pallet_grandpa::{GrandpaEquivocationOffence, GrandpaTimeSlot}; use pallet_grandpa::{GrandpaEquivocationOffence, GrandpaTimeSlot};
use pallet_im_online::{Config as ImOnlineConfig, Pallet as ImOnline, UnresponsivenessOffence}; use pallet_im_online::{Config as ImOnlineConfig, Pallet as ImOnline, UnresponsivenessOffence};
use pallet_offences::{Config as OffencesConfig, Module as Offences}; use pallet_offences::{Config as OffencesConfig, Pallet as Offences};
use pallet_session::historical::{Config as HistoricalConfig, IdentificationTuple}; use pallet_session::historical::{Config as HistoricalConfig, IdentificationTuple};
use pallet_session::{Config as SessionConfig, SessionManager}; use pallet_session::{Config as SessionConfig, SessionManager};
use pallet_staking::{ use pallet_staking::{
+81 -53
View File
@@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
//! # Offences Module //! # Offences Pallet
//! //!
//! Tracks reported offences //! Tracks reported offences
@@ -26,16 +26,16 @@ mod mock;
mod tests; mod tests;
mod migration; mod migration;
use sp_std::vec::Vec; use sp_std::prelude::*;
use frame_support::{ use frame_support::weights::Weight;
decl_module, decl_event, decl_storage, Parameter, weights::Weight,
};
use sp_runtime::{traits::Hash, Perbill}; use sp_runtime::{traits::Hash, Perbill};
use sp_staking::{ use sp_staking::{
SessionIndex, offence::{Kind, Offence, OffenceDetails, OffenceError, OnOffenceHandler, ReportOffence},
offence::{Offence, ReportOffence, Kind, OnOffenceHandler, OffenceDetails, OffenceError}, SessionIndex
}; };
use codec::{Encode, Decode}; use codec::{Decode, Encode};
pub use pallet::*;
/// A binary blob which represents a SCALE codec-encoded `O::TimeSlot`. /// A binary blob which represents a SCALE codec-encoded `O::TimeSlot`.
type OpaqueTimeSlot = Vec<u8>; type OpaqueTimeSlot = Vec<u8>;
@@ -57,59 +57,87 @@ impl WeightInfo for () {
fn on_initialize(_d: u32, ) -> Weight { 1_000_000_000 } fn on_initialize(_d: u32, ) -> Weight { 1_000_000_000 }
} }
/// Offences trait #[frame_support::pallet]
pub trait Config: frame_system::Config { pub mod pallet {
/// The overarching event type. use super::*;
type Event: From<Event> + Into<<Self as frame_system::Config>::Event>; use frame_support::pallet_prelude::*;
/// Full identification of the validator. use frame_system::pallet_prelude::*;
type IdentificationTuple: Parameter + Ord;
/// A handler called for every offence report.
type OnOffenceHandler: OnOffenceHandler<Self::AccountId, Self::IdentificationTuple, Weight>;
}
decl_storage! { #[pallet::pallet]
trait Store for Module<T: Config> as Offences { #[pallet::generate_store(pub(super) trait Store)]
/// The primary structure that holds all offence records keyed by report identifiers. pub struct Pallet<T>(_);
Reports get(fn reports):
map hasher(twox_64_concat) ReportIdOf<T>
=> Option<OffenceDetails<T::AccountId, T::IdentificationTuple>>;
/// A vector of reports of the same kind that happened at the same time slot. /// The pallet's config trait.
ConcurrentReportsIndex: #[pallet::config]
double_map hasher(twox_64_concat) Kind, hasher(twox_64_concat) OpaqueTimeSlot pub trait Config: frame_system::Config {
=> Vec<ReportIdOf<T>>; /// The overarching event type.
type Event: From<Event> + IsType<<Self as frame_system::Config>::Event>;
/// Enumerates all reports of a kind along with the time they happened. /// Full identification of the validator.
/// type IdentificationTuple: Parameter + Ord;
/// All reports are sorted by the time of offence. /// A handler called for every offence report.
/// type OnOffenceHandler: OnOffenceHandler<Self::AccountId, Self::IdentificationTuple, Weight>;
/// Note that the actual type of this mapping is `Vec<u8>`, this is because values of
/// different types are not supported at the moment so we are doing the manual serialization.
ReportsByKindIndex: map hasher(twox_64_concat) Kind => Vec<u8>; // (O::TimeSlot, ReportIdOf<T>)
} }
}
decl_event!( /// The primary structure that holds all offence records keyed by report identifiers.
#[pallet::storage]
#[pallet::getter(fn reports)]
pub type Reports<T: Config> = StorageMap<
_,
Twox64Concat,
ReportIdOf<T>,
OffenceDetails<T::AccountId, T::IdentificationTuple>,
>;
/// A vector of reports of the same kind that happened at the same time slot.
#[pallet::storage]
pub type ConcurrentReportsIndex<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
Kind,
Twox64Concat,
OpaqueTimeSlot,
Vec<ReportIdOf<T>>,
ValueQuery,
>;
/// Enumerates all reports of a kind along with the time they happened.
///
/// All reports are sorted by the time of offence.
///
/// Note that the actual type of this mapping is `Vec<u8>`, this is because values of
/// different types are not supported at the moment so we are doing the manual serialization.
#[pallet::storage]
pub type ReportsByKindIndex<T> = StorageMap<
_,
Twox64Concat,
Kind,
Vec<u8>, // (O::TimeSlot, ReportIdOf<T>)
ValueQuery,
>;
/// Events type.
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event { pub enum Event {
/// There is an offence reported of the given `kind` happened at the `session_index` and /// There is an offence reported of the given `kind` happened at the `session_index` and
/// (kind-specific) time slot. This event is not deposited for duplicate slashes. /// (kind-specific) time slot. This event is not deposited for duplicate slashes.
/// \[kind, timeslot\]. /// \[kind, timeslot\].
Offence(Kind, OpaqueTimeSlot), Offence(Kind, OpaqueTimeSlot),
} }
);
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
fn deposit_event() = default;
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight { fn on_runtime_upgrade() -> Weight {
migration::remove_deferred_storage::<T>() migration::remove_deferred_storage::<T>()
} }
} }
#[pallet::call]
impl<T: Config> Pallet<T> {}
} }
impl<T: Config, O: Offence<T::IdentificationTuple>> impl<T: Config, O: Offence<T::IdentificationTuple>>
ReportOffence<T::AccountId, T::IdentificationTuple, O> for Module<T> ReportOffence<T::AccountId, T::IdentificationTuple, O> for Pallet<T>
where where
T::IdentificationTuple: Clone, T::IdentificationTuple: Clone,
{ {
@@ -120,11 +148,9 @@ where
// Go through all offenders in the offence report and find all offenders that were spotted // Go through all offenders in the offence report and find all offenders that were spotted
// in unique reports. // in unique reports.
let TriageOutcome { concurrent_offenders } = match Self::triage_offence_report::<O>( let TriageOutcome {
reporters, concurrent_offenders,
&time_slot, } = match Self::triage_offence_report::<O>(reporters, &time_slot, offenders) {
offenders,
) {
Some(triage) => triage, Some(triage) => triage,
// The report contained only duplicates, so there is no need to slash again. // The report contained only duplicates, so there is no need to slash again.
None => return Err(OffenceError::DuplicateReport), None => return Err(OffenceError::DuplicateReport),
@@ -136,7 +162,8 @@ where
let new_fraction = O::slash_fraction(offenders_count, validator_set_count); let new_fraction = O::slash_fraction(offenders_count, validator_set_count);
let slash_perbill: Vec<_> = (0..concurrent_offenders.len()) let slash_perbill: Vec<_> = (0..concurrent_offenders.len())
.map(|_| new_fraction.clone()).collect(); .map(|_| new_fraction.clone())
.collect();
T::OnOffenceHandler::on_offence( T::OnOffenceHandler::on_offence(
&concurrent_offenders, &concurrent_offenders,
@@ -160,7 +187,7 @@ where
} }
} }
impl<T: Config> Module<T> { impl<T: Config> Pallet<T> {
/// Compute the ID for the given report properties. /// Compute the ID for the given report properties.
/// ///
/// The report id depends on the offence kind, time slot and the id of offender. /// The report id depends on the offence kind, time slot and the id of offender.
@@ -200,7 +227,8 @@ impl<T: Config> Module<T> {
if any_new { if any_new {
// Load report details for the all reports happened at the same time. // Load report details for the all reports happened at the same time.
let concurrent_offenders = storage.concurrent_reports let concurrent_offenders = storage
.concurrent_reports
.iter() .iter()
.filter_map(|report_id| <Reports<T>>::get(report_id)) .filter_map(|report_id| <Reports<T>>::get(report_id))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@@ -238,7 +266,7 @@ impl<T: Config, O: Offence<T::IdentificationTuple>> ReportIndexStorage<T, O> {
fn load(time_slot: &O::TimeSlot) -> Self { fn load(time_slot: &O::TimeSlot) -> Self {
let opaque_time_slot = time_slot.encode(); let opaque_time_slot = time_slot.encode();
let same_kind_reports = <ReportsByKindIndex>::get(&O::ID); let same_kind_reports = ReportsByKindIndex::<T>::get(&O::ID);
let same_kind_reports = let same_kind_reports =
Vec::<(O::TimeSlot, ReportIdOf<T>)>::decode(&mut &same_kind_reports[..]) Vec::<(O::TimeSlot, ReportIdOf<T>)>::decode(&mut &same_kind_reports[..])
.unwrap_or_default(); .unwrap_or_default();
@@ -272,7 +300,7 @@ impl<T: Config, O: Offence<T::IdentificationTuple>> ReportIndexStorage<T, O> {
/// Dump the indexes to the storage. /// Dump the indexes to the storage.
fn save(self) { fn save(self) {
<ReportsByKindIndex>::insert(&O::ID, self.same_kind_reports.encode()); ReportsByKindIndex::<T>::insert(&O::ID, self.same_kind_reports.encode());
<ConcurrentReportsIndex<T>>::insert( <ConcurrentReportsIndex<T>>::insert(
&O::ID, &O::ID,
&self.opaque_time_slot, &self.opaque_time_slot,
+1 -1
View File
@@ -31,7 +31,7 @@ use sp_runtime::testing::Header;
use sp_runtime::traits::{IdentityLookup, BlakeTwo256}; use sp_runtime::traits::{IdentityLookup, BlakeTwo256};
use sp_core::H256; use sp_core::H256;
use frame_support::{ use frame_support::{
parameter_types, StorageMap, StorageDoubleMap, parameter_types,
weights::{Weight, constants::{WEIGHT_PER_SECOND, RocksDbWeight}}, weights::{Weight, constants::{WEIGHT_PER_SECOND, RocksDbWeight}},
}; };
use crate as offences; use crate as offences;