From 6b764fbe4ea7db535215738429bb861ee25c9cec Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Thu, 28 Mar 2024 17:27:26 +0800 Subject: [PATCH] Clean up residual data from migration Signed-off-by: Xavier Lau --- runtime/crab/src/migration.rs | 55 +++++++++++++++++++++++++++++-- runtime/darwinia/src/migration.rs | 52 ++++++++++++++++++++++++++++- runtime/pangolin/src/migration.rs | 48 ++++++++++++++++++++++++++- 3 files changed, 151 insertions(+), 4 deletions(-) diff --git a/runtime/crab/src/migration.rs b/runtime/crab/src/migration.rs index 1074df6c0..59dc369f0 100644 --- a/runtime/crab/src/migration.rs +++ b/runtime/crab/src/migration.rs @@ -27,11 +27,19 @@ pub struct CustomOnRuntimeUpgrade; impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + assert!(Balances::free_balance(ROOT) != 0); + Ok(Vec::new()) } #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + assert!(Balances::free_balance(ROOT) == 0); + + >::iter_values().for_each(|v| { + assert!(!v.is_empty()); + }); + Ok(()) } @@ -41,9 +49,52 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { } fn migrate() -> frame_support::weights::Weight { - let mut w = 1; + let mut r = 0; + let mut w = 4; let _ = migration::clear_storage_prefix(b"MessageGadget", b"CommitmentContract", &[], None, None); + let _ = Balances::transfer_all(RuntimeOrigin::signed(ROOT), Treasury::account_id(), true); + let lock_ids = [ + // Democracy lock. + *b"democrac", + // Fee market lock. + *b"da/feecr", + ]; + + >::iter().for_each(|(k, mut v)| { + if v.is_empty() { + // Clear the storage entry if the vector is empty. + + >::remove(k); + + w += 1; + } else { + // Find matching lock ids and remove them. + + let mut changed = false; + + v.retain(|l| { + if lock_ids.contains(&l.id) { + // Mark as changed, the storage entry needs to be updated. + changed = true; + + // To remove. + false + } else { + // To keep. + true + } + }); + + if changed { + >::insert(k, v); + + w += 1; + } + } + + r += 1; + }); w += migration_helper::PalletCleaner { name: b"EcdsaAuthority", @@ -59,5 +110,5 @@ fn migrate() -> frame_support::weights::Weight { .remove_all(); // frame_support::weights::Weight::zero() - ::DbWeight::get().reads_writes(0, w as _) + ::DbWeight::get().reads_writes(r as _, w as _) } diff --git a/runtime/darwinia/src/migration.rs b/runtime/darwinia/src/migration.rs index 67ee78e92..494f838de 100644 --- a/runtime/darwinia/src/migration.rs +++ b/runtime/darwinia/src/migration.rs @@ -27,11 +27,19 @@ pub struct CustomOnRuntimeUpgrade; impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + assert!(migration::have_storage_value(b"EcdsaAuthority", b"Authorities", &[])); + Ok(Vec::new()) } #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + assert!(!migration::have_storage_value(b"EcdsaAuthority", b"Authorities", &[])); + + >::iter_values().for_each(|v| { + assert!(!v.is_empty()); + }); + Ok(()) } @@ -41,9 +49,51 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { } fn migrate() -> frame_support::weights::Weight { + let mut r = 0; let mut w = 101; let _ = migration::clear_storage_prefix(b"MessageGadget", b"CommitmentContract", &[], None, None); + let lock_ids = [ + // Democracy lock. + *b"democrac", + // Fee market lock. + *b"da/feecr", + ]; + + >::iter().for_each(|(k, mut v)| { + if v.is_empty() { + // Clear the storage entry if the vector is empty. + + >::remove(k); + + w += 1; + } else { + // Find matching lock ids and remove them. + + let mut changed = false; + + v.retain(|l| { + if lock_ids.contains(&l.id) { + // Mark as changed, the storage entry needs to be updated. + changed = true; + + // To remove. + false + } else { + // To keep. + true + } + }); + + if changed { + >::insert(k, v); + + w += 1; + } + } + + r += 1; + }); w += migration_helper::PalletCleaner { name: b"EcdsaAuthority", @@ -67,5 +117,5 @@ fn migrate() -> frame_support::weights::Weight { ); // frame_support::weights::Weight::zero() - ::DbWeight::get().reads_writes(0, w as _) + ::DbWeight::get().reads_writes(r as _, w as _) } diff --git a/runtime/pangolin/src/migration.rs b/runtime/pangolin/src/migration.rs index 1074df6c0..8ed409989 100644 --- a/runtime/pangolin/src/migration.rs +++ b/runtime/pangolin/src/migration.rs @@ -32,6 +32,10 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { #[cfg(feature = "try-runtime")] fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + >::iter_values().for_each(|v| { + assert!(!v.is_empty()); + }); + Ok(()) } @@ -41,9 +45,51 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { } fn migrate() -> frame_support::weights::Weight { + let mut r = 0; let mut w = 1; let _ = migration::clear_storage_prefix(b"MessageGadget", b"CommitmentContract", &[], None, None); + let lock_ids = [ + // Democracy lock. + *b"democrac", + // Fee market lock. + *b"da/feecr", + ]; + + >::iter().for_each(|(k, mut v)| { + if v.is_empty() { + // Clear the storage entry if the vector is empty. + + >::remove(k); + + w += 1; + } else { + // Find matching lock ids and remove them. + + let mut changed = false; + + v.retain(|l| { + if lock_ids.contains(&l.id) { + // Mark as changed, the storage entry needs to be updated. + changed = true; + + // To remove. + false + } else { + // To keep. + true + } + }); + + if changed { + >::insert(k, v); + + w += 1; + } + } + + r += 1; + }); w += migration_helper::PalletCleaner { name: b"EcdsaAuthority", @@ -59,5 +105,5 @@ fn migrate() -> frame_support::weights::Weight { .remove_all(); // frame_support::weights::Weight::zero() - ::DbWeight::get().reads_writes(0, w as _) + ::DbWeight::get().reads_writes(r as _, w as _) }