Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
ggwpez committed Jan 26, 2023
1 parent 52665c6 commit cc7e3ff
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion frame/support/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub use randomness::Randomness;

mod metadata;
pub use metadata::{
GetStorageVersions, CallMetadata, CrateVersion, GetCallMetadata, GetCallName, GetStorageVersion, PalletInfo,
GetStorageVersions, CallMetadata, CrateVersion, GetCallMetadata, GetCallName, GetStorageVersion, PalletInfo, CheckAllVersions,
PalletInfoAccess, PalletInfoData, PalletsInfoAccess, StorageVersion,
STORAGE_VERSION_STORAGE_KEY_POSTFIX,
};
Expand Down
70 changes: 70 additions & 0 deletions frame/support/src/traits/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ impl StorageVersion {
}
}

impl core::fmt::Display for StorageVersion {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}

impl PartialEq<u16> for StorageVersion {
fn eq(&self, other: &u16) -> bool {
self.0 == *other
Expand Down Expand Up @@ -276,6 +282,70 @@ impl GetStorageVersions for Tuple {
}
}

use sp_weights::Weight;
use crate::traits::OnRuntimeUpgrade;

/// Put this as last into your Migrations with `AllPalletsWithSystem` as `T`.
pub struct CheckAllVersions<T>(sp_std::marker::PhantomData<T>);
impl<T: PalletsInfoAccess + GetStorageVersions> OnRuntimeUpgrade for CheckAllVersions<T> {
#[cfg(feature = "try-runtime")]
fn on_runtime_upgrade() -> Weight {
Weight::zero() // Check done by post_upgrade.
}

#[cfg(not(feature = "try-runtime"))]
fn on_runtime_upgrade() -> Weight {
if let Err(err) = Self::check() {
// Just log it. If you see this in production, then it is too late anyway.
log::error!(
target: "CheckPalletVersions",
"{:?}",
err
);
// TODO weight
};
Weight::zero()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
Self::check()
}
}

impl<T: PalletsInfoAccess + GetStorageVersions> CheckAllVersions<T> {
fn check() -> Result<(), &'static str> {
let pallets = <T as PalletsInfoAccess>::infos();
let onchain = <T as GetStorageVersions>::on_chain_storage_versions();
let storage = <T as GetStorageVersions>::current_storage_versions();
let versions = onchain.iter().zip(storage.iter());

let mut failed = false;
for (pallet, (onchain, storage)) in pallets.iter().zip(versions) {
if onchain != storage {
log::error!(
target: "CheckPalletVersions",
"Chain and code version mismatch for pallet {}: {} != {}",
pallet.name,
onchain,
storage,
);
failed = true;
}
}
if failed {
Err("One or more pallets have incorrect versions, see log.\n\
This is really bad and means that there are unusable pallets on the chain.")
} else {
log::info!(
target: "CheckPalletVersions",
"All pallets have the correct chain version."
);
Ok(())
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit cc7e3ff

Please sign in to comment.