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

Commit a6aa272

Browse files
ggwpezmuharem
authored andcommitted
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 331d4b4. 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>
1 parent a5c6264 commit a6aa272

30 files changed

Lines changed: 698 additions & 92 deletions

frame/alliance/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ pub mod pallet {
497497
pub type UnscrupulousWebsites<T: Config<I>, I: 'static = ()> =
498498
StorageValue<_, BoundedVec<UrlOf<T, I>, T::MaxUnscrupulousItems>, ValueQuery>;
499499

500-
#[pallet::call]
500+
#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
501501
impl<T: Config<I>, I: 'static> Pallet<T, I> {
502502
/// Add a new proposal to be voted on.
503503
///
@@ -649,7 +649,6 @@ pub mod pallet {
649649

650650
/// Set a new IPFS CID to the alliance rule.
651651
#[pallet::call_index(5)]
652-
#[pallet::weight(T::WeightInfo::set_rule())]
653652
pub fn set_rule(origin: OriginFor<T>, rule: Cid) -> DispatchResult {
654653
T::AdminOrigin::ensure_origin(origin)?;
655654

@@ -661,7 +660,6 @@ pub mod pallet {
661660

662661
/// Make an announcement of a new IPFS CID about alliance issues.
663662
#[pallet::call_index(6)]
664-
#[pallet::weight(T::WeightInfo::announce())]
665663
pub fn announce(origin: OriginFor<T>, announcement: Cid) -> DispatchResult {
666664
T::AnnouncementOrigin::ensure_origin(origin)?;
667665

@@ -677,7 +675,6 @@ pub mod pallet {
677675

678676
/// Remove an announcement.
679677
#[pallet::call_index(7)]
680-
#[pallet::weight(T::WeightInfo::remove_announcement())]
681678
pub fn remove_announcement(origin: OriginFor<T>, announcement: Cid) -> DispatchResult {
682679
T::AnnouncementOrigin::ensure_origin(origin)?;
683680

@@ -695,7 +692,6 @@ pub mod pallet {
695692

696693
/// Submit oneself for candidacy. A fixed deposit is reserved.
697694
#[pallet::call_index(8)]
698-
#[pallet::weight(T::WeightInfo::join_alliance())]
699695
pub fn join_alliance(origin: OriginFor<T>) -> DispatchResult {
700696
let who = ensure_signed(origin)?;
701697

@@ -732,7 +728,6 @@ pub mod pallet {
732728
/// A Fellow can nominate someone to join the alliance as an Ally. There is no deposit
733729
/// required from the nominator or nominee.
734730
#[pallet::call_index(9)]
735-
#[pallet::weight(T::WeightInfo::nominate_ally())]
736731
pub fn nominate_ally(origin: OriginFor<T>, who: AccountIdLookupOf<T>) -> DispatchResult {
737732
let nominator = ensure_signed(origin)?;
738733
ensure!(Self::has_voting_rights(&nominator), Error::<T, I>::NoVotingRights);
@@ -757,7 +752,6 @@ pub mod pallet {
757752

758753
/// Elevate an Ally to Fellow.
759754
#[pallet::call_index(10)]
760-
#[pallet::weight(T::WeightInfo::elevate_ally())]
761755
pub fn elevate_ally(origin: OriginFor<T>, ally: AccountIdLookupOf<T>) -> DispatchResult {
762756
T::MembershipManager::ensure_origin(origin)?;
763757
let ally = T::Lookup::lookup(ally)?;
@@ -774,7 +768,6 @@ pub mod pallet {
774768
/// As a member, give a retirement notice and start a retirement period required to pass in
775769
/// order to retire.
776770
#[pallet::call_index(11)]
777-
#[pallet::weight(T::WeightInfo::give_retirement_notice())]
778771
pub fn give_retirement_notice(origin: OriginFor<T>) -> DispatchResult {
779772
let who = ensure_signed(origin)?;
780773
let role = Self::member_role_of(&who).ok_or(Error::<T, I>::NotMember)?;
@@ -797,7 +790,6 @@ pub mod pallet {
797790
/// This can only be done once you have called `give_retirement_notice` and the
798791
/// `RetirementPeriod` has passed.
799792
#[pallet::call_index(12)]
800-
#[pallet::weight(T::WeightInfo::retire())]
801793
pub fn retire(origin: OriginFor<T>) -> DispatchResult {
802794
let who = ensure_signed(origin)?;
803795
let retirement_period_end = RetiringMembers::<T, I>::get(&who)
@@ -820,7 +812,6 @@ pub mod pallet {
820812

821813
/// Kick a member from the Alliance and slash its deposit.
822814
#[pallet::call_index(13)]
823-
#[pallet::weight(T::WeightInfo::kick_member())]
824815
pub fn kick_member(origin: OriginFor<T>, who: AccountIdLookupOf<T>) -> DispatchResult {
825816
T::MembershipManager::ensure_origin(origin)?;
826817
let member = T::Lookup::lookup(who)?;
@@ -922,7 +913,6 @@ pub mod pallet {
922913
/// who do not want to leave the Alliance but do not have the capacity to participate
923914
/// operationally for some time.
924915
#[pallet::call_index(17)]
925-
#[pallet::weight(T::WeightInfo::abdicate_fellow_status())]
926916
pub fn abdicate_fellow_status(origin: OriginFor<T>) -> DispatchResult {
927917
let who = ensure_signed(origin)?;
928918
let role = Self::member_role_of(&who).ok_or(Error::<T, I>::NotMember)?;

frame/alliance/src/tests.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,3 +629,12 @@ fn remove_unscrupulous_items_works() {
629629
assert_eq!(Alliance::unscrupulous_accounts(), Vec::<u64>::new());
630630
});
631631
}
632+
633+
#[test]
634+
fn weights_sane() {
635+
let info = crate::Call::<Test>::join_alliance {}.get_dispatch_info();
636+
assert_eq!(<() as crate::WeightInfo>::join_alliance(), info.weight);
637+
638+
let info = crate::Call::<Test>::nominate_ally { who: 10 }.get_dispatch_info();
639+
assert_eq!(<() as crate::WeightInfo>::nominate_ally(), info.weight);
640+
}

frame/assets/src/lib.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ pub mod pallet {
568568
CallbackFailed,
569569
}
570570

571-
#[pallet::call]
571+
#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
572572
impl<T: Config<I>, I: 'static> Pallet<T, I> {
573573
/// Issue a new class of fungible assets from a public origin.
574574
///
@@ -590,7 +590,6 @@ pub mod pallet {
590590
///
591591
/// Weight: `O(1)`
592592
#[pallet::call_index(0)]
593-
#[pallet::weight(T::WeightInfo::create())]
594593
pub fn create(
595594
origin: OriginFor<T>,
596595
id: T::AssetIdParameter,
@@ -654,7 +653,6 @@ pub mod pallet {
654653
///
655654
/// Weight: `O(1)`
656655
#[pallet::call_index(1)]
657-
#[pallet::weight(T::WeightInfo::force_create())]
658656
pub fn force_create(
659657
origin: OriginFor<T>,
660658
id: T::AssetIdParameter,
@@ -680,7 +678,6 @@ pub mod pallet {
680678
///
681679
/// The asset class must be frozen before calling `start_destroy`.
682680
#[pallet::call_index(2)]
683-
#[pallet::weight(T::WeightInfo::start_destroy())]
684681
pub fn start_destroy(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
685682
let maybe_check_owner = match T::ForceOrigin::try_origin(origin) {
686683
Ok(_) => None,
@@ -749,7 +746,6 @@ pub mod pallet {
749746
///
750747
/// Each successful call emits the `Event::Destroyed` event.
751748
#[pallet::call_index(5)]
752-
#[pallet::weight(T::WeightInfo::finish_destroy())]
753749
pub fn finish_destroy(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
754750
let _ = ensure_signed(origin)?;
755751
let id: T::AssetId = id.into();
@@ -769,7 +765,6 @@ pub mod pallet {
769765
/// Weight: `O(1)`
770766
/// Modes: Pre-existing balance of `beneficiary`; Account pre-existence of `beneficiary`.
771767
#[pallet::call_index(6)]
772-
#[pallet::weight(T::WeightInfo::mint())]
773768
pub fn mint(
774769
origin: OriginFor<T>,
775770
id: T::AssetIdParameter,
@@ -799,7 +794,6 @@ pub mod pallet {
799794
/// Weight: `O(1)`
800795
/// Modes: Post-existence of `who`; Pre & post Zombie-status of `who`.
801796
#[pallet::call_index(7)]
802-
#[pallet::weight(T::WeightInfo::burn())]
803797
pub fn burn(
804798
origin: OriginFor<T>,
805799
id: T::AssetIdParameter,
@@ -834,7 +828,6 @@ pub mod pallet {
834828
/// Modes: Pre-existence of `target`; Post-existence of sender; Account pre-existence of
835829
/// `target`.
836830
#[pallet::call_index(8)]
837-
#[pallet::weight(T::WeightInfo::transfer())]
838831
pub fn transfer(
839832
origin: OriginFor<T>,
840833
id: T::AssetIdParameter,
@@ -868,7 +861,6 @@ pub mod pallet {
868861
/// Modes: Pre-existence of `target`; Post-existence of sender; Account pre-existence of
869862
/// `target`.
870863
#[pallet::call_index(9)]
871-
#[pallet::weight(T::WeightInfo::transfer_keep_alive())]
872864
pub fn transfer_keep_alive(
873865
origin: OriginFor<T>,
874866
id: T::AssetIdParameter,
@@ -903,7 +895,6 @@ pub mod pallet {
903895
/// Modes: Pre-existence of `dest`; Post-existence of `source`; Account pre-existence of
904896
/// `dest`.
905897
#[pallet::call_index(10)]
906-
#[pallet::weight(T::WeightInfo::force_transfer())]
907898
pub fn force_transfer(
908899
origin: OriginFor<T>,
909900
id: T::AssetIdParameter,
@@ -931,7 +922,6 @@ pub mod pallet {
931922
///
932923
/// Weight: `O(1)`
933924
#[pallet::call_index(11)]
934-
#[pallet::weight(T::WeightInfo::freeze())]
935925
pub fn freeze(
936926
origin: OriginFor<T>,
937927
id: T::AssetIdParameter,
@@ -968,7 +958,6 @@ pub mod pallet {
968958
///
969959
/// Weight: `O(1)`
970960
#[pallet::call_index(12)]
971-
#[pallet::weight(T::WeightInfo::thaw())]
972961
pub fn thaw(
973962
origin: OriginFor<T>,
974963
id: T::AssetIdParameter,
@@ -1004,7 +993,6 @@ pub mod pallet {
1004993
///
1005994
/// Weight: `O(1)`
1006995
#[pallet::call_index(13)]
1007-
#[pallet::weight(T::WeightInfo::freeze_asset())]
1008996
pub fn freeze_asset(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
1009997
let origin = ensure_signed(origin)?;
1010998
let id: T::AssetId = id.into();
@@ -1031,7 +1019,6 @@ pub mod pallet {
10311019
///
10321020
/// Weight: `O(1)`
10331021
#[pallet::call_index(14)]
1034-
#[pallet::weight(T::WeightInfo::thaw_asset())]
10351022
pub fn thaw_asset(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
10361023
let origin = ensure_signed(origin)?;
10371024
let id: T::AssetId = id.into();
@@ -1059,7 +1046,6 @@ pub mod pallet {
10591046
///
10601047
/// Weight: `O(1)`
10611048
#[pallet::call_index(15)]
1062-
#[pallet::weight(T::WeightInfo::transfer_ownership())]
10631049
pub fn transfer_ownership(
10641050
origin: OriginFor<T>,
10651051
id: T::AssetIdParameter,
@@ -1103,7 +1089,6 @@ pub mod pallet {
11031089
///
11041090
/// Weight: `O(1)`
11051091
#[pallet::call_index(16)]
1106-
#[pallet::weight(T::WeightInfo::set_team())]
11071092
pub fn set_team(
11081093
origin: OriginFor<T>,
11091094
id: T::AssetIdParameter,
@@ -1173,7 +1158,6 @@ pub mod pallet {
11731158
///
11741159
/// Weight: `O(1)`
11751160
#[pallet::call_index(18)]
1176-
#[pallet::weight(T::WeightInfo::clear_metadata())]
11771161
pub fn clear_metadata(origin: OriginFor<T>, id: T::AssetIdParameter) -> DispatchResult {
11781162
let origin = ensure_signed(origin)?;
11791163
let id: T::AssetId = id.into();
@@ -1257,7 +1241,6 @@ pub mod pallet {
12571241
///
12581242
/// Weight: `O(1)`
12591243
#[pallet::call_index(20)]
1260-
#[pallet::weight(T::WeightInfo::force_clear_metadata())]
12611244
pub fn force_clear_metadata(
12621245
origin: OriginFor<T>,
12631246
id: T::AssetIdParameter,
@@ -1297,7 +1280,6 @@ pub mod pallet {
12971280
///
12981281
/// Weight: `O(1)`
12991282
#[pallet::call_index(21)]
1300-
#[pallet::weight(T::WeightInfo::force_asset_status())]
13011283
pub fn force_asset_status(
13021284
origin: OriginFor<T>,
13031285
id: T::AssetIdParameter,
@@ -1354,7 +1336,6 @@ pub mod pallet {
13541336
///
13551337
/// Weight: `O(1)`
13561338
#[pallet::call_index(22)]
1357-
#[pallet::weight(T::WeightInfo::approve_transfer())]
13581339
pub fn approve_transfer(
13591340
origin: OriginFor<T>,
13601341
id: T::AssetIdParameter,
@@ -1381,7 +1362,6 @@ pub mod pallet {
13811362
///
13821363
/// Weight: `O(1)`
13831364
#[pallet::call_index(23)]
1384-
#[pallet::weight(T::WeightInfo::cancel_approval())]
13851365
pub fn cancel_approval(
13861366
origin: OriginFor<T>,
13871367
id: T::AssetIdParameter,
@@ -1418,7 +1398,6 @@ pub mod pallet {
14181398
///
14191399
/// Weight: `O(1)`
14201400
#[pallet::call_index(24)]
1421-
#[pallet::weight(T::WeightInfo::force_cancel_approval())]
14221401
pub fn force_cancel_approval(
14231402
origin: OriginFor<T>,
14241403
id: T::AssetIdParameter,
@@ -1468,7 +1447,6 @@ pub mod pallet {
14681447
///
14691448
/// Weight: `O(1)`
14701449
#[pallet::call_index(25)]
1471-
#[pallet::weight(T::WeightInfo::transfer_approved())]
14721450
pub fn transfer_approved(
14731451
origin: OriginFor<T>,
14741452
id: T::AssetIdParameter,
@@ -1531,7 +1509,6 @@ pub mod pallet {
15311509
///
15321510
/// Emits `AssetMinBalanceChanged` event when successful.
15331511
#[pallet::call_index(28)]
1534-
#[pallet::weight(T::WeightInfo::set_min_balance())]
15351512
pub fn set_min_balance(
15361513
origin: OriginFor<T>,
15371514
id: T::AssetIdParameter,

frame/assets/src/tests.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use super::*;
2121
use crate::{mock::*, Error};
2222
use frame_support::{
2323
assert_noop, assert_ok,
24+
dispatch::GetDispatchInfo,
2425
traits::{fungibles::InspectEnumerable, tokens::Preservation::Protect, Currency},
2526
};
2627
use pallet_balances::Error as BalancesError;
@@ -1355,3 +1356,12 @@ fn multiple_transfer_alls_work_ok() {
13551356
assert_eq!(Balances::free_balance(&1337), 100);
13561357
});
13571358
}
1359+
1360+
#[test]
1361+
fn weights_sane() {
1362+
let info = crate::Call::<Test>::create { id: 10, admin: 4, min_balance: 3 }.get_dispatch_info();
1363+
assert_eq!(<() as crate::WeightInfo>::create(), info.weight);
1364+
1365+
let info = crate::Call::<Test>::finish_destroy { id: 10 }.get_dispatch_info();
1366+
assert_eq!(<() as crate::WeightInfo>::finish_destroy(), info.weight);
1367+
}

frame/balances/src/lib.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ pub mod pallet {
526526
}
527527
}
528528

529-
#[pallet::call]
529+
#[pallet::call(weight(<T as Config<I>>::WeightInfo))]
530530
impl<T: Config<I>, I: 'static> Pallet<T, I> {
531531
/// Transfer some liquid free balance to another account.
532532
///
@@ -536,7 +536,6 @@ pub mod pallet {
536536
///
537537
/// The dispatch origin for this call must be `Signed` by the transactor.
538538
#[pallet::call_index(0)]
539-
#[pallet::weight(T::WeightInfo::transfer_allow_death())]
540539
pub fn transfer_allow_death(
541540
origin: OriginFor<T>,
542541
dest: AccountIdLookupOf<T>,
@@ -598,7 +597,6 @@ pub mod pallet {
598597
/// Exactly as `transfer_allow_death`, except the origin must be root and the source account
599598
/// may be specified.
600599
#[pallet::call_index(2)]
601-
#[pallet::weight(T::WeightInfo::force_transfer())]
602600
pub fn force_transfer(
603601
origin: OriginFor<T>,
604602
source: AccountIdLookupOf<T>,
@@ -619,7 +617,6 @@ pub mod pallet {
619617
///
620618
/// [`transfer_allow_death`]: struct.Pallet.html#method.transfer
621619
#[pallet::call_index(3)]
622-
#[pallet::weight(T::WeightInfo::transfer_keep_alive())]
623620
pub fn transfer_keep_alive(
624621
origin: OriginFor<T>,
625622
dest: AccountIdLookupOf<T>,
@@ -647,7 +644,6 @@ pub mod pallet {
647644
/// transfer everything except at least the existential deposit, which will guarantee to
648645
/// keep the sender account alive (true).
649646
#[pallet::call_index(4)]
650-
#[pallet::weight(T::WeightInfo::transfer_all())]
651647
pub fn transfer_all(
652648
origin: OriginFor<T>,
653649
dest: AccountIdLookupOf<T>,
@@ -674,7 +670,6 @@ pub mod pallet {
674670
///
675671
/// Can only be called by ROOT.
676672
#[pallet::call_index(5)]
677-
#[pallet::weight(T::WeightInfo::force_unreserve())]
678673
pub fn force_unreserve(
679674
origin: OriginFor<T>,
680675
who: AccountIdLookupOf<T>,

frame/balances/src/tests/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{self as pallet_balances, AccountData, Config, CreditOf, Error, Palle
2323
use codec::{Decode, Encode, MaxEncodedLen};
2424
use frame_support::{
2525
assert_err, assert_noop, assert_ok, assert_storage_noop,
26-
dispatch::DispatchInfo,
26+
dispatch::{DispatchInfo, GetDispatchInfo},
2727
parameter_types,
2828
traits::{
2929
tokens::fungible, ConstU32, ConstU64, ConstU8, Imbalance as ImbalanceT, OnUnbalanced,
@@ -294,3 +294,12 @@ pub fn events() -> Vec<RuntimeEvent> {
294294
pub fn info_from_weight(w: Weight) -> DispatchInfo {
295295
DispatchInfo { weight: w, ..Default::default() }
296296
}
297+
298+
#[test]
299+
fn weights_sane() {
300+
let info = crate::Call::<Test>::transfer_allow_death { dest: 10, value: 4 }.get_dispatch_info();
301+
assert_eq!(<() as crate::WeightInfo>::transfer_allow_death(), info.weight);
302+
303+
let info = crate::Call::<Test>::force_unreserve { who: 10, amount: 4 }.get_dispatch_info();
304+
assert_eq!(<() as crate::WeightInfo>::force_unreserve(), info.weight);
305+
}

0 commit comments

Comments
 (0)