Move PalletVersion away from the crate version (#9165)

* Move `PalletVersion` away from the crate version

Before this pr, `PalletVersion` was referring to the crate version that
hosted the pallet. This pr introduces a custom `package.metadata.frame`
section in the `Cargo.toml` that can contain a `pallet-version` key
value pair. While the value is expected to be a valid u16. If this
key/value pair isn't given, the version is set to 1.

It also changes the `PalletVersion` declaration. We now only have one
`u16` that represents the version. Not a major/minor/patch version. As
the old `PalletVersion` was starting with the `u16` major, decoding the
old values will work.

* Overhaul the entire implementation

- Drop PalletVersion
- Introduce StorageVersion
- StorageVersion needs to be set in the crate and set for the macros
- Added migration

* Fix migrations

* Review feedback

* Remove unneeded dep

* remove pub consts

* Brings back logging and implements `GetStorageVersion`

* Return weight from migration

* Fmt and remove unused import

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/traits/metadata.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Bastian Köcher
2021-07-27 23:21:27 +02:00
committed by GitHub
parent 988c399983
commit 4fe55f0bcb
24 changed files with 550 additions and 657 deletions
@@ -19,7 +19,7 @@
use codec::{Decode, Encode, FullCodec};
use frame_support::{
traits::{GetPalletVersion, PalletVersion},
traits::{PalletInfoAccess, StorageVersion},
weights::Weight,
RuntimeDebug, Twox64Concat,
};
@@ -41,8 +41,8 @@ struct Voter<AccountId, Balance> {
/// Trait to implement to give information about types used for migration
pub trait V2ToV3 {
/// elections-phragmen module, used to check storage version.
type Module: GetPalletVersion;
/// The elections-phragmen pallet.
type Pallet: 'static + PalletInfoAccess;
/// System config account id
type AccountId: 'static + FullCodec;
@@ -67,7 +67,7 @@ frame_support::generate_storage_alias!(
>
);
/// Apply all of the migrations from 2_0_0 to 3_0_0.
/// Apply all of the migrations from 2 to 3.
///
/// ### Warning
///
@@ -77,28 +77,29 @@ frame_support::generate_storage_alias!(
/// Be aware that this migration is intended to be used only for the mentioned versions. Use
/// with care and run at your own risk.
pub fn apply<T: V2ToV3>(old_voter_bond: T::Balance, old_candidacy_bond: T::Balance) -> Weight {
let maybe_storage_version = <T::Module as GetPalletVersion>::storage_version();
let storage_version = StorageVersion::get::<T::Pallet>();
log::info!(
target: "runtime::elections-phragmen",
"Running migration for elections-phragmen with storage version {:?}",
maybe_storage_version,
storage_version,
);
match maybe_storage_version {
Some(storage_version) if storage_version <= PalletVersion::new(2, 0, 0) => {
migrate_voters_to_recorded_deposit::<T>(old_voter_bond);
migrate_candidates_to_recorded_deposit::<T>(old_candidacy_bond);
migrate_runners_up_to_recorded_deposit::<T>(old_candidacy_bond);
migrate_members_to_recorded_deposit::<T>(old_candidacy_bond);
Weight::max_value()
},
_ => {
log::warn!(
target: "runtime::elections-phragmen",
"Attempted to apply migration to V3 but failed because storage version is {:?}",
maybe_storage_version,
);
0
},
if storage_version <= 2 {
migrate_voters_to_recorded_deposit::<T>(old_voter_bond);
migrate_candidates_to_recorded_deposit::<T>(old_candidacy_bond);
migrate_runners_up_to_recorded_deposit::<T>(old_candidacy_bond);
migrate_members_to_recorded_deposit::<T>(old_candidacy_bond);
StorageVersion::new(3).put::<T::Pallet>();
Weight::max_value()
} else {
log::warn!(
target: "runtime::elections-phragmen",
"Attempted to apply migration to V3 but failed because storage version is {:?}",
storage_version,
);
0
}
}
@@ -18,7 +18,7 @@
//! Migrations to version [`4.0.0`], as denoted by the changelog.
use frame_support::{
traits::{Get, GetPalletVersion, PalletVersion},
traits::{Get, StorageVersion},
weights::Weight,
};
@@ -32,9 +32,7 @@ pub const OLD_PREFIX: &[u8] = b"PhragmenElection";
/// `<Runtime as frame_system::Config>::PalletInfo::name::<ElectionsPhragmenPallet>`.
///
/// The old storage prefix, `PhragmenElection` is hardcoded in the migration code.
pub fn migrate<T: frame_system::Config, P: GetPalletVersion, N: AsRef<str>>(
new_pallet_name: N,
) -> Weight {
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::elections-phragmen",
@@ -42,30 +40,30 @@ pub fn migrate<T: frame_system::Config, P: GetPalletVersion, N: AsRef<str>>(
);
return 0
}
let maybe_storage_version = <P as GetPalletVersion>::storage_version();
let storage_version = StorageVersion::get::<crate::Pallet<T>>();
log::info!(
target: "runtime::elections-phragmen",
"Running migration to v4 for elections-phragmen with storage version {:?}",
maybe_storage_version,
storage_version,
);
match maybe_storage_version {
Some(storage_version) if storage_version <= PalletVersion::new(3, 0, 0) => {
log::info!("new prefix: {}", new_pallet_name.as_ref());
frame_support::storage::migration::move_pallet(
OLD_PREFIX,
new_pallet_name.as_ref().as_bytes(),
);
<T as frame_system::Config>::BlockWeights::get().max_block
},
_ => {
log::warn!(
target: "runtime::elections-phragmen",
"Attempted to apply migration to v4 but failed because storage version is {:?}",
maybe_storage_version,
);
0
},
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 {
log::warn!(
target: "runtime::elections-phragmen",
"Attempted to apply migration to v4 but failed because storage version is {:?}",
storage_version,
);
0
}
}
@@ -73,7 +71,7 @@ pub fn migrate<T: frame_system::Config, P: GetPalletVersion, N: AsRef<str>>(
/// [`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: crate::Config, N: AsRef<str>>(new: N) {
let new = new.as_ref();
log::info!("pre-migration elections-phragmen test with new = {}", new);
@@ -94,15 +92,15 @@ pub fn pre_migration<P: GetPalletVersion, N: AsRef<str>>(new: N) {
sp_core::hexdisplay::HexDisplay::from(&sp_io::storage::next_key(new.as_bytes()).unwrap())
);
// ensure storage version is 3.
assert!(<P as GetPalletVersion>::storage_version().unwrap().major == 3);
assert_eq!(StorageVersion::get::<crate::Pallet<T>>(), 3);
}
/// Some checks for after migration. This can be linked to
/// [`frame_support::traits::OnRuntimeUpgrade::post_upgrade`] for further testing.
///
/// Panics if anything goes wrong.
pub fn post_migration<P: GetPalletVersion>() {
pub fn post_migration<T: crate::Config>() {
log::info!("post-migration elections-phragmen");
// 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_eq!(StorageVersion::get::<crate::Pallet<T>>(), 4);
}