Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
intent_leave_modified
  • Loading branch information
NachoPal committed Oct 3, 2021
commit af48adcf282e57a0e30e88670357b3dfbcb129d4
35 changes: 11 additions & 24 deletions pallets/collator-selection/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ use super::*;
#[allow(unused)]
use crate::Pallet as CollatorSelection;
use sp_std::prelude::*;
use sp_runtime::traits::Convert;
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller, account};
use frame_system::{RawOrigin, EventRecord};
use frame_support::{
assert_ok,
assert_noop,
traits::{Currency, Get, EnsureOrigin},
codec::Decode
};
use pallet_authorship::EventHandler;
use pallet_session::{SessionManager, Pallet as Session};
use pallet_session::{self as session, SessionManager};

pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down Expand Up @@ -65,7 +63,7 @@ pub fn create_funded_user<T: Config>(
user
}

fn keys<T: Config>(c: u32) -> <T as pallet_session::Config>::Keys {
fn keys<T: Config + session::Config>(c: u32) -> <T as session::Config>::Keys {
use rand::{RngCore, SeedableRng};

let keys = {
Expand All @@ -82,15 +80,15 @@ fn keys<T: Config>(c: u32) -> <T as pallet_session::Config>::Keys {
Decode::decode(&mut &keys[..]).unwrap()
}

fn validator<T: Config>(c: u32)-> (T::AccountId, <T as pallet_session::Config>::Keys) {
fn validator<T: Config + session::Config>(c: u32)-> (T::AccountId, <T as session::Config>::Keys) {
(create_funded_user::<T>("candidate", c, 1000), keys::<T>(c))
}

fn register_validators<T: Config>(count: u32) {
fn register_validators<T: Config + session::Config>(count: u32) {
let validators = (0..count).map(|c| validator::<T>(c)).collect::<Vec<_>>();

for (who, keys) in validators {
<pallet_session::Module<T>>::set_keys(
<session::Module<T>>::set_keys(
RawOrigin::Signed(who).into(), keys, vec![]
).unwrap();
}
Expand All @@ -107,7 +105,7 @@ fn register_candidates<T: Config>(count: u32) {
}

benchmarks! {
where_clause { where T: pallet_authorship::Config }
where_clause { where T: pallet_authorship::Config + session::Config }

set_invulnerables {
let b in 1 .. T::MaxInvulnerables::get();
Expand Down Expand Up @@ -161,9 +159,9 @@ benchmarks! {
let bond: BalanceOf<T> = T::Currency::minimum_balance() * 2u32.into();
T::Currency::make_free_balance_be(&caller, bond.clone());

<pallet_session::Module<T>>::set_keys(
<session::Module<T>>::set_keys(
RawOrigin::Signed(caller.clone()).into(),
keys::<T>(c+1),
keys::<T>(c + 1),
vec![]
).unwrap();

Expand All @@ -174,7 +172,7 @@ benchmarks! {

// worse case is the last candidate leaving.
leave_intent {
let c in 1 .. T::MaxCandidates::get();
let c in (T::MinCandidates::get() + 1) .. T::MaxCandidates::get();
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<DesiredCandidates<T>>::put(c);

Expand All @@ -183,20 +181,9 @@ benchmarks! {

let leaving = <Candidates<T>>::get().last().unwrap().who.clone();
whitelist!(leaving);
}: {
if c <= T::MinCandidates::get() {
assert_noop!(
<CollatorSelection<T>>::leave_intent(RawOrigin::Signed(leaving.clone()).into()),
Error::<T>::TooFewCandidates
);
} else {
<CollatorSelection<T>>::leave_intent(RawOrigin::Signed(leaving.clone()).into());
}
}
}: _(RawOrigin::Signed(leaving.clone()))
verify {
if c > T::MinCandidates::get() {
assert_last_event::<T>(Event::CandidateRemoved(leaving).into());
}
assert_last_event::<T>(Event::CandidateRemoved(leaving).into());
}

// worse case is paying a non-existing candidate account.
Expand Down
29 changes: 13 additions & 16 deletions pallets/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ pub mod pallet {
},
PalletId,
};
use frame_system::{self as system, pallet_prelude::*};
use frame_system::{pallet_prelude::*};
use frame_system::Config as SystemConfig;
use frame_support::{
sp_runtime::{
RuntimeDebug,
traits::{AccountIdConversion, CheckedSub, Zero, Saturating, OpaqueKeys},
traits::{AccountIdConversion, CheckedSub, Zero, Saturating},
},
weights::DispatchClass,
};
Expand All @@ -112,7 +112,7 @@ pub mod pallet {

/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config + pallet_session::Config {
pub trait Config: frame_system::Config {
/// Overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;

Expand Down Expand Up @@ -157,9 +157,6 @@ pub mod pallet {

/// The weight information of this pallet.
type WeightInfo: WeightInfo;

// /// Session Keys.
// type Keys: OpaqueKeys + Member + Parameter + Default;
}

/// Basic information about a collation candidate.
Expand Down Expand Up @@ -286,7 +283,7 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(<T as self::Config>::WeightInfo::set_invulnerables(new.len() as u32))]
#[pallet::weight(T::WeightInfo::set_invulnerables(new.len() as u32))]
pub fn set_invulnerables(
origin: OriginFor<T>,
new: Vec<T::AccountId>,
Expand All @@ -303,7 +300,7 @@ pub mod pallet {
Ok(().into())
}

#[pallet::weight(<T as self::Config>::WeightInfo::set_desired_candidates())]
#[pallet::weight(T::WeightInfo::set_desired_candidates())]
pub fn set_desired_candidates(origin: OriginFor<T>, max: u32) -> DispatchResultWithPostInfo {
T::UpdateOrigin::ensure_origin(origin)?;
// we trust origin calls, this is just a for more accurate benchmarking
Expand All @@ -317,15 +314,15 @@ pub mod pallet {
Ok(().into())
}

#[pallet::weight(<T as self::Config>::WeightInfo::set_candidacy_bond())]
#[pallet::weight(T::WeightInfo::set_candidacy_bond())]
pub fn set_candidacy_bond(origin: OriginFor<T>, bond: BalanceOf<T>) -> DispatchResultWithPostInfo {
T::UpdateOrigin::ensure_origin(origin)?;
<CandidacyBond<T>>::put(&bond);
Self::deposit_event(Event::NewCandidacyBond(bond));
Ok(().into())
}

#[pallet::weight(<T as self::Config>::WeightInfo::register_as_candidate(T::MaxCandidates::get()))]
#[pallet::weight(T::WeightInfo::register_as_candidate(T::MaxCandidates::get()))]
pub fn register_as_candidate(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

Expand All @@ -334,7 +331,7 @@ pub mod pallet {
ensure!((length as u32) < Self::desired_candidates(), Error::<T>::TooManyCandidates);
ensure!(!Self::invulnerables().contains(&who), Error::<T>::AlreadyInvulnerable);

let validator_key = <T as self::Config>::ValidatorIdOf::convert(who.clone()).ok_or(Error::<T>::NoAssociatedValidatorId)?;
let validator_key = T::ValidatorIdOf::convert(who.clone()).ok_or(Error::<T>::NoAssociatedValidatorId)?;
ensure!(T::ValidatorRegistration::is_registered(&validator_key), Error::<T>::ValidatorNotRegistered);

let deposit = Self::candidacy_bond();
Expand All @@ -354,16 +351,16 @@ pub mod pallet {
})?;

Self::deposit_event(Event::CandidateAdded(who, deposit));
Ok(Some(<T as self::Config>::WeightInfo::register_as_candidate(current_count as u32)).into())
Ok(Some(T::WeightInfo::register_as_candidate(current_count as u32)).into())
}

#[pallet::weight(<T as self::Config>::WeightInfo::leave_intent(T::MaxCandidates::get()))]
#[pallet::weight(T::WeightInfo::leave_intent(T::MaxCandidates::get()))]
pub fn leave_intent(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
ensure!(Self::candidates().len() as u32 > T::MinCandidates::get(), Error::<T>::TooFewCandidates);
let current_count = Self::try_remove_candidate(&who)?;

Ok(Some(<T as self::Config>::WeightInfo::leave_intent(current_count as u32)).into())
Ok(Some(T::WeightInfo::leave_intent(current_count as u32)).into())
}
}

Expand Down Expand Up @@ -432,7 +429,7 @@ pub mod pallet {
<LastAuthoredBlock<T>>::insert(author, frame_system::Pallet::<T>::block_number());

frame_system::Pallet::<T>::register_extra_weight_unchecked(
<T as self::Config>::WeightInfo::note_author(),
T::WeightInfo::note_author(),
DispatchClass::Mandatory,
);
}
Expand All @@ -459,7 +456,7 @@ pub mod pallet {
let removed = candidates_len_before - active_candidates_len;

frame_system::Pallet::<T>::register_extra_weight_unchecked(
<T as self::Config>::WeightInfo::new_session(candidates_len_before as u32, removed as u32),
T::WeightInfo::new_session(candidates_len_before as u32, removed as u32),
DispatchClass::Mandatory,
);
Some(result)
Expand Down
1 change: 0 additions & 1 deletion polkadot-parachains/statemine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,6 @@ impl pallet_collator_selection::Config for Runtime {
type ValidatorIdOf = pallet_collator_selection::IdentityCollator;
type ValidatorRegistration = Session;
type WeightInfo = weights::pallet_collator_selection::WeightInfo<Runtime>;
// type Keys = opaque::SessionKeys;
}

// Create the runtime by composing the FRAME pallets that were previously configured.
Expand Down
2 changes: 1 addition & 1 deletion scripts/benchmarks.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash

steps=50
repeat=20
repeat=1
statemineOutput=./polkadot-parachains/statemine/src/weights
statemintOutput=./polkadot-parachains/statemint/src/weights
statemineChain=statemine-dev
Expand Down