Skip to content

Commit

Permalink
test that all storage entries are migrated
Browse files Browse the repository at this point in the history
  • Loading branch information
Roznovjak committed Jul 27, 2023
1 parent 51246c5 commit c578257
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 32 deletions.
121 changes: 94 additions & 27 deletions pallets/transaction-pause/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use super::*;
use frame_support::{
log, storage_alias,
traits::{Get, StorageVersion},
traits::{Get, OnRuntimeUpgrade, StorageVersion},
weights::Weight,
};

Expand All @@ -35,43 +35,110 @@ pub mod v0 {
pub mod v1 {
use super::*;

pub fn pre_migrate<T: Config>() {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 0, "Storage version too high.");
pub struct Migration<T>(PhantomData<T>);

log::info!(target: TARGET, "Transaction pause migration: PRE checks successful!");
}
impl<T: Config> OnRuntimeUpgrade for Migration<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 0, "Storage version too high.");

let iter = v0::PausedTransactions::<T>::iter_keys();

log::info!(target: TARGET, "Transaction pause migration: PRE checks successful!");

Ok(iter.collect::<Vec<(Vec<u8>, Vec<u8>)>>().encode())
}

fn on_runtime_upgrade() -> Weight {
log::info!(target: TARGET, "Running migration to v1 for Transaction pause");

pub fn migrate<T: Config>() -> 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::<T>::drain().collect::<Vec<_>>();
weight.saturating_accrue(T::DbWeight::get().reads(status.len() as u64));

let status = v0::PausedTransactions::<T>::drain().collect::<Vec<_>>();
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::<u8, ConstU32<MAX_STR_LENGTH>>::try_from(pallet_name.clone());
let function_name_b = BoundedVec::<u8, ConstU32<MAX_STR_LENGTH>>::try_from(function_name.clone());
if pallet_name_b.is_err() || function_name_b.is_err() {
log::info!(
target: TARGET,

Check warning on line 65 in pallets/transaction-pause/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/transaction-pause/src/migration.rs#L64-L65

Added lines #L64 - L65 were not covered by tests
"Value not migrated because it's too long: {:?}",
(pallet_name_b, function_name_b)

Check warning on line 67 in pallets/transaction-pause/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/transaction-pause/src/migration.rs#L67

Added line #L67 was not covered by tests
);
continue;

Check warning on line 69 in pallets/transaction-pause/src/migration.rs

View check run for this annotation

Codecov / codecov/patch

pallets/transaction-pause/src/migration.rs#L69

Added line #L69 was not covered by tests
}

for ((pallet_name, function_name), _) in status.into_iter() {
let pallet_name_b = BoundedVec::<u8, ConstU32<MAX_STR_LENGTH>>::try_from(pallet_name.clone());
let function_name_b = BoundedVec::<u8, ConstU32<MAX_STR_LENGTH>>::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::<T>::insert((pallet_name_b.unwrap(), function_name_b.unwrap()), ());
}

PausedTransactions::<T>::insert((pallet_name_b.unwrap(), function_name_b.unwrap()), ());
StorageVersion::new(1).put::<Pallet<T>>();

T::DbWeight::get().reads_writes(1, 1)
}

StorageVersion::new(1).put::<Pallet<T>>();
#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 1, "Unexpected storage version.");

T::DbWeight::get().reads_writes(1, 1)
}
let previous_state = <Vec<(Vec<u8>, Vec<u8>)> as codec::Decode>::decode(&mut state.as_slice()).unwrap();

pub fn post_migrate<T: Config>() {
assert_eq!(StorageVersion::get::<Pallet<T>>(), 1, "Unexpected storage version.");
let new_state = crate::PausedTransactions::<T>::iter_keys()
.map(|v| (v.0.into_inner(), v.1.into_inner()))
.collect::<Vec<(Vec<u8>, Vec<u8>)>>();

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::<Pallet<T>>(), 0);

v0::PausedTransactions::<T>::insert(
("first pallet".as_bytes().to_vec(), "first function".as_bytes().to_vec()),
(),
);
v0::PausedTransactions::<T>::insert(
(
"second pallet".as_bytes().to_vec(),
"second function".as_bytes().to_vec(),
),
(),
);

let state = v1::Migration::<T>::pre_upgrade().unwrap();
let _w = v1::Migration::<T>::on_runtime_upgrade();
v1::Migration::<T>::post_upgrade(state).unwrap();

assert_eq!(StorageVersion::get::<Pallet<T>>(), 1);

assert_eq!(
crate::PausedTransactions::<T>::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::<T>::get((
BoundedName::try_from("second pallet".as_bytes().to_vec()).unwrap(),
BoundedName::try_from("second function".as_bytes().to_vec()).unwrap()
)),
Some(())
);
});
}
}
11 changes: 6 additions & 5 deletions runtime/hydradx/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
log::info!("PreMigrate Transaction Pause Pallet start");
pallet_transaction_pause::migration::v1::pre_migrate::<Runtime>();
let tx_pause_state = pallet_transaction_pause::migration::v1::Migration::<Runtime>::pre_upgrade()?;
log::info!("PreMigrate Transaction Pause Pallet end");

Check warning on line 16 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L14-L16

Added lines #L14 - L16 were not covered by tests

log::info!("PreMigrate Collator Rewards Pallet start");
Expand All @@ -23,14 +23,15 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration {
pallet_genesis_history::migration::v1::pre_migrate::<Runtime>();
log::info!("PreMigrate Genesis History Pallet end");

Ok(vec![])
Ok(tx_pause_state)

Check warning on line 26 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L26

Added line #L26 was not covered by tests
}

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::<Runtime>());
weight =
weight.saturating_add(pallet_transaction_pause::migration::v1::Migration::<Runtime>::on_runtime_upgrade());
log::info!("Migrate Transaction Pause Pallet to v1 end");

Check warning on line 35 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L32-L35

Added lines #L32 - L35 were not covered by tests

log::info!("Migrate Collator Rewards Pallet to v1 start");
Expand All @@ -45,9 +46,9 @@ impl OnRuntimeUpgrade for OnRuntimeUpgradeMigration {
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
log::info!("PostMigrate Transaction Pause Pallet start");
pallet_transaction_pause::migration::v1::post_migrate::<Runtime>();
pallet_transaction_pause::migration::v1::Migration::<Runtime>::post_upgrade(state)?;
log::info!("PostMigrate Transaction Pause Pallet end");

Check warning on line 52 in runtime/hydradx/src/migrations.rs

View check run for this annotation

Codecov / codecov/patch

runtime/hydradx/src/migrations.rs#L49-L52

Added lines #L49 - L52 were not covered by tests

log::info!("PostMigrate Collator Rewards Pallet start");
Expand Down

0 comments on commit c578257

Please sign in to comment.