Clears Old Storage for Session pallet (#7132)

* Fixes migration for Session pallet

* Moves migration to polkadot

* Minor change

* Fixes test

* ".git/.scripts/commands/fmt/fmt.sh"

* Allow dead_code for test

* removes test

* Minor change

* Fixes build

* Import vec for try-runtime

* Addresses review comment

* ".git/.scripts/commands/fmt/fmt.sh"

* Addresses review comment

---------

Co-authored-by: command-bot <>
This commit is contained in:
gupnik
2023-04-27 10:02:15 +05:30
committed by GitHub
parent 703009fb39
commit 9ed28d07a0
5 changed files with 91 additions and 0 deletions
+2
View File
@@ -27,9 +27,11 @@ pub mod impls;
pub mod paras_registrar; pub mod paras_registrar;
pub mod paras_sudo_wrapper; pub mod paras_sudo_wrapper;
pub mod purchase; pub mod purchase;
pub mod session;
pub mod slot_range; pub mod slot_range;
pub mod slots; pub mod slots;
pub mod traits; pub mod traits;
#[cfg(feature = "try-runtime")] #[cfg(feature = "try-runtime")]
pub mod try_runtime; pub mod try_runtime;
pub mod xcm_sender; pub mod xcm_sender;
@@ -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 <http://www.gnu.org/licenses/>.
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<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for ClearOldSessionStorage<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'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<u8>) -> Result<(), &'static str> {
Ok(())
}
}
@@ -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 <http://www.gnu.org/licenses/>.
pub mod migration;
+1
View File
@@ -1472,6 +1472,7 @@ pub type Migrations = (
// Unreleased - add new migrations here: // Unreleased - add new migrations here:
parachains_configuration::migration::v5::MigrateToV5<Runtime>, parachains_configuration::migration::v5::MigrateToV5<Runtime>,
pallet_offences::migration::v1::MigrateToV1<Runtime>, pallet_offences::migration::v1::MigrateToV1<Runtime>,
runtime_common::session::migration::ClearOldSessionStorage<Runtime>,
); );
/// Unchecked extrinsic type as expected by this runtime. /// Unchecked extrinsic type as expected by this runtime.
+1
View File
@@ -1428,6 +1428,7 @@ pub type Migrations = (
// Unreleased - add new migrations here: // Unreleased - add new migrations here:
parachains_configuration::migration::v5::MigrateToV5<Runtime>, parachains_configuration::migration::v5::MigrateToV5<Runtime>,
pallet_offences::migration::v1::MigrateToV1<Runtime>, pallet_offences::migration::v1::MigrateToV1<Runtime>,
runtime_common::session::migration::ClearOldSessionStorage<Runtime>,
); );
/// Unchecked extrinsic type as expected by this runtime. /// Unchecked extrinsic type as expected by this runtime.