Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
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
8 changes: 8 additions & 0 deletions frame/utility/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,13 @@ benchmarks! {
assert_last_event::<T>(Event::BatchCompleted.into())
}

ensure_dispatch_as {
let caller = account("caller", SEED, SEED);
let call = Box::new(frame_system::Call::remark { remark: vec![] }.into());
let origin: T::RuntimeOrigin = RawOrigin::Signed(caller).into();
let pallets_origin: <T::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin = origin.caller().clone();
let pallets_origin = Into::<T::PalletsOrigin>::into(pallets_origin);
}: _(RawOrigin::Root, Box::new(pallets_origin), call)

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

/// Dispatches a function call with a provided origin. Unlike `dispatch_as`, this
/// function propagates errors returned by the wrapped `call`.
///
/// The dispatch origin for this call must be _Root_.
///
/// ## Complexity
/// - O(1).
#[pallet::call_index(6)]
#[pallet::weight({
let dispatch_info = call.get_dispatch_info();
(
T::WeightInfo::ensure_dispatch_as()
.saturating_add(dispatch_info.weight),
dispatch_info.class,
)
})]
pub fn ensure_dispatch_as(
origin: OriginFor<T>,
as_origin: Box<T::PalletsOrigin>,
call: Box<<T as Config>::RuntimeCall>,
) -> DispatchResult {
ensure_root(origin)?;

call.dispatch_bypass_filter((*as_origin).into()).map_err(|e| e.error)?;

Self::deposit_event(Event::DispatchedAs { result: Ok(()) });
Ok(())
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions frame/utility/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub trait WeightInfo {
fn batch_all(c: u32, ) -> Weight;
fn dispatch_as() -> Weight;
fn force_batch(c: u32, ) -> Weight;
fn ensure_dispatch_as() -> Weight;
}

/// Weights for pallet_utility using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -102,6 +103,13 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Standard Error: 1_803
.saturating_add(Weight::from_ref_time(3_645_950).saturating_mul(c.into()))
}
fn ensure_dispatch_as() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 8_840 nanoseconds.
Weight::from_ref_time(9_280_000)
}
}

// For backwards compatibility and tests
Expand Down Expand Up @@ -150,4 +158,11 @@ impl WeightInfo for () {
// Standard Error: 1_803
.saturating_add(Weight::from_ref_time(3_645_950).saturating_mul(c.into()))
}
fn ensure_dispatch_as() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
// Estimated: `0`
// Minimum execution time: 8_840 nanoseconds.
Weight::from_ref_time(9_280_000)
}
}