polkadot_runtime_common/paras_registrar/
migration.rs1use 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>;