Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

WeightInfo for System, Timestamp, and Utility #6868

Merged
15 commits merged into from
Aug 17, 2020
Prev Previous commit
Next Next commit
Fix overflow in weight calculations
  • Loading branch information
shawntabrizi committed Aug 13, 2020
commit 50a724f4b4d4d680c9dc30f972975062c35cccb7
2 changes: 1 addition & 1 deletion frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ decl_module! {
/// - Writes: Number of subkeys + 1
/// # </weight>
#[weight = (
T::SystemWeightInfo::kill_prefix(*_subkeys + 1),
T::SystemWeightInfo::kill_prefix(_subkeys.saturating_add(1)),
DispatchClass::Operational,
)]
fn kill_prefix(origin, prefix: Key, _subkeys: u32) {
Expand Down
2 changes: 1 addition & 1 deletion frame/utility/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ decl_module! {
#[weight = (
calls.iter()
.map(|call| call.get_dispatch_info().weight)
.sum::<Weight>()
.fold(0, |total: Weight, weight: Weight| total.saturating_add(weight))
.saturating_add(T::WeightInfo::batch(calls.len() as u32)),
{
let all_operational = calls.iter()
Expand Down
20 changes: 19 additions & 1 deletion frame/utility/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl_outer_dispatch! {
pub struct Test;
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockWeight: Weight = Weight::max_value();
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
Expand Down Expand Up @@ -121,6 +121,7 @@ type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
type Utility = Module<Test>;

use frame_system::Call as SystemCall;
use pallet_balances::Call as BalancesCall;
use pallet_balances::Error as BalancesError;

Expand Down Expand Up @@ -236,3 +237,20 @@ fn batch_early_exit_works() {
assert_eq!(Balances::free_balance(2), 15);
});
}

#[test]
fn batch_weight_calculation_doesnt_overflow() {
new_test_ext().execute_with(|| {
let big_call = Call::System(SystemCall::fill_block(Perbill::from_percent(50)));
assert_eq!(big_call.get_dispatch_info().weight, Weight::max_value() / 2);

// 3 * 50% saturates to 100%
let batch_call = Call::Utility(crate::Call::batch(vec![
big_call.clone(),
big_call.clone(),
big_call.clone(),
]));

assert_eq!(batch_call.get_dispatch_info().weight, Weight::max_value());
});
}