1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use super::*;
use frame_support::{storage_alias, Twox64Concat};
pub mod crowdloan_index_migration {
use super::*;
#[storage_alias]
type NextTrieIndex<T: Config> = StorageValue<Pallet<T>, FundIndex>;
#[storage_alias]
type Leases<T: Config> = StorageMap<
Slots,
Twox64Concat,
ParaId,
Vec<Option<(<T as frame_system::Config>::AccountId, BalanceOf<T>)>>,
>;
fn old_fund_account_id<T: Config>(index: ParaId) -> T::AccountId {
T::PalletId::get().into_sub_account_truncating(index)
}
pub fn pre_migrate<T: Config>() -> Result<(), &'static str> {
let next_index = NextTrieIndex::<T>::get().unwrap_or_default();
ensure!(next_index > 0, "Next index is zero, which implies no migration is needed.");
log::info!(
target: "runtime",
"next trie index: {:?}",
next_index,
);
for (para_id, fund) in Funds::<T>::iter() {
let old_fund_account = old_fund_account_id::<T>(para_id);
let total_balance = CurrencyOf::<T>::total_balance(&old_fund_account);
log::info!(
target: "runtime",
"para_id={:?}, old_fund_account={:?}, total_balance={:?}, fund.raised={:?}",
para_id, old_fund_account, total_balance, fund.raised
);
ensure!(
total_balance >= fund.raised,
"Total balance is not equal to the funds raised."
);
let leases = Leases::<T>::get(para_id).unwrap_or_default();
let mut found_lease_deposit = false;
for maybe_deposit in leases.iter() {
if let Some((who, _amount)) = maybe_deposit {
if *who == old_fund_account {
found_lease_deposit = true;
break
}
}
}
if found_lease_deposit {
log::info!(
target: "runtime",
"para_id={:?}, old_fund_account={:?}, leases={:?}",
para_id, old_fund_account, leases,
);
}
}
Ok(())
}
pub fn migrate<T: Config>() -> frame_support::weights::Weight {
let mut weight = Weight::zero();
let next_index = NextTrieIndex::<T>::take().unwrap_or_default();
NextFundIndex::<T>::set(next_index);
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2));
for (para_id, fund) in Funds::<T>::iter() {
let old_fund_account = old_fund_account_id::<T>(para_id);
let new_fund_account = Pallet::<T>::fund_account_id(fund.fund_index);
let account_info = frame_system::Account::<T>::take(&old_fund_account);
frame_system::Account::<T>::insert(&new_fund_account, account_info);
weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2));
let mut leases = Leases::<T>::get(para_id).unwrap_or_default();
for maybe_deposit in leases.iter_mut() {
if let Some((who, _amount)) = maybe_deposit {
if *who == old_fund_account {
*who = new_fund_account.clone();
}
}
}
Leases::<T>::insert(para_id, leases);
}
weight
}
pub fn post_migrate<T: Config>() -> Result<(), &'static str> {
ensure!(NextTrieIndex::<T>::get().is_none(), "NextTrieIndex still has a value.");
let next_index = NextFundIndex::<T>::get();
log::info!(
target: "runtime",
"next fund index: {:?}",
next_index,
);
ensure!(
next_index > 0,
"NextFundIndex was not migrated or is zero. We assume it cannot be zero else no migration is needed."
);
for (para_id, fund) in Funds::<T>::iter() {
let old_fund_account = old_fund_account_id::<T>(para_id);
ensure!(
frame_system::Account::<T>::get(&old_fund_account) == Default::default(),
"Old account wasn't reset to default value."
);
let new_fund_account = Pallet::<T>::fund_account_id(fund.fund_index);
let total_balance = CurrencyOf::<T>::total_balance(&new_fund_account);
ensure!(
total_balance >= fund.raised,
"Total balance in new account is different than the funds raised."
);
let leases = Leases::<T>::get(para_id).unwrap_or_default();
let mut new_account_found = false;
for maybe_deposit in leases.iter() {
if let Some((who, _amount)) = maybe_deposit {
if *who == old_fund_account {
panic!("Old fund account found after migration!");
} else if *who == new_fund_account {
new_account_found = true;
}
}
}
if new_account_found {
log::info!(
target: "runtime::crowdloan",
"para_id={:?}, new_fund_account={:?}, leases={:?}",
para_id, new_fund_account, leases,
);
}
}
Ok(())
}
}