forked from nbd-wtf/trustedcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgettip.go
More file actions
42 lines (35 loc) · 667 Bytes
/
gettip.go
File metadata and controls
42 lines (35 loc) · 667 Bytes
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
package main
import (
"io"
"net/http"
"strconv"
)
func getTip() (tip int64, err error) {
// try bitcoind first
if bitcoind != nil {
if info, err := bitcoind.GetBlockChainInfo(); err == nil {
return int64(info.Headers), nil
}
}
// then try explorers
for _, endpoint := range esploras(network) {
w, errW := http.Get(endpoint + "/blocks/tip/height")
if errW != nil {
err = errW
continue
}
defer w.Body.Close()
data, errW := io.ReadAll(w.Body)
if errW != nil {
err = errW
continue
}
tip, errW = strconv.ParseInt(string(data), 10, 64)
if errW != nil {
err = errW
continue
}
return tip, nil
}
return 0, err
}