generated from flashbots/go-template
/
util.go
120 lines (105 loc) · 3.22 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
package beaconclient
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
)
var (
ErrHTTPErrorResponse = errors.New("got an HTTP error response")
ErrInvalidRequestPayload = errors.New("invalid request payload")
StateIDHead = "head"
StateIDGenesis = "genesis"
StateIDFinalized = "finalized"
StateIDJustified = "justified"
)
func parseBroadcastModeString(s string) (BroadcastMode, bool) {
broadcastModeMap := map[string]BroadcastMode{
"gossip": Gossip,
"consensus": Consensus,
"consensus_and_equivocation": ConsensusAndEquivocation,
}
b, ok := broadcastModeMap[strings.ToLower(s)]
return b, ok
}
func makeJSONRequest(method, url string, payload any) (*http.Request, error) {
payloadBytes, err := json.Marshal(payload)
if err != nil {
return nil, fmt.Errorf("could not marshal request: %w", err)
}
req, err := http.NewRequest(method, url, bytes.NewReader(payloadBytes))
if err != nil {
return nil, fmt.Errorf("invalid request for %s: %w", url, err)
}
// Set content-type
req.Header.Add("Content-Type", "application/json")
return req, nil
}
func makeSSZRequest(method, url string, payload any) (*http.Request, error) {
payloadBytes, ok := payload.([]byte)
if !ok {
return nil, fmt.Errorf("invalid payload type for SSZ request: %w", ErrInvalidRequestPayload)
}
req, err := http.NewRequest(method, url, bytes.NewReader(payloadBytes))
if err != nil {
return nil, fmt.Errorf("invalid request for %s: %w", url, err)
}
// Set content-type
req.Header.Add("Content-Type", "application/octet-stream")
return req, nil
}
func fetchBeacon(method, url string, payload, dst any, timeout *time.Duration, headers http.Header, ssz bool) (code int, err error) {
var req *http.Request
if payload == nil {
req, err = http.NewRequest(method, url, nil)
} else {
if ssz {
req, err = makeSSZRequest(method, url, payload)
} else {
req, err = makeJSONRequest(method, url, payload)
}
}
if err != nil {
return 0, fmt.Errorf("invalid request for %s: %w", url, err)
}
for k, v := range headers {
req.Header.Add(k, v[0])
}
req.Header.Set("accept", "application/json")
client := http.DefaultClient
if timeout != nil && timeout.Seconds() > 0 {
client = &http.Client{ //nolint:exhaustruct
Timeout: *timeout,
}
}
resp, err := client.Do(req)
if err != nil {
return 0, fmt.Errorf("client refused for %s: %w", url, err)
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return resp.StatusCode, fmt.Errorf("could not read response body for %s: %w", url, err)
}
if resp.StatusCode >= http.StatusMultipleChoices {
ec := &struct {
Code int `json:"code"`
Message string `json:"message"`
}{}
if err = json.Unmarshal(bodyBytes, ec); err != nil {
return resp.StatusCode, fmt.Errorf("could not unmarshal error response from beacon node for %s from %s: %w", url, string(bodyBytes), err)
}
return resp.StatusCode, fmt.Errorf("%w: %s", ErrHTTPErrorResponse, ec.Message)
}
if dst != nil {
err = json.Unmarshal(bodyBytes, dst)
if err != nil {
return resp.StatusCode, fmt.Errorf("could not unmarshal response for %s from %s: %w", url, string(bodyBytes), err)
}
}
return resp.StatusCode, nil
}