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 all commits
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
662 changes: 502 additions & 160 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"client/pov-recovery",
"client/service",
"pallets/aura-ext",
"pallets/collator-selection",
"pallets/dmp-queue",
"pallets/parachain-system",
"pallets/xcm",
Expand All @@ -20,6 +21,10 @@ members = [
"polkadot-parachains/pallets/ping",
"polkadot-parachains/rococo-runtime",
"polkadot-parachains/shell-runtime",
"polkadot-parachains/statemint-common",
"polkadot-parachains/statemint-runtime",
"polkadot-parachains/statemine-runtime",
"polkadot-parachains/westmint-runtime",
"test/runtime",
"test/runtime-upgrade",
"test/client",
Expand Down
57 changes: 57 additions & 0 deletions pallets/collator-selection/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[package]
authors = ['Anonymous']
description = 'Simple staking pallet with a fixed stake.'
edition = '2018'
homepage = 'https://substrate.dev'
license = 'Apache-2.0'
name = 'pallet-collator-selection'
readme = 'README.md'
repository = 'https://github.com/paritytech/cumulus/'
version = '3.0.0'

[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']

[dependencies]
log = { version = "0.4.0", default-features = false }
codec = { default-features = false, features = ['derive'], package = 'parity-scale-codec', version = '2.0.0' }
serde = { version = "1.0.119", default-features = false }
sp-std = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
sp-runtime = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
sp-staking = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
frame-support = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
frame-system = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
pallet-authorship = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
pallet-session = { default-features = false, git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }

frame-benchmarking = { default-features = false, optional = true, git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }

[dev-dependencies]
sp-core = { git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
sp-io = { git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
sp-tracing = { git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
sp-runtime = { git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
pallet-timestamp = { git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
sp-consensus-aura = { git = 'https://github.com/paritytech/substrate', branch = "master", version = '0.9.0' }
pallet-balances = { git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }
pallet-aura = { git = 'https://github.com/paritytech/substrate', branch = "master", version = '3.0.0' }

[features]
default = ['std']
runtime-benchmarks = [
'frame-benchmarking',
'frame-support/runtime-benchmarks',
'frame-system/runtime-benchmarks',
]
std = [
'codec/std',
'log/std',
'sp-runtime/std',
'sp-staking/std',
'sp-std/std',
'frame-support/std',
'frame-system/std',
'frame-benchmarking/std',
'pallet-authorship/std',
'pallet-session/std',
]
1 change: 1 addition & 0 deletions pallets/collator-selection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
License: Apache-2.0
190 changes: 190 additions & 0 deletions pallets/collator-selection/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// Copyright (C) 2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Benchmarking setup for pallet-collator-selection

use super::*;

#[allow(unused)]
use crate::Pallet as CollatorSelection;
use sp_std::prelude::*;
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller, account};
use frame_system::{RawOrigin, EventRecord};
use frame_support::{
assert_ok,
traits::{Currency, Get, EnsureOrigin},
};
use pallet_authorship::EventHandler;
use pallet_session::SessionManager;

pub type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

const SEED: u32 = 0;

// TODO: remove if this is given in substrate commit.
macro_rules! whitelist {
($acc:ident) => {
frame_benchmarking::benchmarking::add_to_whitelist(
frame_system::Account::<T>::hashed_key_for(&$acc).into()
);
};
}

fn assert_last_event<T: Config>(generic_event: <T as Config>::Event) {
let events = frame_system::Pallet::<T>::events();
let system_event: <T as frame_system::Config>::Event = generic_event.into();
// compare to the last event record
let EventRecord { event, .. } = &events[events.len() - 1];
assert_eq!(event, &system_event);
}

fn register_candidates<T: Config>(count: u32) {
let candidates = (0..count).map(|c| account("candidate", c, SEED)).collect::<Vec<_>>();
assert!(<CandidacyBond<T>>::get() > 0u32.into(), "Bond cannot be zero!");
for who in candidates {
T::Currency::make_free_balance_be(&who, <CandidacyBond<T>>::get() * 2u32.into());
<CollatorSelection<T>>::register_as_candidate(RawOrigin::Signed(who).into()).unwrap();
}
}

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

set_invulnerables {
let b in 1 .. T::MaxInvulnerables::get();
let new_invulnerables = (0..b).map(|c| account("candidate", c, SEED)).collect::<Vec<_>>();
let origin = T::UpdateOrigin::successful_origin();
}: {
assert_ok!(
<CollatorSelection<T>>::set_invulnerables(origin, new_invulnerables.clone())
);
}
verify {
assert_last_event::<T>(Event::NewInvulnerables(new_invulnerables).into());
}

set_desired_candidates {
let max: u32 = 999;
let origin = T::UpdateOrigin::successful_origin();
}: {
assert_ok!(
<CollatorSelection<T>>::set_desired_candidates(origin, max.clone())
);
}
verify {
assert_last_event::<T>(Event::NewDesiredCandidates(max).into());
}

set_candidacy_bond {
let bond: BalanceOf<T> = T::Currency::minimum_balance() * 10u32.into();
let origin = T::UpdateOrigin::successful_origin();
}: {
assert_ok!(
<CollatorSelection<T>>::set_candidacy_bond(origin, bond.clone())
);
}
verify {
assert_last_event::<T>(Event::NewCandidacyBond(bond).into());
}

// worse case is when we have all the max-candidate slots filled except one, and we fill that
// one.
register_as_candidate {
let c in 1 .. T::MaxCandidates::get();

<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<DesiredCandidates<T>>::put(c + 1);
register_candidates::<T>(c);

let caller: T::AccountId = whitelisted_caller();
let bond: BalanceOf<T> = T::Currency::minimum_balance() * 2u32.into();
T::Currency::make_free_balance_be(&caller, bond.clone());

}: _(RawOrigin::Signed(caller.clone()))
verify {
assert_last_event::<T>(Event::CandidateAdded(caller, bond / 2u32.into()).into());
}

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

let leaving = <Candidates<T>>::get().last().unwrap().who.clone();
whitelist!(leaving);
}: _(RawOrigin::Signed(leaving.clone()))
verify {
assert_last_event::<T>(Event::CandidateRemoved(leaving).into());
}

// worse case is paying a non-existing candidate account.
note_author {
<CandidacyBond<T>>::put(T::Currency::minimum_balance());
T::Currency::make_free_balance_be(
&<CollatorSelection<T>>::account_id(),
T::Currency::minimum_balance() * 4u32.into(),
);
let author = account("author", 0, SEED);
let new_block: T::BlockNumber = 10u32.into();

frame_system::Pallet::<T>::set_block_number(new_block);
assert!(T::Currency::free_balance(&author) == 0u32.into());
}: {
<CollatorSelection<T> as EventHandler<_, _>>::note_author(author.clone())
} verify {
assert!(T::Currency::free_balance(&author) > 0u32.into());
assert_eq!(frame_system::Pallet::<T>::block_number(), new_block);
}

// worse case is on new session.
// TODO review this benchmark
new_session {
let r in 1 .. T::MaxCandidates::get();
let c in 1 .. T::MaxCandidates::get();

<CandidacyBond<T>>::put(T::Currency::minimum_balance());
<DesiredCandidates<T>>::put(c);
frame_system::Pallet::<T>::set_block_number(0u32.into());
register_candidates::<T>(c);

let new_block: T::BlockNumber = 1800u32.into();
let zero_block: T::BlockNumber = 0u32.into();
let candidates = <Candidates<T>>::get();

let non_removals = c.saturating_sub(r);

for i in 0..c {
<LastAuthoredBlock<T>>::insert(candidates[i as usize].who.clone(), zero_block);
}
for i in 0..non_removals {
<LastAuthoredBlock<T>>::insert(candidates[i as usize].who.clone(), new_block);
}

let pre_length = <Candidates<T>>::get().len();
frame_system::Pallet::<T>::set_block_number(new_block);

assert!(<Candidates<T>>::get().len() == c as usize);

}: {
<CollatorSelection<T> as SessionManager<_>>::new_session(0)
} verify {
assert!(<Candidates<T>>::get().len() < pre_length);
}
}

impl_benchmark_test_suite!(CollatorSelection, crate::mock::new_test_ext(), crate::mock::Test,);
Loading