mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-17 21:51:06 +00:00
Change the Config of the MaxRococoNum Slot from a Constant to a Storage function (#7217)
* set MaxPermanentSlots and MaxTemporarySlots with a extrinsic instead of a constant * delete the MaxPermanentSlots and MaxTemporarySlots constants from config on Rococo and Westend * migration code for assigned slots * remove getters * little refactor * set values in the GenesisConfig * refactor in the migration, adding it in the rococo runtime * refactor: fmt * Minor fix * pre_upgrade check * add migration to mod v1 * Logs following Substrate#12873 * fix: current storage version set to 1 * use enact when try-runtime * Vec seems to be missing * feature gate import * fix as per #13993 * address comments Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * address comments Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * benchmarking for assign_perm_parachain_slot extrinsic * benchmark all the extrinsics of the pallet * cargo fmt for assigned slots * migration added for westend * licence in benchmarking file * BuildGenesisConfig * assigned_slots default in genesis * cargo fmt * assigned_slots fix tests config * cargo fmt * fix benchmarking compile error * fix benchmarking imports * benchmark worst case scenario for validation code and head data * add assigned_slots in frame_benchmarking on Rococo and Westend * modify values for para_id in benchmarking * delete the assigned_slots in westend frame_benchmarking * fix benchmarkings and add it to westend * cargo fmt * ".git/.scripts/commands/bench/bench.sh" --subcommand=runtime --runtime=rococo --target_dir=polkadot --pallet=runtime_common::assigned_slots * ".git/.scripts/commands/bench/bench.sh" --subcommand=runtime --runtime=westend --target_dir=polkadot --pallet=runtime_common::assigned_slots * use generated weights in assigned_slots pallet * small changes in set_max_permanent_slots and set_max_temporary_slots * revert last commit * address some comments * wrap migration with VersionCheckedMigrateToV1 * add experimental feature in pallet, and assers in post_upgrade migration * clean warnings * clean unnecesary experimental flag * small typo in comments * cargo fmt * small comments fixes --------- Co-authored-by: al3mart <11448715+al3mart@users.noreply.github.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: command-bot <>
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Benchmarking for assigned_slots pallet
|
||||
|
||||
#![cfg(feature = "runtime-benchmarks")]
|
||||
use super::*;
|
||||
|
||||
use frame_benchmarking::v2::*;
|
||||
use frame_support::assert_ok;
|
||||
use frame_system::{pallet_prelude::BlockNumberFor, RawOrigin};
|
||||
use primitives::Id as ParaId;
|
||||
use sp_runtime::traits::Bounded;
|
||||
|
||||
type CurrencyOf<T> = <<T as Config>::Leaser as Leaser<BlockNumberFor<T>>>::Currency;
|
||||
type BalanceOf<T> = <<<T as Config>::Leaser as Leaser<BlockNumberFor<T>>>::Currency as Currency<
|
||||
<T as frame_system::Config>::AccountId,
|
||||
>>::Balance;
|
||||
#[benchmarks(where T: Config)]
|
||||
mod benchmarks {
|
||||
use super::*;
|
||||
|
||||
use crate::assigned_slots::Pallet as AssignedSlots;
|
||||
|
||||
fn register_parachain<T: Config>(para_id: ParaId) {
|
||||
let who: T::AccountId = whitelisted_caller();
|
||||
let worst_validation_code = T::Registrar::worst_validation_code();
|
||||
let worst_head_data = T::Registrar::worst_head_data();
|
||||
|
||||
CurrencyOf::<T>::make_free_balance_be(&who, BalanceOf::<T>::max_value());
|
||||
|
||||
assert_ok!(T::Registrar::register(
|
||||
who,
|
||||
para_id,
|
||||
worst_head_data,
|
||||
worst_validation_code.clone()
|
||||
));
|
||||
assert_ok!(paras::Pallet::<T>::add_trusted_validation_code(
|
||||
frame_system::Origin::<T>::Root.into(),
|
||||
worst_validation_code,
|
||||
));
|
||||
T::Registrar::execute_pending_transitions();
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn assign_perm_parachain_slot() {
|
||||
let para_id = ParaId::from(1_u32);
|
||||
let caller = RawOrigin::Root;
|
||||
|
||||
let _ =
|
||||
AssignedSlots::<T>::set_max_permanent_slots(frame_system::Origin::<T>::Root.into(), 10);
|
||||
register_parachain::<T>(para_id);
|
||||
|
||||
let counter = PermanentSlotCount::<T>::get();
|
||||
let current_lease_period: BlockNumberFor<T> =
|
||||
T::Leaser::lease_period_index(frame_system::Pallet::<T>::block_number())
|
||||
.and_then(|x| Some(x.0))
|
||||
.unwrap();
|
||||
#[extrinsic_call]
|
||||
assign_perm_parachain_slot(caller, para_id);
|
||||
|
||||
assert_eq!(
|
||||
PermanentSlots::<T>::get(para_id),
|
||||
Some((
|
||||
current_lease_period,
|
||||
LeasePeriodOf::<T>::from(T::PermanentSlotLeasePeriodLength::get()),
|
||||
))
|
||||
);
|
||||
assert_eq!(PermanentSlotCount::<T>::get(), counter + 1);
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn assign_temp_parachain_slot() {
|
||||
let para_id = ParaId::from(2_u32);
|
||||
let caller = RawOrigin::Root;
|
||||
|
||||
let _ =
|
||||
AssignedSlots::<T>::set_max_temporary_slots(frame_system::Origin::<T>::Root.into(), 10);
|
||||
register_parachain::<T>(para_id);
|
||||
|
||||
let current_lease_period: BlockNumberFor<T> =
|
||||
T::Leaser::lease_period_index(frame_system::Pallet::<T>::block_number())
|
||||
.and_then(|x| Some(x.0))
|
||||
.unwrap();
|
||||
|
||||
let counter = TemporarySlotCount::<T>::get();
|
||||
#[extrinsic_call]
|
||||
assign_temp_parachain_slot(caller, para_id, SlotLeasePeriodStart::Current);
|
||||
|
||||
let tmp = ParachainTemporarySlot {
|
||||
manager: whitelisted_caller(),
|
||||
period_begin: current_lease_period,
|
||||
period_count: LeasePeriodOf::<T>::from(T::TemporarySlotLeasePeriodLength::get()),
|
||||
last_lease: Some(BlockNumberFor::<T>::zero()),
|
||||
lease_count: 1,
|
||||
};
|
||||
assert_eq!(TemporarySlots::<T>::get(para_id), Some(tmp));
|
||||
assert_eq!(TemporarySlotCount::<T>::get(), counter + 1);
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn unassign_parachain_slot() {
|
||||
let para_id = ParaId::from(3_u32);
|
||||
let caller = RawOrigin::Root;
|
||||
|
||||
let _ =
|
||||
AssignedSlots::<T>::set_max_temporary_slots(frame_system::Origin::<T>::Root.into(), 10);
|
||||
register_parachain::<T>(para_id);
|
||||
|
||||
let _ = AssignedSlots::<T>::assign_temp_parachain_slot(
|
||||
caller.clone().into(),
|
||||
para_id,
|
||||
SlotLeasePeriodStart::Current,
|
||||
);
|
||||
|
||||
let counter = TemporarySlotCount::<T>::get();
|
||||
#[extrinsic_call]
|
||||
unassign_parachain_slot(caller, para_id);
|
||||
|
||||
assert_eq!(TemporarySlots::<T>::get(para_id), None);
|
||||
assert_eq!(TemporarySlotCount::<T>::get(), counter - 1);
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn set_max_permanent_slots() {
|
||||
let caller = RawOrigin::Root;
|
||||
#[extrinsic_call]
|
||||
set_max_permanent_slots(caller, u32::MAX);
|
||||
|
||||
assert_eq!(MaxPermanentSlots::<T>::get(), u32::MAX);
|
||||
}
|
||||
|
||||
#[benchmark]
|
||||
fn set_max_temporary_slots() {
|
||||
let caller = RawOrigin::Root;
|
||||
#[extrinsic_call]
|
||||
set_max_temporary_slots(caller, u32::MAX);
|
||||
|
||||
assert_eq!(MaxTemporarySlots::<T>::get(), u32::MAX);
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(
|
||||
AssignedSlots,
|
||||
crate::assigned_slots::tests::new_test_ext(),
|
||||
crate::assigned_slots::tests::Test,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// This file is part of Polkadot.
|
||||
|
||||
// Polkadot is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Polkadot is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use super::{Config, MaxPermanentSlots, MaxTemporarySlots, Pallet, LOG_TARGET};
|
||||
use frame_support::{
|
||||
dispatch::GetStorageVersion,
|
||||
traits::{Get, OnRuntimeUpgrade},
|
||||
};
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
use frame_support::ensure;
|
||||
#[cfg(feature = "try-runtime")]
|
||||
use sp_std::vec::Vec;
|
||||
|
||||
pub mod v1 {
|
||||
|
||||
use super::*;
|
||||
pub struct MigrateToV1<T>(sp_std::marker::PhantomData<T>);
|
||||
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
|
||||
let onchain_version = Pallet::<T>::on_chain_storage_version();
|
||||
ensure!(onchain_version < 1, "assigned_slots::MigrateToV1 migration can be deleted");
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
fn on_runtime_upgrade() -> frame_support::weights::Weight {
|
||||
let onchain_version = Pallet::<T>::on_chain_storage_version();
|
||||
if onchain_version < 1 {
|
||||
const MAX_PERMANENT_SLOTS: u32 = 100;
|
||||
const MAX_TEMPORARY_SLOTS: u32 = 100;
|
||||
|
||||
<MaxPermanentSlots<T>>::put(MAX_PERMANENT_SLOTS);
|
||||
<MaxTemporarySlots<T>>::put(MAX_TEMPORARY_SLOTS);
|
||||
// Return the weight consumed by the migration.
|
||||
T::DbWeight::get().reads_writes(1, 3)
|
||||
} else {
|
||||
log::info!(target: LOG_TARGET, "MigrateToV1 should be removed");
|
||||
T::DbWeight::get().reads(1)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "try-runtime")]
|
||||
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
|
||||
let onchain_version = Pallet::<T>::on_chain_storage_version();
|
||||
ensure!(onchain_version == 1, "assigned_slots::MigrateToV1 needs to be run");
|
||||
assert_eq!(<MaxPermanentSlots<T>>::get(), 100);
|
||||
assert_eq!(<MaxTemporarySlots<T>>::get(), 100);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// [`VersionUncheckedMigrateToV1`] wrapped in a
|
||||
/// [`frame_support::migrations::VersionedRuntimeUpgrade`], ensuring the migration is only
|
||||
/// performed when on-chain version is 0.
|
||||
#[cfg(feature = "experimental")]
|
||||
pub type VersionCheckedMigrateToV1<T> = frame_support::migrations::VersionedRuntimeUpgrade<
|
||||
0,
|
||||
1,
|
||||
MigrateToV1<T>,
|
||||
Pallet<T>,
|
||||
<T as frame_system::Config>::DbWeight,
|
||||
>;
|
||||
}
|
||||
+169
-33
@@ -23,10 +23,12 @@
|
||||
//! This pallet should not be used on a production relay chain,
|
||||
//! only on a test relay chain (e.g. Rococo).
|
||||
|
||||
pub mod benchmarking;
|
||||
pub mod migration;
|
||||
|
||||
use crate::{
|
||||
slots::{self, Pallet as Slots, WeightInfo},
|
||||
slots::{self, Pallet as Slots, WeightInfo as SlotsWeightInfo},
|
||||
traits::{LeaseError, Leaser, Registrar},
|
||||
MAXIMUM_BLOCK_WEIGHT,
|
||||
};
|
||||
use frame_support::{pallet_prelude::*, traits::Currency};
|
||||
use frame_system::pallet_prelude::*;
|
||||
@@ -41,6 +43,8 @@ use scale_info::TypeInfo;
|
||||
use sp_runtime::traits::{One, Saturating, Zero};
|
||||
use sp_std::prelude::*;
|
||||
|
||||
const LOG_TARGET: &str = "runtime::assigned_slots";
|
||||
|
||||
/// Lease period an assigned slot should start from (current, or next one).
|
||||
#[derive(Encode, Decode, Clone, Copy, Eq, PartialEq, RuntimeDebug, TypeInfo)]
|
||||
pub enum SlotLeasePeriodStart {
|
||||
@@ -67,6 +71,33 @@ pub struct ParachainTemporarySlot<AccountId, LeasePeriod> {
|
||||
pub lease_count: u32,
|
||||
}
|
||||
|
||||
pub trait WeightInfo {
|
||||
fn assign_perm_parachain_slot() -> Weight;
|
||||
fn assign_temp_parachain_slot() -> Weight;
|
||||
fn unassign_parachain_slot() -> Weight;
|
||||
fn set_max_permanent_slots() -> Weight;
|
||||
fn set_max_temporary_slots() -> Weight;
|
||||
}
|
||||
|
||||
pub struct TestWeightInfo;
|
||||
impl WeightInfo for TestWeightInfo {
|
||||
fn assign_perm_parachain_slot() -> Weight {
|
||||
Weight::zero()
|
||||
}
|
||||
fn assign_temp_parachain_slot() -> Weight {
|
||||
Weight::zero()
|
||||
}
|
||||
fn unassign_parachain_slot() -> Weight {
|
||||
Weight::zero()
|
||||
}
|
||||
fn set_max_permanent_slots() -> Weight {
|
||||
Weight::zero()
|
||||
}
|
||||
fn set_max_temporary_slots() -> Weight {
|
||||
Weight::zero()
|
||||
}
|
||||
}
|
||||
|
||||
type BalanceOf<T> = <<<T as Config>::Leaser as Leaser<BlockNumberFor<T>>>::Currency as Currency<
|
||||
<T as frame_system::Config>::AccountId,
|
||||
>>::Balance;
|
||||
@@ -76,7 +107,11 @@ type LeasePeriodOf<T> = <<T as Config>::Leaser as Leaser<BlockNumberFor<T>>>::Le
|
||||
pub mod pallet {
|
||||
use super::*;
|
||||
|
||||
/// The current storage version.
|
||||
const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
|
||||
|
||||
#[pallet::pallet]
|
||||
#[pallet::storage_version(STORAGE_VERSION)]
|
||||
pub struct Pallet<T>(_);
|
||||
|
||||
#[pallet::config]
|
||||
@@ -103,17 +138,12 @@ pub mod pallet {
|
||||
#[pallet::constant]
|
||||
type TemporarySlotLeasePeriodLength: Get<u32>;
|
||||
|
||||
/// The max number of permanent slots that can be assigned.
|
||||
#[pallet::constant]
|
||||
type MaxPermanentSlots: Get<u32>;
|
||||
|
||||
/// The max number of temporary slots that can be assigned.
|
||||
#[pallet::constant]
|
||||
type MaxTemporarySlots: Get<u32>;
|
||||
|
||||
/// The max number of temporary slots to be scheduled per lease periods.
|
||||
#[pallet::constant]
|
||||
type MaxTemporarySlotPerLeasePeriod: Get<u32>;
|
||||
|
||||
/// Weight Information for the Extrinsics in the Pallet
|
||||
type WeightInfo: WeightInfo;
|
||||
}
|
||||
|
||||
/// Assigned permanent slots, with their start lease period, and duration.
|
||||
@@ -148,13 +178,41 @@ pub mod pallet {
|
||||
#[pallet::getter(fn active_temporary_slot_count)]
|
||||
pub type ActiveTemporarySlotCount<T: Config> = StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
/// The max number of temporary slots that can be assigned.
|
||||
#[pallet::storage]
|
||||
pub type MaxTemporarySlots<T: Config> = StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
/// The max number of permanent slots that can be assigned.
|
||||
#[pallet::storage]
|
||||
pub type MaxPermanentSlots<T: Config> = StorageValue<_, u32, ValueQuery>;
|
||||
|
||||
#[pallet::genesis_config]
|
||||
#[derive(frame_support::DefaultNoBound)]
|
||||
pub struct GenesisConfig<T: Config> {
|
||||
pub max_temporary_slots: u32,
|
||||
pub max_permanent_slots: u32,
|
||||
pub _config: PhantomData<T>,
|
||||
}
|
||||
|
||||
#[pallet::genesis_build]
|
||||
impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
|
||||
fn build(&self) {
|
||||
<MaxPermanentSlots<T>>::put(&self.max_permanent_slots);
|
||||
<MaxTemporarySlots<T>>::put(&self.max_temporary_slots);
|
||||
}
|
||||
}
|
||||
|
||||
#[pallet::event]
|
||||
#[pallet::generate_deposit(pub(super) fn deposit_event)]
|
||||
pub enum Event<T: Config> {
|
||||
/// A para was assigned a permanent parachain slot
|
||||
/// A parachain was assigned a permanent parachain slot
|
||||
PermanentSlotAssigned(ParaId),
|
||||
/// A para was assigned a temporary parachain slot
|
||||
/// A parachain was assigned a temporary parachain slot
|
||||
TemporarySlotAssigned(ParaId),
|
||||
/// The maximum number of permanent slots has been changed
|
||||
MaxPermanentSlotsChanged { slots: u32 },
|
||||
/// The maximum number of temporary slots has been changed
|
||||
MaxTemporarySlotsChanged { slots: u32 },
|
||||
}
|
||||
|
||||
#[pallet::error]
|
||||
@@ -173,9 +231,9 @@ pub mod pallet {
|
||||
SlotNotAssigned,
|
||||
/// An ongoing lease already exists.
|
||||
OngoingLeaseExists,
|
||||
// Maximum number of permanent slots exceeded
|
||||
// The maximum number of permanent slots exceeded
|
||||
MaxPermanentSlotsExceeded,
|
||||
// Maximum number of temporary slots exceeded
|
||||
// The maximum number of temporary slots exceeded
|
||||
MaxTemporarySlotsExceeded,
|
||||
}
|
||||
|
||||
@@ -196,16 +254,15 @@ pub mod pallet {
|
||||
|
||||
#[pallet::call]
|
||||
impl<T: Config> Pallet<T> {
|
||||
// TODO: Benchmark this
|
||||
/// Assign a permanent parachain slot and immediately create a lease for it.
|
||||
#[pallet::call_index(0)]
|
||||
#[pallet::weight(((MAXIMUM_BLOCK_WEIGHT / 10) as Weight, DispatchClass::Operational))]
|
||||
#[pallet::weight((<T as Config>::WeightInfo::assign_perm_parachain_slot(), DispatchClass::Operational))]
|
||||
pub fn assign_perm_parachain_slot(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
|
||||
T::AssignSlotOrigin::ensure_origin(origin)?;
|
||||
|
||||
let manager = T::Registrar::manager_of(id).ok_or(Error::<T>::ParaDoesntExist)?;
|
||||
|
||||
ensure!(T::Registrar::is_parathread(id), Error::<T>::NotParathread,);
|
||||
ensure!(T::Registrar::is_parathread(id), Error::<T>::NotParathread);
|
||||
|
||||
ensure!(
|
||||
!Self::has_permanent_slot(id) && !Self::has_temporary_slot(id),
|
||||
@@ -227,7 +284,7 @@ pub mod pallet {
|
||||
);
|
||||
|
||||
ensure!(
|
||||
PermanentSlotCount::<T>::get() < T::MaxPermanentSlots::get(),
|
||||
PermanentSlotCount::<T>::get() < MaxPermanentSlots::<T>::get(),
|
||||
Error::<T>::MaxPermanentSlotsExceeded
|
||||
);
|
||||
|
||||
@@ -253,12 +310,11 @@ pub mod pallet {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// TODO: Benchmark this
|
||||
/// Assign a temporary parachain slot. The function tries to create a lease for it
|
||||
/// immediately if `SlotLeasePeriodStart::Current` is specified, and if the number
|
||||
/// of currently active temporary slots is below `MaxTemporarySlotPerLeasePeriod`.
|
||||
#[pallet::call_index(1)]
|
||||
#[pallet::weight(((MAXIMUM_BLOCK_WEIGHT / 10) as Weight, DispatchClass::Operational))]
|
||||
#[pallet::weight((<T as Config>::WeightInfo::assign_temp_parachain_slot(), DispatchClass::Operational))]
|
||||
pub fn assign_temp_parachain_slot(
|
||||
origin: OriginFor<T>,
|
||||
id: ParaId,
|
||||
@@ -290,7 +346,7 @@ pub mod pallet {
|
||||
);
|
||||
|
||||
ensure!(
|
||||
TemporarySlotCount::<T>::get() < T::MaxTemporarySlots::get(),
|
||||
TemporarySlotCount::<T>::get() < MaxTemporarySlots::<T>::get(),
|
||||
Error::<T>::MaxTemporarySlotsExceeded
|
||||
);
|
||||
|
||||
@@ -324,9 +380,12 @@ pub mod pallet {
|
||||
// Treat failed lease creation as warning .. slot will be allocated a lease
|
||||
// in a subsequent lease period by the `allocate_temporary_slot_leases`
|
||||
// function.
|
||||
log::warn!(target: "assigned_slots",
|
||||
log::warn!(
|
||||
target: LOG_TARGET,
|
||||
"Failed to allocate a temp slot for para {:?} at period {:?}: {:?}",
|
||||
id, current_lease_period, err
|
||||
id,
|
||||
current_lease_period,
|
||||
err
|
||||
);
|
||||
},
|
||||
}
|
||||
@@ -340,10 +399,9 @@ pub mod pallet {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// TODO: Benchmark this
|
||||
/// Unassign a permanent or temporary parachain slot
|
||||
#[pallet::call_index(2)]
|
||||
#[pallet::weight(((MAXIMUM_BLOCK_WEIGHT / 10) as Weight, DispatchClass::Operational))]
|
||||
#[pallet::weight((<T as Config>::WeightInfo::unassign_parachain_slot(), DispatchClass::Operational))]
|
||||
pub fn unassign_parachain_slot(origin: OriginFor<T>, id: ParaId) -> DispatchResult {
|
||||
T::AssignSlotOrigin::ensure_origin(origin.clone())?;
|
||||
|
||||
@@ -377,15 +435,42 @@ pub mod pallet {
|
||||
// Treat failed downgrade as warning .. slot lease has been cleared,
|
||||
// so the parachain will be downgraded anyway by the slots pallet
|
||||
// at the end of the lease period .
|
||||
log::warn!(target: "assigned_slots",
|
||||
log::warn!(
|
||||
target: LOG_TARGET,
|
||||
"Failed to downgrade parachain {:?} at period {:?}: {:?}",
|
||||
id, Self::current_lease_period_index(), err
|
||||
id,
|
||||
Self::current_lease_period_index(),
|
||||
err
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets the storage value [`MaxPermanentSlots`].
|
||||
#[pallet::call_index(3)]
|
||||
#[pallet::weight((<T as Config>::WeightInfo::set_max_permanent_slots(), DispatchClass::Operational))]
|
||||
pub fn set_max_permanent_slots(origin: OriginFor<T>, slots: u32) -> DispatchResult {
|
||||
ensure_root(origin)?;
|
||||
|
||||
<MaxPermanentSlots<T>>::put(slots);
|
||||
|
||||
Self::deposit_event(Event::<T>::MaxPermanentSlotsChanged { slots });
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Sets the storage value [`MaxTemporarySlots`].
|
||||
#[pallet::call_index(4)]
|
||||
#[pallet::weight((<T as Config>::WeightInfo::set_max_temporary_slots(), DispatchClass::Operational))]
|
||||
pub fn set_max_temporary_slots(origin: OriginFor<T>, slots: u32) -> DispatchResult {
|
||||
ensure_root(origin)?;
|
||||
|
||||
<MaxTemporarySlots<T>>::put(slots);
|
||||
|
||||
Self::deposit_event(Event::<T>::MaxTemporarySlotsChanged { slots });
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -530,9 +615,11 @@ impl<T: Config> Pallet<T> {
|
||||
// Note: leases that have ended in previous lease period, should have been cleaned in slots
|
||||
// pallet.
|
||||
if let Err(err) = Self::allocate_temporary_slot_leases(lease_period_index) {
|
||||
log::error!(target: "assigned_slots",
|
||||
log::error!(
|
||||
target: LOG_TARGET,
|
||||
"Allocating slots failed for lease period {:?}, with: {:?}",
|
||||
lease_period_index, err
|
||||
lease_period_index,
|
||||
err
|
||||
);
|
||||
}
|
||||
<T as slots::Config>::WeightInfo::force_lease() *
|
||||
@@ -673,8 +760,6 @@ mod tests {
|
||||
parameter_types! {
|
||||
pub const PermanentSlotLeasePeriodLength: u32 = 3;
|
||||
pub const TemporarySlotLeasePeriodLength: u32 = 2;
|
||||
pub const MaxPermanentSlots: u32 = 2;
|
||||
pub const MaxTemporarySlots: u32 = 6;
|
||||
pub const MaxTemporarySlotPerLeasePeriod: u32 = 2;
|
||||
}
|
||||
|
||||
@@ -684,9 +769,8 @@ mod tests {
|
||||
type Leaser = Slots;
|
||||
type PermanentSlotLeasePeriodLength = PermanentSlotLeasePeriodLength;
|
||||
type TemporarySlotLeasePeriodLength = TemporarySlotLeasePeriodLength;
|
||||
type MaxPermanentSlots = MaxPermanentSlots;
|
||||
type MaxTemporarySlots = MaxTemporarySlots;
|
||||
type MaxTemporarySlotPerLeasePeriod = MaxTemporarySlotPerLeasePeriod;
|
||||
type WeightInfo = crate::assigned_slots::TestWeightInfo;
|
||||
}
|
||||
|
||||
// This function basically just builds a genesis storage key/value store according to
|
||||
@@ -698,6 +782,15 @@ mod tests {
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
crate::assigned_slots::GenesisConfig::<Test> {
|
||||
max_temporary_slots: 6,
|
||||
max_permanent_slots: 2,
|
||||
_config: Default::default(),
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
t.into()
|
||||
}
|
||||
|
||||
@@ -1324,4 +1417,47 @@ mod tests {
|
||||
assert_eq!(Slots::already_leased(ParaId::from(1_u32), 0, 1), false);
|
||||
});
|
||||
}
|
||||
#[test]
|
||||
fn set_max_permanent_slots_fails_for_no_root_origin() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
|
||||
assert_noop!(
|
||||
AssignedSlots::set_max_permanent_slots(RuntimeOrigin::signed(1), 5),
|
||||
BadOrigin
|
||||
);
|
||||
});
|
||||
}
|
||||
#[test]
|
||||
fn set_max_permanent_slots_succeeds() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
|
||||
assert_eq!(MaxPermanentSlots::<Test>::get(), 2);
|
||||
assert_ok!(AssignedSlots::set_max_permanent_slots(RuntimeOrigin::root(), 10),);
|
||||
assert_eq!(MaxPermanentSlots::<Test>::get(), 10);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn set_max_temporary_slots_fails_for_no_root_origin() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
|
||||
assert_noop!(
|
||||
AssignedSlots::set_max_temporary_slots(RuntimeOrigin::signed(1), 5),
|
||||
BadOrigin
|
||||
);
|
||||
});
|
||||
}
|
||||
#[test]
|
||||
fn set_max_temporary_slots_succeeds() {
|
||||
new_test_ext().execute_with(|| {
|
||||
run_to_block(1);
|
||||
|
||||
assert_eq!(MaxTemporarySlots::<Test>::get(), 6);
|
||||
assert_ok!(AssignedSlots::set_max_temporary_slots(RuntimeOrigin::root(), 12),);
|
||||
assert_eq!(MaxTemporarySlots::<Test>::get(), 12);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user