Skip to content

Commit 5c83bab

Browse files
tomusdrwkianenigmashawntabrizignunicorngui1117
authored
Streamline frame_system weight parametrization (#6629)
* Basic weights builder. * Fixing WiP * Make the tests work. * Fix weights in node/runtime. * WiP. * Update pallets with new weights parameters. * Validate returns a Result now. * Count mandatory weight separately. * DRY * BREAKING: Updating state root, because of the left-over weight-tracking stuff * Update tests affected by Mandatory tracking. * Fixing tests. * Fix defaults for simple_max * Update frame/system/src/weights.rs Co-authored-by: Kian Paimani <[email protected]> * Rework the API a bit. * Fix compilation & tests. * Apply suggestions from code review Co-authored-by: Kian Paimani <[email protected]> * Add extra docs & rename few things. * Fix whitespace in ASCII art. * Update frame/system/src/limits.rs Co-authored-by: Kian Paimani <[email protected]> * Fix max_extrinsic calculations. * Fix conflicts. * Fix compilation. * Fix new code. * re-remove generic asset * Fix usage. * Update state root. * Update proxy. * Fix tests. * Move weights validity to integrity_test * Remove redundant BlockWeights. * Add all/non_mandatory comment * Add test. * Remove fn block_weights * Make the macro prettier. * Fix some docs. * Make max_total behave more predictabily. * Add BlockWeights to metadata. * fix balances test * Fix utility test. Co-authored-by: Kian Paimani <[email protected]> Co-authored-by: Shawn Tabrizi <[email protected]> Co-authored-by: Benjamin Kampmann <[email protected]> Co-authored-by: thiolliere <[email protected]>
1 parent 5159ce5 commit 5c83bab

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

src/lib.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ decl_module! {
533533
///
534534
/// Total Complexity: O(M + B + C + logM + logB + X)
535535
/// # </weight>
536-
#[weight = T::MaximumBlockWeight::get() / 10]
536+
#[weight = T::BlockWeights::get().max_block / 10]
537537
pub fn bid(origin, value: BalanceOf<T, I>) -> DispatchResult {
538538
let who = ensure_signed(origin)?;
539539
ensure!(!<SuspendedCandidates<T, I>>::contains_key(&who), Error::<T, I>::Suspended);
@@ -572,7 +572,7 @@ decl_module! {
572572
///
573573
/// Total Complexity: O(B + X)
574574
/// # </weight>
575-
#[weight = T::MaximumBlockWeight::get() / 10]
575+
#[weight = T::BlockWeights::get().max_block / 10]
576576
pub fn unbid(origin, pos: u32) -> DispatchResult {
577577
let who = ensure_signed(origin)?;
578578

@@ -642,7 +642,7 @@ decl_module! {
642642
///
643643
/// Total Complexity: O(M + B + C + logM + logB + X)
644644
/// # </weight>
645-
#[weight = T::MaximumBlockWeight::get() / 10]
645+
#[weight = T::BlockWeights::get().max_block / 10]
646646
pub fn vouch(origin, who: T::AccountId, value: BalanceOf<T, I>, tip: BalanceOf<T, I>) -> DispatchResult {
647647
let voucher = ensure_signed(origin)?;
648648
// Check user is not suspended.
@@ -683,7 +683,7 @@ decl_module! {
683683
///
684684
/// Total Complexity: O(B)
685685
/// # </weight>
686-
#[weight = T::MaximumBlockWeight::get() / 10]
686+
#[weight = T::BlockWeights::get().max_block / 10]
687687
pub fn unvouch(origin, pos: u32) -> DispatchResult {
688688
let voucher = ensure_signed(origin)?;
689689
ensure!(Self::vouching(&voucher) == Some(VouchingStatus::Vouching), Error::<T, I>::NotVouching);
@@ -721,7 +721,7 @@ decl_module! {
721721
///
722722
/// Total Complexity: O(M + logM + C)
723723
/// # </weight>
724-
#[weight = T::MaximumBlockWeight::get() / 10]
724+
#[weight = T::BlockWeights::get().max_block / 10]
725725
pub fn vote(origin, candidate: <T::Lookup as StaticLookup>::Source, approve: bool) {
726726
let voter = ensure_signed(origin)?;
727727
let candidate = T::Lookup::lookup(candidate)?;
@@ -752,7 +752,7 @@ decl_module! {
752752
///
753753
/// Total Complexity: O(M + logM)
754754
/// # </weight>
755-
#[weight = T::MaximumBlockWeight::get() / 10]
755+
#[weight = T::BlockWeights::get().max_block / 10]
756756
pub fn defender_vote(origin, approve: bool) {
757757
let voter = ensure_signed(origin)?;
758758
let members = <Members<T, I>>::get();
@@ -784,7 +784,7 @@ decl_module! {
784784
///
785785
/// Total Complexity: O(M + logM + P + X)
786786
/// # </weight>
787-
#[weight = T::MaximumBlockWeight::get() / 10]
787+
#[weight = T::BlockWeights::get().max_block / 10]
788788
pub fn payout(origin) {
789789
let who = ensure_signed(origin)?;
790790

@@ -826,7 +826,7 @@ decl_module! {
826826
///
827827
/// Total Complexity: O(1)
828828
/// # </weight>
829-
#[weight = T::MaximumBlockWeight::get() / 10]
829+
#[weight = T::BlockWeights::get().max_block / 10]
830830
fn found(origin, founder: T::AccountId, max_members: u32, rules: Vec<u8>) {
831831
T::FounderSetOrigin::ensure_origin(origin)?;
832832
ensure!(!<Head<T, I>>::exists(), Error::<T, I>::AlreadyFounded);
@@ -853,7 +853,7 @@ decl_module! {
853853
///
854854
/// Total Complexity: O(1)
855855
/// # </weight>
856-
#[weight = T::MaximumBlockWeight::get() / 10]
856+
#[weight = T::BlockWeights::get().max_block / 10]
857857
fn unfound(origin) {
858858
let founder = ensure_signed(origin)?;
859859
ensure!(Founder::<T, I>::get() == Some(founder.clone()), Error::<T, I>::NotFounder);
@@ -895,7 +895,7 @@ decl_module! {
895895
///
896896
/// Total Complexity: O(M + logM + B)
897897
/// # </weight>
898-
#[weight = T::MaximumBlockWeight::get() / 10]
898+
#[weight = T::BlockWeights::get().max_block / 10]
899899
fn judge_suspended_member(origin, who: T::AccountId, forgive: bool) {
900900
T::SuspensionJudgementOrigin::ensure_origin(origin)?;
901901
ensure!(<SuspendedMembers<T, I>>::contains_key(&who), Error::<T, I>::NotSuspended);
@@ -966,7 +966,7 @@ decl_module! {
966966
///
967967
/// Total Complexity: O(M + logM + B + X)
968968
/// # </weight>
969-
#[weight = T::MaximumBlockWeight::get() / 10]
969+
#[weight = T::BlockWeights::get().max_block / 10]
970970
fn judge_suspended_candidate(origin, who: T::AccountId, judgement: Judgement) {
971971
T::SuspensionJudgementOrigin::ensure_origin(origin)?;
972972
if let Some((value, kind)) = <SuspendedCandidates<T, I>>::get(&who) {
@@ -1026,7 +1026,7 @@ decl_module! {
10261026
///
10271027
/// Total Complexity: O(1)
10281028
/// # </weight>
1029-
#[weight = T::MaximumBlockWeight::get() / 10]
1029+
#[weight = T::BlockWeights::get().max_block / 10]
10301030
fn set_max_members(origin, max: u32) {
10311031
ensure_root(origin)?;
10321032
ensure!(max > 1, Error::<T, I>::MaxMembers);
@@ -1038,13 +1038,14 @@ decl_module! {
10381038
let mut members = vec![];
10391039

10401040
let mut weight = 0;
1041+
let weights = T::BlockWeights::get();
10411042

10421043
// Run a candidate/membership rotation
10431044
if (n % T::RotationPeriod::get()).is_zero() {
10441045
members = <Members<T, I>>::get();
10451046
Self::rotate_period(&mut members);
10461047

1047-
weight += T::MaximumBlockWeight::get() / 20;
1048+
weight += weights.max_block / 20;
10481049
}
10491050

10501051
// Run a challenge rotation
@@ -1055,7 +1056,7 @@ decl_module! {
10551056
}
10561057
Self::rotate_challenge(&mut members);
10571058

1058-
weight += T::MaximumBlockWeight::get() / 20;
1059+
weight += weights.max_block / 20;
10591060
}
10601061

10611062
weight

src/mock.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ use frame_support::{
2525
};
2626
use sp_core::H256;
2727
use sp_runtime::{
28-
Perbill,
2928
testing::Header,
3029
traits::{BlakeTwo256, IdentityLookup},
3130
};
@@ -45,14 +44,11 @@ parameter_types! {
4544
pub const PeriodSpend: u64 = 1000;
4645
pub const MaxLockDuration: u64 = 100;
4746
pub const ChallengePeriod: u64 = 8;
48-
4947
pub const BlockHashCount: u64 = 250;
50-
pub const MaximumBlockWeight: u32 = 1024;
51-
pub const MaximumBlockLength: u32 = 2 * 1024;
52-
pub const AvailableBlockRatio: Perbill = Perbill::one();
53-
5448
pub const ExistentialDeposit: u64 = 1;
5549
pub const SocietyModuleId: ModuleId = ModuleId(*b"py/socie");
50+
pub BlockWeights: frame_system::limits::BlockWeights =
51+
frame_system::limits::BlockWeights::simple_max(1024);
5652
}
5753

5854
ord_parameter_types! {
@@ -62,6 +58,9 @@ ord_parameter_types! {
6258

6359
impl frame_system::Config for Test {
6460
type BaseCallFilter = ();
61+
type BlockWeights = ();
62+
type BlockLength = ();
63+
type DbWeight = ();
6564
type Origin = Origin;
6665
type Index = u64;
6766
type BlockNumber = u64;
@@ -73,13 +72,6 @@ impl frame_system::Config for Test {
7372
type Header = Header;
7473
type Event = ();
7574
type BlockHashCount = BlockHashCount;
76-
type MaximumBlockWeight = MaximumBlockWeight;
77-
type DbWeight = ();
78-
type BlockExecutionWeight = ();
79-
type ExtrinsicBaseWeight = ();
80-
type MaximumExtrinsicWeight = MaximumBlockWeight;
81-
type MaximumBlockLength = MaximumBlockLength;
82-
type AvailableBlockRatio = AvailableBlockRatio;
8375
type Version = ();
8476
type PalletInfo = ();
8577
type OnNewAccount = ();

0 commit comments

Comments
 (0)