referrerpolicy=no-referrer-when-downgrade

polkadot_runtime_common/assigned_slots/
migration.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Polkadot.
3
4// Polkadot is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Polkadot is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16
17use super::{Config, MaxPermanentSlots, MaxTemporarySlots, Pallet, LOG_TARGET};
18use frame_support::traits::{Get, GetStorageVersion, UncheckedOnRuntimeUpgrade};
19
20#[cfg(feature = "try-runtime")]
21use alloc::vec::Vec;
22#[cfg(feature = "try-runtime")]
23use frame_support::ensure;
24
25pub mod v1 {
26	use super::*;
27	pub struct VersionUncheckedMigrateToV1<T>(core::marker::PhantomData<T>);
28	impl<T: Config> UncheckedOnRuntimeUpgrade for VersionUncheckedMigrateToV1<T> {
29		#[cfg(feature = "try-runtime")]
30		fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
31			let on_chain_version = Pallet::<T>::on_chain_storage_version();
32			ensure!(on_chain_version < 1, "assigned_slots::MigrateToV1 migration can be deleted");
33			Ok(Default::default())
34		}
35
36		fn on_runtime_upgrade() -> frame_support::weights::Weight {
37			let on_chain_version = Pallet::<T>::on_chain_storage_version();
38			if on_chain_version < 1 {
39				const MAX_PERMANENT_SLOTS: u32 = 100;
40				const MAX_TEMPORARY_SLOTS: u32 = 100;
41
42				MaxPermanentSlots::<T>::put(MAX_PERMANENT_SLOTS);
43				MaxTemporarySlots::<T>::put(MAX_TEMPORARY_SLOTS);
44				// Return the weight consumed by the migration.
45				T::DbWeight::get().reads_writes(1, 3)
46			} else {
47				log::info!(target: LOG_TARGET, "MigrateToV1 should be removed");
48				T::DbWeight::get().reads(1)
49			}
50		}
51
52		#[cfg(feature = "try-runtime")]
53		fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
54			let on_chain_version = Pallet::<T>::on_chain_storage_version();
55			ensure!(on_chain_version == 1, "assigned_slots::MigrateToV1 needs to be run");
56			assert_eq!(MaxPermanentSlots::<T>::get(), 100);
57			assert_eq!(MaxTemporarySlots::<T>::get(), 100);
58			Ok(())
59		}
60	}
61
62	/// [`VersionUncheckedMigrateToV1`] wrapped in a
63	/// [`VersionedMigration`](frame_support::migrations::VersionedMigration), ensuring the
64	/// migration is only performed when on-chain version is 0.
65	pub type MigrateToV1<T> = frame_support::migrations::VersionedMigration<
66		0,
67		1,
68		VersionUncheckedMigrateToV1<T>,
69		Pallet<T>,
70		<T as frame_system::Config>::DbWeight,
71	>;
72}