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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use sp_io::hashing::twox_128;
use sp_std::str;
use frame_support::{
storage::{generator::StorageValue, StoragePrefixedMap},
traits::{
Get, GetStorageVersion, PalletInfoAccess, StorageVersion,
STORAGE_VERSION_STORAGE_KEY_POSTFIX,
},
weights::Weight,
};
use crate::historical as pallet_session_historical;
const OLD_PREFIX: &str = "Session";
pub fn migrate<T: pallet_session_historical::Config, P: GetStorageVersion + PalletInfoAccess>(
) -> Weight {
let new_pallet_name = <P as PalletInfoAccess>::name();
if new_pallet_name == OLD_PREFIX {
log::info!(
target: "runtime::session_historical",
"New pallet name is equal to the old prefix. No migration needs to be done.",
);
return Weight::zero()
}
let on_chain_storage_version = <P as GetStorageVersion>::on_chain_storage_version();
log::info!(
target: "runtime::session_historical",
"Running migration to v1 for session_historical with storage version {:?}",
on_chain_storage_version,
);
if on_chain_storage_version < 1 {
let storage_prefix = pallet_session_historical::HistoricalSessions::<T>::storage_prefix();
frame_support::storage::migration::move_storage_from_pallet(
storage_prefix,
OLD_PREFIX.as_bytes(),
new_pallet_name.as_bytes(),
);
log_migration("migration", storage_prefix, OLD_PREFIX, new_pallet_name);
let storage_prefix = pallet_session_historical::StoredRange::<T>::storage_prefix();
frame_support::storage::migration::move_storage_from_pallet(
storage_prefix,
OLD_PREFIX.as_bytes(),
new_pallet_name.as_bytes(),
);
log_migration("migration", storage_prefix, OLD_PREFIX, new_pallet_name);
StorageVersion::new(1).put::<P>();
<T as frame_system::Config>::BlockWeights::get().max_block
} else {
log::warn!(
target: "runtime::session_historical",
"Attempted to apply migration to v1 but failed because storage version is {:?}",
on_chain_storage_version,
);
Weight::zero()
}
}
pub fn pre_migrate<
T: pallet_session_historical::Config,
P: GetStorageVersion + PalletInfoAccess,
>() {
let new_pallet_name = <P as PalletInfoAccess>::name();
let storage_prefix_historical_sessions =
pallet_session_historical::HistoricalSessions::<T>::storage_prefix();
let storage_prefix_stored_range = pallet_session_historical::StoredRange::<T>::storage_prefix();
log_migration("pre-migration", storage_prefix_historical_sessions, OLD_PREFIX, new_pallet_name);
log_migration("pre-migration", storage_prefix_stored_range, OLD_PREFIX, new_pallet_name);
if new_pallet_name == OLD_PREFIX {
return
}
let new_pallet_prefix = twox_128(new_pallet_name.as_bytes());
let storage_version_key = twox_128(STORAGE_VERSION_STORAGE_KEY_POSTFIX);
let mut new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
new_pallet_prefix.to_vec(),
new_pallet_prefix.to_vec(),
|key| Ok(key.to_vec()),
);
assert!(new_pallet_prefix_iter.all(|key| key == storage_version_key));
assert!(<P as GetStorageVersion>::on_chain_storage_version() < 1);
}
pub fn post_migrate<
T: pallet_session_historical::Config,
P: GetStorageVersion + PalletInfoAccess,
>() {
let new_pallet_name = <P as PalletInfoAccess>::name();
let storage_prefix_historical_sessions =
pallet_session_historical::HistoricalSessions::<T>::storage_prefix();
let storage_prefix_stored_range = pallet_session_historical::StoredRange::<T>::storage_prefix();
log_migration(
"post-migration",
storage_prefix_historical_sessions,
OLD_PREFIX,
new_pallet_name,
);
log_migration("post-migration", storage_prefix_stored_range, OLD_PREFIX, new_pallet_name);
if new_pallet_name == OLD_PREFIX {
return
}
let old_pallet_prefix = twox_128(OLD_PREFIX.as_bytes());
let old_historical_sessions_key =
[&old_pallet_prefix, &twox_128(storage_prefix_historical_sessions)[..]].concat();
let old_historical_sessions_key_iter = frame_support::storage::KeyPrefixIterator::new(
old_historical_sessions_key.to_vec(),
old_historical_sessions_key.to_vec(),
|_| Ok(()),
);
assert_eq!(old_historical_sessions_key_iter.count(), 0);
let old_stored_range_key =
[&old_pallet_prefix, &twox_128(storage_prefix_stored_range)[..]].concat();
let old_stored_range_key_iter = frame_support::storage::KeyPrefixIterator::new(
old_stored_range_key.to_vec(),
old_stored_range_key.to_vec(),
|_| Ok(()),
);
assert_eq!(old_stored_range_key_iter.count(), 0);
let new_pallet_prefix = twox_128(new_pallet_name.as_bytes());
let new_pallet_prefix_iter = frame_support::storage::KeyPrefixIterator::new(
new_pallet_prefix.to_vec(),
new_pallet_prefix.to_vec(),
|_| Ok(()),
);
assert!(new_pallet_prefix_iter.count() >= 1);
assert_eq!(<P as GetStorageVersion>::on_chain_storage_version(), 1);
}
fn log_migration(stage: &str, storage_prefix: &[u8], old_pallet_name: &str, new_pallet_name: &str) {
log::info!(
target: "runtime::session_historical",
"{} prefix of storage '{}': '{}' ==> '{}'",
stage,
str::from_utf8(storage_prefix).unwrap_or("<Invalid UTF8>"),
old_pallet_name,
new_pallet_name,
);
}