|
| 1 | +use crate::{Config, EpochLength, Pallet}; |
| 2 | +use frame_support::{ |
| 3 | + pallet_prelude::{GetStorageVersion, Weight}, |
| 4 | + traits::{Get, OnRuntimeUpgrade, StorageVersion}, |
| 5 | +}; |
| 6 | +use frame_system::pallet_prelude::BlockNumberFor; |
| 7 | + |
| 8 | +const LOG_TARGET: &str = "runtime::capacity"; |
| 9 | + |
| 10 | +#[cfg(feature = "try-runtime")] |
| 11 | +use sp_std::vec::Vec; |
| 12 | + |
| 13 | +/// The OnRuntimeUpgrade implementation for this storage migration |
| 14 | +pub struct MigrationToV4<T>(sp_std::marker::PhantomData<T>); |
| 15 | +impl<T> MigrationToV4<T> |
| 16 | +where |
| 17 | + T: Config, |
| 18 | +{ |
| 19 | + /// Update the epoch length to double the current value |
| 20 | + pub fn update_epoch_length() -> Weight { |
| 21 | + let current_epoch_length = EpochLength::<T>::get(); |
| 22 | + let new_epoch_length: BlockNumberFor<T> = current_epoch_length * 2u32.into(); |
| 23 | + log::info!(target: LOG_TARGET, "🔄 Capacity EpochLength update from {:?} to {:?}", current_epoch_length, new_epoch_length); |
| 24 | + |
| 25 | + EpochLength::<T>::put(new_epoch_length); |
| 26 | + |
| 27 | + T::DbWeight::get().reads_writes(1, 1) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl<T: Config> OnRuntimeUpgrade for MigrationToV4<T> |
| 32 | +where |
| 33 | + T: Config, |
| 34 | +{ |
| 35 | + fn on_runtime_upgrade() -> Weight { |
| 36 | + let on_chain_version = Pallet::<T>::on_chain_storage_version(); // 1r |
| 37 | + |
| 38 | + if on_chain_version.ge(&4) { |
| 39 | + log::info!(target: LOG_TARGET, "Old Capacity EpochLength migration attempted to run. Please remove"); |
| 40 | + return T::DbWeight::get().reads(1); |
| 41 | + } |
| 42 | + |
| 43 | + log::info!(target: LOG_TARGET, "🔄 Capacity EpochLength update migration started"); |
| 44 | + // The migration started with 1r to get the STORAGE_VERSION |
| 45 | + let mut total_weight = T::DbWeight::get().reads_writes(1, 0); |
| 46 | + |
| 47 | + total_weight += Self::update_epoch_length(); |
| 48 | + |
| 49 | + StorageVersion::new(4).put::<Pallet<T>>(); // 1 w |
| 50 | + |
| 51 | + total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(0, 1)); |
| 52 | + |
| 53 | + log::info!(target: LOG_TARGET, "🔄 Capacity EpochLength second update migration finished"); |
| 54 | + |
| 55 | + total_weight |
| 56 | + } |
| 57 | + |
| 58 | + #[cfg(feature = "try-runtime")] |
| 59 | + fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> { |
| 60 | + use frame_support::storage::generator::StorageValue; |
| 61 | + use sp_std::vec; |
| 62 | + let on_chain_version = Pallet::<T>::on_chain_storage_version(); |
| 63 | + if on_chain_version >= 4 { |
| 64 | + return Ok(Vec::new()); |
| 65 | + } |
| 66 | + |
| 67 | + let pallet_prefix = EpochLength::<T>::pallet_prefix(); |
| 68 | + let storage_prefix = EpochLength::<T>::storage_prefix(); |
| 69 | + assert_eq!(&b"Capacity"[..], pallet_prefix); |
| 70 | + assert_eq!(&b"EpochLength"[..], storage_prefix); |
| 71 | + log::info!(target: LOG_TARGET, "Running pre_upgrade..."); |
| 72 | + |
| 73 | + let current_epoch_length = EpochLength::<T>::get(); |
| 74 | + |
| 75 | + #[cfg(feature = "frequency")] |
| 76 | + assert_eq!(current_epoch_length, 7_200u32.into()); |
| 77 | + #[cfg(feature = "frequency-testnet")] |
| 78 | + assert_eq!(current_epoch_length, 100u32.into()); |
| 79 | + |
| 80 | + log::info!(target: LOG_TARGET, "Finish pre_upgrade for with current epoch length {:?} to ", current_epoch_length,); |
| 81 | + Ok(vec![]) |
| 82 | + } |
| 83 | + |
| 84 | + #[cfg(feature = "try-runtime")] |
| 85 | + fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> { |
| 86 | + let on_chain_version = Pallet::<T>::on_chain_storage_version(); |
| 87 | + if on_chain_version >= 4 { |
| 88 | + return Ok(()); |
| 89 | + } |
| 90 | + |
| 91 | + assert_eq!(on_chain_version, crate::pallet::STORAGE_VERSION); |
| 92 | + |
| 93 | + #[cfg(feature = "frequency")] |
| 94 | + { |
| 95 | + let post_upgrade_epoch_length = EpochLength::<T>::get(); |
| 96 | + assert_eq!(post_upgrade_epoch_length, 14_400u32.into()); |
| 97 | + } |
| 98 | + |
| 99 | + #[cfg(feature = "frequency-testnet")] |
| 100 | + { |
| 101 | + let post_upgrade_epoch_length = EpochLength::<T>::get(); |
| 102 | + assert_eq!(post_upgrade_epoch_length, 200u32.into()); |
| 103 | + } |
| 104 | + |
| 105 | + log::info!(target: LOG_TARGET, "✅ migration post_upgrade checks passed"); |
| 106 | + Ok(()) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +#[cfg(test)] |
| 111 | +mod test { |
| 112 | + use super::*; |
| 113 | + use crate::tests::mock::{Test as T, *}; |
| 114 | + |
| 115 | + type MigrationOf<T> = MigrationToV4<T>; |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn migration_works() { |
| 119 | + new_test_ext().execute_with(|| { |
| 120 | + EpochLength::<T>::put(7_200u32); |
| 121 | + |
| 122 | + assert_eq!(EpochLength::<T>::get(), 7_200u32); |
| 123 | + |
| 124 | + MigrationOf::<T>::on_runtime_upgrade(); |
| 125 | + |
| 126 | + let on_chain_version = Pallet::<T>::on_chain_storage_version(); |
| 127 | + assert_eq!(on_chain_version, crate::pallet::STORAGE_VERSION); |
| 128 | + |
| 129 | + assert_eq!(EpochLength::<T>::get(), 14_400u32); |
| 130 | + }) |
| 131 | + } |
| 132 | +} |
0 commit comments