referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_xcmp_queue/migration/
v7.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
17//! Migrates the storage to version 7.
18
19use crate::*;
20use frame_support::{pallet_prelude::*, traits::UncheckedOnRuntimeUpgrade};
21
22/// [`VersionedMigration`](frame_support::migrations::VersionedMigration), that is performed only
23/// when the on-chain version is 6.
24pub type MigrateV6ToV7<T> = frame_support::migrations::VersionedMigration<
25	6,
26	7,
27	unversioned::UncheckedMigrateV6ToV7<T>,
28	Pallet<T>,
29	<T as frame_system::Config>::DbWeight,
30>;
31
32// V6 storage aliases
33mod v6 {
34	use super::*;
35
36	#[derive(Clone, Eq, PartialEq, Encode, Decode, TypeInfo, Debug, MaxEncodedLen)]
37	pub struct OutboundChannelDetails {
38		pub recipient: ParaId,
39		pub state: OutboundState,
40		pub signals_exist: bool,
41		pub first_index: u16,
42		pub last_index: u16,
43		pub flags: OutboundChannelFlags,
44	}
45
46	#[frame_support::storage_alias]
47	pub(super) type OutboundXcmpStatus<T: Config> = StorageValue<
48		Pallet<T>,
49		BoundedVec<OutboundChannelDetails, <T as Config>::MaxActiveOutboundChannels>,
50		ValueQuery,
51	>;
52}
53
54mod unversioned {
55	use super::*;
56	pub struct UncheckedMigrateV6ToV7<T: Config>(PhantomData<T>);
57}
58
59impl<T: Config> UncheckedOnRuntimeUpgrade for unversioned::UncheckedMigrateV6ToV7<T> {
60	fn on_runtime_upgrade() -> frame_support::weights::Weight {
61		// We use `Vec` instead of `BoundedVec` for `pre` in order to avoid any decoding error
62		// in case `T::MaxActiveOutboundChannels` is decreased in the same runtime upgrade where
63		// the migration is executed.
64		let translate = |pre: Vec<v6::OutboundChannelDetails>|
65		 -> BoundedVec<OutboundChannelDetails, T::MaxActiveOutboundChannels> {
66			BoundedVec::defensive_truncate_from(
67				pre.iter()
68					.map(|pre_channel_details| OutboundChannelDetails {
69						recipient: pre_channel_details.recipient,
70						state: pre_channel_details.state,
71						signals_exist: pre_channel_details.signals_exist,
72						first_index: pre_channel_details.first_index,
73						last_index: pre_channel_details.last_index,
74						flags: pre_channel_details.flags,
75						// Corrects itself over time.
76						queued_bytes: 0,
77					})
78					.collect(),
79			)
80		};
81
82		if OutboundXcmpStatus::<T>::translate(|pre| pre.map(translate)).is_err() {
83			defensive!(
84				"unexpected error when performing translation of the OutboundXcmpStatus type \
85				during storage upgrade to v7"
86			);
87		}
88
89		let proof_size = 2 * BoundedVec::<
90			OutboundChannelDetails,
91			<T as Config>::MaxActiveOutboundChannels,
92		>::max_encoded_len();
93		Weight::from_parts(0, proof_size as u64)
94			.saturating_add(T::DbWeight::get().reads_writes(1, 1))
95	}
96}