/
heimdall_network_block.go
99 lines (83 loc) · 3.38 KB
/
heimdall_network_block.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
package targets
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
client "github.com/influxdata/influxdb1-client/v2"
"github.com/vitwit/matic-jagar/alerter"
"github.com/vitwit/matic-jagar/config"
db "github.com/vitwit/matic-jagar/influxdb"
"github.com/vitwit/matic-jagar/scraper"
"github.com/vitwit/matic-jagar/types"
)
// NetworkLatestBlock is to get latest block height of a network
// Calcualtes height difference of validator and network height and stores it in db
// Alerter will alerts when the block diff thresholds meets the height diff
func NetworkLatestBlock(ops types.HTTPOptions, cfg *config.Config, c client.Client) {
bp, err := db.CreateBatchPoints(cfg.InfluxDB.Database)
if err != nil {
log.Printf("Error: %v", err)
return
}
networkBlock, err := scraper.GetStatus(ops)
if err != nil {
log.Printf("Error in network latest block: %v", err)
return
}
if &networkBlock != nil {
networkBlockHeight, err := strconv.Atoi(networkBlock.Result.SyncInfo.LatestBlockHeight)
if err != nil {
log.Println("Error while converting network height from string to int ", err)
return
}
_ = db.WriteToInfluxDb(c, bp, "heimdall_network_latest_block", map[string]string{}, map[string]interface{}{"block_height": networkBlockHeight})
log.Printf("Network height: %d", networkBlockHeight)
// Get validator block height
ops.Endpoint = ops.Endpoint + cfg.Endpoints.HeimdallRPCEndpoint + "/status?"
ops.Endpoint = ops.Endpoint + http.MethodGet
valStatus, err := scraper.GetStatus(ops)
if err != nil {
log.Printf("Validator Status Error: %v", err)
return
}
if &valStatus == nil {
log.Println("Got an empty response from validator rpc !")
return
}
validatorHeight := valStatus.Result.SyncInfo.LatestBlockHeight
vaidatorBlockHeight, _ := strconv.Atoi(validatorHeight)
heightDiff := networkBlockHeight - vaidatorBlockHeight
_ = db.WriteToInfluxDb(c, bp, "heimdall_height_difference", map[string]string{}, map[string]interface{}{"difference": heightDiff})
log.Printf("Network height: %d and Validator Height: %d", networkBlockHeight, vaidatorBlockHeight)
// Send alert
if int64(heightDiff) >= cfg.AlertingThresholds.BlockDiffThreshold && strings.ToUpper(cfg.AlerterPreferences.BlockDiffAlerts) == "YES" {
_ = alerter.SendTelegramAlert(fmt.Sprintf("⚠️ Heimdall Block Difference Alert: Block Difference between network and validator has exceeded %d", cfg.AlertingThresholds.BlockDiffThreshold), cfg)
_ = alerter.SendEmailAlert(fmt.Sprintf("⚠️ Heimdall Block Difference Alert : Block difference between network and validator has exceeded %d", cfg.AlertingThresholds.BlockDiffThreshold), cfg)
log.Println("Sent alert of block height difference")
}
} else {
log.Println("Got an empty response from external rpc !")
return
}
}
// GetNetworkBlock returns network current block height
func GetNetworkBlock(cfg *config.Config, c client.Client) string {
var networkHeight string
q := client.NewQuery("SELECT last(block_height) FROM heimdall_network_latest_block", cfg.InfluxDB.Database, "")
if response, err := c.Query(q); err == nil && response.Error() == nil {
for _, r := range response.Results {
if len(r.Series) != 0 {
for idx, col := range r.Series[0].Columns {
if col == "last" {
heightValue := r.Series[0].Values[0][idx]
networkHeight = fmt.Sprintf("%v", heightValue)
break
}
}
}
}
}
return networkHeight
}