Skip to content
Snippets Groups Projects
Commit 6ae0de57 authored by Shawn Tabrizi's avatar Shawn Tabrizi Committed by GitHub
Browse files

Keep `BlockWeight` in Storage (#6046)

* keep block weight in storage

* Update lib.rs

* rename to `BlockWeight`, update tests

* remove println

* make test better

* keep extrinsics length clean
parent ac049a97
No related merge requests found
...@@ -62,7 +62,7 @@ pub struct TargetedFeeAdjustment<T>(sp_std::marker::PhantomData<T>); ...@@ -62,7 +62,7 @@ pub struct TargetedFeeAdjustment<T>(sp_std::marker::PhantomData<T>);
impl<T: Get<Perquintill>> Convert<Fixed128, Fixed128> for TargetedFeeAdjustment<T> { impl<T: Get<Perquintill>> Convert<Fixed128, Fixed128> for TargetedFeeAdjustment<T> {
fn convert(multiplier: Fixed128) -> Fixed128 { fn convert(multiplier: Fixed128) -> Fixed128 {
let max_weight = MaximumBlockWeight::get(); 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 target_weight = (T::get() * max_weight) as u128;
let block_weight = block_weight as u128; let block_weight = block_weight as u128;
......
...@@ -708,7 +708,7 @@ mod tests { ...@@ -708,7 +708,7 @@ mod tests {
header: Header { header: Header {
parent_hash: [69u8; 32].into(), parent_hash: [69u8; 32].into(),
number: 1, number: 1,
state_root: hex!("409fb5a14aeb8b8c59258b503396a56dee45a0ee28a78de3e622db957425e275").into(), state_root: hex!("05a38fa4a48ca80ffa8482304be7749a484dc8c9c31462a570d0fbadde6a3633").into(),
extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(), extrinsics_root: hex!("03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314").into(),
digest: Digest { logs: vec![], }, digest: Digest { logs: vec![], },
}, },
...@@ -789,7 +789,7 @@ mod tests { ...@@ -789,7 +789,7 @@ mod tests {
Digest::default(), Digest::default(),
)); ));
// Base block execution weight + `on_initialize` weight from the custom module. // Base block execution weight + `on_initialize` weight from the custom module.
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight().total(), base_block_weight); assert_eq!(<frame_system::Module<Runtime>>::block_weight().total(), base_block_weight);
for nonce in 0..=num_to_exhaust_block { for nonce in 0..=num_to_exhaust_block {
let xt = TestXt::new( let xt = TestXt::new(
...@@ -799,7 +799,7 @@ mod tests { ...@@ -799,7 +799,7 @@ mod tests {
if nonce != num_to_exhaust_block { if nonce != num_to_exhaust_block {
assert!(res.is_ok()); assert!(res.is_ok());
assert_eq!( assert_eq!(
<frame_system::Module<Runtime>>::all_extrinsics_weight().total(), <frame_system::Module<Runtime>>::block_weight().total(),
//--------------------- on_initialize + block_execution + extrinsic_base weight //--------------------- on_initialize + block_execution + extrinsic_base weight
(encoded_len + 5) * (nonce + 1) + base_block_weight, (encoded_len + 5) * (nonce + 1) + base_block_weight,
); );
...@@ -819,7 +819,18 @@ mod tests { ...@@ -819,7 +819,18 @@ mod tests {
let len = xt.clone().encode().len() as u32; let len = xt.clone().encode().len() as u32;
let mut t = new_test_ext(1); let mut t = new_test_ext(1);
t.execute_with(|| { t.execute_with(|| {
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight().total(), 0); // Block execution weight + on_initialize weight from custom module
let base_block_weight = 175 + <Runtime as frame_system::Trait>::BlockExecutionWeight::get();
Executive::initialize_block(&Header::new(
1,
H256::default(),
H256::default(),
[69u8; 32].into(),
Digest::default(),
));
assert_eq!(<frame_system::Module<Runtime>>::block_weight().total(), base_block_weight);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 0); assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 0);
assert!(Executive::apply_extrinsic(xt.clone()).unwrap().is_ok()); assert!(Executive::apply_extrinsic(xt.clone()).unwrap().is_ok());
...@@ -827,16 +838,28 @@ mod tests { ...@@ -827,16 +838,28 @@ mod tests {
assert!(Executive::apply_extrinsic(x2.clone()).unwrap().is_ok()); assert!(Executive::apply_extrinsic(x2.clone()).unwrap().is_ok());
// default weight for `TestXt` == encoded length. // default weight for `TestXt` == encoded length.
let extrinsic_weight = len as Weight + <Runtime as frame_system::Trait>::ExtrinsicBaseWeight::get();
assert_eq!( assert_eq!(
<frame_system::Module<Runtime>>::all_extrinsics_weight().total(), <frame_system::Module<Runtime>>::block_weight().total(),
3 * (len as Weight + <Runtime as frame_system::Trait>::ExtrinsicBaseWeight::get()), base_block_weight + 3 * extrinsic_weight,
); );
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 3 * len); assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 3 * len);
let _ = <frame_system::Module<Runtime>>::finalize(); let _ = <frame_system::Module<Runtime>>::finalize();
// All extrinsics length cleaned on `System::finalize`
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight().total(), 0);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 0); assert_eq!(<frame_system::Module<Runtime>>::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!(<frame_system::Module<Runtime>>::block_weight().total(), base_block_weight);
}); });
} }
...@@ -908,7 +931,7 @@ mod tests { ...@@ -908,7 +931,7 @@ mod tests {
// NOTE: might need updates over time if new weights are introduced. // NOTE: might need updates over time if new weights are introduced.
// For now it only accounts for the base block execution weight and // For now it only accounts for the base block execution weight and
// the `on_initialize` weight defined in the custom test module. // the `on_initialize` weight defined in the custom test module.
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight().total(), 175 + 10); assert_eq!(<frame_system::Module<Runtime>>::block_weight().total(), 175 + 10);
}) })
} }
......
...@@ -429,8 +429,8 @@ decl_storage! { ...@@ -429,8 +429,8 @@ decl_storage! {
/// Total extrinsics count for the current block. /// Total extrinsics count for the current block.
ExtrinsicCount: Option<u32>; ExtrinsicCount: Option<u32>;
/// Total weight for all extrinsics for the current block. /// The current weight for the block.
AllExtrinsicsWeight: ExtrinsicsWeight; BlockWeight get(fn block_weight): ExtrinsicsWeight;
/// Total length (in bytes) for all extrinsics put together, for the current block. /// Total length (in bytes) for all extrinsics put together, for the current block.
AllExtrinsicsLen: Option<u32>; AllExtrinsicsLen: Option<u32>;
...@@ -978,11 +978,6 @@ impl<T: Trait> Module<T> { ...@@ -978,11 +978,6 @@ impl<T: Trait> Module<T> {
ExtrinsicCount::get().unwrap_or_default() 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 { pub fn all_extrinsics_len() -> u32 {
AllExtrinsicsLen::get().unwrap_or_default() AllExtrinsicsLen::get().unwrap_or_default()
} }
...@@ -1003,7 +998,7 @@ impl<T: Trait> Module<T> { ...@@ -1003,7 +998,7 @@ impl<T: Trait> Module<T> {
/// ///
/// Another potential use-case could be for the `on_initialize` and `on_finalize` hooks. /// Another potential use-case could be for the `on_initialize` and `on_finalize` hooks.
pub fn register_extra_weight_unchecked(weight: Weight, class: DispatchClass) { pub fn register_extra_weight_unchecked(weight: Weight, class: DispatchClass) {
AllExtrinsicsWeight::mutate(|current_weight| { BlockWeight::mutate(|current_weight| {
current_weight.add(weight, class); current_weight.add(weight, class);
}); });
} }
...@@ -1025,6 +1020,10 @@ impl<T: Trait> Module<T> { ...@@ -1025,6 +1020,10 @@ impl<T: Trait> Module<T> {
<BlockHash<T>>::insert(*number - One::one(), parent_hash); <BlockHash<T>>::insert(*number - One::one(), parent_hash);
<ExtrinsicsRoot<T>>::put(txs_root); <ExtrinsicsRoot<T>>::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 { if let InitKind::Full = kind {
<Events<T>>::kill(); <Events<T>>::kill();
EventCount::kill(); EventCount::kill();
...@@ -1036,7 +1035,6 @@ impl<T: Trait> Module<T> { ...@@ -1036,7 +1035,6 @@ impl<T: Trait> Module<T> {
pub fn finalize() -> T::Header { pub fn finalize() -> T::Header {
ExecutionPhase::kill(); ExecutionPhase::kill();
ExtrinsicCount::kill(); ExtrinsicCount::kill();
AllExtrinsicsWeight::kill();
AllExtrinsicsLen::kill(); AllExtrinsicsLen::kill();
let number = <Number<T>>::take(); let number = <Number<T>>::take();
...@@ -1126,7 +1124,7 @@ impl<T: Trait> Module<T> { ...@@ -1126,7 +1124,7 @@ impl<T: Trait> Module<T> {
/// Set the current block weight. This should only be used in some integration tests. /// Set the current block weight. This should only be used in some integration tests.
#[cfg(any(feature = "std", test))] #[cfg(any(feature = "std", test))]
pub fn set_block_limits(weight: Weight, len: usize) { pub fn set_block_limits(weight: Weight, len: usize) {
AllExtrinsicsWeight::mutate(|current_weight| { BlockWeight::mutate(|current_weight| {
current_weight.put(weight, DispatchClass::Normal) current_weight.put(weight, DispatchClass::Normal)
}); });
AllExtrinsicsLen::put(len as u32); AllExtrinsicsLen::put(len as u32);
...@@ -1383,7 +1381,7 @@ impl<T: Trait + Send + Sync> CheckWeight<T> where ...@@ -1383,7 +1381,7 @@ impl<T: Trait + Send + Sync> CheckWeight<T> where
info: &DispatchInfoOf<T::Call>, info: &DispatchInfoOf<T::Call>,
) -> Result<ExtrinsicsWeight, TransactionValidityError> { ) -> Result<ExtrinsicsWeight, TransactionValidityError> {
let maximum_weight = T::MaximumBlockWeight::get(); let maximum_weight = T::MaximumBlockWeight::get();
let mut all_weight = Module::<T>::all_extrinsics_weight(); let mut all_weight = Module::<T>::block_weight();
match info.class { match info.class {
// If we have a dispatch that must be included in the block, it ignores all the limits. // If we have a dispatch that must be included in the block, it ignores all the limits.
DispatchClass::Mandatory => { DispatchClass::Mandatory => {
...@@ -1474,7 +1472,7 @@ impl<T: Trait + Send + Sync> CheckWeight<T> where ...@@ -1474,7 +1472,7 @@ impl<T: Trait + Send + Sync> CheckWeight<T> where
Self::check_extrinsic_weight(info)?; Self::check_extrinsic_weight(info)?;
AllExtrinsicsLen::put(next_len); AllExtrinsicsLen::put(next_len);
AllExtrinsicsWeight::put(next_weight); BlockWeight::put(next_weight);
Ok(()) Ok(())
} }
...@@ -1565,7 +1563,7 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckWeight<T> where ...@@ -1565,7 +1563,7 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckWeight<T> where
let unspent = post_info.calc_unspent(info); let unspent = post_info.calc_unspent(info);
if unspent > 0 { if unspent > 0 {
AllExtrinsicsWeight::mutate(|current_weight| { BlockWeight::mutate(|current_weight| {
current_weight.sub(unspent, info.class); current_weight.sub(unspent, info.class);
}) })
} }
...@@ -2288,7 +2286,7 @@ pub(crate) mod tests { ...@@ -2288,7 +2286,7 @@ pub(crate) mod tests {
let len = 0_usize; let len = 0_usize;
let reset_check_weight = |i, f, s| { let reset_check_weight = |i, f, s| {
AllExtrinsicsWeight::mutate(|current_weight| { BlockWeight::mutate(|current_weight| {
current_weight.put(s, DispatchClass::Normal) current_weight.put(s, DispatchClass::Normal)
}); });
let r = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, i, len); let r = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, i, len);
...@@ -2310,19 +2308,19 @@ pub(crate) mod tests { ...@@ -2310,19 +2308,19 @@ pub(crate) mod tests {
let len = 0_usize; let len = 0_usize;
// We allow 75% for normal transaction, so we put 25% - extrinsic base weight // 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 - <Test as Trait>::ExtrinsicBaseWeight::get(), DispatchClass::Normal) current_weight.put(256 - <Test as Trait>::ExtrinsicBaseWeight::get(), DispatchClass::Normal)
}); });
let pre = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, &info, len).unwrap(); let pre = CheckWeight::<Test>(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!( assert!(
CheckWeight::<Test>::post_dispatch(pre, &info, &post_info, len, &Ok(())) CheckWeight::<Test>::post_dispatch(pre, &info, &post_info, len, &Ok(()))
.is_ok() .is_ok()
); );
assert_eq!( assert_eq!(
AllExtrinsicsWeight::get().total(), BlockWeight::get().total(),
post_info.actual_weight.unwrap() + 256, post_info.actual_weight.unwrap() + 256,
); );
}) })
...@@ -2335,13 +2333,13 @@ pub(crate) mod tests { ...@@ -2335,13 +2333,13 @@ pub(crate) mod tests {
let post_info = PostDispatchInfo { actual_weight: Some(700), }; let post_info = PostDispatchInfo { actual_weight: Some(700), };
let len = 0_usize; let len = 0_usize;
AllExtrinsicsWeight::mutate(|current_weight| { BlockWeight::mutate(|current_weight| {
current_weight.put(128, DispatchClass::Normal) current_weight.put(128, DispatchClass::Normal)
}); });
let pre = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, &info, len).unwrap(); let pre = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, &info, len).unwrap();
assert_eq!( assert_eq!(
AllExtrinsicsWeight::get().total(), BlockWeight::get().total(),
info.weight + 128 + <Test as Trait>::ExtrinsicBaseWeight::get(), info.weight + 128 + <Test as Trait>::ExtrinsicBaseWeight::get(),
); );
...@@ -2350,7 +2348,7 @@ pub(crate) mod tests { ...@@ -2350,7 +2348,7 @@ pub(crate) mod tests {
.is_ok() .is_ok()
); );
assert_eq!( assert_eq!(
AllExtrinsicsWeight::get().total(), BlockWeight::get().total(),
info.weight + 128 + <Test as Trait>::ExtrinsicBaseWeight::get(), info.weight + 128 + <Test as Trait>::ExtrinsicBaseWeight::get(),
); );
}) })
...@@ -2363,11 +2361,11 @@ pub(crate) mod tests { ...@@ -2363,11 +2361,11 @@ pub(crate) mod tests {
let len = 0_usize; let len = 0_usize;
// Initial weight from `BlockExecutionWeight` // Initial weight from `BlockExecutionWeight`
assert_eq!(System::all_extrinsics_weight().total(), <Test as Trait>::BlockExecutionWeight::get()); assert_eq!(System::block_weight().total(), <Test as Trait>::BlockExecutionWeight::get());
let r = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, &free, len); let r = CheckWeight::<Test>(PhantomData).pre_dispatch(&1, CALL, &free, len);
assert!(r.is_ok()); assert!(r.is_ok());
assert_eq!( assert_eq!(
System::all_extrinsics_weight().total(), System::block_weight().total(),
<Test as Trait>::ExtrinsicBaseWeight::get() + <Test as Trait>::BlockExecutionWeight::get() <Test as Trait>::ExtrinsicBaseWeight::get() + <Test as Trait>::BlockExecutionWeight::get()
); );
}) })
...@@ -2390,8 +2388,8 @@ pub(crate) mod tests { ...@@ -2390,8 +2388,8 @@ pub(crate) mod tests {
check(|max, len| { check(|max, len| {
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(max, len)); assert_ok!(CheckWeight::<Test>::do_pre_dispatch(max, len));
assert_eq!(System::all_extrinsics_weight().total(), Weight::max_value()); assert_eq!(System::block_weight().total(), Weight::max_value());
assert!(System::all_extrinsics_weight().total() > <Test as Trait>::MaximumBlockWeight::get()); assert!(System::block_weight().total() > <Test as Trait>::MaximumBlockWeight::get());
}); });
check(|max, len| { check(|max, len| {
assert_ok!(CheckWeight::<Test>::do_validate(max, len)); assert_ok!(CheckWeight::<Test>::do_validate(max, len));
...@@ -2419,8 +2417,8 @@ pub(crate) mod tests { ...@@ -2419,8 +2417,8 @@ pub(crate) mod tests {
fn register_extra_weight_unchecked_doesnt_care_about_limits() { fn register_extra_weight_unchecked_doesnt_care_about_limits() {
new_test_ext().execute_with(|| { new_test_ext().execute_with(|| {
System::register_extra_weight_unchecked(Weight::max_value(), DispatchClass::Normal); System::register_extra_weight_unchecked(Weight::max_value(), DispatchClass::Normal);
assert_eq!(System::all_extrinsics_weight().total(), Weight::max_value()); assert_eq!(System::block_weight().total(), Weight::max_value());
assert!(System::all_extrinsics_weight().total() > <Test as Trait>::MaximumBlockWeight::get()); assert!(System::block_weight().total() > <Test as Trait>::MaximumBlockWeight::get());
}); });
} }
...@@ -2438,10 +2436,10 @@ pub(crate) mod tests { ...@@ -2438,10 +2436,10 @@ pub(crate) mod tests {
let len = 0_usize; let len = 0_usize;
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&max_normal, len)); assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&max_normal, len));
assert_eq!(System::all_extrinsics_weight().total(), 768); assert_eq!(System::block_weight().total(), 768);
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&rest_operational, len)); assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&rest_operational, len));
assert_eq!(<Test as Trait>::MaximumBlockWeight::get(), 1024); assert_eq!(<Test as Trait>::MaximumBlockWeight::get(), 1024);
assert_eq!(System::all_extrinsics_weight().total(), <Test as Trait>::MaximumBlockWeight::get()); assert_eq!(System::block_weight().total(), <Test as Trait>::MaximumBlockWeight::get());
}); });
} }
...@@ -2456,10 +2454,10 @@ pub(crate) mod tests { ...@@ -2456,10 +2454,10 @@ pub(crate) mod tests {
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&rest_operational, len)); assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&rest_operational, len));
// Extra 15 here from block execution + base extrinsic weight // 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::<Test>::do_pre_dispatch(&max_normal, len)); assert_ok!(CheckWeight::<Test>::do_pre_dispatch(&max_normal, len));
assert_eq!(<Test as Trait>::MaximumBlockWeight::get(), 1024); assert_eq!(<Test as Trait>::MaximumBlockWeight::get(), 1024);
assert_eq!(System::all_extrinsics_weight().total(), <Test as Trait>::MaximumBlockWeight::get()); assert_eq!(System::block_weight().total(), <Test as Trait>::MaximumBlockWeight::get());
}); });
} }
...@@ -2489,7 +2487,7 @@ pub(crate) mod tests { ...@@ -2489,7 +2487,7 @@ pub(crate) mod tests {
let normal_limit = normal_weight_limit(); let normal_limit = normal_weight_limit();
// given almost full block // given almost full block
AllExtrinsicsWeight::mutate(|current_weight| { BlockWeight::mutate(|current_weight| {
current_weight.put(normal_limit, DispatchClass::Normal) current_weight.put(normal_limit, DispatchClass::Normal)
}); });
// will not fit. // will not fit.
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment