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

Commit

Permalink
implement dispatch_as
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Oct 5, 2021
1 parent 8f5b446 commit f984779
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime>;
}

Expand Down
1 change: 1 addition & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
1 change: 1 addition & 0 deletions frame/proxy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand Down
39 changes: 39 additions & 0 deletions frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ 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>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand All @@ -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]
Expand Down Expand Up @@ -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_.
///
/// # <weight>
/// - O(1).
/// - Limited storage reads.
/// - One DB write (event).
/// - Weight of derivative `call` execution + 10,000.
/// # </weight>
#[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<T>,
as_origin: 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::DsipatchAsDone(res.map(|_| ()).map_err(|e| e.error)));
// Sudo user does not pay a fee.
Ok(())
}
}
}

Expand Down

0 comments on commit f984779

Please sign in to comment.