/
handle_periodic_operations.go
44 lines (36 loc) · 1.19 KB
/
handle_periodic_operations.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package consensus
import (
"fmt"
"github.com/forbole/soljuno/modules/utils"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"
)
func (m *Module) RegisterPeriodicOperations(scheduler *gocron.Scheduler) error {
if _, err := scheduler.Every(1).Hour().Do(func() {
utils.WatchMethod(m, m.RunPeriodicOperations)
}); err != nil {
return fmt.Errorf("error while setting up consensus periodic operation: %s", err)
}
return nil
}
func (m *Module) RunPeriodicOperations() error {
return m.updateSlotTimeInHour()
}
// updateSlotTimeInHour insert average slot time in the latest hour
func (m *Module) updateSlotTimeInHour() error {
log.Trace().Str("module", "consensus").Str("operation", "block time").
Msg("updating block time in hours")
block, err := m.db.GetLastBlock()
if err != nil {
return fmt.Errorf("error while getting last block: %s", err)
}
hour, found, err := m.db.GetBlockHourAgo(block.Timestamp)
if err != nil {
return fmt.Errorf("error while getting block an hour ago: %s", err)
}
if !found {
return nil
}
newBlockTime := block.Timestamp.Sub(hour.Timestamp).Seconds() / float64(block.Slot-hour.Slot)
return m.db.SaveAverageSlotTimePerHour(block.Slot, newBlockTime)
}