referrerpolicy=no-referrer-when-downgrade

pallet_grandpa/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 crate::LOG_TARGET;
19use frame_support::{
20	traits::{Get, StorageVersion},
21	weights::Weight,
22};
23use sp_io::hashing::twox_128;
24
25/// The old prefix.
26pub const OLD_PREFIX: &[u8] = b"GrandpaFinality";
27
28/// Migrate the entire storage of this pallet to a new prefix.
29///
30/// This new prefix must be the same as the one set in construct_runtime. For safety, use
31/// `PalletInfo` to get it, as:
32/// `<Runtime as frame_system::Config>::PalletInfo::name::<GrandpaPallet>`.
33///
34/// The old storage prefix, `GrandpaFinality` is hardcoded in the migration code.
35pub fn migrate<T: crate::Config, N: AsRef<str>>(new_pallet_name: N) -> Weight {
36	if new_pallet_name.as_ref().as_bytes() == OLD_PREFIX {
37		log::info!(
38			target: LOG_TARGET,
39			"New pallet name is equal to the old prefix. No migration needs to be done.",
40		);
41		return Weight::zero()
42	}
43	let storage_version = StorageVersion::get::<crate::Pallet<T>>();
44	log::info!(
45		target: LOG_TARGET,
46		"Running migration to v3.1 for grandpa with storage version {:?}",
47		storage_version,
48	);
49
50	if storage_version <= 3 {
51		log::info!("new prefix: {}", new_pallet_name.as_ref());
52		frame_support::storage::migration::move_pallet(
53			OLD_PREFIX,
54			new_pallet_name.as_ref().as_bytes(),
55		);
56
57		StorageVersion::new(4).put::<crate::Pallet<T>>();
58
59		<T as frame_system::Config>::BlockWeights::get().max_block
60	} else {
61		Weight::zero()
62	}
63}
64
65/// Some checks prior to migration. This can be linked to
66/// `frame_support::traits::OnRuntimeUpgrade::pre_upgrade` for further testing.
67///
68/// Panics if anything goes wrong.
69pub fn pre_migration<T: crate::Config, N: AsRef<str>>(new: N) {
70	let new = new.as_ref();
71	log::info!("pre-migration grandpa test with new = {}", new);
72
73	// the next key must exist, and start with the hash of `OLD_PREFIX`.
74	let next_key = sp_io::storage::next_key(&twox_128(OLD_PREFIX)).unwrap();
75	assert!(next_key.starts_with(&twox_128(OLD_PREFIX)));
76
77	// The pallet version is already stored using the pallet name
78	let storage_key = StorageVersion::storage_key::<crate::Pallet<T>>();
79
80	// ensure nothing is stored in the new prefix.
81	assert!(
82		sp_io::storage::next_key(&twox_128(new.as_bytes())).map_or(
83			// either nothing is there
84			true,
85			// or we ensure that it has no common prefix with twox_128(new),
86			// or isn't the pallet version that is already stored using the pallet name
87			|next_key| {
88				!next_key.starts_with(&twox_128(new.as_bytes())) || next_key == storage_key
89			},
90		),
91		"unexpected next_key({}) = {:?}",
92		new,
93		sp_core::hexdisplay::HexDisplay::from(
94			&sp_io::storage::next_key(&twox_128(new.as_bytes())).unwrap()
95		),
96	);
97	// ensure storage version is 3.
98	assert_eq!(StorageVersion::get::<crate::Pallet<T>>(), 3);
99}
100
101/// Some checks for after migration. This can be linked to
102/// `frame_support::traits::OnRuntimeUpgrade::post_upgrade` for further testing.
103///
104/// Panics if anything goes wrong.
105pub fn post_migration() {
106	log::info!("post-migration grandpa");
107
108	// Assert that nothing remains at the old prefix
109	assert!(sp_io::storage::next_key(&twox_128(OLD_PREFIX))
110		.map_or(true, |next_key| !next_key.starts_with(&twox_128(OLD_PREFIX))));
111}