Repository: paritytech/substrate
File: frame/system/src/lib.rs (Rust)

Code before:
	/// Checks the given code if it is a valid runtime wasm blob by instantianting
	/// 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())
			}
		}

		Ok(())
	}
}

/// Event handler which registers a provider when created.
pub struct Provider<T>(PhantomData<T>);
impl<T: Config> HandleLifetime<T::AccountId> for Provider<T> {
	fn created(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::inc_providers(t);
		Ok(())
	}
	fn killed(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::dec_providers(t).map(|_| ())
	}
}

/// Event handler which registers a self-sufficient when created.
pub struct SelfSufficient<T>(PhantomData<T>);
impl<T: Config> HandleLifetime<T::AccountId> for SelfSufficient<T> {
	fn created(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::inc_sufficients(t);
		Ok(())
	}
	fn killed(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::dec_sufficients(t);

Review comment (bug):
Could we rewrite this entire function to:

```
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())
           }
      }
}
```

Code after:
					core::hint::black_box((new_version, current_version));
					Ok(())
			  } else {
				  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(())
			  }
		}
	}
}

/// Event handler which registers a provider when created.
pub struct Provider<T>(PhantomData<T>);
impl<T: Config> HandleLifetime<T::AccountId> for Provider<T> {
	fn created(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::inc_providers(t);
		Ok(())
	}
	fn killed(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::dec_providers(t).map(|_| ())
	}
}

/// Event handler which registers a self-sufficient when created.
pub struct SelfSufficient<T>(PhantomData<T>);
impl<T: Config> HandleLifetime<T::AccountId> for SelfSufficient<T> {
	fn created(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::inc_sufficients(t);
		Ok(())
	}
	fn killed(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::dec_sufficients(t);
		Ok(())
	}
}

/// Event handler which registers a consumer when created.
pub struct Consumer<T>(PhantomData<T>);
impl<T: Config> HandleLifetime<T::AccountId> for Consumer<T> {
	fn created(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::inc_consumers(t)
	}
	fn killed(t: &T::AccountId) -> Result<(), DispatchError> {
		Pallet::<T>::dec_consumers(t);
		Ok(())