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
}
}