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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

use super::*;
use frame_support::{
	storage_alias,
	traits::{GetStorageVersion, OnRuntimeUpgrade, StorageVersion},
	Twox64Concat,
};

pub struct MigrateToTrackInactiveV2<T>(core::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToTrackInactiveV2<T> {
	fn on_runtime_upgrade() -> Weight {
		let on_chain_version = Pallet::<T>::on_chain_storage_version();

		if on_chain_version == 1 {
			let mut translated = 0u64;
			for item in Funds::<T>::iter_values() {
				let b =
					CurrencyOf::<T>::total_balance(&Pallet::<T>::fund_account_id(item.fund_index));
				CurrencyOf::<T>::deactivate(b);
				translated.saturating_inc();
			}

			StorageVersion::new(2).put::<Pallet<T>>();
			log::info!(target: "runtime::crowdloan", "Summed {} funds, storage to version 1", translated);
			T::DbWeight::get().reads_writes(translated * 2 + 1, translated * 2 + 1)
		} else {
			log::info!(target: "runtime::crowdloan",  "Migration did not execute. This probably should be removed");
			T::DbWeight::get().reads(1)
		}
	}

	#[cfg(feature = "try-runtime")]
	fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
		let total = Funds::<T>::iter_values()
			.map(|item| {
				CurrencyOf::<T>::total_balance(&Pallet::<T>::fund_account_id(item.fund_index))
			})
			.fold(BalanceOf::<T>::zero(), |a, i| a.saturating_add(i));
		Ok((total, CurrencyOf::<T>::active_issuance()).encode())
	}

	#[cfg(feature = "try-runtime")]
	fn post_upgrade(total: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
		if let Ok((total, active)) = <(BalanceOf<T>, BalanceOf<T>)>::decode(&mut total.as_slice()) {
			ensure!(active - total == CurrencyOf::<T>::active_issuance(), "the total be correct");
			Ok(())
		} else {
			Err("the state parameter should be something that was generated by pre_upgrade".into())
		}
	}
}

/// Migrations for using fund index to create fund accounts instead of para ID.
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>)>>,
	>;

	// The old way we generated fund accounts.
	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> {
		// `NextTrieIndex` should have a value.

		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
			);

			// Each fund should have some non-zero balance.
			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 (who, _amount) in leases.iter().flatten() {
				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(())
	}

	/// This migration converts crowdloans to use a crowdloan index rather than the parachain id as
	/// a unique identifier. This makes it easier to swap two crowdloans between parachains.
	pub fn migrate<T: Config>() -> frame_support::weights::Weight {
		let mut weight = Weight::zero();

		// First migrate `NextTrieIndex` counter to `NextFundIndex`.

		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));

		// Migrate all accounts from `old_fund_account` to `fund_account` using `fund_index`.
		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);

			// Funds should only have a free balance and a reserve balance. Both of these are in the
			// `Account` storage item, so we just swap them.
			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 (who, _amount) in leases.iter_mut().flatten() {
				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> {
		// `NextTrieIndex` should not have a value, and `NextFundIndex` should.
		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."
		);

		// Each fund should have balance migrated correctly.
		for (para_id, fund) in Funds::<T>::iter() {
			// Old fund account is deleted.
			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."
			);

			// New fund account has the correct balance.
			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 (who, _amount) in leases.iter().flatten() {
				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(())
	}
}