From f98477913292e5de63621578964502772bcf7dca Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 5 Oct 2021 21:20:12 +1300 Subject: [PATCH 1/8] implement dispatch_as --- bin/node/runtime/src/lib.rs | 3 ++- frame/contracts/src/tests.rs | 1 + frame/proxy/src/tests.rs | 1 + frame/utility/src/lib.rs | 39 ++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index b115087d514d0..5919d510d5f3a 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -222,8 +222,9 @@ impl frame_system::Config for Runtime { impl pallet_randomness_collective_flip::Config for Runtime {} impl pallet_utility::Config for Runtime { - type Event = Event; +type Event = Event; type Call = Call; + type PalletsOrigin = OriginCaller; type WeightInfo = pallet_utility::weights::SubstrateWeight; } diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index b2141ca18b0b1..1fde2c338093a 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -244,6 +244,7 @@ impl pallet_timestamp::Config for Test { impl pallet_utility::Config for Test { type Event = Event; type Call = Call; + type PalletsOrigin = OriginCaller; type WeightInfo = (); } parameter_types! { diff --git a/frame/proxy/src/tests.rs b/frame/proxy/src/tests.rs index d319ebb1a5ab0..c38d3de03b90f 100644 --- a/frame/proxy/src/tests.rs +++ b/frame/proxy/src/tests.rs @@ -96,6 +96,7 @@ impl pallet_balances::Config for Test { impl pallet_utility::Config for Test { type Event = Event; type Call = Call; + type PalletsOrigin = OriginCaller; type WeightInfo = (); } parameter_types! { diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index 54de87c4740c8..88d9048f63541 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -96,6 +96,9 @@ pub mod pallet { + IsSubType> + IsType<::Call>; + /// The caller origin, overarching type of all pallets origins. + type PalletsOrigin: Parameter + Into<::Origin>; + /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; } @@ -110,6 +113,8 @@ pub mod pallet { BatchCompleted, /// A single item within a Batch of dispatches has completed with no error. ItemCompleted, + /// A call was dispatched. \[result\] + DsipatchAsDone(DispatchResult), } #[pallet::extra_constants] @@ -323,6 +328,40 @@ pub mod pallet { let base_weight = T::WeightInfo::batch_all(calls_len as u32); Ok(Some(base_weight + weight).into()) } + + /// Dispatches a function call with a provided origin. + /// + /// The dispatch origin for this call must be _Root_. + /// + /// # + /// - O(1). + /// - Limited storage reads. + /// - One DB write (event). + /// - Weight of derivative `call` execution + 10,000. + /// # + #[pallet::weight({ + let dispatch_info = call.get_dispatch_info(); + ( + dispatch_info.weight + .saturating_add(10_000) + // AccountData for inner call origin accountdata. + .saturating_add(T::DbWeight::get().reads_writes(1, 1)), + dispatch_info.class, + ) + })] + pub fn dispatch_as( + origin: OriginFor, + as_origin: T::PalletsOrigin, + call: Box<::Call>, + ) -> DispatchResult { + ensure_root(origin)?; + + let res = call.dispatch_bypass_filter(as_origin.into()); + + Self::deposit_event(Event::DsipatchAsDone(res.map(|_| ()).map_err(|e| e.error))); + // Sudo user does not pay a fee. + Ok(()) + } } } From 5e549962fa03db417fad12e340d5b1ea80e4e5ac Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 5 Oct 2021 21:32:06 +1300 Subject: [PATCH 2/8] fix --- bin/node/runtime/src/lib.rs | 2 +- frame/utility/src/tests.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 5919d510d5f3a..f60db654d7012 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -222,7 +222,7 @@ impl frame_system::Config for Runtime { impl pallet_randomness_collective_flip::Config for Runtime {} impl pallet_utility::Config for Runtime { -type Event = Event; + type Event = Event; type Call = Call; type PalletsOrigin = OriginCaller; type WeightInfo = pallet_utility::weights::SubstrateWeight; diff --git a/frame/utility/src/tests.rs b/frame/utility/src/tests.rs index bbfbb417e23d1..da537d48bde4b 100644 --- a/frame/utility/src/tests.rs +++ b/frame/utility/src/tests.rs @@ -168,6 +168,7 @@ impl Contains for TestBaseCallFilter { impl Config for Test { type Event = Event; type Call = Call; + type PalletsOrigin = OriginCaller; type WeightInfo = (); } From 4e19053efb1bb609c7bb1c1a6f9ce6042d361b74 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Tue, 5 Oct 2021 22:18:16 +1300 Subject: [PATCH 3/8] fix --- frame/utility/src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index 88d9048f63541..67f39de6dcd21 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -114,7 +114,7 @@ pub mod pallet { /// A single item within a Batch of dispatches has completed with no error. ItemCompleted, /// A call was dispatched. \[result\] - DsipatchAsDone(DispatchResult), + DispatchedAs(DispatchResult), } #[pallet::extra_constants] @@ -358,8 +358,7 @@ pub mod pallet { let res = call.dispatch_bypass_filter(as_origin.into()); - Self::deposit_event(Event::DsipatchAsDone(res.map(|_| ()).map_err(|e| e.error))); - // Sudo user does not pay a fee. + Self::deposit_event(Event::DispatchedAs(res.map(|_| ()).map_err(|e| e.error))); Ok(()) } } From 0722dcf0932d842a07985d7cd73651b692f53e9e Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Sat, 30 Oct 2021 17:24:37 +1300 Subject: [PATCH 4/8] weight for dispatch_as --- frame/support/src/traits/dispatch.rs | 2 +- frame/utility/src/benchmarking.rs | 8 ++++++++ frame/utility/src/lib.rs | 14 ++++++++------ frame/utility/src/weights.rs | 7 +++++++ 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/frame/support/src/traits/dispatch.rs b/frame/support/src/traits/dispatch.rs index 92b832ba32961..667367ba73a20 100644 --- a/frame/support/src/traits/dispatch.rs +++ b/frame/support/src/traits/dispatch.rs @@ -56,7 +56,7 @@ pub trait OriginTrait: Sized { type Call; /// The caller origin, overarching type of all pallets origins. - type PalletsOrigin; + type PalletsOrigin: Clone; /// The AccountId used across the system. type AccountId; diff --git a/frame/utility/src/benchmarking.rs b/frame/utility/src/benchmarking.rs index 70cc61f87b9c9..639b6348c285a 100644 --- a/frame/utility/src/benchmarking.rs +++ b/frame/utility/src/benchmarking.rs @@ -64,5 +64,13 @@ benchmarks! { assert_last_event::(Event::BatchCompleted.into()) } + dispatch_as { + let caller = account("caller", SEED, SEED); + let call = Box::new(frame_system::Call::remark { remark: vec![] }.into()); + let origin : T::Origin = RawOrigin::Signed(caller).into(); + let pallets_origin: ::PalletsOrigin = origin.caller().clone(); + let pallets_origin = Into::::into(pallets_origin.clone()); + }: _(RawOrigin::Root, Box::new(pallets_origin), call) + impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test); } diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index 67f39de6dcd21..28b24c62b381d 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -97,7 +97,9 @@ pub mod pallet { + IsType<::Call>; /// The caller origin, overarching type of all pallets origins. - type PalletsOrigin: Parameter + Into<::Origin>; + type PalletsOrigin: Parameter + + Into<::Origin> + + IsType<<::Origin as frame_support::traits::OriginTrait>::PalletsOrigin>; /// Weight information for extrinsics in this pallet. type WeightInfo: WeightInfo; @@ -337,13 +339,13 @@ pub mod pallet { /// - O(1). /// - Limited storage reads. /// - One DB write (event). - /// - Weight of derivative `call` execution + 10,000. + /// - Weight of derivative `call` execution + T::WeightInfo::dispatch_as(). /// # #[pallet::weight({ let dispatch_info = call.get_dispatch_info(); ( - dispatch_info.weight - .saturating_add(10_000) + T::WeightInfo::dispatch_as() + .saturating_add(dispatch_info.weight) // AccountData for inner call origin accountdata. .saturating_add(T::DbWeight::get().reads_writes(1, 1)), dispatch_info.class, @@ -351,12 +353,12 @@ pub mod pallet { })] pub fn dispatch_as( origin: OriginFor, - as_origin: T::PalletsOrigin, + as_origin: Box, call: Box<::Call>, ) -> DispatchResult { ensure_root(origin)?; - let res = call.dispatch_bypass_filter(as_origin.into()); + let res = call.dispatch_bypass_filter((*as_origin).into()); Self::deposit_event(Event::DispatchedAs(res.map(|_| ()).map_err(|e| e.error))); Ok(()) diff --git a/frame/utility/src/weights.rs b/frame/utility/src/weights.rs index 6ac23419e3ef7..9f28cce6a42b6 100644 --- a/frame/utility/src/weights.rs +++ b/frame/utility/src/weights.rs @@ -48,6 +48,7 @@ pub trait WeightInfo { fn batch(c: u32, ) -> Weight; fn as_derivative() -> Weight; fn batch_all(c: u32, ) -> Weight; + fn dispatch_as() -> Weight; } /// Weights for pallet_utility using the Substrate node and recommended hardware. @@ -66,6 +67,9 @@ impl WeightInfo for SubstrateWeight { // Standard Error: 3_000 .saturating_add((7_251_000 as Weight).saturating_mul(c as Weight)) } + fn dispatch_as() -> Weight { + (4_030_000 as Weight) + } } // For backwards compatibility and tests @@ -83,4 +87,7 @@ impl WeightInfo for () { // Standard Error: 3_000 .saturating_add((7_251_000 as Weight).saturating_mul(c as Weight)) } + fn dispatch_as() -> Weight { + (4_030_000 as Weight) + } } From d3f9c812820a139d094384490ab6adba35b023b3 Mon Sep 17 00:00:00 2001 From: Parity Bot Date: Sat, 30 Oct 2021 04:32:33 +0000 Subject: [PATCH 5/8] cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_utility --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/utility/src/weights.rs --template=./.maintain/frame-weight-template.hbs --- frame/utility/src/weights.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/frame/utility/src/weights.rs b/frame/utility/src/weights.rs index 9f28cce6a42b6..bce18271684bc 100644 --- a/frame/utility/src/weights.rs +++ b/frame/utility/src/weights.rs @@ -18,7 +18,7 @@ //! Autogenerated weights for pallet_utility //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev -//! DATE: 2021-08-07, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! DATE: 2021-10-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]` //! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 128 // Executed Command: @@ -55,39 +55,39 @@ pub trait WeightInfo { pub struct SubstrateWeight(PhantomData); impl WeightInfo for SubstrateWeight { fn batch(c: u32, ) -> Weight { - (30_319_000 as Weight) + (18_293_000 as Weight) // Standard Error: 3_000 - .saturating_add((6_759_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((5_530_000 as Weight).saturating_mul(c as Weight)) } fn as_derivative() -> Weight { - (4_030_000 as Weight) + (3_387_000 as Weight) } fn batch_all(c: u32, ) -> Weight { - (26_621_000 as Weight) - // Standard Error: 3_000 - .saturating_add((7_251_000 as Weight).saturating_mul(c as Weight)) + (19_223_000 as Weight) + // Standard Error: 4_000 + .saturating_add((5_998_000 as Weight).saturating_mul(c as Weight)) } fn dispatch_as() -> Weight { - (4_030_000 as Weight) + (14_340_000 as Weight) } } // For backwards compatibility and tests impl WeightInfo for () { fn batch(c: u32, ) -> Weight { - (30_319_000 as Weight) + (18_293_000 as Weight) // Standard Error: 3_000 - .saturating_add((6_759_000 as Weight).saturating_mul(c as Weight)) + .saturating_add((5_530_000 as Weight).saturating_mul(c as Weight)) } fn as_derivative() -> Weight { - (4_030_000 as Weight) + (3_387_000 as Weight) } fn batch_all(c: u32, ) -> Weight { - (26_621_000 as Weight) - // Standard Error: 3_000 - .saturating_add((7_251_000 as Weight).saturating_mul(c as Weight)) + (19_223_000 as Weight) + // Standard Error: 4_000 + .saturating_add((5_998_000 as Weight).saturating_mul(c as Weight)) } fn dispatch_as() -> Weight { - (4_030_000 as Weight) + (14_340_000 as Weight) } } From f9c1fa76e18d10af334660c4850d3666775b2eb6 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Sat, 30 Oct 2021 18:03:23 +1300 Subject: [PATCH 6/8] fix --- frame/support/src/dispatch.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index a492bc12f6a38..ed4f11ba1d7a4 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -2679,6 +2679,7 @@ mod tests { } } + #[derive(Clone)] pub struct OuterOrigin; impl crate::traits::OriginTrait for OuterOrigin { From 2b879fc85362ccf69134b781ba8e0df9a55e6e5a Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Sat, 30 Oct 2021 23:49:12 +1300 Subject: [PATCH 7/8] Update frame/utility/src/benchmarking.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Alexander Theißen --- frame/utility/src/benchmarking.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame/utility/src/benchmarking.rs b/frame/utility/src/benchmarking.rs index 639b6348c285a..3a47c12f48d04 100644 --- a/frame/utility/src/benchmarking.rs +++ b/frame/utility/src/benchmarking.rs @@ -67,7 +67,7 @@ benchmarks! { dispatch_as { let caller = account("caller", SEED, SEED); let call = Box::new(frame_system::Call::remark { remark: vec![] }.into()); - let origin : T::Origin = RawOrigin::Signed(caller).into(); + let origin: T::Origin = RawOrigin::Signed(caller).into(); let pallets_origin: ::PalletsOrigin = origin.caller().clone(); let pallets_origin = Into::::into(pallets_origin.clone()); }: _(RawOrigin::Root, Box::new(pallets_origin), call) From 0187df5d4562f714956983384387ddccb2117140 Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Sat, 30 Oct 2021 23:54:30 +1300 Subject: [PATCH 8/8] fix issues --- frame/support/src/dispatch.rs | 1 - frame/support/src/traits/dispatch.rs | 2 +- frame/utility/src/benchmarking.rs | 1 + frame/utility/src/lib.rs | 4 +--- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index ed4f11ba1d7a4..a492bc12f6a38 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -2679,7 +2679,6 @@ mod tests { } } - #[derive(Clone)] pub struct OuterOrigin; impl crate::traits::OriginTrait for OuterOrigin { diff --git a/frame/support/src/traits/dispatch.rs b/frame/support/src/traits/dispatch.rs index 667367ba73a20..92b832ba32961 100644 --- a/frame/support/src/traits/dispatch.rs +++ b/frame/support/src/traits/dispatch.rs @@ -56,7 +56,7 @@ pub trait OriginTrait: Sized { type Call; /// The caller origin, overarching type of all pallets origins. - type PalletsOrigin: Clone; + type PalletsOrigin; /// The AccountId used across the system. type AccountId; diff --git a/frame/utility/src/benchmarking.rs b/frame/utility/src/benchmarking.rs index 3a47c12f48d04..ce59d7e898eb1 100644 --- a/frame/utility/src/benchmarking.rs +++ b/frame/utility/src/benchmarking.rs @@ -30,6 +30,7 @@ fn assert_last_event(generic_event: ::Event) { } benchmarks! { + where_clause { where ::PalletsOrigin: Clone } batch { let c in 0 .. 1000; let mut calls: Vec<::Call> = Vec::new(); diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index 28b24c62b381d..786c26254c0a5 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -345,9 +345,7 @@ pub mod pallet { let dispatch_info = call.get_dispatch_info(); ( T::WeightInfo::dispatch_as() - .saturating_add(dispatch_info.weight) - // AccountData for inner call origin accountdata. - .saturating_add(T::DbWeight::get().reads_writes(1, 1)), + .saturating_add(dispatch_info.weight), dispatch_info.class, ) })]