referrerpolicy=no-referrer-when-downgrade

pallet_tips/migrations/
v4.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use core::str;
19use sp_io::hashing::twox_128;
20
21use super::super::LOG_TARGET;
22use frame_support::{
23	storage::StoragePrefixedMap,
24	traits::{
25		Get, GetStorageVersion, PalletInfoAccess, StorageVersion,
26		STORAGE_VERSION_STORAGE_KEY_POSTFIX,
27	},
28	weights::Weight,
29};
30
31use crate as pallet_tips;
32
33/// Migrate the entire storage of this pallet to a new prefix.
34///
35/// This new prefix must be the same as the one set in construct_runtime.
36/// For safety, use `PalletInfo` to get it, as:
37/// `<Runtime as frame_system::Config>::PalletInfo::name::<TipsPallet>`.
38///
39/// The migration will look into the storage version in order not to trigger a migration on an up
40/// to date storage. Thus the on chain storage version must be less than 4 in order to trigger the
41/// migration.
42pub fn migrate<T: pallet_tips::Config, P: GetStorageVersion + PalletInfoAccess, N: AsRef<str>>(
43	old_pallet_name: N,
44) -> Weight {
45	let old_pallet_name = old_pallet_name.as_ref();
46	let new_pallet_name = <P as PalletInfoAccess>::name();
47
48	if new_pallet_name == old_pallet_name {
49		log::info!(
50			target: LOG_TARGET,
51			"New pallet name is equal to the old prefix. No migration needs to be done.",
52		);
53		return Weight::zero()
54	}
55
56	let on_chain_storage_version = <P as GetStorageVersion>::on_chain_storage_version();
57	log::info!(
58		target: LOG_TARGET,
59		"Running migration to v4 for tips with storage version {:?}",
60		on_chain_storage_version,
61	);
62
63	if on_chain_storage_version < 4 {
64		let storage_prefix = pallet_tips::Tips::<T>::storage_prefix();
65		frame_support::storage::migration::move_storage_from_pallet(
66			storage_prefix,
67			old_pallet_name.as_bytes(),
68			new_pallet_name.as_bytes(),
69		);
70		log_migration("migration", storage_prefix, old_pallet_name, new_pallet_name);
71
72		let storage_prefix = pallet_tips::Reasons::<T>::storage_prefix();
73		frame_support::storage::migration::move_storage_from_pallet(
74			storage_prefix,
75			old_pallet_name.as_bytes(),
76			new_pallet_name.as_bytes(),
77		);
78		log_migration("migration", storage_prefix, old_pallet_name, new_pallet_name);
79
80		StorageVersion::new(4).put::<P>();
81		<T as frame_system::Config>::BlockWeights::get().max_block
82	} else {
83		log::warn!(
84			target: LOG_TARGET,
85			"Attempted to apply migration to v4 but failed because storage version is {:?}",
86			on_chain_storage_version,
87		);
88		Weight::zero()
89	}
90}
91
92/// Some checks prior to migration. This can be linked to
93/// `frame_support::traits::OnRuntimeUpgrade::pre_upgrade` for further testing.
94///
95/// Panics if anything goes wrong.
96pub fn pre_migrate<
97	T: pallet_tips::Config,
98	P: GetStorageVersion + PalletInfoAccess,
99	N: AsRef<str>,
100>(
101	old_pallet_name: N,
102) {
103	let old_pallet_name = old_pallet_name.as_ref();
104	let new_pallet_name = <P as PalletInfoAccess>::name();
105
106	let storage_prefix_tips = pallet_tips::Tips::<T>::storage_prefix();
107	let storage_prefix_reasons = pallet_tips::Reasons::<T>::storage_prefix();
108
109	log_migration("pre-migration", storage_prefix_tips, old_pallet_name, new_pallet_name);
110	log_migration("pre-migration", storage_prefix_reasons, old_pallet_name, new_pallet_name);
111
112	if new_pallet_name == old_pallet_name {
113		return
114	}
115
116	let new_pallet_prefix = twox_128(new_pallet_name.as_bytes());
117	let storage_version_key = twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX);
118
119	let mut new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
120		new_pallet_prefix.to_vec(),
121		new_pallet_prefix.to_vec(),
122		|key| Ok(key.to_vec()),
123	);
124
125	// Ensure nothing except the storage_version_key is stored in the new prefix.
126	assert!(new_pallet_prefix_iter.all(|key| key == storage_version_key));
127
128	assert!(<P as GetStorageVersion>::on_chain_storage_version() < 4);
129}
130
131/// Some checks for after migration. This can be linked to
132/// `frame_support::traits::OnRuntimeUpgrade::post_upgrade` for further testing.
133///
134/// Panics if anything goes wrong.
135pub fn post_migrate<
136	T: pallet_tips::Config,
137	P: GetStorageVersion + PalletInfoAccess,
138	N: AsRef<str>,
139>(
140	old_pallet_name: N,
141) {
142	let old_pallet_name = old_pallet_name.as_ref();
143	let new_pallet_name = <P as PalletInfoAccess>::name();
144
145	let storage_prefix_tips = pallet_tips::Tips::<T>::storage_prefix();
146	let storage_prefix_reasons = pallet_tips::Reasons::<T>::storage_prefix();
147
148	log_migration("post-migration", storage_prefix_tips, old_pallet_name, new_pallet_name);
149	log_migration("post-migration", storage_prefix_reasons, old_pallet_name, new_pallet_name);
150
151	if new_pallet_name == old_pallet_name {
152		return
153	}
154
155	// Assert that no `Tips` and `Reasons` storages remains at the old prefix.
156	let old_pallet_prefix = twox_128(old_pallet_name.as_bytes());
157	let old_tips_key = [&old_pallet_prefix, &twox_128(storage_prefix_tips)[..]].concat();
158	let old_tips_key_iter = frame_support::storage::KeyPrefixIterator::new(
159		old_tips_key.to_vec(),
160		old_tips_key.to_vec(),
161		|_| Ok(()),
162	);
163	assert_eq!(old_tips_key_iter.count(), 0);
164
165	let old_reasons_key = [&old_pallet_prefix, &twox_128(storage_prefix_reasons)[..]].concat();
166	let old_reasons_key_iter = frame_support::storage::KeyPrefixIterator::new(
167		old_reasons_key.to_vec(),
168		old_reasons_key.to_vec(),
169		|_| Ok(()),
170	);
171	assert_eq!(old_reasons_key_iter.count(), 0);
172
173	// Assert that the `Tips` and `Reasons` storages (if they exist) have been moved to the new
174	// prefix.
175	// NOTE: storage_version_key is already in the new prefix.
176	let new_pallet_prefix = twox_128(new_pallet_name.as_bytes());
177	let new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
178		new_pallet_prefix.to_vec(),
179		new_pallet_prefix.to_vec(),
180		|_| Ok(()),
181	);
182	assert!(new_pallet_prefix_iter.count() >= 1);
183
184	assert_eq!(<P as GetStorageVersion>::on_chain_storage_version(), 4);
185}
186
187fn log_migration(stage: &str, storage_prefix: &[u8], old_pallet_name: &str, new_pallet_name: &str) {
188	log::info!(
189		target: LOG_TARGET,
190		"{} prefix of storage '{}': '{}' ==> '{}'",
191		stage,
192		str::from_utf8(storage_prefix).unwrap_or("<Invalid UTF8>"),
193		old_pallet_name,
194		new_pallet_name,
195	);
196}