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

Allow two Parachains to swap #4772

Merged
merged 26 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
fd778cf
add support for parachain to parachain swap
shawntabrizi Jan 23, 2022
3eaa619
enable swaps on kusama
shawntabrizi Jan 23, 2022
31426cf
sanity test in paras_registrar
shawntabrizi Jan 23, 2022
d457803
express more errors
shawntabrizi Jan 23, 2022
331a835
finish up tests
shawntabrizi Jan 23, 2022
bc23e47
fmt
shawntabrizi Jan 23, 2022
3916763
Merge branch 'master' into shawntabrizi-complete-swap
shawntabrizi Jan 23, 2022
578ad43
make fields pub
shawntabrizi Jan 23, 2022
4981fdb
refactor integration tests to use real accounts
shawntabrizi Jan 23, 2022
2527a7c
Merge branch 'master' into shawntabrizi-complete-swap
shawntabrizi Feb 1, 2022
9548b0a
Update Crowdloan Account to FundIndex (#4824)
shawntabrizi Feb 1, 2022
c643273
finish parachain swap test
shawntabrizi Feb 1, 2022
a54d1f2
format
shawntabrizi Feb 1, 2022
c963177
fix warning
shawntabrizi Feb 1, 2022
3d85c33
fix spacing
shawntabrizi Feb 1, 2022
19f7a04
fix formatting
shawntabrizi Feb 1, 2022
ba1dc91
write migrations
shawntabrizi Feb 1, 2022
ed04dc9
add migration
shawntabrizi Feb 1, 2022
61df5dc
fixes
shawntabrizi Feb 1, 2022
603771d
more fixes to migration
shawntabrizi Feb 1, 2022
5d82def
Merge branch 'master' into shawntabrizi-complete-swap
shawntabrizi Feb 1, 2022
0ad9e8f
Update runtime/common/src/crowdloan/mod.rs
shawntabrizi Feb 1, 2022
e38d881
Update runtime/common/src/paras_registrar.rs
shawntabrizi Feb 1, 2022
5c3137b
Merge branch 'master' into shawntabrizi-complete-swap
shawntabrizi Feb 6, 2022
dd2eaca
Update migration.rs
shawntabrizi Feb 7, 2022
6217ef5
extract swap function
shawntabrizi Feb 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions runtime/common/src/crowdloan/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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 <http://www.gnu.org/licenses/>.

use super::*;
use frame_support::generate_storage_alias;

/// Migrations for using fund index to create fund accounts instead of para ID.
pub mod crowdloan_index_migration {
// The old way we generated fund accounts.
fn old_fund_account_id<T: Config>(index: ParaId) -> T::AccountId {
emostov marked this conversation as resolved.
Show resolved Hide resolved
T::PalletId::get().into_sub_account(index)
}

pub fn pre_migration<T: Config>() {
// `NextTrieIndex` should have a value.
generate_storage_alias!(Crowdloan, NextTrieIndex => Value<FundIndex>);
let next_index = NextTrieIndex::take().unwrap_or_default();
assert!(next_index > 0);

// Each fund should have some non-zero balance.
Funds::<T>::iter().for_each(|(para_id, fund)| {
let old_fund_account = old_fund_account_id::<T>(para_id);
let total_balance = T::Currency::total_balance(old_fund_account);

assert_eq!(total_balance, fund.raised);
assert!(total_balance > Zero::zero());
});
}

/// 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<T: Config>() {
// First migrate `NextTrieIndex` counter to `NextFundIndex`.
generate_storage_alias!(Crowdloan, NextTrieIndex => Value<FundIndex>);

let next_index = NextTrieIndex::take().unwrap_or_default();
NextFundIndex::<T>::set(next_index);

// Migrate all accounts from `old_fund_account` to `fund_account` using `fund_index`.
Funds::<T>::iter().for_each(|(para_id, fund)| {
let old_fund_account = old_fund_account_id::<T>(para_id);
let new_fund_account = Pallet::<T>::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::<T>::take(old_fund_account);
frame_system::Account::<T>::insert(new_fund_account, account_info);
});
}

pub fn post_migrate<T: Config>() {
// `NextTrieIndex` should not have a value, and `NextFundIndex` should.
generate_storage_alias!(Crowdloan, NextTrieIndex => Value<FundIndex>);
assert!(NextTrieIndex::take().is_none());
assert!(NextFundIndex::get() > 0);

// Each fund should have balance migrated correctly.
Funds::<T>::iter().for_each(|(para_id, fund)| {
// Old fund account is deleted.
let old_fund_account = old_fund_account_id::<T>(para_id);
assert_eq!(frame_system::Account::<T>::get(old_fund_account_id), Default::default());

// New fund account has the correct balance.
let new_fund_account = Pallet::<T>::fund_account_id(fund.fund_index);
let total_balance = T::Currency::total_balance(new_fund_account);

assert_eq!(total_balance, fund.raised);
assert!(total_balance > Zero::zero());
});
}
}
Loading