Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Change the Config of the MaxRococoNum Slot from a Constant to a Storage function #7217

Merged
merged 61 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
1217e20
set MaxPermanentSlots and MaxTemporarySlots with a extrinsic instead …
AlexD10S Apr 4, 2023
8a0d765
delete the MaxPermanentSlots and MaxTemporarySlots constants from co…
AlexD10S Apr 4, 2023
74daef0
merge master
AlexD10S May 12, 2023
2d7f78f
migration code for assigned slots
AlexD10S May 12, 2023
ef02259
remove getters
AlexD10S May 16, 2023
c1792f2
little refactor
AlexD10S May 16, 2023
89992b1
set values in the GenesisConfig
AlexD10S May 17, 2023
f68e45a
refactor in the migration, adding it in the rococo runtime
AlexD10S May 17, 2023
9bc0f07
refactor: fmt
AlexD10S May 17, 2023
d189c76
Minor fix
al3mart May 18, 2023
2bbbda7
pre_upgrade check
al3mart May 18, 2023
81e269c
add migration to mod v1
al3mart May 18, 2023
09c691b
Logs following Substrate#12873
al3mart May 18, 2023
110ff1d
fix: current storage version set to 1
AlexD10S May 19, 2023
6e18128
Merge branch 'master' into alexd10s/revamp_assigned_slots_rococo
al3mart May 19, 2023
e77d8d7
use enact when try-runtime
al3mart May 19, 2023
98b2a38
Vec seems to be missing
al3mart May 22, 2023
87c988b
merge master
al3mart May 22, 2023
eccbe04
feature gate import
al3mart May 22, 2023
3ac81c7
merge master
al3mart Jun 12, 2023
4fedca4
fix as per #13993
al3mart Jun 13, 2023
25f1d71
address comments
al3mart Jun 13, 2023
86b4e8f
address comments
al3mart Jun 13, 2023
6854094
benchmarking for assign_perm_parachain_slot extrinsic
AlexD10S Jul 4, 2023
d95157f
benchmark all the extrinsics of the pallet
AlexD10S Jul 4, 2023
4067e2e
merge master branch
AlexD10S Jul 5, 2023
3142816
cargo fmt for assigned slots
AlexD10S Jul 5, 2023
2f36bb8
migration added for westend
AlexD10S Jul 5, 2023
a944d52
Merge branch 'master' into alexd10s/revamp_assigned_slots_rococo
AlexD10S Jul 24, 2023
9705b9d
licence in benchmarking file
AlexD10S Jul 24, 2023
c993252
Merge branch 'master' into alexd10s/revamp_assigned_slots_rococo
al3mart Jul 24, 2023
c04fb38
BuildGenesisConfig
al3mart Jul 24, 2023
a3af0ff
assigned_slots default in genesis
AlexD10S Jul 24, 2023
03dd31b
cargo fmt
AlexD10S Jul 25, 2023
1c059af
assigned_slots fix tests config
AlexD10S Jul 25, 2023
200597a
cargo fmt
AlexD10S Jul 25, 2023
5c2b2eb
fix benchmarking compile error
AlexD10S Jul 26, 2023
4b5130c
Merge branch 'master' into alexd10s/revamp_assigned_slots_rococo
AlexD10S Jul 26, 2023
473d073
fix benchmarking imports
AlexD10S Aug 3, 2023
1cb2362
benchmark worst case scenario for validation code and head data
AlexD10S Aug 3, 2023
cbffba0
add assigned_slots in frame_benchmarking on Rococo and Westend
AlexD10S Aug 3, 2023
ca65088
modify values for para_id in benchmarking
AlexD10S Aug 3, 2023
61b6a95
delete the assigned_slots in westend frame_benchmarking
AlexD10S Aug 3, 2023
7be2dc0
fix benchmarkings and add it to westend
AlexD10S Aug 7, 2023
34e8bc1
cargo fmt
AlexD10S Aug 7, 2023
216cdb1
Merge branch 'master' of https://github.com/paritytech/polkadot into …
Aug 7, 2023
86f977f
".git/.scripts/commands/bench/bench.sh" --subcommand=runtime --runtim…
Aug 7, 2023
8b0d088
".git/.scripts/commands/bench/bench.sh" --subcommand=runtime --runtim…
Aug 7, 2023
38ea151
use generated weights in assigned_slots pallet
AlexD10S Aug 7, 2023
a022c7b
small changes in set_max_permanent_slots and set_max_temporary_slots
AlexD10S Aug 8, 2023
86015e5
revert last commit
AlexD10S Aug 8, 2023
4e5bcb8
Merge branch 'master' into alexd10s/revamp_assigned_slots_rococo
AlexD10S Aug 8, 2023
9603fc6
address some comments
AlexD10S Aug 10, 2023
176c8b3
wrap migration with VersionCheckedMigrateToV1
AlexD10S Aug 10, 2023
cd237a7
add experimental feature in pallet, and assers in post_upgrade migration
AlexD10S Aug 10, 2023
795aed4
clean warnings
AlexD10S Aug 10, 2023
16d2c52
clean unnecesary experimental flag
AlexD10S Aug 11, 2023
9c6cf4b
Merge branch 'master' into alexd10s/revamp_assigned_slots_rococo
AlexD10S Aug 15, 2023
e855ce3
small typo in comments
AlexD10S Aug 15, 2023
1061caa
cargo fmt
AlexD10S Aug 15, 2023
9095fd0
small comments fixes
AlexD10S Aug 15, 2023
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
62 changes: 62 additions & 0 deletions runtime/common/src/assigned_slots/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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, StorageVersion},
weights::Weight,
};

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>, &'static str> {
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);
// Update storage version.
StorageVersion::new(1).put::<Pallet<T>>();
// Return the weight consumed by the migration.
T::DbWeight::get().reads_writes(1, 3)
} else {
log::info!(target: LOG_TARGET, "assigned_slots::MigrateToV1 should be removed");
al3mart marked this conversation as resolved.
Show resolved Hide resolved

Weight::zero()
al3mart marked this conversation as resolved.
Show resolved Hide resolved
}
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
let onchain_version = Pallet::<T>::on_chain_storage_version();
ensure!(onchain_version == 1, "assigned_slots::MigrateToV1 needs to be run");
Ok(())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
//! This pallet should not be used on a production relay chain,
//! only on a test relay chain (e.g. Rococo).

pub mod migration;

use crate::{
slots::{self, Pallet as Slots, WeightInfo},
traits::{LeaseError, Leaser, Registrar},
Expand All @@ -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 {
Expand Down Expand Up @@ -77,7 +81,11 @@ type LeasePeriodOf<T> =
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]
Expand All @@ -104,14 +112,6 @@ 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>;
Expand Down Expand Up @@ -149,13 +149,40 @@ pub mod pallet {
#[pallet::getter(fn active_temporary_slot_count)]
pub type ActiveTemporarySlotCount<T: Config> = StorageValue<_, u32, ValueQuery>;

/// Assigned max temporary slots storage.
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::storage]
pub type MaxTemporarySlots<T: Config> = StorageValue<_, u32, ValueQuery>;

/// Assigned max permanent slots storage.
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::storage]
pub type MaxPermanentSlots<T: Config> = StorageValue<_, u32, ValueQuery>;

#[pallet::genesis_config]
#[derive(Default)]
pub struct GenesisConfig {
pub max_temporary_slots: u32,
pub max_permanent_slots: u32,
}

#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig {
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
PermanentSlotAssigned(ParaId),
/// A para was assigned a temporary parachain slot
TemporarySlotAssigned(ParaId),
/// A maximum number of permanent slots has been assigned
MaxPermanentSlotsAssigned { slots: u32 },
/// A maximum number of temporary slots has been assigned
MaxTemporarySlotsAssigned { slots: u32 },
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
}

#[pallet::error]
Expand Down Expand Up @@ -228,7 +255,7 @@ pub mod pallet {
);

ensure!(
PermanentSlotCount::<T>::get() < T::MaxPermanentSlots::get(),
PermanentSlotCount::<T>::get() < MaxPermanentSlots::<T>::get(),
Error::<T>::MaxPermanentSlotsExceeded
);

Expand Down Expand Up @@ -291,7 +318,7 @@ pub mod pallet {
);

ensure!(
TemporarySlotCount::<T>::get() < T::MaxTemporarySlots::get(),
TemporarySlotCount::<T>::get() < MaxTemporarySlots::<T>::get(),
Error::<T>::MaxTemporarySlotsExceeded
);

Expand Down Expand Up @@ -324,9 +351,12 @@ pub mod pallet {
Err(err) => {
// 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
);
},
}
Expand Down Expand Up @@ -377,15 +407,48 @@ 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(())
}

// TODO: Benchmark this
/// Assign a max permanent slot number.
#[pallet::call_index(3)]
#[pallet::weight(((MAXIMUM_BLOCK_WEIGHT / 10) as Weight, DispatchClass::Operational))]
pub fn set_max_permanent_slots(origin: OriginFor<T>, slots: u32) -> DispatchResult {
ensure_root(origin)?;

<MaxPermanentSlots<T>>::put(slots);

// Emit an event.
Self::deposit_event(Event::<T>::MaxPermanentSlotsAssigned { slots });
// Return a successful DispatchResult
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}

// TODO: Benchmark this
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is nothing in this pallet benchmarked 🙈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added!
Not sure about how to proceed with the weights, Do I have to generate the weights file using my machine?
Or do something like the auctions:
https://github.com/paritytech/polkadot/blob/master/runtime/common/src/auctions.rs#L50
https://github.com/paritytech/polkadot/blob/master/runtime/common/src/auctions.rs#L283

/// Assign a max temporary slot number.
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::call_index(4)]
#[pallet::weight(((MAXIMUM_BLOCK_WEIGHT / 10) as Weight, DispatchClass::Operational))]
pub fn set_max_temporary_slots(origin: OriginFor<T>, slots: u32) -> DispatchResult {
ensure_root(origin)?;

<MaxTemporarySlots<T>>::put(slots);

// Emit an event.
Self::deposit_event(Event::<T>::MaxTemporarySlotsAssigned { slots });
// Return a successful DispatchResult
AlexD10S marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}
}
}

Expand Down Expand Up @@ -528,9 +591,11 @@ impl<T: Config> Pallet<T> {
fn manage_lease_period_start(lease_period_index: LeasePeriodOf<T>) -> Weight {
// 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() *
Expand Down Expand Up @@ -673,8 +738,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;
}

Expand All @@ -684,8 +747,6 @@ mod tests {
type Leaser = Slots;
type PermanentSlotLeasePeriodLength = PermanentSlotLeasePeriodLength;
type TemporarySlotLeasePeriodLength = TemporarySlotLeasePeriodLength;
type MaxPermanentSlots = MaxPermanentSlots;
type MaxTemporarySlots = MaxTemporarySlots;
type MaxTemporarySlotPerLeasePeriod = MaxTemporarySlotPerLeasePeriod;
}

Expand All @@ -698,6 +759,16 @@ mod tests {
}
.assimilate_storage(&mut t)
.unwrap();

GenesisBuild::<Test>::assimilate_storage(
&crate::assigned_slots::GenesisConfig {
max_temporary_slots: 6,
max_permanent_slots: 2,
},
&mut t,
)
.unwrap();

t.into()
}

Expand Down Expand Up @@ -1324,4 +1395,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);
});
}
}
6 changes: 1 addition & 5 deletions runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,8 +1296,6 @@ impl paras_sudo_wrapper::Config for Runtime {}
parameter_types! {
pub const PermanentSlotLeasePeriodLength: u32 = 365;
pub const TemporarySlotLeasePeriodLength: u32 = 5;
pub const MaxPermanentSlots: u32 = 100;
pub const MaxTemporarySlots: u32 = 100;
pub const MaxTemporarySlotPerLeasePeriod: u32 = 5;
}

Expand All @@ -1307,8 +1305,6 @@ impl assigned_slots::Config for Runtime {
type Leaser = Slots;
type PermanentSlotLeasePeriodLength = PermanentSlotLeasePeriodLength;
type TemporarySlotLeasePeriodLength = TemporarySlotLeasePeriodLength;
type MaxPermanentSlots = MaxPermanentSlots;
type MaxTemporarySlots = MaxTemporarySlots;
type MaxTemporarySlotPerLeasePeriod = MaxTemporarySlotPerLeasePeriod;
}

Expand Down Expand Up @@ -1495,7 +1491,7 @@ pub mod migrations {
);

/// Unreleased migrations. Add new ones here:
pub type Unreleased = ();
pub type Unreleased = (assigned_slots::migration::v1::MigrateToV1<Runtime>,);
}

/// Executive: handles dispatch to the various modules.
Expand Down
4 changes: 0 additions & 4 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -947,8 +947,6 @@ impl paras_sudo_wrapper::Config for Runtime {}
parameter_types! {
gilescope marked this conversation as resolved.
Show resolved Hide resolved
pub const PermanentSlotLeasePeriodLength: u32 = 26;
pub const TemporarySlotLeasePeriodLength: u32 = 1;
pub const MaxPermanentSlots: u32 = 5;
pub const MaxTemporarySlots: u32 = 20;
pub const MaxTemporarySlotPerLeasePeriod: u32 = 5;
}

Expand All @@ -958,8 +956,6 @@ impl assigned_slots::Config for Runtime {
type Leaser = Slots;
type PermanentSlotLeasePeriodLength = PermanentSlotLeasePeriodLength;
type TemporarySlotLeasePeriodLength = TemporarySlotLeasePeriodLength;
type MaxPermanentSlots = MaxPermanentSlots;
type MaxTemporarySlots = MaxTemporarySlots;
type MaxTemporarySlotPerLeasePeriod = MaxTemporarySlotPerLeasePeriod;
}

Expand Down