polkadot_runtime_common/slots/
migration.rs1use super::*;
18use crate::crowdloan;
19use sp_runtime::traits::AccountIdConversion;
20
21pub mod slots_crowdloan_index_migration {
23 use super::*;
24
25 fn old_fund_account_id<T: Config + crowdloan::Config>(index: ParaId) -> T::AccountId {
27 <T as crowdloan::Config>::PalletId::get().into_sub_account_truncating(index)
28 }
29
30 pub fn pre_migrate<T: Config + crowdloan::Config>() -> Result<(), &'static str> {
31 for (para_id, leases) in Leases::<T>::iter() {
32 let old_fund_account = old_fund_account_id::<T>(para_id);
33
34 for (who, _amount) in leases.iter().flatten() {
35 if *who == old_fund_account {
36 let crowdloan =
37 crowdloan::Funds::<T>::get(para_id).ok_or("no crowdloan found")?;
38 log::info!(
39 target: "runtime",
40 "para_id={:?}, old_fund_account={:?}, fund_id={:?}, leases={:?}",
41 para_id, old_fund_account, crowdloan.fund_index, leases,
42 );
43 break
44 }
45 }
46 }
47
48 Ok(())
49 }
50
51 pub fn migrate<T: Config + crowdloan::Config>() -> frame_support::weights::Weight {
52 let mut weight = Weight::zero();
53
54 for (para_id, mut leases) in Leases::<T>::iter() {
55 weight = weight.saturating_add(T::DbWeight::get().reads(2));
56 if let Some(fund) = crowdloan::Funds::<T>::get(para_id) {
58 let old_fund_account = old_fund_account_id::<T>(para_id);
59 let new_fund_account = crowdloan::Pallet::<T>::fund_account_id(fund.fund_index);
60
61 for (who, _amount) in leases.iter_mut().flatten() {
63 if *who == old_fund_account {
64 *who = new_fund_account.clone();
65 }
66 }
67
68 weight = weight.saturating_add(T::DbWeight::get().writes(1));
70 Leases::<T>::insert(para_id, leases);
71 }
72 }
73
74 weight
75 }
76
77 pub fn post_migrate<T: Config + crowdloan::Config>() -> Result<(), &'static str> {
78 for (para_id, leases) in Leases::<T>::iter() {
79 let old_fund_account = old_fund_account_id::<T>(para_id);
80 log::info!(target: "runtime", "checking para_id: {:?}", para_id);
81 for (who, _amount) in leases.iter().flatten() {
83 if *who == old_fund_account {
84 panic!("old fund account found after migration!");
85 }
86 }
87 }
88 Ok(())
89 }
90}