Skip to content

Commit e9180e1

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 734e1ad commit e9180e1

File tree

1 file changed

+36
-26
lines changed

1 file changed

+36
-26
lines changed

src/lib.rs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,12 @@ where
251251
weight = weight.saturating_add(
252252
<frame_system::Module<System> as OnInitialize<System::BlockNumber>>::on_initialize(*block_number)
253253
);
254-
weight = weight.saturating_add(<AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number))
255-
.saturating_add(<System::BlockExecutionWeight as frame_support::traits::Get<_>>::get());
254+
weight = weight.saturating_add(
255+
<AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number)
256+
);
257+
weight = weight.saturating_add(
258+
<System::BlockWeights as frame_support::traits::Get<_>>::get().base_block
259+
);
256260
<frame_system::Module::<System>>::register_extra_weight_unchecked(weight, DispatchClass::Mandatory);
257261

258262
frame_system::Module::<System>::note_finished_initialize();
@@ -482,7 +486,7 @@ mod tests {
482486
use super::*;
483487
use sp_core::H256;
484488
use sp_runtime::{
485-
generic::{Era, DigestItem}, Perbill, DispatchError, testing::{Digest, Header, Block},
489+
generic::{Era, DigestItem}, DispatchError, testing::{Digest, Header, Block},
486490
traits::{Header as HeaderT, BlakeTwo256, IdentityLookup},
487491
transaction_validity::{
488492
InvalidTransaction, ValidTransaction, TransactionValidityError, UnknownTransaction
@@ -493,7 +497,9 @@ mod tests {
493497
weights::{Weight, RuntimeDbWeight, IdentityFee, WeightToFeePolynomial},
494498
traits::{Currency, LockIdentifier, LockableCurrency, WithdrawReasons},
495499
};
496-
use frame_system::{Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo};
500+
use frame_system::{
501+
Call as SystemCall, ChainContext, LastRuntimeUpgradeInfo,
502+
};
497503
use pallet_transaction_payment::CurrencyAdapter;
498504
use pallet_balances::Call as BalancesCall;
499505
use hex_literal::hex;
@@ -584,18 +590,22 @@ mod tests {
584590

585591
parameter_types! {
586592
pub const BlockHashCount: u64 = 250;
587-
pub const MaximumBlockWeight: Weight = 1024;
588-
pub const MaximumBlockLength: u32 = 2 * 1024;
589-
pub const AvailableBlockRatio: Perbill = Perbill::one();
590-
pub const BlockExecutionWeight: Weight = 10;
591-
pub const ExtrinsicBaseWeight: Weight = 5;
593+
pub BlockWeights: frame_system::limits::BlockWeights =
594+
frame_system::limits::BlockWeights::builder()
595+
.base_block(10)
596+
.for_class(DispatchClass::all(), |weights| weights.base_extrinsic = 5)
597+
.for_class(DispatchClass::non_mandatory(), |weights| weights.max_total = 1024.into())
598+
.build_or_panic();
592599
pub const DbWeight: RuntimeDbWeight = RuntimeDbWeight {
593600
read: 10,
594601
write: 100,
595602
};
596603
}
597604
impl frame_system::Config for Runtime {
598605
type BaseCallFilter = ();
606+
type BlockWeights = BlockWeights;
607+
type BlockLength = ();
608+
type DbWeight = ();
599609
type Origin = Origin;
600610
type Index = u64;
601611
type Call = Call;
@@ -607,13 +617,6 @@ mod tests {
607617
type Header = Header;
608618
type Event = Event;
609619
type BlockHashCount = BlockHashCount;
610-
type MaximumBlockWeight = MaximumBlockWeight;
611-
type DbWeight = DbWeight;
612-
type BlockExecutionWeight = BlockExecutionWeight;
613-
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
614-
type MaximumExtrinsicWeight = MaximumBlockWeight;
615-
type AvailableBlockRatio = AvailableBlockRatio;
616-
type MaximumBlockLength = MaximumBlockLength;
617620
type Version = RuntimeVersion;
618621
type PalletInfo = PalletInfo;
619622
type AccountData = pallet_balances::AccountData<Balance>;
@@ -715,7 +718,8 @@ mod tests {
715718
balances: vec![(1, 211)],
716719
}.assimilate_storage(&mut t).unwrap();
717720
let xt = TestXt::new(Call::Balances(BalancesCall::transfer(2, 69)), sign_extra(1, 0, 0));
718-
let weight = xt.get_dispatch_info().weight + <Runtime as frame_system::Config>::ExtrinsicBaseWeight::get();
721+
let weight = xt.get_dispatch_info().weight +
722+
<Runtime as frame_system::Config>::BlockWeights::get().get(DispatchClass::Normal).base_extrinsic;
719723
let fee: Balance
720724
= <Runtime as pallet_transaction_payment::Config>::WeightToFee::calc(&weight);
721725
let mut t = sp_io::TestExternalities::new(t);
@@ -749,7 +753,7 @@ mod tests {
749753
header: Header {
750754
parent_hash: [69u8; 32].into(),
751755
number: 1,
752-
state_root: hex!("465a1569d309039bdf84b0479d28064ea29e6584584dc7d788904bb14489c6f6").into(),
756+
state_root: hex!("6a3ad91caba5b8ac15c325a36d7568adf6a7e49321865de7527b851d870343d4").into(),
753757
extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(),
754758
digest: Digest { logs: vec![], },
755759
},
@@ -817,9 +821,11 @@ mod tests {
817821
let xt = TestXt::new(Call::Balances(BalancesCall::transfer(33, 0)), sign_extra(1, 0, 0));
818822
let encoded = xt.encode();
819823
let encoded_len = encoded.len() as Weight;
820-
// on_initialize weight + block execution weight
821-
let base_block_weight = 175 + <Runtime as frame_system::Config>::BlockExecutionWeight::get();
822-
let limit = AvailableBlockRatio::get() * MaximumBlockWeight::get() - base_block_weight;
824+
// on_initialize weight + base block execution weight
825+
let block_weights = <Runtime as frame_system::Config>::BlockWeights::get();
826+
let base_block_weight = 175 + block_weights.base_block;
827+
let limit = block_weights.get(DispatchClass::Normal).max_total.unwrap()
828+
- base_block_weight;
823829
let num_to_exhaust_block = limit / (encoded_len + 5);
824830
t.execute_with(|| {
825831
Executive::initialize_block(&Header::new(
@@ -861,7 +867,7 @@ mod tests {
861867
let mut t = new_test_ext(1);
862868
t.execute_with(|| {
863869
// Block execution weight + on_initialize weight from custom module
864-
let base_block_weight = 175 + <Runtime as frame_system::Config>::BlockExecutionWeight::get();
870+
let base_block_weight = 175 + <Runtime as frame_system::Config>::BlockWeights::get().base_block;
865871

866872
Executive::initialize_block(&Header::new(
867873
1,
@@ -879,7 +885,8 @@ mod tests {
879885
assert!(Executive::apply_extrinsic(x2.clone()).unwrap().is_ok());
880886

881887
// default weight for `TestXt` == encoded length.
882-
let extrinsic_weight = len as Weight + <Runtime as frame_system::Config>::ExtrinsicBaseWeight::get();
888+
let extrinsic_weight = len as Weight + <Runtime as frame_system::Config>::BlockWeights
889+
::get().get(DispatchClass::Normal).base_extrinsic;
883890
assert_eq!(
884891
<frame_system::Module<Runtime>>::block_weight().total(),
885892
base_block_weight + 3 * extrinsic_weight,
@@ -945,8 +952,11 @@ mod tests {
945952
Call::System(SystemCall::remark(vec![1u8])),
946953
sign_extra(1, 0, 0),
947954
);
948-
let weight = xt.get_dispatch_info().weight
949-
+ <Runtime as frame_system::Config>::ExtrinsicBaseWeight::get();
955+
let weight = xt.get_dispatch_info().weight + <Runtime as frame_system::Config>
956+
::BlockWeights
957+
::get()
958+
.get(DispatchClass::Normal)
959+
.base_extrinsic;
950960
let fee: Balance =
951961
<Runtime as pallet_transaction_payment::Config>::WeightToFee::calc(&weight);
952962
Executive::initialize_block(&Header::new(
@@ -1106,7 +1116,7 @@ mod tests {
11061116
let runtime_upgrade_weight = <AllModules as OnRuntimeUpgrade>::on_runtime_upgrade();
11071117
let frame_system_on_initialize_weight = frame_system::Module::<Runtime>::on_initialize(block_number);
11081118
let on_initialize_weight = <AllModules as OnInitialize<u64>>::on_initialize(block_number);
1109-
let base_block_weight = <Runtime as frame_system::Config>::BlockExecutionWeight::get();
1119+
let base_block_weight = <Runtime as frame_system::Config>::BlockWeights::get().base_block;
11101120

11111121
// Weights are recorded correctly
11121122
assert_eq!(

0 commit comments

Comments
 (0)