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

Migrate pallet-grandpa to attribute macros #8724

Merged
18 commits merged into from
May 26, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
frame/grandpa: address review comments
octol committed May 12, 2021
commit 1c8e18a1ce66048142af5b5ddf82997791ac9e3c
37 changes: 28 additions & 9 deletions frame/grandpa/src/migrations/v4.rs
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ use frame_support::{
weights::Weight,
traits::{GetPalletVersion, PalletVersion, Get},
};
use sp_io::hashing::twox_128;

/// The old prefix.
pub const OLD_PREFIX: &[u8] = b"GrandpaFinality";
@@ -74,25 +75,37 @@ pub fn migrate<
/// [`frame_support::traits::OnRuntimeUpgrade::pre_upgrade`] for further testing.
///
/// Panics if anything goes wrong.
pub fn pre_migration<P: GetPalletVersion, N: AsRef<str>>(new: N) {
pub fn pre_migration<
T: frame_system::Config,
P: GetPalletVersion + 'static,
N: AsRef<str>,
>(new: N) {
let new = new.as_ref();
log::info!("pre-migration grandpa test with new = {}", new);

// the next key must exist, and start with the hash of `OLD_PREFIX`.
let next_key = sp_io::storage::next_key(OLD_PREFIX).unwrap();
assert!(next_key.starts_with(&sp_io::hashing::twox_128(OLD_PREFIX)));
let next_key = sp_io::storage::next_key(&twox_128(OLD_PREFIX)).unwrap();
assert!(next_key.starts_with(&twox_128(OLD_PREFIX)));

// The pallet version is already stored using the pallet name
let storage_key = PalletVersion::storage_key::<T::PalletInfo, P>().unwrap();

// ensure nothing is stored in the new prefix.
assert!(
sp_io::storage::next_key(new.as_bytes()).map_or(
sp_io::storage::next_key(&twox_128(new.as_bytes())).map_or(
// either nothing is there
true,
// or we ensure that it has no common prefix with twox_128(new).
|next_key| !next_key.starts_with(&sp_io::hashing::twox_128(new.as_bytes()))
// or we ensure that it has no common prefix with twox_128(new),
// or isn't the pallet version that is already stored using the pallet name
|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(new.as_bytes()).unwrap())
sp_core::hexdisplay::HexDisplay::from(
&sp_io::storage::next_key(&twox_128(new.as_bytes())).unwrap()
),
);
// ensure storage version is 3.
assert!(<P as GetPalletVersion>::storage_version().unwrap().major == 3);
@@ -104,6 +117,12 @@ pub fn pre_migration<P: GetPalletVersion, N: AsRef<str>>(new: N) {
/// Panics if anything goes wrong.
pub fn post_migration<P: GetPalletVersion>() {
log::info!("post-migration grandpa");
// ensure we've been updated to v4 by the automatic write of crate version -> storage version.
assert!(<P as GetPalletVersion>::storage_version().unwrap().major == 4);

// Assert that nothing remains at the old prefix
assert!(
sp_io::storage::next_key(&twox_128(OLD_PREFIX)).map_or(
true,
|next_key| !next_key.starts_with(&twox_128(OLD_PREFIX))
)
);
}