Skip to content

Commit 1a0af36

Browse files
gavofyorkggwpez
andauthored
Tweak to active total migrations (paritytech#12832)
* Tweak to active total migrations * Formatting * Expose trait * Remove empty pre_ and post_upgrade hooks. Signed-off-by: Oliver Tale-Yazdi <[email protected]> Signed-off-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: Oliver Tale-Yazdi <[email protected]>
1 parent 9812205 commit 1a0af36

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

Diff for: frame/balances/src/migration.rs

+24-11
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ use frame_support::{pallet_prelude::*, traits::OnRuntimeUpgrade, weights::Weight
1919

2020
// NOTE: This must be used alongside the account whose balance is expected to be inactive.
2121
// Generally this will be used for the XCM teleport checking account.
22-
pub struct MigrateToTrackInactive<T: Config, A: Get<T::AccountId>>(
23-
sp_std::marker::PhantomData<(T, A)>,
24-
);
22+
pub struct MigrateToTrackInactive<T, A>(PhantomData<(T, A)>);
2523
impl<T: Config, A: Get<T::AccountId>> OnRuntimeUpgrade for MigrateToTrackInactive<T, A> {
2624
fn on_runtime_upgrade() -> Weight {
2725
let current_version = Pallet::<T>::current_storage_version();
@@ -32,20 +30,35 @@ impl<T: Config, A: Get<T::AccountId>> OnRuntimeUpgrade for MigrateToTrackInactiv
3230
Pallet::<T>::deactivate(b);
3331
current_version.put::<Pallet<T>>();
3432
log::info!(target: "runtime::balances", "Storage to version {:?}", current_version);
35-
T::DbWeight::get().reads_writes(3, 3)
33+
T::DbWeight::get().reads_writes(4, 3)
3634
} else {
3735
log::info!(target: "runtime::balances", "Migration did not execute. This probably should be removed");
3836
T::DbWeight::get().reads(2)
3937
}
4038
}
39+
}
4140

42-
#[cfg(feature = "try-runtime")]
43-
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
44-
Ok(vec![])
45-
}
41+
// NOTE: This must be used alongside the account whose balance is expected to be inactive.
42+
// Generally this will be used for the XCM teleport checking account.
43+
pub struct MigrateManyToTrackInactive<T, A>(PhantomData<(T, A)>);
44+
impl<T: Config, A: Get<Vec<T::AccountId>>> OnRuntimeUpgrade for MigrateManyToTrackInactive<T, A> {
45+
fn on_runtime_upgrade() -> Weight {
46+
let current_version = Pallet::<T>::current_storage_version();
47+
let onchain_version = Pallet::<T>::on_chain_storage_version();
4648

47-
#[cfg(feature = "try-runtime")]
48-
fn post_upgrade(total: Vec<u8>) -> Result<(), &'static str> {
49-
Ok(())
49+
if onchain_version == 0 && current_version == 1 {
50+
let accounts = A::get();
51+
let total = accounts
52+
.iter()
53+
.map(|a| Pallet::<T>::total_balance(a))
54+
.fold(T::Balance::zero(), |a, e| a.saturating_add(e));
55+
Pallet::<T>::deactivate(total);
56+
current_version.put::<Pallet<T>>();
57+
log::info!(target: "runtime::balances", "Storage to version {:?}", current_version);
58+
T::DbWeight::get().reads_writes(3 + accounts.len() as u64, 3)
59+
} else {
60+
log::info!(target: "runtime::balances", "Migration did not execute. This probably should be removed");
61+
T::DbWeight::get().reads(2)
62+
}
5063
}
5164
}

Diff for: frame/support/src/traits.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
pub mod tokens;
2323
pub use tokens::{
2424
currency::{
25-
Currency, LockIdentifier, LockableCurrency, NamedReservableCurrency, ReservableCurrency,
26-
TotalIssuanceOf, VestingSchedule,
25+
ActiveIssuanceOf, Currency, LockIdentifier, LockableCurrency, NamedReservableCurrency,
26+
ReservableCurrency, TotalIssuanceOf, VestingSchedule,
2727
},
2828
fungible, fungibles,
2929
imbalance::{Imbalance, OnUnbalanced, SignedImbalance},

0 commit comments

Comments
 (0)