referrerpolicy=no-referrer-when-downgrade

pallet_collective/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 sp_io::hashing::twox_128;
19
20use super::super::LOG_TARGET;
21use frame_support::{
22	traits::{
23		Get, GetStorageVersion, PalletInfoAccess, StorageVersion,
24		STORAGE_VERSION_STORAGE_KEY_POSTFIX,
25	},
26	weights::Weight,
27};
28
29/// Migrate the entire storage of this pallet to a new prefix.
30///
31/// This new prefix must be the same as the one set in construct_runtime. For safety, use
32/// `PalletInfo` to get it, as:
33/// `<Runtime as frame_system::Config>::PalletInfo::name::<CollectivePallet>`.
34///
35/// The migration will look into the storage version in order not to trigger a migration on an up
36/// to date storage. Thus the on chain storage version must be less than 4 in order to trigger the
37/// migration.
38pub fn migrate<T: frame_system::Config, P: GetStorageVersion + PalletInfoAccess, N: AsRef<str>>(
39	old_pallet_name: N,
40) -> Weight {
41	let old_pallet_name = old_pallet_name.as_ref();
42	let new_pallet_name = <P as PalletInfoAccess>::name();
43
44	if new_pallet_name == old_pallet_name {
45		log::info!(
46			target: LOG_TARGET,
47			"New pallet name is equal to the old pallet name. No migration needs to be done.",
48		);
49		return Weight::zero()
50	}
51
52	let on_chain_storage_version = <P as GetStorageVersion>::on_chain_storage_version();
53	log::info!(
54		target: LOG_TARGET,
55		"Running migration to v4 for collective with storage version {:?}",
56		on_chain_storage_version,
57	);
58
59	if on_chain_storage_version < 4 {
60		frame_support::storage::migration::move_pallet(
61			old_pallet_name.as_bytes(),
62			new_pallet_name.as_bytes(),
63		);
64		log_migration("migration", old_pallet_name, new_pallet_name);
65
66		StorageVersion::new(4).put::<P>();
67		<T as frame_system::Config>::BlockWeights::get().max_block
68	} else {
69		log::warn!(
70			target: LOG_TARGET,
71			"Attempted to apply migration to v4 but failed because storage version is {:?}",
72			on_chain_storage_version,
73		);
74		Weight::zero()
75	}
76}
77
78/// Some checks prior to migration. This can be linked to
79/// `frame_support::traits::OnRuntimeUpgrade::pre_upgrade` for further testing.
80///
81/// Panics if anything goes wrong.
82pub fn pre_migrate<P: GetStorageVersion + PalletInfoAccess, N: AsRef<str>>(old_pallet_name: N) {
83	let old_pallet_name = old_pallet_name.as_ref();
84	let new_pallet_name = <P as PalletInfoAccess>::name();
85	log_migration("pre-migration", old_pallet_name, new_pallet_name);
86
87	if new_pallet_name == old_pallet_name {
88		return
89	}
90
91	let new_pallet_prefix = twox_128(new_pallet_name.as_bytes());
92	let storage_version_key = twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX);
93
94	let mut new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
95		new_pallet_prefix.to_vec(),
96		new_pallet_prefix.to_vec(),
97		|key| Ok(key.to_vec()),
98	);
99
100	// Ensure nothing except the storage_version_key is stored in the new prefix.
101	assert!(new_pallet_prefix_iter.all(|key| key == storage_version_key));
102
103	assert!(<P as GetStorageVersion>::on_chain_storage_version() < 4);
104}
105
106/// Some checks for after migration. This can be linked to
107/// `frame_support::traits::OnRuntimeUpgrade::post_upgrade` for further testing.
108///
109/// Panics if anything goes wrong.
110pub fn post_migrate<P: GetStorageVersion + PalletInfoAccess, N: AsRef<str>>(old_pallet_name: N) {
111	let old_pallet_name = old_pallet_name.as_ref();
112	let new_pallet_name = <P as PalletInfoAccess>::name();
113	log_migration("post-migration", old_pallet_name, new_pallet_name);
114
115	if new_pallet_name == old_pallet_name {
116		return
117	}
118
119	// Assert that nothing remains at the old prefix.
120	let old_pallet_prefix = twox_128(old_pallet_name.as_bytes());
121	let old_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
122		old_pallet_prefix.to_vec(),
123		old_pallet_prefix.to_vec(),
124		|_| Ok(()),
125	);
126	assert_eq!(old_pallet_prefix_iter.count(), 0);
127
128	// NOTE: storage_version_key is already in the new prefix.
129	let new_pallet_prefix = twox_128(new_pallet_name.as_bytes());
130	let new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
131		new_pallet_prefix.to_vec(),
132		new_pallet_prefix.to_vec(),
133		|_| Ok(()),
134	);
135	assert!(new_pallet_prefix_iter.count() >= 1);
136
137	assert_eq!(<P as GetStorageVersion>::on_chain_storage_version(), 4);
138}
139
140fn log_migration(stage: &str, old_pallet_name: &str, new_pallet_name: &str) {
141	log::info!(
142		target: LOG_TARGET,
143		"{}, prefix: '{}' ==> '{}'",
144		stage,
145		old_pallet_name,
146		new_pallet_name,
147	);
148}