referrerpolicy=no-referrer-when-downgrade

cumulus_pallet_dmp_queue/
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
17//! Migrates the storage from the previously deleted DMP pallet.
18
19use crate::*;
20use alloc::vec::Vec;
21use cumulus_primitives_core::relay_chain::BlockNumber as RelayBlockNumber;
22use frame_support::{pallet_prelude::*, storage_alias, traits::HandleMessage};
23
24pub(crate) const LOG: &str = "runtime::dmp-queue-export-xcms";
25
26/// The old `PageIndexData` struct.
27#[derive(Copy, Clone, Eq, PartialEq, Default, Encode, Decode, RuntimeDebug, TypeInfo)]
28pub struct PageIndexData {
29	/// The lowest used page index.
30	pub begin_used: PageCounter,
31	/// The lowest unused page index.
32	pub end_used: PageCounter,
33	/// The number of overweight messages ever recorded (and thus the lowest free index).
34	pub overweight_count: OverweightIndex,
35}
36
37/// The old `MigrationState` type.
38pub type OverweightIndex = u64;
39/// The old `MigrationState` type.
40pub type PageCounter = u32;
41
42/// The old `PageIndex` storage item.
43#[storage_alias]
44pub type PageIndex<T: Config> = StorageValue<Pallet<T>, PageIndexData, ValueQuery>;
45
46/// The old `Pages` storage item.
47#[storage_alias]
48pub type Pages<T: Config> = StorageMap<
49	Pallet<T>,
50	Blake2_128Concat,
51	PageCounter,
52	Vec<(RelayBlockNumber, Vec<u8>)>,
53	ValueQuery,
54>;
55
56/// The old `Overweight` storage item.
57#[storage_alias]
58pub type Overweight<T: Config> = CountedStorageMap<
59	Pallet<T>,
60	Blake2_128Concat,
61	OverweightIndex,
62	(RelayBlockNumber, Vec<u8>),
63	OptionQuery,
64>;
65
66pub(crate) mod testing_only {
67	use super::*;
68
69	/// This alias is not used by the migration but only for testing.
70	///
71	/// Note that the alias type is wrong on purpose.
72	#[storage_alias]
73	pub type Configuration<T: Config> = StorageValue<Pallet<T>, u32>;
74}
75
76/// Migrates a single page to the `DmpSink`.
77pub(crate) fn migrate_page<T: crate::Config>(p: PageCounter) -> Result<(), ()> {
78	let page = Pages::<T>::take(p);
79	log::debug!(target: LOG, "Migrating page #{p} with {} messages ...", page.len());
80	if page.is_empty() {
81		log::error!(target: LOG, "Page #{p}: EMPTY - storage corrupted?");
82		return Err(())
83	}
84
85	for (m, (block, msg)) in page.iter().enumerate() {
86		let Ok(bound) = BoundedVec::<u8, _>::try_from(msg.clone()) else {
87			log::error!(target: LOG, "[Page {p}] Message #{m}: TOO LONG - dropping");
88			continue
89		};
90
91		T::DmpSink::handle_message(bound.as_bounded_slice());
92		log::debug!(target: LOG, "[Page {p}] Migrated message #{m} from block {block}");
93	}
94
95	Ok(())
96}
97
98/// Migrates a single overweight message to the `DmpSink`.
99pub(crate) fn migrate_overweight<T: crate::Config>(i: OverweightIndex) -> Result<(), ()> {
100	let Some((block, msg)) = Overweight::<T>::take(i) else {
101		log::error!(target: LOG, "[Overweight {i}] Message: EMPTY - storage corrupted?");
102		return Err(())
103	};
104	let Ok(bound) = BoundedVec::<u8, _>::try_from(msg) else {
105		log::error!(target: LOG, "[Overweight {i}] Message: TOO LONG - dropping");
106		return Err(())
107	};
108
109	T::DmpSink::handle_message(bound.as_bounded_slice());
110	log::debug!(target: LOG, "[Overweight {i}] Migrated message from block {block}");
111
112	Ok(())
113}