Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Ensure all StorageVersions on Rococo/Westend are correct and migration hooks pass #7251

Merged
merged 15 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion runtime/parachains/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,8 +485,10 @@ impl WeightInfo for TestWeightInfo {
pub mod pallet {
use super::*;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(5);

#[pallet::pallet]
#[pallet::storage_version(migration::STORAGE_VERSION)]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

Expand Down
16 changes: 9 additions & 7 deletions runtime/parachains/src/configuration/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use sp_std::vec::Vec;
/// v4-v5: <https://github.com/paritytech/polkadot/pull/6937>
/// + <https://github.com/paritytech/polkadot/pull/6961>
/// + <https://github.com/paritytech/polkadot/pull/6934>
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(5);

pub mod v5 {
use super::*;
Expand Down Expand Up @@ -144,9 +143,12 @@ pub mod v5 {
impl<T: Config> OnRuntimeUpgrade for MigrateToV5<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
log::trace!(target: crate::configuration::LOG_TARGET, "Running pre_upgrade()");
log::trace!(target: configuration::LOG_TARGET, "Running pre_upgrade()");

ensure!(StorageVersion::get::<Pallet<T>>() == 4, "The migration requires version 4");
ensure!(
Pallet::<T>::current_storage_version() >= Pallet::<T>::on_chain_storage_version(),
"configuration::migration::v5: on_chain version is greater than current version"
);
liamaharon marked this conversation as resolved.
Show resolved Hide resolved
Ok(Vec::new())
}

Expand All @@ -155,11 +157,11 @@ pub mod v5 {
let weight_consumed = migrate_to_v5::<T>();

log::info!(target: configuration::LOG_TARGET, "MigrateToV5 executed successfully");
STORAGE_VERSION.put::<Pallet<T>>();
StorageVersion::new(5).put::<Pallet<T>>();

weight_consumed
} else {
log::warn!(target: configuration::LOG_TARGET, "MigrateToV5 should be removed.");
log::warn!(target: configuration::LOG_TARGET, "MigrateToV5 can be removed.");
T::DbWeight::get().reads(1)
}
}
Expand All @@ -168,8 +170,8 @@ pub mod v5 {
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
log::trace!(target: crate::configuration::LOG_TARGET, "Running post_upgrade()");
ensure!(
StorageVersion::get::<Pallet<T>>() == STORAGE_VERSION,
"Storage version should be 5 after the migration"
StorageVersion::get::<Pallet<T>>() >= StorageVersion::new(5),
"Storage version must be at least 5 after this migration"
);

Ok(())
Expand Down
86 changes: 85 additions & 1 deletion runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1486,6 +1486,7 @@ pub type Migrations =
#[allow(deprecated, missing_docs)]
pub mod migrations {
use super::*;
use frame_support::traits::{GetStorageVersion, OnRuntimeUpgrade, StorageVersion};

pub type V0940 = ();
pub type V0941 = (); // Node only release - no migrations.
Expand All @@ -1495,7 +1496,90 @@ pub mod migrations {
);

/// Unreleased migrations. Add new ones here:
pub type Unreleased = ();
pub type Unreleased = SetStorageVersions;

/// Migrations that set `StorageVersion`s we missed to set.
///
/// It's *possible* that these pallets have not in fact been migrated to the versions being set,
/// which we should keep in mind in the future if we notice any strange behavior.
/// We opted to not check exactly what on-chain versions each pallet is at, since it would be
/// an involved effort, this is testnet, and no one has complained
/// (https://github.com/paritytech/polkadot/issues/6657#issuecomment-1552956439).
pub struct SetStorageVersions;

impl OnRuntimeUpgrade for SetStorageVersions {
fn on_runtime_upgrade() -> Weight {
let mut writes = 0;
let mut reads = 0;

// Council
if Council::on_chain_storage_version() < 4 {
// Safe to assume Council was created with V4 pallet.
StorageVersion::new(4).put::<Council>();
writes += 1;
}
reads += 1;

// Technical Committee
if TechnicalCommittee::on_chain_storage_version() < 4 {
StorageVersion::new(4).put::<TechnicalCommittee>();
writes += 1;
}
reads += 1;

// PhragmenElection
if PhragmenElection::on_chain_storage_version() < 4 {
StorageVersion::new(4).put::<PhragmenElection>();
writes += 1;
}
reads += 1;

// TechnicalMembership
if TechnicalMembership::on_chain_storage_version() < 4 {
// Did not miss V4 upgrade because version would have been set in storage, qed.
StorageVersion::new(4).put::<TechnicalMembership>();
writes += 1;
}
reads += 1;

// Scheduler
if Scheduler::on_chain_storage_version() < 4 {
StorageVersion::new(4).put::<Scheduler>();
writes += 1;
}
reads += 1;

// Bounties
if Bounties::on_chain_storage_version() < 4 {
StorageVersion::new(4).put::<Bounties>();
writes += 1;
}
reads += 1;

// Tips
if Tips::on_chain_storage_version() < 4 {
StorageVersion::new(4).put::<Tips>();
writes += 1;
}
reads += 1;

// NisCounterpartBalances
if NisCounterpartBalances::on_chain_storage_version() < 1 {
StorageVersion::new(1).put::<NisCounterpartBalances>();
writes += 1;
}
reads += 1;

// Crowdloan
if Crowdloan::on_chain_storage_version() < 2 {
StorageVersion::new(2).put::<Crowdloan>();
writes += 1;
}
reads += 1;

RocksDbWeight::get().reads_writes(reads, writes)
}
}
}

/// Executive: handles dispatch to the various modules.
Expand Down
18 changes: 17 additions & 1 deletion runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,8 @@ pub type Migrations =
/// The runtime migrations per release.
#[allow(deprecated, missing_docs)]
pub mod migrations {
use frame_support::traits::{GetStorageVersion, OnRuntimeUpgrade, StorageVersion};

use super::*;

pub type V0940 = (
Expand All @@ -1237,7 +1239,21 @@ pub mod migrations {
);

/// Unreleased migrations. Add new ones here:
pub type Unreleased = ();
pub type Unreleased = SetStorageVersions;

/// Migrations that set `StorageVersion`s we missed to set.
pub struct SetStorageVersions;

impl OnRuntimeUpgrade for SetStorageVersions {
fn on_runtime_upgrade() -> Weight {
if FastUnstake::on_chain_storage_version() < 1 {
StorageVersion::new(1).put::<FastUnstake>();
return RocksDbWeight::get().reads_writes(1, 1)
}

RocksDbWeight::get().reads(1)
}
}
}

/// Unchecked extrinsic type as expected by this runtime.
Expand Down