@@ -46,31 +46,28 @@ pub mod pallet {
4646
4747 #[ pallet:: hooks]
4848 impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
49- // on_initialize() will be called at the beginning of each new block, before anything
5049 fn on_initialize ( n : T :: BlockNumber ) -> Weight {
5150 let mut used_weight = 0 ;
52- // TODO: get the reminders for the block `n`
53- let reminders: Vec < Vec < u8 > > = vec ! [ ] ;
54- // this is an example of how do we get system weights for read and writes.
55- // you only have to mesure read and writes for this exercice !
56- //
57- // try to do this hook in one read and two writes !
51+ let reminders = Self :: reminders ( n) ;
5852 used_weight += T :: DbWeight :: get ( ) . reads ( 1 ) ;
59-
60- // TODO:
61- // find a way to count events for this block, and put the total in the
62- // corresponding storage
53+ let mut event_counter = 0 ;
6354
6455 for reminder in reminders {
65- // TODO: now, emit a `Reminder` event for each events"
56+ Self :: deposit_event ( Event :: Reminder ( reminder. clone ( ) ) ) ;
57+ event_counter += 1 ;
6658 }
59+ <EventCounter < T > >:: mutate ( |value| * value = event_counter) ;
60+ used_weight += T :: DbWeight :: get ( ) . writes ( 1 ) ;
61+
62+ <Reminders < T > >:: remove ( n) ;
63+ used_weight += T :: DbWeight :: get ( ) . writes ( 1 ) ;
6764
68- // TODO: clean the storage, a.k remove the events, after emitting them
6965 used_weight
7066 }
7167
7268 fn on_finalize ( _: T :: BlockNumber ) {
73- // TODO: emit a `RemindersExecutes` event, with the right value
69+ let count = Self :: event_counter ( ) ;
70+ Self :: deposit_event ( Event :: RemindersExecuteds ( count) ) ;
7471 }
7572 }
7673
0 commit comments