Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward validator registrations without decoding #733

Merged
merged 9 commits into from
Feb 13, 2025
21 changes: 15 additions & 6 deletions server/mock/mock_relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,21 @@ func (m *Relay) handleRegisterValidator(w http.ResponseWriter, req *http.Request

// defaultHandleRegisterValidator returns the default handler for handleRegisterValidator
func (m *Relay) defaultHandleRegisterValidator(w http.ResponseWriter, req *http.Request) {
payload := []builderApiV1.SignedValidatorRegistration{}
decoder := json.NewDecoder(req.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
reqContentType := req.Header.Get("Content-Type")
if reqContentType == "" || reqContentType == "application/json" {
var payload []builderApiV1.SignedValidatorRegistration
decoder := json.NewDecoder(req.Body)
decoder.DisallowUnknownFields()
if err := decoder.Decode(&payload); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
} else if reqContentType == "application/octet-stream" {
// TODO(jtraglia): Handle this when a SignedValidatorRegistrationList type exists.
// See: https://github.com/attestantio/go-builder-client/pull/38
_ = reqContentType
} else {
panic("invalid content type: " + reqContentType)
}

w.Header().Set("Content-Type", "application/json")
Expand Down
100 changes: 100 additions & 0 deletions server/register_validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package server

import (
"bytes"
"context"
"fmt"
"net/http"
"net/url"

"github.com/flashbots/mev-boost/server/params"
"github.com/flashbots/mev-boost/server/types"
"github.com/sirupsen/logrus"
)

func (m *BoostService) registerValidator(log *logrus.Entry, regBytes []byte, header http.Header) error {
respErrCh := make(chan error, len(m.relays))

// Forward request to each relay
for _, relay := range m.relays {
go func(relay types.RelayEntry) {
// Get the URL for this relay
requestURL := relay.GetURI(params.PathRegisterValidator)
log := log.WithField("url", requestURL)

// Build the new request
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, requestURL, bytes.NewReader(regBytes))
if err != nil {
log.WithError(err).Warn("error creating new request")
respErrCh <- err
return
}

// Extend the request header with our values
for key, values := range header {
req.Header[key] = values
}

// Send the request
resp, err := m.httpClientRegVal.Do(req)
if err != nil {
log.WithError(err).Warn("error calling registerValidator on relay")
respErrCh <- err
return
}
resp.Body.Close()

// Check if response is successful
if resp.StatusCode == http.StatusOK {
respErrCh <- nil
} else {
respErrCh <- fmt.Errorf("%w: %d", errHTTPErrorResponse, resp.StatusCode)
}
}(relay)
}

// Return OK if any relay responds OK
for range m.relays {
respErr := <-respErrCh
if respErr == nil {
// Goroutines are independent, so if there are a lot of configured
// relays and the first one responds OK, this will continue to send
// validator registrations to the other relays.
return nil
}
}

// None of the relays responded OK
return errNoSuccessfulRelayResponse
}

func (m *BoostService) sendValidatorRegistrationsToRelayMonitors(log *logrus.Entry, regBytes []byte, header http.Header) {
// Forward request to each relay monitor
for _, relayMonitor := range m.relayMonitors {
go func(relayMonitor *url.URL) {
// Get the URL for this relay monitor
requestURL := types.GetURI(relayMonitor, params.PathRegisterValidator)
log := log.WithField("url", requestURL)

// Build the new request
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, requestURL, bytes.NewReader(regBytes))
if err != nil {
log.WithError(err).Warn("error creating new request")
return
}

// Extend the request header with our values
for key, values := range header {
req.Header[key] = values
}

// Send the request
resp, err := m.httpClientRegVal.Do(req)
if err != nil {
log.WithError(err).Warn("error calling registerValidator on relay monitor")
return
}
resp.Body.Close()
}(relayMonitor)
}
}
Loading
Loading