Skip to content

Commit 1ef0b4f

Browse files
authored
Fix on_runtime_upgrade weight recording (#7480)
* Fix on_runtime_upgrade weight recording * fix naming * Update lib.rs * fix line width * fix line width again
1 parent 9b43231 commit 1ef0b4f

File tree

1 file changed

+49
-7
lines changed

1 file changed

+49
-7
lines changed

src/lib.rs

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ where
234234
extrinsics_root: &System::Hash,
235235
digest: &Digest<System::Hash>,
236236
) {
237+
let mut weight = 0;
237238
if Self::runtime_upgraded() {
238239
// System is not part of `AllModules`, so we need to call this manually.
239-
let mut weight = <frame_system::Module::<System> as OnRuntimeUpgrade>::on_runtime_upgrade();
240+
weight = weight.saturating_add(<frame_system::Module::<System> as OnRuntimeUpgrade>::on_runtime_upgrade());
240241
weight = weight.saturating_add(COnRuntimeUpgrade::on_runtime_upgrade());
241242
weight = weight.saturating_add(<AllModules as OnRuntimeUpgrade>::on_runtime_upgrade());
242-
<frame_system::Module<System>>::register_extra_weight_unchecked(weight, DispatchClass::Mandatory);
243243
}
244244
<frame_system::Module<System>>::initialize(
245245
block_number,
@@ -248,8 +248,10 @@ where
248248
digest,
249249
frame_system::InitKind::Full,
250250
);
251-
<frame_system::Module<System> as OnInitialize<System::BlockNumber>>::on_initialize(*block_number);
252-
let weight = <AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number)
251+
weight = weight.saturating_add(
252+
<frame_system::Module<System> as OnInitialize<System::BlockNumber>>::on_initialize(*block_number)
253+
);
254+
weight = weight.saturating_add(<AllModules as OnInitialize<System::BlockNumber>>::on_initialize(*block_number))
253255
.saturating_add(<System::BlockExecutionWeight as frame_support::traits::Get<_>>::get());
254256
<frame_system::Module::<System>>::register_extra_weight_unchecked(weight, DispatchClass::Mandatory);
255257

@@ -543,7 +545,7 @@ mod tests {
543545

544546
fn on_runtime_upgrade() -> Weight {
545547
sp_io::storage::set(super::TEST_KEY, "module".as_bytes());
546-
0
548+
200
547549
}
548550
}
549551
}
@@ -675,7 +677,7 @@ mod tests {
675677
fn on_runtime_upgrade() -> Weight {
676678
sp_io::storage::set(TEST_KEY, "custom_upgrade".as_bytes());
677679
sp_io::storage::set(CUSTOM_ON_RUNTIME_KEY, &true.encode());
678-
0
680+
100
679681
}
680682
}
681683

@@ -810,7 +812,7 @@ mod tests {
810812
let xt = TestXt::new(Call::Balances(BalancesCall::transfer(33, 0)), sign_extra(1, 0, 0));
811813
let encoded = xt.encode();
812814
let encoded_len = encoded.len() as Weight;
813-
// Block execution weight + on_initialize weight
815+
// on_initialize weight + block execution weight
814816
let base_block_weight = 175 + <Runtime as frame_system::Trait>::BlockExecutionWeight::get();
815817
let limit = AvailableBlockRatio::get() * MaximumBlockWeight::get() - base_block_weight;
816818
let num_to_exhaust_block = limit / (encoded_len + 5);
@@ -1073,4 +1075,44 @@ mod tests {
10731075
assert_eq!(sp_io::storage::get(CUSTOM_ON_RUNTIME_KEY).unwrap(), true.encode());
10741076
});
10751077
}
1078+
1079+
#[test]
1080+
fn all_weights_are_recorded_correctly() {
1081+
new_test_ext(1).execute_with(|| {
1082+
// Make sure `on_runtime_upgrade` is called for maximum complexity
1083+
RUNTIME_VERSION.with(|v| *v.borrow_mut() = sp_version::RuntimeVersion {
1084+
spec_version: 1,
1085+
..Default::default()
1086+
});
1087+
1088+
let block_number = 1;
1089+
1090+
Executive::initialize_block(&Header::new(
1091+
block_number,
1092+
H256::default(),
1093+
H256::default(),
1094+
[69u8; 32].into(),
1095+
Digest::default(),
1096+
));
1097+
1098+
// All weights that show up in the `initialize_block_impl`
1099+
let frame_system_upgrade_weight = frame_system::Module::<Runtime>::on_runtime_upgrade();
1100+
let custom_runtime_upgrade_weight = CustomOnRuntimeUpgrade::on_runtime_upgrade();
1101+
let runtime_upgrade_weight = <AllModules as OnRuntimeUpgrade>::on_runtime_upgrade();
1102+
let frame_system_on_initialize_weight = frame_system::Module::<Runtime>::on_initialize(block_number);
1103+
let on_initialize_weight = <AllModules as OnInitialize<u64>>::on_initialize(block_number);
1104+
let base_block_weight = <Runtime as frame_system::Trait>::BlockExecutionWeight::get();
1105+
1106+
// Weights are recorded correctly
1107+
assert_eq!(
1108+
frame_system::Module::<Runtime>::block_weight().total(),
1109+
frame_system_upgrade_weight +
1110+
custom_runtime_upgrade_weight +
1111+
runtime_upgrade_weight +
1112+
frame_system_on_initialize_weight +
1113+
on_initialize_weight +
1114+
base_block_weight,
1115+
);
1116+
});
1117+
}
10761118
}

0 commit comments

Comments
 (0)