Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: relocate remove_sudo migration code #936

Merged
merged 2 commits into from
Nov 7, 2022
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
1 change: 0 additions & 1 deletion runtime/litmus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ pub type Executive = frame_executive::Executive<
// it was reverse order before.
// See the comment before collation related pallets too.
AllPalletsWithSystem,
migration::RemoveSudoAndStorage<Runtime>,
>;

impl_opaque_keys! {
Expand Down
70 changes: 70 additions & 0 deletions runtime/litmus/src/migration/P9115.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2020-2021 Litentry Technologies GmbH.
// This file is part of Litentry.
//
// Litentry 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.
//
// Litentry 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 Litentry. If not, see <https://www.gnu.org/licenses/>.
use frame_support::{
traits::{Get, OnRuntimeUpgrade},
StorageHasher, Twox128,
};
use sp_std::marker::PhantomData;

pub struct RemoveSudoAndStorage<T>(PhantomData<T>);
impl<T> OnRuntimeUpgrade for RemoveSudoAndStorage<T>
where
T: frame_system::Config,
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
log::info!("Pre check pallet Sudo exists");
assert!(
frame_support::storage::migration::have_storage_value(b"Sudo", b"Key", b"",),
"Storage query fails: Sudo Key"
);
Ok(())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
use sp_io::KillStorageResult;
// Remove Sudo Storage
// TODO: Very Weak safety
let entries: u64 = 4 + 100;
let _res: KillStorageResult = frame_support::storage::unhashed::clear_prefix(
&Twox128::hash(b"Sudo"),
Some(entries.try_into().unwrap()),
None,
)
.into();
<T as frame_system::Config>::DbWeight::get().writes(entries)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
use sp_io::KillStorageResult;

log::info!("Post check Sudo");
let res: KillStorageResult =
frame_support::storage::unhashed::clear_prefix(&Twox128::hash(b"Sudo"), Some(0), None)
.into();

match res {
KillStorageResult::AllRemoved(0) | KillStorageResult::SomeRemaining(0) => {},
KillStorageResult::AllRemoved(n) | KillStorageResult::SomeRemaining(n) => {
log::error!("Remaining entries: {:?}", n);
return Err("Sudo not removed")
},
};

Ok(())
}
}
6 changes: 6 additions & 0 deletions runtime/litmus/src/migration/migration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
P9115.rs
https://github.com/litentry/litentry-parachain/releases/tag/v0.9.11-1
# This migration is for the remove of Sudo on Litmus

<!-- RemoveSudoAndStorage -->
# The main purpose of runtime upgrade is for removing sudo and its storage.
69 changes: 0 additions & 69 deletions runtime/litmus/src/migration/mod.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1 @@
// Copyright 2020-2021 Litentry Technologies GmbH.
// This file is part of Litentry.
//
// Litentry 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.
//
// Litentry 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 Litentry. If not, see <https://www.gnu.org/licenses/>.
use frame_support::{
traits::{Get, OnRuntimeUpgrade},
StorageHasher, Twox128,
};
use sp_std::marker::PhantomData;

pub struct RemoveSudoAndStorage<T>(PhantomData<T>);
impl<T> OnRuntimeUpgrade for RemoveSudoAndStorage<T>
where
T: frame_system::Config,
{
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
log::info!("Pre check pallet Sudo exists");
assert!(
frame_support::storage::migration::have_storage_value(b"Sudo", b"Key", b"",),
"Storage query fails: Sudo Key"
);
Ok(())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
use sp_io::KillStorageResult;
// Remove Sudo Storage
// TODO: Very Weak safety
let entries: u64 = 4 + 100;
let _res: KillStorageResult = frame_support::storage::unhashed::clear_prefix(
&Twox128::hash(b"Sudo"),
Some(entries.try_into().unwrap()),
None,
)
.into();
<T as frame_system::Config>::DbWeight::get().writes(entries)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
use sp_io::KillStorageResult;

log::info!("Post check Sudo");
let res: KillStorageResult =
frame_support::storage::unhashed::clear_prefix(&Twox128::hash(b"Sudo"), Some(0), None)
.into();

match res {
KillStorageResult::AllRemoved(0) | KillStorageResult::SomeRemaining(0) => {},
KillStorageResult::AllRemoved(n) | KillStorageResult::SomeRemaining(n) => {
log::error!("Remaining entries: {:?}", n);
return Err("Sudo not removed")
},
};

Ok(())
}
}