mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-16 14:21:10 +00:00
Implements pallet versioning (#7208)
* Start * Make macro work * Rename `ModuleToIndex` to `PalletRuntimeSetup` Besides the renaming it also adds support getting the name of a pallet as configured in the runtime. * Rename it to `PalletInfo` * Remove accidentally added files * Some work * Make everything compile * Adds a test and fixes some bugs * Implement ordering for `PalletVersion` * Apply suggestions from code review * Review feedback * Update frame/support/src/dispatch.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Update frame/support/src/dispatch.rs Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com> * Fix compilation * Fix test * Fix doc test Co-authored-by: Alexander Popiak <alexander.popiak@parity.io> Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
@@ -1709,7 +1709,7 @@ impl<T> IsType<T> for T {
|
||||
/// "InstanceNMyModule".
|
||||
pub trait Instance: 'static {
|
||||
/// Unique module prefix. E.g. "InstanceNMyModule" or "MyModule"
|
||||
const PREFIX: &'static str ;
|
||||
const PREFIX: &'static str;
|
||||
}
|
||||
|
||||
/// A trait similar to `Convert` to convert values from `B` an abstract balance type
|
||||
@@ -1826,6 +1826,96 @@ pub trait IsSubType<T> {
|
||||
fn is_sub_type(&self) -> Option<&T>;
|
||||
}
|
||||
|
||||
/// The storage key postfix that is used to store the [`PalletVersion`] per pallet.
|
||||
///
|
||||
/// The full storage key is built by using:
|
||||
/// Twox128([`PalletInfo::name`]) ++ Twox128([`PALLET_VERSION_STORAGE_KEY_POSTFIX`])
|
||||
pub const PALLET_VERSION_STORAGE_KEY_POSTFIX: &[u8] = b":__PALLET_VERSION__:";
|
||||
|
||||
/// The version of a pallet.
|
||||
///
|
||||
/// 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)]
|
||||
pub struct PalletVersion {
|
||||
/// The major version of the pallet.
|
||||
pub major: u16,
|
||||
/// The minor version of the pallet.
|
||||
pub minor: u8,
|
||||
/// The patch version of the pallet.
|
||||
pub patch: u8,
|
||||
}
|
||||
|
||||
impl PalletVersion {
|
||||
/// Creates a new instance of `Self`.
|
||||
pub fn new(major: u16, minor: u8, patch: u8) -> Self {
|
||||
Self {
|
||||
major,
|
||||
minor,
|
||||
patch,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the storage key for a pallet version.
|
||||
///
|
||||
/// See [`PALLET_VERSION_STORAGE_KEY_POSTIFX`] on how this key is built.
|
||||
///
|
||||
/// Returns `None` if the given `PI` returned a `None` as name for the given
|
||||
/// `Pallet`.
|
||||
pub fn storage_key<PI: PalletInfo, Pallet: 'static>() -> Option<[u8; 32]> {
|
||||
let pallet_name = PI::name::<Pallet>()?;
|
||||
|
||||
let pallet_name = sp_io::hashing::twox_128(pallet_name.as_bytes());
|
||||
let postfix = sp_io::hashing::twox_128(PALLET_VERSION_STORAGE_KEY_POSTFIX);
|
||||
|
||||
let mut final_key = [0u8; 32];
|
||||
final_key[..16].copy_from_slice(&pallet_name);
|
||||
final_key[16..].copy_from_slice(&postfix);
|
||||
|
||||
Some(final_key)
|
||||
}
|
||||
}
|
||||
|
||||
impl sp_std::cmp::PartialOrd for PalletVersion {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<sp_std::cmp::Ordering> {
|
||||
let res = self.major
|
||||
.cmp(&other.major)
|
||||
.then_with(||
|
||||
self.minor
|
||||
.cmp(&other.minor)
|
||||
.then_with(|| self.patch.cmp(&other.patch)
|
||||
));
|
||||
|
||||
Some(res)
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides version information about a pallet.
|
||||
///
|
||||
/// This trait provides two functions for returning the version of a
|
||||
/// pallet. There is a state where both functions can return distinct versions.
|
||||
/// See [`GetPalletVersion::storage_version`] for more information about this.
|
||||
pub trait GetPalletVersion {
|
||||
/// Returns the current version of the pallet.
|
||||
fn current_version() -> PalletVersion;
|
||||
|
||||
/// Returns the version of the pallet that is stored in storage.
|
||||
///
|
||||
/// Most of the time this will return the exact same version as
|
||||
/// [`GetPalletVersion::current_version`]. Only when being in
|
||||
/// a state after a runtime upgrade happened and the pallet did
|
||||
/// not yet updated its version in storage, this will return a
|
||||
/// different(the previous, seen from the time of calling) version.
|
||||
///
|
||||
/// See [`PalletVersion`] for more information.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// If there was no previous version of the pallet stored in the state,
|
||||
/// this function returns `None`.
|
||||
fn storage_version() -> Option<PalletVersion>;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -1847,4 +1937,18 @@ mod tests {
|
||||
assert_eq!(<(Test, Test)>::on_initialize(0), 20);
|
||||
assert_eq!(<(Test, Test)>::on_runtime_upgrade(), 40);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_pallet_version_ordering() {
|
||||
let version = PalletVersion::new(1, 0, 0);
|
||||
assert!(version > PalletVersion::new(0, 1, 2));
|
||||
assert!(version == PalletVersion::new(1, 0, 0));
|
||||
assert!(version < PalletVersion::new(1, 0, 1));
|
||||
assert!(version < PalletVersion::new(1, 1, 0));
|
||||
|
||||
let version = PalletVersion::new(2, 50, 50);
|
||||
assert!(version < PalletVersion::new(2, 50, 51));
|
||||
assert!(version > PalletVersion::new(2, 49, 51));
|
||||
assert!(version < PalletVersion::new(3, 49, 51));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user