referrerpolicy=no-referrer-when-downgrade

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