mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 17:01:09 +00:00
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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user