Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating polkadot-runtime-common slots benchmarking to v2 #6614

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
121 changes: 76 additions & 45 deletions polkadot/runtime/common/src/slots/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub mod pallet {
if let Some((lease_period, first_block)) = Self::lease_period_index(n) {
// If we're beginning a new lease period then handle that.
if first_block {
return Self::manage_lease_period_start(lease_period)
return Self::manage_lease_period_start(lease_period);
}
}

Expand Down Expand Up @@ -237,7 +237,7 @@ impl<T: Config> Pallet<T> {
let mut parachains = Vec::new();
for (para, mut lease_periods) in Leases::<T>::iter() {
if lease_periods.is_empty() {
continue
continue;
}
// ^^ should never be empty since we would have deleted the entry otherwise.

Expand Down Expand Up @@ -381,7 +381,7 @@ impl<T: Config> Leaser<BlockNumberFor<T>> for Pallet<T> {
// attempt.
//
// We bail, not giving any lease and leave it for governance to sort out.
return Err(LeaseError::AlreadyLeased)
return Err(LeaseError::AlreadyLeased);
}
} else if d.len() == i {
// Doesn't exist. This is usual.
Expand Down Expand Up @@ -488,7 +488,7 @@ impl<T: Config> Leaser<BlockNumberFor<T>> for Pallet<T> {
for slot in offset..=offset + period_count {
if let Some(Some(_)) = leases.get(slot) {
// If there exists any lease period, we exit early and return true.
return true
return true;
}
}

Expand Down Expand Up @@ -973,7 +973,7 @@ mod benchmarking {
use polkadot_runtime_parachains::paras;
use sp_runtime::traits::{Bounded, One};

use frame_benchmarking::{account, benchmarks, whitelisted_caller, BenchmarkError};
use frame_benchmarking::v2::*;

use crate::slots::Pallet as Slots;

Expand Down Expand Up @@ -1009,10 +1009,15 @@ mod benchmarking {
(para, leaser)
}

benchmarks! {
where_clause { where T: paras::Config }
#[benchmarks(
where T: paras::Config,
)]

force_lease {
mod benchmarks {
use super::*;

#[benchmark]
fn force_lease() -> Result<(), BenchmarkError> {
// If there is an offset, we need to be on that block to be able to do lease things.
frame_system::Pallet::<T>::set_block_number(T::LeaseOffset::get() + One::one());
let para = ParaId::from(1337);
Expand All @@ -1023,33 +1028,40 @@ mod benchmarking {
let period_count = 3u32.into();
let origin =
T::ForceOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
}: _<T::RuntimeOrigin>(origin, para, leaser.clone(), amount, period_begin, period_count)
verify {
assert_last_event::<T>(Event::<T>::Leased {
para_id: para,
leaser, period_begin,
period_count,
extra_reserved: amount,
total_amount: amount,
}.into());
}

// Worst case scenario, T on-demand parachains onboard, and C lease holding parachains offboard.
manage_lease_period_start {
// Assume reasonable maximum of 100 paras at any time
let c in 0 .. 100;
let t in 0 .. 100;
#[extrinsic_call]
_(origin as T::RuntimeOrigin, para, leaser.clone(), amount, period_begin, period_count);

assert_last_event::<T>(
Event::<T>::Leased {
para_id: para,
leaser,
period_begin,
period_count,
extra_reserved: amount,
total_amount: amount,
}
.into(),
);

Ok(())
}

// Worst case scenario, T on-demand parachains onboard, and C lease holding parachains
// offboard. Assume reasonable maximum of 100 paras at any time
#[benchmark]
fn manage_lease_period_start(
c: Linear<0, 100>,
t: Linear<0, 100>,
) -> Result<(), BenchmarkError> {
let period_begin = 1u32.into();
let period_count = 4u32.into();

// If there is an offset, we need to be on that block to be able to do lease things.
frame_system::Pallet::<T>::set_block_number(T::LeaseOffset::get() + One::one());

// Make T parathreads (on-demand parachains)
let paras_info = (0..t).map(|i| {
register_a_parathread::<T>(i)
}).collect::<Vec<_>>();
let paras_info = (0..t).map(|i| register_a_parathread::<T>(i)).collect::<Vec<_>>();

T::Registrar::execute_pending_transitions();

Expand All @@ -1064,43 +1076,48 @@ mod benchmarking {
T::Registrar::execute_pending_transitions();

// C lease holding parachains are downgrading to on-demand parachains
for i in 200 .. 200 + c {
let (para, leaser) = register_a_parathread::<T>(i);
for i in 200..200 + c {
let (para, _) = register_a_parathread::<T>(i);
T::Registrar::make_parachain(para)?;
}

T::Registrar::execute_pending_transitions();

for i in 0 .. t {
for i in 0..t {
assert!(T::Registrar::is_parathread(ParaId::from(i)));
}

for i in 200 .. 200 + c {
for i in 200..200 + c {
assert!(T::Registrar::is_parachain(ParaId::from(i)));
}
}: {
Slots::<T>::manage_lease_period_start(period_begin);
} verify {
#[block]
{
let _ = Slots::<T>::manage_lease_period_start(period_begin);
}

// All paras should have switched.
T::Registrar::execute_pending_transitions();
for i in 0 .. t {
for i in 0..t {
assert!(T::Registrar::is_parachain(ParaId::from(i)));
}
for i in 200 .. 200 + c {
for i in 200..200 + c {
assert!(T::Registrar::is_parathread(ParaId::from(i)));
}

Ok(())
}

// Assume that at most 8 people have deposits for leases on a parachain.
// This would cover at least 4 years of leases in the worst case scenario.
clear_all_leases {
#[benchmark]
fn clear_all_leases() -> Result<(), BenchmarkError> {
let max_people = 8;
let (para, _) = register_a_parathread::<T>(1);

// If there is an offset, we need to be on that block to be able to do lease things.
frame_system::Pallet::<T>::set_block_number(T::LeaseOffset::get() + One::one());

for i in 0 .. max_people {
for i in 0..max_people {
let leaser = account("lease_deposit", i, 0);
let amount = T::Currency::minimum_balance();
T::Currency::make_free_balance_be(&leaser, BalanceOf::<T>::max_value());
Expand All @@ -1113,31 +1130,45 @@ mod benchmarking {
Slots::<T>::force_lease(origin, para, leaser, amount, period_begin, period_count)?;
}

for i in 0 .. max_people {
for i in 0..max_people {
let leaser = account("lease_deposit", i, 0);
assert_eq!(T::Currency::reserved_balance(&leaser), T::Currency::minimum_balance());
}

let origin =
T::ForceOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
}: _<T::RuntimeOrigin>(origin, para)
verify {
for i in 0 .. max_people {

#[extrinsic_call]
_(origin as T::RuntimeOrigin, para);

for i in 0..max_people {
let leaser = account("lease_deposit", i, 0);
assert_eq!(T::Currency::reserved_balance(&leaser), 0u32.into());
}

Ok(())
}

trigger_onboard {
#[benchmark]
fn trigger_onboard() -> Result<(), BenchmarkError> {
// get a parachain into a bad state where they did not onboard
let (para, _) = register_a_parathread::<T>(1);
Leases::<T>::insert(para, vec![Some((account::<T::AccountId>("lease_insert", 0, 0), BalanceOf::<T>::default()))]);
Leases::<T>::insert(
para,
vec![Some((
account::<T::AccountId>("lease_insert", 0, 0),
BalanceOf::<T>::default(),
))],
);
assert!(T::Registrar::is_parathread(para));
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), para)
verify {

#[extrinsic_call]
_(RawOrigin::Signed(caller), para);

T::Registrar::execute_pending_transitions();
assert!(T::Registrar::is_parachain(para));
Ok(())
}

impl_benchmark_test_suite!(
Expand Down
Loading