use crate::{Config, Configuration, Overweight, Pallet, DEFAULT_POV_SIZE};
use frame_support::{
pallet_prelude::*,
traits::{OnRuntimeUpgrade, StorageVersion},
weights::{constants::WEIGHT_REF_TIME_PER_MILLIS, Weight},
};
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
pub struct Migration<T: Config>(PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for Migration<T> {
fn on_runtime_upgrade() -> Weight {
let mut weight = T::DbWeight::get().reads(1);
if StorageVersion::get::<Pallet<T>>() == 0 {
weight.saturating_accrue(migrate_to_v1::<T>());
StorageVersion::new(1).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}
if StorageVersion::get::<Pallet<T>>() == 1 {
weight.saturating_accrue(migrate_to_v2::<T>());
StorageVersion::new(2).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
}
weight
}
}
mod v0 {
use super::*;
use codec::{Decode, Encode};
#[derive(Decode, Encode, Debug)]
pub struct ConfigData {
pub max_individual: u64,
}
impl Default for ConfigData {
fn default() -> Self {
ConfigData { max_individual: 10u64 * WEIGHT_REF_TIME_PER_MILLIS }
}
}
}
pub fn migrate_to_v1<T: Config>() -> Weight {
let translate = |pre: v0::ConfigData| -> super::ConfigData {
super::ConfigData {
max_individual: Weight::from_parts(pre.max_individual, DEFAULT_POV_SIZE),
}
};
if Configuration::<T>::translate(|pre| pre.map(translate)).is_err() {
log::error!(
target: "dmp_queue",
"unexpected error when performing translation of the QueueConfig type during storage upgrade to v2"
);
}
T::DbWeight::get().reads_writes(1, 1)
}
pub fn migrate_to_v2<T: Config>() -> Weight {
let overweight_messages = Overweight::<T>::initialize_counter() as u64;
T::DbWeight::get().reads_writes(overweight_messages, 1)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::{new_test_ext, Test};
#[test]
fn test_migration_to_v1() {
let v0 = v0::ConfigData { max_individual: 30_000_000_000 };
new_test_ext().execute_with(|| {
frame_support::storage::unhashed::put_raw(
&crate::Configuration::<Test>::hashed_key(),
&v0.encode(),
);
migrate_to_v1::<Test>();
let v1 = crate::Configuration::<Test>::get();
assert_eq!(v0.max_individual, v1.max_individual.ref_time());
assert_eq!(v1.max_individual.proof_size(), DEFAULT_POV_SIZE);
});
}
}