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
This commit is contained in:
Shawn Tabrizi
2020-05-21 16:13:13 +02:00
committed by GitHub
parent ac049a97be
commit 6ae0de5700
3 changed files with 62 additions and 41 deletions
+1 -1
View File
@@ -62,7 +62,7 @@ pub struct TargetedFeeAdjustment<T>(sp_std::marker::PhantomData<T>);
impl<T: Get<Perquintill>> Convert<Fixed128, Fixed128> for TargetedFeeAdjustment<T> {
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;
+32 -9
View File
@@ -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!(<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 {
let xt = TestXt::new(
@@ -799,7 +799,7 @@ mod tests {
if nonce != num_to_exhaust_block {
assert!(res.is_ok());
assert_eq!(
<frame_system::Module<Runtime>>::all_extrinsics_weight().total(),
<frame_system::Module<Runtime>>::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!(<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!(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 + <Runtime as frame_system::Trait>::ExtrinsicBaseWeight::get();
assert_eq!(
<frame_system::Module<Runtime>>::all_extrinsics_weight().total(),
3 * (len as Weight + <Runtime as frame_system::Trait>::ExtrinsicBaseWeight::get()),
<frame_system::Module<Runtime>>::block_weight().total(),
base_block_weight + 3 * extrinsic_weight,
);
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_len(), 3 * len);
let _ = <frame_system::Module<Runtime>>::finalize();
assert_eq!(<frame_system::Module<Runtime>>::all_extrinsics_weight().total(), 0);
// All extrinsics length cleaned on `System::finalize`
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 {
// 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!(<frame_system::Module<Runtime>>::all_extrinsics_weight().total(), 175 + 10);
assert_eq!(<frame_system::Module<Runtime>>::block_weight().total(), 175 + 10);
})
}
+29 -31
View File
@@ -429,8 +429,8 @@ decl_storage! {
/// Total extrinsics count for the current block.
ExtrinsicCount: Option<u32>;
/// 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<u32>;
@@ -978,11 +978,6 @@ impl<T: Trait> Module<T> {
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<T: Trait> Module<T> {
///
/// 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<T: Trait> Module<T> {
<BlockHash<T>>::insert(*number - One::one(), parent_hash);
<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 {
<Events<T>>::kill();
EventCount::kill();
@@ -1036,7 +1035,6 @@ impl<T: Trait> Module<T> {
pub fn finalize() -> T::Header {
ExecutionPhase::kill();
ExtrinsicCount::kill();
AllExtrinsicsWeight::kill();
AllExtrinsicsLen::kill();
let number = <Number<T>>::take();
@@ -1126,7 +1124,7 @@ impl<T: Trait> Module<T> {
/// 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<T: Trait + Send + Sync> CheckWeight<T> where
info: &DispatchInfoOf<T::Call>,
) -> Result<ExtrinsicsWeight, TransactionValidityError> {
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 {
// If we have a dispatch that must be included in the block, it ignores all the limits.
DispatchClass::Mandatory => {
@@ -1474,7 +1472,7 @@ impl<T: Trait + Send + Sync> CheckWeight<T> where
Self::check_extrinsic_weight(info)?;
AllExtrinsicsLen::put(next_len);
AllExtrinsicsWeight::put(next_weight);
BlockWeight::put(next_weight);
Ok(())
}
@@ -1565,7 +1563,7 @@ impl<T: Trait + Send + Sync> SignedExtension for CheckWeight<T> 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::<Test>(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 - <Test as Trait>::ExtrinsicBaseWeight::get(), DispatchClass::Normal)
});
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!(
CheckWeight::<Test>::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::<Test>(PhantomData).pre_dispatch(&1, CALL, &info, len).unwrap();
assert_eq!(
AllExtrinsicsWeight::get().total(),
BlockWeight::get().total(),
info.weight + 128 + <Test as Trait>::ExtrinsicBaseWeight::get(),
);
@@ -2350,7 +2348,7 @@ pub(crate) mod tests {
.is_ok()
);
assert_eq!(
AllExtrinsicsWeight::get().total(),
BlockWeight::get().total(),
info.weight + 128 + <Test as Trait>::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(), <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);
assert!(r.is_ok());
assert_eq!(
System::all_extrinsics_weight().total(),
System::block_weight().total(),
<Test as Trait>::ExtrinsicBaseWeight::get() + <Test as Trait>::BlockExecutionWeight::get()
);
})
@@ -2390,8 +2388,8 @@ pub(crate) mod tests {
check(|max, len| {
assert_ok!(CheckWeight::<Test>::do_pre_dispatch(max, len));
assert_eq!(System::all_extrinsics_weight().total(), Weight::max_value());
assert!(System::all_extrinsics_weight().total() > <Test as Trait>::MaximumBlockWeight::get());
assert_eq!(System::block_weight().total(), Weight::max_value());
assert!(System::block_weight().total() > <Test as Trait>::MaximumBlockWeight::get());
});
check(|max, len| {
assert_ok!(CheckWeight::<Test>::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() > <Test as Trait>::MaximumBlockWeight::get());
assert_eq!(System::block_weight().total(), Weight::max_value());
assert!(System::block_weight().total() > <Test as Trait>::MaximumBlockWeight::get());
});
}
@@ -2438,10 +2436,10 @@ pub(crate) mod tests {
let len = 0_usize;
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_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 {
assert_ok!(CheckWeight::<Test>::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::<Test>::do_pre_dispatch(&max_normal, len));
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 {
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.