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

Add with_weight extrinsic #12848

Merged
merged 2 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>,
call: Box<<T as Config>::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)
Copy link
Member Author

@shawntabrizi shawntabrizi Dec 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other functions similar to this emit an event with the result, swallow the result, and just return Ok(()). I think now that we have storage layer by default, we can simply return the result, which I have opted for here.

}
}
}

Expand Down
19 changes: 19 additions & 0 deletions frame/utility/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Test>::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
);
})
}