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

Downgrade class #13912

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions frame/utility/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,9 @@ benchmarks! {
assert_last_event::<T>(Event::BatchCompleted.into())
}

downgrade_class {
let call = Box::new(frame_system::Call::set_code_without_checks { code: vec![] }.into());
}: _(RawOrigin::Root, call)

impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
}
23 changes: 23 additions & 0 deletions frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,29 @@ pub mod pallet {
let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into());
res.map(|_| ()).map_err(|e| e.error)
}

/// Dispatch a function call with normal class.
///
/// This function sets the dispatch class of the call to normal by force.
///
/// The dispatch origin for this call must be _Root_.
#[pallet::call_index(6)]
#[pallet::weight({
let dispatch_info = call.get_dispatch_info();
(
T::WeightInfo::downgrade_class()
.saturating_add(dispatch_info.weight),
DispatchClass::Normal
)
})]
pub fn downgrade_class(
origin: OriginFor<T>,
call: Box<<T as Config>::RuntimeCall>,
) -> DispatchResult {
ensure_root(origin)?;
let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into());
res.map(|_| ()).map_err(|e| e.error)
}
}
}

Expand Down
21 changes: 20 additions & 1 deletion frame/utility/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::*;
use crate as utility;
use frame_support::{
assert_err_ignore_postinfo, assert_noop, assert_ok,
dispatch::{DispatchError, DispatchErrorWithPostInfo, Dispatchable, Pays},
dispatch::{DispatchClass, DispatchError, DispatchErrorWithPostInfo, Dispatchable, Pays},
error::BadOrigin,
parameter_types, storage,
traits::{ConstU32, ConstU64, Contains, GenesisBuild},
Expand Down Expand Up @@ -937,3 +937,22 @@ fn with_weight_works() {
);
})
}

#[test]
fn downgrade_class_works() {
new_test_ext().execute_with(|| {
let upgrade_code_call =
Box::new(RuntimeCall::System(frame_system::Call::set_code_without_checks {
code: vec![],
}));
// Class before is `Operational`.
assert_eq!(upgrade_code_call.get_dispatch_info().class, DispatchClass::Operational);

let with_weight_call = Call::<Test>::downgrade_class { call: upgrade_code_call };
// Class after is `Normal`.
assert_eq!(
with_weight_call.get_dispatch_info().class,
frame_support::dispatch::DispatchClass::Normal
);
})
}
15 changes: 15 additions & 0 deletions frame/utility/src/weights.rs

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