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

Commit

Permalink
Clears Old Storage for Session pallet (#7132)
Browse files Browse the repository at this point in the history
* 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 <>
  • Loading branch information
gupnik committed Apr 27, 2023
1 parent 64170f4 commit ccffb73
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
70 changes: 70 additions & 0 deletions runtime/common/src/session/migration.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
}
17 changes: 17 additions & 0 deletions runtime/common/src/session/mod.rs
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1472,6 +1472,7 @@ pub type Migrations = (
// Unreleased - add new migrations here:
parachains_configuration::migration::v5::MigrateToV5<Runtime>,
pallet_offences::migration::v1::MigrateToV1<Runtime>,
runtime_common::session::migration::ClearOldSessionStorage<Runtime>,
);

/// Unchecked extrinsic type as expected by this runtime.
Expand Down
1 change: 1 addition & 0 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,7 @@ pub type Migrations = (
// Unreleased - add new migrations here:
parachains_configuration::migration::v5::MigrateToV5<Runtime>,
pallet_offences::migration::v1::MigrateToV1<Runtime>,
runtime_common::session::migration::ClearOldSessionStorage<Runtime>,
);

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

0 comments on commit ccffb73

Please sign in to comment.