implement dispatch_as (#9934)

* implement dispatch_as

* fix

* fix

* weight for dispatch_as

* 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

* fix

* Update frame/utility/src/benchmarking.rs

Co-authored-by: Alexander Theißen <alex.theissen@me.com>

* fix issues

Co-authored-by: Parity Bot <admin@parity.io>
Co-authored-by: Alexander Theißen <alex.theissen@me.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Xiliang Chen
2021-11-05 03:34:29 +13:00
committed by GitHub
parent acc835acd3
commit 58a3ab2813
7 changed files with 71 additions and 13 deletions
+38
View File
@@ -96,6 +96,11 @@ pub mod pallet {
+ IsSubType<Call<Self>>
+ IsType<<Self as frame_system::Config>::Call>;
/// The caller origin, overarching type of all pallets origins.
type PalletsOrigin: Parameter +
Into<<Self as frame_system::Config>::Origin> +
IsType<<<Self as frame_system::Config>::Origin as frame_support::traits::OriginTrait>::PalletsOrigin>;
/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
@@ -110,6 +115,8 @@ pub mod pallet {
BatchCompleted,
/// A single item within a Batch of dispatches has completed with no error.
ItemCompleted,
/// A call was dispatched. \[result\]
DispatchedAs(DispatchResult),
}
// Align the call size to 1KB. As we are currently compiling the runtime for native/wasm
@@ -342,6 +349,37 @@ 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_.
///
/// # <weight>
/// - O(1).
/// - Limited storage reads.
/// - One DB write (event).
/// - Weight of derivative `call` execution + T::WeightInfo::dispatch_as().
/// # </weight>
#[pallet::weight({
let dispatch_info = call.get_dispatch_info();
(
T::WeightInfo::dispatch_as()
.saturating_add(dispatch_info.weight),
dispatch_info.class,
)
})]
pub fn dispatch_as(
origin: OriginFor<T>,
as_origin: Box<T::PalletsOrigin>,
call: Box<<T as Config>::Call>,
) -> DispatchResult {
ensure_root(origin)?;
let res = call.dispatch_bypass_filter((*as_origin).into());
Self::deposit_event(Event::DispatchedAs(res.map(|_| ()).map_err(|e| e.error)));
Ok(())
}
}
}