From b0f79c2039536d6bae87820aadffdd7fd560497f Mon Sep 17 00:00:00 2001 From: Gavin Wood Date: Wed, 11 Jan 2023 10:45:54 +0000 Subject: [PATCH] Improve inactive fund tracking (#6471) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Improve inactive fund tracking * Docs * Introduce the re-migration code * Bump * Update runtime/kusama/src/lib.rs Co-authored-by: Oliver Tale-Yazdi * Update runtime/common/src/crowdloan/migration.rs Co-authored-by: Oliver Tale-Yazdi * Update runtime/common/src/crowdloan/migration.rs Co-authored-by: Oliver Tale-Yazdi * Grumbles * Apply comments suggestions from code review Co-authored-by: Bastian Köcher Co-authored-by: Oliver Tale-Yazdi Co-authored-by: muharem Co-authored-by: Bastian Köcher Co-authored-by: parity-processbot <> --- .../runtime/common/src/crowdloan/migration.rs | 19 ++++++++++--------- polkadot/runtime/common/src/crowdloan/mod.rs | 2 -- polkadot/runtime/kusama/src/lib.rs | 4 +++- polkadot/runtime/polkadot/src/lib.rs | 4 +++- polkadot/runtime/rococo/src/lib.rs | 4 +++- polkadot/runtime/westend/src/lib.rs | 4 +++- 6 files changed, 22 insertions(+), 15 deletions(-) diff --git a/polkadot/runtime/common/src/crowdloan/migration.rs b/polkadot/runtime/common/src/crowdloan/migration.rs index 468c2a572b..a46e72cc1a 100644 --- a/polkadot/runtime/common/src/crowdloan/migration.rs +++ b/polkadot/runtime/common/src/crowdloan/migration.rs @@ -22,20 +22,21 @@ use frame_support::{ Twox64Concat, }; -pub struct MigrateToTrackInactive(sp_std::marker::PhantomData); -impl OnRuntimeUpgrade for MigrateToTrackInactive { +pub struct MigrateToTrackInactiveV2(sp_std::marker::PhantomData); +impl OnRuntimeUpgrade for MigrateToTrackInactiveV2 { fn on_runtime_upgrade() -> Weight { let onchain_version = Pallet::::on_chain_storage_version(); - if onchain_version == 0 { + if onchain_version == 1 { let mut translated = 0u64; - for index in Funds::::iter_keys() { - let b = CurrencyOf::::total_balance(&Pallet::::fund_account_id(index.into())); + for item in Funds::::iter_values() { + let b = + CurrencyOf::::total_balance(&Pallet::::fund_account_id(item.fund_index)); CurrencyOf::::deactivate(b); translated.saturating_inc(); } - StorageVersion::new(1).put::>(); + StorageVersion::new(2).put::>(); log::info!(target: "runtime::crowdloan", "Summed {} funds, storage to version 1", translated); T::DbWeight::get().reads_writes(translated * 2 + 1, translated * 2 + 1) } else { @@ -46,9 +47,9 @@ impl OnRuntimeUpgrade for MigrateToTrackInactive { #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result, &'static str> { - let total = Funds::::iter_keys() - .map(|index| { - CurrencyOf::::total_balance(&Pallet::::fund_account_id(index.into())) + let total = Funds::::iter_values() + .map(|item| { + CurrencyOf::::total_balance(&Pallet::::fund_account_id(item.fund_index)) }) .fold(BalanceOf::::zero(), |a, i| a.saturating_add(i)); Ok((total, CurrencyOf::::active_issuance()).encode()) diff --git a/polkadot/runtime/common/src/crowdloan/mod.rs b/polkadot/runtime/common/src/crowdloan/mod.rs index 711bf0b7b3..57b8f27f50 100644 --- a/polkadot/runtime/common/src/crowdloan/mod.rs +++ b/polkadot/runtime/common/src/crowdloan/mod.rs @@ -51,8 +51,6 @@ pub mod migration; -// TODO: Expose the total amount held. - use crate::{ slot_range::SlotRange, traits::{Auctioneer, Registrar}, diff --git a/polkadot/runtime/kusama/src/lib.rs b/polkadot/runtime/kusama/src/lib.rs index 8cf3e95f79..ba6bc86bf9 100644 --- a/polkadot/runtime/kusama/src/lib.rs +++ b/polkadot/runtime/kusama/src/lib.rs @@ -1484,8 +1484,10 @@ impl Get<&'static str> for StakingMigrationV11OldPallet { /// /// Should be cleared after every release. pub type Migrations = ( + pallet_balances::migration::ResetInactive, + // We need to apply this migration again, because `ResetInactive` resets the state again. pallet_balances::migration::MigrateToTrackInactive, - crowdloan::migration::MigrateToTrackInactive, + crowdloan::migration::MigrateToTrackInactiveV2, pallet_referenda::migration::v1::MigrateV0ToV1, pallet_referenda::migration::v1::MigrateV0ToV1< Runtime, diff --git a/polkadot/runtime/polkadot/src/lib.rs b/polkadot/runtime/polkadot/src/lib.rs index dc32a91c7a..81a7a35159 100644 --- a/polkadot/runtime/polkadot/src/lib.rs +++ b/polkadot/runtime/polkadot/src/lib.rs @@ -1601,8 +1601,10 @@ impl Get<&'static str> for StakingMigrationV11OldPallet { /// /// Should be cleared after every release. pub type Migrations = ( + pallet_balances::migration::ResetInactive, + // We need to apply this migration again, because `ResetInactive` resets the state again. pallet_balances::migration::MigrateToTrackInactive, - crowdloan::migration::MigrateToTrackInactive, + crowdloan::migration::MigrateToTrackInactiveV2, pallet_scheduler::migration::v4::CleanupAgendas, pallet_staking::migrations::v13::MigrateToV13, parachains_disputes::migration::v1::MigrateToV1, diff --git a/polkadot/runtime/rococo/src/lib.rs b/polkadot/runtime/rococo/src/lib.rs index 38d5ce28d1..4424b47305 100644 --- a/polkadot/runtime/rococo/src/lib.rs +++ b/polkadot/runtime/rococo/src/lib.rs @@ -1481,8 +1481,10 @@ pub type UncheckedExtrinsic = /// /// Should be cleared after every release. pub type Migrations = ( + pallet_balances::migration::ResetInactive, + // We need to apply this migration again, because `ResetInactive` resets the state again. pallet_balances::migration::MigrateToTrackInactive, - crowdloan::migration::MigrateToTrackInactive, + crowdloan::migration::MigrateToTrackInactiveV2, pallet_scheduler::migration::v4::CleanupAgendas, parachains_disputes::migration::v1::MigrateToV1, parachains_configuration::migration::v4::MigrateToV4, diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index b10f76a270..c71b5238c5 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1215,8 +1215,10 @@ impl Get<&'static str> for StakingMigrationV11OldPallet { /// /// Should be cleared after every release. pub type Migrations = ( + pallet_balances::migration::ResetInactive, + // We need to apply this migration again, because `ResetInactive` resets the state again. pallet_balances::migration::MigrateToTrackInactive, - crowdloan::migration::MigrateToTrackInactive, + crowdloan::migration::MigrateToTrackInactiveV2, pallet_scheduler::migration::v4::CleanupAgendas, pallet_staking::migrations::v13::MigrateToV13, parachains_disputes::migration::v1::MigrateToV1,