78
78
79
79
use rstd:: { prelude:: * , marker:: PhantomData } ;
80
80
use sr_primitives:: {
81
- generic:: Digest , ApplyResult , weights:: GetDispatchInfo ,
81
+ generic:: Digest , ApplyResult ,
82
+ weights:: { GetDispatchInfo , WeighBlock } ,
82
83
traits:: {
83
84
self , Header , Zero , One , Checkable , Applyable , CheckEqual , OnFinalize , OnInitialize ,
84
85
NumberFor , Block as BlockT , OffchainWorker , Dispatchable ,
@@ -110,7 +111,11 @@ impl<
110
111
Block : traits:: Block < Header =System :: Header , Hash =System :: Hash > ,
111
112
Context : Default ,
112
113
UnsignedValidator ,
113
- AllModules : OnInitialize < System :: BlockNumber > + OnFinalize < System :: BlockNumber > + OffchainWorker < System :: BlockNumber > ,
114
+ AllModules :
115
+ OnInitialize < System :: BlockNumber > +
116
+ OnFinalize < System :: BlockNumber > +
117
+ OffchainWorker < System :: BlockNumber > +
118
+ WeighBlock < System :: BlockNumber > ,
114
119
> ExecuteBlock < Block > for Executive < System , Block , Context , UnsignedValidator , AllModules >
115
120
where
116
121
Block :: Extrinsic : Checkable < Context > + Codec ,
@@ -130,7 +135,11 @@ impl<
130
135
Block : traits:: Block < Header =System :: Header , Hash =System :: Hash > ,
131
136
Context : Default ,
132
137
UnsignedValidator ,
133
- AllModules : OnInitialize < System :: BlockNumber > + OnFinalize < System :: BlockNumber > + OffchainWorker < System :: BlockNumber > ,
138
+ AllModules :
139
+ OnInitialize < System :: BlockNumber > +
140
+ OnFinalize < System :: BlockNumber > +
141
+ OffchainWorker < System :: BlockNumber > +
142
+ WeighBlock < System :: BlockNumber > ,
134
143
> Executive < System , Block , Context , UnsignedValidator , AllModules >
135
144
where
136
145
Block :: Extrinsic : Checkable < Context > + Codec ,
@@ -154,6 +163,12 @@ where
154
163
) {
155
164
<system:: Module < System > >:: initialize ( block_number, parent_hash, extrinsics_root, digest) ;
156
165
<AllModules as OnInitialize < System :: BlockNumber > >:: on_initialize ( * block_number) ;
166
+ <system:: Module < System > >:: register_extra_weight_unchecked (
167
+ <AllModules as WeighBlock < System :: BlockNumber > >:: on_initialize ( * block_number)
168
+ ) ;
169
+ <system:: Module < System > >:: register_extra_weight_unchecked (
170
+ <AllModules as WeighBlock < System :: BlockNumber > >:: on_finalize ( * block_number)
171
+ ) ;
157
172
}
158
173
159
174
fn initial_checks ( block : & Block ) {
@@ -309,12 +324,48 @@ mod tests {
309
324
impl_outer_event, impl_outer_origin, parameter_types, impl_outer_dispatch,
310
325
traits:: { Currency , LockIdentifier , LockableCurrency , WithdrawReasons , WithdrawReason } ,
311
326
} ;
312
- use system:: Call as SystemCall ;
327
+ use system:: { Call as SystemCall , ChainContext } ;
313
328
use balances:: Call as BalancesCall ;
314
329
use hex_literal:: hex;
315
330
331
+ mod custom {
332
+ use sr_primitives:: weights:: SimpleDispatchInfo ;
333
+
334
+ pub trait Trait : system:: Trait { }
335
+
336
+ support:: decl_module! {
337
+ pub struct Module <T : Trait > for enum Call where origin: T :: Origin {
338
+ #[ weight = SimpleDispatchInfo :: FixedNormal ( 100 ) ]
339
+ fn some_function( origin) {
340
+ // NOTE: does not make any different.
341
+ let _ = system:: ensure_signed( origin) ;
342
+ }
343
+ #[ weight = SimpleDispatchInfo :: FixedOperational ( 200 ) ]
344
+ fn some_root_operation( origin) {
345
+ let _ = system:: ensure_root( origin) ;
346
+ }
347
+ #[ weight = SimpleDispatchInfo :: FreeNormal ]
348
+ fn some_unsigned_message( origin) {
349
+ let _ = system:: ensure_none( origin) ;
350
+ }
351
+
352
+ // module hooks.
353
+ // one with block number arg and one without
354
+ #[ weight = SimpleDispatchInfo :: FixedNormal ( 25 ) ]
355
+ fn on_initialize( n: T :: BlockNumber ) {
356
+ println!( "on_initialize({})" , n) ;
357
+ }
358
+ #[ weight = SimpleDispatchInfo :: FixedNormal ( 150 ) ]
359
+ fn on_finalize( ) {
360
+ println!( "on_finalize(?)" ) ;
361
+ }
362
+ }
363
+ }
364
+ }
365
+
316
366
type System = system:: Module < Runtime > ;
317
367
type Balances = balances:: Module < Runtime > ;
368
+ type Custom = custom:: Module < Runtime > ;
318
369
319
370
impl_outer_origin ! {
320
371
pub enum Origin for Runtime { }
@@ -386,6 +437,7 @@ mod tests {
386
437
type WeightToFee = ConvertInto ;
387
438
type FeeMultiplierUpdate = ( ) ;
388
439
}
440
+ impl custom:: Trait for Runtime { }
389
441
390
442
#[ allow( deprecated) ]
391
443
impl ValidateUnsigned for Runtime {
@@ -409,8 +461,9 @@ mod tests {
409
461
system:: CheckWeight < Runtime > ,
410
462
transaction_payment:: ChargeTransactionPayment < Runtime >
411
463
) ;
464
+ type AllModules = ( System , Balances , Custom ) ;
412
465
type TestXt = sr_primitives:: testing:: TestXt < Call , SignedExtra > ;
413
- type Executive = super :: Executive < Runtime , Block < TestXt > , system :: ChainContext < Runtime > , Runtime , ( ) > ;
466
+ type Executive = super :: Executive < Runtime , Block < TestXt > , ChainContext < Runtime > , Runtime , AllModules > ;
414
467
415
468
fn extra ( nonce : u64 , fee : u64 ) -> SignedExtra {
416
469
(
@@ -534,7 +587,7 @@ mod tests {
534
587
let xt = sr_primitives:: testing:: TestXt ( sign_extra ( 1 , 0 , 0 ) , Call :: Balances ( BalancesCall :: transfer ( 33 , 0 ) ) ) ;
535
588
let encoded = xt. encode ( ) ;
536
589
let encoded_len = encoded. len ( ) as Weight ;
537
- let limit = AvailableBlockRatio :: get ( ) * MaximumBlockWeight :: get ( ) ;
590
+ let limit = AvailableBlockRatio :: get ( ) * MaximumBlockWeight :: get ( ) - 175 ;
538
591
let num_to_exhaust_block = limit / encoded_len;
539
592
t. execute_with ( || {
540
593
Executive :: initialize_block ( & Header :: new (
@@ -544,7 +597,8 @@ mod tests {
544
597
[ 69u8 ; 32 ] . into ( ) ,
545
598
Digest :: default ( ) ,
546
599
) ) ;
547
- assert_eq ! ( <system:: Module <Runtime >>:: all_extrinsics_weight( ) , 0 ) ;
600
+ // Initial block weight form the custom module.
601
+ assert_eq ! ( <system:: Module <Runtime >>:: all_extrinsics_weight( ) , 175 ) ;
548
602
549
603
for nonce in 0 ..=num_to_exhaust_block {
550
604
let xt = sr_primitives:: testing:: TestXt (
@@ -555,7 +609,7 @@ mod tests {
555
609
assert ! ( res. is_ok( ) ) ;
556
610
assert_eq ! (
557
611
<system:: Module <Runtime >>:: all_extrinsics_weight( ) ,
558
- encoded_len * ( nonce + 1 ) ,
612
+ encoded_len * ( nonce + 1 ) + 175 ,
559
613
) ;
560
614
assert_eq ! ( <system:: Module <Runtime >>:: extrinsic_index( ) , Some ( nonce as u32 + 1 ) ) ;
561
615
} else {
@@ -652,4 +706,15 @@ mod tests {
652
706
execute_with_lock ( WithdrawReasons :: all ( ) ) ;
653
707
execute_with_lock ( WithdrawReasons :: except ( WithdrawReason :: TransactionPayment ) ) ;
654
708
}
709
+
710
+ #[ test]
711
+ fn block_hooks_weight_is_stored ( ) {
712
+ new_test_ext ( 0 ) . execute_with ( || {
713
+
714
+ Executive :: initialize_block ( & Header :: new_from_number ( 1 ) ) ;
715
+ // NOTE: might need updates over time if system and balance introduce new weights. For
716
+ // now only accounts for the custom module.
717
+ assert_eq ! ( <system:: Module <Runtime >>:: all_extrinsics_weight( ) , 150 + 25 ) ;
718
+ } )
719
+ }
655
720
}
0 commit comments