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

            
17
use super::*;
18
use frame_support::{
19
	pallet_prelude::*,
20
	traits::{OnRuntimeUpgrade, PalletInfoAccess},
21
	weights::Weight,
22
};
23

            
24
fn migrate_v0_to_v1<T: Config<I>, I: 'static>(accounts: &[T::AccountId]) -> Weight {
25
	let on_chain_version = Pallet::<T, I>::on_chain_storage_version();
26

            
27
	if on_chain_version == 0 {
28
		let total = accounts
29
			.iter()
30
			.map(|a| Pallet::<T, I>::total_balance(a))
31
			.fold(T::Balance::zero(), |a, e| a.saturating_add(e));
32
		Pallet::<T, I>::deactivate(total);
33

            
34
		// Remove the old `StorageVersion` type.
35
		frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
36
			Pallet::<T, I>::name().as_bytes(),
37
			"StorageVersion".as_bytes(),
38
		));
39

            
40
		// Set storage version to `1`.
41
		StorageVersion::new(1).put::<Pallet<T, I>>();
42

            
43
		log::info!(target: LOG_TARGET, "Storage to version 1");
44
		T::DbWeight::get().reads_writes(2 + accounts.len() as u64, 3)
45
	} else {
46
		log::info!(
47
			target: LOG_TARGET,
48
			"Migration did not execute. This probably should be removed"
49
		);
50
		T::DbWeight::get().reads(1)
51
	}
52
}
53

            
54
// NOTE: This must be used alongside the account whose balance is expected to be inactive.
55
// Generally this will be used for the XCM teleport checking account.
56
pub struct MigrateToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
57
impl<T: Config<I>, A: Get<T::AccountId>, I: 'static> OnRuntimeUpgrade
58
	for MigrateToTrackInactive<T, A, I>
59
{
60
	fn on_runtime_upgrade() -> Weight {
61
		migrate_v0_to_v1::<T, I>(&[A::get()])
62
	}
63
}
64

            
65
// NOTE: This must be used alongside the accounts whose balance is expected to be inactive.
66
// Generally this will be used for the XCM teleport checking accounts.
67
pub struct MigrateManyToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
68
impl<T: Config<I>, A: Get<Vec<T::AccountId>>, I: 'static> OnRuntimeUpgrade
69
	for MigrateManyToTrackInactive<T, A, I>
70
{
71
	fn on_runtime_upgrade() -> Weight {
72
		migrate_v0_to_v1::<T, I>(&A::get())
73
	}
74
}
75

            
76
pub struct ResetInactive<T, I = ()>(PhantomData<(T, I)>);
77
impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for ResetInactive<T, I> {
78
	fn on_runtime_upgrade() -> Weight {
79
		let on_chain_version = Pallet::<T, I>::on_chain_storage_version();
80

            
81
		if on_chain_version == 1 {
82
			// Remove the old `StorageVersion` type.
83
			frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
84
				Pallet::<T, I>::name().as_bytes(),
85
				"StorageVersion".as_bytes(),
86
			));
87

            
88
			InactiveIssuance::<T, I>::kill();
89

            
90
			// Set storage version to `0`.
91
			StorageVersion::new(0).put::<Pallet<T, I>>();
92

            
93
			log::info!(target: LOG_TARGET, "Storage to version 0");
94
			T::DbWeight::get().reads_writes(1, 3)
95
		} else {
96
			log::info!(
97
				target: LOG_TARGET,
98
				"Migration did not execute. This probably should be removed"
99
			);
100
			T::DbWeight::get().reads(1)
101
		}
102
	}
103
}