From d12d61e201c85d446a232a32bbb5972680333a0d Mon Sep 17 00:00:00 2001 From: lemunozm Date: Fri, 8 Mar 2024 15:02:42 +0100 Subject: [PATCH 1/6] working --- pallets/loans/src/lib.rs | 9 +-- pallets/loans/src/migrations.rs | 68 +++++++++++-------- runtime/altair/src/migrations.rs | 2 + runtime/centrifuge/src/migrations.rs | 2 + .../migrations/increase_storage_version.rs | 54 +++++++++++++++ runtime/development/src/migrations.rs | 27 +------- 6 files changed, 100 insertions(+), 62 deletions(-) diff --git a/pallets/loans/src/lib.rs b/pallets/loans/src/lib.rs index 44bc9e25e8..81691abe92 100644 --- a/pallets/loans/src/lib.rs +++ b/pallets/loans/src/lib.rs @@ -65,7 +65,7 @@ mod tests; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; -mod migrations; +pub mod migrations; pub use pallet::*; pub use weights::WeightInfo; @@ -438,13 +438,6 @@ pub mod pallet { } } - #[pallet::hooks] - impl Hooks> for Pallet { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - migrations::migrate_from_v2_to_v3::() - } - } - #[pallet::call] impl Pallet { /// Creates a new loan against the collateral provided diff --git a/pallets/loans/src/migrations.rs b/pallets/loans/src/migrations.rs index 6876ade1f2..33c81ab743 100644 --- a/pallets/loans/src/migrations.rs +++ b/pallets/loans/src/migrations.rs @@ -12,7 +12,11 @@ use cfg_traits::PoolNAV; use frame_support::{ - dispatch::GetStorageVersion, inherent::Vec, log, pallet_prelude::StorageVersion, traits::Get, + dispatch::GetStorageVersion, + inherent::Vec, + log, + pallet_prelude::StorageVersion, + traits::{Get, OnRuntimeUpgrade}, weights::Weight, }; @@ -38,38 +42,44 @@ mod v2 { >; } -pub fn migrate_from_v2_to_v3() -> Weight { - if Pallet::::on_chain_storage_version() == StorageVersion::new(2) { - log::info!("Loans: Starting migration v2 -> v3"); +pub struct LoansV3(sp_std::marker::PhantomData); - let mut changed_pools = Vec::new(); - ActiveLoans::::translate::, _>(|pool_id, active_loans| { - changed_pools.push(pool_id); - Some( - active_loans - .into_iter() - .map(|(loan_id, active_loan)| (loan_id, active_loan.migrate())) - .collect::>() - .try_into() - .expect("size doest not change, qed"), - ) - }); +impl OnRuntimeUpgrade for LoansV3 { + fn on_runtime_upgrade() -> Weight { + if Pallet::::on_chain_storage_version() == StorageVersion::new(2) { + log::info!("Loans: Starting migration v2 -> v3"); - for pool_id in &changed_pools { - match Pallet::::update_nav(*pool_id) { - Ok(_) => log::info!("Loans: updated portfolio for pool_id: {pool_id:?}"), - Err(e) => log::error!("Loans: error updating the portfolio for {pool_id:?}: {e:?}"), + let mut changed_pools = Vec::new(); + ActiveLoans::::translate::, _>(|pool_id, active_loans| { + changed_pools.push(pool_id); + Some( + active_loans + .into_iter() + .map(|(loan_id, active_loan)| (loan_id, active_loan.migrate())) + .collect::>() + .try_into() + .expect("size doest not change, qed"), + ) + }); + + for pool_id in &changed_pools { + match Pallet::::update_nav(*pool_id) { + Ok(_) => log::info!("Loans: updated portfolio for pool_id: {pool_id:?}"), + Err(e) => { + log::error!("Loans: error updating the portfolio for {pool_id:?}: {e:?}") + } + } } - } - Pallet::::current_storage_version().put::>(); + Pallet::::current_storage_version().put::>(); - let count = changed_pools.len() as u64; - log::info!("Loans: Migrated {} pools", count); - T::DbWeight::get().reads_writes(count + 1, count + 1) - } else { - // wrong storage version - log::info!("Loans: Migration did not execute. This probably should be removed"); - T::DbWeight::get().reads_writes(1, 0) + let count = changed_pools.len() as u64; + log::info!("Loans: Migrated {} pools", count); + T::DbWeight::get().reads_writes(count + 1, count + 1) + } else { + // wrong storage version + log::warn!("Loans: Migration did not execute. This probably should be removed"); + T::DbWeight::get().reads_writes(1, 0) + } } } diff --git a/runtime/altair/src/migrations.rs b/runtime/altair/src/migrations.rs index 79e60ab196..8d119c5557 100644 --- a/runtime/altair/src/migrations.rs +++ b/runtime/altair/src/migrations.rs @@ -88,6 +88,8 @@ pub type UpgradeAltair1034 = ( runtime_common::migrations::increase_storage_version::Migration, // Data was already moved but storage version not increased from 0 to 4 runtime_common::migrations::increase_storage_version::Migration, + // Loans from v2 to v3 + pallet_loans::migrations::LoansV3, ); #[allow(clippy::upper_case_acronyms)] diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index b33b804a20..43ea063bbb 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -87,6 +87,8 @@ pub type UpgradeCentrifuge1025 = ( // Burns tokens from other domains that are falsly not burned when they were transferred back // to their domain burn_unburned::Migration, + // Loans from v2 to v3 + pallet_loans::migrations::LoansV3, ); // Copyright 2021 Centrifuge Foundation (centrifuge.io). diff --git a/runtime/common/src/migrations/increase_storage_version.rs b/runtime/common/src/migrations/increase_storage_version.rs index 3142eb83b2..db936eceae 100644 --- a/runtime/common/src/migrations/increase_storage_version.rs +++ b/runtime/common/src/migrations/increase_storage_version.rs @@ -76,3 +76,57 @@ where Ok(()) } } + +/// Simply bumps the storage version of a pallet +/// +/// NOTE: Use with extreme caution! Must ensure beforehand that a migration is +/// not necessary +pub struct ForceMigration( + sp_std::marker::PhantomData

, +); +impl OnRuntimeUpgrade + for ForceMigration +where + P: GetStorageVersion + PalletInfoAccess, +{ + fn on_runtime_upgrade() -> Weight { + if P::on_chain_storage_version() == FROM_VERSION { + log::info!( + "{LOG_PREFIX} Increasing storage version of {:?} from {:?} to {TO_VERSION:?}", + P::name(), + P::on_chain_storage_version(), + ); + StorageVersion::new(TO_VERSION).put::

(); + + log::info!( + "{LOG_PREFIX} chain_version {:?}", + P::on_chain_storage_version() + ); + RocksDbWeight::get().writes(1) + } else { + log::error!( + "{LOG_PREFIX} Mismatching versions. Wanted to upgrade from \ + {FROM_VERSION} but on-chain version is {:?}", + P::on_chain_storage_version(), + ); + Zero::zero() + } + } + + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, sp_runtime::DispatchError> { + assert_eq!( + P::on_chain_storage_version(), + FROM_VERSION, + "Unexpected onchain version: Expected {FROM_VERSION:?}, received {:?}", + P::on_chain_storage_version(), + ); + Ok(sp_std::vec![]) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(_: sp_std::vec::Vec) -> Result<(), sp_runtime::DispatchError> { + assert_eq!(P::on_chain_storage_version(), TO_VERSION); + Ok(()) + } +} diff --git a/runtime/development/src/migrations.rs b/runtime/development/src/migrations.rs index bf56c19a45..2b4a04155f 100644 --- a/runtime/development/src/migrations.rs +++ b/runtime/development/src/migrations.rs @@ -24,29 +24,6 @@ frame_support::parameter_types! { } pub type UpgradeDevelopment1042 = ( - // Register LocalUSDC - runtime_common::migrations::local_currency::register::Migration< - super::Runtime, - LocalCurrencyIdUsdc, - >, - // Init local representation for all assets - runtime_common::migrations::local_currency::translate_metadata::Migration< - super::Runtime, - UsdcVariants, - LocalAssetIdUsdc, - >, - // Migrate EpochExecution struct - runtime_common::migrations::epoch_execution::Migration, - // Reset pallets - runtime_common::migrations::nuke::ResetPallet, - runtime_common::migrations::nuke::ResetPallet< - crate::TransferAllowList, - crate::RocksDbWeight, - 0, - >, - // Apply relative treasury inflation - pallet_block_rewards::migrations::v2::RelativeTreasuryInflationMigration< - crate::Runtime, - AnnualTreasuryInflationPercent, - >, + runtime_common::migrations::increase_storage_version::ForceMigration, + pallet_loans::migrations::LoansV3, ); From d1d55bfb3010cfcfff094380899d86719d720d07 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Fri, 8 Mar 2024 15:04:11 +0100 Subject: [PATCH 2/6] minimal diff changes --- pallets/loans/src/lib.rs | 9 +++- pallets/loans/src/migrations.rs | 68 ++++++++++++--------------- runtime/altair/src/migrations.rs | 2 - runtime/centrifuge/src/migrations.rs | 2 - runtime/development/src/migrations.rs | 6 +-- 5 files changed, 39 insertions(+), 48 deletions(-) diff --git a/pallets/loans/src/lib.rs b/pallets/loans/src/lib.rs index 81691abe92..44bc9e25e8 100644 --- a/pallets/loans/src/lib.rs +++ b/pallets/loans/src/lib.rs @@ -65,7 +65,7 @@ mod tests; #[cfg(feature = "runtime-benchmarks")] mod benchmarking; -pub mod migrations; +mod migrations; pub use pallet::*; pub use weights::WeightInfo; @@ -438,6 +438,13 @@ pub mod pallet { } } + #[pallet::hooks] + impl Hooks> for Pallet { + fn on_runtime_upgrade() -> frame_support::weights::Weight { + migrations::migrate_from_v2_to_v3::() + } + } + #[pallet::call] impl Pallet { /// Creates a new loan against the collateral provided diff --git a/pallets/loans/src/migrations.rs b/pallets/loans/src/migrations.rs index 33c81ab743..ace3393389 100644 --- a/pallets/loans/src/migrations.rs +++ b/pallets/loans/src/migrations.rs @@ -12,11 +12,7 @@ use cfg_traits::PoolNAV; use frame_support::{ - dispatch::GetStorageVersion, - inherent::Vec, - log, - pallet_prelude::StorageVersion, - traits::{Get, OnRuntimeUpgrade}, + dispatch::GetStorageVersion, inherent::Vec, log, pallet_prelude::StorageVersion, traits::Get, weights::Weight, }; @@ -42,44 +38,38 @@ mod v2 { >; } -pub struct LoansV3(sp_std::marker::PhantomData); +pub fn migrate_from_v2_to_v3() -> Weight { + if Pallet::::on_chain_storage_version() == StorageVersion::new(2) { + log::info!("Loans: Starting migration v2 -> v3"); -impl OnRuntimeUpgrade for LoansV3 { - fn on_runtime_upgrade() -> Weight { - if Pallet::::on_chain_storage_version() == StorageVersion::new(2) { - log::info!("Loans: Starting migration v2 -> v3"); + let mut changed_pools = Vec::new(); + ActiveLoans::::translate::, _>(|pool_id, active_loans| { + changed_pools.push(pool_id); + Some( + active_loans + .into_iter() + .map(|(loan_id, active_loan)| (loan_id, active_loan.migrate())) + .collect::>() + .try_into() + .expect("size doest not change, qed"), + ) + }); - let mut changed_pools = Vec::new(); - ActiveLoans::::translate::, _>(|pool_id, active_loans| { - changed_pools.push(pool_id); - Some( - active_loans - .into_iter() - .map(|(loan_id, active_loan)| (loan_id, active_loan.migrate())) - .collect::>() - .try_into() - .expect("size doest not change, qed"), - ) - }); - - for pool_id in &changed_pools { - match Pallet::::update_nav(*pool_id) { - Ok(_) => log::info!("Loans: updated portfolio for pool_id: {pool_id:?}"), - Err(e) => { - log::error!("Loans: error updating the portfolio for {pool_id:?}: {e:?}") - } - } + for pool_id in &changed_pools { + match Pallet::::update_nav(*pool_id) { + Ok(_) => log::info!("Loans: updated portfolio for pool_id: {pool_id:?}"), + Err(e) => log::error!("Loans: error updating the portfolio for {pool_id:?}: {e:?}"), } + } - Pallet::::current_storage_version().put::>(); + Pallet::::current_storage_version().put::>(); - let count = changed_pools.len() as u64; - log::info!("Loans: Migrated {} pools", count); - T::DbWeight::get().reads_writes(count + 1, count + 1) - } else { - // wrong storage version - log::warn!("Loans: Migration did not execute. This probably should be removed"); - T::DbWeight::get().reads_writes(1, 0) - } + let count = changed_pools.len() as u64; + log::info!("Loans: Migrated {} pools", count); + T::DbWeight::get().reads_writes(count + 1, count + 1) + } else { + // wrong storage version + log::warn!("Loans: Migration did not execute. This probably should be removed"); + T::DbWeight::get().reads_writes(1, 0) } } diff --git a/runtime/altair/src/migrations.rs b/runtime/altair/src/migrations.rs index 8d119c5557..79e60ab196 100644 --- a/runtime/altair/src/migrations.rs +++ b/runtime/altair/src/migrations.rs @@ -88,8 +88,6 @@ pub type UpgradeAltair1034 = ( runtime_common::migrations::increase_storage_version::Migration, // Data was already moved but storage version not increased from 0 to 4 runtime_common::migrations::increase_storage_version::Migration, - // Loans from v2 to v3 - pallet_loans::migrations::LoansV3, ); #[allow(clippy::upper_case_acronyms)] diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 43ea063bbb..b33b804a20 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -87,8 +87,6 @@ pub type UpgradeCentrifuge1025 = ( // Burns tokens from other domains that are falsly not burned when they were transferred back // to their domain burn_unburned::Migration, - // Loans from v2 to v3 - pallet_loans::migrations::LoansV3, ); // Copyright 2021 Centrifuge Foundation (centrifuge.io). diff --git a/runtime/development/src/migrations.rs b/runtime/development/src/migrations.rs index 2b4a04155f..0fb435f3f5 100644 --- a/runtime/development/src/migrations.rs +++ b/runtime/development/src/migrations.rs @@ -23,7 +23,5 @@ frame_support::parameter_types! { pub const AnnualTreasuryInflationPercent: u32 = 3; } -pub type UpgradeDevelopment1042 = ( - runtime_common::migrations::increase_storage_version::ForceMigration, - pallet_loans::migrations::LoansV3, -); +pub type UpgradeDevelopment1042 = + (runtime_common::migrations::increase_storage_version::ForceMigration,); From 44c60da3549f0c9fbd19597965fe87721a9114a3 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Fri, 8 Mar 2024 15:11:21 +0100 Subject: [PATCH 3/6] minor log clean --- runtime/common/src/migrations/increase_storage_version.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/runtime/common/src/migrations/increase_storage_version.rs b/runtime/common/src/migrations/increase_storage_version.rs index db936eceae..0caf8151d6 100644 --- a/runtime/common/src/migrations/increase_storage_version.rs +++ b/runtime/common/src/migrations/increase_storage_version.rs @@ -98,10 +98,6 @@ where ); StorageVersion::new(TO_VERSION).put::

(); - log::info!( - "{LOG_PREFIX} chain_version {:?}", - P::on_chain_storage_version() - ); RocksDbWeight::get().writes(1) } else { log::error!( From 17ea4a527cc0269bd526c3606fde60306f451585 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Fri, 8 Mar 2024 15:15:50 +0100 Subject: [PATCH 4/6] removed unused imports --- runtime/development/src/migrations.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/runtime/development/src/migrations.rs b/runtime/development/src/migrations.rs index 0fb435f3f5..ae35b2d635 100644 --- a/runtime/development/src/migrations.rs +++ b/runtime/development/src/migrations.rs @@ -10,18 +10,5 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -use cfg_types::tokens::{ - usdc::{CURRENCY_ID_DOT_NATIVE, CURRENCY_ID_LOCAL, CURRENCY_ID_LP_ETH, LOCAL_ASSET_ID}, - CurrencyId, LocalAssetId, -}; - -frame_support::parameter_types! { - pub const UsdcVariants: [CurrencyId; 1] = [CURRENCY_ID_LP_ETH]; - pub const LocalAssetIdUsdc: LocalAssetId = LOCAL_ASSET_ID; - pub const LocalCurrencyIdUsdc: CurrencyId = CURRENCY_ID_LOCAL; - pub const PoolCurrencyAnemoy: CurrencyId = CURRENCY_ID_DOT_NATIVE; - pub const AnnualTreasuryInflationPercent: u32 = 3; -} - pub type UpgradeDevelopment1042 = (runtime_common::migrations::increase_storage_version::ForceMigration,); From 5824aed221a90839e0518b0452dcd8801426eeb8 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Fri, 8 Mar 2024 15:17:33 +0100 Subject: [PATCH 5/6] add log line --- runtime/common/src/migrations/increase_storage_version.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/runtime/common/src/migrations/increase_storage_version.rs b/runtime/common/src/migrations/increase_storage_version.rs index 0caf8151d6..d8e6afba11 100644 --- a/runtime/common/src/migrations/increase_storage_version.rs +++ b/runtime/common/src/migrations/increase_storage_version.rs @@ -91,6 +91,7 @@ where { fn on_runtime_upgrade() -> Weight { if P::on_chain_storage_version() == FROM_VERSION { + log::warn!("Double-check you really want this migration!!!!",); log::info!( "{LOG_PREFIX} Increasing storage version of {:?} from {:?} to {TO_VERSION:?}", P::name(), From 590d76b575a36124e8c11485fd920b4ca214b076 Mon Sep 17 00:00:00 2001 From: lemunozm Date: Fri, 8 Mar 2024 17:14:22 +0100 Subject: [PATCH 6/6] bump to 1043 --- Cargo.lock | 2 +- runtime/development/Cargo.toml | 2 +- runtime/development/src/lib.rs | 4 ++-- runtime/development/src/migrations.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ea182404e..226abb0a14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2451,7 +2451,7 @@ dependencies = [ [[package]] name = "development-runtime" -version = "0.10.42" +version = "0.10.43" dependencies = [ "axelar-gateway-precompile", "cfg-primitives", diff --git a/runtime/development/Cargo.toml b/runtime/development/Cargo.toml index bd1983ea42..05dff3db18 100644 --- a/runtime/development/Cargo.toml +++ b/runtime/development/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "development-runtime" -version = "0.10.42" +version = "0.10.43" build = "build.rs" authors.workspace = true edition.workspace = true diff --git a/runtime/development/src/lib.rs b/runtime/development/src/lib.rs index 820feb386d..d72d9b0878 100644 --- a/runtime/development/src/lib.rs +++ b/runtime/development/src/lib.rs @@ -157,7 +157,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("centrifuge-devel"), impl_name: create_runtime_str!("centrifuge-devel"), authoring_version: 1, - spec_version: 1042, + spec_version: 1043, impl_version: 1, #[cfg(not(feature = "disable-runtime-api"))] apis: RUNTIME_API_VERSIONS, @@ -2040,7 +2040,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - crate::migrations::UpgradeDevelopment1042, + crate::migrations::UpgradeDevelopment1043, >; // Frame Order in this block dictates the index of each one in the metadata diff --git a/runtime/development/src/migrations.rs b/runtime/development/src/migrations.rs index ae35b2d635..fff72d8738 100644 --- a/runtime/development/src/migrations.rs +++ b/runtime/development/src/migrations.rs @@ -10,5 +10,5 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -pub type UpgradeDevelopment1042 = +pub type UpgradeDevelopment1043 = (runtime_common::migrations::increase_storage_version::ForceMigration,);