@@ -1099,34 +1099,61 @@ pub mod pallet_macros {
1099
1099
1100
1100
/// Allows specifying the weight of a call.
1101
1101
///
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`.
1104
1110
///
1105
1111
/// ## Example
1106
1112
///
1107
1113
/// ```
1108
1114
/// #[frame_support::pallet]
1109
1115
/// 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
+ ///
1113
1119
/// #[pallet::pallet]
1114
1120
/// pub struct Pallet<T>(_);
1115
1121
///
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)]
1117
1129
/// 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())]
1119
1132
/// #[pallet::call_index(0)]
1120
- /// pub fn something (
1121
- /// _ : OriginFor<T>,
1133
+ /// pub fn do_something (
1134
+ /// origin : OriginFor<T>,
1122
1135
/// foo: u32,
1123
1136
/// ) -> DispatchResult {
1124
- /// unimplemented!( )
1137
+ /// Ok(() )
1125
1138
/// }
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
+ /// }
1130
1157
/// }
1131
1158
/// ```
1132
1159
pub use frame_support_procedural:: weight;
@@ -2182,9 +2209,69 @@ pub mod pallet_macros {
2182
2209
/// [`crate::dispatch::DispatchResultWithPostInfo`]).
2183
2210
///
2184
2211
/// **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
+ /// ```
2188
2275
///
2189
2276
/// ## Default Behavior
2190
2277
///
0 commit comments