From 09eb4891d520f8068c966455371e3c60128d0b7c Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Fri, 31 Jan 2025 18:53:38 +0800 Subject: [PATCH] Fixes #219 --- .../src/weights/pallet_utility.rs | 3 ++ .../src/weights/pallet_utility.rs | 3 ++ .../src/weights/pallet_utility.rs | 3 ++ .../src/weights/pallet_utility.rs | 3 ++ .../src/weights/pallet_utility.rs | 3 ++ .../src/weights/pallet_utility.rs | 3 ++ .../src/weights/pallet_utility.rs | 3 ++ .../src/weights/pallet_utility.rs | 3 ++ .../src/weights/pallet_utility.rs | 3 ++ .../rococo/src/weights/pallet_utility.rs | 3 ++ .../westend/src/weights/pallet_utility.rs | 3 ++ substrate/frame/utility/src/benchmarking.rs | 12 ++++++++ substrate/frame/utility/src/lib.rs | 30 +++++++++++++++++++ substrate/frame/utility/src/weights.rs | 7 +++++ 14 files changed, 82 insertions(+) diff --git a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weights/pallet_utility.rs b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weights/pallet_utility.rs index a82115b9d093..86a430ca38a6 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weights/pallet_utility.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-rococo/src/weights/pallet_utility.rs @@ -99,4 +99,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 1_745 .saturating_add(Weight::from_parts(6_562_902, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/weights/pallet_utility.rs b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/weights/pallet_utility.rs index a7952d6da00e..09b434c99243 100644 --- a/cumulus/parachains/runtimes/assets/asset-hub-westend/src/weights/pallet_utility.rs +++ b/cumulus/parachains/runtimes/assets/asset-hub-westend/src/weights/pallet_utility.rs @@ -98,4 +98,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 3_765 .saturating_add(Weight::from_parts(6_028_416, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/pallet_utility.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/pallet_utility.rs index d96b9e88840f..f7499f69c624 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/pallet_utility.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/weights/pallet_utility.rs @@ -98,4 +98,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 1_601 .saturating_add(Weight::from_parts(5_138_293, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/weights/pallet_utility.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/weights/pallet_utility.rs index 44cd0cf91e79..00dc43ddb4c8 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/weights/pallet_utility.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/weights/pallet_utility.rs @@ -99,4 +99,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 1_601 .saturating_add(Weight::from_parts(5_138_293, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/cumulus/parachains/runtimes/collectives/collectives-westend/src/weights/pallet_utility.rs b/cumulus/parachains/runtimes/collectives/collectives-westend/src/weights/pallet_utility.rs index c60a79d91da3..b6934bf179f4 100644 --- a/cumulus/parachains/runtimes/collectives/collectives-westend/src/weights/pallet_utility.rs +++ b/cumulus/parachains/runtimes/collectives/collectives-westend/src/weights/pallet_utility.rs @@ -98,4 +98,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 1_395 .saturating_add(Weight::from_parts(5_000_971, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/weights/pallet_utility.rs b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/weights/pallet_utility.rs index 84eb97838680..3b45e6d093ce 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-rococo/src/weights/pallet_utility.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-rococo/src/weights/pallet_utility.rs @@ -99,4 +99,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 1_621 .saturating_add(Weight::from_parts(3_312_302, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/cumulus/parachains/runtimes/coretime/coretime-westend/src/weights/pallet_utility.rs b/cumulus/parachains/runtimes/coretime/coretime-westend/src/weights/pallet_utility.rs index 0f5340843bd6..9393e163e70a 100644 --- a/cumulus/parachains/runtimes/coretime/coretime-westend/src/weights/pallet_utility.rs +++ b/cumulus/parachains/runtimes/coretime/coretime-westend/src/weights/pallet_utility.rs @@ -99,4 +99,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 740 .saturating_add(Weight::from_parts(2_800_888, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/cumulus/parachains/runtimes/people/people-rococo/src/weights/pallet_utility.rs b/cumulus/parachains/runtimes/people/people-rococo/src/weights/pallet_utility.rs index 134bd1fbbc58..a645fec28e7e 100644 --- a/cumulus/parachains/runtimes/people/people-rococo/src/weights/pallet_utility.rs +++ b/cumulus/parachains/runtimes/people/people-rococo/src/weights/pallet_utility.rs @@ -96,4 +96,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 3_915 .saturating_add(Weight::from_parts(4_372_646, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/cumulus/parachains/runtimes/people/people-westend/src/weights/pallet_utility.rs b/cumulus/parachains/runtimes/people/people-westend/src/weights/pallet_utility.rs index 782b0ad6de8d..d362a47661c8 100644 --- a/cumulus/parachains/runtimes/people/people-westend/src/weights/pallet_utility.rs +++ b/cumulus/parachains/runtimes/people/people-westend/src/weights/pallet_utility.rs @@ -96,4 +96,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 7_605 .saturating_add(Weight::from_parts(4_306_193, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/polkadot/runtime/rococo/src/weights/pallet_utility.rs b/polkadot/runtime/rococo/src/weights/pallet_utility.rs index 6f2a374247f8..c168a30b3dc8 100644 --- a/polkadot/runtime/rococo/src/weights/pallet_utility.rs +++ b/polkadot/runtime/rococo/src/weights/pallet_utility.rs @@ -99,4 +99,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 460 .saturating_add(Weight::from_parts(3_173_577, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/polkadot/runtime/westend/src/weights/pallet_utility.rs b/polkadot/runtime/westend/src/weights/pallet_utility.rs index f8238e9351dc..d113faaf6050 100644 --- a/polkadot/runtime/westend/src/weights/pallet_utility.rs +++ b/polkadot/runtime/westend/src/weights/pallet_utility.rs @@ -99,4 +99,7 @@ impl pallet_utility::WeightInfo for WeightInfo { // Standard Error: 2_817 .saturating_add(Weight::from_parts(5_113_539, 0).saturating_mul(c.into())) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } diff --git a/substrate/frame/utility/src/benchmarking.rs b/substrate/frame/utility/src/benchmarking.rs index 88556c05195a..bfcb5ad14a3f 100644 --- a/substrate/frame/utility/src/benchmarking.rs +++ b/substrate/frame/utility/src/benchmarking.rs @@ -92,6 +92,18 @@ mod benchmark { assert_last_event::(Event::BatchCompleted.into()); } + #[benchmark] + fn dispatch_as_checked() { + 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 = origin.caller().clone(); + let pallets_origin = T::PalletsOrigin::from(pallets_origin); + + #[extrinsic_call] + _(RawOrigin::Root, Box::new(pallets_origin), call); + } + impl_benchmark_test_suite! { Pallet, tests::new_test_ext(), diff --git a/substrate/frame/utility/src/lib.rs b/substrate/frame/utility/src/lib.rs index 26c38d1f0459..d6072cbf0f53 100644 --- a/substrate/frame/utility/src/lib.rs +++ b/substrate/frame/utility/src/lib.rs @@ -454,6 +454,36 @@ 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. + /// Almost the same as [`Pallet::dispatch_as`] but check the result of the 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::dispatch_as_checked() + .saturating_add(dispatch_info.call_weight), + dispatch_info.class, + ) + })] + pub fn dispatch_as_checked( + origin: OriginFor, + as_origin: Box, + call: Box<::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(()) + } } impl Pallet { diff --git a/substrate/frame/utility/src/weights.rs b/substrate/frame/utility/src/weights.rs index 8b31eb2ced85..ca1b97e35b0b 100644 --- a/substrate/frame/utility/src/weights.rs +++ b/substrate/frame/utility/src/weights.rs @@ -56,6 +56,7 @@ pub trait WeightInfo { fn batch_all(c: u32, ) -> Weight; fn dispatch_as() -> Weight; fn force_batch(c: u32, ) -> Weight; + fn dispatch_as_checked() -> Weight; } /// Weights for `pallet_utility` using the Substrate node and recommended hardware. @@ -125,6 +126,9 @@ impl WeightInfo for SubstrateWeight { .saturating_add(Weight::from_parts(4_570_923, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(2_u64)) } + fn dispatch_as_checked() -> Weight { + 0.into() + } } // For backwards compatibility and tests. @@ -193,4 +197,7 @@ impl WeightInfo for () { .saturating_add(Weight::from_parts(4_570_923, 0).saturating_mul(c.into())) .saturating_add(RocksDbWeight::get().reads(2_u64)) } + fn dispatch_as_checked() -> Weight { + 0.into() + } }