From c8321deb7feb350f3b9447ff3236d55304fffe76 Mon Sep 17 00:00:00 2001 From: Chris Sosnin Date: Tue, 28 Mar 2023 00:48:17 +0400 Subject: [PATCH 1/2] update scheduler migration --- runtime/kusama/src/lib.rs | 1 + runtime/parachains/src/scheduler.rs | 12 ++--- runtime/parachains/src/scheduler/migration.rs | 49 +++++++++++-------- runtime/polkadot/src/lib.rs | 1 + runtime/rococo/src/lib.rs | 1 + runtime/westend/src/lib.rs | 1 + 6 files changed, 36 insertions(+), 29 deletions(-) diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index 5aa25df0d19d..aa1438f86fe9 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -1483,6 +1483,7 @@ pub type Migrations = ( >, /* Asynchronous backing mirgration */ parachains_configuration::migration::v5::MigrateToV5, + parachains_scheduler::migration::v1::MigrateToV1, ); /// Unchecked extrinsic type as expected by this runtime. diff --git a/runtime/parachains/src/scheduler.rs b/runtime/parachains/src/scheduler.rs index 1b6679dcd784..f2c2eaee82e5 100644 --- a/runtime/parachains/src/scheduler.rs +++ b/runtime/parachains/src/scheduler.rs @@ -36,7 +36,6 @@ //! over time. use frame_support::pallet_prelude::*; -use frame_system::pallet_prelude::*; use primitives::{ CollatorId, CoreIndex, CoreOccupied, GroupIndex, GroupRotationInfo, Id as ParaId, ParathreadClaim, ParathreadEntry, ScheduledCore, ValidatorIndex, @@ -52,7 +51,9 @@ pub use pallet::*; #[cfg(test)] mod tests; -mod migration; +pub mod migration; + +const LOG_TARGET: &str = "runtime::scheduler"; /// A queued parathread entry, pre-assigned to a core. #[derive(Encode, Decode, TypeInfo)] @@ -166,13 +167,6 @@ pub mod pallet { #[pallet::config] pub trait Config: frame_system::Config + configuration::Config + paras::Config {} - #[pallet::hooks] - impl Hooks> for Pallet { - fn on_runtime_upgrade() -> Weight { - migration::on_runtime_upgrade::() - } - } - /// All the validator groups. One for each core. Indices are into `ActiveValidators` - not the /// broader set of Polkadot validators, but instead just the subset used for parachains during /// this session. diff --git a/runtime/parachains/src/scheduler/migration.rs b/runtime/parachains/src/scheduler/migration.rs index 3960dd238637..638a973e6742 100644 --- a/runtime/parachains/src/scheduler/migration.rs +++ b/runtime/parachains/src/scheduler/migration.rs @@ -16,29 +16,14 @@ //! A module that is responsible for migration of storage. -use crate::scheduler::{self, AssignmentKind, Config, Pallet, Scheduled}; -use frame_support::{pallet_prelude::*, traits::StorageVersion, weights::Weight}; -use parity_scale_codec::{Decode, Encode}; +use frame_support::traits::StorageVersion; /// The current storage version. pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); -/// Call this during the next runtime upgrade for this module. -pub fn on_runtime_upgrade() -> Weight { - let mut weight: Weight = Weight::zero(); - - if StorageVersion::get::>() == 0 { - weight = weight - .saturating_add(v1::migrate::()) - .saturating_add(T::DbWeight::get().writes(1)); - StorageVersion::new(1).put::>(); - } - - weight -} - mod v0 { - use super::*; + use crate::scheduler::{self, AssignmentKind}; + use parity_scale_codec::{Decode, Encode}; use primitives::{CoreIndex, GroupIndex, Id as ParaId}; #[derive(Encode, Decode)] @@ -58,11 +43,35 @@ mod v0 { /// V1: Group index is dropped from the core assignment, it's explicitly computed during /// candidates processing. -mod v1 { +pub mod v1 { use super::*; + use crate::scheduler::{self, Config, Pallet, Scheduled}; + use frame_support::{pallet_prelude::*, traits::OnRuntimeUpgrade, weights::Weight}; use sp_std::vec::Vec; - pub fn migrate() -> Weight { + pub struct MigrateToV1(sp_std::marker::PhantomData); + impl OnRuntimeUpgrade for MigrateToV1 { + fn on_runtime_upgrade() -> Weight { + let mut weight: Weight = Weight::zero(); + + if StorageVersion::get::>() < STORAGE_VERSION { + log::info!(target: scheduler::LOG_TARGET, "Migrating scheduler storage to v1"); + weight = weight + .saturating_add(migrate::()) + .saturating_add(T::DbWeight::get().reads_writes(1, 1)); + STORAGE_VERSION.put::>(); + } else { + log::info!( + target: scheduler::LOG_TARGET, + "Scheduler storage up to date - no need for migration" + ); + } + + weight + } + } + + fn migrate() -> Weight { let _ = Scheduled::::translate(|scheduled: Option>| { scheduled.map(|scheduled| { scheduled.into_iter().map(|old| scheduler::CoreAssignment::from(old)).collect() diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 6338ab8bbca8..33be13db902a 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -1420,6 +1420,7 @@ pub type Migrations = ( >, /* Asynchronous backing mirgration */ parachains_configuration::migration::v5::MigrateToV5, + parachains_scheduler::migration::v1::MigrateToV1, ); /// Unchecked extrinsic type as expected by this runtime. diff --git a/runtime/rococo/src/lib.rs b/runtime/rococo/src/lib.rs index a41006becc95..3e96f158e912 100644 --- a/runtime/rococo/src/lib.rs +++ b/runtime/rococo/src/lib.rs @@ -1497,6 +1497,7 @@ pub type UncheckedExtrinsic = pub type Migrations = ( /* Asynchronous backing mirgration */ parachains_configuration::migration::v5::MigrateToV5, + parachains_scheduler::migration::v1::MigrateToV1, ); /// Executive: handles dispatch to the various modules. diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index dba8970f15a1..8252b98d3c3a 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1216,6 +1216,7 @@ pub type Migrations = ( >, /* Asynchronous backing mirgration */ parachains_configuration::migration::v5::MigrateToV5, + parachains_scheduler::migration::v1::MigrateToV1, ); /// Unchecked extrinsic type as expected by this runtime. From 9c061c971f4a047cf4e79f98f65155e3012b5674 Mon Sep 17 00:00:00 2001 From: Chris Sosnin Date: Mon, 10 Apr 2023 17:39:47 +0400 Subject: [PATCH 2/2] Adjust weight to account for storage read --- runtime/parachains/src/scheduler/migration.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/parachains/src/scheduler/migration.rs b/runtime/parachains/src/scheduler/migration.rs index 638a973e6742..88569dfec11b 100644 --- a/runtime/parachains/src/scheduler/migration.rs +++ b/runtime/parachains/src/scheduler/migration.rs @@ -52,13 +52,13 @@ pub mod v1 { pub struct MigrateToV1(sp_std::marker::PhantomData); impl OnRuntimeUpgrade for MigrateToV1 { fn on_runtime_upgrade() -> Weight { - let mut weight: Weight = Weight::zero(); + let mut weight: Weight = T::DbWeight::get().reads(1); if StorageVersion::get::>() < STORAGE_VERSION { log::info!(target: scheduler::LOG_TARGET, "Migrating scheduler storage to v1"); weight = weight .saturating_add(migrate::()) - .saturating_add(T::DbWeight::get().reads_writes(1, 1)); + .saturating_add(T::DbWeight::get().writes(1)); STORAGE_VERSION.put::>(); } else { log::info!(