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

Commit

Permalink
Rewrite set_code function
Browse files Browse the repository at this point in the history
  • Loading branch information
hirschenberger committed Feb 17, 2023
1 parent 6406281 commit c480343
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frame/system/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
cfg-if = "1.0"
codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = ["derive"] }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
Expand Down
53 changes: 30 additions & 23 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,13 @@ pub mod pallet {
///
/// ## Complexity
/// - `O(C)` where `C` length of `code`
/// - 1 storage write (codec `O(C)`).
/// - 1 digest item.
/// - 1 event.
/// The weight of this function is dependent on the runtime.
/// # </weight>
#[pallet::call_index(3)]
#[pallet::weight((T::BlockWeights::get().max_block, DispatchClass::Operational))]
#[pallet::weight((T::SystemWeightInfo::set_code(), DispatchClass::Operational))]
pub fn set_code_without_checks(
origin: OriginFor<T>,
code: Vec<u8>,
Expand Down Expand Up @@ -1612,29 +1617,31 @@ impl<T: Config> Pallet<T> {
/// it and extracting the runtime version of it. It checks that the runtime version
/// of the old and new runtime has the same spec name and that the spec version is increasing.
pub fn can_set_code(code: &[u8]) -> Result<(), sp_runtime::DispatchError> {
#[cfg_attr(feature = "runtime-benchmarks", allow(unused_variables))]
let (current_version, new_version) = (
T::Version::get(),
sp_io::misc::runtime_version(code)
.and_then(|v| RuntimeVersion::decode(&mut &v[..]).ok())
.ok_or(Error::<T>::FailedToExtractRuntimeVersion)?,
);

// Disable checks if we are benchmarking `set_code` to make it possible to set arbitrary
// runtimes without modification
#[cfg(not(feature = "runtime-benchmarks"))]
{
if new_version.spec_name != current_version.spec_name {
log::debug!("New: {new_version:?}, Current: {current_version:?}");
return Err(Error::<T>::InvalidSpecName.into())
}

if new_version.spec_version <= current_version.spec_version {
return Err(Error::<T>::SpecVersionNeedsToIncrease.into())
}
let current_version = T::Version::get();
let new_version = sp_io::misc::runtime_version(code)
.and_then(|v| RuntimeVersion::decode(&mut &v[..]).ok())
.ok_or(Error::<T>::FailedToExtractRuntimeVersion)?;

cfg_if::cfg_if! {
if #[cfg(not(feature = "runtime-benchmarks"))] {
if new_version.spec_name != current_version.spec_name {
return Err(Error::<T>::InvalidSpecName.into())
}

if new_version.spec_version <= current_version.spec_version {
return Err(Error::<T>::SpecVersionNeedsToIncrease.into())
}

Ok(())
} else {
// Let's ensure the compiler doesn't optimize our fetching of the runtime version away.
if new_version.spec_name != current_version.spec_name || new_version.spec_version != current_version.spec_version {
Ok(())
} else {
Err(Error::<T>::InvalidSpecName.into())
}
}
}

Ok(())
}
}

Expand Down

0 comments on commit c480343

Please sign in to comment.