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

Commit 2704ab3

Browse files
authored
pallet-balances: Fix inactive funds migration (#12840)
* pallet-balances: Fix inactive funds migration Fixes the inactive funds migration. It was missing to set the `storage_version` attribute for the `Pallet` struct. Besides that it also removes the old `StorageVersion` representation and adds support for instances of pallet-balances. * Fix test
1 parent 0ab43bc commit 2704ab3

3 files changed

Lines changed: 50 additions & 62 deletions

File tree

frame/balances/src/lib.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,13 @@ pub mod pallet {
246246
type ReserveIdentifier: Parameter + Member + MaxEncodedLen + Ord + Copy;
247247
}
248248

249+
/// The current storage version.
250+
const STORAGE_VERSION: frame_support::traits::StorageVersion =
251+
frame_support::traits::StorageVersion::new(1);
252+
249253
#[pallet::pallet]
250254
#[pallet::generate_store(pub(super) trait Store)]
255+
#[pallet::storage_version(STORAGE_VERSION)]
251256
pub struct Pallet<T, I = ()>(PhantomData<(T, I)>);
252257

253258
#[pallet::call]
@@ -556,13 +561,6 @@ pub mod pallet {
556561
ValueQuery,
557562
>;
558563

559-
/// Storage version of the pallet.
560-
///
561-
/// This is set to v2.0.0 for new networks.
562-
#[pallet::storage]
563-
pub(super) type StorageVersion<T: Config<I>, I: 'static = ()> =
564-
StorageValue<_, Releases, ValueQuery>;
565-
566564
#[pallet::genesis_config]
567565
pub struct GenesisConfig<T: Config<I>, I: 'static = ()> {
568566
pub balances: Vec<(T::AccountId, T::Balance)>,
@@ -581,8 +579,6 @@ pub mod pallet {
581579
let total = self.balances.iter().fold(Zero::zero(), |acc: T::Balance, &(_, n)| acc + n);
582580
<TotalIssuance<T, I>>::put(total);
583581

584-
<StorageVersion<T, I>>::put(Releases::V2_0_0);
585-
586582
for (_, balance) in &self.balances {
587583
assert!(
588584
*balance >= <T as Config<I>>::ExistentialDeposit::get(),
@@ -727,21 +723,6 @@ impl<Balance: Saturating + Copy + Ord> AccountData<Balance> {
727723
}
728724
}
729725

730-
// A value placed in storage that represents the current version of the Balances storage.
731-
// This value is used by the `on_runtime_upgrade` logic to determine whether we run
732-
// storage migration logic. This should match directly with the semantic versions of the Rust crate.
733-
#[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, RuntimeDebug, MaxEncodedLen, TypeInfo)]
734-
enum Releases {
735-
V1_0_0,
736-
V2_0_0,
737-
}
738-
739-
impl Default for Releases {
740-
fn default() -> Self {
741-
Releases::V1_0_0
742-
}
743-
}
744-
745726
pub struct DustCleaner<T: Config<I>, I: 'static = ()>(
746727
Option<(T::AccountId, NegativeImbalance<T, I>)>,
747728
);

frame/balances/src/migration.rs

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,57 @@
1515
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
1616

1717
use super::*;
18-
use frame_support::{pallet_prelude::*, traits::OnRuntimeUpgrade, weights::Weight};
18+
use frame_support::{
19+
pallet_prelude::*,
20+
traits::{OnRuntimeUpgrade, PalletInfoAccess},
21+
weights::Weight,
22+
};
1923

20-
// NOTE: This must be used alongside the account whose balance is expected to be inactive.
21-
// Generally this will be used for the XCM teleport checking account.
22-
pub struct MigrateToTrackInactive<T, A>(PhantomData<(T, A)>);
23-
impl<T: Config, A: Get<T::AccountId>> OnRuntimeUpgrade for MigrateToTrackInactive<T, A> {
24-
fn on_runtime_upgrade() -> Weight {
25-
let current_version = Pallet::<T>::current_storage_version();
26-
let onchain_version = Pallet::<T>::on_chain_storage_version();
24+
fn migrate_v0_to_v1<T: Config<I>, I: 'static>(accounts: &[T::AccountId]) -> Weight {
25+
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
26+
27+
if onchain_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+
// Remove the old `StorageVersion` type.
35+
frame_support::storage::unhashed::kill(&frame_support::storage::storage_prefix(
36+
Pallet::<T, I>::name().as_bytes(),
37+
"StorageVersion".as_bytes(),
38+
));
2739

28-
if onchain_version == 0 && current_version == 1 {
29-
let b = Pallet::<T>::total_balance(&A::get());
30-
Pallet::<T>::deactivate(b);
31-
current_version.put::<Pallet<T>>();
32-
log::info!(target: "runtime::balances", "Storage to version {:?}", current_version);
33-
T::DbWeight::get().reads_writes(4, 3)
34-
} else {
35-
log::info!(target: "runtime::balances", "Migration did not execute. This probably should be removed");
36-
T::DbWeight::get().reads(2)
37-
}
40+
// Set storage version to `1`.
41+
StorageVersion::new(1).put::<Pallet<T, I>>();
42+
43+
log::info!(target: "runtime::balances", "Storage to version 1");
44+
T::DbWeight::get().reads_writes(2 + accounts.len() as u64, 3)
45+
} else {
46+
log::info!(target: "runtime::balances", "Migration did not execute. This probably should be removed");
47+
T::DbWeight::get().reads(1)
3848
}
3949
}
4050

4151
// NOTE: This must be used alongside the account whose balance is expected to be inactive.
4252
// 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> {
53+
pub struct MigrateToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
54+
impl<T: Config<I>, A: Get<T::AccountId>, I: 'static> OnRuntimeUpgrade
55+
for MigrateToTrackInactive<T, A, I>
56+
{
4557
fn on_runtime_upgrade() -> Weight {
46-
let current_version = Pallet::<T>::current_storage_version();
47-
let onchain_version = Pallet::<T>::on_chain_storage_version();
58+
migrate_v0_to_v1::<T, I>(&[A::get()])
59+
}
60+
}
4861

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-
}
62+
// NOTE: This must be used alongside the accounts whose balance is expected to be inactive.
63+
// Generally this will be used for the XCM teleport checking accounts.
64+
pub struct MigrateManyToTrackInactive<T, A, I = ()>(PhantomData<(T, A, I)>);
65+
impl<T: Config<I>, A: Get<Vec<T::AccountId>>, I: 'static> OnRuntimeUpgrade
66+
for MigrateManyToTrackInactive<T, A, I>
67+
{
68+
fn on_runtime_upgrade() -> Weight {
69+
migrate_v0_to_v1::<T, I>(&A::get())
6370
}
6471
}

frame/executive/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,13 @@ mod tests {
951951
block_import_works_inner(
952952
new_test_ext_v0(1),
953953
array_bytes::hex_n_into_unchecked(
954-
"0d786e24c1f9e6ce237806a22c005bbbc7dee4edd6692b6c5442843d164392de",
954+
"216e61b2689d1243eb56d89c9084db48e50ebebc4871d758db131432c675d7c0",
955955
),
956956
);
957957
block_import_works_inner(
958958
new_test_ext(1),
959959
array_bytes::hex_n_into_unchecked(
960-
"348485a4ab856467b440167e45f99b491385e8528e09b0e51f85f814a3021c93",
960+
"4738b4c0aab02d6ddfa62a2a6831ccc975a9f978f7db8d7ea8e68eba8639530a",
961961
),
962962
);
963963
}

0 commit comments

Comments
 (0)