pallet_balances/
migration.rs1use super::*;
18use frame_support::{
19 pallet_prelude::*,
20 traits::{OnRuntimeUpgrade, PalletInfoAccess},
21 weights::Weight,
22};
23
24fn migrate_v0_to_v1<T: Config<I>, I: 'static>(accounts: &[T::AccountId]) -> Weight {
25 let on_chain_version = Pallet::<T, I>::on_chain_storage_version();
26
27 if on_chain_version == 0 {
28 let total = accounts
29 .iter()
30 .map(|a| Pallet::<T, I>::total_balance(a))
31 .fold(T::Balance::zero(), |a, e| a.saturating_add(e));
32 Pallet::<T, I>::deactivate(total);
33
34 frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
36 Pallet::<T, I>::name().as_bytes(),
37 "StorageVersion".as_bytes(),
38 ));
39
40 StorageVersion::new(1).put::<Pallet<T, I>>();
42
43 log::info!(target: LOG_TARGET, "Storage to version 1");
44 T::DbWeight::get().reads_writes(2 + accounts.len() as u64, 3)
45 } else {
46 log::info!(
47 target: LOG_TARGET,
48 "Migration did not execute. This probably should be removed"
49 );
50 T::DbWeight::get().reads(1)
51 }
52}
53
54pub struct MigrateToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
57impl<T: Config<I>, A: Get<T::AccountId>, I: 'static> OnRuntimeUpgrade
58 for MigrateToTrackInactive<T, A, I>
59{
60 fn on_runtime_upgrade() -> Weight {
61 migrate_v0_to_v1::<T, I>(&[A::get()])
62 }
63}
64
65pub struct MigrateManyToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
68impl<T: Config<I>, A: Get<Vec<T::AccountId>>, I: 'static> OnRuntimeUpgrade
69 for MigrateManyToTrackInactive<T, A, I>
70{
71 fn on_runtime_upgrade() -> Weight {
72 migrate_v0_to_v1::<T, I>(&A::get())
73 }
74}
75
76pub struct ResetInactive<T, I = ()>(PhantomData<(T, I)>);
77impl<T: Config<I>, I: 'static> OnRuntimeUpgrade for ResetInactive<T, I> {
78 fn on_runtime_upgrade() -> Weight {
79 let on_chain_version = Pallet::<T, I>::on_chain_storage_version();
80
81 if on_chain_version == 1 {
82 frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
84 Pallet::<T, I>::name().as_bytes(),
85 "StorageVersion".as_bytes(),
86 ));
87
88 InactiveIssuance::<T, I>::kill();
89
90 StorageVersion::new(0).put::<Pallet<T, I>>();
92
93 log::info!(target: LOG_TARGET, "Storage to version 0");
94 T::DbWeight::get().reads_writes(1, 3)
95 } else {
96 log::info!(
97 target: LOG_TARGET,
98 "Migration did not execute. This probably should be removed"
99 );
100 T::DbWeight::get().reads(1)
101 }
102 }
103}