referrerpolicy=no-referrer-when-downgrade

pallet_bounties/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 frame_support::{
20	storage::{generator::StorageValue, StoragePrefixedMap},
21	traits::{
22		Get, GetStorageVersion, PalletInfoAccess, StorageVersion,
23		STORAGE_VERSION_STORAGE_KEY_POSTFIX,
24	},
25	weights::Weight,
26};
27use sp_core::hexdisplay::HexDisplay;
28use sp_io::{hashing::twox_128, storage};
29
30use crate as pallet_bounties;
31
32/// Migrate the storage of the bounties pallet to a new prefix, leaving all other storage untouched
33///
34/// This new prefix must be the same as the one set in construct_runtime. For safety, use
35/// `PalletInfo` to get it, as:
36/// `<Runtime as frame_system::Config>::PalletInfo::name::<BountiesPallet>`.
37///
38/// The migration will look into the storage version in order not to trigger a migration on an up
39/// to date storage. Thus the on chain storage version must be less than 4 in order to trigger the
40/// migration.
41pub fn migrate<
42	T: pallet_bounties::Config,
43	P: GetStorageVersion + PalletInfoAccess,
44	N: AsRef<str>,
45>(
46	old_pallet_name: N,
47	new_pallet_name: N,
48) -> Weight {
49	let old_pallet_name = old_pallet_name.as_ref();
50	let new_pallet_name = new_pallet_name.as_ref();
51
52	if new_pallet_name == old_pallet_name {
53		log::info!(
54			target: "runtime::bounties",
55			"New pallet name is equal to the old prefix. No migration needs to be done.",
56		);
57		return Weight::zero()
58	}
59
60	let on_chain_storage_version = <P as GetStorageVersion>::on_chain_storage_version();
61	log::info!(
62		target: "runtime::bounties",
63		"Running migration to v4 for bounties with storage version {:?}",
64		on_chain_storage_version,
65	);
66
67	if on_chain_storage_version < 4 {
68		let storage_prefix = pallet_bounties::BountyCount::<T>::storage_prefix();
69		frame_support::storage::migration::move_storage_from_pallet(
70			storage_prefix,
71			old_pallet_name.as_bytes(),
72			new_pallet_name.as_bytes(),
73		);
74		log_migration("migration", storage_prefix, old_pallet_name, new_pallet_name);
75
76		let storage_prefix = pallet_bounties::Bounties::<T>::storage_prefix();
77		frame_support::storage::migration::move_storage_from_pallet(
78			storage_prefix,
79			old_pallet_name.as_bytes(),
80			new_pallet_name.as_bytes(),
81		);
82		log_migration("migration", storage_prefix, old_pallet_name, new_pallet_name);
83
84		let storage_prefix = pallet_bounties::BountyDescriptions::<T>::storage_prefix();
85		frame_support::storage::migration::move_storage_from_pallet(
86			storage_prefix,
87			old_pallet_name.as_bytes(),
88			new_pallet_name.as_bytes(),
89		);
90		log_migration("migration", storage_prefix, old_pallet_name, new_pallet_name);
91
92		let storage_prefix = pallet_bounties::BountyApprovals::<T>::storage_prefix();
93		frame_support::storage::migration::move_storage_from_pallet(
94			storage_prefix,
95			old_pallet_name.as_bytes(),
96			new_pallet_name.as_bytes(),
97		);
98		log_migration("migration", storage_prefix, old_pallet_name, new_pallet_name);
99
100		StorageVersion::new(4).put::<P>();
101		<T as frame_system::Config>::BlockWeights::get().max_block
102	} else {
103		log::warn!(
104			target: "runtime::bounties",
105			"Attempted to apply migration to v4 but failed because storage version is {:?}",
106			on_chain_storage_version,
107		);
108		Weight::zero()
109	}
110}
111
112/// Some checks prior to migration. This can be linked to
113/// `frame_support::traits::OnRuntimeUpgrade::pre_upgrade` for further testing.
114///
115/// Panics if anything goes wrong.
116pub fn pre_migration<T: pallet_bounties::Config, P: GetStorageVersion + 'static, N: AsRef<str>>(
117	old_pallet_name: N,
118	new_pallet_name: N,
119) {
120	let old_pallet_name = old_pallet_name.as_ref();
121	let new_pallet_name = new_pallet_name.as_ref();
122	let storage_prefix_bounties_count = pallet_bounties::BountyCount::<T>::storage_prefix();
123	let storage_prefix_bounties = pallet_bounties::Bounties::<T>::storage_prefix();
124	let storage_prefix_bounties_description =
125		pallet_bounties::BountyDescriptions::<T>::storage_prefix();
126	let storage_prefix_bounties_approvals = pallet_bounties::BountyApprovals::<T>::storage_prefix();
127	log_migration("pre-migration", storage_prefix_bounties_count, old_pallet_name, new_pallet_name);
128	log_migration("pre-migration", storage_prefix_bounties, old_pallet_name, new_pallet_name);
129	log_migration(
130		"pre-migration",
131		storage_prefix_bounties_description,
132		old_pallet_name,
133		new_pallet_name,
134	);
135	log_migration(
136		"pre-migration",
137		storage_prefix_bounties_approvals,
138		old_pallet_name,
139		new_pallet_name,
140	);
141
142	let new_pallet_prefix = twox_128(new_pallet_name.as_bytes());
143	let storage_version_key =
144		[&new_pallet_prefix, &twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX)[..]].concat();
145
146	// ensure nothing is stored in the new prefix.
147	assert!(
148		storage::next_key(&new_pallet_prefix).map_or(
149			// either nothing is there
150			true,
151			// or we ensure that the next key has no common prefix with twox_128(new),
152			// or is the pallet version that is already stored using the pallet name
153			|next_key| {
154				storage::next_key(&next_key).map_or(true, |next_key| {
155					!next_key.starts_with(&new_pallet_prefix) || next_key == storage_version_key
156				})
157			},
158		),
159		"unexpected next_key({}) = {:?}",
160		new_pallet_name,
161		HexDisplay::from(&sp_io::storage::next_key(&new_pallet_prefix).unwrap()),
162	);
163	assert!(<P as GetStorageVersion>::on_chain_storage_version() < 4);
164}
165
166/// Some checks for after migration. This can be linked to
167/// `frame_support::traits::OnRuntimeUpgrade::post_upgrade` for further testing.
168///
169/// Panics if anything goes wrong.
170pub fn post_migration<T: pallet_bounties::Config, P: GetStorageVersion, N: AsRef<str>>(
171	old_pallet_name: N,
172	new_pallet_name: N,
173) {
174	let old_pallet_name = old_pallet_name.as_ref();
175	let new_pallet_name = new_pallet_name.as_ref();
176	let storage_prefix_bounties_count = pallet_bounties::BountyCount::<T>::storage_prefix();
177	let storage_prefix_bounties = pallet_bounties::Bounties::<T>::storage_prefix();
178	let storage_prefix_bounties_description =
179		pallet_bounties::BountyDescriptions::<T>::storage_prefix();
180	let storage_prefix_bounties_approvals = pallet_bounties::BountyApprovals::<T>::storage_prefix();
181	log_migration(
182		"post-migration",
183		storage_prefix_bounties_count,
184		old_pallet_name,
185		new_pallet_name,
186	);
187	log_migration("post-migration", storage_prefix_bounties, old_pallet_name, new_pallet_name);
188	log_migration(
189		"post-migration",
190		storage_prefix_bounties_description,
191		old_pallet_name,
192		new_pallet_name,
193	);
194	log_migration(
195		"post-migration",
196		storage_prefix_bounties_approvals,
197		old_pallet_name,
198		new_pallet_name,
199	);
200
201	let old_pallet_prefix = twox_128(old_pallet_name.as_bytes());
202	let old_bounties_count_key =
203		[&old_pallet_prefix, &twox_128(storage_prefix_bounties_count)[..]].concat();
204	let old_bounties_key = [&old_pallet_prefix, &twox_128(storage_prefix_bounties)[..]].concat();
205	let old_bounties_description_key =
206		[&old_pallet_prefix, &twox_128(storage_prefix_bounties_description)[..]].concat();
207	let old_bounties_approvals_key =
208		[&old_pallet_prefix, &twox_128(storage_prefix_bounties_approvals)[..]].concat();
209	assert!(storage::next_key(&old_bounties_count_key)
210		.map_or(true, |next_key| !next_key.starts_with(&old_bounties_count_key)));
211	assert!(storage::next_key(&old_bounties_key)
212		.map_or(true, |next_key| !next_key.starts_with(&old_bounties_key)));
213	assert!(storage::next_key(&old_bounties_description_key)
214		.map_or(true, |next_key| !next_key.starts_with(&old_bounties_description_key)));
215	assert!(storage::next_key(&old_bounties_approvals_key)
216		.map_or(true, |next_key| !next_key.starts_with(&old_bounties_approvals_key)));
217
218	assert_eq!(<P as GetStorageVersion>::on_chain_storage_version(), 4);
219}
220
221fn log_migration(stage: &str, storage_prefix: &[u8], old_pallet_name: &str, new_pallet_name: &str) {
222	log::info!(
223		target: "runtime::bounties",
224		"{} prefix of storage '{}': '{}' ==> '{}'",
225		stage,
226		str::from_utf8(storage_prefix).unwrap_or("<Invalid UTF8>"),
227		old_pallet_name,
228		new_pallet_name,
229	);
230}