Skip to content

Commit

Permalink
feat: fix/edit missing migration (#3230)
Browse files Browse the repository at this point in the history
* feat: fix/edit missing migration

* chore: fix

* chore: fix

* chore: fix clippy

* chore: fix

---------

Co-authored-by: Kai <[email protected]>
  • Loading branch information
wangminqi and Kailai-Wang authored Jan 15, 2025
1 parent 3473603 commit 0727fb3
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
2 changes: 1 addition & 1 deletion parachain/pallets/collab-ai/curator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion parachain/pallets/collab-ai/guardian/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion parachain/pallets/collab-ai/pool-proposal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;
Expand Down
52 changes: 49 additions & 3 deletions parachain/runtime/litentry/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ pub type Migrations<Runtime> = (
// TODO: Where does this number come from?
BalancesUpdateStorageVersionResetInactive<Runtime>,
// 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<Runtime>,
// The ofifical migration only effects onging proposal/referedum, NextExternal, which we are already good
// So we just bump version storage instead
DemocracyUpdateStorageVersion<Runtime>,
// Bounties V0 => V4
// The official migration does nothing but change pallet name and bump version
// So we just bump version storage instead
Expand Down Expand Up @@ -307,6 +307,52 @@ where
}
}

const DEMOCRACY_LOG_TARGET: &str = "runtime::democracy";
pub struct DemocracyUpdateStorageVersion<T>(PhantomData<T>);
impl<T> OnRuntimeUpgrade for DemocracyUpdateStorageVersion<T>
where
T: frame_system::Config + pallet_democracy::Config,
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
ensure!(
StorageVersion::get::<pallet_democracy::Pallet<T>>() == 0,
"Already upgrade to some non-zero version"
);
Ok(Vec::<u8>::new())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
let on_chain_version = pallet_democracy::Pallet::<T>::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::<T>::name().as_bytes(),
"StorageVersion".as_bytes(),
));

// Set storage version to `1`.
StorageVersion::new(1).put::<pallet_democracy::Pallet<T>>();

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<u8>) -> Result<(), sp_runtime::DispatchError> {
ensure!(StorageVersion::get::<pallet_democracy::Pallet<T>>() == 1, "Must upgrade");
Ok(())
}
}

const BOUNTIES_LOG_TARGET: &str = "runtime::bounties";
pub struct BountiesUpdateStorageVersion<T>(PhantomData<T>);
impl<T> OnRuntimeUpgrade for BountiesUpdateStorageVersion<T>
Expand Down
61 changes: 61 additions & 0 deletions parachain/runtime/paseo/src/migration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime> = (
// 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<Runtime>,
// V3 to V4
// XCMP QueueConfig has different default value
// Migration targeting at changing storage value to new default value if old value matched
Expand All @@ -42,3 +57,49 @@ pub type Migrations<Runtime> = (
// This migration should have no effect except bumping storage version
cumulus_pallet_xcmp_queue::migration::v5::MigrateV4ToV5<Runtime>,
);

const IDENTITY_LOG_TARGET: &str = "runtime::identity";
pub struct IdentityUpdateStorageVersion<T>(PhantomData<T>);
impl<T> OnRuntimeUpgrade for IdentityUpdateStorageVersion<T>
where
T: frame_system::Config + pallet_identity::Config,
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
ensure!(
StorageVersion::get::<pallet_identity::Pallet<T>>() == 0,
"Already upgrade to some non-zero version"
);
Ok(Vec::<u8>::new())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
let on_chain_version = pallet_identity::Pallet::<T>::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::<T>::name().as_bytes(),
"StorageVersion".as_bytes(),
));

// Set storage version to `1`.
StorageVersion::new(1).put::<pallet_identity::Pallet<T>>();

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<u8>) -> Result<(), sp_runtime::DispatchError> {
ensure!(StorageVersion::get::<pallet_identity::Pallet<T>>() == 1, "Must upgrade");
Ok(())
}
}

0 comments on commit 0727fb3

Please sign in to comment.