referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_parachain_system/
migration.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use crate::{Config, Pallet, ReservedDmpWeightOverride, ReservedXcmpWeightOverride};
18use frame_support::{
19	pallet_prelude::*,
20	traits::{Get, OnRuntimeUpgrade, StorageVersion},
21	weights::Weight,
22};
23
24/// The in-code storage version.
25pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
26
27/// Migrates the pallet storage to the most recent version.
28pub struct Migration<T: Config>(PhantomData<T>);
29
30impl<T: Config> OnRuntimeUpgrade for Migration<T> {
31	fn on_runtime_upgrade() -> Weight {
32		let mut weight: Weight = T::DbWeight::get().reads(2);
33
34		if StorageVersion::get::<Pallet<T>>() == 0 {
35			weight = weight
36				.saturating_add(v1::migrate::<T>())
37				.saturating_add(T::DbWeight::get().writes(1));
38			StorageVersion::new(1).put::<Pallet<T>>();
39		}
40
41		if StorageVersion::get::<Pallet<T>>() == 1 {
42			weight = weight
43				.saturating_add(v2::migrate::<T>())
44				.saturating_add(T::DbWeight::get().writes(1));
45			StorageVersion::new(2).put::<Pallet<T>>();
46		}
47
48		weight
49	}
50}
51
52/// V2: Migrate to 2D weights for ReservedXcmpWeightOverride and ReservedDmpWeightOverride.
53mod v2 {
54	use super::*;
55	const DEFAULT_POV_SIZE: u64 = 64 * 1024; // 64 KB
56
57	pub fn migrate<T: Config>() -> Weight {
58		let translate = |pre: u64| -> Weight { Weight::from_parts(pre, DEFAULT_POV_SIZE) };
59
60		if ReservedXcmpWeightOverride::<T>::translate(|pre| pre.map(translate)).is_err() {
61			log::error!(
62				target: "parachain_system",
63				"unexpected error when performing translation of the ReservedXcmpWeightOverride type during storage upgrade to v2"
64			);
65		}
66
67		if ReservedDmpWeightOverride::<T>::translate(|pre| pre.map(translate)).is_err() {
68			log::error!(
69				target: "parachain_system",
70				"unexpected error when performing translation of the ReservedDmpWeightOverride type during storage upgrade to v2"
71			);
72		}
73
74		T::DbWeight::get().reads_writes(2, 2)
75	}
76}
77
78/// V1: `LastUpgrade` block number is removed from the storage since the upgrade
79/// mechanism now uses signals instead of block offsets.
80mod v1 {
81	use crate::{Config, Pallet};
82	#[allow(deprecated)]
83	use frame_support::{migration::remove_storage_prefix, pallet_prelude::*};
84
85	pub fn migrate<T: Config>() -> Weight {
86		#[allow(deprecated)]
87		remove_storage_prefix(<Pallet<T>>::name().as_bytes(), b"LastUpgrade", b"");
88		T::DbWeight::get().writes(1)
89	}
90}