frame-system: Add last_runtime_upgrade_spec_version (#2351)

Adds a function for querying the last runtime upgrade spec version. This
can be useful for when writing runtime level migrations to ensure that
they are not executed multiple times. An example would be a session key
migration.

---------

Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
Bastian Köcher
2023-11-15 23:54:08 +01:00
committed by GitHub
parent f4bb17cc86
commit ea4085ab74
5 changed files with 71 additions and 27 deletions
+19
View File
@@ -1094,6 +1094,25 @@ pub enum DecRefStatus {
}
impl<T: Config> Pallet<T> {
/// Returns the `spec_version` of the last runtime upgrade.
///
/// This function is useful for writing guarded runtime migrations in the runtime. A runtime
/// migration can use the `spec_version` to ensure that it isn't applied twice. This works
/// similar as the storage version for pallets.
///
/// This functions returns the `spec_version` of the last runtime upgrade while executing the
/// runtime migrations
/// [`on_runtime_upgrade`](frame_support::traits::OnRuntimeUpgrade::on_runtime_upgrade)
/// function. After all migrations are executed, this will return the `spec_version` of the
/// current runtime until there is another runtime upgrade.
///
/// Example:
#[doc = docify::embed!("src/tests.rs", last_runtime_upgrade_spec_version_usage)]
pub fn last_runtime_upgrade_spec_version() -> u32 {
LastRuntimeUpgrade::<T>::get().map_or(0, |l| l.spec_version.0)
}
/// Returns true if the given account exists.
pub fn account_exists(who: &T::AccountId) -> bool {
Account::<T>::contains_key(who)
}