Skip to content

Commit 54deef9

Browse files
Nathy-bajogui1117bkchr
authored
Documentation update for weight. #7354 (#7376)
resolves #7354 Polkadot address: 121HJWZtD13GJQPD82oEj3gSeHqsRYm1mFgRALu4L96kfPD1 --------- Co-authored-by: Guillaume Thiolliere <[email protected]> Co-authored-by: Bastian Köcher <[email protected]>
1 parent c578318 commit 54deef9

File tree

2 files changed

+117
-17
lines changed

2 files changed

+117
-17
lines changed

prdoc/pr_7376.prdoc

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
title: Documentation update for weight.
2+
3+
doc:
4+
- audience: Runtime Dev
5+
description: |-
6+
Document the usage of `#[pallet::call(weight = <T as Config>::WeightInfo)]` within FRAME macros.
7+
This update enhances the documentation for `#[pallet::call]` and `#[pallet::weight]`, providing
8+
examples and clarifying weight specifications for dispatchable functions, ensuring consistency
9+
with existing guidelines.
10+
11+
crates:
12+
- name: frame-support
13+
bump: patch

substrate/frame/support/src/lib.rs

+104-17
Original file line numberDiff line numberDiff line change
@@ -1099,34 +1099,61 @@ pub mod pallet_macros {
10991099

11001100
/// Allows specifying the weight of a call.
11011101
///
1102-
/// Each dispatchable needs to define a weight with the `#[pallet::weight($expr)]`
1103-
/// attribute. The first argument must be `origin: OriginFor<T>`.
1102+
/// Each dispatchable needs to define a weight.
1103+
/// This attribute allows to define a weight using the expression:
1104+
/// `#[pallet::weight($expr)]` Note that argument of the call are available inside the
1105+
/// expression.
1106+
///
1107+
/// If not defined explicitly, the weight can be implicitly inferred from the weight info
1108+
/// defined in the attribute `pallet::call`: `#[pallet::call(weight = $WeightInfo)]`.
1109+
/// Or it can be simply ignored when the pallet is in `dev_mode`.
11041110
///
11051111
/// ## Example
11061112
///
11071113
/// ```
11081114
/// #[frame_support::pallet]
11091115
/// mod pallet {
1110-
/// # use frame_support::pallet_prelude::*;
1111-
/// # use frame_system::pallet_prelude::*;
1112-
/// #
1116+
/// use frame_support::pallet_prelude::*;
1117+
/// use frame_system::pallet_prelude::*;
1118+
///
11131119
/// #[pallet::pallet]
11141120
/// pub struct Pallet<T>(_);
11151121
///
1116-
/// #[pallet::call]
1122+
/// #[pallet::config]
1123+
/// pub trait Config: frame_system::Config {
1124+
/// /// Type for specifying dispatchable weights.
1125+
/// type WeightInfo: WeightInfo;
1126+
/// }
1127+
///
1128+
/// #[pallet::call(weight = <T as Config>::WeightInfo)]
11171129
/// impl<T: Config> Pallet<T> {
1118-
/// #[pallet::weight({0})] // <- set actual weight here
1130+
/// // Explicit weight definition
1131+
/// #[pallet::weight(<T as Config>::WeightInfo::do_something())]
11191132
/// #[pallet::call_index(0)]
1120-
/// pub fn something(
1121-
/// _: OriginFor<T>,
1133+
/// pub fn do_something(
1134+
/// origin: OriginFor<T>,
11221135
/// foo: u32,
11231136
/// ) -> DispatchResult {
1124-
/// unimplemented!()
1137+
/// Ok(())
11251138
/// }
1126-
/// }
1127-
/// #
1128-
/// # #[pallet::config]
1129-
/// # pub trait Config: frame_system::Config {}
1139+
///
1140+
/// // Implicit weight definition, the macro looks up to the weight info defined in
1141+
/// // `#[pallet::call(weight = $WeightInfo)]` attribute. Then use
1142+
/// // `$WeightInfo::do_something_else` as the weight function.
1143+
/// #[pallet::call_index(1)]
1144+
/// pub fn do_something_else(
1145+
/// origin: OriginFor<T>,
1146+
/// bar: u64,
1147+
/// ) -> DispatchResult {
1148+
/// Ok(())
1149+
/// }
1150+
/// }
1151+
///
1152+
/// /// The `WeightInfo` trait defines weight functions for dispatchable calls.
1153+
/// pub trait WeightInfo {
1154+
/// fn do_something() -> Weight;
1155+
/// fn do_something_else() -> Weight;
1156+
/// }
11301157
/// }
11311158
/// ```
11321159
pub use frame_support_procedural::weight;
@@ -2182,9 +2209,69 @@ pub mod pallet_macros {
21822209
/// [`crate::dispatch::DispatchResultWithPostInfo`]).
21832210
///
21842211
/// **WARNING**: modifying dispatchables, changing their order (i.e. using [`call_index`]),
2185-
/// removing some, etc., must be done with care. This will change the encoding of the , and
2186-
/// the call can be stored on-chain (e.g. in `pallet-scheduler`). Thus, migration might be
2187-
/// needed. This is why the use of `call_index` is mandatory by default in FRAME.
2212+
/// removing some, etc., must be done with care. This will change the encoding of the call,
2213+
/// and the call can be stored on-chain (e.g. in `pallet-scheduler`). Thus, migration
2214+
/// might be needed. This is why the use of `call_index` is mandatory by default in FRAME.
2215+
///
2216+
/// ## Weight info
2217+
///
2218+
/// Each call needs to define a weight.
2219+
/// * The weight can be defined explicitly using the attribute `#[pallet::weight($expr)]`
2220+
/// (Note that argument of the call are available inside the expression).
2221+
/// * Or it can be defined implicitly, the weight info for the calls needs to be specified
2222+
/// in the call attribute: `#[pallet::call(weight = $WeightInfo)]`, then each call that
2223+
/// doesn't have explicit weight will use `$WeightInfo::$call_name` as the weight.
2224+
///
2225+
/// * Or it can be simply ignored when the pallet is in `dev_mode`.
2226+
///
2227+
/// ```
2228+
/// #[frame_support::pallet]
2229+
/// mod pallet {
2230+
/// use frame_support::pallet_prelude::*;
2231+
/// use frame_system::pallet_prelude::*;
2232+
///
2233+
/// #[pallet::pallet]
2234+
/// pub struct Pallet<T>(_);
2235+
///
2236+
/// #[pallet::config]
2237+
/// pub trait Config: frame_system::Config {
2238+
/// /// Type for specifying dispatchable weights.
2239+
/// type WeightInfo: WeightInfo;
2240+
/// }
2241+
///
2242+
/// /// The `WeightInfo` trait defines weight functions for dispatchable calls.
2243+
/// pub trait WeightInfo {
2244+
/// fn do_something() -> Weight;
2245+
/// fn do_something_else() -> Weight;
2246+
/// }
2247+
///
2248+
/// #[pallet::call(weight = <T as Config>::WeightInfo)]
2249+
/// impl<T: Config> Pallet<T> {
2250+
/// // Explicit weight definition using `#[pallet::weight(...)]`
2251+
/// #[pallet::weight(<T as Config>::WeightInfo::do_something())]
2252+
/// #[pallet::call_index(0)]
2253+
/// pub fn do_something(
2254+
/// origin: OriginFor<T>,
2255+
/// foo: u32,
2256+
/// ) -> DispatchResult {
2257+
/// // Function logic here
2258+
/// Ok(())
2259+
/// }
2260+
///
2261+
/// // Implicit weight definition, the macro looks up to the weight info defined in
2262+
/// // `#[pallet::call(weight = $WeightInfo)]` attribute. Then use
2263+
/// // `$WeightInfo::do_something_else` as the weight function.
2264+
/// #[pallet::call_index(1)]
2265+
/// pub fn do_something_else(
2266+
/// origin: OriginFor<T>,
2267+
/// bar: u64,
2268+
/// ) -> DispatchResult {
2269+
/// // Function logic here
2270+
/// Ok(())
2271+
/// }
2272+
/// }
2273+
/// }
2274+
/// ```
21882275
///
21892276
/// ## Default Behavior
21902277
///

0 commit comments

Comments
 (0)