1pub mod v5;
20
21use crate::{Config, OverweightIndex, Pallet, QueueConfig, QueueConfigData, DEFAULT_POV_SIZE};
22use alloc::vec::Vec;
23use cumulus_primitives_core::XcmpMessageFormat;
24use frame_support::{
25 pallet_prelude::*,
26 traits::{EnqueueMessage, StorageVersion, UncheckedOnRuntimeUpgrade},
27 weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight},
28};
29
30pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(5);
32
33pub const LOG: &str = "runtime::xcmp-queue-migration";
34
35mod v1 {
36 use super::*;
37 use codec::{Decode, Encode};
38
39 #[frame_support::storage_alias]
40 pub(crate) type QueueConfig<T: Config> = StorageValue<Pallet<T>, QueueConfigData, ValueQuery>;
41
42 #[derive(Encode, Decode, Debug)]
43 pub struct QueueConfigData {
44 pub suspend_threshold: u32,
45 pub drop_threshold: u32,
46 pub resume_threshold: u32,
47 pub threshold_weight: u64,
48 pub weight_restrict_decay: u64,
49 pub xcmp_max_individual_weight: u64,
50 }
51
52 impl Default for QueueConfigData {
53 fn default() -> Self {
54 QueueConfigData {
55 suspend_threshold: 2,
56 drop_threshold: 5,
57 resume_threshold: 1,
58 threshold_weight: 100_000,
59 weight_restrict_decay: 2,
60 xcmp_max_individual_weight: 20u64 * WEIGHT_REF_TIME_PER_MILLIS,
61 }
62 }
63 }
64}
65
66pub mod v2 {
67 use super::*;
68
69 #[frame_support::storage_alias]
70 pub(crate) type QueueConfig<T: Config> = StorageValue<Pallet<T>, QueueConfigData, ValueQuery>;
71
72 #[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, RuntimeDebug, TypeInfo)]
73 pub struct QueueConfigData {
74 pub suspend_threshold: u32,
75 pub drop_threshold: u32,
76 pub resume_threshold: u32,
77 pub threshold_weight: Weight,
78 pub weight_restrict_decay: Weight,
79 pub xcmp_max_individual_weight: Weight,
80 }
81
82 impl Default for QueueConfigData {
83 fn default() -> Self {
84 Self {
85 suspend_threshold: 2,
86 drop_threshold: 5,
87 resume_threshold: 1,
88 threshold_weight: Weight::from_parts(100_000, 0),
89 weight_restrict_decay: Weight::from_parts(2, 0),
90 xcmp_max_individual_weight: Weight::from_parts(
91 20u64 * WEIGHT_REF_TIME_PER_MILLIS,
92 DEFAULT_POV_SIZE,
93 ),
94 }
95 }
96 }
97
98 pub struct UncheckedMigrationToV2<T: Config>(PhantomData<T>);
101
102 impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV2<T> {
103 #[allow(deprecated)]
104 fn on_runtime_upgrade() -> Weight {
105 let translate = |pre: v1::QueueConfigData| -> v2::QueueConfigData {
106 v2::QueueConfigData {
107 suspend_threshold: pre.suspend_threshold,
108 drop_threshold: pre.drop_threshold,
109 resume_threshold: pre.resume_threshold,
110 threshold_weight: Weight::from_parts(pre.threshold_weight, 0),
111 weight_restrict_decay: Weight::from_parts(pre.weight_restrict_decay, 0),
112 xcmp_max_individual_weight: Weight::from_parts(
113 pre.xcmp_max_individual_weight,
114 DEFAULT_POV_SIZE,
115 ),
116 }
117 };
118
119 if v2::QueueConfig::<T>::translate(|pre| pre.map(translate)).is_err() {
120 tracing::error!(
121 target: crate::LOG_TARGET,
122 "unexpected error when performing translation of the QueueConfig type \
123 during storage upgrade to v2"
124 );
125 }
126
127 T::DbWeight::get().reads_writes(1, 1)
128 }
129 }
130
131 #[allow(dead_code)]
135 pub type MigrationToV2<T> = frame_support::migrations::VersionedMigration<
136 1,
137 2,
138 UncheckedMigrationToV2<T>,
139 Pallet<T>,
140 <T as frame_system::Config>::DbWeight,
141 >;
142}
143
144pub mod v3 {
145 use super::*;
146 use crate::*;
147
148 #[frame_support::storage_alias]
150 pub(crate) type InboundXcmpStatus<T: Config> =
151 StorageValue<Pallet<T>, Vec<InboundChannelDetails>, OptionQuery>;
152
153 #[frame_support::storage_alias]
155 pub(crate) type InboundXcmpMessages<T: Config> = StorageDoubleMap<
156 Pallet<T>,
157 Blake2_128Concat,
158 ParaId,
159 Twox64Concat,
160 RelayBlockNumber,
161 Vec<u8>,
162 OptionQuery,
163 >;
164
165 #[frame_support::storage_alias]
166 pub(crate) type QueueConfig<T: Config> =
167 StorageValue<Pallet<T>, v2::QueueConfigData, ValueQuery>;
168
169 #[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, TypeInfo)]
170 pub struct InboundChannelDetails {
171 pub sender: ParaId,
173 pub state: InboundState,
175 pub message_metadata: Vec<(RelayBlockNumber, XcmpMessageFormat)>,
180 }
181
182 #[derive(
183 Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug, TypeInfo,
184 )]
185 pub enum InboundState {
186 Ok,
187 Suspended,
188 }
189
190 pub struct UncheckedMigrationToV3<T: Config>(PhantomData<T>);
192
193 impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV3<T> {
194 fn on_runtime_upgrade() -> Weight {
195 #[frame_support::storage_alias]
196 type Overweight<T: Config> =
197 CountedStorageMap<Pallet<T>, Twox64Concat, OverweightIndex, ParaId>;
198 let overweight_messages = Overweight::<T>::initialize_counter() as u64;
199
200 T::DbWeight::get().reads_writes(overweight_messages, 1)
201 }
202 }
203
204 pub type MigrationToV3<T> = frame_support::migrations::VersionedMigration<
208 2,
209 3,
210 UncheckedMigrationToV3<T>,
211 Pallet<T>,
212 <T as frame_system::Config>::DbWeight,
213 >;
214
215 pub fn lazy_migrate_inbound_queue<T: Config>() {
216 let Some(mut states) = v3::InboundXcmpStatus::<T>::get() else {
217 tracing::debug!(target: LOG, "Lazy migration finished: item gone");
218 return
219 };
220 let Some(ref mut next) = states.first_mut() else {
221 tracing::debug!(target: LOG, "Lazy migration finished: item empty");
222 v3::InboundXcmpStatus::<T>::kill();
223 return
224 };
225 tracing::debug!(
226 target: LOG,
227 sibling=?next.sender,
228 msgs_left=%next.message_metadata.len(),
229 "Migrating inbound HRMP channel."
230 );
231 let Some((block_number, format)) = next.message_metadata.pop() else {
233 states.remove(0);
234 v3::InboundXcmpStatus::<T>::put(states);
235 return
236 };
237 if format != XcmpMessageFormat::ConcatenatedVersionedXcm {
238 tracing::warn!(
239 target: LOG,
240 ?format,
241 "Dropping message (not ConcatenatedVersionedXcm)"
242 );
243 v3::InboundXcmpMessages::<T>::remove(&next.sender, &block_number);
244 v3::InboundXcmpStatus::<T>::put(states);
245 return
246 }
247
248 let Some(msg) = v3::InboundXcmpMessages::<T>::take(&next.sender, &block_number) else {
249 defensive!("Storage corrupted: HRMP message missing:", (next.sender, block_number));
250 v3::InboundXcmpStatus::<T>::put(states);
251 return
252 };
253
254 let Ok(msg): Result<BoundedVec<_, _>, _> = msg.try_into() else {
255 tracing::error!(target: LOG, "Message dropped: too big");
256 v3::InboundXcmpStatus::<T>::put(states);
257 return
258 };
259
260 T::XcmpQueue::enqueue_message(msg.as_bounded_slice(), next.sender);
262 tracing::debug!(target: LOG, next_sender=?next.sender, ?block_number, "Migrated HRMP message to MQ");
263 v3::InboundXcmpStatus::<T>::put(states);
264 }
265}
266
267pub mod v4 {
268 use super::*;
269
270 pub struct UncheckedMigrationToV4<T: Config>(PhantomData<T>);
273
274 impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV4<T> {
275 fn on_runtime_upgrade() -> Weight {
276 let translate = |pre: v2::QueueConfigData| -> QueueConfigData {
277 let pre_default = v2::QueueConfigData::default();
278 if pre.suspend_threshold == pre_default.suspend_threshold &&
281 pre.drop_threshold == pre_default.drop_threshold &&
282 pre.resume_threshold == pre_default.resume_threshold
283 {
284 return QueueConfigData::default()
285 }
286
287 QueueConfigData {
289 suspend_threshold: pre.suspend_threshold,
290 drop_threshold: pre.drop_threshold,
291 resume_threshold: pre.resume_threshold,
292 }
293 };
294
295 if QueueConfig::<T>::translate(|pre| pre.map(translate)).is_err() {
296 tracing::error!(
297 target: crate::LOG_TARGET,
298 "unexpected error when performing translation of the QueueConfig type \
299 during storage upgrade to v4"
300 );
301 }
302
303 T::DbWeight::get().reads_writes(1, 1)
304 }
305 }
306
307 pub type MigrationToV4<T> = frame_support::migrations::VersionedMigration<
311 3,
312 4,
313 UncheckedMigrationToV4<T>,
314 Pallet<T>,
315 <T as frame_system::Config>::DbWeight,
316 >;
317}
318
319#[cfg(all(feature = "try-runtime", test))]
320mod tests {
321 use super::*;
322 use crate::mock::{new_test_ext, Test};
323 use frame_support::traits::OnRuntimeUpgrade;
324
325 #[test]
326 #[allow(deprecated)]
327 fn test_migration_to_v2() {
328 let v1 = v1::QueueConfigData {
329 suspend_threshold: 5,
330 drop_threshold: 12,
331 resume_threshold: 3,
332 threshold_weight: 333_333,
333 weight_restrict_decay: 1,
334 xcmp_max_individual_weight: 10_000_000_000,
335 };
336
337 new_test_ext().execute_with(|| {
338 let storage_version = StorageVersion::new(1);
339 storage_version.put::<Pallet<Test>>();
340
341 frame_support::storage::unhashed::put_raw(
342 &crate::QueueConfig::<Test>::hashed_key(),
343 &v1.encode(),
344 );
345
346 let bytes = v2::MigrationToV2::<Test>::pre_upgrade();
347 assert!(bytes.is_ok());
348 v2::MigrationToV2::<Test>::on_runtime_upgrade();
349 assert!(v2::MigrationToV2::<Test>::post_upgrade(bytes.unwrap()).is_ok());
350
351 let v2 = v2::QueueConfig::<Test>::get();
352
353 assert_eq!(v1.suspend_threshold, v2.suspend_threshold);
354 assert_eq!(v1.drop_threshold, v2.drop_threshold);
355 assert_eq!(v1.resume_threshold, v2.resume_threshold);
356 assert_eq!(v1.threshold_weight, v2.threshold_weight.ref_time());
357 assert_eq!(v1.weight_restrict_decay, v2.weight_restrict_decay.ref_time());
358 assert_eq!(v1.xcmp_max_individual_weight, v2.xcmp_max_individual_weight.ref_time());
359 });
360 }
361
362 #[test]
363 #[allow(deprecated)]
364 fn test_migration_to_v4() {
365 new_test_ext().execute_with(|| {
366 let storage_version = StorageVersion::new(3);
367 storage_version.put::<Pallet<Test>>();
368
369 let v2 = v2::QueueConfigData {
370 drop_threshold: 5,
371 suspend_threshold: 2,
372 resume_threshold: 1,
373 ..Default::default()
374 };
375
376 frame_support::storage::unhashed::put_raw(
377 &crate::QueueConfig::<Test>::hashed_key(),
378 &v2.encode(),
379 );
380
381 let bytes = v4::MigrationToV4::<Test>::pre_upgrade();
382 assert!(bytes.is_ok());
383 v4::MigrationToV4::<Test>::on_runtime_upgrade();
384 assert!(v4::MigrationToV4::<Test>::post_upgrade(bytes.unwrap()).is_ok());
385
386 let v4 = QueueConfig::<Test>::get();
387
388 assert_eq!(
389 v4,
390 QueueConfigData { suspend_threshold: 32, drop_threshold: 48, resume_threshold: 8 }
391 );
392 });
393
394 new_test_ext().execute_with(|| {
395 let storage_version = StorageVersion::new(3);
396 storage_version.put::<Pallet<Test>>();
397
398 let v2 = v2::QueueConfigData {
399 drop_threshold: 100,
400 suspend_threshold: 50,
401 resume_threshold: 40,
402 ..Default::default()
403 };
404
405 frame_support::storage::unhashed::put_raw(
406 &crate::QueueConfig::<Test>::hashed_key(),
407 &v2.encode(),
408 );
409
410 let bytes = v4::MigrationToV4::<Test>::pre_upgrade();
411 assert!(bytes.is_ok());
412 v4::MigrationToV4::<Test>::on_runtime_upgrade();
413 assert!(v4::MigrationToV4::<Test>::post_upgrade(bytes.unwrap()).is_ok());
414
415 let v4 = QueueConfig::<Test>::get();
416
417 assert_eq!(
418 v4,
419 QueueConfigData {
420 suspend_threshold: 50,
421 drop_threshold: 100,
422 resume_threshold: 40
423 }
424 );
425 });
426 }
427}