-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* generic nuke migration * moved to runtime-common to use it generically * restore altair runtime * remove limit when nuking
- Loading branch information
Showing
7 changed files
with
139 additions
and
104 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2023 Centrifuge Foundation (centrifuge.io). | ||
// This file is part of Centrifuge chain project. | ||
|
||
// Centrifuge 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 (see http://www.gnu.org/licenses). | ||
|
||
// Centrifuge 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. | ||
|
||
#[cfg(feature = "try-runtime")] | ||
use frame_support::ensure; | ||
use frame_support::{ | ||
dispatch::GetStorageVersion, | ||
storage::unhashed, | ||
traits::{Get, OnRuntimeUpgrade, PalletInfoAccess, StorageVersion}, | ||
weights::{RuntimeDbWeight, Weight}, | ||
}; | ||
#[cfg(feature = "try-runtime")] | ||
use sp_std::vec::Vec; | ||
|
||
/// This upgrade nukes all storages from the pallet individually. | ||
/// This upgrade is only executed if pallet version has changed. | ||
/// | ||
/// To handle possible issues forgeting removing the upgrade, | ||
/// you must specify the ON_CHAIN_VERSION, | ||
/// which represent the expected previous on-chain version when the upgrade is | ||
/// done. If these numbers mistmatch, the upgrade will not take effect. | ||
pub struct Migration<Pallet, DbWeight, const ON_CHAIN_VERSION: u16>( | ||
sp_std::marker::PhantomData<(Pallet, DbWeight)>, | ||
); | ||
|
||
impl<Pallet, DbWeight, const ON_CHAIN_VERSION: u16> OnRuntimeUpgrade | ||
for Migration<Pallet, DbWeight, ON_CHAIN_VERSION> | ||
where | ||
Pallet: GetStorageVersion + PalletInfoAccess, | ||
DbWeight: Get<RuntimeDbWeight>, | ||
{ | ||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<Vec<u8>, &'static str> { | ||
ensure!( | ||
Pallet::on_chain_storage_version() == StorageVersion::new(ON_CHAIN_VERSION), | ||
"Pallet on-chain version must match with ON_CHAIN_VERSION" | ||
); | ||
|
||
ensure!( | ||
Pallet::on_chain_storage_version() < Pallet::current_storage_version(), | ||
"Pallet is already updated" | ||
); | ||
|
||
ensure!( | ||
unhashed::contains_prefixed_key(&pallet_prefix::<Pallet>()), | ||
"Pallet prefix doesn't exists" | ||
); | ||
|
||
Ok(Vec::new()) | ||
} | ||
|
||
fn on_runtime_upgrade() -> Weight { | ||
if Pallet::on_chain_storage_version() != StorageVersion::new(ON_CHAIN_VERSION) { | ||
log::error!( | ||
"Nuke-{}: nuke aborted. This upgrade must be removed!", | ||
Pallet::name() | ||
); | ||
return Weight::zero(); | ||
} | ||
|
||
if Pallet::on_chain_storage_version() < Pallet::current_storage_version() { | ||
log::info!("Nuke-{}: nuking pallet...", Pallet::name()); | ||
|
||
let result = unhashed::clear_prefix(&pallet_prefix::<Pallet>(), None, None); | ||
match result.maybe_cursor { | ||
None => log::info!("Nuke-{}: storage cleared successful", Pallet::name()), | ||
Some(_) => { | ||
// TODO: Should we loop over maybe_cursor as a new prefix? | ||
// By now, returning error. | ||
log::error!("Nuke-{}: storage not totally cleared", Pallet::name()) | ||
} | ||
} | ||
|
||
log::info!( | ||
"Nuke-{}: iteration result. backend: {} unique: {} loops: {}", | ||
Pallet::name(), | ||
result.backend, | ||
result.unique, | ||
result.loops, | ||
); | ||
|
||
Pallet::current_storage_version().put::<Pallet>(); | ||
|
||
DbWeight::get().writes(result.unique.into()) | ||
+ DbWeight::get().reads(result.loops.into()) | ||
+ DbWeight::get().reads_writes(1, 1) // Version read & writen | ||
} else { | ||
log::warn!( | ||
"Nuke-{}: pallet on-chain version is not {:?}. This upgrade can be removed.", | ||
Pallet::name(), | ||
Pallet::current_storage_version() | ||
); | ||
DbWeight::get().reads(1) | ||
} | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> { | ||
assert_eq!( | ||
Pallet::on_chain_storage_version(), | ||
Pallet::current_storage_version(), | ||
"on-chain storage version should have been updated" | ||
); | ||
|
||
ensure!( | ||
!unhashed::contains_prefixed_key(&pallet_prefix::<Pallet>()), | ||
"Pallet prefix still exists!" | ||
); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn pallet_prefix<Pallet: PalletInfoAccess>() -> [u8; 16] { | ||
sp_io::hashing::twox_128(Pallet::name().as_bytes()) | ||
} |