Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Substrate companion #11387
Browse files Browse the repository at this point in the history
  • Loading branch information
bkchr committed May 16, 2022
1 parent 3f7b1c2 commit 053e123
Showing 1 changed file with 10 additions and 20 deletions.
30 changes: 10 additions & 20 deletions runtime/common/src/crowdloan/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,27 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use super::*;
use frame_support::{generate_storage_alias, Twox64Concat};
use frame_support::{storage_alias, Twox64Concat};

/// Migrations for using fund index to create fund accounts instead of para ID.
pub mod crowdloan_index_migration {
use super::*;

#[storage_alias]
type NextTrieIndex<T: Config> = StorageValue<Pallet<T>, FundIndex>;

#[storage_alias]
type Leases<T: Config> = StorageMap<Pallet<T>, Twox64Concat, ParaId, Vec<Option<(<T as frame_system::Config>::AccountId, BalanceOf<T>)>>>;

// The old way we generated fund accounts.
fn old_fund_account_id<T: Config>(index: ParaId) -> T::AccountId {
T::PalletId::get().into_sub_account(index)
}

pub fn pre_migrate<T: Config>() -> Result<(), &'static str> {
// `NextTrieIndex` should have a value.
generate_storage_alias!(Crowdloan, NextTrieIndex => Value<FundIndex>);

generate_storage_alias!(
Slots,
Leases<T: Config> => Map<(Twox64Concat, ParaId), Vec<Option<(T::AccountId, BalanceOf<T>)>>>
);
let next_index = NextTrieIndex::get().unwrap_or_default();
let next_index = NextTrieIndex::<T>::get().unwrap_or_default();
ensure!(next_index > 0, "Next index is zero, which implies no migration is needed.");

log::info!(
Expand Down Expand Up @@ -87,13 +88,8 @@ pub mod crowdloan_index_migration {
let mut weight = 0;

// First migrate `NextTrieIndex` counter to `NextFundIndex`.
generate_storage_alias!(Crowdloan, NextTrieIndex => Value<FundIndex>);

generate_storage_alias!(
Slots,
Leases<T: Config> => Map<(Twox64Concat, ParaId), Vec<Option<(T::AccountId, BalanceOf<T>)>>>
);
let next_index = NextTrieIndex::take().unwrap_or_default();
let next_index = NextTrieIndex::<T>::take().unwrap_or_default();
NextFundIndex::<T>::set(next_index);

weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2));
Expand Down Expand Up @@ -127,13 +123,7 @@ pub mod crowdloan_index_migration {

pub fn post_migrate<T: Config>() -> Result<(), &'static str> {
// `NextTrieIndex` should not have a value, and `NextFundIndex` should.
generate_storage_alias!(Crowdloan, NextTrieIndex => Value<FundIndex>);
ensure!(NextTrieIndex::get().is_none(), "NextTrieIndex still has a value.");

generate_storage_alias!(
Slots,
Leases<T: Config> => Map<(Twox64Concat, ParaId), Vec<Option<(T::AccountId, BalanceOf<T>)>>>
);
ensure!(NextTrieIndex::<T>::get().is_none(), "NextTrieIndex still has a value.");

let next_index = NextFundIndex::<T>::get();
log::info!(
Expand Down

0 comments on commit 053e123

Please sign in to comment.