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
+5 -1
View File
@@ -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};
@@ -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::*;
@@ -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]
+2 -2
View File
@@ -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;
@@ -16,7 +16,7 @@
// limitations under the License.
use frame_support::{
traits::{Get, GetPalletVersion, PalletVersion},
traits::{Get, StorageVersion},
weights::Weight,
};
use sp_io::hashing::twox_128;
@@ -31,9 +31,7 @@ pub const OLD_PREFIX: &[u8] = b"GrandpaFinality";
/// `<Runtime as frame_system::Config>::PalletInfo::name::<GrandpaPallet>`.
///
/// The old storage prefix, `GrandpaFinality` 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::afg",
@@ -41,30 +39,25 @@ 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::afg",
"Running migration to v3.1 for grandpa 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::afg",
"Attempted to apply migration to v3.1 but cancelled 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 {
0
}
}
@@ -72,9 +65,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<T: frame_system::Config, P: GetPalletVersion + 'static, 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 grandpa test with new = {}", new);
@@ -83,7 +74,7 @@ pub fn pre_migration<T: frame_system::Config, P: GetPalletVersion + 'static, N:
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();
let storage_key = StorageVersion::storage_key::<crate::Pallet<T>>();
// ensure nothing is stored in the new prefix.
assert!(
@@ -103,14 +94,14 @@ pub fn pre_migration<T: frame_system::Config, P: GetPalletVersion + 'static, N:
),
);
// 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() {
log::info!("post-migration grandpa");
// Assert that nothing remains at the old prefix