This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Allow two Parachains to swap #4772
Merged
Merged
Changes from all 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 3eaa619
enable swaps on kusama
shawntabrizi 31426cf
sanity test in paras_registrar
shawntabrizi d457803
express more errors
shawntabrizi 331a835
finish up tests
shawntabrizi bc23e47
fmt
shawntabrizi 3916763
Merge branch 'master' into shawntabrizi-complete-swap
shawntabrizi 578ad43
make fields pub
shawntabrizi 4981fdb
refactor integration tests to use real accounts
shawntabrizi 2527a7c
Merge branch 'master' into shawntabrizi-complete-swap
shawntabrizi 9548b0a
Update Crowdloan Account to FundIndex (#4824)
shawntabrizi c643273
finish parachain swap test
shawntabrizi a54d1f2
format
shawntabrizi c963177
fix warning
shawntabrizi 3d85c33
fix spacing
shawntabrizi 19f7a04
fix formatting
shawntabrizi ba1dc91
write migrations
shawntabrizi ed04dc9
add migration
shawntabrizi 61df5dc
fixes
shawntabrizi 603771d
more fixes to migration
shawntabrizi 5d82def
Merge branch 'master' into shawntabrizi-complete-swap
shawntabrizi 0ad9e8f
Update runtime/common/src/crowdloan/mod.rs
shawntabrizi e38d881
Update runtime/common/src/paras_registrar.rs
shawntabrizi 5c3137b
Merge branch 'master' into shawntabrizi-complete-swap
shawntabrizi dd2eaca
Update migration.rs
shawntabrizi 6217ef5
extract swap function
shawntabrizi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // 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 { | ||
| use super::*; | ||
|
|
||
| // 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>); | ||
| let next_index = NextTrieIndex::get().unwrap_or_default(); | ||
emostov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ensure!(next_index > 0, "Next index is zero, which implies no migration is needed."); | ||
|
|
||
| log::info!( | ||
| target: "runtime", | ||
| "next trie index: {:?}", | ||
| next_index, | ||
| ); | ||
emostov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Each fund should have some non-zero balance. | ||
| for (para_id, fund) in Funds::<T>::iter() { | ||
| let old_fund_account = old_fund_account_id::<T>(para_id); | ||
| let total_balance = CurrencyOf::<T>::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 | ||
emostov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| ensure!( | ||
| total_balance >= fund.raised, | ||
| "Total balance is not equal to the funds raised." | ||
| ); | ||
| ensure!(total_balance > Zero::zero(), "Total balance is equal to zero."); | ||
| } | ||
|
|
||
| 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<T: Config>() -> frame_support::weights::Weight { | ||
| let mut weight = 0; | ||
|
|
||
| // 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); | ||
|
|
||
| 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::<T>::iter() { | ||
| 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); | ||
|
|
||
| weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 2)); | ||
| } | ||
|
|
||
| weight | ||
| } | ||
|
|
||
| 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."); | ||
|
|
||
| let next_index = NextFundIndex::<T>::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::<T>::iter() { | ||
| // Old fund account is deleted. | ||
| let old_fund_account = old_fund_account_id::<T>(para_id); | ||
| ensure!( | ||
| frame_system::Account::<T>::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::<T>::fund_account_id(fund.fund_index); | ||
| let total_balance = CurrencyOf::<T>::total_balance(&new_fund_account); | ||
|
|
||
| ensure!( | ||
| total_balance >= fund.raised, | ||
| "Total balance in new account is different than the funds raised." | ||
| ); | ||
| ensure!(total_balance > Zero::zero(), "Total balance in the account is zero."); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.