From 4171d8b39ab99f78ca26bbadb691f3e6b80322de Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Mon, 13 Nov 2023 10:36:55 +0300 Subject: [PATCH] fix migration (https://github.com/paritytech/polkadot-sdk/pull/1969) --- CHANGELOG.md | 6 +- relay/kusama/src/lib.rs | 9 +- relay/kusama/src/paras_scheduler_migration.rs | 218 ------------------ relay/polkadot/src/lib.rs | 9 +- .../polkadot/src/paras_scheduler_migration.rs | 218 ------------------ 5 files changed, 13 insertions(+), 447 deletions(-) delete mode 100644 relay/kusama/src/paras_scheduler_migration.rs delete mode 100644 relay/polkadot/src/paras_scheduler_migration.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 6091c648aa..48a94c62a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -52,15 +52,15 @@ TODO for bridging: port https://github.com/paritytech/polkadot-sdk/pull/2023/fil - [x] **ADDED MIGRATION** [NPoS] Fix for Reward Deficit in the pool (#1255 @Ank4n) [Pallets] - [x] (INTERNAL) frame-support: RuntimeDebug\Eq\PartialEq impls for Imbalance (#1717 @muharem) [Frame] - [x] **ADDED MIGRATION** Tvl pool staking (#1322 @PieWol) [Frame] -- [ ] **TODO: do the same for fellowship SP?** Init System Parachain storage versions and add migration check jobs to CI (#1344 @liamaharon) [Frame] +- [X] **Could be added later (ping author)** Init System Parachain storage versions and add migration check jobs to CI (#1344 @liamaharon) [Frame] - [x] (INTERNAL) expose the last relay chain block number as an API from parachain-system (#1761 @rphmeier) [Pallets] - [x] (INTERNAL) feat: compute pallet/storage prefix hash at compile time (#1539 @yjhmelody) [Frame] -- [ ] **TODO: do the same for fellowship SP?** Treasury spends various asset kinds (#1333 @muharem) [Frame] +- [ ] **Could be added later (ping author)** Treasury spends various asset kinds (#1333 @muharem) [Frame] - [x] (INTERNAL) Make CheckNonce refuse transactions signed by accounts with no providers (#1578 @zdave-parity) [Frame] - [x] (INTERNAL) Warn on unchecked weight witness (#1818 @ggwpez) [Frame] - [x] (INTERNAL) frame: use derive-impl for beefy and mmr pallets (#1867 @acatangiu) [Pallets] - [x] (INTERNAL) Macros to use path instead of ident (#1474 @juangirini) [Frame] -- [ ] **TODO: no migration required?** Refactor staking ledger (#1484 @gpestana) [Frame, Pallets] +- [X] **descr saids that no migration required** Refactor staking ledger (#1484 @gpestana) [Frame, Pallets] - [x] (INTERNAL) extract amount method for fungible/s Imbalance (#1847 @muharem) [Frame] - [x] (INTERNAL) Allow Locks/Holds/Reserves/Freezes by default when using pallet_balances TestDefaultConfig (#1880 @liamaharon) [Frame, Pallets] - [x] (INTERNAL) nit: use traits::tokens::fungible => use traits::fungible (#1753 @gilescope) [Pallets] diff --git a/relay/kusama/src/lib.rs b/relay/kusama/src/lib.rs index 8acdc53327..524293e2d5 100644 --- a/relay/kusama/src/lib.rs +++ b/relay/kusama/src/lib.rs @@ -121,8 +121,6 @@ mod weights; // Voter bag threshold definitions. mod bag_thresholds; -// mod paras_scheduler_migration; // TODO: has it happened? - // Historical information of society finances. mod past_payouts; @@ -1795,7 +1793,7 @@ pub mod migrations { >, pallet_im_online::migration::v1::Migration, parachains_configuration::migration::v7::MigrateToV7, - // paras_scheduler_migration::v1::MigrateToV1, TODO: has it happened? + runtime_parachains::scheduler::migration::v1::MigrateToV1, parachains_configuration::migration::v8::MigrateToV8, // Unlock/unreserve balances from Gov v1 pallets that hold them @@ -2573,7 +2571,10 @@ mod fees_tests { #[cfg(test)] mod multiplier_tests { use super::*; - use frame_support::{dispatch::DispatchInfo, traits::{OnFinalize, PalletInfoAccess}}; + use frame_support::{ + dispatch::DispatchInfo, + traits::{OnFinalize, PalletInfoAccess}, + }; use runtime_common::{MinimumMultiplier, TargetBlockFullness}; use separator::Separatable; use sp_runtime::traits::Convert; diff --git a/relay/kusama/src/paras_scheduler_migration.rs b/relay/kusama/src/paras_scheduler_migration.rs deleted file mode 100644 index d294861843..0000000000 --- a/relay/kusama/src/paras_scheduler_migration.rs +++ /dev/null @@ -1,218 +0,0 @@ -//! A copy of the migration found in the polkadot sdk repo. -//! -//! It is copied as the version of the migration found in the crate used by this runtime is broken. - -use frame_support::{ - migrations::VersionedMigration, pallet_prelude::ValueQuery, storage_alias, - traits::OnRuntimeUpgrade, weights::Weight, -}; -use frame_system::pallet_prelude::BlockNumberFor; -use parity_scale_codec::{Decode, Encode}; -use primitives::{ - v5::{Assignment, ParasEntry}, - CoreIndex, CoreOccupied, GroupIndex, Id as ParaId, -}; -use runtime_parachains::scheduler::*; -use scale_info::TypeInfo; -use sp_core::{Get, RuntimeDebug}; -use sp_std::{ - collections::{btree_map::BTreeMap, vec_deque::VecDeque}, - prelude::*, -}; - -const LOG_TARGET: &str = "runtime::parachains::scheduler"; - -mod v0 { - use super::*; - - use primitives::{CollatorId, Id}; - - #[storage_alias] - pub(super) type Scheduled = StorageValue, Vec, ValueQuery>; - - #[derive(Clone, Encode, Decode)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub struct ParathreadClaim(pub Id, pub CollatorId); - - #[derive(Clone, Encode, Decode)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub struct ParathreadEntry { - /// The claim. - pub claim: ParathreadClaim, - /// Number of retries. - pub retries: u32, - } - - /// What is occupying a specific availability core. - #[derive(Clone, Encode, Decode)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub enum CoreOccupied { - /// A parathread. - Parathread(ParathreadEntry), - /// A parachain. - Parachain, - } - - /// The actual type isn't important, as we only delete the key in the state. - #[storage_alias] - pub(crate) type AvailabilityCores = - StorageValue, Vec>, ValueQuery>; - - /// The actual type isn't important, as we only delete the key in the state. - #[storage_alias] - pub(super) type ParathreadQueue = StorageValue, (), ValueQuery>; - - #[storage_alias] - pub(super) type ParathreadClaimIndex = StorageValue, (), ValueQuery>; - - /// The assignment type. - #[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub enum AssignmentKind { - /// A parachain. - Parachain, - /// A parathread. - Parathread(CollatorId, u32), - } - - /// How a free core is scheduled to be assigned. - #[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub struct CoreAssignment { - /// The core that is assigned. - pub core: CoreIndex, - /// The unique ID of the para that is assigned to the core. - pub para_id: ParaId, - /// The kind of the assignment. - pub kind: AssignmentKind, - /// The index of the validator group assigned to the core. - pub group_idx: GroupIndex, - } -} - -pub mod v1 { - use super::*; - - #[storage_alias] - pub(crate) type AvailabilityCores = - StorageValue, Vec>>, ValueQuery>; - - #[storage_alias] - pub(crate) type ClaimQueue = StorageValue< - Pallet, - BTreeMap>>>>, - ValueQuery, - >; - - #[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 { - let weight_consumed = migrate_to_v1::(); - - log::info!(target: LOG_TARGET, "Migrating para scheduler storage to v1"); - - weight_consumed - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::DispatchError> { - let n: u32 = v0::Scheduled::::get().len() as u32 + - v0::AvailabilityCores::::get().iter().filter(|c| c.is_some()).count() as u32; - - log::info!( - target: LOG_TARGET, - "Number of scheduled and waiting for availability before: {n}", - ); - - Ok(n.encode()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), sp_runtime::DispatchError> { - log::info!(target: LOG_TARGET, "Running post_upgrade()"); - - frame_support::ensure!( - v0::Scheduled::::get().is_empty(), - "Scheduled should be empty after the migration" - ); - - let expected_len = u32::decode(&mut &state[..]).unwrap(); - let availability_cores_waiting = AvailabilityCores::::get() - .iter() - .filter(|c| !matches!(c, CoreOccupied::Free)) - .count(); - - frame_support::ensure!( - ClaimQueue::::get().iter().map(|la_vec| la_vec.1.len()).sum::() as u32 + - availability_cores_waiting as u32 == - expected_len, - "ClaimQueue and AvailabilityCores should have the correct length", - ); - - Ok(()) - } - } -} - -pub fn migrate_to_v1() -> Weight { - let mut weight: Weight = Weight::zero(); - - v0::ParathreadQueue::::kill(); - v0::ParathreadClaimIndex::::kill(); - - let now = >::block_number(); - let scheduled = v0::Scheduled::::take(); - let sched_len = scheduled.len() as u64; - for core_assignment in scheduled { - let core_idx = core_assignment.core; - let assignment = Assignment::new(core_assignment.para_id); - let pe = ParasEntry::new(assignment, now); - - v1::ClaimQueue::::mutate(|la| { - la.entry(core_idx).or_default().push_back(Some(pe)); - }); - } - - let parachains = runtime_parachains::paras::Pallet::::parachains(); - let availability_cores = v0::AvailabilityCores::::take(); - let mut new_availability_cores = Vec::new(); - - for (core_index, core) in availability_cores.into_iter().enumerate() { - let new_core = if let Some(core) = core { - match core { - v0::CoreOccupied::Parachain => CoreOccupied::Paras(ParasEntry::new( - Assignment::new(parachains[core_index]), - now, - )), - v0::CoreOccupied::Parathread(entry) => - CoreOccupied::Paras(ParasEntry::new(Assignment::new(entry.claim.0), now)), - } - } else { - CoreOccupied::Free - }; - - new_availability_cores.push(new_core); - } - - v1::AvailabilityCores::::set(new_availability_cores); - - // 2x as once for Scheduled and once for Claimqueue - weight = weight.saturating_add(T::DbWeight::get().reads_writes(2 * sched_len, 2 * sched_len)); - // reading parachains + availability_cores, writing AvailabilityCores - weight = weight.saturating_add(T::DbWeight::get().reads_writes(2, 1)); - // 2x kill - weight = weight.saturating_add(T::DbWeight::get().writes(2)); - - weight -} diff --git a/relay/polkadot/src/lib.rs b/relay/polkadot/src/lib.rs index 8371a18bd1..49f1d21b3d 100644 --- a/relay/polkadot/src/lib.rs +++ b/relay/polkadot/src/lib.rs @@ -119,8 +119,6 @@ mod weights; mod bag_thresholds; -// mod paras_scheduler_migration; // TODO: has it happened? - // Governance configurations. pub mod governance; use governance::{ @@ -1631,7 +1629,7 @@ pub mod migrations { pub type Unreleased = ( pallet_im_online::migration::v1::Migration, parachains_configuration::migration::v7::MigrateToV7, - // crate::paras_scheduler_migration::v1::MigrateToV1, // TODO: has it happened? + runtime_parachains::scheduler::migration::v1::MigrateToV1, parachains_configuration::migration::v8::MigrateToV8, // Gov v1 storage migrations @@ -2542,7 +2540,10 @@ mod test { #[cfg(test)] mod multiplier_tests { use super::*; - use frame_support::{dispatch::DispatchInfo, traits::OnFinalize}; + use frame_support::{ + dispatch::DispatchInfo, + traits::{OnFinalize, PalletInfoAccess}, + }; use runtime_common::{MinimumMultiplier, TargetBlockFullness}; use scale_info::TypeInfo; use separator::Separatable; diff --git a/relay/polkadot/src/paras_scheduler_migration.rs b/relay/polkadot/src/paras_scheduler_migration.rs deleted file mode 100644 index d294861843..0000000000 --- a/relay/polkadot/src/paras_scheduler_migration.rs +++ /dev/null @@ -1,218 +0,0 @@ -//! A copy of the migration found in the polkadot sdk repo. -//! -//! It is copied as the version of the migration found in the crate used by this runtime is broken. - -use frame_support::{ - migrations::VersionedMigration, pallet_prelude::ValueQuery, storage_alias, - traits::OnRuntimeUpgrade, weights::Weight, -}; -use frame_system::pallet_prelude::BlockNumberFor; -use parity_scale_codec::{Decode, Encode}; -use primitives::{ - v5::{Assignment, ParasEntry}, - CoreIndex, CoreOccupied, GroupIndex, Id as ParaId, -}; -use runtime_parachains::scheduler::*; -use scale_info::TypeInfo; -use sp_core::{Get, RuntimeDebug}; -use sp_std::{ - collections::{btree_map::BTreeMap, vec_deque::VecDeque}, - prelude::*, -}; - -const LOG_TARGET: &str = "runtime::parachains::scheduler"; - -mod v0 { - use super::*; - - use primitives::{CollatorId, Id}; - - #[storage_alias] - pub(super) type Scheduled = StorageValue, Vec, ValueQuery>; - - #[derive(Clone, Encode, Decode)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub struct ParathreadClaim(pub Id, pub CollatorId); - - #[derive(Clone, Encode, Decode)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub struct ParathreadEntry { - /// The claim. - pub claim: ParathreadClaim, - /// Number of retries. - pub retries: u32, - } - - /// What is occupying a specific availability core. - #[derive(Clone, Encode, Decode)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub enum CoreOccupied { - /// A parathread. - Parathread(ParathreadEntry), - /// A parachain. - Parachain, - } - - /// The actual type isn't important, as we only delete the key in the state. - #[storage_alias] - pub(crate) type AvailabilityCores = - StorageValue, Vec>, ValueQuery>; - - /// The actual type isn't important, as we only delete the key in the state. - #[storage_alias] - pub(super) type ParathreadQueue = StorageValue, (), ValueQuery>; - - #[storage_alias] - pub(super) type ParathreadClaimIndex = StorageValue, (), ValueQuery>; - - /// The assignment type. - #[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub enum AssignmentKind { - /// A parachain. - Parachain, - /// A parathread. - Parathread(CollatorId, u32), - } - - /// How a free core is scheduled to be assigned. - #[derive(Clone, Encode, Decode, TypeInfo, RuntimeDebug)] - #[cfg_attr(feature = "std", derive(PartialEq))] - pub struct CoreAssignment { - /// The core that is assigned. - pub core: CoreIndex, - /// The unique ID of the para that is assigned to the core. - pub para_id: ParaId, - /// The kind of the assignment. - pub kind: AssignmentKind, - /// The index of the validator group assigned to the core. - pub group_idx: GroupIndex, - } -} - -pub mod v1 { - use super::*; - - #[storage_alias] - pub(crate) type AvailabilityCores = - StorageValue, Vec>>, ValueQuery>; - - #[storage_alias] - pub(crate) type ClaimQueue = StorageValue< - Pallet, - BTreeMap>>>>, - ValueQuery, - >; - - #[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 { - let weight_consumed = migrate_to_v1::(); - - log::info!(target: LOG_TARGET, "Migrating para scheduler storage to v1"); - - weight_consumed - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, sp_runtime::DispatchError> { - let n: u32 = v0::Scheduled::::get().len() as u32 + - v0::AvailabilityCores::::get().iter().filter(|c| c.is_some()).count() as u32; - - log::info!( - target: LOG_TARGET, - "Number of scheduled and waiting for availability before: {n}", - ); - - Ok(n.encode()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(state: Vec) -> Result<(), sp_runtime::DispatchError> { - log::info!(target: LOG_TARGET, "Running post_upgrade()"); - - frame_support::ensure!( - v0::Scheduled::::get().is_empty(), - "Scheduled should be empty after the migration" - ); - - let expected_len = u32::decode(&mut &state[..]).unwrap(); - let availability_cores_waiting = AvailabilityCores::::get() - .iter() - .filter(|c| !matches!(c, CoreOccupied::Free)) - .count(); - - frame_support::ensure!( - ClaimQueue::::get().iter().map(|la_vec| la_vec.1.len()).sum::() as u32 + - availability_cores_waiting as u32 == - expected_len, - "ClaimQueue and AvailabilityCores should have the correct length", - ); - - Ok(()) - } - } -} - -pub fn migrate_to_v1() -> Weight { - let mut weight: Weight = Weight::zero(); - - v0::ParathreadQueue::::kill(); - v0::ParathreadClaimIndex::::kill(); - - let now = >::block_number(); - let scheduled = v0::Scheduled::::take(); - let sched_len = scheduled.len() as u64; - for core_assignment in scheduled { - let core_idx = core_assignment.core; - let assignment = Assignment::new(core_assignment.para_id); - let pe = ParasEntry::new(assignment, now); - - v1::ClaimQueue::::mutate(|la| { - la.entry(core_idx).or_default().push_back(Some(pe)); - }); - } - - let parachains = runtime_parachains::paras::Pallet::::parachains(); - let availability_cores = v0::AvailabilityCores::::take(); - let mut new_availability_cores = Vec::new(); - - for (core_index, core) in availability_cores.into_iter().enumerate() { - let new_core = if let Some(core) = core { - match core { - v0::CoreOccupied::Parachain => CoreOccupied::Paras(ParasEntry::new( - Assignment::new(parachains[core_index]), - now, - )), - v0::CoreOccupied::Parathread(entry) => - CoreOccupied::Paras(ParasEntry::new(Assignment::new(entry.claim.0), now)), - } - } else { - CoreOccupied::Free - }; - - new_availability_cores.push(new_core); - } - - v1::AvailabilityCores::::set(new_availability_cores); - - // 2x as once for Scheduled and once for Claimqueue - weight = weight.saturating_add(T::DbWeight::get().reads_writes(2 * sched_len, 2 * sched_len)); - // reading parachains + availability_cores, writing AvailabilityCores - weight = weight.saturating_add(T::DbWeight::get().reads_writes(2, 1)); - // 2x kill - weight = weight.saturating_add(T::DbWeight::get().writes(2)); - - weight -}