Skip to content

Commit

Permalink
Add benchmark for system::set_code intrinsic
Browse files Browse the repository at this point in the history
  • Loading branch information
hirschenberger committed Feb 13, 2023
1 parent 1da458a commit 88589c6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
3 changes: 0 additions & 3 deletions frame/system/benchmarking/runtimes/README.md

This file was deleted.

Binary file not shown.
12 changes: 10 additions & 2 deletions frame/system/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use codec::Encode;
use frame_benchmarking::v1::{benchmarks, whitelisted_caller};
use frame_support::{dispatch::DispatchClass, storage, traits::Get};
use frame_system::{Call, Pallet as System, RawOrigin};
use frame_system::{Call, EventRecord, Pallet as System, Phase, RawOrigin};
use sp_core::storage::well_known_keys;
use sp_runtime::traits::Hash;
use sp_std::{prelude::*, vec};
Expand All @@ -49,9 +49,17 @@ benchmarks! {
}: _(RawOrigin::Root, Default::default())

set_code {
let runtime_blob = include_bytes!("../runtimes/kitchensink_runtime.compact.compressed.wasm");
let runtime_blob = include_bytes!("../res/kitchensink_runtime.compact.compressed.wasm");
}: _(RawOrigin::Root, runtime_blob.to_vec())
verify {
assert_eq!(
System::<T>::events(),
vec![EventRecord {
phase: Phase::Initialization,
event: frame_system::Event::<T>::CodeUpdated.into(),
topics: vec![],
}],
);
}

#[extra]
Expand Down
21 changes: 13 additions & 8 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,10 @@ pub mod pallet {
/// - 1 digest item.
/// - 1 event.
/// The weight of this function is dependent on the runtime, but generally this is very
/// expensive. We will treat this as a full block.
/// expensive.
/// # </weight>
#[pallet::call_index(2)]
#[pallet::weight((T::SystemWeightInfo::set_code(code.len() as u32), DispatchClass::Operational))]
#[pallet::weight((T::SystemWeightInfo::set_code(), DispatchClass::Operational))]
pub fn set_code(origin: OriginFor<T>, code: Vec<u8>) -> DispatchResultWithPostInfo {
ensure_root(origin)?;
Self::can_set_code(&code)?;
Expand Down Expand Up @@ -1624,13 +1624,18 @@ impl<T: Config> Pallet<T> {
.and_then(|v| RuntimeVersion::decode(&mut &v[..]).ok())
.ok_or(Error::<T>::FailedToExtractRuntimeVersion)?;

if new_version.spec_name != current_version.spec_name {
log::debug!("New: {new_version:?}, Current: {current_version:?}");
return Err(Error::<T>::InvalidSpecName.into());
}
// 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());
if new_version.spec_version <= current_version.spec_version {
return Err(Error::<T>::SpecVersionNeedsToIncrease.into());
}
}

Ok(())
Expand Down
6 changes: 3 additions & 3 deletions frame/system/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn remark(b: u32, ) -> Weight;
fn remark_with_event(b: u32, ) -> Weight;
fn set_code(b: u32, ) -> Weight;
fn set_code() -> Weight;
fn set_heap_pages() -> Weight;
fn set_storage(i: u32, ) -> Weight;
fn kill_storage(i: u32, ) -> Weight;
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<T: crate::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(Weight::from_ref_time(631_438).saturating_mul(i.into()))
.saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(i.into())))
}
fn set_code(b: u32) -> Weight {
fn set_code() -> Weight {
Weight::from_ref_time(1_000_000)
}
/// Storage: Skipped Metadata (r:0 w:0)
Expand Down Expand Up @@ -160,7 +160,7 @@ impl WeightInfo for () {
// Standard Error: 0
.saturating_add(Weight::from_ref_time(1_423).saturating_mul(b.into()))
}
fn set_code(b: u32) -> Weight {
fn set_code() -> Weight {
Weight::from_ref_time(1_000_000)
}
/// Storage: System Digest (r:1 w:1)
Expand Down

0 comments on commit 88589c6

Please sign in to comment.