referrerpolicy=no-referrer-when-downgrade

polkadot_runtime_common/paras_registrar/
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::*;
18use frame_support::traits::{Contains, UncheckedOnRuntimeUpgrade};
19
20#[derive(Encode, Decode)]
21pub struct ParaInfoV1<Account, Balance> {
22	manager: Account,
23	deposit: Balance,
24	locked: bool,
25}
26
27pub struct VersionUncheckedMigrateToV1<T, UnlockParaIds>(
28	core::marker::PhantomData<(T, UnlockParaIds)>,
29);
30impl<T: Config, UnlockParaIds: Contains<ParaId>> UncheckedOnRuntimeUpgrade
31	for VersionUncheckedMigrateToV1<T, UnlockParaIds>
32{
33	fn on_runtime_upgrade() -> Weight {
34		let mut count = 0u64;
35		Paras::<T>::translate::<ParaInfoV1<T::AccountId, BalanceOf<T>>, _>(|key, v1| {
36			count.saturating_inc();
37			Some(ParaInfo {
38				manager: v1.manager,
39				deposit: v1.deposit,
40				locked: if UnlockParaIds::contains(&key) { None } else { Some(v1.locked) },
41			})
42		});
43
44		log::info!(target: "runtime::registrar", "Upgraded {} storages to version 1", count);
45		T::DbWeight::get().reads_writes(count, count)
46	}
47
48	#[cfg(feature = "try-runtime")]
49	fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
50		Ok((Paras::<T>::iter_keys().count() as u32).encode())
51	}
52
53	#[cfg(feature = "try-runtime")]
54	fn post_upgrade(state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
55		let old_count = u32::decode(&mut &state[..]).expect("Known good");
56		let new_count = Paras::<T>::iter_values().count() as u32;
57
58		ensure!(old_count == new_count, "Paras count should not change");
59		Ok(())
60	}
61}
62
63pub type MigrateToV1<T, UnlockParaIds> = frame_support::migrations::VersionedMigration<
64	0,
65	1,
66	VersionUncheckedMigrateToV1<T, UnlockParaIds>,
67	super::Pallet<T>,
68	<T as frame_system::Config>::DbWeight,
69>;