Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
weight for dispatch_as
  • Loading branch information
xlc committed Oct 30, 2021
commit 0722dcf0932d842a07985d7cd73651b692f53e9e
2 changes: 1 addition & 1 deletion frame/support/src/traits/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions frame/utility/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,13 @@ benchmarks! {
assert_last_event::<T>(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: <T::Origin as frame_support::traits::OriginTrait>::PalletsOrigin = origin.caller().clone();
let pallets_origin = Into::<T::PalletsOrigin>::into(pallets_origin.clone());
}: _(RawOrigin::Root, Box::new(pallets_origin), call)

impl_benchmark_test_suite!(Pallet, crate::tests::new_test_ext(), crate::tests::Test);
}
14 changes: 8 additions & 6 deletions frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ pub mod pallet {
+ 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>;
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;
Expand Down Expand Up @@ -337,26 +339,26 @@ 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().
/// # </weight>
#[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,
)
})]
pub fn dispatch_as(
origin: OriginFor<T>,
as_origin: T::PalletsOrigin,
as_origin: Box<T::PalletsOrigin>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to box it? It wasn't boxed before you added the benchmark. We should solve it there when possible.

Copy link
Contributor Author

@xlc xlc Oct 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is generally good idea to box big data structure in call parameter to avoid increase the Call enum size. the same reason box is used elsewhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I thought it was related to the benchmarks. Didn't know that this is a big argument, though.

call: Box<<T as Config>::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(())
Expand Down
7 changes: 7 additions & 0 deletions frame/utility/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -66,6 +67,9 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// 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
Expand All @@ -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)
}
}