@@ -31,9 +31,16 @@ use frame_system::ensure_root;
3131use sp_runtime:: { traits:: SaturatedConversion , DispatchResult , RuntimeDebug } ;
3232use sp_std:: prelude:: Vec ;
3333
34+ mod default_weight;
3435mod mock;
3536mod tests;
3637
38+ pub trait WeightInfo {
39+ fn gradually_update ( ) -> Weight ;
40+ fn cancel_gradually_update ( ) -> Weight ;
41+ fn on_initialize ( need_update : bool , update_len : usize ) -> Weight ;
42+ }
43+
3744type StorageKey = Vec < u8 > ;
3845type StorageValue = Vec < u8 > ;
3946
@@ -42,11 +49,11 @@ type StorageValue = Vec<u8>;
4249#[ derive( Encode , Decode , Clone , Eq , PartialEq , RuntimeDebug ) ]
4350pub struct GraduallyUpdate {
4451 /// The storage key of the value to update
45- key : StorageKey ,
52+ pub key : StorageKey ,
4653 /// The target value
47- target_value : StorageValue ,
54+ pub target_value : StorageValue ,
4855 /// The amount of the value to update per one block
49- per_block : StorageValue ,
56+ pub per_block : StorageValue ,
5057}
5158
5259pub trait Trait : frame_system:: Trait {
@@ -55,6 +62,8 @@ pub trait Trait: frame_system::Trait {
5562 type UpdateFrequency : Get < Self :: BlockNumber > ;
5663 /// The origin that can schedule an update
5764 type DispatchOrigin : EnsureOrigin < Self :: Origin > ;
65+ /// Weight information for extrinsics in this module.
66+ type WeightInfo : WeightInfo ;
5867}
5968
6069decl_storage ! {
@@ -103,7 +112,7 @@ decl_module! {
103112 const UpdateFrequency : T :: BlockNumber = T :: UpdateFrequency :: get( ) ;
104113
105114 /// Add gradually_update to adjust numeric parameter.
106- #[ weight = 10_000 ]
115+ #[ weight = T :: WeightInfo :: gradually_update ( ) ]
107116 pub fn gradually_update( origin, update: GraduallyUpdate ) {
108117 T :: DispatchOrigin :: try_origin( origin) . map( |_| ( ) ) . or_else( ensure_root) ?;
109118
@@ -127,7 +136,7 @@ decl_module! {
127136 }
128137
129138 /// Cancel gradually_update to adjust numeric parameter.
130- #[ weight = 10_000 ]
139+ #[ weight = T :: WeightInfo :: cancel_gradually_update ( ) ]
131140 pub fn cancel_gradually_update( origin, key: StorageKey ) {
132141 T :: DispatchOrigin :: try_origin( origin) . map( |_| ( ) ) . or_else( ensure_root) ?;
133142
@@ -143,10 +152,10 @@ decl_module! {
143152 Self :: deposit_event( RawEvent :: GraduallyUpdateCancelled ( key) ) ;
144153 }
145154
146- /// dummy `on_initialize` to return the weight used in `on_finalize`.
155+ /// `on_initialize` to return the weight used in `on_finalize`.
147156 fn on_initialize( ) -> Weight {
148- // weight of `on_finalize`
149- 0
157+ let now = <frame_system :: Module < T >> :: block_number ( ) ;
158+ T :: WeightInfo :: on_initialize ( Self :: _need_update ( now ) , GraduallyUpdates :: get ( ) . len ( ) )
150159 }
151160
152161 /// Update gradually_update to adjust numeric parameter.
@@ -157,8 +166,12 @@ decl_module! {
157166}
158167
159168impl < T : Trait > Module < T > {
169+ fn _need_update ( now : T :: BlockNumber ) -> bool {
170+ now >= Self :: last_updated_at ( ) + T :: UpdateFrequency :: get ( )
171+ }
172+
160173 fn _on_finalize ( now : T :: BlockNumber ) {
161- if now < Self :: last_updated_at ( ) + T :: UpdateFrequency :: get ( ) {
174+ if ! Self :: _need_update ( now ) {
162175 return ;
163176 }
164177
0 commit comments