@@ -46,31 +46,28 @@ pub mod pallet {
46
46
47
47
#[ pallet:: hooks]
48
48
impl < T : Config > Hooks < BlockNumberFor < T > > for Pallet < T > {
49
- // on_initialize() will be called at the beginning of each new block, before anything
50
49
fn on_initialize ( n : T :: BlockNumber ) -> Weight {
51
50
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) ;
58
52
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 ;
63
54
64
55
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 ;
66
58
}
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 ) ;
67
64
68
- // TODO: clean the storage, a.k remove the events, after emitting them
69
65
used_weight
70
66
}
71
67
72
68
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) ) ;
74
71
}
75
72
}
76
73
0 commit comments