Skip to content

Commit

Permalink
Generic nuke migrations (#1492)
Browse files Browse the repository at this point in the history
* generic nuke migration

* moved to runtime-common to use it generically

* restore altair runtime

* remove limit when nuking
  • Loading branch information
lemunozm committed Aug 17, 2023
1 parent d4e64a9 commit 5ae0caa
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 104 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions pallets/loans/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ strum = { version = "0.24", default-features = false, features = ["derive"] }
# Optionals for benchmarking
frame-benchmarking = { git = "https://github.com/paritytech/substrate", default-features = false, optional = true, branch = "polkadot-v0.9.38" }

# Used for migrations (no longer needed once migratios is done)
log = "0.4"
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
Expand All @@ -49,7 +45,6 @@ cfg-mocks = { path = "../../libs/mocks" }
default = ["std"]
std = [
"codec/std",
"log/std",
"scale-info/std",
"frame-support/std",
"frame-system/std",
Expand All @@ -60,7 +55,6 @@ std = [
"cfg-traits/std",
"cfg-types/std",
"frame-benchmarking/std",
"sp-io/std",
"strum/std",
"orml-traits/std",
]
Expand Down
6 changes: 1 addition & 5 deletions pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@
//! [`Pallet::update_portfolio_valuation()`] that should go through all active
//! loans.

pub mod migrations {
pub mod nuke;
}

/// High level types that uses `pallet::Config`
pub mod entities {
pub mod interest;
Expand Down Expand Up @@ -119,7 +115,7 @@ pub mod pallet {
pub type ChangeOf<T> =
Change<<T as Config>::LoanId, <T as Config>::Rate, <T as Config>::MaxWriteOffPolicySize>;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
Expand Down
91 changes: 0 additions & 91 deletions pallets/loans/src/migrations/nuke.rs

This file was deleted.

8 changes: 7 additions & 1 deletion runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,19 @@ pallet-liquidity-pools = { path = "../../pallets/liquidity-pools", default-featu
pallet-loans = { path = "../../pallets/loans", default-features = false }
pallet-pool-system = { path = "../../pallets/pool-system", default-features = false }

# Used for migrations
log = "0.4"
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }

[dev-dependencies]
hex-literal = "0.2.1"
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.38" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = true, branch = "polkadot-v0.9.38" }

[features]
default = ["std"]
std = [
"codec/std",
"log/std",
"frame-support/std",
"frame-system/std",
"pallet-authorship/std",
Expand All @@ -91,6 +96,7 @@ std = [
"sp-arithmetic/std",
"sp-core/std",
"sp-runtime/std",
"sp-io/std",
"cfg-types/std",
"pallet-anchors/std",
"frame-support/std",
Expand Down
4 changes: 4 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#[cfg(test)]
mod tests;

pub mod migrations {
pub mod nuke;
}

pub mod account_conversion;
pub mod apis;
pub mod evm;
Expand Down
126 changes: 126 additions & 0 deletions runtime/common/src/migrations/nuke.rs
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())
}

0 comments on commit 5ae0caa

Please sign in to comment.