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 12 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
2 changes: 1 addition & 1 deletion client/network/src/request_responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,7 @@ pub enum ResponseFailure {
/// Implements the libp2p [`RequestResponseCodec`] trait. Defines how streams of bytes are turned
/// into requests and responses and vice-versa.
#[derive(Debug, Clone)]
#[doc(hidden)]// Needs to be public in order to satisfy the Rust compiler.
#[doc(hidden)] // Needs to be public in order to satisfy the Rust compiler.
pub struct GenericCodec {
max_request_size: u64,
max_response_size: u64,
Expand Down
6 changes: 5 additions & 1 deletion frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ use crate::{
};
use frame_support::{
dispatch::Dispatchable,
traits::{Currency, Filter, Get, OnUnbalanced, Randomness, Time},
traits::{Currency, Filter, Get, OnUnbalanced, Randomness, StorageVersion, Time},
weights::{GetDispatchInfo, PostDispatchInfo, Weight, WithPostDispatchInfo},
};
use frame_system::Pallet as System;
Expand All @@ -134,6 +134,9 @@ type NegativeImbalanceOf<T> = <<T as Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
>>::NegativeImbalance;

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

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -273,6 +276,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
17 changes: 6 additions & 11 deletions frame/contracts/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,17 @@
use crate::{Config, Pallet, Weight};
use frame_support::{
storage::migration,
traits::{Get, GetPalletVersion, PalletInfoAccess, PalletVersion},
traits::{Get, PalletInfoAccess, StorageVersion},
};

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
6 changes: 5 additions & 1 deletion frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ use frame_support::{
traits::{
ChangeMembers, Contains, ContainsLengthBound, Currency, CurrencyToVote, Get,
InitializeMembers, LockIdentifier, LockableCurrency, OnUnbalanced, ReservableCurrency,
SortedMembers, WithdrawReasons,
SortedMembers, StorageVersion, WithdrawReasons,
},
weights::Weight,
};
Expand All @@ -122,6 +122,9 @@ pub use weights::WeightInfo;
/// All migrations.
pub mod migrations;

/// The current storage version.
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
45 changes: 23 additions & 22 deletions frame/elections-phragmen/src/migrations/v3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use codec::{Decode, Encode, FullCodec};
use frame_support::{
traits::{GetPalletVersion, PalletVersion},
traits::{PalletInfoAccess, StorageVersion},
weights::Weight,
RuntimeDebug, Twox64Concat,
};
Expand All @@ -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;
Expand All @@ -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
///
Expand All @@ -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
}
}

Expand Down
52 changes: 25 additions & 27 deletions frame/elections-phragmen/src/migrations/v4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -32,48 +32,46 @@ 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",
"New pallet name is equal to the old prefix. No migration needs to be done.",
);
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 @@ -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);
}
6 changes: 5 additions & 1 deletion frame/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use fg_primitives::{
use frame_support::{
dispatch::DispatchResultWithPostInfo,
storage,
traits::{KeyOwnerProofSystem, OneSessionHandler},
traits::{KeyOwnerProofSystem, OneSessionHandler, StorageVersion},
weights::{Pays, Weight},
};
use sp_runtime::{generic::DigestItem, traits::Zero, DispatchResult, KeyTypeId};
Expand All @@ -67,6 +67,9 @@ pub use equivocation::{

pub use pallet::*;

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

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand All @@ -75,6 +78,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