-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
196 lines (166 loc) · 4.32 KB
/
client.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
package execution
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/alexallah/ethereum-healthmon/internal/common"
)
var blockTrack common.BlockTrack
func isReady(addr, token string, timeout int64) error {
// is syncing?
syncInfo, err := getSyncing(addr, token, timeout)
if err != nil {
return err
}
if syncInfo != nil {
return fmt.Errorf("syncing, distance %d blocks", syncInfo.distance())
}
// get latest block info
block, err := getLatestBlock(addr, token, timeout)
if err != nil {
return err
}
blockNumber, err := block.number()
if err != nil {
return err
}
blockTrack.AddBlock(blockNumber)
// make sure it is recent enough
if err := block.checkAge(); err != nil {
return err
}
// wait for a new block
if err := blockTrack.HealthCheck(); err != nil {
return err
}
return nil
}
// json unmarshal helpers
type SyncInfo struct {
CurrentBlockHex string `json:"currentBlock"`
HighestBlockHex string `json:"highestBlock"`
}
func (s *SyncInfo) currentBlock() uint64 {
return parseUintFromHex(s.CurrentBlockHex)
}
func (s *SyncInfo) highestBlock() uint64 {
return parseUintFromHex(s.HighestBlockHex)
}
func (s *SyncInfo) distance() uint64 {
return s.highestBlock() - s.currentBlock()
}
// json unmarshal helper
type JsonResultSync struct {
Result SyncInfo `json:"result"`
}
type JsonResultBool struct {
Result bool `json:"result"`
}
type JsonResultString struct {
Result string `json:"result"`
}
type Block struct {
Timestamp string `json:"timestamp"`
Number string `json:"number"`
}
func (b *Block) time() (time.Time, error) {
timeHex := strings.TrimLeft(b.Timestamp, "0x")
unixtime, err := strconv.ParseInt(timeHex, 16, 64)
if err != nil {
return time.Time{}, fmt.Errorf("can not parse block timestamp: %w", err)
}
return time.Unix(unixtime, 0), nil
}
func (b *Block) number() (uint64, error) {
numHex := strings.TrimLeft(b.Number, "0x")
blockNumber, err := strconv.ParseUint(numHex, 16, 64)
if err != nil {
return 0, fmt.Errorf("can not parse block number: %w", err)
}
return blockNumber, nil
}
func (b *Block) checkAge() error {
created, err := b.time()
if err != nil {
return err
}
age := time.Since(created)
if age > 300*time.Second {
return fmt.Errorf("latest block is too old: %s", age.Truncate(time.Second))
}
return nil
}
type JsonResultBlock struct {
Result Block `json:"result"`
}
// execute an RPC request and return true if the server is synced and ready
func getSyncing(addr, token string, timeout int64) (*SyncInfo, error) {
payload := []byte(`{"jsonrpc":"2.0","method":"eth_syncing","params":[],"id":1}`)
body, err := request(addr, token, timeout, payload)
if err != nil {
return nil, err
}
resultSync := new(JsonResultSync)
err = json.Unmarshal(body, resultSync)
if err == nil {
return &resultSync.Result, nil
}
// try parsing as bool
resultBool := new(JsonResultBool)
if err := json.Unmarshal(body, resultBool); err != nil {
return nil, err
}
if resultBool.Result {
return nil, errors.New("syncing is true, not expected")
}
return nil, nil
}
func parseUintFromHex(hex string) uint64 {
trimmed := strings.TrimPrefix(hex, "0x")
result, err := strconv.ParseUint(trimmed, 16, 64)
if err != nil {
log.Panicf("error parsing hex %v: %q", hex, err)
}
return result
}
func getLatestBlock(addr, token string, timeout int64) (*Block, error) {
payload := []byte(`{"jsonrpc":"2.0","method":"eth_getBlockByNumber","params":["latest",false],"id":1}`)
body, err := request(addr, token, timeout, payload)
if err != nil {
return nil, err
}
result := new(JsonResultBlock)
if err := json.Unmarshal(body, result); err != nil {
return nil, err
}
return &result.Result, nil
}
func request(addr, token string, timeout int64, payload []byte) ([]byte, error) {
req, err := http.NewRequest("POST", addr, bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if token != "" {
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
}
client := &http.Client{
Timeout: time.Duration(timeout) * time.Second,
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("incorrect response status code: %d", resp.StatusCode)
}
return io.ReadAll(resp.Body)
}