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