/
api_gateway_v1alpha1.go
476 lines (458 loc) · 13.7 KB
/
api_gateway_v1alpha1.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package evaluators
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
e2e "github.com/prysmaticlabs/prysm/v4/testing/endtoend/params"
"github.com/prysmaticlabs/prysm/v4/testing/endtoend/policies"
e2etypes "github.com/prysmaticlabs/prysm/v4/testing/endtoend/types"
"google.golang.org/grpc"
)
// APIGatewayV1Alpha1VerifyIntegrity of our API gateway for the Prysm v1alpha1 API.
// This ensures our gRPC HTTP gateway returns and processes the same data _for the same endpoints_
// as using a gRPC connection to interact with the API. Running this in end-to-end tests helps us
// ensure parity between our HTTP gateway for our API and gRPC never breaks.
// This evaluator checks a few request/response trips for both GET and POST requests.
var APIGatewayV1Alpha1VerifyIntegrity = e2etypes.Evaluator{
Name: "api_gateway_v1alpha1_verify_integrity_epoch_%d",
Policy: policies.OnEpoch(2),
Evaluation: apiGatewayV1Alpha1Verify,
}
const (
v1Alpha1GatewayPathTemplate = "http://localhost:%d/eth/v1alpha1"
)
type apiComparisonFunc func(beaconNodeIdx int, conn *grpc.ClientConn) error
func apiGatewayV1Alpha1Verify(_ *e2etypes.EvaluationContext, conns ...*grpc.ClientConn) error {
for beaconNodeIdx, conn := range conns {
if err := runAPIComparisonFunctions(
beaconNodeIdx,
conn,
withComparePeers,
withCompareListAttestations,
withCompareValidators,
withCompareChainHead,
); err != nil {
return err
}
}
return nil
}
func withComparePeers(beaconNodeIdx int, conn *grpc.ClientConn) error {
type peerJSON struct {
Address string `json:"address"`
Direction string `json:"direction"`
ConnectionState string `json:"connectionState"`
PeerId string `json:"peerId"`
Enr string `json:"enr"`
}
type peersResponseJSON struct {
Peers []*peerJSON `json:"peers"`
}
ctx := context.Background()
nodeClient := ethpb.NewNodeClient(conn)
resp, err := nodeClient.ListPeers(ctx, &empty.Empty{})
if err != nil {
return err
}
respJSON := &peersResponseJSON{}
if err := doGatewayJSONRequest(
"/node/peers",
beaconNodeIdx,
respJSON,
); err != nil {
return err
}
if len(respJSON.Peers) != len(resp.Peers) {
return fmt.Errorf(
"HTTP gateway number of peers %d does not match gRPC %d",
len(respJSON.Peers),
len(resp.Peers),
)
}
grpcPeerMap := make(map[string]*ethpb.Peer)
jsonPeerMap := make(map[string]*peerJSON)
for i := 0; i < len(respJSON.Peers); i++ {
grpcPeerMap[resp.Peers[i].PeerId] = resp.Peers[i]
jsonPeerMap[respJSON.Peers[i].PeerId] = respJSON.Peers[i]
}
for id, peer := range jsonPeerMap {
grpcPeer, ok := grpcPeerMap[id]
if !ok {
return errors.Errorf("grpc peer %s doesn't exist", id)
}
if peer.Address != grpcPeer.Address {
return fmt.Errorf(
"HTTP gateway peer %s with address %s does not match gRPC %s",
id,
peer.Address,
grpcPeer.Address,
)
}
if peer.Direction != grpcPeer.Direction.String() {
return fmt.Errorf(
"HTTP gateway peer %s with direction %s does not match gRPC %s",
id,
peer.Direction,
grpcPeer.Direction,
)
}
if peer.ConnectionState != grpcPeer.ConnectionState.String() {
return fmt.Errorf(
"HTTP gateway peer %s with connection state %s does not match gRPC %s",
id,
peer.ConnectionState,
grpcPeer.ConnectionState,
)
}
if peer.PeerId != grpcPeer.PeerId {
return fmt.Errorf(
"HTTP gateway peer %s with peer id %s does not match gRPC %s",
id,
peer.PeerId,
grpcPeer.PeerId,
)
}
if peer.Enr != grpcPeer.Enr {
return fmt.Errorf(
"HTTP gateway peer %s with enr %s does not match gRPC %s",
id,
peer.Enr,
grpcPeer.Enr,
)
}
}
return nil
}
func withCompareListAttestations(beaconNodeIdx int, conn *grpc.ClientConn) error {
type checkpointJSON struct {
Epoch string `json:"epoch"`
Root string `json:"root"`
}
type attestationDataJSON struct {
Slot string `json:"slot"`
CommitteeIndex string `json:"committeeIndex"`
BeaconBlockRoot string `json:"beaconBlockRoot"`
Source *checkpointJSON `json:"source"`
Target *checkpointJSON `json:"target"`
}
type attestationJSON struct {
AggregationBits string `json:"aggregationBits"`
Data *attestationDataJSON `json:"data"`
Signature string `json:"signature"`
}
type attestationsResponseJSON struct {
Attestations []*attestationJSON `json:"attestations"`
NextPageToken string `json:"nextPageToken"`
TotalSize int32 `json:"totalSize"`
}
ctx := context.Background()
beaconClient := ethpb.NewBeaconChainClient(conn)
resp, err := beaconClient.ListAttestations(ctx, ðpb.ListAttestationsRequest{
QueryFilter: ðpb.ListAttestationsRequest_GenesisEpoch{GenesisEpoch: true},
})
if err != nil {
return err
}
respJSON := &attestationsResponseJSON{}
if err := doGatewayJSONRequest(
"/beacon/attestations?genesis_epoch=true",
beaconNodeIdx,
respJSON,
); err != nil {
return err
}
// Begin comparisons.
if respJSON.NextPageToken != resp.NextPageToken {
return fmt.Errorf(
"HTTP gateway next page token %s does not match gRPC %s",
respJSON.NextPageToken,
resp.NextPageToken,
)
}
if respJSON.TotalSize != resp.TotalSize {
return fmt.Errorf(
"HTTP gateway total size %d does not match gRPC %d",
respJSON.TotalSize,
resp.TotalSize,
)
}
for i, att := range respJSON.Attestations {
grpcAtt := resp.Attestations[i]
if att.AggregationBits != base64.StdEncoding.EncodeToString(grpcAtt.AggregationBits) {
return fmt.Errorf(
"HTTP gateway attestation %d aggregation bits %s does not match gRPC %d",
i,
att.AggregationBits,
grpcAtt.AggregationBits,
)
}
data := att.Data
grpcData := grpcAtt.Data
if data.Slot != fmt.Sprintf("%d", grpcData.Slot) {
return fmt.Errorf(
"HTTP gateway attestation %d slot %s does not match gRPC %d",
i,
data.Slot,
grpcData.Slot,
)
}
if data.CommitteeIndex != fmt.Sprintf("%d", grpcData.CommitteeIndex) {
return fmt.Errorf(
"HTTP gateway attestation %d committee index %s does not match gRPC %d",
i,
data.CommitteeIndex,
grpcData.CommitteeIndex,
)
}
if data.BeaconBlockRoot != base64.StdEncoding.EncodeToString(grpcData.BeaconBlockRoot) {
return fmt.Errorf(
"HTTP gateway attestation %d beacon block root %s does not match gRPC %d",
i,
data.BeaconBlockRoot,
grpcData.BeaconBlockRoot,
)
}
if data.Source.Epoch != fmt.Sprintf("%d", grpcData.Source.Epoch) {
return fmt.Errorf(
"HTTP gateway attestation %d source epoch %s does not match gRPC %d",
i,
data.Source.Epoch,
grpcData.Source.Epoch,
)
}
if data.Source.Root != base64.StdEncoding.EncodeToString(grpcData.Source.Root) {
return fmt.Errorf(
"HTTP gateway attestation %d source root %s does not match gRPC %d",
i,
data.Source.Root,
grpcData.Source.Root,
)
}
if data.Target.Epoch != fmt.Sprintf("%d", grpcData.Target.Epoch) {
return fmt.Errorf(
"HTTP gateway attestation %d target epoch %s does not match gRPC %d",
i,
data.Target.Epoch,
grpcData.Target.Epoch,
)
}
if data.Target.Root != base64.StdEncoding.EncodeToString(grpcData.Target.Root) {
return fmt.Errorf(
"HTTP gateway attestation %d target root %s does not match gRPC %d",
i,
data.Target.Root,
grpcData.Target.Root,
)
}
if att.Signature != base64.StdEncoding.EncodeToString(grpcAtt.Signature) {
return fmt.Errorf(
"HTTP gateway attestation %d signature %s does not match gRPC %d",
i,
att.Signature,
grpcAtt.Signature,
)
}
}
return nil
}
func withCompareValidators(beaconNodeIdx int, conn *grpc.ClientConn) error {
type validatorJSON struct {
PublicKey string `json:"publicKey"`
WithdrawalCredentials string `json:"withdrawalCredentials"`
EffectiveBalance string `json:"effectiveBalance"`
Slashed bool `json:"slashed"`
ActivationEligibilityEpoch string `json:"activationEligibilityEpoch"`
ActivationEpoch string `json:"activationEpoch"`
ExitEpoch string `json:"exitEpoch"`
WithdrawableEpoch string `json:"withdrawableEpoch"`
}
type validatorContainerJSON struct {
Index string `json:"index"`
Validator *validatorJSON `json:"validator"`
}
type validatorsResponseJSON struct {
Epoch string `json:"epoch"`
ValidatorList []*validatorContainerJSON `json:"validatorList"`
NextPageToken string `json:"nextPageToken"`
TotalSize int32 `json:"totalSize"`
}
ctx := context.Background()
beaconClient := ethpb.NewBeaconChainClient(conn)
resp, err := beaconClient.ListValidators(ctx, ðpb.ListValidatorsRequest{
QueryFilter: ðpb.ListValidatorsRequest_Genesis{
Genesis: true,
},
PageSize: 4,
})
if err != nil {
return err
}
respJSON := &validatorsResponseJSON{}
if err := doGatewayJSONRequest(
"/validators?genesis=true&page_size=4",
beaconNodeIdx,
respJSON,
); err != nil {
return err
}
// Begin comparisons.
if respJSON.Epoch != fmt.Sprintf("%d", resp.Epoch) {
return fmt.Errorf(
"HTTP gateway epoch %s does not match gRPC %d",
respJSON.Epoch,
resp.Epoch,
)
}
if respJSON.NextPageToken != resp.NextPageToken {
return fmt.Errorf(
"HTTP gateway next page token %s does not match gRPC %s",
respJSON.NextPageToken,
resp.NextPageToken,
)
}
if respJSON.TotalSize != resp.TotalSize {
return fmt.Errorf(
"HTTP gateway total size %d does not match gRPC %d",
respJSON.TotalSize,
resp.TotalSize,
)
}
// Compare validators.
for i, val := range respJSON.ValidatorList {
if val.Index != fmt.Sprintf("%d", resp.ValidatorList[i].Index) {
return fmt.Errorf(
"HTTP gateway validator %d index %s does not match gRPC %d",
i,
val.Index,
resp.ValidatorList[i].Index,
)
}
httpVal := val.Validator
grpcVal := resp.ValidatorList[i].Validator
if httpVal.PublicKey != base64.StdEncoding.EncodeToString(grpcVal.PublicKey) {
return fmt.Errorf(
"HTTP gateway validator %d public key %s does not match gRPC %d",
i,
httpVal.PublicKey,
grpcVal.PublicKey,
)
}
continue
}
return nil
}
// Compares a regular beacon chain head GET request with no arguments gRPC and gRPC gateway.
func withCompareChainHead(beaconNodeIdx int, conn *grpc.ClientConn) error {
type chainHeadResponseJSON struct {
HeadSlot string `json:"headSlot"`
HeadEpoch string `json:"headEpoch"`
HeadBlockRoot string `json:"headBlockRoot"`
FinalizedSlot string `json:"finalizedSlot"`
FinalizedEpoch string `json:"finalizedEpoch"`
FinalizedBlockRoot string `json:"finalizedBlockRoot"`
JustifiedSlot string `json:"justifiedSlot"`
JustifiedEpoch string `json:"justifiedEpoch"`
JustifiedBlockRoot string `json:"justifiedBlockRoot"`
}
beaconClient := ethpb.NewBeaconChainClient(conn)
ctx := context.Background()
resp, err := beaconClient.GetChainHead(ctx, &empty.Empty{})
if err != nil {
return err
}
respJSON := &chainHeadResponseJSON{}
if err := doGatewayJSONRequest(
"/beacon/chainhead",
beaconNodeIdx,
respJSON,
); err != nil {
return err
}
if respJSON.HeadSlot != fmt.Sprintf("%d", resp.HeadSlot) {
return fmt.Errorf(
"HTTP gateway head slot %s does not match gRPC %d",
respJSON.HeadSlot,
resp.HeadSlot,
)
}
if respJSON.HeadEpoch != fmt.Sprintf("%d", resp.HeadEpoch) {
return fmt.Errorf(
"HTTP gateway head epoch %s does not match gRPC %d",
respJSON.HeadEpoch,
resp.HeadEpoch,
)
}
if respJSON.HeadBlockRoot != base64.StdEncoding.EncodeToString(resp.HeadBlockRoot) {
return fmt.Errorf(
"HTTP gateway head block root %s does not match gRPC %s",
respJSON.HeadBlockRoot,
resp.HeadBlockRoot,
)
}
if respJSON.FinalizedSlot != fmt.Sprintf("%d", resp.FinalizedSlot) {
return fmt.Errorf(
"HTTP gateway finalized slot %s does not match gRPC %d",
respJSON.FinalizedSlot,
resp.FinalizedSlot,
)
}
if respJSON.FinalizedEpoch != fmt.Sprintf("%d", resp.FinalizedEpoch) {
return fmt.Errorf(
"HTTP gateway finalized epoch %s does not match gRPC %d",
respJSON.FinalizedEpoch,
resp.FinalizedEpoch,
)
}
if respJSON.FinalizedBlockRoot != base64.StdEncoding.EncodeToString(resp.FinalizedBlockRoot) {
return fmt.Errorf(
"HTTP gateway finalized block root %s does not match gRPC %s",
respJSON.FinalizedBlockRoot,
resp.FinalizedBlockRoot,
)
}
if respJSON.JustifiedSlot != fmt.Sprintf("%d", resp.JustifiedSlot) {
return fmt.Errorf(
"HTTP gateway justified slot %s does not match gRPC %d",
respJSON.FinalizedSlot,
resp.FinalizedSlot,
)
}
if respJSON.JustifiedEpoch != fmt.Sprintf("%d", resp.JustifiedEpoch) {
return fmt.Errorf(
"HTTP gateway justified epoch %s does not match gRPC %d",
respJSON.FinalizedEpoch,
resp.FinalizedEpoch,
)
}
if respJSON.JustifiedBlockRoot != base64.StdEncoding.EncodeToString(resp.JustifiedBlockRoot) {
return fmt.Errorf(
"HTTP gateway justified block root %s does not match gRPC %s",
respJSON.JustifiedBlockRoot,
resp.JustifiedBlockRoot,
)
}
return nil
}
func doGatewayJSONRequest(requestPath string, beaconNodeIdx int, dst interface{}) error {
basePath := fmt.Sprintf(v1Alpha1GatewayPathTemplate, e2e.TestParams.Ports.PrysmBeaconNodeGatewayPort+beaconNodeIdx)
httpResp, err := http.Get(
basePath + requestPath,
)
if err != nil {
return err
}
return json.NewDecoder(httpResp.Body).Decode(&dst)
}
func runAPIComparisonFunctions(beaconNodeIdx int, conn *grpc.ClientConn, fs ...apiComparisonFunc) error {
for _, f := range fs {
if err := f(beaconNodeIdx, conn); err != nil {
return err
}
}
return nil
}