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
+14 -10
View File
@@ -1325,11 +1325,8 @@ macro_rules! decl_module {
$crate::sp_tracing::enter_span!($crate::sp_tracing::trace_span!("on_runtime_upgrade"));
let result: $return = (|| { $( $impl )* })();
let key = $crate::traits::PalletVersion::storage_key::<
<$trait_instance as $system::Trait>::PalletInfo, Self
>().expect("Every active pallet has a name in the runtime; qed");
let version = $crate::crate_to_pallet_version!();
$crate::storage::unhashed::put(&key, &version);
$crate::crate_to_pallet_version!()
.put_into_storage::<<$trait_instance as $system::Trait>::PalletInfo, Self>();
let additional_write = <
<$trait_instance as $system::Trait>::DbWeight as $crate::traits::Get<_>
@@ -1352,11 +1349,8 @@ macro_rules! decl_module {
fn on_runtime_upgrade() -> $crate::dispatch::Weight {
$crate::sp_tracing::enter_span!($crate::sp_tracing::trace_span!("on_runtime_upgrade"));
let key = $crate::traits::PalletVersion::storage_key::<
<$trait_instance as $system::Trait>::PalletInfo, Self
>().expect("Every active pallet has a name in the runtime; qed");
let version = $crate::crate_to_pallet_version!();
$crate::storage::unhashed::put(&key, &version);
$crate::crate_to_pallet_version!()
.put_into_storage::<<$trait_instance as $system::Trait>::PalletInfo, Self>();
<
<$trait_instance as $system::Trait>::DbWeight as $crate::traits::Get<_>
@@ -1837,6 +1831,16 @@ macro_rules! decl_module {
}
}
// Implement `OnGenesis` for `Module`
impl<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?> $crate::traits::OnGenesis
for $mod_type<$trait_instance $(, $instance)?> where $( $other_where_bounds )*
{
fn on_genesis() {
$crate::crate_to_pallet_version!()
.put_into_storage::<<$trait_instance as $system::Trait>::PalletInfo, Self>();
}
}
// manual implementation of clone/eq/partialeq because using derive erroneously requires
// clone/eq/partialeq from T.
impl<$trait_instance: $trait_name $(<I>, $instance: $instantiable)?> $crate::dispatch::Clone