Skip to content

Commit a6487be

Browse files
committed
Move functionality to new register_validator.go file
1 parent 3de02f5 commit a6487be

File tree

2 files changed

+112
-68
lines changed

2 files changed

+112
-68
lines changed

server/register_validator.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package server
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"fmt"
7+
"net/http"
8+
"net/url"
9+
10+
"github.com/flashbots/mev-boost/server/params"
11+
"github.com/flashbots/mev-boost/server/types"
12+
"github.com/sirupsen/logrus"
13+
)
14+
15+
func (m *BoostService) registerValidator(log *logrus.Entry, regBytes []byte, header http.Header) error {
16+
respErrCh := make(chan error, len(m.relays))
17+
18+
// Forward request to each relay
19+
for _, relay := range m.relays {
20+
go func(relay types.RelayEntry) {
21+
// Get the URL for this relay
22+
requestURL := relay.GetURI(params.PathRegisterValidator)
23+
log := log.WithField("url", requestURL)
24+
25+
// Build the new request
26+
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, requestURL.String(), bytes.NewReader(regBytes))
27+
if err != nil {
28+
log.WithError(err).Warn("error creating new request")
29+
return
30+
}
31+
32+
// Extend the request header with our values
33+
for key, values := range header {
34+
req.Header[key] = values
35+
}
36+
37+
// Send the request
38+
resp, err := m.httpClientRegVal.Do(req)
39+
if err != nil {
40+
log.WithError(err).Warn("error calling registerValidator on relay")
41+
respErrCh <- err
42+
return
43+
}
44+
resp.Body.Close()
45+
46+
// Check if response is successful
47+
if resp.StatusCode == http.StatusOK {
48+
respErrCh <- nil
49+
} else {
50+
respErrCh <- fmt.Errorf("%w: %d", errHTTPErrorResponse, resp.StatusCode)
51+
}
52+
}(relay)
53+
}
54+
55+
// Return OK if any relay responds OK
56+
for range m.relays {
57+
respErr := <-respErrCh
58+
if respErr == nil {
59+
// Goroutines are independent, so if there are a lot of configured
60+
// relays and the first one responds OK, this will continue to send
61+
// validator registrations to the other relays.
62+
return nil
63+
}
64+
}
65+
66+
// None of the relays responded OK
67+
return errNoSuccessfulRelayResponse
68+
}
69+
70+
func (m *BoostService) sendValidatorRegistrationsToRelayMonitors(log *logrus.Entry, regBytes []byte, header http.Header) {
71+
// Forward request to each relay monitor
72+
for _, relayMonitor := range m.relayMonitors {
73+
go func(relayMonitor *url.URL) {
74+
// Get the URL for this relay monitor
75+
requestURL := types.GetURI(relayMonitor, params.PathRegisterValidator)
76+
log := log.WithField("url", requestURL)
77+
78+
// Build the new request
79+
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, requestURL.String(), bytes.NewReader(regBytes))
80+
if err != nil {
81+
log.WithError(err).Warn("error creating new request")
82+
return
83+
}
84+
85+
// Extend the request header with our values
86+
for key, values := range header {
87+
req.Header[key] = values
88+
}
89+
90+
// Send the request
91+
resp, err := m.httpClientRegVal.Do(req)
92+
if err != nil {
93+
log.WithError(err).Warn("error calling registerValidator on relay monitor")
94+
return
95+
}
96+
resp.Body.Close()
97+
}(relayMonitor)
98+
}
99+
}

server/service.go

Lines changed: 13 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -208,22 +208,6 @@ func (m *BoostService) startBidCacheCleanupTask() {
208208
}
209209
}
210210

211-
func (m *BoostService) sendValidatorRegistrationsToRelayMonitors(regBytes []byte) {
212-
log := m.log.WithField("method", "sendValidatorRegistrationsToRelayMonitors").WithField("registrationsLen", len(regBytes))
213-
for _, relayMonitor := range m.relayMonitors {
214-
go func(relayMonitor *url.URL) {
215-
url := types.GetURI(relayMonitor, params.PathRegisterValidator)
216-
log = log.WithField("url", url)
217-
_, err := SendHTTPRequest(context.Background(), m.httpClientRegVal, http.MethodPost, url.String(), "", nil, regBytes, nil)
218-
if err != nil {
219-
log.WithError(err).Warn("error calling registerValidator on relay monitor")
220-
return
221-
}
222-
log.Debug("sent validator registrations to relay monitor")
223-
}(relayMonitor)
224-
}
225-
}
226-
227211
func (m *BoostService) handleRoot(w http.ResponseWriter, _ *http.Request) {
228212
m.respondOK(w, nilResponse)
229213
}
@@ -251,10 +235,9 @@ func (m *BoostService) handleRegisterValidator(w http.ResponseWriter, req *http.
251235
log = log.WithFields(logrus.Fields{"ua": ua})
252236

253237
// Additional header fields
254-
headers := map[string]string{
255-
"User-Agent": wrapUserAgent(ua),
256-
HeaderStartTimeUnixMS: fmt.Sprintf("%d", time.Now().UTC().UnixMilli()),
257-
}
238+
header := req.Header
239+
header.Set("User-Agent", wrapUserAgent(ua))
240+
header.Set(HeaderStartTimeUnixMS, fmt.Sprintf("%d", time.Now().UTC().UnixMilli()))
258241

259242
// Read the validator registrations
260243
regBytes, err := io.ReadAll(req.Body)
@@ -264,56 +247,18 @@ func (m *BoostService) handleRegisterValidator(w http.ResponseWriter, req *http.
264247
}
265248
req.Body.Close()
266249

267-
// Forward request to each relay
268-
respErrCh := make(chan error, len(m.relays))
269-
for _, relay := range m.relays {
270-
go func(relay types.RelayEntry) {
271-
// Build the new request
272-
relayReq := req.Clone(req.Context())
273-
relayReq.URL = relay.GetURI(params.PathRegisterValidator)
274-
relayReq.Body = io.NopCloser(bytes.NewReader(regBytes))
275-
for key, value := range headers {
276-
relayReq.Header.Set(key, value)
277-
}
278-
279-
// Create a new logger with this request URL
280-
log := log.WithField("url", relayReq.URL)
281-
282-
// Send the request
283-
resp, err := m.httpClientGetHeader.Do(relayReq)
284-
if err != nil {
285-
log.WithError(err).Warn("error calling registerValidator on relay")
286-
respErrCh <- err
287-
return
288-
}
289-
resp.Body.Close()
290-
291-
// Check if response is successful
292-
if resp.StatusCode == http.StatusOK {
293-
respErrCh <- nil
294-
} else {
295-
respErrCh <- fmt.Errorf("%w: %d", errHTTPErrorResponse, resp.StatusCode)
296-
}
297-
}(relay)
298-
}
299-
300250
// Send the registrations to relay monitors, if configured
301-
go m.sendValidatorRegistrationsToRelayMonitors(regBytes)
302-
303-
// Return OK if any relay responds OK
304-
for range m.relays {
305-
respErr := <-respErrCh
306-
if respErr == nil {
307-
w.WriteHeader(http.StatusOK)
308-
// Goroutines are independent, so if there are a lot of configured
309-
// relays and the first one responds OK, this will continue to send
310-
// validator registrations to the other relays.
311-
return
312-
}
313-
}
251+
go m.sendValidatorRegistrationsToRelayMonitors(log, regBytes, header)
314252

315-
// None of the relays responded OK
316-
m.respondError(w, http.StatusBadGateway, errNoSuccessfulRelayResponse.Error())
253+
// Send the registrations to each relay
254+
err = m.registerValidator(log, regBytes, header)
255+
if err == nil {
256+
// One of the relays responded OK
257+
m.respondOK(w, nilResponse)
258+
} else {
259+
// None of the relays responded OK
260+
m.respondError(w, http.StatusBadGateway, err.Error())
261+
}
317262
}
318263

319264
// handleGetHeader requests bids from the relays

0 commit comments

Comments
 (0)