-
Notifications
You must be signed in to change notification settings - Fork 985
/
util.go
161 lines (143 loc) · 3.96 KB
/
util.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
package beaconapi
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/api"
"github.com/prysmaticlabs/prysm/v5/testing/endtoend/params"
log "github.com/sirupsen/logrus"
)
var (
errEmptyPrysmData = errors.New("Prysm data is empty")
errEmptyLighthouseData = errors.New("Lighthouse data is empty")
)
const (
msgWrongJson = "JSON response has wrong structure, expected %T, got %T"
msgRequestFailed = "%s request failed with response code %d with response body %s"
msgUnknownNode = "unknown node type %s"
msgSSZUnmarshalFailed = "failed to unmarshal SSZ"
)
func doJSONGetRequest(template string, requestPath string, beaconNodeIdx int, resp interface{}, bnType ...string) error {
if len(bnType) == 0 {
bnType = []string{"prysm"}
}
var port int
switch bnType[0] {
case "prysm":
port = params.TestParams.Ports.PrysmBeaconNodeGatewayPort
case "lighthouse":
port = params.TestParams.Ports.LighthouseBeaconNodeHTTPPort
default:
return fmt.Errorf(msgUnknownNode, bnType[0])
}
basePath := fmt.Sprintf(template, port+beaconNodeIdx)
httpResp, err := http.Get(
basePath + requestPath,
)
if err != nil {
return err
}
var body interface{}
if httpResp.StatusCode != http.StatusOK {
if httpResp.Header.Get("Content-Type") == api.JsonMediaType {
if err = json.NewDecoder(httpResp.Body).Decode(&body); err != nil {
return err
}
} else {
body, err = io.ReadAll(httpResp.Body)
if err != nil {
return err
}
}
return fmt.Errorf(msgRequestFailed, bnType[0], httpResp.StatusCode, body)
}
return json.NewDecoder(httpResp.Body).Decode(&resp)
}
func doSSZGetRequest(template string, requestPath string, beaconNodeIdx int, bnType ...string) ([]byte, error) {
if len(bnType) == 0 {
bnType = []string{"prysm"}
}
client := &http.Client{}
var port int
switch bnType[0] {
case "prysm":
port = params.TestParams.Ports.PrysmBeaconNodeGatewayPort
case "lighthouse":
port = params.TestParams.Ports.LighthouseBeaconNodeHTTPPort
default:
return nil, fmt.Errorf(msgUnknownNode, bnType[0])
}
basePath := fmt.Sprintf(template, port+beaconNodeIdx)
req, err := http.NewRequest(http.MethodGet, basePath+requestPath, nil)
if err != nil {
return nil, err
}
req.Header.Set("Accept", "application/octet-stream")
rsp, err := client.Do(req)
if err != nil {
return nil, err
}
if rsp.StatusCode != http.StatusOK {
var body interface{}
if err := json.NewDecoder(rsp.Body).Decode(&body); err != nil {
return nil, err
}
return nil, fmt.Errorf(msgRequestFailed, bnType[0], rsp.StatusCode, body)
}
defer closeBody(rsp.Body)
body, err := io.ReadAll(rsp.Body)
if err != nil {
return nil, err
}
return body, nil
}
func doJSONPostRequest(template string, requestPath string, beaconNodeIdx int, postData, resp interface{}, bnType ...string) error {
if len(bnType) == 0 {
bnType = []string{"prysm"}
}
var port int
switch bnType[0] {
case "prysm":
port = params.TestParams.Ports.PrysmBeaconNodeGatewayPort
case "lighthouse":
port = params.TestParams.Ports.LighthouseBeaconNodeHTTPPort
default:
return fmt.Errorf(msgUnknownNode, bnType[0])
}
basePath := fmt.Sprintf(template, port+beaconNodeIdx)
b, err := json.Marshal(postData)
if err != nil {
return err
}
httpResp, err := http.Post(
basePath+requestPath,
"application/json",
bytes.NewBuffer(b),
)
if err != nil {
return err
}
var body interface{}
if httpResp.StatusCode != http.StatusOK {
if httpResp.Header.Get("Content-Type") == api.JsonMediaType {
if err = json.NewDecoder(httpResp.Body).Decode(&body); err != nil {
return err
}
} else {
body, err = io.ReadAll(httpResp.Body)
if err != nil {
return err
}
}
return fmt.Errorf(msgRequestFailed, bnType[0], httpResp.StatusCode, body)
}
return json.NewDecoder(httpResp.Body).Decode(&resp)
}
func closeBody(body io.Closer) {
if err := body.Close(); err != nil {
log.WithError(err).Error("could not close response body")
}
}