5252#![ cfg_attr( not( feature = "std" ) , no_std) ]
5353
5454mod benchmarking;
55+ mod default_weights;
5556
5657use sp_std:: { prelude:: * , marker:: PhantomData , borrow:: Borrow } ;
5758use codec:: { Encode , Decode , Codec } ;
@@ -69,15 +70,6 @@ pub trait WeightInfo {
6970 fn cancel ( s : u32 , ) -> Weight ;
7071 fn schedule_named ( s : u32 , ) -> Weight ;
7172 fn cancel_named ( s : u32 , ) -> Weight ;
72- fn on_initialize ( s : u32 , ) -> Weight ;
73- }
74-
75- impl WeightInfo for ( ) {
76- fn schedule ( _s : u32 , ) -> Weight { 1_000_000_000 }
77- fn cancel ( _s : u32 , ) -> Weight { 1_000_000_000 }
78- fn schedule_named ( _s : u32 , ) -> Weight { 1_000_000_000 }
79- fn cancel_named ( _s : u32 , ) -> Weight { 1_000_000_000 }
80- fn on_initialize ( _s : u32 , ) -> Weight { 1_000_000_000 }
8173}
8274
8375/// Our pallet's configuration trait. All our types and constants go in here. If the
@@ -106,6 +98,10 @@ pub trait Trait: system::Trait {
10698 /// Required origin to schedule or cancel calls.
10799 type ScheduleOrigin : EnsureOrigin < <Self as system:: Trait >:: Origin > ;
108100
101+ /// The maximum number of scheduled calls in the queue for a single block.
102+ /// Not strictly enforced, but used for weight estimation.
103+ type MaxScheduledPerBlock : Get < u32 > ;
104+
109105 /// Weight information for extrinsics in this pallet.
110106 type WeightInfo : WeightInfo ;
111107}
@@ -213,7 +209,7 @@ decl_module! {
213209 /// - Write: Agenda
214210 /// - Will use base weight of 25 which should be good for up to 30 scheduled calls
215211 /// # </weight>
216- #[ weight = 25_000_000 + T :: DbWeight :: get( ) . reads_writes ( 1 , 1 ) ]
212+ #[ weight = T :: WeightInfo :: schedule ( T :: MaxScheduledPerBlock :: get( ) ) ]
217213 fn schedule( origin,
218214 when: T :: BlockNumber ,
219215 maybe_periodic: Option <schedule:: Period <T :: BlockNumber >>,
@@ -235,7 +231,7 @@ decl_module! {
235231 /// - Write: Agenda, Lookup
236232 /// - Will use base weight of 100 which should be good for up to 30 scheduled calls
237233 /// # </weight>
238- #[ weight = 100_000_000 + T :: DbWeight :: get( ) . reads_writes ( 1 , 2 ) ]
234+ #[ weight = T :: WeightInfo :: cancel ( T :: MaxScheduledPerBlock :: get( ) ) ]
239235 fn cancel( origin, when: T :: BlockNumber , index: u32 ) {
240236 T :: ScheduleOrigin :: ensure_origin( origin. clone( ) ) ?;
241237 let origin = <T as Trait >:: Origin :: from( origin) ;
@@ -252,7 +248,7 @@ decl_module! {
252248 /// - Write: Agenda, Lookup
253249 /// - Will use base weight of 35 which should be good for more than 30 scheduled calls
254250 /// # </weight>
255- #[ weight = 35_000_000 + T :: DbWeight :: get( ) . reads_writes ( 2 , 2 ) ]
251+ #[ weight = T :: WeightInfo :: schedule_named ( T :: MaxScheduledPerBlock :: get( ) ) ]
256252 fn schedule_named( origin,
257253 id: Vec <u8 >,
258254 when: T :: BlockNumber ,
@@ -277,7 +273,7 @@ decl_module! {
277273 /// - Write: Agenda, Lookup
278274 /// - Will use base weight of 100 which should be good for up to 30 scheduled calls
279275 /// # </weight>
280- #[ weight = 100_000_000 + T :: DbWeight :: get( ) . reads_writes ( 2 , 2 ) ]
276+ #[ weight = T :: WeightInfo :: cancel_named ( T :: MaxScheduledPerBlock :: get( ) ) ]
281277 fn cancel_named( origin, id: Vec <u8 >) {
282278 T :: ScheduleOrigin :: ensure_origin( origin. clone( ) ) ?;
283279 let origin = <T as Trait >:: Origin :: from( origin) ;
@@ -289,7 +285,7 @@ decl_module! {
289285 /// # <weight>
290286 /// Same as [`schedule`].
291287 /// # </weight>
292- #[ weight = 25_000_000 + T :: DbWeight :: get( ) . reads_writes ( 1 , 1 ) ]
288+ #[ weight = T :: WeightInfo :: schedule ( T :: MaxScheduledPerBlock :: get( ) ) ]
293289 fn schedule_after( origin,
294290 after: T :: BlockNumber ,
295291 maybe_periodic: Option <schedule:: Period <T :: BlockNumber >>,
@@ -308,7 +304,7 @@ decl_module! {
308304 /// # <weight>
309305 /// Same as [`schedule_named`].
310306 /// # </weight>
311- #[ weight = 35_000_000 + T :: DbWeight :: get( ) . reads_writes ( 2 , 2 ) ]
307+ #[ weight = T :: WeightInfo :: schedule_named ( T :: MaxScheduledPerBlock :: get( ) ) ]
312308 fn schedule_named_after( origin,
313309 id: Vec <u8 >,
314310 after: T :: BlockNumber ,
@@ -340,16 +336,20 @@ decl_module! {
340336 . enumerate( )
341337 . filter_map( |( index, s) | s. map( |inner| ( index as u32 , inner) ) )
342338 . collect:: <Vec <_>>( ) ;
339+ if queued. len( ) as u32 > T :: MaxScheduledPerBlock :: get( ) {
340+ frame_support:: debug:: warn!(
341+ "Warning: This block has more items queued in Scheduler than \
342+ expected from the runtime configuration. An update might be needed."
343+ ) ;
344+ }
343345 queued. sort_by_key( |( _, s) | s. priority) ;
344- let base_weight: Weight = T :: DbWeight :: get( ) . reads_writes( 1 , 2 ) // Agenda + Agenda(next)
345- . saturating_add( 10_000_000 ) ; // Base Weight
346+ let base_weight: Weight = T :: DbWeight :: get( ) . reads_writes( 1 , 2 ) ; // Agenda + Agenda(next)
346347 let mut total_weight: Weight = 0 ;
347348 queued. into_iter( )
348349 . enumerate( )
349350 . scan( base_weight, |cumulative_weight, ( order, ( index, s) ) | {
350351 * cumulative_weight = cumulative_weight
351- . saturating_add( s. call. get_dispatch_info( ) . weight)
352- . saturating_add( 25_000_000 ) ; // Base multiplier
352+ . saturating_add( s. call. get_dispatch_info( ) . weight) ;
353353
354354 if s. maybe_id. is_some( ) {
355355 // Remove/Modify Lookup
@@ -466,6 +466,12 @@ impl<T: Trait> Module<T> {
466466 } ) ;
467467 Agenda :: < T > :: append ( when, s) ;
468468 let index = Agenda :: < T > :: decode_len ( when) . unwrap_or ( 1 ) as u32 - 1 ;
469+ if index > T :: MaxScheduledPerBlock :: get ( ) {
470+ frame_support:: debug:: warn!(
471+ "Warning: There are more items queued in the Scheduler than \
472+ expected from the runtime configuration. An update might be needed."
473+ ) ;
474+ }
469475 Self :: deposit_event ( RawEvent :: Scheduled ( when, index) ) ;
470476
471477 Ok ( ( when, index) )
@@ -535,6 +541,12 @@ impl<T: Trait> Module<T> {
535541 } ;
536542 Agenda :: < T > :: append ( when, Some ( s) ) ;
537543 let index = Agenda :: < T > :: decode_len ( when) . unwrap_or ( 1 ) as u32 - 1 ;
544+ if index > T :: MaxScheduledPerBlock :: get ( ) {
545+ frame_support:: debug:: warn!(
546+ "Warning: There are more items queued in the Scheduler than \
547+ expected from the runtime configuration. An update might be needed."
548+ ) ;
549+ }
538550 let address = ( when, index) ;
539551 Lookup :: < T > :: insert ( & id, & address) ;
540552 Self :: deposit_event ( RawEvent :: Scheduled ( when, index) ) ;
@@ -734,6 +746,7 @@ mod tests {
734746 }
735747 parameter_types ! {
736748 pub MaximumSchedulerWeight : Weight = Perbill :: from_percent( 80 ) * MaximumBlockWeight :: get( ) ;
749+ pub const MaxScheduledPerBlock : u32 = 10 ;
737750 }
738751 ord_parameter_types ! {
739752 pub const One : u64 = 1 ;
@@ -746,6 +759,7 @@ mod tests {
746759 type Call = Call ;
747760 type MaximumWeight = MaximumSchedulerWeight ;
748761 type ScheduleOrigin = EnsureOneOf < u64 , EnsureRoot < u64 > , EnsureSignedBy < One , u64 > > ;
762+ type MaxScheduledPerBlock = MaxScheduledPerBlock ;
749763 type WeightInfo = ( ) ;
750764 }
751765 type System = system:: Module < Test > ;
@@ -982,8 +996,8 @@ mod tests {
982996 #[ test]
983997 fn on_initialize_weight_is_correct ( ) {
984998 new_test_ext ( ) . execute_with ( || {
985- let base_weight: Weight = <Test as frame_system:: Trait >:: DbWeight :: get ( ) . reads_writes ( 1 , 2 ) + 10_000_000 ;
986- let base_multiplier = 25_000_000 ;
999+ let base_weight: Weight = <Test as frame_system:: Trait >:: DbWeight :: get ( ) . reads_writes ( 1 , 2 ) ;
1000+ let base_multiplier = 0 ;
9871001 let named_multiplier = <Test as frame_system:: Trait >:: DbWeight :: get ( ) . writes ( 1 ) ;
9881002 let periodic_multiplier = <Test as frame_system:: Trait >:: DbWeight :: get ( ) . reads_writes ( 1 , 1 ) ;
9891003
0 commit comments