Skip to content

Commit

Permalink
[polkadot-runtime-parachains] migrate disputes and disputes/slashing …
Browse files Browse the repository at this point in the history
…to benchmarking to bench v2 syntax (#6577)

[polkadot-runtime-parachains] migrate disputes and disputes/slashing to
benchmarking to bench v2 syntax

Part of:
* #6202

---------

Co-authored-by: Giuseppe Re <[email protected]>
Co-authored-by: Bastian Köcher <[email protected]>
  • Loading branch information
3 people authored Dec 19, 2024
1 parent 2cbb437 commit ade1f75
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 28 deletions.
16 changes: 11 additions & 5 deletions polkadot/runtime/parachains/src/disputes/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@

use super::*;

use frame_benchmarking::benchmarks;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;
use sp_runtime::traits::One;

benchmarks! {
force_unfreeze {
#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn force_unfreeze() {
Frozen::<T>::set(Some(One::one()));
}: _(RawOrigin::Root)
verify {

#[extrinsic_call]
_(RawOrigin::Root);

assert!(Frozen::<T>::get().is_none())
}

Expand Down
6 changes: 3 additions & 3 deletions polkadot/runtime/parachains/src/disputes/slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,12 @@ impl<T: Config> HandleReports<T> for () {
}

pub trait WeightInfo {
fn report_dispute_lost(validator_count: ValidatorSetCount) -> Weight;
fn report_dispute_lost_unsigned(validator_count: ValidatorSetCount) -> Weight;
}

pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
fn report_dispute_lost(_validator_count: ValidatorSetCount) -> Weight {
fn report_dispute_lost_unsigned(_validator_count: ValidatorSetCount) -> Weight {
Weight::zero()
}
}
Expand Down Expand Up @@ -445,7 +445,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(<T as Config>::WeightInfo::report_dispute_lost(
#[pallet::weight(<T as Config>::WeightInfo::report_dispute_lost_unsigned(
key_owner_proof.validator_count()
))]
pub fn report_dispute_lost_unsigned(
Expand Down
38 changes: 19 additions & 19 deletions polkadot/runtime/parachains/src/disputes/slashing/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use super::*;

use crate::{disputes::SlashingHandler, initializer, shared};
use codec::Decode;
use frame_benchmarking::{benchmarks, whitelist_account};
use frame_benchmarking::v2::*;
use frame_support::traits::{OnFinalize, OnInitialize};
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
use pallet_staking::testing_utils::create_validators;
Expand All @@ -29,6 +29,11 @@ use sp_session::MembershipProof;
// Candidate hash of the disputed candidate.
const CANDIDATE_HASH: CandidateHash = CandidateHash(Hash::zero());

// Simplify getting the value in the benchmark
pub const fn max_validators_for<T: super::Config>() -> u32 {
<<T>::BenchmarkingConfig as BenchmarkingConfiguration>::MAX_VALIDATORS
}

pub trait Config:
pallet_session::Config
+ pallet_session::historical::Config
Expand Down Expand Up @@ -106,6 +111,7 @@ where
(session_index, key_owner_proof, validator_id)
}

/// Submits a single `ForInvalid` dispute.
fn setup_dispute<T>(session_index: SessionIndex, validator_id: ValidatorId) -> DisputeProof
where
T: Config,
Expand All @@ -125,6 +131,7 @@ where
dispute_proof(session_index, validator_id, validator_index)
}

/// Creates a `ForInvalid` dispute proof.
fn dispute_proof(
session_index: SessionIndex,
validator_id: ValidatorId,
Expand All @@ -136,27 +143,20 @@ fn dispute_proof(
DisputeProof { time_slot, kind, validator_index, validator_id }
}

benchmarks! {
where_clause {
where T: Config<KeyOwnerProof = MembershipProof>,
}

// in this setup we have a single `ForInvalid` dispute
// submitted for a past session
report_dispute_lost {
let n in 4..<<T as super::Config>::BenchmarkingConfig as BenchmarkingConfiguration>::MAX_VALIDATORS;
#[benchmarks(where T: Config<KeyOwnerProof = MembershipProof>)]
mod benchmarks {
use super::*;

let origin = RawOrigin::None.into();
#[benchmark]
fn report_dispute_lost_unsigned(n: Linear<4, { max_validators_for::<T>() }>) {
let (session_index, key_owner_proof, validator_id) = setup_validator_set::<T>(n);

// submit a single `ForInvalid` dispute for a past session.
let dispute_proof = setup_dispute::<T>(session_index, validator_id);
}: {
let result = Pallet::<T>::report_dispute_lost_unsigned(
origin,
Box::new(dispute_proof),
key_owner_proof,
);
assert!(result.is_ok());
} verify {

#[extrinsic_call]
_(RawOrigin::None, Box::new(dispute_proof), key_owner_proof);

let unapplied = <UnappliedSlashes<T>>::get(session_index, CANDIDATE_HASH);
assert!(unapplied.is_none());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<T: frame_system::Config> polkadot_runtime_parachains::disputes::slashing::W
/// Storage: Staking UnappliedSlashes (r:1 w:1)
/// Proof Skipped: Staking UnappliedSlashes (max_values: None, max_size: None, mode: Measured)
/// The range of component `n` is `[4, 300]`.
fn report_dispute_lost(n: u32, ) -> Weight {
fn report_dispute_lost_unsigned(n: u32, ) -> Weight {
// Proof Size summary in bytes:
// Measured: `4531 + n * (189 ±0)`
// Estimated: `7843 + n * (192 ±0)`
Expand Down

0 comments on commit ade1f75

Please sign in to comment.