polkadot_runtime_parachains/disputes/
migration.rs1use frame_support::traits::StorageVersion;
20
21pub mod v1 {
22 use super::*;
23 use crate::disputes::{Config, Pallet};
24 use alloc::vec::Vec;
25 use frame_support::{
26 pallet_prelude::*, storage_alias, traits::OnRuntimeUpgrade, weights::Weight,
27 };
28 use polkadot_primitives::SessionIndex;
29
30 #[storage_alias]
31 type SpamSlots<T: Config> = StorageMap<Pallet<T>, Twox64Concat, SessionIndex, Vec<u32>>;
32
33 pub struct MigrateToV1<T>(core::marker::PhantomData<T>);
34 impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
35 fn on_runtime_upgrade() -> Weight {
36 let mut weight: Weight = Weight::zero();
37
38 if StorageVersion::get::<Pallet<T>>() < 1 {
39 log::info!(target: crate::disputes::LOG_TARGET, "Migrating disputes storage to v1");
40 weight += migrate_to_v1::<T>();
41 StorageVersion::new(1).put::<Pallet<T>>();
42 weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));
43 } else {
44 log::info!(
45 target: crate::disputes::LOG_TARGET,
46 "Disputes storage up to date - no need for migration"
47 );
48 }
49
50 weight
51 }
52
53 #[cfg(feature = "try-runtime")]
54 fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
55 log::trace!(
56 target: crate::disputes::LOG_TARGET,
57 "SpamSlots before migration: {}",
58 SpamSlots::<T>::iter().count()
59 );
60 ensure!(
61 StorageVersion::get::<Pallet<T>>() == 0,
62 "Storage version should be less than `1` before the migration",
63 );
64 Ok(Vec::new())
65 }
66
67 #[cfg(feature = "try-runtime")]
68 fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
69 log::trace!(target: crate::disputes::LOG_TARGET, "Running post_upgrade()");
70 ensure!(
71 StorageVersion::get::<Pallet<T>>() >= 1,
72 "Storage version should be `1` after the migration"
73 );
74 ensure!(
75 SpamSlots::<T>::iter().count() == 0,
76 "SpamSlots should be empty after the migration"
77 );
78 Ok(())
79 }
80 }
81
82 pub fn migrate_to_v1<T: Config>() -> Weight {
85 let mut weight: Weight = Weight::zero();
86
87 let res = SpamSlots::<T>::clear(u32::MAX, None);
89 weight = weight
93 .saturating_add(T::DbWeight::get().reads_writes(res.loops as u64, res.backend as u64));
94
95 weight
96 }
97}