From 0727fb3bef950d8cbe6e4bf6eb47257424b6801b Mon Sep 17 00:00:00 2001 From: WMQ <46511820+wangminqi@users.noreply.github.com> Date: Wed, 15 Jan 2025 10:21:31 +0800 Subject: [PATCH] feat: fix/edit missing migration (#3230) * feat: fix/edit missing migration * chore: fix * chore: fix * chore: fix clippy * chore: fix --------- Co-authored-by: Kai <7630809+Kailai-Wang@users.noreply.github.com> --- .../pallets/collab-ai/curator/src/lib.rs | 2 +- .../pallets/collab-ai/guardian/src/lib.rs | 2 +- .../collab-ai/pool-proposal/src/lib.rs | 2 +- .../runtime/litentry/src/migration/mod.rs | 52 +++++++++++++++- parachain/runtime/paseo/src/migration/mod.rs | 61 +++++++++++++++++++ 5 files changed, 113 insertions(+), 6 deletions(-) diff --git a/parachain/pallets/collab-ai/curator/src/lib.rs b/parachain/pallets/collab-ai/curator/src/lib.rs index 6e0803213e..42fcd10516 100644 --- a/parachain/pallets/collab-ai/curator/src/lib.rs +++ b/parachain/pallets/collab-ai/curator/src/lib.rs @@ -52,7 +52,7 @@ pub mod pallet { use super::*; /// The current storage version. - const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] diff --git a/parachain/pallets/collab-ai/guardian/src/lib.rs b/parachain/pallets/collab-ai/guardian/src/lib.rs index 7bf8c860de..ece2a2b7d9 100644 --- a/parachain/pallets/collab-ai/guardian/src/lib.rs +++ b/parachain/pallets/collab-ai/guardian/src/lib.rs @@ -52,7 +52,7 @@ pub mod pallet { use super::*; /// The current storage version. - const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); #[pallet::pallet] #[pallet::storage_version(STORAGE_VERSION)] diff --git a/parachain/pallets/collab-ai/pool-proposal/src/lib.rs b/parachain/pallets/collab-ai/pool-proposal/src/lib.rs index b67f809f9a..8bfbe67c9e 100644 --- a/parachain/pallets/collab-ai/pool-proposal/src/lib.rs +++ b/parachain/pallets/collab-ai/pool-proposal/src/lib.rs @@ -71,7 +71,7 @@ pub mod pallet { /// CollabAI investing pool proposal const MODULE_ID: PalletId = PalletId(*b"cbai/ipp"); /// The current storage version. - const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(0); pub type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; diff --git a/parachain/runtime/litentry/src/migration/mod.rs b/parachain/runtime/litentry/src/migration/mod.rs index 15209e348d..fdd3fb883b 100644 --- a/parachain/runtime/litentry/src/migration/mod.rs +++ b/parachain/runtime/litentry/src/migration/mod.rs @@ -81,9 +81,9 @@ pub type Migrations = ( // TODO: Where does this number come from? BalancesUpdateStorageVersionResetInactive, // Democracy V0 => V1 - // This migration only effects onging proposal/referedum, NextExternal - // The referedum info's proposal hash is migrated if the hash is in old form (In our case, even for an onging one it will do nothing) - pallet_democracy::migrations::v1::v1::Migration, + // The ofifical migration only effects onging proposal/referedum, NextExternal, which we are already good + // So we just bump version storage instead + DemocracyUpdateStorageVersion, // Bounties V0 => V4 // The official migration does nothing but change pallet name and bump version // So we just bump version storage instead @@ -307,6 +307,52 @@ where } } +const DEMOCRACY_LOG_TARGET: &str = "runtime::democracy"; +pub struct DemocracyUpdateStorageVersion(PhantomData); +impl OnRuntimeUpgrade for DemocracyUpdateStorageVersion +where + T: frame_system::Config + pallet_democracy::Config, +{ + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + ensure!( + StorageVersion::get::>() == 0, + "Already upgrade to some non-zero version" + ); + Ok(Vec::::new()) + } + + fn on_runtime_upgrade() -> frame_support::weights::Weight { + let on_chain_version = pallet_democracy::Pallet::::on_chain_storage_version(); + + if on_chain_version == 0 { + // Remove the old `StorageVersion` type. + frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix( + pallet_democracy::Pallet::::name().as_bytes(), + "StorageVersion".as_bytes(), + )); + + // Set storage version to `1`. + StorageVersion::new(1).put::>(); + + log::info!(target: DEMOCRACY_LOG_TARGET, "Storage to version 1"); + T::DbWeight::get().reads_writes(1, 3) + } else { + log::info!( + target: DEMOCRACY_LOG_TARGET, + "Migration did not execute. This probably should be removed" + ); + T::DbWeight::get().reads(1) + } + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + ensure!(StorageVersion::get::>() == 1, "Must upgrade"); + Ok(()) + } +} + const BOUNTIES_LOG_TARGET: &str = "runtime::bounties"; pub struct BountiesUpdateStorageVersion(PhantomData); impl OnRuntimeUpgrade for BountiesUpdateStorageVersion diff --git a/parachain/runtime/paseo/src/migration/mod.rs b/parachain/runtime/paseo/src/migration/mod.rs index 3b492ef07a..aa7577bf2a 100644 --- a/parachain/runtime/paseo/src/migration/mod.rs +++ b/parachain/runtime/paseo/src/migration/mod.rs @@ -16,19 +16,34 @@ // By the time of this migration on Paseo 9221 (polkadot stable2407) // The current storage version: pallet version: +// parachainIdentity - 0 // xcmpQueue: - 3 // transactionPayment: V2 0 // vesting: V1 0 // Our target storage version: pallet version: (stable2407) +// parachainIdentity - 1 // xcmpQueue: - 3 => 5 // transactionPayment: V2 0 // vesting: V1 0 // In try-runtime, current implementation, the storage version is not checked, // Pallet version is used instead. +#[cfg(feature = "try-runtime")] +use frame_support::ensure; +use frame_support::traits::{ + Get, GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess, StorageVersion, +}; +use sp_std::marker::PhantomData; +#[cfg(feature = "try-runtime")] +use sp_std::vec::Vec; pub type Migrations = ( + // Identity V0 => V1 + // Our storage is empty + // The official migration is for can old IdentityFor into new IdentityFor + // Should do nothing but bump version storage for us + IdentityUpdateStorageVersion, // V3 to V4 // XCMP QueueConfig has different default value // Migration targeting at changing storage value to new default value if old value matched @@ -42,3 +57,49 @@ pub type Migrations = ( // This migration should have no effect except bumping storage version cumulus_pallet_xcmp_queue::migration::v5::MigrateV4ToV5, ); + +const IDENTITY_LOG_TARGET: &str = "runtime::identity"; +pub struct IdentityUpdateStorageVersion(PhantomData); +impl OnRuntimeUpgrade for IdentityUpdateStorageVersion +where + T: frame_system::Config + pallet_identity::Config, +{ + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + ensure!( + StorageVersion::get::>() == 0, + "Already upgrade to some non-zero version" + ); + Ok(Vec::::new()) + } + + fn on_runtime_upgrade() -> frame_support::weights::Weight { + let on_chain_version = pallet_identity::Pallet::::on_chain_storage_version(); + + if on_chain_version == 0 { + // Remove the old `StorageVersion` type. + frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix( + pallet_identity::Pallet::::name().as_bytes(), + "StorageVersion".as_bytes(), + )); + + // Set storage version to `1`. + StorageVersion::new(1).put::>(); + + log::info!(target: IDENTITY_LOG_TARGET, "Storage to version 1"); + T::DbWeight::get().reads_writes(1, 3) + } else { + log::info!( + target: IDENTITY_LOG_TARGET, + "Migration did not execute. This probably should be removed" + ); + T::DbWeight::get().reads(1) + } + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), sp_runtime::DispatchError> { + ensure!(StorageVersion::get::>() == 1, "Must upgrade"); + Ok(()) + } +}