referrerpolicy=no-referrer-when-downgrade

pallet_balances/
migration.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Substrate.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use super::*;
18use frame_support::{
19	pallet_prelude::*,
20	traits::{OnRuntimeUpgrade, PalletInfoAccess},
21	weights::Weight,
22};
23
24fn 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.
56pub struct MigrateToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
57impl<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.
67pub struct MigrateManyToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
68impl<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
76pub struct ResetInactive<T, I = ()>(PhantomData<(T, I)>);
77impl<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}