-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Polkadot v1.7.2 migrations #1849
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
92b490b
feat: add hold reason migration
wischli 2a409c1
feat: centrifuge migrations
wischli 9c443c0
feat: altair migrations
wischli a073f37
feat: dev + demo migrations
wischli 1701762
fix: clippy
wischli ca29774
fix: build
wischli 9ec1a7e
fmt: fix using nightly
wischli 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 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
This file contains 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
This file contains 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,151 @@ | ||
// Copyright 2023 Centrifuge Foundation (centrifuge.io). | ||
// | ||
// This file is part of the Centrifuge chain project. | ||
// Centrifuge 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 (see http://www.gnu.org/licenses). | ||
// Centrifuge 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. | ||
|
||
use cfg_primitives::{AccountId, Balance}; | ||
use frame_support::{ | ||
pallet_prelude::ValueQuery, | ||
storage_alias, | ||
traits::{ConstU32, Get, Len, OnRuntimeUpgrade, VariantCountOf}, | ||
Blake2_128Concat, BoundedVec, Parameter, | ||
}; | ||
use pallet_balances::IdAmount; | ||
use pallet_order_book::weights::Weight; | ||
use pallet_transfer_allowlist::HoldReason; | ||
#[cfg(feature = "try-runtime")] | ||
use parity_scale_codec::{Decode, Encode}; | ||
use parity_scale_codec::{FullCodec, FullEncode}; | ||
#[cfg(feature = "try-runtime")] | ||
use sp_runtime::Saturating; | ||
#[cfg(feature = "try-runtime")] | ||
use sp_runtime::TryRuntimeError; | ||
use sp_runtime::{traits::Member, SaturatedConversion}; | ||
use sp_std::{vec, vec::Vec}; | ||
|
||
const LOG_PREFIX: &str = "MigrateTransferAllowList HoldReason:"; | ||
|
||
pub struct MigrateTransferAllowListHolds<T, RuntimeHoldReason>( | ||
sp_std::marker::PhantomData<(T, RuntimeHoldReason)>, | ||
); | ||
|
||
type OldHolds = BoundedVec<IdAmount<(), Balance>, ConstU32<10>>; | ||
type NewHolds<RuntimeHoldReason> = | ||
BoundedVec<IdAmount<RuntimeHoldReason, Balance>, VariantCountOf<RuntimeHoldReason>>; | ||
|
||
#[storage_alias] | ||
pub type Holds<T: pallet_balances::Config> = | ||
StorageMap<pallet_balances::Pallet<T>, Blake2_128Concat, AccountId, OldHolds, ValueQuery>; | ||
|
||
impl<T, RuntimeHoldReason> OnRuntimeUpgrade for MigrateTransferAllowListHolds<T, RuntimeHoldReason> | ||
where | ||
T: pallet_balances::Config<Balance = Balance, RuntimeHoldReason = RuntimeHoldReason> | ||
+ pallet_transfer_allowlist::Config | ||
+ frame_system::Config<AccountId = AccountId>, | ||
<T as pallet_balances::Config>::RuntimeHoldReason: From<pallet_transfer_allowlist::HoldReason>, | ||
RuntimeHoldReason: frame_support::traits::VariantCount | ||
+ FullCodec | ||
+ FullEncode | ||
+ Parameter | ||
+ Member | ||
+ sp_std::fmt::Debug, | ||
{ | ||
fn on_runtime_upgrade() -> Weight { | ||
let transfer_allowlist_accounts: Vec<AccountId> = | ||
pallet_transfer_allowlist::AccountCurrencyTransferAllowance::<T>::iter_keys() | ||
.map(|(a, _, _)| a) | ||
.collect(); | ||
let mut weight = | ||
T::DbWeight::get().reads(transfer_allowlist_accounts.len().saturated_into()); | ||
|
||
pallet_balances::Holds::<T>::translate::<OldHolds, _>(|who, holds| { | ||
if Self::account_can_be_migrated(&who, &transfer_allowlist_accounts) { | ||
log::info!("{LOG_PREFIX} Migrating hold for account {who:?}"); | ||
let id_amount = IdAmount::<RuntimeHoldReason, Balance> { | ||
id: HoldReason::TransferAllowance.into(), | ||
// Non-emptiness ensured above | ||
amount: holds[0].amount, | ||
}; | ||
weight.saturating_accrue(T::DbWeight::get().reads_writes(1, 1)); | ||
|
||
Some(NewHolds::truncate_from(vec![id_amount])) | ||
} else { | ||
None | ||
} | ||
}); | ||
|
||
log::info!( | ||
"{LOG_PREFIX} migrated {:?} accounts", | ||
transfer_allowlist_accounts.len() | ||
); | ||
|
||
weight | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> { | ||
let transfer_allowlist_accounts: Vec<AccountId> = | ||
pallet_transfer_allowlist::AccountCurrencyTransferAllowance::<T>::iter_keys() | ||
.map(|(a, _, _)| a) | ||
.collect(); | ||
|
||
let mut n = 0u64; | ||
for (acc, _) in Holds::<T>::iter() { | ||
assert!(Self::account_can_be_migrated( | ||
&acc, | ||
&transfer_allowlist_accounts | ||
)); | ||
n.saturating_accrue(1); | ||
} | ||
|
||
log::info!("{LOG_PREFIX} pre checks done"); | ||
Ok(n.encode()) | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade(pre_state: Vec<u8>) -> Result<(), TryRuntimeError> { | ||
let count_pre: u64 = Decode::decode(&mut pre_state.as_slice()) | ||
.expect("pre_upgrade provides a valid state; qed"); | ||
|
||
let holds_post = pallet_balances::Holds::<T>::iter(); | ||
let count_post: u64 = holds_post.count().saturated_into(); | ||
assert_eq!(count_pre, count_post); | ||
|
||
for (_, hold) in pallet_balances::Holds::<T>::iter() { | ||
assert_eq!(hold.len(), 1); | ||
assert_eq!(hold[0].id, HoldReason::TransferAllowance.into()); | ||
} | ||
|
||
log::info!("{LOG_PREFIX} post checks done"); | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl<T, RuntimeHoldReason> MigrateTransferAllowListHolds<T, RuntimeHoldReason> | ||
where | ||
T: pallet_balances::Config | ||
+ pallet_transfer_allowlist::Config | ||
+ frame_system::Config<AccountId = AccountId>, | ||
{ | ||
fn account_can_be_migrated(who: &AccountId, whitelist: &[AccountId]) -> bool { | ||
if !whitelist.iter().any(|a| a == who) { | ||
log::warn!("{LOG_PREFIX} Account {who:?} is skipped due to missing AccountCurrencyTransferAllowance storage entry"); | ||
return false; | ||
} | ||
|
||
match Holds::<T>::get(who) { | ||
holds if holds.len() == 1 => true, | ||
_ => { | ||
log::warn!("{LOG_PREFIX} Account {who:?} does not meet Hold storage assumptions"); | ||
false | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if Self::account_can_be_migrated(&who, &transfer_allowlist_accounts)
is false, then if the old Hold storage still has an entry, that means that we need to migrate it withpallet_restricted_tokens::HoldReason::NativeIndex.into()
because it means that someone used that pallet to hold something thought that pallet.Or am I wrong in my assumption?
The flow will be:
pallet_transfer_allowlist::HoldReason::TransferAllowance.into()
pallet_restricted_tokens::HoldReason::NativeIndex.into()
None
Still scared about how can we know that an account that has been used by transfer allowance has not also be used by restricted_tokens 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have three code paths which use
Currency::hold
in our codebase:pallet-rewards
when depositing stakepallet-transfer-allowlist
when adding a transfer filterpallet-orderbook
when placing or updating an order.All three codepaths use the
RestrictedTokens
as theirCurrency
trait which provides two implementations forhold
: One for native currency (i.e. fungible which falls back to the impl ofpallet-balances
) and one for non-native assets (i.e. fungibles which falls back to the impl oforml-tokens
). The crucial part is that theorml-tokens
implementation ofhold
has not made use of theRuntimeHoldReason
(it does not even have storage for that) such that we only need to migrate theHolds
storage ofpallet-balances
.Now coming back to the three codepaths which make use of
hold
API: Bothpallet-rewards
andpallet-orderbook
only hold fungibles because staking currency is not native and we can assume noCFG <-> *
orders to exist until we execute the RU. So there can only be a single reason why there exists an entry inpallet_balances::Holds
storage which points back to the transfer allowlist. There cannot be any other stored holds.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your reasoning!
Just one thing. Root can call the extrinsic
restricted_tokens::set_balance()
(not sure if this already happened), modifying the hold amount in the account for CFGThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we never use
orderbook
to create an order with CFGThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Luckily,
set_balance
is rather a dev tool than something which is used in production. Ironically, we have used it once on Centrifuge Chain to recover a lost wallet from an early Gnosis backer (-> Gov forum) but did not set any holds there. Again, I have checked all storage on all our chains to ensure this migration works. Before we propose the RU, I will redo that step.Assuming the worst case of some unexpected hold to spawn in between RU proposal and execution, which is not tied to transfer allowlist, then we can fix this afterwards. The user will just not be able to remove the hold until then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I think we have all these cases tied!