From c578257db3ee169076c9b2298f8a06b26814b64e Mon Sep 17 00:00:00 2001 From: Roznovjak Date: Thu, 27 Jul 2023 20:49:31 +0200 Subject: [PATCH] test that all storage entries are migrated --- pallets/transaction-pause/src/migration.rs | 121 ++++++++++++++++----- runtime/hydradx/src/migrations.rs | 11 +- 2 files changed, 100 insertions(+), 32 deletions(-) diff --git a/pallets/transaction-pause/src/migration.rs b/pallets/transaction-pause/src/migration.rs index e111d8ab7..75dccfca7 100644 --- a/pallets/transaction-pause/src/migration.rs +++ b/pallets/transaction-pause/src/migration.rs @@ -18,7 +18,7 @@ use super::*; use frame_support::{ log, storage_alias, - traits::{Get, StorageVersion}, + traits::{Get, OnRuntimeUpgrade, StorageVersion}, weights::Weight, }; @@ -35,43 +35,110 @@ pub mod v0 { pub mod v1 { use super::*; - pub fn pre_migrate() { - assert_eq!(StorageVersion::get::>(), 0, "Storage version too high."); + pub struct Migration(PhantomData); - log::info!(target: TARGET, "Transaction pause migration: PRE checks successful!"); - } + impl OnRuntimeUpgrade for Migration { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + assert_eq!(StorageVersion::get::>(), 0, "Storage version too high."); + + let iter = v0::PausedTransactions::::iter_keys(); + + log::info!(target: TARGET, "Transaction pause migration: PRE checks successful!"); + + Ok(iter.collect::, Vec)>>().encode()) + } + + fn on_runtime_upgrade() -> Weight { + log::info!(target: TARGET, "Running migration to v1 for Transaction pause"); - pub fn migrate() -> Weight { - log::info!(target: TARGET, "Running migration to v1 for Transaction pause"); + let mut weight = Weight::zero(); - let mut weight = Weight::zero(); + let status = v0::PausedTransactions::::drain().collect::>(); + weight.saturating_accrue(T::DbWeight::get().reads(status.len() as u64)); - let status = v0::PausedTransactions::::drain().collect::>(); - weight.saturating_accrue(T::DbWeight::get().reads(status.len() as u64)); + for ((pallet_name, function_name), _) in status.into_iter() { + let pallet_name_b = BoundedVec::>::try_from(pallet_name.clone()); + let function_name_b = BoundedVec::>::try_from(function_name.clone()); + if pallet_name_b.is_err() || function_name_b.is_err() { + log::info!( + target: TARGET, + "Value not migrated because it's too long: {:?}", + (pallet_name_b, function_name_b) + ); + continue; + } - for ((pallet_name, function_name), _) in status.into_iter() { - let pallet_name_b = BoundedVec::>::try_from(pallet_name.clone()); - let function_name_b = BoundedVec::>::try_from(function_name.clone()); - if pallet_name_b.is_err() || function_name_b.is_err() { - log::info!( - target: TARGET, - "Value not migrated because it's too long: {:?}", - (pallet_name_b, function_name_b) - ); - continue; + crate::PausedTransactions::::insert((pallet_name_b.unwrap(), function_name_b.unwrap()), ()); } - PausedTransactions::::insert((pallet_name_b.unwrap(), function_name_b.unwrap()), ()); + StorageVersion::new(1).put::>(); + + T::DbWeight::get().reads_writes(1, 1) } - StorageVersion::new(1).put::>(); + #[cfg(feature = "try-runtime")] + fn post_upgrade(state: Vec) -> Result<(), &'static str> { + assert_eq!(StorageVersion::get::>(), 1, "Unexpected storage version."); - T::DbWeight::get().reads_writes(1, 1) - } + let previous_state = , Vec)> as codec::Decode>::decode(&mut state.as_slice()).unwrap(); - pub fn post_migrate() { - assert_eq!(StorageVersion::get::>(), 1, "Unexpected storage version."); + let new_state = crate::PausedTransactions::::iter_keys() + .map(|v| (v.0.into_inner(), v.1.into_inner())) + .collect::, Vec)>>(); - log::info!(target: TARGET, "Transaction pause migration: POST checks successful!"); + assert_eq!(previous_state, new_state, "Migrated storage entries don't match the entries prior migration!"); + + log::info!(target: TARGET, "Transaction pause migration: POST checks successful!"); + + Ok(()) + } + } +} + +#[cfg(test)] +#[cfg(feature = "try-runtime")] +mod test { + use super::*; + use crate::mock::{Runtime as T, *}; + + #[test] + fn migration_works() { + ExtBuilder::default().build().execute_with(|| { + assert_eq!(StorageVersion::get::>(), 0); + + v0::PausedTransactions::::insert( + ("first pallet".as_bytes().to_vec(), "first function".as_bytes().to_vec()), + (), + ); + v0::PausedTransactions::::insert( + ( + "second pallet".as_bytes().to_vec(), + "second function".as_bytes().to_vec(), + ), + (), + ); + + let state = v1::Migration::::pre_upgrade().unwrap(); + let _w = v1::Migration::::on_runtime_upgrade(); + v1::Migration::::post_upgrade(state).unwrap(); + + assert_eq!(StorageVersion::get::>(), 1); + + assert_eq!( + crate::PausedTransactions::::get(( + BoundedName::try_from("first pallet".as_bytes().to_vec()).unwrap(), + BoundedName::try_from("first function".as_bytes().to_vec()).unwrap() + )), + Some(()) + ); + assert_eq!( + crate::PausedTransactions::::get(( + BoundedName::try_from("second pallet".as_bytes().to_vec()).unwrap(), + BoundedName::try_from("second function".as_bytes().to_vec()).unwrap() + )), + Some(()) + ); + }); } } diff --git a/runtime/hydradx/src/migrations.rs b/runtime/hydradx/src/migrations.rs index 8da441891..020cbfcd1 100644 --- a/runtime/hydradx/src/migrations.rs +++ b/runtime/hydradx/src/migrations.rs @@ -12,7 +12,7 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { log::info!("PreMigrate Transaction Pause Pallet start"); - pallet_transaction_pause::migration::v1::pre_migrate::(); + let tx_pause_state = pallet_transaction_pause::migration::v1::Migration::::pre_upgrade()?; log::info!("PreMigrate Transaction Pause Pallet end"); log::info!("PreMigrate Collator Rewards Pallet start"); @@ -23,14 +23,15 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration { pallet_genesis_history::migration::v1::pre_migrate::(); log::info!("PreMigrate Genesis History Pallet end"); - Ok(vec![]) + Ok(tx_pause_state) } fn on_runtime_upgrade() -> Weight { let mut weight: Weight = Weight::zero(); log::info!("Migrate Transaction Pause Pallet to v1 start"); - weight = weight.saturating_add(pallet_transaction_pause::migration::v1::migrate::()); + weight = + weight.saturating_add(pallet_transaction_pause::migration::v1::Migration::::on_runtime_upgrade()); log::info!("Migrate Transaction Pause Pallet to v1 end"); log::info!("Migrate Collator Rewards Pallet to v1 start"); @@ -45,9 +46,9 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration { } #[cfg(feature = "try-runtime")] - fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + fn post_upgrade(state: Vec) -> Result<(), &'static str> { log::info!("PostMigrate Transaction Pause Pallet start"); - pallet_transaction_pause::migration::v1::post_migrate::(); + pallet_transaction_pause::migration::v1::Migration::::post_upgrade(state)?; log::info!("PostMigrate Transaction Pause Pallet end"); log::info!("PostMigrate Collator Rewards Pallet start");