referrerpolicy=no-referrer-when-downgrade

pallet_elections_phragmen/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
18//! Migrations to version [`4.0.0`], as denoted by the changelog.
19
20use super::super::LOG_TARGET;
21use frame_support::{
22	traits::{Get, StorageVersion},
23	weights::Weight,
24};
25
26/// The old prefix.
27pub const OLD_PREFIX: &[u8] = b"PhragmenElection";
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::<ElectionsPhragmenPallet>`.
34///
35/// The old storage prefix, `PhragmenElection` is hardcoded in the migration code.
36pub fn migrate<T: crate::Config, N: AsRef<str>>(new_pallet_name: N) -> Weight {
37	if new_pallet_name.as_ref().as_bytes() == OLD_PREFIX {
38		log::info!(
39			target: LOG_TARGET,
40			"New pallet name is equal to the old prefix. No migration needs to be done.",
41		);
42		return Weight::zero()
43	}
44	let storage_version = StorageVersion::get::<crate::Pallet<T>>();
45	log::info!(
46		target: LOG_TARGET,
47		"Running migration to v4 for elections-phragmen with storage version {:?}",
48		storage_version,
49	);
50
51	if storage_version <= 3 {
52		log::info!("new prefix: {}", new_pallet_name.as_ref());
53		frame_support::storage::migration::move_pallet(
54			OLD_PREFIX,
55			new_pallet_name.as_ref().as_bytes(),
56		);
57
58		StorageVersion::new(4).put::<crate::Pallet<T>>();
59
60		<T as frame_system::Config>::BlockWeights::get().max_block
61	} else {
62		log::warn!(
63			target: LOG_TARGET,
64			"Attempted to apply migration to v4 but failed because storage version is {:?}",
65			storage_version,
66		);
67		Weight::zero()
68	}
69}
70
71/// Some checks prior to migration. This can be linked to
72/// `frame_support::traits::OnRuntimeUpgrade::pre_upgrade` for further testing.
73///
74/// Panics if anything goes wrong.
75pub fn pre_migration<T: crate::Config, N: AsRef<str>>(new: N) {
76	let new = new.as_ref();
77	log::info!("pre-migration elections-phragmen test with new = {}", new);
78
79	// the next key must exist, and start with the hash of `OLD_PREFIX`.
80	let next_key = sp_io::storage::next_key(OLD_PREFIX).unwrap();
81	assert!(next_key.starts_with(&sp_io::hashing::twox_128(OLD_PREFIX)));
82
83	// ensure nothing is stored in the new prefix.
84	assert!(
85		sp_io::storage::next_key(new.as_bytes()).map_or(
86			// either nothing is there
87			true,
88			// or we ensure that it has no common prefix with twox_128(new).
89			|next_key| !next_key.starts_with(&sp_io::hashing::twox_128(new.as_bytes()))
90		),
91		"unexpected next_key({}) = {:?}",
92		new,
93		sp_core::hexdisplay::HexDisplay::from(&sp_io::storage::next_key(new.as_bytes()).unwrap())
94	);
95	// ensure storage version is 3.
96	assert_eq!(StorageVersion::get::<crate::Pallet<T>>(), 3);
97}
98
99/// Some checks for after migration. This can be linked to
100/// `frame_support::traits::OnRuntimeUpgrade::post_upgrade` for further testing.
101///
102/// Panics if anything goes wrong.
103pub fn post_migration<T: crate::Config>() {
104	log::info!("post-migration elections-phragmen");
105	// ensure we've been updated to v4 by the automatic write of crate version -> storage version.
106	assert_eq!(StorageVersion::get::<crate::Pallet<T>>(), 4);
107}