From 710fe676d128008d48c08b464fe24a2712fa6a08 Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Fri, 22 Apr 2022 11:40:12 +0200 Subject: [PATCH 01/12] Remove SlotsCrowdloanIndexMigration --- runtime/kusama/src/lib.rs | 18 ------------------ runtime/polkadot/src/lib.rs | 19 ------------------- runtime/westend/src/lib.rs | 19 ------------------- 3 files changed, 56 deletions(-) diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index aff164b4579c..3cb63eee7099 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -1532,30 +1532,12 @@ pub type Executive = frame_executive::Executive< Runtime, AllPalletsWithSystem, ( - SlotsCrowdloanIndexMigration, pallet_staking::migrations::v9::InjectValidatorsIntoVoterList, ), >; /// The payload being signed in the transactions. pub type SignedPayload = generic::SignedPayload; -pub struct SlotsCrowdloanIndexMigration; -impl OnRuntimeUpgrade for SlotsCrowdloanIndexMigration { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - slots::migration::slots_crowdloan_index_migration::migrate::() - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result<(), &'static str> { - slots::migration::slots_crowdloan_index_migration::pre_migrate::() - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade() -> Result<(), &'static str> { - slots::migration::slots_crowdloan_index_migration::post_migrate::() - } -} - #[cfg(feature = "runtime-benchmarks")] #[macro_use] extern crate frame_benchmarking; diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index d7ec12aa51e9..ea8f1a0fd2a8 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -1485,31 +1485,12 @@ pub type Executive = frame_executive::Executive< Runtime, AllPalletsWithSystem, ( - SlotsCrowdloanIndexMigration, pallet_staking::migrations::v9::InjectValidatorsIntoVoterList, ), >; /// The payload being signed in transactions. pub type SignedPayload = generic::SignedPayload; -// Migration for crowdloan pallet to use fund index for account generation. -pub struct SlotsCrowdloanIndexMigration; -impl OnRuntimeUpgrade for SlotsCrowdloanIndexMigration { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - slots::migration::slots_crowdloan_index_migration::migrate::() - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result<(), &'static str> { - slots::migration::slots_crowdloan_index_migration::pre_migrate::() - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade() -> Result<(), &'static str> { - slots::migration::slots_crowdloan_index_migration::post_migrate::() - } -} - #[cfg(feature = "runtime-benchmarks")] #[macro_use] extern crate frame_benchmarking; diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 10b8cde94ca2..10c21d3c4470 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1094,31 +1094,12 @@ pub type Executive = frame_executive::Executive< Runtime, AllPalletsWithSystem, ( - SlotsCrowdloanIndexMigration, pallet_staking::migrations::v9::InjectValidatorsIntoVoterList, ), >; /// The payload being signed in transactions. pub type SignedPayload = generic::SignedPayload; -// Migration for crowdloan pallet to use fund index for account generation. -pub struct SlotsCrowdloanIndexMigration; -impl OnRuntimeUpgrade for SlotsCrowdloanIndexMigration { - fn on_runtime_upgrade() -> frame_support::weights::Weight { - slots::migration::slots_crowdloan_index_migration::migrate::() - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result<(), &'static str> { - slots::migration::slots_crowdloan_index_migration::pre_migrate::() - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade() -> Result<(), &'static str> { - slots::migration::slots_crowdloan_index_migration::post_migrate::() - } -} - #[cfg(feature = "runtime-benchmarks")] #[macro_use] extern crate frame_benchmarking; From 62c5a28e21d2949189bdb52835cc3a44f6e9c561 Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Fri, 22 Apr 2022 11:41:37 +0200 Subject: [PATCH 02/12] Remove slots index migration code --- runtime/common/src/slots/migration.rs | 96 --------------------------- 1 file changed, 96 deletions(-) delete mode 100644 runtime/common/src/slots/migration.rs diff --git a/runtime/common/src/slots/migration.rs b/runtime/common/src/slots/migration.rs deleted file mode 100644 index b59ab35d4c63..000000000000 --- a/runtime/common/src/slots/migration.rs +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright 2017-2020 Parity Technologies (UK) Ltd. -// This file is part of Polkadot. - -// Polkadot 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. - -// Polkadot 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. - -// You should have received a copy of the GNU General Public License -// along with Polkadot. If not, see . - -use super::*; -use crate::crowdloan; -use sp_runtime::traits::AccountIdConversion; - -/// Migrations for using fund index to create fund accounts instead of para ID. -pub mod slots_crowdloan_index_migration { - use super::*; - - // The old way we generated fund accounts. - fn old_fund_account_id(index: ParaId) -> T::AccountId { - ::PalletId::get().into_sub_account(index) - } - - pub fn pre_migrate() -> Result<(), &'static str> { - for (para_id, leases) in Leases::::iter() { - let old_fund_account = old_fund_account_id::(para_id); - - for maybe_deposit in leases.iter() { - if let Some((who, _amount)) = maybe_deposit { - if *who == old_fund_account { - let crowdloan = - crowdloan::Funds::::get(para_id).ok_or("no crowdloan found")?; - log::info!( - target: "runtime", - "para_id={:?}, old_fund_account={:?}, fund_id={:?}, leases={:?}", - para_id, old_fund_account, crowdloan.fund_index, leases, - ); - break - } - } - } - } - - Ok(()) - } - - pub fn migrate() -> frame_support::weights::Weight { - let mut weight = 0; - - for (para_id, mut leases) in Leases::::iter() { - weight = weight.saturating_add(T::DbWeight::get().reads(2)); - // the para id must have a crowdloan - if let Some(fund) = crowdloan::Funds::::get(para_id) { - let old_fund_account = old_fund_account_id::(para_id); - let new_fund_account = crowdloan::Pallet::::fund_account_id(fund.fund_index); - - // look for places the old account is used, and replace with the new account. - for maybe_deposit in leases.iter_mut() { - if let Some((who, _amount)) = maybe_deposit { - if *who == old_fund_account { - *who = new_fund_account.clone(); - } - } - } - - // insert the changes. - weight = weight.saturating_add(T::DbWeight::get().writes(1)); - Leases::::insert(para_id, leases); - } - } - - weight - } - - pub fn post_migrate() -> Result<(), &'static str> { - for (para_id, leases) in Leases::::iter() { - let old_fund_account = old_fund_account_id::(para_id); - log::info!(target: "runtime", "checking para_id: {:?}", para_id); - // check the old fund account doesn't exist anywhere. - for maybe_deposit in leases.iter() { - if let Some((who, _amount)) = maybe_deposit { - if *who == old_fund_account { - panic!("old fund account found after migration!"); - } - } - } - } - Ok(()) - } -} From fe84b59e3133e950740d911fb019e6002f60d5a3 Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Fri, 22 Apr 2022 11:41:59 +0200 Subject: [PATCH 03/12] Remove crowdloan index migration code --- runtime/common/src/crowdloan/migration.rs | 190 ---------------------- 1 file changed, 190 deletions(-) delete mode 100644 runtime/common/src/crowdloan/migration.rs diff --git a/runtime/common/src/crowdloan/migration.rs b/runtime/common/src/crowdloan/migration.rs deleted file mode 100644 index 59511259815c..000000000000 --- a/runtime/common/src/crowdloan/migration.rs +++ /dev/null @@ -1,190 +0,0 @@ -// Copyright 2017-2020 Parity Technologies (UK) Ltd. -// This file is part of Polkadot. - -// Polkadot 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. - -// Polkadot 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. - -// You should have received a copy of the GNU General Public License -// along with Polkadot. If not, see . - -use super::*; -use frame_support::{generate_storage_alias, Twox64Concat}; - -/// Migrations for using fund index to create fund accounts instead of para ID. -pub mod crowdloan_index_migration { - use super::*; - - // The old way we generated fund accounts. - fn old_fund_account_id(index: ParaId) -> T::AccountId { - T::PalletId::get().into_sub_account(index) - } - - pub fn pre_migrate() -> Result<(), &'static str> { - // `NextTrieIndex` should have a value. - generate_storage_alias!(Crowdloan, NextTrieIndex => Value); - - generate_storage_alias!( - Slots, - Leases => Map<(Twox64Concat, ParaId), Vec)>>> - ); - let next_index = NextTrieIndex::get().unwrap_or_default(); - ensure!(next_index > 0, "Next index is zero, which implies no migration is needed."); - - log::info!( - target: "runtime", - "next trie index: {:?}", - next_index, - ); - - for (para_id, fund) in Funds::::iter() { - let old_fund_account = old_fund_account_id::(para_id); - let total_balance = CurrencyOf::::total_balance(&old_fund_account); - - log::info!( - target: "runtime", - "para_id={:?}, old_fund_account={:?}, total_balance={:?}, fund.raised={:?}", - para_id, old_fund_account, total_balance, fund.raised - ); - - // Each fund should have some non-zero balance. - ensure!( - total_balance >= fund.raised, - "Total balance is not equal to the funds raised." - ); - - let leases = Leases::::get(para_id).unwrap_or_default(); - let mut found_lease_deposit = false; - for maybe_deposit in leases.iter() { - if let Some((who, _amount)) = maybe_deposit { - if *who == old_fund_account { - found_lease_deposit = true; - break - } - } - } - if found_lease_deposit { - log::info!( - target: "runtime", - "para_id={:?}, old_fund_account={:?}, leases={:?}", - para_id, old_fund_account, leases, - ); - } - } - - Ok(()) - } - - /// This migration converts crowdloans to use a crowdloan index rather than the parachain id as a - /// unique identifier. This makes it easier to swap two crowdloans between parachains. - pub fn migrate() -> frame_support::weights::Weight { - let mut weight = 0; - - // First migrate `NextTrieIndex` counter to `NextFundIndex`. - generate_storage_alias!(Crowdloan, NextTrieIndex => Value); - - generate_storage_alias!( - Slots, - Leases => Map<(Twox64Concat, ParaId), Vec)>>> - ); - let next_index = NextTrieIndex::take().unwrap_or_default(); - NextFundIndex::::set(next_index); - - weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2)); - - // Migrate all accounts from `old_fund_account` to `fund_account` using `fund_index`. - for (para_id, fund) in Funds::::iter() { - let old_fund_account = old_fund_account_id::(para_id); - let new_fund_account = Pallet::::fund_account_id(fund.fund_index); - - // Funds should only have a free balance and a reserve balance. Both of these are in the - // `Account` storage item, so we just swap them. - let account_info = frame_system::Account::::take(&old_fund_account); - frame_system::Account::::insert(&new_fund_account, account_info); - - weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2)); - - let mut leases = Leases::::get(para_id).unwrap_or_default(); - for maybe_deposit in leases.iter_mut() { - if let Some((who, _amount)) = maybe_deposit { - if *who == old_fund_account { - *who = new_fund_account.clone(); - } - } - } - - Leases::::insert(para_id, leases); - } - - weight - } - - pub fn post_migrate() -> Result<(), &'static str> { - // `NextTrieIndex` should not have a value, and `NextFundIndex` should. - generate_storage_alias!(Crowdloan, NextTrieIndex => Value); - ensure!(NextTrieIndex::get().is_none(), "NextTrieIndex still has a value."); - - generate_storage_alias!( - Slots, - Leases => Map<(Twox64Concat, ParaId), Vec)>>> - ); - - let next_index = NextFundIndex::::get(); - log::info!( - target: "runtime", - "next fund index: {:?}", - next_index, - ); - - ensure!( - next_index > 0, - "NextFundIndex was not migrated or is zero. We assume it cannot be zero else no migration is needed." - ); - - // Each fund should have balance migrated correctly. - for (para_id, fund) in Funds::::iter() { - // Old fund account is deleted. - let old_fund_account = old_fund_account_id::(para_id); - ensure!( - frame_system::Account::::get(&old_fund_account) == Default::default(), - "Old account wasn't reset to default value." - ); - - // New fund account has the correct balance. - let new_fund_account = Pallet::::fund_account_id(fund.fund_index); - let total_balance = CurrencyOf::::total_balance(&new_fund_account); - - ensure!( - total_balance >= fund.raised, - "Total balance in new account is different than the funds raised." - ); - - let leases = Leases::::get(para_id).unwrap_or_default(); - let mut new_account_found = false; - for maybe_deposit in leases.iter() { - if let Some((who, _amount)) = maybe_deposit { - if *who == old_fund_account { - panic!("Old fund account found after migration!"); - } else if *who == new_fund_account { - new_account_found = true; - } - } - } - if new_account_found { - log::info!( - target: "runtime::crowdloan", - "para_id={:?}, new_fund_account={:?}, leases={:?}", - para_id, new_fund_account, leases, - ); - } - } - - Ok(()) - } -} From 1f8806c010210b638cc59979ab873619910596c0 Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Fri, 22 Apr 2022 11:55:41 +0200 Subject: [PATCH 04/12] remove reference to migration --- runtime/common/src/crowdloan/mod.rs | 2 -- runtime/common/src/slots/mod.rs | 2 -- 2 files changed, 4 deletions(-) diff --git a/runtime/common/src/crowdloan/mod.rs b/runtime/common/src/crowdloan/mod.rs index ea4d3026bad3..3e166d8dfa26 100644 --- a/runtime/common/src/crowdloan/mod.rs +++ b/runtime/common/src/crowdloan/mod.rs @@ -49,8 +49,6 @@ //! the parachain remains active. Users can withdraw their funds once the slot is completed and funds are //! returned to the crowdloan account. -pub mod migration; - use crate::{ slot_range::SlotRange, traits::{Auctioneer, Registrar}, diff --git a/runtime/common/src/slots/mod.rs b/runtime/common/src/slots/mod.rs index c479df5cba01..0b7c71c87fed 100644 --- a/runtime/common/src/slots/mod.rs +++ b/runtime/common/src/slots/mod.rs @@ -21,8 +21,6 @@ //! This doesn't handle the mechanics of determining which para ID actually ends up with a parachain lease. This //! must handled by a separately, through the trait interface that this pallet provides or the root dispatchables. -pub mod migration; - use crate::traits::{LeaseError, Leaser, Registrar}; use frame_support::{ pallet_prelude::*, From c8139dd8314c0ca866be5bcdffad1e3c778f82cc Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Fri, 22 Apr 2022 11:55:52 +0200 Subject: [PATCH 05/12] rustfmt --- runtime/kusama/src/lib.rs | 4 +--- runtime/polkadot/src/lib.rs | 4 +--- runtime/westend/src/lib.rs | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index 3cb63eee7099..601fdda4ff1e 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -1531,9 +1531,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - ( - pallet_staking::migrations::v9::InjectValidatorsIntoVoterList, - ), + (pallet_staking::migrations::v9::InjectValidatorsIntoVoterList,), >; /// The payload being signed in the transactions. pub type SignedPayload = generic::SignedPayload; diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index ea8f1a0fd2a8..06997215c0e5 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -1484,9 +1484,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - ( - pallet_staking::migrations::v9::InjectValidatorsIntoVoterList, - ), + (pallet_staking::migrations::v9::InjectValidatorsIntoVoterList,), >; /// The payload being signed in transactions. pub type SignedPayload = generic::SignedPayload; diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 10c21d3c4470..755455d2c1c1 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1093,9 +1093,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - ( - pallet_staking::migrations::v9::InjectValidatorsIntoVoterList, - ), + (pallet_staking::migrations::v9::InjectValidatorsIntoVoterList,), >; /// The payload being signed in transactions. pub type SignedPayload = generic::SignedPayload; From 4f62b19834d0596b2bb9c8cd37e15f284b2e2600 Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Fri, 22 Apr 2022 12:15:51 +0200 Subject: [PATCH 06/12] westend: remove unused import OnRuntimeUpgrade --- runtime/westend/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 755455d2c1c1..9fbc150c3f03 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -25,7 +25,7 @@ use beefy_primitives::crypto::AuthorityId as BeefyId; use frame_election_provider_support::{onchain, SequentialPhragmen}; use frame_support::{ construct_runtime, parameter_types, - traits::{Contains, InstanceFilter, KeyOwnerProofSystem, OnRuntimeUpgrade}, + traits::{Contains, InstanceFilter, KeyOwnerProofSystem}, weights::ConstantMultiplier, PalletId, }; From e6da335b871219cf1fc6b8e2de5956d5b318859f Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Fri, 22 Apr 2022 12:33:58 +0200 Subject: [PATCH 07/12] kusama: remove unused import OnRuntimeUpgrade --- runtime/kusama/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index 601fdda4ff1e..cd6c916c0bae 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -52,8 +52,7 @@ use frame_election_provider_support::{ use frame_support::{ construct_runtime, parameter_types, traits::{ - Contains, EnsureOneOf, InstanceFilter, KeyOwnerProofSystem, LockIdentifier, - OnRuntimeUpgrade, PrivilegeCmp, + Contains, EnsureOneOf, InstanceFilter, KeyOwnerProofSystem, LockIdentifier, PrivilegeCmp, }, weights::ConstantMultiplier, PalletId, RuntimeDebug, From c42c07f58db59237016b149d55f4a40f25a2b49d Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Fri, 22 Apr 2022 12:34:10 +0200 Subject: [PATCH 08/12] polkadot: remove unused import OnRuntimeUpgrade --- runtime/polkadot/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 06997215c0e5..0000d07a6f67 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -41,8 +41,7 @@ use frame_election_provider_support::{generate_solution_type, onchain, Sequentia use frame_support::{ construct_runtime, parameter_types, traits::{ - Contains, EnsureOneOf, InstanceFilter, KeyOwnerProofSystem, LockIdentifier, - OnRuntimeUpgrade, PrivilegeCmp, + Contains, EnsureOneOf, InstanceFilter, KeyOwnerProofSystem, LockIdentifier, PrivilegeCmp, }, weights::ConstantMultiplier, PalletId, RuntimeDebug, From ff2a1644826b7359c74638beb4ce128911d3c4b7 Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Mon, 25 Apr 2022 09:40:51 +0200 Subject: [PATCH 09/12] Revert "Remove crowdloan index migration code" This reverts commit fe84b59e3133e950740d911fb019e6002f60d5a3. --- runtime/common/src/crowdloan/migration.rs | 190 ++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 runtime/common/src/crowdloan/migration.rs diff --git a/runtime/common/src/crowdloan/migration.rs b/runtime/common/src/crowdloan/migration.rs new file mode 100644 index 000000000000..59511259815c --- /dev/null +++ b/runtime/common/src/crowdloan/migration.rs @@ -0,0 +1,190 @@ +// Copyright 2017-2020 Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot 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. + +// Polkadot 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. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +use super::*; +use frame_support::{generate_storage_alias, Twox64Concat}; + +/// Migrations for using fund index to create fund accounts instead of para ID. +pub mod crowdloan_index_migration { + use super::*; + + // The old way we generated fund accounts. + fn old_fund_account_id(index: ParaId) -> T::AccountId { + T::PalletId::get().into_sub_account(index) + } + + pub fn pre_migrate() -> Result<(), &'static str> { + // `NextTrieIndex` should have a value. + generate_storage_alias!(Crowdloan, NextTrieIndex => Value); + + generate_storage_alias!( + Slots, + Leases => Map<(Twox64Concat, ParaId), Vec)>>> + ); + let next_index = NextTrieIndex::get().unwrap_or_default(); + ensure!(next_index > 0, "Next index is zero, which implies no migration is needed."); + + log::info!( + target: "runtime", + "next trie index: {:?}", + next_index, + ); + + for (para_id, fund) in Funds::::iter() { + let old_fund_account = old_fund_account_id::(para_id); + let total_balance = CurrencyOf::::total_balance(&old_fund_account); + + log::info!( + target: "runtime", + "para_id={:?}, old_fund_account={:?}, total_balance={:?}, fund.raised={:?}", + para_id, old_fund_account, total_balance, fund.raised + ); + + // Each fund should have some non-zero balance. + ensure!( + total_balance >= fund.raised, + "Total balance is not equal to the funds raised." + ); + + let leases = Leases::::get(para_id).unwrap_or_default(); + let mut found_lease_deposit = false; + for maybe_deposit in leases.iter() { + if let Some((who, _amount)) = maybe_deposit { + if *who == old_fund_account { + found_lease_deposit = true; + break + } + } + } + if found_lease_deposit { + log::info!( + target: "runtime", + "para_id={:?}, old_fund_account={:?}, leases={:?}", + para_id, old_fund_account, leases, + ); + } + } + + Ok(()) + } + + /// This migration converts crowdloans to use a crowdloan index rather than the parachain id as a + /// unique identifier. This makes it easier to swap two crowdloans between parachains. + pub fn migrate() -> frame_support::weights::Weight { + let mut weight = 0; + + // First migrate `NextTrieIndex` counter to `NextFundIndex`. + generate_storage_alias!(Crowdloan, NextTrieIndex => Value); + + generate_storage_alias!( + Slots, + Leases => Map<(Twox64Concat, ParaId), Vec)>>> + ); + let next_index = NextTrieIndex::take().unwrap_or_default(); + NextFundIndex::::set(next_index); + + weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2)); + + // Migrate all accounts from `old_fund_account` to `fund_account` using `fund_index`. + for (para_id, fund) in Funds::::iter() { + let old_fund_account = old_fund_account_id::(para_id); + let new_fund_account = Pallet::::fund_account_id(fund.fund_index); + + // Funds should only have a free balance and a reserve balance. Both of these are in the + // `Account` storage item, so we just swap them. + let account_info = frame_system::Account::::take(&old_fund_account); + frame_system::Account::::insert(&new_fund_account, account_info); + + weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2)); + + let mut leases = Leases::::get(para_id).unwrap_or_default(); + for maybe_deposit in leases.iter_mut() { + if let Some((who, _amount)) = maybe_deposit { + if *who == old_fund_account { + *who = new_fund_account.clone(); + } + } + } + + Leases::::insert(para_id, leases); + } + + weight + } + + pub fn post_migrate() -> Result<(), &'static str> { + // `NextTrieIndex` should not have a value, and `NextFundIndex` should. + generate_storage_alias!(Crowdloan, NextTrieIndex => Value); + ensure!(NextTrieIndex::get().is_none(), "NextTrieIndex still has a value."); + + generate_storage_alias!( + Slots, + Leases => Map<(Twox64Concat, ParaId), Vec)>>> + ); + + let next_index = NextFundIndex::::get(); + log::info!( + target: "runtime", + "next fund index: {:?}", + next_index, + ); + + ensure!( + next_index > 0, + "NextFundIndex was not migrated or is zero. We assume it cannot be zero else no migration is needed." + ); + + // Each fund should have balance migrated correctly. + for (para_id, fund) in Funds::::iter() { + // Old fund account is deleted. + let old_fund_account = old_fund_account_id::(para_id); + ensure!( + frame_system::Account::::get(&old_fund_account) == Default::default(), + "Old account wasn't reset to default value." + ); + + // New fund account has the correct balance. + let new_fund_account = Pallet::::fund_account_id(fund.fund_index); + let total_balance = CurrencyOf::::total_balance(&new_fund_account); + + ensure!( + total_balance >= fund.raised, + "Total balance in new account is different than the funds raised." + ); + + let leases = Leases::::get(para_id).unwrap_or_default(); + let mut new_account_found = false; + for maybe_deposit in leases.iter() { + if let Some((who, _amount)) = maybe_deposit { + if *who == old_fund_account { + panic!("Old fund account found after migration!"); + } else if *who == new_fund_account { + new_account_found = true; + } + } + } + if new_account_found { + log::info!( + target: "runtime::crowdloan", + "para_id={:?}, new_fund_account={:?}, leases={:?}", + para_id, new_fund_account, leases, + ); + } + } + + Ok(()) + } +} From c6b8223975c7e87449a517b159f952af4cd33ab7 Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Mon, 25 Apr 2022 09:40:59 +0200 Subject: [PATCH 10/12] Revert "Remove slots index migration code" This reverts commit 62c5a28e21d2949189bdb52835cc3a44f6e9c561. --- runtime/common/src/slots/migration.rs | 96 +++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 runtime/common/src/slots/migration.rs diff --git a/runtime/common/src/slots/migration.rs b/runtime/common/src/slots/migration.rs new file mode 100644 index 000000000000..b59ab35d4c63 --- /dev/null +++ b/runtime/common/src/slots/migration.rs @@ -0,0 +1,96 @@ +// Copyright 2017-2020 Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot 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. + +// Polkadot 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. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +use super::*; +use crate::crowdloan; +use sp_runtime::traits::AccountIdConversion; + +/// Migrations for using fund index to create fund accounts instead of para ID. +pub mod slots_crowdloan_index_migration { + use super::*; + + // The old way we generated fund accounts. + fn old_fund_account_id(index: ParaId) -> T::AccountId { + ::PalletId::get().into_sub_account(index) + } + + pub fn pre_migrate() -> Result<(), &'static str> { + for (para_id, leases) in Leases::::iter() { + let old_fund_account = old_fund_account_id::(para_id); + + for maybe_deposit in leases.iter() { + if let Some((who, _amount)) = maybe_deposit { + if *who == old_fund_account { + let crowdloan = + crowdloan::Funds::::get(para_id).ok_or("no crowdloan found")?; + log::info!( + target: "runtime", + "para_id={:?}, old_fund_account={:?}, fund_id={:?}, leases={:?}", + para_id, old_fund_account, crowdloan.fund_index, leases, + ); + break + } + } + } + } + + Ok(()) + } + + pub fn migrate() -> frame_support::weights::Weight { + let mut weight = 0; + + for (para_id, mut leases) in Leases::::iter() { + weight = weight.saturating_add(T::DbWeight::get().reads(2)); + // the para id must have a crowdloan + if let Some(fund) = crowdloan::Funds::::get(para_id) { + let old_fund_account = old_fund_account_id::(para_id); + let new_fund_account = crowdloan::Pallet::::fund_account_id(fund.fund_index); + + // look for places the old account is used, and replace with the new account. + for maybe_deposit in leases.iter_mut() { + if let Some((who, _amount)) = maybe_deposit { + if *who == old_fund_account { + *who = new_fund_account.clone(); + } + } + } + + // insert the changes. + weight = weight.saturating_add(T::DbWeight::get().writes(1)); + Leases::::insert(para_id, leases); + } + } + + weight + } + + pub fn post_migrate() -> Result<(), &'static str> { + for (para_id, leases) in Leases::::iter() { + let old_fund_account = old_fund_account_id::(para_id); + log::info!(target: "runtime", "checking para_id: {:?}", para_id); + // check the old fund account doesn't exist anywhere. + for maybe_deposit in leases.iter() { + if let Some((who, _amount)) = maybe_deposit { + if *who == old_fund_account { + panic!("old fund account found after migration!"); + } + } + } + } + Ok(()) + } +} From 7e17a5ac14b65e4d7dfdf5e201b98f3ae7510e36 Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Mon, 25 Apr 2022 09:41:35 +0200 Subject: [PATCH 11/12] Revert "remove reference to migration" This reverts commit 1f8806c010210b638cc59979ab873619910596c0. --- runtime/common/src/crowdloan/mod.rs | 2 ++ runtime/common/src/slots/mod.rs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/runtime/common/src/crowdloan/mod.rs b/runtime/common/src/crowdloan/mod.rs index 3e166d8dfa26..ea4d3026bad3 100644 --- a/runtime/common/src/crowdloan/mod.rs +++ b/runtime/common/src/crowdloan/mod.rs @@ -49,6 +49,8 @@ //! the parachain remains active. Users can withdraw their funds once the slot is completed and funds are //! returned to the crowdloan account. +pub mod migration; + use crate::{ slot_range::SlotRange, traits::{Auctioneer, Registrar}, diff --git a/runtime/common/src/slots/mod.rs b/runtime/common/src/slots/mod.rs index 0b7c71c87fed..c479df5cba01 100644 --- a/runtime/common/src/slots/mod.rs +++ b/runtime/common/src/slots/mod.rs @@ -21,6 +21,8 @@ //! This doesn't handle the mechanics of determining which para ID actually ends up with a parachain lease. This //! must handled by a separately, through the trait interface that this pallet provides or the root dispatchables. +pub mod migration; + use crate::traits::{LeaseError, Leaser, Registrar}; use frame_support::{ pallet_prelude::*, From 620f87273533eb47957a705e6e5f6214c8470770 Mon Sep 17 00:00:00 2001 From: Mara Broda Date: Fri, 22 Apr 2022 14:10:40 +0200 Subject: [PATCH 12/12] Remove InjectValidatorsIntoVoterList migration --- runtime/kusama/src/lib.rs | 2 +- runtime/polkadot/src/lib.rs | 2 +- runtime/westend/src/lib.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index cd6c916c0bae..709d260f4f62 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -1530,7 +1530,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - (pallet_staking::migrations::v9::InjectValidatorsIntoVoterList,), + (), >; /// The payload being signed in the transactions. pub type SignedPayload = generic::SignedPayload; diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 0000d07a6f67..6ef905144457 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -1483,7 +1483,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - (pallet_staking::migrations::v9::InjectValidatorsIntoVoterList,), + (), >; /// The payload being signed in transactions. pub type SignedPayload = generic::SignedPayload; diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 9fbc150c3f03..ab07d0193682 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -1093,7 +1093,7 @@ pub type Executive = frame_executive::Executive< frame_system::ChainContext, Runtime, AllPalletsWithSystem, - (pallet_staking::migrations::v9::InjectValidatorsIntoVoterList,), + (), >; /// The payload being signed in transactions. pub type SignedPayload = generic::SignedPayload;