Skip to content

Commit

Permalink
Substrate companion #11387 (#5531)
Browse files Browse the repository at this point in the history
* Substrate companion #11387

paritytech/substrate#11387

* Update runtime/common/src/crowdloan/migration.rs

* cargo update -p sp-io

* fmt

Co-authored-by: Shawn Tabrizi <[email protected]>
  • Loading branch information
bkchr and shawntabrizi authored May 17, 2022
1 parent fa123dc commit 6f41a7d
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions polkadot/runtime/common/src/crowdloan/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,32 @@
// 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<
Slots,
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_truncating(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 +93,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 +128,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 6f41a7d

Please sign in to comment.