Make sure pallet versions are set at genesis (#7451)

* Make sure pallet versions are set at genesis

This pr ensures that pallet versions are also set at genesis. It does
this by hooking into the runtime `GenesisConfig` which means that it
will only work when the storage is setup using this genesis config. So,
the version will not be set in pallet local tests. However, I think this
isn't such a problem. The genesis config will call `on_genesis` on all
pallets. This function comes from the new trait `OnGenesis`. Currently
the user is not able to provide any custom implementation of this trait.

Besides that it also implements `Clone` and `Copy` for the pallet
version struct.

This pr also moves the macro for generating the runtime genesis config
to `frame-support` as most of the other FRAME related macros.

* Reduce line width

* Update frame/support/src/traits.rs

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
This commit is contained in:
Bastian Köcher
2020-10-29 20:20:08 +01:00
committed by GitHub
parent a5ec7e5c4e
commit e1b56f8dd3
7 changed files with 218 additions and 129 deletions
+38 -5
View File
@@ -1421,16 +1421,19 @@ pub trait GetCallMetadata {
fn get_call_metadata(&self) -> CallMetadata;
}
/// The block finalization trait. Implementing this lets you express what should happen
/// for your module when the block is ending.
/// The block finalization trait.
///
/// Implementing this lets you express what should happen for your pallet when the block is ending.
#[impl_for_tuples(30)]
pub trait OnFinalize<BlockNumber> {
/// The block is being finalized. Implement to have something happen.
fn on_finalize(_n: BlockNumber) {}
}
/// The block initialization trait. Implementing this lets you express what should happen
/// for your module when the block is beginning (right before the first extrinsic is executed).
/// The block initialization trait.
///
/// Implementing this lets you express what should happen for your pallet when the block is
/// beginning (right before the first extrinsic is executed).
pub trait OnInitialize<BlockNumber> {
/// The block is being initialized. Implement to have something happen.
///
@@ -1447,6 +1450,17 @@ impl<BlockNumber: Clone> OnInitialize<BlockNumber> for Tuple {
}
}
/// A trait that will be called at genesis.
///
/// Implementing this trait for a pallet let's you express operations that should
/// happen at genesis. It will be called in an externalities provided environment and
/// will see the genesis state after all pallets have written their genesis state.
#[impl_for_tuples(30)]
pub trait OnGenesis {
/// Something that should happen at genesis.
fn on_genesis() {}
}
/// The runtime upgrade trait.
///
/// Implementing this lets you express what should happen when the runtime upgrades,
@@ -1834,7 +1848,7 @@ pub const PALLET_VERSION_STORAGE_KEY_POSTFIX: &[u8] = b":__PALLET_VERSION__:";
///
/// Each pallet version is stored in the state under a fixed key. See
/// [`PALLET_VERSION_STORAGE_KEY_POSTFIX`] for how this key is built.
#[derive(RuntimeDebug, Eq, PartialEq, Encode, Decode, Ord)]
#[derive(RuntimeDebug, Eq, PartialEq, Encode, Decode, Ord, Clone, Copy)]
pub struct PalletVersion {
/// The major version of the pallet.
pub major: u16,
@@ -1872,6 +1886,25 @@ impl PalletVersion {
Some(final_key)
}
/// Put this pallet version into the storage.
///
/// It will use the storage key that is associated with the given `Pallet`.
///
/// # Panics
///
/// This function will panic iff `Pallet` can not be found by `PalletInfo`.
/// In a runtime that is put together using
/// [`construct_runtime!`](crate::construct_runtime) this should never happen.
///
/// It will also panic if this function isn't executed in an externalities
/// provided environment.
pub fn put_into_storage<PI: PalletInfo, Pallet: 'static>(&self) {
let key = Self::storage_key::<PI, Pallet>()
.expect("Every active pallet has a name in the runtime; qed");
crate::storage::unhashed::put(&key, self);
}
}
impl sp_std::cmp::PartialOrd for PalletVersion {