frame_system/migrations/
mod.rs1use super::LOG_TARGET;
21use crate::{Config, Pallet};
22use codec::{Decode, Encode, FullCodec};
23use frame_support::{
24 pallet_prelude::ValueQuery, traits::PalletInfoAccess, weights::Weight, Blake2_128Concat,
25};
26use sp_runtime::RuntimeDebug;
27
28type RefCount = u32;
30
31#[derive(Clone, Eq, PartialEq, Default, RuntimeDebug, Encode, Decode)]
33struct AccountInfo<Nonce, AccountData> {
34 nonce: Nonce,
35 consumers: RefCount,
36 providers: RefCount,
37 sufficients: RefCount,
38 data: AccountData,
39}
40
41pub trait V2ToV3 {
43 type Pallet: 'static + PalletInfoAccess;
45
46 type AccountId: 'static + FullCodec;
48
49 type Nonce: 'static + FullCodec + Copy;
51
52 type AccountData: 'static + FullCodec;
54}
55
56#[frame_support::storage_alias]
57type UpgradedToU32RefCount<T: Config> = StorageValue<Pallet<T>, bool, ValueQuery>;
58
59#[frame_support::storage_alias]
60type UpgradedToTripleRefCount<T: Config> = StorageValue<Pallet<T>, bool, ValueQuery>;
61
62#[frame_support::storage_alias]
63type Account<V, T: Config> = StorageMap<
64 Pallet<T>,
65 Blake2_128Concat,
66 <V as V2ToV3>::AccountId,
67 AccountInfo<<V as V2ToV3>::Nonce, <V as V2ToV3>::AccountData>,
68>;
69
70pub fn migrate_from_single_u8_to_triple_ref_count<V: V2ToV3, T: Config>() -> Weight {
72 let mut translated: usize = 0;
73 <Account<V, T>>::translate::<(V::Nonce, u8, V::AccountData), _>(|_key, (nonce, rc, data)| {
74 translated += 1;
75 Some(AccountInfo { nonce, consumers: rc as RefCount, providers: 1, sufficients: 0, data })
76 });
77 log::info!(
78 target: LOG_TARGET,
79 "Applied migration from single u8 to triple reference counting to {:?} elements.",
80 translated
81 );
82 <UpgradedToU32RefCount<T>>::put(true);
83 <UpgradedToTripleRefCount<T>>::put(true);
84 Weight::MAX
85}
86
87pub fn migrate_from_single_to_triple_ref_count<V: V2ToV3, T: Config>() -> Weight {
89 let mut translated: usize = 0;
90 <Account<V, T>>::translate::<(V::Nonce, RefCount, V::AccountData), _>(
91 |_key, (nonce, consumers, data)| {
92 translated += 1;
93 Some(AccountInfo { nonce, consumers, providers: 1, sufficients: 0, data })
94 },
95 );
96 log::info!(
97 target: LOG_TARGET,
98 "Applied migration from single to triple reference counting to {:?} elements.",
99 translated
100 );
101 <UpgradedToTripleRefCount<T>>::put(true);
102 Weight::MAX
103}
104
105pub fn migrate_from_dual_to_triple_ref_count<V: V2ToV3, T: Config>() -> Weight {
107 let mut translated: usize = 0;
108 <Account<V, T>>::translate::<(V::Nonce, RefCount, RefCount, V::AccountData), _>(
109 |_key, (nonce, consumers, providers, data)| {
110 translated += 1;
111 Some(AccountInfo { nonce, consumers, providers, sufficients: 0, data })
112 },
113 );
114 log::info!(
115 target: LOG_TARGET,
116 "Applied migration from dual to triple reference counting to {:?} elements.",
117 translated
118 );
119 <UpgradedToTripleRefCount<T>>::put(true);
120 Weight::MAX
121}