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

Move PalletVersion away from the crate version #9165

Merged
15 commits merged into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ use sp_runtime::{
Perbill,
};
use frame_support::{
traits::{OnUnbalanced, Currency, Get, Time, Randomness},
traits::{OnUnbalanced, Currency, Get, Time, Randomness, StorageVersion},
weights::{Weight, PostDispatchInfo, WithPostDispatchInfo},
};
use frame_system::Pallet as System;
Expand All @@ -134,6 +134,9 @@ type BalanceOf<T> =
type NegativeImbalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::NegativeImbalance;

/// The current storage version.
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);
bkchr marked this conversation as resolved.
Show resolved Hide resolved

#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
Expand Down Expand Up @@ -239,6 +242,7 @@ pub mod pallet {
}

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

an alternative syntax could be a new item:

#[pallet::storage_version]
const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);

But the current syntax is good to me

pub struct Pallet<T>(PhantomData<T>);

#[pallet::hooks]
Expand Down
24 changes: 10 additions & 14 deletions frame/contracts/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,20 @@
// limitations under the License.

use crate::{Config, Weight, Pallet};
use frame_support::{
storage::migration,
traits::{GetPalletVersion, PalletVersion, PalletInfoAccess, Get},
};
use frame_support::{storage::migration, traits::{StorageVersion, PalletInfoAccess, Get}};

pub fn migrate<T: Config>() -> Weight {
let mut weight: Weight = 0;

match <Pallet<T>>::storage_version() {
Some(version) if version == PalletVersion::new(3, 0, 0) => {
weight = weight.saturating_add(T::DbWeight::get().writes(1));
migration::remove_storage_prefix(
<Pallet<T>>::name().as_bytes(),
b"CurrentSchedule",
b"",
);
}
_ => (),
if StorageVersion::get::<Pallet<T>>() == 3 {
weight = weight.saturating_add(T::DbWeight::get().writes(1));
migration::remove_storage_prefix(
<Pallet<T>>::name().as_bytes(),
b"CurrentSchedule",
b"",
);

StorageVersion::new(4).put::<Pallet<T>>();
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
}

weight
Expand Down
8 changes: 6 additions & 2 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@

use codec::{Decode, Encode};
use frame_support::{
dispatch::{WithPostDispatchInfo},
dispatch::WithPostDispatchInfo,
traits::{
ChangeMembers, Contains, ContainsLengthBound, Currency, CurrencyToVote, Get,
InitializeMembers, LockIdentifier, LockableCurrency, OnUnbalanced, ReservableCurrency,
WithdrawReasons, SortedMembers,
WithdrawReasons, SortedMembers, StorageVersion,
},
weights::Weight,
};
Expand All @@ -122,6 +122,9 @@ pub use weights::WeightInfo;
/// All migrations.
pub mod migrations;

/// The current storage version.
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);

/// The maximum votes allowed per voter.
pub const MAXIMUM_VOTE: usize = 16;

Expand Down Expand Up @@ -239,6 +242,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(PhantomData<T>);

#[pallet::hooks]
Expand Down
46 changes: 23 additions & 23 deletions frame/elections-phragmen/src/migrations/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
use codec::{Encode, Decode, FullCodec};
use sp_std::prelude::*;
use frame_support::{
RuntimeDebug, weights::Weight, Twox64Concat,
traits::{GetPalletVersion, PalletVersion},
RuntimeDebug, Twox64Concat, traits::{PalletInfoAccess, StorageVersion}, weights::Weight,
};

#[derive(Encode, Decode, Clone, Default, RuntimeDebug, PartialEq)]
Expand All @@ -40,8 +39,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;
Expand All @@ -66,7 +65,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
///
Expand All @@ -76,28 +75,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
}
}

Expand Down
54 changes: 25 additions & 29 deletions frame/elections-phragmen/src/migrations/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@

//! Migrations to version [`4.0.0`], as denoted by the changelog.

use frame_support::{
weights::Weight,
traits::{GetPalletVersion, PalletVersion, Get},
};
use frame_support::{weights::Weight, traits::{StorageVersion, Get}};

/// The old prefix.
pub const OLD_PREFIX: &[u8] = b"PhragmenElection";
Expand All @@ -33,8 +30,7 @@ pub const OLD_PREFIX: &[u8] = b"PhragmenElection";
///
/// The old storage prefix, `PhragmenElection` is hardcoded in the migration code.
pub fn migrate<
T: frame_system::Config,
P: GetPalletVersion,
T: crate::Config,
N: AsRef<str>,
>(new_pallet_name: N) -> Weight {
if new_pallet_name.as_ref().as_bytes() == OLD_PREFIX {
Expand All @@ -44,38 +40,38 @@ pub fn migrate<
);
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
}
}

/// Some checks prior to migration. This can be linked to
/// [`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);

Expand All @@ -96,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);
}
7 changes: 6 additions & 1 deletion frame/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ use fg_primitives::{
};
use frame_support::{
dispatch::DispatchResultWithPostInfo,
storage, traits::{OneSessionHandler, KeyOwnerProofSystem}, weights::{Pays, Weight},
storage, traits::{OneSessionHandler, KeyOwnerProofSystem, StorageVersion},
weights::{Pays, Weight},
};
use sp_runtime::{
generic::DigestItem,
Expand Down Expand Up @@ -69,6 +70,9 @@ pub use equivocation::{

pub use pallet::*;

/// The current storage version.
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);

#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
Expand All @@ -77,6 +81,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::config]
Expand Down
4 changes: 2 additions & 2 deletions frame/grandpa/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
// See the License for the specific language governing permissions and
// limitations under the License.

/// Version 3.1.
pub mod v3_1;
/// Version 4.
pub mod v4;
Loading