diff --git a/polkadot/runtime/common/src/lib.rs b/polkadot/runtime/common/src/lib.rs index 7ba653db3c..8c68ff97fc 100644 --- a/polkadot/runtime/common/src/lib.rs +++ b/polkadot/runtime/common/src/lib.rs @@ -27,9 +27,11 @@ pub mod impls; pub mod paras_registrar; pub mod paras_sudo_wrapper; pub mod purchase; +pub mod session; pub mod slot_range; pub mod slots; pub mod traits; + #[cfg(feature = "try-runtime")] pub mod try_runtime; pub mod xcm_sender; diff --git a/polkadot/runtime/common/src/session/migration.rs b/polkadot/runtime/common/src/session/migration.rs new file mode 100644 index 0000000000..87bcd8d7db --- /dev/null +++ b/polkadot/runtime/common/src/session/migration.rs @@ -0,0 +1,70 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +use frame_support::{ + storage::storage_prefix, + traits::{Get, OnRuntimeUpgrade}, + weights::Weight, +}; +use pallet_session::Config; +use sp_io::{storage::clear_prefix, KillStorageResult}; +#[cfg(feature = "try-runtime")] +use sp_std::vec::Vec; + +/// This migration clears everything under `Session::HistoricalSessions` +/// and `Session::StoredRange` that were not cleared when +/// the pallet was moved to its new prefix (`Historical`) +pub struct ClearOldSessionStorage(sp_std::marker::PhantomData); +impl OnRuntimeUpgrade for ClearOldSessionStorage { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + Ok(Vec::new()) + } + + fn on_runtime_upgrade() -> Weight { + let prefix = storage_prefix(b"Session", b"StoredRange"); + let keys_removed_stored_range = match clear_prefix(&prefix, None) { + KillStorageResult::AllRemoved(value) => value, + KillStorageResult::SomeRemaining(value) => { + log::error!( + "`clear_prefix` failed to remove all keys. THIS SHOULD NEVER HAPPEN! ๐Ÿšจ", + ); + value + }, + } as u64; + + let prefix = storage_prefix(b"Session", b"HistoricalSessions"); + let keys_removed_historical_sessions = match clear_prefix(&prefix, None) { + KillStorageResult::AllRemoved(value) => value, + KillStorageResult::SomeRemaining(value) => { + log::error!( + "`clear_prefix` failed to remove all keys. THIS SHOULD NEVER HAPPEN! ๐Ÿšจ", + ); + value + }, + } as u64; + + let keys_removed = keys_removed_stored_range + keys_removed_historical_sessions; + log::info!("Removed {} keys ๐Ÿงน", keys_removed); + + T::DbWeight::get().reads_writes(keys_removed, keys_removed) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_state: Vec) -> Result<(), &'static str> { + Ok(()) + } +} diff --git a/polkadot/runtime/common/src/session/mod.rs b/polkadot/runtime/common/src/session/mod.rs new file mode 100644 index 0000000000..5f99cba0eb --- /dev/null +++ b/polkadot/runtime/common/src/session/mod.rs @@ -0,0 +1,17 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +pub mod migration; diff --git a/polkadot/runtime/kusama/src/lib.rs b/polkadot/runtime/kusama/src/lib.rs index 01a56ae42b..5dd7819ea5 100644 --- a/polkadot/runtime/kusama/src/lib.rs +++ b/polkadot/runtime/kusama/src/lib.rs @@ -1472,6 +1472,7 @@ pub type Migrations = ( // Unreleased - add new migrations here: parachains_configuration::migration::v5::MigrateToV5, pallet_offences::migration::v1::MigrateToV1, + runtime_common::session::migration::ClearOldSessionStorage, ); /// Unchecked extrinsic type as expected by this runtime. diff --git a/polkadot/runtime/polkadot/src/lib.rs b/polkadot/runtime/polkadot/src/lib.rs index 2029f29053..b94a882be4 100644 --- a/polkadot/runtime/polkadot/src/lib.rs +++ b/polkadot/runtime/polkadot/src/lib.rs @@ -1428,6 +1428,7 @@ pub type Migrations = ( // Unreleased - add new migrations here: parachains_configuration::migration::v5::MigrateToV5, pallet_offences::migration::v1::MigrateToV1, + runtime_common::session::migration::ClearOldSessionStorage, ); /// Unchecked extrinsic type as expected by this runtime.