Skip to content

Commit 7cb5ddf

Browse files
getHeader 204 (no content) (#190)
* getHeader 204 (no content) * Update server/utils_test.go Co-authored-by: Luca G.F. <[email protected]> * cleanup - remove commented lines Co-authored-by: Luca G.F. <[email protected]>
1 parent 52cf1a2 commit 7cb5ddf

File tree

6 files changed

+37
-34
lines changed

6 files changed

+37
-34
lines changed

cmd/test-cli/beacon.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type partialSignedBeaconBlock struct {
5353

5454
func getCurrentBeaconBlock(beaconEndpoint string) (beaconBlockData, error) {
5555
var blockResp partialSignedBeaconBlock
56-
err := server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodGet, beaconEndpoint+"/eth/v2/beacon/blocks/head", nil, &blockResp)
56+
_, err := server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodGet, beaconEndpoint+"/eth/v2/beacon/blocks/head", nil, &blockResp)
5757
if err != nil {
5858
return beaconBlockData{}, err
5959
}

cmd/test-cli/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func doRegisterValidator(v validatorPrivateData, boostEndpoint string, builderSi
3131
if err != nil {
3232
log.WithError(err).Fatal("Could not prepare registration message")
3333
}
34-
err = server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodPost, boostEndpoint+"/eth/v1/builder/validators", message, nil)
34+
_, err = server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodPost, boostEndpoint+"/eth/v1/builder/validators", message, nil)
3535

3636
if err != nil {
3737
log.WithError(err).Fatal("Validator registration not successful")
@@ -68,7 +68,7 @@ func doGetHeader(v validatorPrivateData, boostEndpoint string, beaconNode Beacon
6868
uri := fmt.Sprintf("%s/eth/v1/builder/header/%d/%s/%s", boostEndpoint, currentBlock.Slot+1, currentBlockHash, v.Pk.String())
6969

7070
var getHeaderResp boostTypes.GetHeaderResponse
71-
if err := server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodGet, uri, nil, &getHeaderResp); err != nil {
71+
if _, err := server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodGet, uri, nil, &getHeaderResp); err != nil {
7272
log.WithError(err).WithField("currentBlockHash", currentBlockHash).Fatal("Could not get header")
7373
}
7474

@@ -120,7 +120,7 @@ func doGetPayload(v validatorPrivateData, boostEndpoint string, beaconNode Beaco
120120
Signature: signature,
121121
}
122122
var respPayload boostTypes.GetPayloadResponse
123-
if err := server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodPost, boostEndpoint+"/eth/v1/builder/blinded_blocks", payload, &respPayload); err != nil {
123+
if _, err := server.SendHTTPRequest(context.TODO(), *http.DefaultClient, http.MethodPost, boostEndpoint+"/eth/v1/builder/blinded_blocks", payload, &respPayload); err != nil {
124124
log.WithError(err).Fatal("could not get payload")
125125
}
126126

server/service.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ func (m *BoostService) handleStatus(w http.ResponseWriter, req *http.Request) {
162162
log.Debug("Checking relay status")
163163

164164
url := relay.Address + pathStatus
165-
err := SendHTTPRequest(ctx, m.httpClient, http.MethodGet, url, nil, nil)
166-
165+
_, err := SendHTTPRequest(ctx, m.httpClient, http.MethodGet, url, nil, nil)
167166
if err != nil && ctx.Err() != context.Canceled {
168167
log.WithError(err).Error("failed to retrieve relay status")
169168
return
@@ -208,7 +207,7 @@ func (m *BoostService) handleRegisterValidator(w http.ResponseWriter, req *http.
208207
url := relayAddr + pathRegisterValidator
209208
log := log.WithField("url", url)
210209

211-
err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodPost, url, payload, nil)
210+
_, err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodPost, url, payload, nil)
212211
if err != nil {
213212
log.WithError(err).Warn("error in registerValidator to relay")
214213
return
@@ -271,13 +270,17 @@ func (m *BoostService) handleGetHeader(w http.ResponseWriter, req *http.Request)
271270
url := fmt.Sprintf("%s/eth/v1/builder/header/%s/%s/%s", relayAddr, slot, parentHashHex, pubkey)
272271
log := log.WithField("url", url)
273272
responsePayload := new(types.GetHeaderResponse)
274-
err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodGet, url, nil, responsePayload)
275-
273+
code, err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodGet, url, nil, responsePayload)
276274
if err != nil {
277275
log.WithError(err).Warn("error making request to relay")
278276
return
279277
}
280278

279+
if code == http.StatusNoContent {
280+
log.Info("no-content response")
281+
return
282+
}
283+
281284
// Skip if invalid payload
282285
if responsePayload.Data == nil || responsePayload.Data.Message == nil || responsePayload.Data.Message.Header == nil || responsePayload.Data.Message.Header.BlockHash == nilHash {
283286
return
@@ -329,8 +332,8 @@ func (m *BoostService) handleGetHeader(w http.ResponseWriter, req *http.Request)
329332
wg.Wait()
330333

331334
if result.Data == nil || result.Data.Message == nil || result.Data.Message.Header == nil || result.Data.Message.Header.BlockHash == nilHash {
332-
log.Warn("no successful relay response")
333-
m.respondError(w, http.StatusBadGateway, errNoSuccessfulRelayResponse.Error())
335+
log.Info("no bids received from relay")
336+
w.WriteHeader(http.StatusNoContent)
334337
return
335338
}
336339

@@ -365,7 +368,7 @@ func (m *BoostService) handleGetPayload(w http.ResponseWriter, req *http.Request
365368
url := fmt.Sprintf("%s%s", relayAddr, pathGetPayload)
366369
log := log.WithField("url", url)
367370
responsePayload := new(types.GetPayloadResponse)
368-
err := SendHTTPRequest(requestCtx, m.httpClient, http.MethodPost, url, payload, responsePayload)
371+
_, err := SendHTTPRequest(requestCtx, m.httpClient, http.MethodPost, url, payload, responsePayload)
369372

370373
if err != nil {
371374
log.WithError(err).Warn("error making request to relay")
@@ -421,7 +424,7 @@ func (m *BoostService) CheckRelays() bool {
421424
for _, relay := range m.relays {
422425
m.log.WithField("relay", relay).Info("Checking relay")
423426

424-
err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodGet, relay.Address+pathStatus, nil, nil)
427+
_, err := SendHTTPRequest(context.Background(), m.httpClient, http.MethodGet, relay.Address+pathStatus, nil, nil)
425428
if err != nil {
426429
m.log.WithError(err).WithField("relay", relay).Error("relay check failed")
427430
return false

server/service_test.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ func TestGetHeader(t *testing.T) {
257257
rr = backend.request(t, http.MethodGet, path, nil)
258258
require.Equal(t, 2, backend.relays[0].GetRequestCount(path))
259259
require.Equal(t, 2, backend.relays[1].GetRequestCount(path))
260-
require.Equal(t, `{"code":502,"message":"no successful relay response"}`+"\n", rr.Body.String())
261-
require.Equal(t, http.StatusBadGateway, rr.Code, rr.Body.String())
260+
require.Equal(t, http.StatusNoContent, rr.Code)
262261
})
263262

264263
t.Run("Use header with highest value", func(t *testing.T) {
@@ -319,9 +318,8 @@ func TestGetHeader(t *testing.T) {
319318
rr := backend.request(t, http.MethodGet, path, nil)
320319
require.Equal(t, 1, backend.relays[0].GetRequestCount(path))
321320

322-
// Request should have failed
323-
require.Equal(t, `{"code":502,"message":"no successful relay response"}`+"\n", rr.Body.String())
324-
require.Equal(t, http.StatusBadGateway, rr.Code, rr.Body.String())
321+
// Request should have no content
322+
require.Equal(t, http.StatusNoContent, rr.Code)
325323
})
326324

327325
t.Run("Invalid relay signature", func(t *testing.T) {
@@ -339,9 +337,8 @@ func TestGetHeader(t *testing.T) {
339337
rr := backend.request(t, http.MethodGet, path, nil)
340338
require.Equal(t, 1, backend.relays[0].GetRequestCount(path))
341339

342-
// Request should have failed
343-
require.Equal(t, `{"code":502,"message":"no successful relay response"}`+"\n", rr.Body.String())
344-
require.Equal(t, http.StatusBadGateway, rr.Code, rr.Body.String())
340+
// Request should have no content
341+
require.Equal(t, http.StatusNoContent, rr.Code)
345342
})
346343

347344
t.Run("Invalid slot number", func(t *testing.T) {
@@ -381,8 +378,7 @@ func TestGetHeader(t *testing.T) {
381378

382379
invalidParentHashPath := getPath(1, types.Hash{}, pubkey)
383380
rr := backend.request(t, http.MethodGet, invalidParentHashPath, nil)
384-
385-
require.Equal(t, `{"code":502,"message":"no successful relay response"}`+"\n", rr.Body.String())
381+
require.Equal(t, http.StatusNoContent, rr.Code)
386382
require.Equal(t, 0, backend.relays[0].GetRequestCount(path))
387383
})
388384
}

server/utils.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,53 @@ import (
1515
)
1616

1717
// SendHTTPRequest - prepare and send HTTP request, marshaling the payload if any, and decoding the response if dst is set
18-
func SendHTTPRequest(ctx context.Context, client http.Client, method, url string, payload any, dst any) error {
18+
func SendHTTPRequest(ctx context.Context, client http.Client, method, url string, payload any, dst any) (code int, err error) {
1919
var req *http.Request
20-
var err error
2120

2221
if payload == nil {
2322
req, err = http.NewRequestWithContext(ctx, method, url, nil)
2423
} else {
2524
payloadBytes, err2 := json.Marshal(payload)
2625
if err2 != nil {
27-
return fmt.Errorf("could not marshal request: %w", err2)
26+
return 0, fmt.Errorf("could not marshal request: %w", err2)
2827
}
2928
req, err = http.NewRequestWithContext(ctx, method, url, bytes.NewReader(payloadBytes))
3029
}
3130
if err != nil {
32-
return fmt.Errorf("could not prepare request: %w", err)
31+
return 0, fmt.Errorf("could not prepare request: %w", err)
3332
}
3433

3534
req.Header.Add("Content-Type", "application/json")
3635
resp, err := client.Do(req)
3736
if err != nil {
38-
return err
37+
return 0, err
3938
}
4039
defer resp.Body.Close()
4140

41+
if resp.StatusCode == http.StatusNoContent {
42+
return resp.StatusCode, nil
43+
}
44+
4245
if resp.StatusCode > 299 {
4346
bodyBytes, err := io.ReadAll(resp.Body)
4447
if err != nil {
45-
return fmt.Errorf("could not read error response body for status code %d: %w", resp.StatusCode, err)
48+
return resp.StatusCode, fmt.Errorf("could not read error response body for status code %d: %w", resp.StatusCode, err)
4649
}
47-
return fmt.Errorf("HTTP error response: %d / %s", resp.StatusCode, string(bodyBytes))
50+
return resp.StatusCode, fmt.Errorf("HTTP error response: %d / %s", resp.StatusCode, string(bodyBytes))
4851
}
4952

5053
if dst != nil {
5154
bodyBytes, err := io.ReadAll(resp.Body)
5255
if err != nil {
53-
return fmt.Errorf("could not read response body: %w", err)
56+
return resp.StatusCode, fmt.Errorf("could not read response body: %w", err)
5457
}
5558

5659
if err := json.Unmarshal(bodyBytes, dst); err != nil {
57-
return fmt.Errorf("could not unmarshal response %s: %w", string(bodyBytes), err)
60+
return resp.StatusCode, fmt.Errorf("could not unmarshal response %s: %w", string(bodyBytes), err)
5861
}
5962
}
6063

61-
return nil
64+
return resp.StatusCode, nil
6265
}
6366

6467
// ComputeDomain computes the signing domain

server/utils_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
func TestMakePostRequest(t *testing.T) {
1212
// Test errors
1313
var x chan bool
14-
err := SendHTTPRequest(context.Background(), *http.DefaultClient, http.MethodGet, "", x, nil)
14+
code, err := SendHTTPRequest(context.Background(), *http.DefaultClient, http.MethodGet, "", x, nil)
1515
require.Error(t, err)
16+
require.Equal(t, 0, code)
1617
}

0 commit comments

Comments
 (0)