diff --git a/substrate/bin/node/runtime/src/impls.rs b/substrate/bin/node/runtime/src/impls.rs index 884bde08df..85c28c9615 100644 --- a/substrate/bin/node/runtime/src/impls.rs +++ b/substrate/bin/node/runtime/src/impls.rs @@ -62,7 +62,7 @@ pub struct TargetedFeeAdjustment(sp_std::marker::PhantomData); impl> Convert for TargetedFeeAdjustment { fn convert(multiplier: Fixed128) -> Fixed128 { let max_weight = MaximumBlockWeight::get(); - let block_weight = System::all_extrinsics_weight().total().min(max_weight); + let block_weight = System::block_weight().total().min(max_weight); let target_weight = (T::get() * max_weight) as u128; let block_weight = block_weight as u128; diff --git a/substrate/frame/executive/src/lib.rs b/substrate/frame/executive/src/lib.rs index f7ac060a6c..04e095fec4 100644 --- a/substrate/frame/executive/src/lib.rs +++ b/substrate/frame/executive/src/lib.rs @@ -708,7 +708,7 @@ mod tests { header: Header { parent_hash: [69u8; 32].into(), number: 1, - state_root: hex!("409fb5a14aeb8b8c59258b503396a56dee45a0ee28a78de3e622db957425e275").into(), + state_root: hex!("05a38fa4a48ca80ffa8482304be7749a484dc8c9c31462a570d0fbadde6a3633").into(), extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(), digest: Digest { logs: vec![], }, }, @@ -789,7 +789,7 @@ mod tests { Digest::default(), )); // Base block execution weight + `on_initialize` weight from the custom module. - assert_eq!(>::all_extrinsics_weight().total(), base_block_weight); + assert_eq!(>::block_weight().total(), base_block_weight); for nonce in 0..=num_to_exhaust_block { let xt = TestXt::new( @@ -799,7 +799,7 @@ mod tests { if nonce != num_to_exhaust_block { assert!(res.is_ok()); assert_eq!( - >::all_extrinsics_weight().total(), + >::block_weight().total(), //--------------------- on_initialize + block_execution + extrinsic_base weight (encoded_len + 5) * (nonce + 1) + base_block_weight, ); @@ -819,7 +819,18 @@ mod tests { let len = xt.clone().encode().len() as u32; let mut t = new_test_ext(1); t.execute_with(|| { - assert_eq!(>::all_extrinsics_weight().total(), 0); + // Block execution weight + on_initialize weight from custom module + let base_block_weight = 175 + ::BlockExecutionWeight::get(); + + Executive::initialize_block(&Header::new( + 1, + H256::default(), + H256::default(), + [69u8; 32].into(), + Digest::default(), + )); + + assert_eq!(>::block_weight().total(), base_block_weight); assert_eq!(>::all_extrinsics_len(), 0); assert!(Executive::apply_extrinsic(xt.clone()).unwrap().is_ok()); @@ -827,16 +838,28 @@ mod tests { assert!(Executive::apply_extrinsic(x2.clone()).unwrap().is_ok()); // default weight for `TestXt` == encoded length. + let extrinsic_weight = len as Weight + ::ExtrinsicBaseWeight::get(); assert_eq!( - >::all_extrinsics_weight().total(), - 3 * (len as Weight + ::ExtrinsicBaseWeight::get()), + >::block_weight().total(), + base_block_weight + 3 * extrinsic_weight, ); assert_eq!(>::all_extrinsics_len(), 3 * len); let _ = >::finalize(); - - assert_eq!(>::all_extrinsics_weight().total(), 0); + // All extrinsics length cleaned on `System::finalize` assert_eq!(>::all_extrinsics_len(), 0); + + // New Block + Executive::initialize_block(&Header::new( + 2, + H256::default(), + H256::default(), + [69u8; 32].into(), + Digest::default(), + )); + + // Block weight cleaned up on `System::initialize` + assert_eq!(>::block_weight().total(), base_block_weight); }); } @@ -908,7 +931,7 @@ mod tests { // NOTE: might need updates over time if new weights are introduced. // For now it only accounts for the base block execution weight and // the `on_initialize` weight defined in the custom test module. - assert_eq!(>::all_extrinsics_weight().total(), 175 + 10); + assert_eq!(>::block_weight().total(), 175 + 10); }) } diff --git a/substrate/frame/system/src/lib.rs b/substrate/frame/system/src/lib.rs index 746e6536ea..4fa826ce89 100644 --- a/substrate/frame/system/src/lib.rs +++ b/substrate/frame/system/src/lib.rs @@ -429,8 +429,8 @@ decl_storage! { /// Total extrinsics count for the current block. ExtrinsicCount: Option; - /// Total weight for all extrinsics for the current block. - AllExtrinsicsWeight: ExtrinsicsWeight; + /// The current weight for the block. + BlockWeight get(fn block_weight): ExtrinsicsWeight; /// Total length (in bytes) for all extrinsics put together, for the current block. AllExtrinsicsLen: Option; @@ -978,11 +978,6 @@ impl Module { ExtrinsicCount::get().unwrap_or_default() } - /// Gets the weight of all executed extrinsics. - pub fn all_extrinsics_weight() -> ExtrinsicsWeight { - AllExtrinsicsWeight::get() - } - pub fn all_extrinsics_len() -> u32 { AllExtrinsicsLen::get().unwrap_or_default() } @@ -1003,7 +998,7 @@ impl Module { /// /// Another potential use-case could be for the `on_initialize` and `on_finalize` hooks. pub fn register_extra_weight_unchecked(weight: Weight, class: DispatchClass) { - AllExtrinsicsWeight::mutate(|current_weight| { + BlockWeight::mutate(|current_weight| { current_weight.add(weight, class); }); } @@ -1025,6 +1020,10 @@ impl Module { >::insert(*number - One::one(), parent_hash); >::put(txs_root); + // Remove previous block data from storage + BlockWeight::kill(); + + // Kill inspectable storage entries in state when `InitKind::Full`. if let InitKind::Full = kind { >::kill(); EventCount::kill(); @@ -1036,7 +1035,6 @@ impl Module { pub fn finalize() -> T::Header { ExecutionPhase::kill(); ExtrinsicCount::kill(); - AllExtrinsicsWeight::kill(); AllExtrinsicsLen::kill(); let number = >::take(); @@ -1126,7 +1124,7 @@ impl Module { /// Set the current block weight. This should only be used in some integration tests. #[cfg(any(feature = "std", test))] pub fn set_block_limits(weight: Weight, len: usize) { - AllExtrinsicsWeight::mutate(|current_weight| { + BlockWeight::mutate(|current_weight| { current_weight.put(weight, DispatchClass::Normal) }); AllExtrinsicsLen::put(len as u32); @@ -1383,7 +1381,7 @@ impl CheckWeight where info: &DispatchInfoOf, ) -> Result { let maximum_weight = T::MaximumBlockWeight::get(); - let mut all_weight = Module::::all_extrinsics_weight(); + let mut all_weight = Module::::block_weight(); match info.class { // If we have a dispatch that must be included in the block, it ignores all the limits. DispatchClass::Mandatory => { @@ -1474,7 +1472,7 @@ impl CheckWeight where Self::check_extrinsic_weight(info)?; AllExtrinsicsLen::put(next_len); - AllExtrinsicsWeight::put(next_weight); + BlockWeight::put(next_weight); Ok(()) } @@ -1565,7 +1563,7 @@ impl SignedExtension for CheckWeight where let unspent = post_info.calc_unspent(info); if unspent > 0 { - AllExtrinsicsWeight::mutate(|current_weight| { + BlockWeight::mutate(|current_weight| { current_weight.sub(unspent, info.class); }) } @@ -2288,7 +2286,7 @@ pub(crate) mod tests { let len = 0_usize; let reset_check_weight = |i, f, s| { - AllExtrinsicsWeight::mutate(|current_weight| { + BlockWeight::mutate(|current_weight| { current_weight.put(s, DispatchClass::Normal) }); let r = CheckWeight::(PhantomData).pre_dispatch(&1, CALL, i, len); @@ -2310,19 +2308,19 @@ pub(crate) mod tests { let len = 0_usize; // We allow 75% for normal transaction, so we put 25% - extrinsic base weight - AllExtrinsicsWeight::mutate(|current_weight| { + BlockWeight::mutate(|current_weight| { current_weight.put(256 - ::ExtrinsicBaseWeight::get(), DispatchClass::Normal) }); let pre = CheckWeight::(PhantomData).pre_dispatch(&1, CALL, &info, len).unwrap(); - assert_eq!(AllExtrinsicsWeight::get().total(), info.weight + 256); + assert_eq!(BlockWeight::get().total(), info.weight + 256); assert!( CheckWeight::::post_dispatch(pre, &info, &post_info, len, &Ok(())) .is_ok() ); assert_eq!( - AllExtrinsicsWeight::get().total(), + BlockWeight::get().total(), post_info.actual_weight.unwrap() + 256, ); }) @@ -2335,13 +2333,13 @@ pub(crate) mod tests { let post_info = PostDispatchInfo { actual_weight: Some(700), }; let len = 0_usize; - AllExtrinsicsWeight::mutate(|current_weight| { + BlockWeight::mutate(|current_weight| { current_weight.put(128, DispatchClass::Normal) }); let pre = CheckWeight::(PhantomData).pre_dispatch(&1, CALL, &info, len).unwrap(); assert_eq!( - AllExtrinsicsWeight::get().total(), + BlockWeight::get().total(), info.weight + 128 + ::ExtrinsicBaseWeight::get(), ); @@ -2350,7 +2348,7 @@ pub(crate) mod tests { .is_ok() ); assert_eq!( - AllExtrinsicsWeight::get().total(), + BlockWeight::get().total(), info.weight + 128 + ::ExtrinsicBaseWeight::get(), ); }) @@ -2363,11 +2361,11 @@ pub(crate) mod tests { let len = 0_usize; // Initial weight from `BlockExecutionWeight` - assert_eq!(System::all_extrinsics_weight().total(), ::BlockExecutionWeight::get()); + assert_eq!(System::block_weight().total(), ::BlockExecutionWeight::get()); let r = CheckWeight::(PhantomData).pre_dispatch(&1, CALL, &free, len); assert!(r.is_ok()); assert_eq!( - System::all_extrinsics_weight().total(), + System::block_weight().total(), ::ExtrinsicBaseWeight::get() + ::BlockExecutionWeight::get() ); }) @@ -2390,8 +2388,8 @@ pub(crate) mod tests { check(|max, len| { assert_ok!(CheckWeight::::do_pre_dispatch(max, len)); - assert_eq!(System::all_extrinsics_weight().total(), Weight::max_value()); - assert!(System::all_extrinsics_weight().total() > ::MaximumBlockWeight::get()); + assert_eq!(System::block_weight().total(), Weight::max_value()); + assert!(System::block_weight().total() > ::MaximumBlockWeight::get()); }); check(|max, len| { assert_ok!(CheckWeight::::do_validate(max, len)); @@ -2419,8 +2417,8 @@ pub(crate) mod tests { fn register_extra_weight_unchecked_doesnt_care_about_limits() { new_test_ext().execute_with(|| { System::register_extra_weight_unchecked(Weight::max_value(), DispatchClass::Normal); - assert_eq!(System::all_extrinsics_weight().total(), Weight::max_value()); - assert!(System::all_extrinsics_weight().total() > ::MaximumBlockWeight::get()); + assert_eq!(System::block_weight().total(), Weight::max_value()); + assert!(System::block_weight().total() > ::MaximumBlockWeight::get()); }); } @@ -2438,10 +2436,10 @@ pub(crate) mod tests { let len = 0_usize; assert_ok!(CheckWeight::::do_pre_dispatch(&max_normal, len)); - assert_eq!(System::all_extrinsics_weight().total(), 768); + assert_eq!(System::block_weight().total(), 768); assert_ok!(CheckWeight::::do_pre_dispatch(&rest_operational, len)); assert_eq!(::MaximumBlockWeight::get(), 1024); - assert_eq!(System::all_extrinsics_weight().total(), ::MaximumBlockWeight::get()); + assert_eq!(System::block_weight().total(), ::MaximumBlockWeight::get()); }); } @@ -2456,10 +2454,10 @@ pub(crate) mod tests { assert_ok!(CheckWeight::::do_pre_dispatch(&rest_operational, len)); // Extra 15 here from block execution + base extrinsic weight - assert_eq!(System::all_extrinsics_weight().total(), 266); + assert_eq!(System::block_weight().total(), 266); assert_ok!(CheckWeight::::do_pre_dispatch(&max_normal, len)); assert_eq!(::MaximumBlockWeight::get(), 1024); - assert_eq!(System::all_extrinsics_weight().total(), ::MaximumBlockWeight::get()); + assert_eq!(System::block_weight().total(), ::MaximumBlockWeight::get()); }); } @@ -2489,7 +2487,7 @@ pub(crate) mod tests { let normal_limit = normal_weight_limit(); // given almost full block - AllExtrinsicsWeight::mutate(|current_weight| { + BlockWeight::mutate(|current_weight| { current_weight.put(normal_limit, DispatchClass::Normal) }); // will not fit.