Skip to content

Commit

Permalink
Merge branch 'master' into xcm-transact-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoaguirre authored Dec 2, 2024
2 parents 5af33c7 + 3d8da81 commit 2fa07bc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 46 deletions.
107 changes: 63 additions & 44 deletions substrate/frame/offences/benchmarking/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use alloc::{vec, vec::Vec};

use frame_benchmarking::v1::{account, benchmarks};
use frame_benchmarking::v2::*;
use frame_support::traits::Get;
use frame_system::{Config as SystemConfig, Pallet as System, RawOrigin};

Expand Down Expand Up @@ -144,7 +144,7 @@ fn create_offender<T: Config>(n: u32, nominators: u32) -> Result<Offender<T>, &'
fn make_offenders<T: Config>(
num_offenders: u32,
num_nominators: u32,
) -> Result<(Vec<IdentificationTuple<T>>, Vec<Offender<T>>), &'static str> {
) -> Result<Vec<IdentificationTuple<T>>, &'static str> {
Staking::<T>::new_session(0);

let mut offenders = vec![];
Expand All @@ -167,29 +167,58 @@ fn make_offenders<T: Config>(
.expect("failed to convert validator id to full identification")
})
.collect::<Vec<IdentificationTuple<T>>>();
Ok((id_tuples, offenders))
Ok(id_tuples)
}

benchmarks! {
where_clause {
where
#[cfg(test)]
fn assert_all_slashes_applied<T>(offender_count: usize)
where
T: Config,
<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_staking::Event<T>>,
<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_balances::Event<T>>,
<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_offences::Event>,
<T as frame_system::Config>::RuntimeEvent: TryInto<frame_system::Event<T>>,
{
// make sure that all slashes have been applied
// (n nominators + one validator) * (slashed + unlocked) + deposit to reporter +
// reporter account endowed + some funds rescinded from issuance.
assert_eq!(
System::<T>::read_events_for_pallet::<pallet_balances::Event<T>>().len(),
2 * (offender_count + 1) + 3
);
// (n nominators + one validator) * slashed + Slash Reported
assert_eq!(
System::<T>::read_events_for_pallet::<pallet_staking::Event<T>>().len(),
1 * (offender_count + 1) + 1
);
// offence
assert_eq!(System::<T>::read_events_for_pallet::<pallet_offences::Event>().len(), 1);
// reporter new account
assert_eq!(System::<T>::read_events_for_pallet::<frame_system::Event<T>>().len(), 1);
}

#[benchmarks(
where
<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_staking::Event<T>>,
<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_balances::Event<T>>,
<T as frame_system::Config>::RuntimeEvent: TryInto<pallet_offences::Event>,
<T as frame_system::Config>::RuntimeEvent: TryInto<frame_system::Event<T>>,
}

report_offence_grandpa {
let n in 0 .. MAX_NOMINATORS.min(MaxNominationsOf::<T>::get());

)]
mod benchmarks {
use super::*;

#[benchmark]
pub fn report_offence_grandpa(
n: Linear<0, { MAX_NOMINATORS.min(MaxNominationsOf::<T>::get()) }>,
) -> Result<(), BenchmarkError> {
// for grandpa equivocation reports the number of reporters
// and offenders is always 1
let reporters = vec![account("reporter", 1, SEED)];

// make sure reporters actually get rewarded
Staking::<T>::set_slash_reward_fraction(Perbill::one());

let (mut offenders, raw_offenders) = make_offenders::<T>(1, n)?;
let mut offenders = make_offenders::<T>(1, n)?;
let validator_set_count = Session::<T>::validators().len() as u32;

let offence = GrandpaEquivocationOffence {
Expand All @@ -199,36 +228,32 @@ benchmarks! {
offender: T::convert(offenders.pop().unwrap()),
};
assert_eq!(System::<T>::event_count(), 0);
}: {
let _ = Offences::<T>::report_offence(reporters, offence);
}
verify {

#[block]
{
let _ = Offences::<T>::report_offence(reporters, offence);
}

#[cfg(test)]
{
// make sure that all slashes have been applied
// (n nominators + one validator) * (slashed + unlocked) + deposit to reporter + reporter
// account endowed + some funds rescinded from issuance.
assert_eq!(System::<T>::read_events_for_pallet::<pallet_balances::Event<T>>().len(), 2 * (n + 1) as usize + 3);
// (n nominators + one validator) * slashed + Slash Reported
assert_eq!(System::<T>::read_events_for_pallet::<pallet_staking::Event<T>>().len(), 1 * (n + 1) as usize + 1);
// offence
assert_eq!(System::<T>::read_events_for_pallet::<pallet_offences::Event>().len(), 1);
// reporter new account
assert_eq!(System::<T>::read_events_for_pallet::<frame_system::Event<T>>().len(), 1);
assert_all_slashes_applied::<T>(n as usize);
}
}

report_offence_babe {
let n in 0 .. MAX_NOMINATORS.min(MaxNominationsOf::<T>::get());
Ok(())
}

#[benchmark]
fn report_offence_babe(
n: Linear<0, { MAX_NOMINATORS.min(MaxNominationsOf::<T>::get()) }>,
) -> Result<(), BenchmarkError> {
// for babe equivocation reports the number of reporters
// and offenders is always 1
let reporters = vec![account("reporter", 1, SEED)];

// make sure reporters actually get rewarded
Staking::<T>::set_slash_reward_fraction(Perbill::one());

let (mut offenders, raw_offenders) = make_offenders::<T>(1, n)?;
let mut offenders = make_offenders::<T>(1, n)?;
let validator_set_count = Session::<T>::validators().len() as u32;

let offence = BabeEquivocationOffence {
Expand All @@ -238,23 +263,17 @@ benchmarks! {
offender: T::convert(offenders.pop().unwrap()),
};
assert_eq!(System::<T>::event_count(), 0);
}: {
let _ = Offences::<T>::report_offence(reporters, offence);
}
verify {

#[block]
{
let _ = Offences::<T>::report_offence(reporters, offence);
}
#[cfg(test)]
{
// make sure that all slashes have been applied
// (n nominators + one validator) * (slashed + unlocked) + deposit to reporter + reporter
// account endowed + some funds rescinded from issuance.
assert_eq!(System::<T>::read_events_for_pallet::<pallet_balances::Event<T>>().len(), 2 * (n + 1) as usize + 3);
// (n nominators + one validator) * slashed + Slash Reported
assert_eq!(System::<T>::read_events_for_pallet::<pallet_staking::Event<T>>().len(), 1 * (n + 1) as usize + 1);
// offence
assert_eq!(System::<T>::read_events_for_pallet::<pallet_offences::Event>().len(), 1);
// reporter new account
assert_eq!(System::<T>::read_events_for_pallet::<frame_system::Event<T>>().len(), 1);
assert_all_slashes_applied::<T>(n as usize);
}

Ok(())
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test);
Expand Down
5 changes: 3 additions & 2 deletions substrate/frame/offences/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use frame_system as system;
use pallet_session::historical as pallet_session_historical;
use sp_runtime::{
testing::{Header, UintAuthorityId},
BuildStorage, Perbill,
BuildStorage, KeyTypeId, Perbill,
};

type AccountId = u64;
Expand Down Expand Up @@ -66,7 +66,8 @@ sp_runtime::impl_opaque_keys! {

pub struct TestSessionHandler;
impl pallet_session::SessionHandler<AccountId> for TestSessionHandler {
const KEY_TYPE_IDS: &'static [sp_runtime::KeyTypeId] = &[];
// corresponds to the opaque key id above
const KEY_TYPE_IDS: &'static [KeyTypeId] = &[KeyTypeId([100u8, 117u8, 109u8, 121u8])];

fn on_genesis_session<Ks: sp_runtime::traits::OpaqueKeys>(_validators: &[(AccountId, Ks)]) {}

Expand Down

0 comments on commit 2fa07bc

Please sign in to comment.