@@ -234,12 +234,12 @@ where
234
234
extrinsics_root : & System :: Hash ,
235
235
digest : & Digest < System :: Hash > ,
236
236
) {
237
+ let mut weight = 0 ;
237
238
if Self :: runtime_upgraded ( ) {
238
239
// 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 ( ) ) ;
240
241
weight = weight. saturating_add ( COnRuntimeUpgrade :: on_runtime_upgrade ( ) ) ;
241
242
weight = weight. saturating_add ( <AllModules as OnRuntimeUpgrade >:: on_runtime_upgrade ( ) ) ;
242
- <frame_system:: Module < System > >:: register_extra_weight_unchecked ( weight, DispatchClass :: Mandatory ) ;
243
243
}
244
244
<frame_system:: Module < System > >:: initialize (
245
245
block_number,
@@ -248,8 +248,10 @@ where
248
248
digest,
249
249
frame_system:: InitKind :: Full ,
250
250
) ;
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) )
253
255
. saturating_add ( <System :: BlockExecutionWeight as frame_support:: traits:: Get < _ > >:: get ( ) ) ;
254
256
<frame_system:: Module :: < System > >:: register_extra_weight_unchecked ( weight , DispatchClass :: Mandatory ) ;
255
257
@@ -543,7 +545,7 @@ mod tests {
543
545
544
546
fn on_runtime_upgrade( ) -> Weight {
545
547
sp_io:: storage:: set( super :: TEST_KEY , "module" . as_bytes( ) ) ;
546
- 0
548
+ 200
547
549
}
548
550
}
549
551
}
@@ -675,7 +677,7 @@ mod tests {
675
677
fn on_runtime_upgrade ( ) -> Weight {
676
678
sp_io:: storage:: set ( TEST_KEY , "custom_upgrade" . as_bytes ( ) ) ;
677
679
sp_io:: storage:: set ( CUSTOM_ON_RUNTIME_KEY , & true . encode ( ) ) ;
678
- 0
680
+ 100
679
681
}
680
682
}
681
683
@@ -810,7 +812,7 @@ mod tests {
810
812
let xt = TestXt :: new ( Call :: Balances ( BalancesCall :: transfer ( 33 , 0 ) ) , sign_extra ( 1 , 0 , 0 ) ) ;
811
813
let encoded = xt. encode ( ) ;
812
814
let encoded_len = encoded. len ( ) as Weight ;
813
- // Block execution weight + on_initialize weight
815
+ // on_initialize weight + block execution weight
814
816
let base_block_weight = 175 + <Runtime as frame_system:: Trait >:: BlockExecutionWeight :: get ( ) ;
815
817
let limit = AvailableBlockRatio :: get ( ) * MaximumBlockWeight :: get ( ) - base_block_weight;
816
818
let num_to_exhaust_block = limit / ( encoded_len + 5 ) ;
@@ -1073,4 +1075,44 @@ mod tests {
1073
1075
assert_eq ! ( sp_io:: storage:: get( CUSTOM_ON_RUNTIME_KEY ) . unwrap( ) , true . encode( ) ) ;
1074
1076
} ) ;
1075
1077
}
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
+ }
1076
1118
}
0 commit comments