Skip to content

Commit

Permalink
Remove legacy storage
Browse files Browse the repository at this point in the history
Signed-off-by: Dengjianping <djptux@gmail.com>
  • Loading branch information
Dengjianping committed Jan 10, 2024
1 parent 812db69 commit 514579e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runtime/manta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, RuntimeCall, Si

/// Types for runtime upgrading.
/// Each type should implement trait `OnRuntimeUpgrade`.
pub type OnRuntimeUpgradeHooks = ();
pub type OnRuntimeUpgradeHooks = (migrations::manta_pay::RemoveMantaPay<Runtime>,);
/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Expand Down
110 changes: 110 additions & 0 deletions runtime/manta/src/migrations/manta_pay.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2020-2024 Manta Network.
// This file is part of Manta.
//
// Manta 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.
//
// Manta 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 Manta. If not, see <http://www.gnu.org/licenses/>.

#![allow(clippy::unnecessary_cast)]

use core::marker::PhantomData;
use frame_support::migration::clear_storage_prefix;
use frame_support::{
migration::have_storage_value,
pallet_prelude::Weight,
traits::{Get, OnRuntimeUpgrade},
};
use sp_runtime::DispatchError;
use sp_std::vec::Vec;

pub struct RemoveMantaPay<T>(PhantomData<T>);
impl<T: frame_system::Config> OnRuntimeUpgrade for RemoveMantaPay<T> {
fn on_runtime_upgrade() -> Weight {
let mut reads = 0;
let mut writes = 0;
log::info!(target: "MantaPay", "✅ {:?} has been removed.", have_storage_value(b"MantaPay", b"", b""));
// NullifierCommitmentSet
if have_storage_value(b"MantaPay", b"NullifierCommitmentSet", b"") {
clear_storage_prefix(b"MantaPay", b"Key", b"", None, None);
clear_storage_prefix(b"MantaPay", b":__STORAGE_VERSION__:", b"", None, None);
log::info!(target: "MantaPay", "✅ NullifierCommitmentSet has been removed.");
log::info!(target: "MantaPay", "✅ The pallet version has been removed.");
reads += 1;
writes += 2;
}
// NullifierSetInsertionOrder
if have_storage_value(b"MantaPay", b"NullifierSetInsertionOrder", b"") {
clear_storage_prefix(b"MantaPay", b"NullifierSetInsertionOrder", b"", None, None);
log::info!(target: "MantaPay", "✅ NullifierSetInsertionOrder has been removed.");
reads += 1;
writes += 1;
}
// NullifierSetSize
if have_storage_value(b"MantaPay", b"NullifierSetSize", b"") {
clear_storage_prefix(b"MantaPay", b"NullifierSetSize", b"", None, None);
log::info!(target: "MantaPay", "✅ NullifierSetSize has been removed.");
reads += 1;
writes += 1;
}
// ShardTrees
if have_storage_value(b"MantaPay", b"ShardTrees", b"") {
clear_storage_prefix(b"MantaPay", b"ShardTrees", b"", None, None);
log::info!(target: "MantaPay", "✅ ShardTrees has been removed.");
reads += 1;
writes += 1;
}
// Shards
if have_storage_value(b"MantaPay", b"Shards", b"") {
clear_storage_prefix(b"MantaPay", b"Shards", b"", None, None);
log::info!(target: "MantaPay", "✅ Shards has been removed.");
reads += 1;
writes += 1;
}
// UtxoAccumulatorOutputs
if have_storage_value(b"MantaPay", b"UtxoAccumulatorOutputs", b"") {
clear_storage_prefix(b"MantaPay", b"UtxoAccumulatorOutputs", b"", None, None);
log::info!(target: "MantaPay", "✅ UtxoAccumulatorOutputs has been removed.");
reads += 1;
writes += 1;
}
// UtxoSet
if have_storage_value(b"MantaPay", b"UtxoSet", b"") {
clear_storage_prefix(b"MantaPay", b"UtxoSet", b"", None, None);
log::info!(target: "MantaPay", "✅ UtxoSet has been removed.");
reads += 1;
writes += 1;
}
log::info!(target: "MantaPay", "✅ {:?} has been removed.", have_storage_value(b"MantaPay", b"", b""));
T::DbWeight::get()
.reads(reads)
.saturating_add(T::DbWeight::get().writes(writes))
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, DispatchError> {
if have_storage_value(b"MantaPay", b"", b"") {
log::info!(target: "MantaPay will be removed soon.");
Ok(Vec::new())
} else {
Err(DispatchError::Other("Sudo doesn't exist."))
}
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), DispatchError> {
if have_storage_value(b"MantaPay", b"", b"") {
Err(DispatchError::Other("Failed to remove MantaPay module."))
} else {
Ok(())
}
}
}
1 change: 1 addition & 0 deletions runtime/manta/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@

pub mod asset_manager;
pub mod assets_genesis;
pub mod manta_pay;

0 comments on commit 514579e

Please sign in to comment.