/
rpc.go
240 lines (231 loc) · 6.4 KB
/
rpc.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package tenderduty
import (
"context"
"errors"
"fmt"
dash "github.com/blockpane/tenderduty/v2/td2/dashboard"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
"io"
"net/http"
"net/url"
"regexp"
"time"
)
// newRpc sets up the rpc client used for monitoring. It will try nodes in order until a working node is found.
// it will also get some initial info on the validator's status.
func (cc *ChainConfig) newRpc() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var anyWorking bool // if healthchecks are running, we will skip to the first known good node.
for _, endpoint := range cc.Nodes {
anyWorking = anyWorking || !endpoint.down
}
// grab the first working endpoint
tryUrl := func(u string) (msg string, down, syncing bool) {
_, err := url.Parse(u)
if err != nil {
msg = fmt.Sprintf("❌ could not parse url %s: (%s) %s", cc.name, u, err)
l(msg)
down = true
return
}
cc.client, err = rpchttp.New(u, "/websocket")
if err != nil {
msg = fmt.Sprintf("❌ could not connect client for %s: (%s) %s", cc.name, u, err)
l(msg)
down = true
return
}
status, err := cc.client.Status(ctx)
if err != nil {
msg = fmt.Sprintf("❌ could not get status for %s: (%s) %s", cc.name, u, err)
down = true
l(msg)
return
}
if status.NodeInfo.Network != cc.ChainId {
msg = fmt.Sprintf("chain id %s on %s does not match, expected %s, skipping", status.NodeInfo.Network, u, cc.ChainId)
down = true
l(msg)
return
}
if status.SyncInfo.CatchingUp {
msg = fmt.Sprint("🐢 node is not synced, skipping ", u)
syncing = true
down = true
l(msg)
return
}
cc.noNodes = false
return
}
down := func(endpoint *NodeConfig, msg string) {
if !endpoint.down {
endpoint.down = true
endpoint.downSince = time.Now()
}
endpoint.lastMsg = msg
}
for _, endpoint := range cc.Nodes {
if anyWorking && endpoint.down {
continue
}
if msg, failed, syncing := tryUrl(endpoint.Url); failed {
endpoint.syncing = syncing
down(endpoint, msg)
continue
}
return nil
}
if cc.PublicFallback {
if u, ok := getRegistryUrl(cc.ChainId); ok {
node := guessPublicEndpoint(u)
l(cc.ChainId, "⛑ attemtping to use public fallback node", node)
if _, kk, _ := tryUrl(node); !kk {
l(cc.ChainId, "⛑ connected to public endpoint", node)
return nil
}
} else {
l("could not find a public endpoint for", cc.ChainId)
}
}
cc.noNodes = true
alarms.clearAll(cc.name)
cc.lastError = "no usable RPC endpoints available for " + cc.ChainId
if td.EnableDash {
td.updateChan <- &dash.ChainStatus{
MsgType: "status",
Name: cc.name,
ChainId: cc.ChainId,
Moniker: cc.valInfo.Moniker,
Bonded: cc.valInfo.Bonded,
Jailed: cc.valInfo.Jailed,
Tombstoned: cc.valInfo.Tombstoned,
Missed: cc.valInfo.Missed,
Window: cc.valInfo.Window,
Nodes: len(cc.Nodes),
HealthyNodes: 0,
ActiveAlerts: 1,
Height: 0,
LastError: cc.lastError,
Blocks: cc.blocksResults,
}
}
return errors.New("no usable endpoints available for " + cc.ChainId)
}
func (cc *ChainConfig) monitorHealth(ctx context.Context, chainName string) {
tick := time.NewTicker(time.Minute)
if cc.client == nil {
_ = cc.newRpc()
}
for {
select {
case <-ctx.Done():
return
case <-tick.C:
var err error
for _, node := range cc.Nodes {
go func(node *NodeConfig) {
alert := func(msg string) {
node.lastMsg = fmt.Sprintf("%-12s node %s is %s", chainName, node.Url, msg)
if !node.AlertIfDown {
// even if we aren't alerting, we want to display the status in the dashboard.
node.down = true
return
}
if !node.down {
node.down = true
node.downSince = time.Now()
}
if td.Prom {
td.statsChan <- cc.mkUpdate(metricNodeDownSeconds, time.Since(node.downSince).Seconds(), node.Url)
}
l("⚠️ " + node.lastMsg)
}
c, e := rpchttp.New(node.Url, "/websocket")
if e != nil {
alert(e.Error())
}
cwt, cancel := context.WithTimeout(context.Background(), 10*time.Second)
status, e := c.Status(cwt)
cancel()
if e != nil {
alert("down")
return
}
if status.NodeInfo.Network != cc.ChainId {
alert("on the wrong network")
return
}
if status.SyncInfo.CatchingUp {
alert("not synced")
node.syncing = true
return
}
// node's OK, clear the note
if node.down {
node.lastMsg = ""
node.wasDown = true
}
td.statsChan <- cc.mkUpdate(metricNodeDownSeconds, 0, node.Url)
node.down = false
node.syncing = false
node.downSince = time.Unix(0, 0)
cc.noNodes = false
l(fmt.Sprintf("🟢 %-12s node %s is healthy", chainName, node.Url))
}(node)
}
if cc.client == nil {
e := cc.newRpc()
if e != nil {
l("💥", cc.ChainId, e)
}
}
if cc.valInfo != nil {
cc.lastValInfo = &ValInfo{
Moniker: cc.valInfo.Moniker,
Bonded: cc.valInfo.Bonded,
Jailed: cc.valInfo.Jailed,
Tombstoned: cc.valInfo.Tombstoned,
Missed: cc.valInfo.Missed,
Window: cc.valInfo.Window,
Conspub: cc.valInfo.Conspub,
Valcons: cc.valInfo.Valcons,
}
}
err = cc.GetValInfo(false)
if err != nil {
l("❓ refreshing signing info for", cc.ValAddress, err)
}
}
}
}
// endpointRex matches the first a tag's hostname and port if present.
var endpointRex = regexp.MustCompile(`//([^/:]+)(:\d+)?`)
// guessPublicEndpoint attempts to deal with a shortcoming in the tendermint RPC client that doesn't allow path prefixes.
// The cosmos.directory requires them. This is a workaround to get the actual URL for the server behind their proxy.
// The RPC base URL will return links endpoints, and we can parse this to guess the original URL.
func guessPublicEndpoint(u string) string {
resp, err := http.Get(u + "/")
if err != nil {
return u
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return u
}
_ = resp.Body.Close()
matches := endpointRex.FindStringSubmatch(string(b))
if len(matches) < 2 {
// didn't work
return u
}
proto := "https://"
port := ":443"
// will be 3 elements if there is a port no port means listening on https
if len(matches) == 3 && matches[2] != "" && matches[2] != ":443" {
proto = "http://"
port = matches[2]
}
return proto + matches[1] + port
}