-
Notifications
You must be signed in to change notification settings - Fork 230
/
handle_periodic_operations.go
138 lines (109 loc) · 4.03 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package consensus
import (
"fmt"
"github.com/go-co-op/gocron"
"github.com/rs/zerolog/log"
"github.com/forbole/bdjuno/database"
"github.com/forbole/bdjuno/modules/utils"
)
// Register registers the utils that should be run periodically
func Register(scheduler *gocron.Scheduler, db *database.Db) error {
log.Debug().Str("module", "consensus").Msg("setting up periodic tasks")
if _, err := scheduler.Every(1).Minute().Do(func() {
utils.WatchMethod(func() error { return updateBlockTimeInMinute(db) })
}); err != nil {
return fmt.Errorf("error while setting up consensus periodic operation: %s", err)
}
if _, err := scheduler.Every(1).Hour().StartImmediately().Do(func() {
utils.WatchMethod(func() error { return updateBlockTimeInHour(db) })
}); err != nil {
return fmt.Errorf("error while setting up consensus periodic operation: %s", err)
}
if _, err := scheduler.Every(1).Day().StartImmediately().Do(func() {
utils.WatchMethod(func() error { return updateBlockTimeInDay(db) })
}); err != nil {
return fmt.Errorf("error while setting up consensus periodic operation: %s", err)
}
return nil
}
// updateBlockTimeInMinute insert average block time in the latest minute
func updateBlockTimeInMinute(db *database.Db) error {
log.Trace().Str("module", "consensus").Str("operation", "block time").
Msg("updating block time in minutes")
block, err := db.GetLastBlock()
if err != nil {
return fmt.Errorf("error while getting last block: %s", err)
}
genesis, err := db.GetGenesis()
if err != nil {
return fmt.Errorf("error while getting genesis: %s", err)
}
// Skip if the genesis does not exist
if genesis == nil {
return nil
}
// Check if the chain has been created at least a minute ago
if block.Timestamp.Sub(genesis.Time).Minutes() < 0 {
return nil
}
minute, err := db.GetBlockHeightTimeMinuteAgo(block.Timestamp)
if err != nil {
return fmt.Errorf("error while gettting block height a minute ago: %s", err)
}
newBlockTime := block.Timestamp.Sub(minute.Timestamp).Seconds() / float64(block.Height-minute.Height)
return db.SaveAverageBlockTimePerMin(newBlockTime, block.Height)
}
// updateBlockTimeInHour insert average block time in the latest hour
func updateBlockTimeInHour(db *database.Db) error {
log.Trace().Str("module", "consensus").Str("operation", "block time").
Msg("updating block time in hours")
block, err := db.GetLastBlock()
if err != nil {
return fmt.Errorf("error while getting last block: %s", err)
}
genesis, err := db.GetGenesis()
if err != nil {
return fmt.Errorf("error while getting genesis: %s", err)
}
// Skip if the genesis does not exist
if genesis == nil {
return nil
}
// Check if the chain has been created at least an hour ago
if block.Timestamp.Sub(genesis.Time).Hours() < 0 {
return nil
}
hour, err := db.GetBlockHeightTimeHourAgo(block.Timestamp)
if err != nil {
return fmt.Errorf("error while getting block height an hour ago: %s", err)
}
newBlockTime := block.Timestamp.Sub(hour.Timestamp).Seconds() / float64(block.Height-hour.Height)
return db.SaveAverageBlockTimePerHour(newBlockTime, block.Height)
}
// updateBlockTimeInDay insert average block time in the latest minute
func updateBlockTimeInDay(db *database.Db) error {
log.Trace().Str("module", "consensus").Str("operation", "block time").
Msg("updating block time in days")
block, err := db.GetLastBlock()
if err != nil {
return fmt.Errorf("error while getting last block: %s", err)
}
genesis, err := db.GetGenesis()
if err != nil {
return fmt.Errorf("error while getting genesis: %s", err)
}
// Skip if the genesis does not exist
if genesis == nil {
return nil
}
// Check if the chain has been created at least a days ago
if block.Timestamp.Sub(genesis.Time).Hours() < 24 {
return nil
}
day, err := db.GetBlockHeightTimeDayAgo(block.Timestamp)
if err != nil {
return fmt.Errorf("error while getting block time a day ago: %s", err)
}
newBlockTime := block.Timestamp.Sub(day.Timestamp).Seconds() / float64(block.Height-day.Height)
return db.SaveAverageBlockTimePerDay(newBlockTime, block.Height)
}