Skip to content

Commit 2871c94

Browse files
shawntabriziltfschoen
authored andcommitted
Add with_weight extrinsic (paritytech#12848)
* add with weight extrinsic * improve test
1 parent b1a9e9d commit 2871c94

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

frame/utility/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,23 @@ pub mod pallet {
474474
let base_weight = T::WeightInfo::batch(calls_len as u32);
475475
Ok(Some(base_weight.saturating_add(weight)).into())
476476
}
477+
478+
/// Dispatch a function call with a specified weight.
479+
///
480+
/// This function does not check the weight of the call, and instead allows the
481+
/// Root origin to specify the weight of the call.
482+
///
483+
/// The dispatch origin for this call must be _Root_.
484+
#[pallet::weight((*_weight, call.get_dispatch_info().class))]
485+
pub fn with_weight(
486+
origin: OriginFor<T>,
487+
call: Box<<T as Config>::RuntimeCall>,
488+
_weight: Weight,
489+
) -> DispatchResult {
490+
ensure_root(origin)?;
491+
let res = call.dispatch_bypass_filter(frame_system::RawOrigin::Root.into());
492+
res.map(|_| ()).map_err(|e| e.error)
493+
}
477494
}
478495
}
479496

frame/utility/src/tests.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,3 +901,30 @@ fn batch_all_works_with_council_origin() {
901901
));
902902
})
903903
}
904+
905+
#[test]
906+
fn with_weight_works() {
907+
new_test_ext().execute_with(|| {
908+
let upgrade_code_call =
909+
Box::new(RuntimeCall::System(frame_system::Call::set_code_without_checks {
910+
code: vec![],
911+
}));
912+
// Weight before is max.
913+
assert_eq!(upgrade_code_call.get_dispatch_info().weight, Weight::MAX);
914+
assert_eq!(
915+
upgrade_code_call.get_dispatch_info().class,
916+
frame_support::dispatch::DispatchClass::Operational
917+
);
918+
919+
let with_weight_call = Call::<Test>::with_weight {
920+
call: upgrade_code_call,
921+
weight: Weight::from_parts(123, 456),
922+
};
923+
// Weight after is set by Root.
924+
assert_eq!(with_weight_call.get_dispatch_info().weight, Weight::from_parts(123, 456));
925+
assert_eq!(
926+
with_weight_call.get_dispatch_info().class,
927+
frame_support::dispatch::DispatchClass::Operational
928+
);
929+
})
930+
}

0 commit comments

Comments
 (0)