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

scheduler: update storage migration #6963

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,8 @@ pub type Migrations = (
// Unreleased - add new migrations here:
pallet_nomination_pools::migration::v5::MigrateToV5<Runtime>,
parachains_configuration::migration::v5::MigrateToV5<Runtime>,
/* Asynchronous backing mirgration */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think we should prefer consistent comment syntax?

Suggested change
/* Asynchronous backing mirgration */
// Asynchronous backing mirgration

parachains_scheduler::migration::v1::MigrateToV1<Runtime>,
);

/// Unchecked extrinsic type as expected by this runtime.
Expand Down
12 changes: 3 additions & 9 deletions runtime/parachains/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)]
Expand Down Expand Up @@ -166,13 +167,6 @@ pub mod pallet {
#[pallet::config]
pub trait Config: frame_system::Config + configuration::Config + paras::Config {}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migration::on_runtime_upgrade::<T>()
}
}

/// 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.
Expand Down
49 changes: 29 additions & 20 deletions runtime/parachains/src/scheduler/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Config>() -> Weight {
let mut weight: Weight = Weight::zero();

if StorageVersion::get::<Pallet<T>>() == 0 {
weight = weight
.saturating_add(v1::migrate::<T>())
.saturating_add(T::DbWeight::get().writes(1));
StorageVersion::new(1).put::<Pallet<T>>();
}

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)]
Expand All @@ -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<T: Config>() -> Weight {
pub struct MigrateToV1<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for MigrateToV1<T> {
fn on_runtime_upgrade() -> Weight {
let mut weight: Weight = T::DbWeight::get().reads(1);

if StorageVersion::get::<Pallet<T>>() < STORAGE_VERSION {
log::info!(target: scheduler::LOG_TARGET, "Migrating scheduler storage to v1");
weight = weight
.saturating_add(migrate::<T>())
.saturating_add(T::DbWeight::get().writes(1));
STORAGE_VERSION.put::<Pallet<T>>();
} else {
log::info!(
target: scheduler::LOG_TARGET,
"Scheduler storage up to date - no need for migration"
);
}

weight
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we account for the StorageVersion::get read in the else case? Right now the weight in that case is zero().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also just initialise the weight to 1 read, since it needs to be accounted for regardless of what happens in the conditional

}
}

fn migrate<T: Config>() -> Weight {
let _ = Scheduled::<T>::translate(|scheduled: Option<Vec<v0::CoreAssignment>>| {
scheduled.map(|scheduled| {
scheduled.into_iter().map(|old| scheduler::CoreAssignment::from(old)).collect()
Expand Down
2 changes: 2 additions & 0 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,8 @@ pub type Migrations = (
// Unreleased - add new migrations here:
pallet_nomination_pools::migration::v5::MigrateToV5<Runtime>,
parachains_configuration::migration::v5::MigrateToV5<Runtime>,
/* Asynchronous backing mirgration */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: consistent comment syntax

Suggested change
/* Asynchronous backing mirgration */
// Asynchronous backing mirgration

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intentional. Similar to

/***** Asynchronous backing *****/
/// Returns the state of parachain backing for a given para.
/// This is a staging method! Do not use on production runtimes!
#[api_version(99)]
fn staging_para_backing_state(_: ppp::Id) -> Option<vstaging::BackingState<H, N>>;
/// Returns candidate's acceptance limitations for asynchronous backing for a relay parent.
#[api_version(99)]
fn staging_async_backing_params() -> vstaging::AsyncBackingParams;

Note that this PR targets feature branch, not master. Our migrations shouldn't be dropped unless applied on all networks, unlike migrations coming from master.

parachains_scheduler::migration::v1::MigrateToV1<Runtime>,
);

/// Unchecked extrinsic type as expected by this runtime.
Expand Down
6 changes: 5 additions & 1 deletion runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,7 +1495,11 @@ pub type UncheckedExtrinsic =
///
/// This contains the combined migrations of the last 10 releases. It allows to skip runtime
/// upgrades in case governance decides to do so.
pub type Migrations = parachains_configuration::migration::v5::MigrateToV5<Runtime>;
pub type Migrations = (
parachains_configuration::migration::v5::MigrateToV5<Runtime>,
/* Asynchronous backing mirgration */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Asynchronous backing mirgration */
// Asynchronous backing mirgration

parachains_scheduler::migration::v1::MigrateToV1<Runtime>,
);

/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Expand Down
2 changes: 2 additions & 0 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,8 @@ pub type Migrations = (
// Unreleased - add new migrations here:
pallet_nomination_pools::migration::v5::MigrateToV5<Runtime>,
parachains_configuration::migration::v5::MigrateToV5<Runtime>,
/* Asynchronous backing mirgration */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/* Asynchronous backing mirgration */
// Asynchronous backing mirgration

parachains_scheduler::migration::v1::MigrateToV1<Runtime>,
);

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