From 4d837bac88fa5b8ae5c766ede626ef590a85940c Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Thu, 7 Apr 2022 10:51:37 -0400 Subject: [PATCH] Custom Slots Migration for Fund Index Change (#5227) * new migration * use new migration in runtime --- polkadot/runtime/common/src/crowdloan/mod.rs | 2 +- .../runtime/common/src/slots/migration.rs | 96 +++++++++++++++++++ .../common/src/{slots.rs => slots/mod.rs} | 2 + polkadot/runtime/kusama/src/lib.rs | 12 +-- polkadot/runtime/polkadot/src/lib.rs | 12 +-- polkadot/runtime/westend/src/lib.rs | 12 +-- 6 files changed, 117 insertions(+), 19 deletions(-) create mode 100644 polkadot/runtime/common/src/slots/migration.rs rename polkadot/runtime/common/src/{slots.rs => slots/mod.rs} (99%) diff --git a/polkadot/runtime/common/src/crowdloan/mod.rs b/polkadot/runtime/common/src/crowdloan/mod.rs index e41ceb15dc..ea4d3026ba 100644 --- a/polkadot/runtime/common/src/crowdloan/mod.rs +++ b/polkadot/runtime/common/src/crowdloan/mod.rs @@ -228,7 +228,7 @@ pub mod pallet { /// Info on all of the funds. #[pallet::storage] #[pallet::getter(fn funds)] - pub(super) type Funds = StorageMap< + pub(crate) type Funds = StorageMap< _, Twox64Concat, ParaId, diff --git a/polkadot/runtime/common/src/slots/migration.rs b/polkadot/runtime/common/src/slots/migration.rs new file mode 100644 index 0000000000..b59ab35d4c --- /dev/null +++ b/polkadot/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(()) + } +} diff --git a/polkadot/runtime/common/src/slots.rs b/polkadot/runtime/common/src/slots/mod.rs similarity index 99% rename from polkadot/runtime/common/src/slots.rs rename to polkadot/runtime/common/src/slots/mod.rs index 0b7c71c87f..c479df5cba 100644 --- a/polkadot/runtime/common/src/slots.rs +++ b/polkadot/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::*, diff --git a/polkadot/runtime/kusama/src/lib.rs b/polkadot/runtime/kusama/src/lib.rs index cab8385ddf..3b242c057a 100644 --- a/polkadot/runtime/kusama/src/lib.rs +++ b/polkadot/runtime/kusama/src/lib.rs @@ -1524,27 +1524,27 @@ pub type Executive = frame_executive::Executive< Runtime, AllPalletsWithSystem, ( - CrowdloanIndexMigration, + SlotsCrowdloanIndexMigration, pallet_staking::migrations::v9::InjectValidatorsIntoVoterList, ), >; /// The payload being signed in the transactions. pub type SignedPayload = generic::SignedPayload; -pub struct CrowdloanIndexMigration; -impl OnRuntimeUpgrade for CrowdloanIndexMigration { +pub struct SlotsCrowdloanIndexMigration; +impl OnRuntimeUpgrade for SlotsCrowdloanIndexMigration { fn on_runtime_upgrade() -> frame_support::weights::Weight { - crowdloan::migration::crowdloan_index_migration::migrate::() + slots::migration::slots_crowdloan_index_migration::migrate::() } #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result<(), &'static str> { - crowdloan::migration::crowdloan_index_migration::pre_migrate::() + slots::migration::slots_crowdloan_index_migration::pre_migrate::() } #[cfg(feature = "try-runtime")] fn post_upgrade() -> Result<(), &'static str> { - crowdloan::migration::crowdloan_index_migration::post_migrate::() + slots::migration::slots_crowdloan_index_migration::post_migrate::() } } diff --git a/polkadot/runtime/polkadot/src/lib.rs b/polkadot/runtime/polkadot/src/lib.rs index ebf881b221..719fd64cc7 100644 --- a/polkadot/runtime/polkadot/src/lib.rs +++ b/polkadot/runtime/polkadot/src/lib.rs @@ -1480,7 +1480,7 @@ pub type Executive = frame_executive::Executive< AllPalletsWithSystem, ( FixCouncilDepositMigration, - CrowdloanIndexMigration, + SlotsCrowdloanIndexMigration, pallet_staking::migrations::v9::InjectValidatorsIntoVoterList, ), >; @@ -1488,20 +1488,20 @@ pub type Executive = frame_executive::Executive< pub type SignedPayload = generic::SignedPayload; // Migration for crowdloan pallet to use fund index for account generation. -pub struct CrowdloanIndexMigration; -impl OnRuntimeUpgrade for CrowdloanIndexMigration { +pub struct SlotsCrowdloanIndexMigration; +impl OnRuntimeUpgrade for SlotsCrowdloanIndexMigration { fn on_runtime_upgrade() -> frame_support::weights::Weight { - crowdloan::migration::crowdloan_index_migration::migrate::() + slots::migration::slots_crowdloan_index_migration::migrate::() } #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result<(), &'static str> { - crowdloan::migration::crowdloan_index_migration::pre_migrate::() + slots::migration::slots_crowdloan_index_migration::pre_migrate::() } #[cfg(feature = "try-runtime")] fn post_upgrade() -> Result<(), &'static str> { - crowdloan::migration::crowdloan_index_migration::post_migrate::() + slots::migration::slots_crowdloan_index_migration::post_migrate::() } } diff --git a/polkadot/runtime/westend/src/lib.rs b/polkadot/runtime/westend/src/lib.rs index 9b679b9fbc..02fe6d6cc7 100644 --- a/polkadot/runtime/westend/src/lib.rs +++ b/polkadot/runtime/westend/src/lib.rs @@ -1086,7 +1086,7 @@ pub type Executive = frame_executive::Executive< Runtime, AllPalletsWithSystem, ( - CrowdloanIndexMigration, + SlotsCrowdloanIndexMigration, pallet_staking::migrations::v9::InjectValidatorsIntoVoterList, ), >; @@ -1094,20 +1094,20 @@ pub type Executive = frame_executive::Executive< pub type SignedPayload = generic::SignedPayload; // Migration for crowdloan pallet to use fund index for account generation. -pub struct CrowdloanIndexMigration; -impl OnRuntimeUpgrade for CrowdloanIndexMigration { +pub struct SlotsCrowdloanIndexMigration; +impl OnRuntimeUpgrade for SlotsCrowdloanIndexMigration { fn on_runtime_upgrade() -> frame_support::weights::Weight { - crowdloan::migration::crowdloan_index_migration::migrate::() + slots::migration::slots_crowdloan_index_migration::migrate::() } #[cfg(feature = "try-runtime")] fn pre_upgrade() -> Result<(), &'static str> { - crowdloan::migration::crowdloan_index_migration::pre_migrate::() + slots::migration::slots_crowdloan_index_migration::pre_migrate::() } #[cfg(feature = "try-runtime")] fn post_upgrade() -> Result<(), &'static str> { - crowdloan::migration::crowdloan_index_migration::post_migrate::() + slots::migration::slots_crowdloan_index_migration::post_migrate::() } }