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
use frame_support::{
traits::{Get, StorageVersion},
weights::Weight,
};
use sp_io::hashing::twox_128;
pub const OLD_PREFIX: &[u8] = b"GrandpaFinality";
pub fn migrate<T: crate::Config, N: AsRef<str>>(new_pallet_name: N) -> Weight {
if new_pallet_name.as_ref().as_bytes() == OLD_PREFIX {
log::info!(
target: "runtime::afg",
"New pallet name is equal to the old prefix. No migration needs to be done.",
);
return 0
}
let storage_version = StorageVersion::get::<crate::Pallet<T>>();
log::info!(
target: "runtime::afg",
"Running migration to v3.1 for grandpa with storage version {:?}",
storage_version,
);
if storage_version <= 3 {
log::info!("new prefix: {}", new_pallet_name.as_ref());
frame_support::storage::migration::move_pallet(
OLD_PREFIX,
new_pallet_name.as_ref().as_bytes(),
);
StorageVersion::new(4).put::<crate::Pallet<T>>();
<T as frame_system::Config>::BlockWeights::get().max_block
} else {
0
}
}
pub fn pre_migration<T: crate::Config, N: AsRef<str>>(new: N) {
let new = new.as_ref();
log::info!("pre-migration grandpa test with new = {}", new);
let next_key = sp_io::storage::next_key(&twox_128(OLD_PREFIX)).unwrap();
assert!(next_key.starts_with(&twox_128(OLD_PREFIX)));
let storage_key = StorageVersion::storage_key::<crate::Pallet<T>>();
assert!(
sp_io::storage::next_key(&twox_128(new.as_bytes())).map_or(
true,
|next_key| {
!next_key.starts_with(&twox_128(new.as_bytes())) || next_key == storage_key
},
),
"unexpected next_key({}) = {:?}",
new,
sp_core::hexdisplay::HexDisplay::from(
&sp_io::storage::next_key(&twox_128(new.as_bytes())).unwrap()
),
);
assert_eq!(StorageVersion::get::<crate::Pallet<T>>(), 3);
}
pub fn post_migration() {
log::info!("post-migration grandpa");
assert!(sp_io::storage::next_key(&twox_128(OLD_PREFIX))
.map_or(true, |next_key| !next_key.starts_with(&twox_128(OLD_PREFIX))));
}