-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathbeaconapi.go
308 lines (258 loc) · 8.23 KB
/
beaconapi.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
package ethereum
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
eth2client "github.com/attestantio/go-eth2-client"
"github.com/attestantio/go-eth2-client/api"
apiv1 "github.com/attestantio/go-eth2-client/api/v1"
ethttp "github.com/attestantio/go-eth2-client/http"
"github.com/attestantio/go-eth2-client/spec/phase0"
"github.com/rs/zerolog"
ethcommon "github.com/ethereum/go-ethereum/common"
ethereumtypes "github.com/srdtrk/solidity-ibc-eureka/e2e/v8/types/ethereum"
)
type BeaconAPIClient struct {
ctx context.Context
cancel context.CancelFunc
client eth2client.Service
url string
Retries int
RetryWait time.Duration
}
func (b BeaconAPIClient) GetBeaconAPIURL() string {
return b.url
}
func (s Spec) ToForkParameters() ethereumtypes.ForkParameters {
return ethereumtypes.ForkParameters{
GenesisForkVersion: ethcommon.Bytes2Hex(s.GenesisForkVersion[:]),
GenesisSlot: s.GenesisSlot,
Altair: ethereumtypes.Fork{
Version: ethcommon.Bytes2Hex(s.AltairForkVersion[:]),
Epoch: s.AltairForkEpoch,
},
Bellatrix: ethereumtypes.Fork{
Version: ethcommon.Bytes2Hex(s.BellatrixForkVersion[:]),
Epoch: s.BellatrixForkEpoch,
},
Capella: ethereumtypes.Fork{
Version: ethcommon.Bytes2Hex(s.CapellaForkVersion[:]),
Epoch: s.CapellaForkEpoch,
},
Deneb: ethereumtypes.Fork{
Version: ethcommon.Bytes2Hex(s.DenebForkVersion[:]),
Epoch: s.DenebForkEpoch,
},
Electra: ethereumtypes.Fork{
Version: ethcommon.Bytes2Hex(s.ElectraForkVersion[:]),
Epoch: s.ElectraForkEpoch,
},
}
}
func (s Spec) Period() uint64 {
return s.EpochsPerSyncCommitteePeriod * s.SlotsPerEpoch
}
func (b BeaconAPIClient) Close() {
b.cancel()
}
func NewBeaconAPIClient(ctx context.Context, beaconAPIAddress string) (BeaconAPIClient, error) {
ctx, cancel := context.WithCancel(ctx)
client, err := ethttp.New(ctx,
// WithAddress supplies the address of the beacon node, as a URL.
ethttp.WithAddress(beaconAPIAddress),
// LogLevel supplies the level of logging to carry out.
ethttp.WithLogLevel(zerolog.WarnLevel),
)
if err != nil {
cancel()
return BeaconAPIClient{}, err
}
return BeaconAPIClient{
ctx: ctx,
cancel: cancel,
client: client,
url: beaconAPIAddress,
Retries: 60,
RetryWait: 10 * time.Second,
}, nil
}
func retry[T any](retries int, waitTime time.Duration, fn func() (T, error)) (T, error) {
var err error
var result T
for range retries {
result, err = fn()
if err == nil {
return result, nil
}
fmt.Printf("Retrying for %T: %s in %f seconds\n", result, err, waitTime.Seconds())
time.Sleep(waitTime)
}
return result, err
}
func (b BeaconAPIClient) GetHeader(blockID string) (*apiv1.BeaconBlockHeader, error) {
return retry(b.Retries, b.RetryWait, func() (*apiv1.BeaconBlockHeader, error) {
headerResponse, err := b.client.(eth2client.BeaconBlockHeadersProvider).BeaconBlockHeader(b.ctx, &api.BeaconBlockHeaderOpts{
Block: blockID,
})
if err != nil {
return nil, err
}
return headerResponse.Data, nil
})
}
func (b BeaconAPIClient) GetBootstrap(finalizedRoot phase0.Root) (Bootstrap, error) {
return retry(b.Retries, b.RetryWait, func() (Bootstrap, error) {
finalizedRootStr := finalizedRoot.String()
url := fmt.Sprintf("%s/eth/v1/beacon/light_client/bootstrap/%s", b.url, finalizedRootStr)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return Bootstrap{}, err
}
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return Bootstrap{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return Bootstrap{}, err
}
if resp.StatusCode != 200 {
return Bootstrap{}, fmt.Errorf("get bootstrap (%s) failed with status code: %d, body: %s", url, resp.StatusCode, body)
}
var bootstrap Bootstrap
if err := json.Unmarshal(body, &bootstrap); err != nil {
return Bootstrap{}, err
}
return bootstrap, nil
})
}
func (b BeaconAPIClient) GetLightClientUpdates(startPeriod uint64, count uint64) (LightClientUpdatesResponse, error) {
return retry(b.Retries, b.RetryWait, func() (LightClientUpdatesResponse, error) {
url := fmt.Sprintf("%s/eth/v1/beacon/light_client/updates?start_period=%d&count=%d", b.url, startPeriod, count)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return LightClientUpdatesResponse{}, err
}
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return LightClientUpdatesResponse{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return LightClientUpdatesResponse{}, err
}
var lightClientUpdatesResponse LightClientUpdatesResponse
if err := json.Unmarshal(body, &lightClientUpdatesResponse); err != nil {
return LightClientUpdatesResponse{}, err
}
return lightClientUpdatesResponse, nil
})
}
func (b BeaconAPIClient) GetGenesis() (*apiv1.Genesis, error) {
return retry(b.Retries, b.RetryWait, func() (*apiv1.Genesis, error) {
genesisResponse, err := b.client.(eth2client.GenesisProvider).Genesis(b.ctx, &api.GenesisOpts{})
if err != nil {
return nil, err
}
return genesisResponse.Data, nil
})
}
func (b BeaconAPIClient) GetSpec() (Spec, error) {
return retry(b.Retries, b.RetryWait, func() (Spec, error) {
specResponse, err := b.client.(eth2client.SpecProvider).Spec(b.ctx, &api.SpecOpts{})
if err != nil {
return Spec{}, err
}
specJsonBz, err := json.Marshal(specResponse.Data)
if err != nil {
return Spec{}, err
}
var spec Spec
if err := json.Unmarshal(specJsonBz, &spec); err != nil {
return Spec{}, err
}
return spec, nil
})
}
func (b BeaconAPIClient) GetFinalityUpdate() (FinalityUpdateJSONResponse, error) {
return retry(b.Retries, b.RetryWait, func() (FinalityUpdateJSONResponse, error) {
url := fmt.Sprintf("%s/eth/v1/beacon/light_client/finality_update", b.url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return FinalityUpdateJSONResponse{}, err
}
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return FinalityUpdateJSONResponse{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return FinalityUpdateJSONResponse{}, err
}
var finalityUpdate FinalityUpdateJSONResponse
if err := json.Unmarshal(body, &finalityUpdate); err != nil {
return FinalityUpdateJSONResponse{}, err
}
return finalityUpdate, nil
})
}
func (b BeaconAPIClient) GetBeaconBlocks(blockID string) (BeaconBlocksResponseJSON, error) {
return retry(b.Retries, b.RetryWait, func() (BeaconBlocksResponseJSON, error) {
url := fmt.Sprintf("%s/eth/v2/beacon/blocks/%s", b.url, blockID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return BeaconBlocksResponseJSON{}, err
}
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return BeaconBlocksResponseJSON{}, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return BeaconBlocksResponseJSON{}, err
}
if resp.StatusCode != 200 {
return BeaconBlocksResponseJSON{}, fmt.Errorf("get execution height (%s) failed with status code: %d, body: %s", url, resp.StatusCode, body)
}
var beaconBlocksResponse BeaconBlocksResponseJSON
if err := json.Unmarshal(body, &beaconBlocksResponse); err != nil {
return BeaconBlocksResponseJSON{}, err
}
return beaconBlocksResponse, nil
})
}
func (b BeaconAPIClient) GetFinalizedBlocks() (BeaconBlocksResponseJSON, error) {
return retry(b.Retries, b.RetryWait, func() (BeaconBlocksResponseJSON, error) {
resp, err := b.GetBeaconBlocks("finalized")
if err != nil {
return BeaconBlocksResponseJSON{}, err
}
if !resp.Finalized {
return BeaconBlocksResponseJSON{}, fmt.Errorf("block is not finalized")
}
return resp, nil
})
}
func (b BeaconAPIClient) GetExecutionHeight(blockID string) (uint64, error) {
return retry(b.Retries, b.RetryWait, func() (uint64, error) {
resp, err := b.GetBeaconBlocks(blockID)
if err != nil {
return 0, err
}
if blockID == "finalized" && !resp.Finalized {
return 0, fmt.Errorf("block is not finalized")
}
return resp.Data.Message.Body.ExecutionPayload.BlockNumber, nil
})
}