From 36fa2751d782e97c2ebd7d54e1b26f3a3ab22043 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 5 Dec 2022 13:38:24 -0400 Subject: [PATCH 1/2] add with weight extrinsic --- frame/utility/src/lib.rs | 17 +++++++++++++++++ frame/utility/src/tests.rs | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index 41710be930b90..00cb18e1b23aa 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -474,6 +474,23 @@ pub mod pallet { let base_weight = T::WeightInfo::batch(calls_len as u32); Ok(Some(base_weight.saturating_add(weight)).into()) } + + /// Dispatch a function call with a specified weight. + /// + /// This function does not check the weight of the call, and instead allows the + /// Root origin to specify the weight of the call. + /// + /// The dispatch origin for this call must be _Root_. + #[pallet::weight((*_weight, call.get_dispatch_info().class))] + pub fn with_weight( + origin: OriginFor, + call: Box<::RuntimeCall>, + _weight: Weight, + ) -> DispatchResult { + ensure_root(origin)?; + let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into()); + res.map(|_| ()).map_err(|e| e.error) + } } } diff --git a/frame/utility/src/tests.rs b/frame/utility/src/tests.rs index 848fc374619b7..ef5ab3454073d 100644 --- a/frame/utility/src/tests.rs +++ b/frame/utility/src/tests.rs @@ -901,3 +901,22 @@ fn batch_all_works_with_council_origin() { )); }) } + +#[test] +fn with_weight_works() { + new_test_ext().execute_with(|| { + let upgrade_code_call = + Box::new(RuntimeCall::System(frame_system::Call::set_code_without_checks { + code: vec![], + })); + let with_weight_call = Call::::with_weight { + call: upgrade_code_call, + weight: Weight::from_parts(123, 456), + }; + assert_eq!(with_weight_call.get_dispatch_info().weight, Weight::from_parts(123, 456)); + assert_eq!( + with_weight_call.get_dispatch_info().class, + frame_support::dispatch::DispatchClass::Operational + ); + }) +} From f90788fc19a5201d10d90799d0663b89219e7d48 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Mon, 5 Dec 2022 13:43:31 -0400 Subject: [PATCH 2/2] improve test --- frame/utility/src/tests.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/frame/utility/src/tests.rs b/frame/utility/src/tests.rs index ef5ab3454073d..d48ce139d839c 100644 --- a/frame/utility/src/tests.rs +++ b/frame/utility/src/tests.rs @@ -909,10 +909,18 @@ fn with_weight_works() { Box::new(RuntimeCall::System(frame_system::Call::set_code_without_checks { code: vec![], })); + // Weight before is max. + assert_eq!(upgrade_code_call.get_dispatch_info().weight, Weight::MAX); + assert_eq!( + upgrade_code_call.get_dispatch_info().class, + frame_support::dispatch::DispatchClass::Operational + ); + let with_weight_call = Call::::with_weight { call: upgrade_code_call, weight: Weight::from_parts(123, 456), }; + // Weight after is set by Root. assert_eq!(with_weight_call.get_dispatch_info().weight, Weight::from_parts(123, 456)); assert_eq!( with_weight_call.get_dispatch_info().class,