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

Commit 17c07af

Browse files
KiChjangggwpezathei
authored
Add storage size component to weights (#12277)
* Add storage size component to weights * Rename storage_size to proof_size * Update primitives/weights/src/weight_v2.rs Co-authored-by: Oliver Tale-Yazdi <[email protected]> * Fixes * cargo fmt * Implement custom Decode and CompactAs * Add missing import * Fixes * Remove CompactAs implementation * Properly migrate from 1D weight * Remove #[pallet::compact] from Weight parameters * More #[pallet::compact] removals * Add unit tests * Set appropriate default block proof size * cargo fmt * Remove nonsensical weight constant * Test only for the reference time weight in frame_system::limits * Only check for reference time weight on idle * Use destructuring syntax * Update test expectations * Fixes * Fixes * Fixes * Correctly migrate from 1D weights * cargo fmt * Migrate using extra extrinsics instead of custom Decode * Fixes * Silence dispatch call warnings that were previously allowed * Fix gas_left test * Use OldWeight instead of u64 * Fixes * Only check for reference time weight in election provider * Fix test expectations * Fix test expectations * Use only reference time weight in grandpa test * Use only reference time weight in examples test * Use only reference time weight in examples test * Fix test expectations Co-authored-by: Oliver Tale-Yazdi <[email protected]> Co-authored-by: Alexander Theißen <[email protected]>
1 parent 2ee4cb4 commit 17c07af

File tree

20 files changed

+531
-128
lines changed

20 files changed

+531
-128
lines changed

frame/alliance/src/lib.rs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ use frame_support::{
120120
ChangeMembers, Currency, Get, InitializeMembers, IsSubType, OnUnbalanced,
121121
ReservableCurrency,
122122
},
123-
weights::Weight,
123+
weights::{OldWeight, Weight},
124124
};
125125
use pallet_identity::IdentityField;
126126

@@ -620,25 +620,22 @@ pub mod pallet {
620620
.max(T::WeightInfo::close_early_disapproved(x, y, p2))
621621
.max(T::WeightInfo::close_approved(b, x, y, p2))
622622
.max(T::WeightInfo::close_disapproved(x, y, p2))
623-
.saturating_add(p1)
623+
.saturating_add(p1.into())
624624
})]
625-
pub fn close(
625+
#[allow(deprecated)]
626+
#[deprecated(note = "1D weight is used in this extrinsic, please migrate to use `close`")]
627+
pub fn close_old_weight(
626628
origin: OriginFor<T>,
627629
proposal_hash: T::Hash,
628630
#[pallet::compact] index: ProposalIndex,
629-
#[pallet::compact] proposal_weight_bound: Weight,
631+
#[pallet::compact] proposal_weight_bound: OldWeight,
630632
#[pallet::compact] length_bound: u32,
631633
) -> DispatchResultWithPostInfo {
634+
let proposal_weight_bound: Weight = proposal_weight_bound.into();
632635
let who = ensure_signed(origin)?;
633636
ensure!(Self::has_voting_rights(&who), Error::<T, I>::NoVotingRights);
634637

635-
let info = T::ProposalProvider::close_proposal(
636-
proposal_hash,
637-
index,
638-
proposal_weight_bound,
639-
length_bound,
640-
)?;
641-
Ok(info.into())
638+
Self::do_close(proposal_hash, index, proposal_weight_bound, length_bound)
642639
}
643640

644641
/// Initialize the Alliance, onboard founders, fellows, and allies.
@@ -985,6 +982,34 @@ pub mod pallet {
985982
Self::deposit_event(Event::UnscrupulousItemRemoved { items });
986983
Ok(())
987984
}
985+
986+
/// Close a vote that is either approved, disapproved, or whose voting period has ended.
987+
///
988+
/// Requires the sender to be a founder or fellow.
989+
#[pallet::weight({
990+
let b = *length_bound;
991+
let x = T::MaxFounders::get();
992+
let y = T::MaxFellows::get();
993+
let p1 = *proposal_weight_bound;
994+
let p2 = T::MaxProposals::get();
995+
T::WeightInfo::close_early_approved(b, x, y, p2)
996+
.max(T::WeightInfo::close_early_disapproved(x, y, p2))
997+
.max(T::WeightInfo::close_approved(b, x, y, p2))
998+
.max(T::WeightInfo::close_disapproved(x, y, p2))
999+
.saturating_add(p1)
1000+
})]
1001+
pub fn close(
1002+
origin: OriginFor<T>,
1003+
proposal_hash: T::Hash,
1004+
#[pallet::compact] index: ProposalIndex,
1005+
proposal_weight_bound: Weight,
1006+
#[pallet::compact] length_bound: u32,
1007+
) -> DispatchResultWithPostInfo {
1008+
let who = ensure_signed(origin)?;
1009+
ensure!(Self::has_voting_rights(&who), Error::<T, I>::NoVotingRights);
1010+
1011+
Self::do_close(proposal_hash, index, proposal_weight_bound, length_bound)
1012+
}
9881013
}
9891014
}
9901015

@@ -1197,4 +1222,19 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
11971222
}
11981223
res
11991224
}
1225+
1226+
fn do_close(
1227+
proposal_hash: T::Hash,
1228+
index: ProposalIndex,
1229+
proposal_weight_bound: Weight,
1230+
length_bound: u32,
1231+
) -> DispatchResultWithPostInfo {
1232+
let info = T::ProposalProvider::close_proposal(
1233+
proposal_hash,
1234+
index,
1235+
proposal_weight_bound,
1236+
length_bound,
1237+
)?;
1238+
Ok(info.into())
1239+
}
12001240
}

frame/babe/src/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,8 @@ fn valid_equivocation_reports_dont_pay_fees() {
852852
.get_dispatch_info();
853853

854854
// it should have non-zero weight and the fee has to be paid.
855-
assert!(info.weight.all_gt(Weight::zero()));
855+
// TODO: account for proof size weight
856+
assert!(info.weight.ref_time() > 0);
856857
assert_eq!(info.pays_fee, Pays::Yes);
857858

858859
// report the equivocation.

frame/collective/src/lib.rs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use frame_support::{
5757
traits::{
5858
Backing, ChangeMembers, EnsureOrigin, Get, GetBacking, InitializeMembers, StorageVersion,
5959
},
60-
weights::Weight,
60+
weights::{OldWeight, Weight},
6161
};
6262

6363
#[cfg(test)]
@@ -620,17 +620,20 @@ pub mod pallet {
620620
.max(T::WeightInfo::close_early_disapproved(m, p2))
621621
.max(T::WeightInfo::close_approved(b, m, p2))
622622
.max(T::WeightInfo::close_disapproved(m, p2))
623-
.saturating_add(p1)
623+
.saturating_add(p1.into())
624624
},
625625
DispatchClass::Operational
626626
))]
627-
pub fn close(
627+
#[allow(deprecated)]
628+
#[deprecated(note = "1D weight is used in this extrinsic, please migrate to `close`")]
629+
pub fn close_old_weight(
628630
origin: OriginFor<T>,
629631
proposal_hash: T::Hash,
630632
#[pallet::compact] index: ProposalIndex,
631-
#[pallet::compact] proposal_weight_bound: Weight,
633+
#[pallet::compact] proposal_weight_bound: OldWeight,
632634
#[pallet::compact] length_bound: u32,
633635
) -> DispatchResultWithPostInfo {
636+
let proposal_weight_bound: Weight = proposal_weight_bound.into();
634637
let _ = ensure_signed(origin)?;
635638

636639
Self::do_close(proposal_hash, index, proposal_weight_bound, length_bound)
@@ -659,6 +662,64 @@ pub mod pallet {
659662
let proposal_count = Self::do_disapprove_proposal(proposal_hash);
660663
Ok(Some(T::WeightInfo::disapprove_proposal(proposal_count)).into())
661664
}
665+
666+
/// Close a vote that is either approved, disapproved or whose voting period has ended.
667+
///
668+
/// May be called by any signed account in order to finish voting and close the proposal.
669+
///
670+
/// If called before the end of the voting period it will only close the vote if it is
671+
/// has enough votes to be approved or disapproved.
672+
///
673+
/// If called after the end of the voting period abstentions are counted as rejections
674+
/// unless there is a prime member set and the prime member cast an approval.
675+
///
676+
/// If the close operation completes successfully with disapproval, the transaction fee will
677+
/// be waived. Otherwise execution of the approved operation will be charged to the caller.
678+
///
679+
/// + `proposal_weight_bound`: The maximum amount of weight consumed by executing the closed
680+
/// proposal.
681+
/// + `length_bound`: The upper bound for the length of the proposal in storage. Checked via
682+
/// `storage::read` so it is `size_of::<u32>() == 4` larger than the pure length.
683+
///
684+
/// # <weight>
685+
/// ## Weight
686+
/// - `O(B + M + P1 + P2)` where:
687+
/// - `B` is `proposal` size in bytes (length-fee-bounded)
688+
/// - `M` is members-count (code- and governance-bounded)
689+
/// - `P1` is the complexity of `proposal` preimage.
690+
/// - `P2` is proposal-count (code-bounded)
691+
/// - DB:
692+
/// - 2 storage reads (`Members`: codec `O(M)`, `Prime`: codec `O(1)`)
693+
/// - 3 mutations (`Voting`: codec `O(M)`, `ProposalOf`: codec `O(B)`, `Proposals`: codec
694+
/// `O(P2)`)
695+
/// - any mutations done while executing `proposal` (`P1`)
696+
/// - up to 3 events
697+
/// # </weight>
698+
#[pallet::weight((
699+
{
700+
let b = *length_bound;
701+
let m = T::MaxMembers::get();
702+
let p1 = *proposal_weight_bound;
703+
let p2 = T::MaxProposals::get();
704+
T::WeightInfo::close_early_approved(b, m, p2)
705+
.max(T::WeightInfo::close_early_disapproved(m, p2))
706+
.max(T::WeightInfo::close_approved(b, m, p2))
707+
.max(T::WeightInfo::close_disapproved(m, p2))
708+
.saturating_add(p1)
709+
},
710+
DispatchClass::Operational
711+
))]
712+
pub fn close(
713+
origin: OriginFor<T>,
714+
proposal_hash: T::Hash,
715+
#[pallet::compact] index: ProposalIndex,
716+
proposal_weight_bound: Weight,
717+
#[pallet::compact] length_bound: u32,
718+
) -> DispatchResultWithPostInfo {
719+
let _ = ensure_signed(origin)?;
720+
721+
Self::do_close(proposal_hash, index, proposal_weight_bound, length_bound)
722+
}
662723
}
663724
}
664725

0 commit comments

Comments
 (0)