forked from sourcegraph/checkup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bncchecker.go
416 lines (357 loc) · 11.7 KB
/
bncchecker.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
package checkup
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/sourcegraph/checkup/utils"
)
/*
```
Summary: Get node information.
https://docs.binance.org/api-reference/node-rpc.html#node-rpc
URL for mainnet: http://dataseed1.binance.org:80/status
URL for testnet: http://data-seed-pre-0-s1.binance.org:80/status
latest_block_height
{
"jsonrpc": "2.0",
"id": "",
"result": {
"node_info": {
"protocol_version": {
"p2p": "7",
"block": "10",
"app": "0"
},
"id": "782303c9060d46211225662fdd1dd411c638263a",
"listen_addr": "52.197.243.252:27146",
"network": "Binance-Chain-Tigris",
"version": "0.30.1",
"channels": "354020212223303800",
"moniker": "data-seed-0",
"other": {
"tx_index": "on",
"rpc_address": "tcp://0.0.0.0:27147"
}
},
"sync_info": {
"latest_block_hash": "FF42CE48AC5987F7CD4A051B757A1B58B066081A2DDC006AA8F168CD5045C835",
"latest_app_hash": "D7C80CE18D1D1D5103CFA3221DF5C51EE8D3F5949DA3E943E782B7B227123D96",
"latest_block_height": "12766888",
"latest_block_time": "2019-06-13T06:37:04.78651439Z",
"catching_up": false
},
"validator_info": {
"address": "A88BAB486162E44380AA456DFA7C1DCD997985D9",
"pub_key": {
"type": "tendermint/PubKeyEd25519",
"value": "TYgEeiyMDbt8drIPkoyAMcISvlTNVQGU6NUsL4uWEG0="
},
"voting_power": "0"
}
}
}
```
*/
type BNBStatus struct {
StatusRPCVer string `json:"jsonrpc`
StatusResult StatusResultStruct `json:"result"`
// TODO: Transactions
}
/*
{
"node_info": {
"protocol_version": {
"p2p": 7,
"block": 10,
"app": 0
},
"id": "f52252fcda9c161c0089d971c9f1b941a26023ef",
"listen_addr": "10.211.33.206:27146",
"network": "Binance-Chain-Tigris",
"version": "0.30.1",
"channels": "3540202122233038",
"moniker": "Everest",
"other": {
"tx_index": "on",
"rpc_address": "tcp://0.0.0.0:27147"
}
},
"sync_info": {
"latest_block_hash": "0A82DF62E127C346DF3227A2E11B880A05FC4BEE3D1D97A24577B3506CCF9FD7",
"latest_app_hash": "C427816633C9B631B8059A3391B94F3ADEDD716DE6E682325B55AB7A929D47A2",
"latest_block_height": 16723750,
"latest_block_time": "2019-06-30T03:27:11.31300562Z",
"catching_up": false
},*/
type BNBDEXAPIStatus struct {
SyncInfoData SyncInfoStruct `json:"sync_info"`
// TODO: Transactions
}
type StatusResultStruct struct {
SyncInfoData SyncInfoStruct `json:"sync_info"`
}
type SyncInfoStruct struct {
SyncInfo_latest_block_hash string `json:"latest_block_hash"`
SyncInfo_latest_app_hash string `json:"latest_app_hash"`
SyncInfo_latest_block_height string `json:"latest_block_height"`
SyncInfo_latest_block_time string `json:"latest_block_time"`
SyncInfo_catching_up bool `json:"catching_up"`
}
// GetBlockHeight return the block height
func (bc *BNBStatus) GetLatestBlockHeight() string {
return bc.StatusResult.SyncInfoData.SyncInfo_latest_block_height
}
// GetTimestamp return the block timestamp
func (bc *BNBStatus) GetLatestBlockTimestamp() string {
return bc.StatusResult.SyncInfoData.SyncInfo_latest_block_time
}
// GetBlockHashString return the hex encoded string of the block hash
func (bc *BNBStatus) GetLatestBlockHashString() string {
return bc.StatusResult.SyncInfoData.SyncInfo_latest_block_hash
}
// BNCChecker implements a Checker for Binance chain endpoints.
type BNCChecker struct {
// Name is the name of the endpoint.
Name string `json:"endpoint_name"`
// URL is the URL of the endpoint.
URL string `json:"endpoint_url"`
// User is the user name
User string `json:"user"`
// Password is the password
Password string `json:"password"`
// BlockHeightBehind is the threshold of the current block height behind etherscan
BlockHeightBehind uint32 `json:"blockHeightBehind"`
// URL is the URL of the endpoint.
ReferURL string `json:"refer_url,omitempty"`
// UpStatus is the HTTP status code expected by
// a healthy endpoint. Default is http.StatusOK.
UpStatus int `json:"up_status,omitempty"`
// ThresholdRTT is the maximum round trip time to
// allow for a healthy endpoint. If non-zero and a
// request takes longer than ThresholdRTT, the
// endpoint will be considered unhealthy. Note that
// this duration includes any in-between network
// latency.
ThresholdRTT time.Duration `json:"threshold_rtt,omitempty"`
// MustContain is a string that the response body
// must contain in order to be considered up.
// NOTE: If set, the entire response body will
// be consumed, which has the potential of using
// lots of memory and slowing down checks if the
// response body is large.
MustContain string `json:"must_contain,omitempty"`
// MustNotContain is a string that the response
// body must NOT contain in order to be considered
// up. If both MustContain and MustNotContain are
// set, they are and-ed together. NOTE: If set,
// the entire response body will be consumed, which
// has the potential of using lots of memory and
// slowing down checks if the response body is large.
MustNotContain string `json:"must_not_contain,omitempty"`
// Attempts is how many requests the client will
// make to the endpoint in a single check.
Attempts int `json:"attempts,omitempty"`
// AttemptSpacing spaces out each attempt in a check
// by this duration to avoid hitting a remote too
// quickly in succession. By default, no waiting
// occurs between attempts.
AttemptSpacing time.Duration `json:"attempt_spacing,omitempty"`
// Client is the http.Client with which to make
// requests. If not set, DefaultHTTPClient is
// used.
Client *http.Client `json:"-"`
// Headers contains headers to added to the request
// that is sent for the check
Headers http.Header `json:"headers,omitempty"`
}
// Check performs checks using c according to its configuration.
// An error is only returned if there is a configuration error.
func (c BNCChecker) Check() (Result, error) {
if c.Attempts < 1 {
c.Attempts = 1
}
if c.Client == nil {
c.Client = DefaultHTTPClient
}
if c.UpStatus == 0 {
c.UpStatus = http.StatusOK
}
result := Result{Title: c.Name, Endpoint: c.URL, Timestamp: Timestamp()}
req, err := http.NewRequest("POST", c.URL, bytes.NewBuffer([]byte("")))
req.Header.Set("Content-Type", "application/json")
if c.User != "" && c.Password != "" {
req.SetBasicAuth(c.User, c.Password)
}
if err != nil {
return result, err
}
if c.Headers != nil {
for key, header := range c.Headers {
req.Header.Add(key, strings.Join(header, ", "))
}
}
result.Times = c.doChecks(req)
return c.conclude(result), nil
}
// doChecks executes req using c.Client and returns each attempt.
func (c BNCChecker) doChecks(req *http.Request) Attempts {
checks := make(Attempts, c.Attempts)
for i := 0; i < c.Attempts; i++ {
start := time.Now()
resp, err := c.Client.Do(req)
checks[i].RTT = time.Since(start)
if err != nil {
checks[i].Error = err.Error()
continue
}
err = c.checkDown(resp)
if err != nil {
checks[i].Error = err.Error()
}
resp.Body.Close()
if c.AttemptSpacing > 0 {
time.Sleep(c.AttemptSpacing)
}
}
return checks
}
// conclude takes the data in result from the attempts and
// computes remaining values needed to fill out the result.
// It detects degraded (high-latency) responses and makes
// the conclusion about the result's status.
func (c BNCChecker) conclude(result Result) Result {
result.ThresholdRTT = c.ThresholdRTT
// Check errors (down)
for i := range result.Times {
if result.Times[i].Error != "" {
result.Down = true
result.Message = result.Times[i].Error
return result
}
}
// Check round trip time (degraded)
if c.ThresholdRTT > 0 {
stats := result.ComputeStats()
if stats.Median > c.ThresholdRTT {
result.Notice = fmt.Sprintf("median round trip time exceeded threshold (%s)", c.ThresholdRTT)
result.Degraded = true
return result
}
}
result.Healthy = true
return result
}
// checkDown checks whether the endpoint is down based on resp and
// the configuration of c. It returns a non-nil error if down.
// Note that it does not check for degraded response.
func (c BNCChecker) checkDown(resp *http.Response) error {
// Check status code
if resp.StatusCode != c.UpStatus {
return fmt.Errorf("response status %s", resp.Status)
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("reading response body: %v", err)
}
// parse blockheight
var checkresult = new(BNBStatus)
err = json.Unmarshal(bodyBytes, &checkresult)
if err != nil {
return fmt.Errorf("Unmarshal response body: %v", err)
}
var lastBNBBlockNum int64
if c.ReferURL == "" {
c.ReferURL = "https://dex.binance.org/api/v1/node-info"
log.Printf("use default, set c.ReferURL:%s\n", c.ReferURL)
}
if c.ReferURL == "http://dataseed1.binance.org:80/status" {
lastBNBBlockNum, err = c.getHeightDataseed1()
} else if c.ReferURL == "https://dex.binance.org/api/v1/node-info" {
lastBNBBlockNum, err = c.getHeightDEXAPI()
} else {
return fmt.Errorf("unsupported c.ReferURL:%s", c.ReferURL)
}
lastBNBCheckBlockNum, _ := strconv.ParseInt(checkresult.GetLatestBlockHeight(), 10, 64)
blockDiff := lastBNBBlockNum - lastBNBCheckBlockNum
log.Printf("BNC(%s) %s BlockHeight:%d, check url:%s last block BlockHeight:%d\n", c.Name, c.ReferURL, lastBNBBlockNum, c.URL, lastBNBCheckBlockNum)
if (lastBNBBlockNum > lastBNBCheckBlockNum) && (blockDiff > int64(c.BlockHeightBehind)) {
return fmt.Errorf("blockheight(%d) was behind BNC %s (%d) > %d blocks, threshold(%d)", lastBNBCheckBlockNum, c.ReferURL, lastBNBBlockNum, blockDiff, c.BlockHeightBehind)
}
return nil
}
func (c BNCChecker) getHeightDataseed1() (int64, error) {
var responsebody []byte
url := "http://dataseed1.binance.org:80/status"
if c.ReferURL != "" {
log.Printf("BNC(%s) height ReferURL:%s\n", c.Name, c.ReferURL)
url = c.ReferURL
}
responsebodyStr := utils.GlobalCacheGetString(url)
if responsebodyStr == "" {
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte("")))
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return 0, err
}
defer res.Body.Close()
responsebody, err = ioutil.ReadAll(res.Body)
if err != nil {
return 0, err
}
str := string(responsebody[:])
utils.GlobalCacheSetString(url, str)
} else {
log.Printf("BNC(%s) use cache key: %s", c.Name, url)
responsebody = []byte(responsebodyStr)
}
var bnbresult = new(BNBStatus)
err := json.Unmarshal(responsebody, &bnbresult)
if err != nil {
return 0, fmt.Errorf("url:%s Unmarshal response body: %v", url, err)
}
lastBNBBlockNum, err := strconv.ParseInt(bnbresult.GetLatestBlockHeight(), 10, 64)
return lastBNBBlockNum, err
}
func (c BNCChecker) getHeightDEXAPI() (int64, error) {
var responsebody []byte
url := "https://dex.binance.org/api/v1/node-info"
if c.ReferURL != "" {
log.Printf("BNC(%s) height ReferURL:%s\n", c.Name, c.ReferURL)
url = c.ReferURL
}
responsebodyStr := utils.GlobalCacheGetString(url)
if responsebodyStr == "" {
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer([]byte("")))
req.Header.Set("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
return 0, err
}
defer res.Body.Close()
responsebody, err = ioutil.ReadAll(res.Body)
if err != nil {
return 0, err
}
str := string(responsebody[:])
utils.GlobalCacheSetString(url, str)
} else {
log.Printf("BNC(%s) use cache key: %s", c.Name, url)
responsebody = []byte(responsebodyStr)
}
var bnbresult = new(BNBDEXAPIStatus)
err := json.Unmarshal(responsebody, &bnbresult)
if err != nil {
return 0, fmt.Errorf("url:%s Unmarshal response body: %v", url, err)
}
lastBNBBlockNum, err := strconv.ParseInt(bnbresult.SyncInfoData.SyncInfo_latest_block_height, 10, 64)
return lastBNBBlockNum, err
}