From 39b4041aff0240a33a3f5cc058d3353428f00ce4 Mon Sep 17 00:00:00 2001 From: nuno Date: Fri, 22 Sep 2023 17:23:02 +0200 Subject: [PATCH 01/10] centrifuge: Anemoy pool currency migration [MVP] --- runtime/centrifuge/src/migrations.rs | 77 +++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index c3c3631ff7..e19c230905 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -9,5 +9,80 @@ // 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. +use codec::{Decode, Encode}; -pub type UpgradeCentrifuge1021 = (); +use crate::{PoolSystem, Runtime, Weight}; + +pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; + +/// Migrate the Anemoy Pool's currency from LpEthUSC to Circle's USDC, +/// native on Polkadot's AssetHub. +mod anemoy_pool { + + use cfg_primitives::PoolId; + use cfg_types::tokens::CurrencyId; + use frame_support::traits::OnRuntimeUpgrade; + use pallet_pool_system::PoolDetailsOf; + #[cfg(feature = "try-runtime")] + use sp_std::vec::Vec; + #[cfg(feature = "try-runtime")] + use frame_support::ensure; + + use super::*; + + const ANEMOY_POOL_ID: PoolId = 4_139_607_887; + const LP_ETH_USDC: CurrencyId = CurrencyId::ForeignAsset(100_001); + const DOT_NATIVE_USDC: CurrencyId = CurrencyId::ForeignAsset(6); + + pub struct Migration; + + impl OnRuntimeUpgrade for Migration { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + let pool_details: PoolDetailsOf = + PoolSystem::pool(ANEMOY_POOL_ID).ok_or("Could not find Anemoy Pool")?; + + ensure!( + pool_details.currency == LP_ETH_USDC, + "anemoy_pool::Migration: pre_upgrade failing as Anemoy's currency should be LpEthUSDC" + ); + + Ok(pool_details.encode()) + } + + fn on_runtime_upgrade() -> Weight { + // To be executed at 1021, reject higher spec_versions + if crate::VERSION.spec_version >= 1022 { + log::info!("anemoy_pool::Migration: NOT execution since VERSION.spec_version >= 1022"); + return Weight::zero(); + } + + pallet_pool_system::Pool::::mutate(ANEMOY_POOL_ID, |details| { + let details = details.as_mut().unwrap(); + details.currency = DOT_NATIVE_USDC; + log::info!("anemoy_pool::Migration: Finished mutating currency to USDC"); + }); + + ::DbWeight::get().reads_writes(1, 1) + } + + #[cfg(feature = "try-runtime")] + fn post_upgrade(old_state: Vec) -> Result<(), &'static str> { + let mut old_pool_details = PoolDetailsOf::::decode(&mut old_state.as_ref()) + .map_err(|_| "Error decoding pre-upgrade state")?; + + let pool_details: PoolDetailsOf = + PoolSystem::pool(ANEMOY_POOL_ID).ok_or("Could not find Anemoy Pool")?; + + // Ensure the currency set to USDC is the only mutation performed + old_pool_details.currency = DOT_NATIVE_USDC; + ensure!( + old_pool_details == pool_details, + "Corrupted migration: Only the currency of the Anemoy pool should have changed" + ); + + log::info!("anemoy_pool::Migration: post_upgrade succeeded"); + Ok(()) + } + } +} From 766bef35ad79415647697046868268ebf386d4cf Mon Sep 17 00:00:00 2001 From: nuno Date: Fri, 22 Sep 2023 17:59:34 +0200 Subject: [PATCH 02/10] wip: sanity checks fn --- libs/types/src/tokens.rs | 4 ++-- runtime/centrifuge/src/migrations.rs | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/libs/types/src/tokens.rs b/libs/types/src/tokens.rs index 02051f2393..12c9b7d58b 100644 --- a/libs/types/src/tokens.rs +++ b/libs/types/src/tokens.rs @@ -187,8 +187,8 @@ where )] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] pub struct TrancheCurrency { - pub(crate) pool_id: PoolId, - pub(crate) tranche_id: TrancheId, + pub pool_id: PoolId, + pub tranche_id: TrancheId, } impl From for CurrencyId { diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index e19c230905..61f03f4e5c 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -11,7 +11,7 @@ // GNU General Public License for more details. use codec::{Decode, Encode}; -use crate::{PoolSystem, Runtime, Weight}; +use crate::{Investments, PoolSystem, Runtime, Weight}; pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; @@ -19,14 +19,15 @@ pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; /// native on Polkadot's AssetHub. mod anemoy_pool { - use cfg_primitives::PoolId; - use cfg_types::tokens::CurrencyId; + use cfg_primitives::{PoolId, TrancheId}; + use cfg_types::tokens::{CurrencyId, TrancheCurrency}; use frame_support::traits::OnRuntimeUpgrade; use pallet_pool_system::PoolDetailsOf; #[cfg(feature = "try-runtime")] use sp_std::vec::Vec; #[cfg(feature = "try-runtime")] use frame_support::ensure; + use cfg_types::orders::TotalOrder; use super::*; @@ -85,4 +86,15 @@ mod anemoy_pool { Ok(()) } } + + // todo(nuno): also check that pool value is 0 and check also that Investments::InvestOrders and + // Investments::RedeemOrder have no entries from Anemoy; the latter ones seem tricky at first + // sight since they are double maps first keyed by an AccountId, meaning we need to transverse + // that first which is more costly. + fn sanity_checks(tranche_id: TrancheId) -> bool { + let tc = TrancheCurrency { pool_id: ANEMOY_POOL_ID, tranche_id}; + + Investments::acc_active_invest_order(tc) == TotalOrder::default() + && Investments::acc_active_redeem_order(tc) == TotalOrder::default() + } } From 215413d5af61269c340ca234e3cb9b24d358cfbb Mon Sep 17 00:00:00 2001 From: nuno Date: Fri, 22 Sep 2023 18:04:27 +0200 Subject: [PATCH 03/10] fixups --- runtime/centrifuge/src/migrations.rs | 34 ++++++++++++++++++---------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 61f03f4e5c..53659e68e3 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -9,8 +9,6 @@ // 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. -use codec::{Decode, Encode}; - use crate::{Investments, PoolSystem, Runtime, Weight}; pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; @@ -20,18 +18,24 @@ pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; mod anemoy_pool { use cfg_primitives::{PoolId, TrancheId}; - use cfg_types::tokens::{CurrencyId, TrancheCurrency}; + use cfg_types::{ + orders::TotalOrder, + tokens::{CurrencyId, TrancheCurrency}, + }; + #[cfg(feature = "try-runtime")] + use codec::{Decode, Encode}; + #[cfg(feature = "try-runtime")] + use frame_support::ensure; use frame_support::traits::OnRuntimeUpgrade; + #[cfg(feature = "try-runtime")] use pallet_pool_system::PoolDetailsOf; #[cfg(feature = "try-runtime")] use sp_std::vec::Vec; - #[cfg(feature = "try-runtime")] - use frame_support::ensure; - use cfg_types::orders::TotalOrder; use super::*; const ANEMOY_POOL_ID: PoolId = 4_139_607_887; + #[cfg(feature = "try-runtime")] const LP_ETH_USDC: CurrencyId = CurrencyId::ForeignAsset(100_001); const DOT_NATIVE_USDC: CurrencyId = CurrencyId::ForeignAsset(6); @@ -54,7 +58,9 @@ mod anemoy_pool { fn on_runtime_upgrade() -> Weight { // To be executed at 1021, reject higher spec_versions if crate::VERSION.spec_version >= 1022 { - log::info!("anemoy_pool::Migration: NOT execution since VERSION.spec_version >= 1022"); + log::info!( + "anemoy_pool::Migration: NOT execution since VERSION.spec_version >= 1022" + ); return Weight::zero(); } @@ -87,12 +93,16 @@ mod anemoy_pool { } } - // todo(nuno): also check that pool value is 0 and check also that Investments::InvestOrders and - // Investments::RedeemOrder have no entries from Anemoy; the latter ones seem tricky at first - // sight since they are double maps first keyed by an AccountId, meaning we need to transverse - // that first which is more costly. + // todo(nuno): also check that pool value is 0 and check also that + // Investments::InvestOrders and Investments::RedeemOrder have no entries from + // Anemoy; the latter ones seem tricky at first sight since they are double maps + // first keyed by an AccountId, meaning we need to transverse that first which + // is more costly. fn sanity_checks(tranche_id: TrancheId) -> bool { - let tc = TrancheCurrency { pool_id: ANEMOY_POOL_ID, tranche_id}; + let tc = TrancheCurrency { + pool_id: ANEMOY_POOL_ID, + tranche_id, + }; Investments::acc_active_invest_order(tc) == TotalOrder::default() && Investments::acc_active_redeem_order(tc) == TotalOrder::default() From bbb1cb746dace238d9ba8edba8529c31af9dfe36 Mon Sep 17 00:00:00 2001 From: nuno Date: Sat, 23 Sep 2023 14:17:00 +0200 Subject: [PATCH 04/10] Apply sanity checks --- pallets/investments/src/lib.rs | 6 +-- runtime/centrifuge/src/migrations.rs | 70 +++++++++++++++++++--------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/pallets/investments/src/lib.rs b/pallets/investments/src/lib.rs index ca4ef743c9..3801bbc90d 100644 --- a/pallets/investments/src/lib.rs +++ b/pallets/investments/src/lib.rs @@ -224,7 +224,7 @@ pub mod pallet { StorageMap<_, Blake2_128Concat, T::InvestmentId, OrderId, ValueQuery>; #[pallet::storage] - pub(crate) type InvestOrders = StorageDoubleMap< + pub type InvestOrders = StorageDoubleMap< _, Blake2_128Concat, T::AccountId, @@ -234,7 +234,7 @@ pub mod pallet { >; #[pallet::storage] - pub(crate) type RedeemOrders = StorageDoubleMap< + pub type RedeemOrders = StorageDoubleMap< _, Blake2_128Concat, T::AccountId, @@ -245,7 +245,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn acc_active_invest_order)] - pub(crate) type ActiveInvestOrders = + pub type ActiveInvestOrders = StorageMap<_, Blake2_128Concat, T::InvestmentId, TotalOrder, ValueQuery>; #[pallet::storage] diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 53659e68e3..4b1bc8dd7b 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -9,7 +9,7 @@ // 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. -use crate::{Investments, PoolSystem, Runtime, Weight}; +use crate::{Runtime, Weight}; pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; @@ -17,11 +17,8 @@ pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; /// native on Polkadot's AssetHub. mod anemoy_pool { - use cfg_primitives::{PoolId, TrancheId}; - use cfg_types::{ - orders::TotalOrder, - tokens::{CurrencyId, TrancheCurrency}, - }; + use cfg_primitives::PoolId; + use cfg_types::tokens::CurrencyId; #[cfg(feature = "try-runtime")] use codec::{Decode, Encode}; #[cfg(feature = "try-runtime")] @@ -33,6 +30,8 @@ mod anemoy_pool { use sp_std::vec::Vec; use super::*; + #[cfg(feature = "try-runtime")] + use crate::PoolSystem; const ANEMOY_POOL_ID: PoolId = 4_139_607_887; #[cfg(feature = "try-runtime")] @@ -58,19 +57,31 @@ mod anemoy_pool { fn on_runtime_upgrade() -> Weight { // To be executed at 1021, reject higher spec_versions if crate::VERSION.spec_version >= 1022 { - log::info!( + log::error!( "anemoy_pool::Migration: NOT execution since VERSION.spec_version >= 1022" ); return Weight::zero(); } + let (sanity_checks, weight) = verify_sanity_checks(); + if !sanity_checks { + log::error!("anemoy_pool::Migration: Sanity checks FAILED"); + return weight; + } + pallet_pool_system::Pool::::mutate(ANEMOY_POOL_ID, |details| { let details = details.as_mut().unwrap(); details.currency = DOT_NATIVE_USDC; - log::info!("anemoy_pool::Migration: Finished mutating currency to USDC"); + log::info!("anemoy_pool::Migration: currency set to USDC ✓"); }); - ::DbWeight::get().reads_writes(1, 1) + weight.saturating_add( + ::DbWeight::get().reads( + pallet_pool_system::Pool::::iter_keys() + .count() + .saturating_mul(2) as u64, + ), + ) } #[cfg(feature = "try-runtime")] @@ -88,23 +99,36 @@ mod anemoy_pool { "Corrupted migration: Only the currency of the Anemoy pool should have changed" ); - log::info!("anemoy_pool::Migration: post_upgrade succeeded"); + log::info!("anemoy_pool::Migration: post_upgrade succeeded ✓"); Ok(()) } } - // todo(nuno): also check that pool value is 0 and check also that - // Investments::InvestOrders and Investments::RedeemOrder have no entries from - // Anemoy; the latter ones seem tricky at first sight since they are double maps - // first keyed by an AccountId, meaning we need to transverse that first which - // is more costly. - fn sanity_checks(tranche_id: TrancheId) -> bool { - let tc = TrancheCurrency { - pool_id: ANEMOY_POOL_ID, - tranche_id, - }; - - Investments::acc_active_invest_order(tc) == TotalOrder::default() - && Investments::acc_active_redeem_order(tc) == TotalOrder::default() + fn verify_sanity_checks() -> (bool, Weight) { + let res = + pallet_investments::ActiveInvestOrders::::iter_keys() + .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) + .count() == 0 && pallet_investments::ActiveInvestOrders::::iter_keys() + .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) + .count() == 0 && pallet_investments::InvestOrders::::iter_keys() + .filter(|(_, investment)| investment.pool_id == ANEMOY_POOL_ID) + .count() == 0 && pallet_investments::RedeemOrders::::iter_keys() + .filter(|(_, investment)| investment.pool_id == ANEMOY_POOL_ID) + .count() == 0; + + let weight = ::DbWeight::get().reads( + 0u64.saturating_add( + pallet_investments::ActiveInvestOrders::::iter_keys().count() as u64, + ) + .saturating_add( + pallet_investments::ActiveInvestOrders::::iter_keys().count() as u64, + ) + .saturating_add(pallet_investments::InvestOrders::::iter_keys().count() as u64) + .saturating_add(pallet_investments::RedeemOrders::::iter_keys().count() as u64) + // 2x, first for the sanity checks and now for calculating these weights + .saturating_mul(2), + ); + + (res, weight) } } From 790af2d4e4938a29579883f63130ab5d7e15aa90 Mon Sep 17 00:00:00 2001 From: nuno Date: Mon, 25 Sep 2023 09:54:31 +0200 Subject: [PATCH 05/10] Check pool value == 0 --- runtime/centrifuge/src/migrations.rs | 35 +++++++++++++++++----------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 4b1bc8dd7b..6f3bc285fd 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -18,12 +18,13 @@ pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; mod anemoy_pool { use cfg_primitives::PoolId; + use cfg_traits::PoolInspect; use cfg_types::tokens::CurrencyId; #[cfg(feature = "try-runtime")] use codec::{Decode, Encode}; #[cfg(feature = "try-runtime")] use frame_support::ensure; - use frame_support::traits::OnRuntimeUpgrade; + use frame_support::traits::{fungibles::Inspect, OnRuntimeUpgrade}; #[cfg(feature = "try-runtime")] use pallet_pool_system::PoolDetailsOf; #[cfg(feature = "try-runtime")] @@ -106,9 +107,10 @@ mod anemoy_pool { fn verify_sanity_checks() -> (bool, Weight) { let res = - pallet_investments::ActiveInvestOrders::::iter_keys() - .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) - .count() == 0 && pallet_investments::ActiveInvestOrders::::iter_keys() + crate::Tokens::balance(LP_ETH_USDC, &PoolSystem::account_for(ANEMOY_POOL_ID)) == 0 + && pallet_investments::ActiveInvestOrders::::iter_keys() + .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) + .count() == 0 && pallet_investments::ActiveInvestOrders::::iter_keys() .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) .count() == 0 && pallet_investments::InvestOrders::::iter_keys() .filter(|(_, investment)| investment.pool_id == ANEMOY_POOL_ID) @@ -117,16 +119,21 @@ mod anemoy_pool { .count() == 0; let weight = ::DbWeight::get().reads( - 0u64.saturating_add( - pallet_investments::ActiveInvestOrders::::iter_keys().count() as u64, - ) - .saturating_add( - pallet_investments::ActiveInvestOrders::::iter_keys().count() as u64, - ) - .saturating_add(pallet_investments::InvestOrders::::iter_keys().count() as u64) - .saturating_add(pallet_investments::RedeemOrders::::iter_keys().count() as u64) - // 2x, first for the sanity checks and now for calculating these weights - .saturating_mul(2), + 1u64 // Anemoy pool account balance read + .saturating_add( + pallet_investments::ActiveInvestOrders::::iter_keys().count() as u64, + ) + .saturating_add( + pallet_investments::ActiveInvestOrders::::iter_keys().count() as u64, + ) + .saturating_add( + pallet_investments::InvestOrders::::iter_keys().count() as u64, + ) + .saturating_add( + pallet_investments::RedeemOrders::::iter_keys().count() as u64, + ) + // 2x, first for the sanity checks and now for calculating these weights + .saturating_mul(2), ); (res, weight) From a9205d2251ba94dd1cc316fb87f53372fe2e4a5f Mon Sep 17 00:00:00 2001 From: nuno Date: Mon, 25 Sep 2023 10:00:59 +0200 Subject: [PATCH 06/10] fixup --- runtime/centrifuge/src/migrations.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 6f3bc285fd..1497b75fc8 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -16,7 +16,6 @@ pub type UpgradeCentrifuge1021 = anemoy_pool::Migration; /// Migrate the Anemoy Pool's currency from LpEthUSC to Circle's USDC, /// native on Polkadot's AssetHub. mod anemoy_pool { - use cfg_primitives::PoolId; use cfg_traits::PoolInspect; use cfg_types::tokens::CurrencyId; @@ -31,11 +30,9 @@ mod anemoy_pool { use sp_std::vec::Vec; use super::*; - #[cfg(feature = "try-runtime")] use crate::PoolSystem; const ANEMOY_POOL_ID: PoolId = 4_139_607_887; - #[cfg(feature = "try-runtime")] const LP_ETH_USDC: CurrencyId = CurrencyId::ForeignAsset(100_001); const DOT_NATIVE_USDC: CurrencyId = CurrencyId::ForeignAsset(6); @@ -107,10 +104,12 @@ mod anemoy_pool { fn verify_sanity_checks() -> (bool, Weight) { let res = - crate::Tokens::balance(LP_ETH_USDC, &PoolSystem::account_for(ANEMOY_POOL_ID)) == 0 - && pallet_investments::ActiveInvestOrders::::iter_keys() - .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) - .count() == 0 && pallet_investments::ActiveInvestOrders::::iter_keys() + crate::Tokens::balance( + LP_ETH_USDC, + &>::account_for(ANEMOY_POOL_ID), + ) == 0 && pallet_investments::ActiveInvestOrders::::iter_keys() + .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) + .count() == 0 && pallet_investments::ActiveInvestOrders::::iter_keys() .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) .count() == 0 && pallet_investments::InvestOrders::::iter_keys() .filter(|(_, investment)| investment.pool_id == ANEMOY_POOL_ID) From 71181bb66b51ece25ac86fc8a5e0cc2d051821f7 Mon Sep 17 00:00:00 2001 From: nuno Date: Mon, 25 Sep 2023 10:05:49 +0200 Subject: [PATCH 07/10] Simply weight calculation and dup ActiveInvestOrders read --- pallets/investments/src/lib.rs | 2 +- runtime/centrifuge/src/migrations.rs | 24 ++++++++---------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/pallets/investments/src/lib.rs b/pallets/investments/src/lib.rs index 3801bbc90d..c25a523152 100644 --- a/pallets/investments/src/lib.rs +++ b/pallets/investments/src/lib.rs @@ -250,7 +250,7 @@ pub mod pallet { #[pallet::storage] #[pallet::getter(fn acc_active_redeem_order)] - pub(crate) type ActiveRedeemOrders = + pub type ActiveRedeemOrders = StorageMap<_, Blake2_128Concat, T::InvestmentId, TotalOrder, ValueQuery>; #[pallet::storage] diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 1497b75fc8..1ec03168c5 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -109,7 +109,7 @@ mod anemoy_pool { &>::account_for(ANEMOY_POOL_ID), ) == 0 && pallet_investments::ActiveInvestOrders::::iter_keys() .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) - .count() == 0 && pallet_investments::ActiveInvestOrders::::iter_keys() + .count() == 0 && pallet_investments::ActiveRedeemOrders::::iter_keys() .filter(|investment| investment.pool_id == ANEMOY_POOL_ID) .count() == 0 && pallet_investments::InvestOrders::::iter_keys() .filter(|(_, investment)| investment.pool_id == ANEMOY_POOL_ID) @@ -118,21 +118,13 @@ mod anemoy_pool { .count() == 0; let weight = ::DbWeight::get().reads( - 1u64 // Anemoy pool account balance read - .saturating_add( - pallet_investments::ActiveInvestOrders::::iter_keys().count() as u64, - ) - .saturating_add( - pallet_investments::ActiveInvestOrders::::iter_keys().count() as u64, - ) - .saturating_add( - pallet_investments::InvestOrders::::iter_keys().count() as u64, - ) - .saturating_add( - pallet_investments::RedeemOrders::::iter_keys().count() as u64, - ) - // 2x, first for the sanity checks and now for calculating these weights - .saturating_mul(2), + vec![ + 1, // pool account balance read + pallet_investments::ActiveInvestOrders::::iter_keys().count(), + pallet_investments::ActiveRedeemOrders::::iter_keys().count(), + pallet_investments::InvestOrders::::iter_keys().count(), + pallet_investments::RedeemOrders::::iter_keys().count() + ].iter().sum() as u64 ); (res, weight) From 9b659d4dc1b98fd3f38cb505cd6ce9b561f6680b Mon Sep 17 00:00:00 2001 From: nuno Date: Mon, 25 Sep 2023 10:07:22 +0200 Subject: [PATCH 08/10] fmt --- runtime/centrifuge/src/migrations.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 1ec03168c5..91733bde54 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -123,8 +123,10 @@ mod anemoy_pool { pallet_investments::ActiveInvestOrders::::iter_keys().count(), pallet_investments::ActiveRedeemOrders::::iter_keys().count(), pallet_investments::InvestOrders::::iter_keys().count(), - pallet_investments::RedeemOrders::::iter_keys().count() - ].iter().sum() as u64 + pallet_investments::RedeemOrders::::iter_keys().count(), + ] + .iter() + .sum() as u64, ); (res, weight) From 125d133929ad7b0870d47ae87339a24089e4e814 Mon Sep 17 00:00:00 2001 From: nuno Date: Mon, 25 Sep 2023 10:13:26 +0200 Subject: [PATCH 09/10] fixup --- runtime/centrifuge/src/migrations.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 91733bde54..93063f9b2d 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -26,6 +26,7 @@ mod anemoy_pool { use frame_support::traits::{fungibles::Inspect, OnRuntimeUpgrade}; #[cfg(feature = "try-runtime")] use pallet_pool_system::PoolDetailsOf; + use sp_std::vec; #[cfg(feature = "try-runtime")] use sp_std::vec::Vec; @@ -126,7 +127,7 @@ mod anemoy_pool { pallet_investments::RedeemOrders::::iter_keys().count(), ] .iter() - .sum() as u64, + .fold(0u64, |acc, x| acc.saturating_add(*x as u64)), ); (res, weight) From c10e932e6ea543d93e6364842d8414236841c56c Mon Sep 17 00:00:00 2001 From: nuno Date: Mon, 25 Sep 2023 10:57:07 +0200 Subject: [PATCH 10/10] Fix weights --- runtime/centrifuge/src/migrations.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/runtime/centrifuge/src/migrations.rs b/runtime/centrifuge/src/migrations.rs index 93063f9b2d..0ed0b6d2d1 100644 --- a/runtime/centrifuge/src/migrations.rs +++ b/runtime/centrifuge/src/migrations.rs @@ -75,11 +75,7 @@ mod anemoy_pool { }); weight.saturating_add( - ::DbWeight::get().reads( - pallet_pool_system::Pool::::iter_keys() - .count() - .saturating_mul(2) as u64, - ), + ::DbWeight::get().reads_writes(1, 1), ) }