FRAME: inherited call weight syntax (#13932)

* First approach on pallet::call_weight

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Use attr on pallet::call instead

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Ui tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Rename to weight(prefix = ...))

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Simplify to #[pallet::call(weight(T))]

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Add stray token error

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Cleanup

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Migrate remaining pallets

Using script from https://github.com/ggwpez/substrate-scripts/blob/e1b5ea5b5b4018867f3e869fce6f448b4ba9d71f/frame-code-migration/src/call_weight.rs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Try to add some docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Revert "Migrate remaining pallets"

Lets do this as a follow-up, I dont want to bloat this small MR.

This reverts commit 331d4b42d72de1dacaed714d69166fa1bc9c92dd.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Renames

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Review fixes

Co-authored-by: Sam Johnson <sam@durosoft.com>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Test weights

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update UI tests

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update frame/support/procedural/src/pallet/parse/mod.rs

Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com>

* Remove old code

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update docs

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

---------

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: parity-processbot <>
Co-authored-by: Muharem Ismailov <ismailov.m.h@gmail.com>
This commit is contained in:
Oliver Tale-Yazdi
2023-04-27 16:08:08 +02:00
committed by GitHub
parent 6e5141fc23
commit b5201fa0ec
30 changed files with 698 additions and 92 deletions
+1 -11
View File
@@ -497,7 +497,7 @@ pub mod pallet {
pub type UnscrupulousWebsites<T: Config<I>, I: 'static = ()> =
StorageValue<_, BoundedVec<UrlOf<T, I>, T::MaxUnscrupulousItems>, ValueQuery>;
#[pallet::call]
#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Add a new proposal to be voted on.
///
@@ -649,7 +649,6 @@ pub mod pallet {
/// Set a new IPFS CID to the alliance rule.
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::set_rule())]
pub fn set_rule(origin: OriginFor<T>, rule: Cid) -> DispatchResult {
T::AdminOrigin::ensure_origin(origin)?;
@@ -661,7 +660,6 @@ pub mod pallet {
/// Make an announcement of a new IPFS CID about alliance issues.
#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::announce())]
pub fn announce(origin: OriginFor<T>, announcement: Cid) -> DispatchResult {
T::AnnouncementOrigin::ensure_origin(origin)?;
@@ -677,7 +675,6 @@ pub mod pallet {
/// Remove an announcement.
#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::remove_announcement())]
pub fn remove_announcement(origin: OriginFor<T>, announcement: Cid) -> DispatchResult {
T::AnnouncementOrigin::ensure_origin(origin)?;
@@ -695,7 +692,6 @@ pub mod pallet {
/// Submit oneself for candidacy. A fixed deposit is reserved.
#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::join_alliance())]
pub fn join_alliance(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
@@ -732,7 +728,6 @@ pub mod pallet {
/// A Fellow can nominate someone to join the alliance as an Ally. There is no deposit
/// required from the nominator or nominee.
#[pallet::call_index(9)]
#[pallet::weight(T::WeightInfo::nominate_ally())]
pub fn nominate_ally(origin: OriginFor<T>, who: AccountIdLookupOf<T>) -> DispatchResult {
let nominator = ensure_signed(origin)?;
ensure!(Self::has_voting_rights(&nominator), Error::<T, I>::NoVotingRights);
@@ -757,7 +752,6 @@ pub mod pallet {
/// Elevate an Ally to Fellow.
#[pallet::call_index(10)]
#[pallet::weight(T::WeightInfo::elevate_ally())]
pub fn elevate_ally(origin: OriginFor<T>, ally: AccountIdLookupOf<T>) -> DispatchResult {
T::MembershipManager::ensure_origin(origin)?;
let ally = T::Lookup::lookup(ally)?;
@@ -774,7 +768,6 @@ pub mod pallet {
/// As a member, give a retirement notice and start a retirement period required to pass in
/// order to retire.
#[pallet::call_index(11)]
#[pallet::weight(T::WeightInfo::give_retirement_notice())]
pub fn give_retirement_notice(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let role = Self::member_role_of(&who).ok_or(Error::<T, I>::NotMember)?;
@@ -797,7 +790,6 @@ pub mod pallet {
/// This can only be done once you have called `give_retirement_notice` and the
/// `RetirementPeriod` has passed.
#[pallet::call_index(12)]
#[pallet::weight(T::WeightInfo::retire())]
pub fn retire(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let retirement_period_end = RetiringMembers::<T, I>::get(&who)
@@ -820,7 +812,6 @@ pub mod pallet {
/// Kick a member from the Alliance and slash its deposit.
#[pallet::call_index(13)]
#[pallet::weight(T::WeightInfo::kick_member())]
pub fn kick_member(origin: OriginFor<T>, who: AccountIdLookupOf<T>) -> DispatchResult {
T::MembershipManager::ensure_origin(origin)?;
let member = T::Lookup::lookup(who)?;
@@ -922,7 +913,6 @@ pub mod pallet {
/// who do not want to leave the Alliance but do not have the capacity to participate
/// operationally for some time.
#[pallet::call_index(17)]
#[pallet::weight(T::WeightInfo::abdicate_fellow_status())]
pub fn abdicate_fellow_status(origin: OriginFor<T>) -> DispatchResult {
let who = ensure_signed(origin)?;
let role = Self::member_role_of(&who).ok_or(Error::<T, I>::NotMember)?;
+9
View File
@@ -629,3 +629,12 @@ fn remove_unscrupulous_items_works() {
assert_eq!(Alliance::unscrupulous_accounts(), Vec::<u64>::new());
});
}
#[test]
fn weights_sane() {
let info = crate::Call::<Test>::join_alliance {}.get_dispatch_info();
assert_eq!(<() as crate::WeightInfo>::join_alliance(), info.weight);
let info = crate::Call::<Test>::nominate_ally { who: 10 }.get_dispatch_info();
assert_eq!(<() as crate::WeightInfo>::nominate_ally(), info.weight);
}
+1 -24
View File
@@ -568,7 +568,7 @@ pub mod pallet {
CallbackFailed,
}
#[pallet::call]
#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Issue a new class of fungible assets from a public origin.
///
@@ -590,7 +590,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::create())]
pub fn create(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -654,7 +653,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::force_create())]
pub fn force_create(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -680,7 +678,6 @@ pub mod pallet {
///
/// The asset class must be frozen before calling `start_destroy`.
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::start_destroy())]
pub fn start_destroy(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
let maybe_check_owner = match T::ForceOrigin::try_origin(origin) {
Ok(_) => None,
@@ -749,7 +746,6 @@ pub mod pallet {
///
/// Each successful call emits the `Event::Destroyed` event.
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::finish_destroy())]
pub fn finish_destroy(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
let _ = ensure_signed(origin)?;
let id: T::AssetId = id.into();
@@ -769,7 +765,6 @@ pub mod pallet {
/// Weight: `O(1)`
/// Modes: Pre-existing balance of `beneficiary`; Account pre-existence of `beneficiary`.
#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::mint())]
pub fn mint(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -799,7 +794,6 @@ pub mod pallet {
/// Weight: `O(1)`
/// Modes: Post-existence of `who`; Pre & post Zombie-status of `who`.
#[pallet::call_index(7)]
#[pallet::weight(T::WeightInfo::burn())]
pub fn burn(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -834,7 +828,6 @@ pub mod pallet {
/// Modes: Pre-existence of `target`; Post-existence of sender; Account pre-existence of
/// `target`.
#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::transfer())]
pub fn transfer(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -868,7 +861,6 @@ pub mod pallet {
/// Modes: Pre-existence of `target`; Post-existence of sender; Account pre-existence of
/// `target`.
#[pallet::call_index(9)]
#[pallet::weight(T::WeightInfo::transfer_keep_alive())]
pub fn transfer_keep_alive(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -903,7 +895,6 @@ pub mod pallet {
/// Modes: Pre-existence of `dest`; Post-existence of `source`; Account pre-existence of
/// `dest`.
#[pallet::call_index(10)]
#[pallet::weight(T::WeightInfo::force_transfer())]
pub fn force_transfer(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -931,7 +922,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(11)]
#[pallet::weight(T::WeightInfo::freeze())]
pub fn freeze(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -968,7 +958,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(12)]
#[pallet::weight(T::WeightInfo::thaw())]
pub fn thaw(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -1004,7 +993,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(13)]
#[pallet::weight(T::WeightInfo::freeze_asset())]
pub fn freeze_asset(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
let origin = ensure_signed(origin)?;
let id: T::AssetId = id.into();
@@ -1031,7 +1019,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(14)]
#[pallet::weight(T::WeightInfo::thaw_asset())]
pub fn thaw_asset(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
let origin = ensure_signed(origin)?;
let id: T::AssetId = id.into();
@@ -1059,7 +1046,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(15)]
#[pallet::weight(T::WeightInfo::transfer_ownership())]
pub fn transfer_ownership(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -1103,7 +1089,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(16)]
#[pallet::weight(T::WeightInfo::set_team())]
pub fn set_team(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -1173,7 +1158,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(18)]
#[pallet::weight(T::WeightInfo::clear_metadata())]
pub fn clear_metadata(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
let origin = ensure_signed(origin)?;
let id: T::AssetId = id.into();
@@ -1257,7 +1241,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(20)]
#[pallet::weight(T::WeightInfo::force_clear_metadata())]
pub fn force_clear_metadata(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -1297,7 +1280,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(21)]
#[pallet::weight(T::WeightInfo::force_asset_status())]
pub fn force_asset_status(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -1354,7 +1336,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(22)]
#[pallet::weight(T::WeightInfo::approve_transfer())]
pub fn approve_transfer(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -1381,7 +1362,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(23)]
#[pallet::weight(T::WeightInfo::cancel_approval())]
pub fn cancel_approval(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -1418,7 +1398,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(24)]
#[pallet::weight(T::WeightInfo::force_cancel_approval())]
pub fn force_cancel_approval(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -1468,7 +1447,6 @@ pub mod pallet {
///
/// Weight: `O(1)`
#[pallet::call_index(25)]
#[pallet::weight(T::WeightInfo::transfer_approved())]
pub fn transfer_approved(
origin: OriginFor<T>,
id: T::AssetIdParameter,
@@ -1531,7 +1509,6 @@ pub mod pallet {
///
/// Emits `AssetMinBalanceChanged` event when successful.
#[pallet::call_index(28)]
#[pallet::weight(T::WeightInfo::set_min_balance())]
pub fn set_min_balance(
origin: OriginFor<T>,
id: T::AssetIdParameter,
+10
View File
@@ -21,6 +21,7 @@ use super::*;
use crate::{mock::*, Error};
use frame_support::{
assert_noop, assert_ok,
dispatch::GetDispatchInfo,
traits::{fungibles::InspectEnumerable, tokens::Preservation::Protect, Currency},
};
use pallet_balances::Error as BalancesError;
@@ -1355,3 +1356,12 @@ fn multiple_transfer_alls_work_ok() {
assert_eq!(Balances::free_balance(&1337), 100);
});
}
#[test]
fn weights_sane() {
let info = crate::Call::<Test>::create { id: 10, admin: 4, min_balance: 3 }.get_dispatch_info();
assert_eq!(<() as crate::WeightInfo>::create(), info.weight);
let info = crate::Call::<Test>::finish_destroy { id: 10 }.get_dispatch_info();
assert_eq!(<() as crate::WeightInfo>::finish_destroy(), info.weight);
}
+1 -6
View File
@@ -526,7 +526,7 @@ pub mod pallet {
}
}
#[pallet::call]
#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Transfer some liquid free balance to another account.
///
@@ -536,7 +536,6 @@ pub mod pallet {
///
/// The dispatch origin for this call must be `Signed` by the transactor.
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::transfer_allow_death())]
pub fn transfer_allow_death(
origin: OriginFor<T>,
dest: AccountIdLookupOf<T>,
@@ -598,7 +597,6 @@ pub mod pallet {
/// Exactly as `transfer_allow_death`, except the origin must be root and the source account
/// may be specified.
#[pallet::call_index(2)]
#[pallet::weight(T::WeightInfo::force_transfer())]
pub fn force_transfer(
origin: OriginFor<T>,
source: AccountIdLookupOf<T>,
@@ -619,7 +617,6 @@ pub mod pallet {
///
/// [`transfer_allow_death`]: struct.Pallet.html#method.transfer
#[pallet::call_index(3)]
#[pallet::weight(T::WeightInfo::transfer_keep_alive())]
pub fn transfer_keep_alive(
origin: OriginFor<T>,
dest: AccountIdLookupOf<T>,
@@ -647,7 +644,6 @@ pub mod pallet {
/// transfer everything except at least the existential deposit, which will guarantee to
/// keep the sender account alive (true).
#[pallet::call_index(4)]
#[pallet::weight(T::WeightInfo::transfer_all())]
pub fn transfer_all(
origin: OriginFor<T>,
dest: AccountIdLookupOf<T>,
@@ -674,7 +670,6 @@ pub mod pallet {
///
/// Can only be called by ROOT.
#[pallet::call_index(5)]
#[pallet::weight(T::WeightInfo::force_unreserve())]
pub fn force_unreserve(
origin: OriginFor<T>,
who: AccountIdLookupOf<T>,
+10 -1
View File
@@ -23,7 +23,7 @@ use crate::{self as pallet_balances, AccountData, Config, CreditOf, Error, Palle
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
assert_err, assert_noop, assert_ok, assert_storage_noop,
dispatch::DispatchInfo,
dispatch::{DispatchInfo, GetDispatchInfo},
parameter_types,
traits::{
tokens::fungible, ConstU32, ConstU64, ConstU8, Imbalance as ImbalanceT, OnUnbalanced,
@@ -294,3 +294,12 @@ pub fn events() -> Vec<RuntimeEvent> {
pub fn info_from_weight(w: Weight) -> DispatchInfo {
DispatchInfo { weight: w, ..Default::default() }
}
#[test]
fn weights_sane() {
let info = crate::Call::<Test>::transfer_allow_death { dest: 10, value: 4 }.get_dispatch_info();
assert_eq!(<() as crate::WeightInfo>::transfer_allow_death(), info.weight);
let info = crate::Call::<Test>::force_unreserve { who: 10, amount: 4 }.get_dispatch_info();
assert_eq!(<() as crate::WeightInfo>::force_unreserve(), info.weight);
}
+1 -4
View File
@@ -441,7 +441,7 @@ pub mod pallet {
// against them as the first thing you do in your function. There are three convenience calls
// in system that do the matching for you and return a convenient result: `ensure_signed`,
// `ensure_root` and `ensure_none`.
#[pallet::call]
#[pallet::call(weight(<T as Config>::WeightInfo))]
impl<T: Config> Pallet<T> {
/// This is your public interface. Be extremely careful.
/// This is just a simple example of how to interact with the pallet from the external
@@ -497,9 +497,6 @@ pub mod pallet {
// The weight for this extrinsic we rely on the auto-generated `WeightInfo` from the
// benchmark toolchain.
#[pallet::call_index(0)]
#[pallet::weight(
<T as pallet::Config>::WeightInfo::accumulate_dummy()
)]
pub fn accumulate_dummy(origin: OriginFor<T>, increase_by: T::Balance) -> DispatchResult {
// This is a public call, so we ensure that the origin is some signed account.
let _sender = ensure_signed(origin)?;
@@ -192,6 +192,7 @@ fn weights_work() {
// aka. `let info = <Call<Test> as GetDispatchInfo>::get_dispatch_info(&default_call);`
// TODO: account for proof size weight
assert!(info1.weight.ref_time() > 0);
assert_eq!(info1.weight, <Test as Config>::WeightInfo::accumulate_dummy());
// `set_dummy` is simpler than `accumulate_dummy`, and the weight
// should be less.
@@ -15,8 +15,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{pallet::Def, COUNTER};
use quote::ToTokens;
use crate::{
pallet::{parse::call::CallWeightDef, Def},
COUNTER,
};
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, ToTokens};
use syn::spanned::Spanned;
///
@@ -74,15 +78,12 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
call_index_warnings.push(warning);
}
let fn_weight = methods.iter().map(|method| &method.weight);
let mut fn_weight = Vec::<TokenStream2>::new();
let mut weight_warnings = Vec::new();
for weight in fn_weight.clone() {
if def.dev_mode {
continue
}
match weight {
syn::Expr::Lit(lit) => {
for method in &methods {
match &method.weight {
CallWeightDef::DevModeDefault => fn_weight.push(syn::parse_quote!(0)),
CallWeightDef::Immediate(e @ syn::Expr::Lit(lit)) if !def.dev_mode => {
let warning = proc_macro_warning::Warning::new_deprecated("ConstantWeight")
.index(weight_warnings.len())
.old("use hard-coded constant as call weight")
@@ -91,10 +92,26 @@ pub fn expand_call(def: &mut Def) -> proc_macro2::TokenStream {
.span(lit.span())
.build();
weight_warnings.push(warning);
fn_weight.push(e.into_token_stream());
},
CallWeightDef::Immediate(e) => fn_weight.push(e.into_token_stream()),
CallWeightDef::Inherited => {
let pallet_weight = def
.call
.as_ref()
.expect("we have methods; we have calls; qed")
.inherited_call_weight
.as_ref()
.expect("the parser prevents this");
// Expand `<<T as Config>::WeightInfo>::call_name()`.
let t = &pallet_weight.typename;
let n = &method.name;
fn_weight.push(quote!({ < #t > :: #n () }));
},
_ => {},
}
}
debug_assert_eq!(fn_weight.len(), methods.len());
let fn_doc = methods.iter().map(|method| &method.docs).collect::<Vec<_>>();
@@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use super::helper;
use super::{helper, InheritedCallWeightAttr};
use frame_support_procedural_tools::get_doc_literals;
use quote::ToTokens;
use std::collections::HashMap;
@@ -46,6 +46,23 @@ pub struct CallDef {
pub attr_span: proc_macro2::Span,
/// Docs, specified on the impl Block.
pub docs: Vec<syn::Expr>,
/// The optional `weight` attribute on the `pallet::call`.
pub inherited_call_weight: Option<InheritedCallWeightAttr>,
}
/// The weight of a call.
#[derive(Clone)]
pub enum CallWeightDef {
/// Explicitly set on the call itself with `#[pallet::weight(…)]`. This value is used.
Immediate(syn::Expr),
/// The default value that should be set for dev-mode pallets. Usually zero.
DevModeDefault,
/// Inherits whatever value is configured on the pallet level.
///
/// The concrete value is not known at this point.
Inherited,
}
/// Definition of dispatchable typically: `#[weight...] fn foo(origin .., param1: ...) -> ..`
@@ -55,8 +72,8 @@ pub struct CallVariantDef {
pub name: syn::Ident,
/// Information on args: `(is_compact, name, type)`
pub args: Vec<(bool, syn::Ident, Box<syn::Type>)>,
/// Weight formula.
pub weight: syn::Expr,
/// Weight for the call.
pub weight: CallWeightDef,
/// Call index of the dispatchable.
pub call_index: u8,
/// Whether an explicit call index was specified.
@@ -151,6 +168,7 @@ impl CallDef {
index: usize,
item: &mut syn::Item,
dev_mode: bool,
inherited_call_weight: Option<InheritedCallWeightAttr>,
) -> syn::Result<Self> {
let item_impl = if let syn::Item::Impl(item) = item {
item
@@ -228,17 +246,23 @@ impl CallDef {
weight_attrs.push(FunctionAttr::Weight(empty_weight));
}
if weight_attrs.len() != 1 {
let msg = if weight_attrs.is_empty() {
"Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]`"
} else {
"Invalid pallet::call, too many weight attributes given"
};
return Err(syn::Error::new(method.sig.span(), msg))
}
let weight = match weight_attrs.pop().unwrap() {
FunctionAttr::Weight(w) => w,
_ => unreachable!("checked during creation of the let binding"),
let weight = match weight_attrs.len() {
0 if inherited_call_weight.is_some() => CallWeightDef::Inherited,
0 if dev_mode => CallWeightDef::DevModeDefault,
0 => return Err(syn::Error::new(
method.sig.span(),
"A pallet::call requires either a concrete `#[pallet::weight($expr)]` or an
inherited weight from the `#[pallet:call(weight($type))]` attribute, but
none were given.",
)),
1 => match weight_attrs.pop().unwrap() {
FunctionAttr::Weight(w) => CallWeightDef::Immediate(w),
_ => unreachable!("checked during creation of the let binding"),
},
_ => {
let msg = "Invalid pallet::call, too many weight attributes given";
return Err(syn::Error::new(method.sig.span(), msg))
},
};
if call_idx_attrs.len() > 1 {
@@ -321,6 +345,7 @@ impl CallDef {
methods,
where_clause: item_impl.generics.where_clause.clone(),
docs: get_doc_literals(&item_impl.attrs),
inherited_call_weight,
})
}
}
@@ -110,8 +110,8 @@ impl Def {
let m = hooks::HooksDef::try_from(span, index, item)?;
hooks = Some(m);
},
Some(PalletAttr::RuntimeCall(span)) if call.is_none() =>
call = Some(call::CallDef::try_from(span, index, item, dev_mode)?),
Some(PalletAttr::RuntimeCall(cw, span)) if call.is_none() =>
call = Some(call::CallDef::try_from(span, index, item, dev_mode, cw)?),
Some(PalletAttr::Error(span)) if error.is_none() =>
error = Some(error::ErrorDef::try_from(span, index, item)?),
Some(PalletAttr::RuntimeEvent(span)) if event.is_none() =>
@@ -402,6 +402,7 @@ impl GenericKind {
mod keyword {
syn::custom_keyword!(origin);
syn::custom_keyword!(call);
syn::custom_keyword!(weight);
syn::custom_keyword!(event);
syn::custom_keyword!(config);
syn::custom_keyword!(hooks);
@@ -425,7 +426,44 @@ enum PalletAttr {
Config(proc_macro2::Span),
Pallet(proc_macro2::Span),
Hooks(proc_macro2::Span),
RuntimeCall(proc_macro2::Span),
/// A `#[pallet::call]` with optional attributes to specialize the behaviour.
///
/// # Attributes
///
/// Each attribute `attr` can take the form of `#[pallet::call(attr = …)]` or
/// `#[pallet::call(attr(…))]`. The possible attributes are:
///
/// ## `weight`
///
/// Can be used to reduce the repetitive weight annotation in the trivial case. It accepts one
/// argument that is expected to be an implementation of the `WeightInfo` or something that
/// behaves syntactically equivalent. This allows to annotate a `WeightInfo` for all the calls.
/// Now each call does not need to specify its own `#[pallet::weight]` but can instead use the
/// one from the `#[pallet::call]` definition. So instead of having to write it on each call:
///
/// ```ignore
/// #[pallet::call]
/// impl<T: Config> Pallet<T> {
/// #[pallet::weight(T::WeightInfo::create())]
/// pub fn create(
/// ```
/// you can now omit it on the call itself, if the name of the weigh function matches the call:
///
/// ```ignore
/// #[pallet::call(weight = <T as crate::Config>::WeightInfo)]
/// impl<T: Config> Pallet<T> {
/// pub fn create(
/// ```
///
/// It is possible to use this syntax together with instantiated pallets by using `Config<I>`
/// instead.
///
/// ### Dev Mode
///
/// Normally the `dev_mode` sets all weights of calls without a `#[pallet::weight]` annotation
/// to zero. Now when there is a `weight` attribute on the `#[pallet::call]`, then that is used
/// instead of the zero weight. So to say: it works together with `dev_mode`.
RuntimeCall(Option<InheritedCallWeightAttr>, proc_macro2::Span),
Error(proc_macro2::Span),
RuntimeEvent(proc_macro2::Span),
RuntimeOrigin(proc_macro2::Span),
@@ -445,7 +483,7 @@ impl PalletAttr {
Self::Config(span) => *span,
Self::Pallet(span) => *span,
Self::Hooks(span) => *span,
Self::RuntimeCall(span) => *span,
Self::RuntimeCall(_, span) => *span,
Self::Error(span) => *span,
Self::RuntimeEvent(span) => *span,
Self::RuntimeOrigin(span) => *span,
@@ -477,7 +515,12 @@ impl syn::parse::Parse for PalletAttr {
} else if lookahead.peek(keyword::hooks) {
Ok(PalletAttr::Hooks(content.parse::<keyword::hooks>()?.span()))
} else if lookahead.peek(keyword::call) {
Ok(PalletAttr::RuntimeCall(content.parse::<keyword::call>()?.span()))
let span = content.parse::<keyword::call>().expect("peeked").span();
let attr = match content.is_empty() {
true => None,
false => Some(InheritedCallWeightAttr::parse(&content)?),
};
Ok(PalletAttr::RuntimeCall(attr, span))
} else if lookahead.peek(keyword::error) {
Ok(PalletAttr::Error(content.parse::<keyword::error>()?.span()))
} else if lookahead.peek(keyword::event) {
@@ -505,3 +548,33 @@ impl syn::parse::Parse for PalletAttr {
}
}
}
/// The optional weight annotation on a `#[pallet::call]` like `#[pallet::call(weight($type))]`.
#[derive(Clone)]
pub struct InheritedCallWeightAttr {
pub typename: syn::Type,
pub span: proc_macro2::Span,
}
impl syn::parse::Parse for InheritedCallWeightAttr {
// Parses `(weight($type))` or `(weight = $type)`.
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let content;
syn::parenthesized!(content in input);
content.parse::<keyword::weight>()?;
let lookahead = content.lookahead1();
let buffer = if lookahead.peek(syn::token::Paren) {
let inner;
syn::parenthesized!(inner in content);
inner
} else if lookahead.peek(syn::Token![=]) {
content.parse::<syn::Token![=]>().expect("peeked");
content
} else {
return Err(lookahead.error())
};
Ok(Self { typename: buffer.parse()?, span: input.span() })
}
}
@@ -61,8 +61,8 @@ pub enum PalletStructAttr {
impl PalletStructAttr {
fn span(&self) -> proc_macro2::Span {
match self {
Self::GenerateStore { span, .. } => *span,
Self::WithoutStorageInfoTrait(span) => *span,
Self::GenerateStore { span, .. } |
Self::WithoutStorageInfoTrait(span) |
Self::StorageVersion { span, .. } => *span,
}
}
+1 -1
View File
@@ -2600,7 +2600,7 @@ pub mod pallet_prelude {
/// #[pallet::extra_constants]
/// impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// /// Some description
/// fn exra_constant_name() -> u128 { 4u128 }
/// fn extra_constant_name() -> u128 { 4u128 }
/// }
///
/// #[pallet::pallet]
@@ -1,5 +1,7 @@
error: Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]`
--> $DIR/call_missing_weight.rs:17:7
error: A pallet::call requires either a concrete `#[pallet::weight($expr)]` or an
inherited weight from the `#[pallet:call(weight($type))]` attribute, but
none were given.
--> tests/pallet_ui/call_missing_weight.rs:17:7
|
17 | pub fn foo(origin: OriginFor<T>) -> DispatchResultWithPostInfo {}
| ^^
@@ -0,0 +1,50 @@
use frame_support::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet]
mod pallet {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(invalid)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call = invalid]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,11 @@
error: expected `weight`
--> tests/pallet_ui/call_weight_inherited_invalid.rs:19:17
|
19 | #[pallet::call(invalid)]
| ^^^^^^^
error: expected parentheses
--> tests/pallet_ui/call_weight_inherited_invalid.rs:40:17
|
40 | #[pallet::call = invalid]
| ^
@@ -0,0 +1,53 @@
// Weight is an ident instead of a type.
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(prefix))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = prefix)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,11 @@
error[E0412]: cannot find type `prefix` in this scope
--> tests/pallet_ui/call_weight_inherited_invalid2.rs:22:24
|
22 | #[pallet::call(weight(prefix))]
| ^^^^^^ not found in this scope
error[E0412]: cannot find type `prefix` in this scope
--> tests/pallet_ui/call_weight_inherited_invalid2.rs:43:26
|
43 | #[pallet::call(weight = prefix)]
| ^^^^^^ not found in this scope
@@ -0,0 +1,53 @@
// Call weight is an LitInt instead of a type.
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(123))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = 123)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,19 @@
error: expected one of: `for`, parentheses, `fn`, `unsafe`, `extern`, identifier, `::`, `<`, `dyn`, square brackets, `*`, `&`, `!`, `impl`, `_`, lifetime
--> tests/pallet_ui/call_weight_inherited_invalid3.rs:22:24
|
22 | #[pallet::call(weight(123))]
| ^^^
error: expected one of: `for`, parentheses, `fn`, `unsafe`, `extern`, identifier, `::`, `<`, `dyn`, square brackets, `*`, `&`, `!`, `impl`, `_`, lifetime
--> tests/pallet_ui/call_weight_inherited_invalid3.rs:43:26
|
43 | #[pallet::call(weight = 123)]
| ^^^
error: unused import: `frame_system::pallet_prelude::*`
--> tests/pallet_ui/call_weight_inherited_invalid3.rs:4:5
|
4 | use frame_system::pallet_prelude::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D unused-imports` implied by `-D warnings`
@@ -0,0 +1,52 @@
// Function does not exist in the trait.
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(<T as Config>::WeightInfo))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = <T as Config>::WeightInfo)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,11 @@
error[E0599]: no function or associated item named `foo` found for associated type `<T as Config>::WeightInfo` in the current scope
--> tests/pallet_ui/call_weight_inherited_invalid4.rs:24:10
|
24 | pub fn foo(_: OriginFor<T>) -> DispatchResult {
| ^^^ function or associated item not found in `<T as Config>::WeightInfo`
error[E0599]: no function or associated item named `foo` found for associated type `<T as Config>::WeightInfo` in the current scope
--> tests/pallet_ui/call_weight_inherited_invalid4.rs:45:10
|
45 | pub fn foo(_: OriginFor<T>) -> DispatchResult {
| ^^^ function or associated item not found in `<T as Config>::WeightInfo`
@@ -0,0 +1,44 @@
// Stray tokens after good input.
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(<T as Config>::WeightInfo straycat))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = <T as Config>::WeightInfo straycat)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,11 @@
error: unexpected token
--> tests/pallet_ui/call_weight_inherited_invalid5.rs:14:50
|
14 | #[pallet::call(weight(<T as Config>::WeightInfo straycat))]
| ^^^^^^^^
error: unexpected token
--> tests/pallet_ui/call_weight_inherited_invalid5.rs:34:52
|
34 | #[pallet::call(weight = <T as Config>::WeightInfo straycat)]
| ^^^^^^^^
@@ -1,7 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)]
pub use pallet::*;
#[frame_support::pallet]
pub mod pallet {
use frame_support::pallet_prelude::*;
@@ -1,11 +1,7 @@
error: Invalid pallet::call, requires weight attribute i.e. `#[pallet::weight($expr)]`
--> tests/pallet_ui/dev_mode_without_arg.rs:24:7
error: A pallet::call requires either a concrete `#[pallet::weight($expr)]` or an
inherited weight from the `#[pallet:call(weight($type))]` attribute, but
none were given.
--> tests/pallet_ui/dev_mode_without_arg.rs:22:7
|
24 | pub fn my_call(_origin: OriginFor<T>) -> DispatchResult {
22 | pub fn my_call(_origin: OriginFor<T>) -> DispatchResult {
| ^^
error[E0432]: unresolved import `pallet`
--> tests/pallet_ui/dev_mode_without_arg.rs:3:9
|
3 | pub use pallet::*;
| ^^^^^^ help: a similar path exists: `test_pallet::pallet`
@@ -0,0 +1,51 @@
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(<T as Config>::WeightInfo))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: crate::WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = <T as Config>::WeightInfo)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,57 @@
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
impl WeightInfo for () {
fn foo() -> Weight {
Weight::zero()
}
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
// Crazy man just uses `()`, but it still works ;)
#[pallet::call(weight(()))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
// Crazy man just uses `()`, but it still works ;)
#[pallet::call(weight = ())]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,54 @@
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
// If, for whatever reason, you dont to not use a `WeightInfo` trait - it will still work.
struct Impl;
impl Impl {
fn foo() -> Weight {
Weight::zero()
}
}
#[frame_support::pallet]
mod parentheses {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(crate::Impl))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
#[frame_support::pallet]
mod assign {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight = crate::Impl)]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}
@@ -0,0 +1,30 @@
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
pub trait WeightInfo {
fn foo() -> Weight;
}
#[frame_support::pallet(dev_mode)]
mod pallet {
use super::*;
#[pallet::config]
pub trait Config: frame_system::Config {
type WeightInfo: WeightInfo;
}
#[pallet::pallet]
pub struct Pallet<T>(core::marker::PhantomData<T>);
#[pallet::call(weight(<T as Config>::WeightInfo))]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
pub fn foo(_: OriginFor<T>) -> DispatchResult {
Ok(())
}
}
}
fn main() {
}