From 3ea2cb5562c588021ca437687b05cad557be7d9d Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 18 Oct 2023 00:19:34 +0200 Subject: [PATCH] Fix para-scheduler migration on Rococo (#1921) Closes https://github.com/paritytech/polkadot-sdk/issues/1916 Changes: - Trivially wrap the migration into a version migration to enforce idempotency. - Opinionated logging nits @liamaharon maybe we can add a check to the `try-runtime-cli` that migrations are idempotent? It should be possible to check that the storage root is identical after executing a second time (and that it does not panic like it did here :laughing:). --------- Signed-off-by: Oliver Tale-Yazdi Co-authored-by: Liam Aharon Co-authored-by: Francisco Aguirre --- .../parachains/src/scheduler/migration.rs | 36 ++++++++++--------- prdoc/pr_1921.prdoc | 19 ++++++++++ substrate/frame/support/src/migrations.rs | 5 +-- 3 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 prdoc/pr_1921.prdoc diff --git a/polkadot/runtime/parachains/src/scheduler/migration.rs b/polkadot/runtime/parachains/src/scheduler/migration.rs index accff7016ed1..c1ce95b10e3c 100644 --- a/polkadot/runtime/parachains/src/scheduler/migration.rs +++ b/polkadot/runtime/parachains/src/scheduler/migration.rs @@ -18,7 +18,8 @@ use super::*; use frame_support::{ - pallet_prelude::ValueQuery, storage_alias, traits::OnRuntimeUpgrade, weights::Weight, + migrations::VersionedMigration, pallet_prelude::ValueQuery, storage_alias, + traits::OnRuntimeUpgrade, weights::Weight, }; mod v0 { @@ -83,22 +84,26 @@ mod v0 { pub mod v1 { use super::*; use crate::scheduler; - use frame_support::traits::StorageVersion; - pub struct MigrateToV1(sp_std::marker::PhantomData); - impl OnRuntimeUpgrade for MigrateToV1 { + #[allow(deprecated)] + pub type MigrateToV1 = VersionedMigration< + 0, + 1, + UncheckedMigrateToV1, + Pallet, + ::DbWeight, + >; + + #[deprecated(note = "Use MigrateToV1 instead")] + pub struct UncheckedMigrateToV1(sp_std::marker::PhantomData); + #[allow(deprecated)] + impl OnRuntimeUpgrade for UncheckedMigrateToV1 { fn on_runtime_upgrade() -> Weight { - if StorageVersion::get::>() == 0 { - let weight_consumed = migrate_to_v1::(); + let weight_consumed = migrate_to_v1::(); - log::info!(target: scheduler::LOG_TARGET, "Migrating para scheduler storage to v1"); - StorageVersion::new(1).put::>(); + log::info!(target: scheduler::LOG_TARGET, "Migrating para scheduler storage to v1"); - weight_consumed - } else { - log::warn!(target: scheduler::LOG_TARGET, "Para scheduler v1 migration should be removed."); - T::DbWeight::get().reads(1) - } + weight_consumed } #[cfg(feature = "try-runtime")] @@ -117,10 +122,7 @@ pub mod v1 { #[cfg(feature = "try-runtime")] fn post_upgrade(state: Vec) -> Result<(), sp_runtime::DispatchError> { log::trace!(target: crate::scheduler::LOG_TARGET, "Running post_upgrade()"); - ensure!( - StorageVersion::get::>() >= 1, - "Storage version should be at least `1` after the migration" - ); + ensure!( v0::Scheduled::::get().len() == 0, "Scheduled should be empty after the migration" diff --git a/prdoc/pr_1921.prdoc b/prdoc/pr_1921.prdoc new file mode 100644 index 000000000000..5ed0137cd5f9 --- /dev/null +++ b/prdoc/pr_1921.prdoc @@ -0,0 +1,19 @@ +title: Fix para-scheduler migration + +doc: + - audience: Core Dev + description: | + Changing the `MigrateToV1` migration in the `ParachainScheduler` pallet to be truly idempotent. It is achieved by wrapping it in a `VersionedMigration`. + +migrations: + db: [] + + runtime: + - pallet: "ParachainScheduler" + description: Non-critical fixup for `MigrateToV1`. + +crates: + - name: "polkadot-runtime-parachains" + semver: patch + +host_functions: [] diff --git a/substrate/frame/support/src/migrations.rs b/substrate/frame/support/src/migrations.rs index 8f490030c25b..d6ae6c6291b1 100644 --- a/substrate/frame/support/src/migrations.rs +++ b/substrate/frame/support/src/migrations.rs @@ -119,7 +119,7 @@ impl< let on_chain_version = Pallet::on_chain_storage_version(); if on_chain_version == FROM { log::info!( - "Running {} VersionedOnRuntimeUpgrade: version {:?} to {:?}.", + "🚚 Pallet {:?} migrating storage version from {:?} to {:?}.", Pallet::name(), FROM, TO @@ -134,9 +134,10 @@ impl< weight.saturating_add(DbWeight::get().reads_writes(1, 1)) } else { log::warn!( - "{} VersionedOnRuntimeUpgrade for version {:?} skipped because current on-chain version is {:?}.", + "🚚 Pallet {:?} migration {}->{} can be removed; on-chain is already at {:?}.", Pallet::name(), FROM, + TO, on_chain_version ); DbWeight::get().reads(1)