Return 404 from eth/v1/beacon/headers when there are no blocks (#13185)

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Radosław Kapka 2023-11-13 13:06:26 -05:00 committed by GitHub
parent 0946b5853f
commit 9b97f3fd92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -1747,6 +1747,11 @@ func (s *Server) GetBlockHeaders(w http.ResponseWriter, r *http.Request) {
}
}
if len(blks) == 0 {
http2.HandleError(w, "No blocks found", http.StatusNotFound)
return
}
isOptimistic := false
isFinalized := true
blkHdrs := make([]*shared.SignedBeaconBlockHeaderContainer, len(blks))

View File

@ -2670,6 +2670,7 @@ func TestGetBlockHeaders(t *testing.T) {
writer.Body = &bytes.Buffer{}
bs.GetBlockHeaders(writer, request)
require.Equal(t, http.StatusOK, writer.Code)
resp := &GetBlockHeadersResponse{}
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
@ -2721,6 +2722,7 @@ func TestGetBlockHeaders(t *testing.T) {
writer.Body = &bytes.Buffer{}
bs.GetBlockHeaders(writer, request)
require.Equal(t, http.StatusOK, writer.Code)
resp := &GetBlockHeadersResponse{}
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
assert.Equal(t, true, resp.ExecutionOptimistic)
@ -2764,6 +2766,7 @@ func TestGetBlockHeaders(t *testing.T) {
writer.Body = &bytes.Buffer{}
bs.GetBlockHeaders(writer, request)
require.Equal(t, http.StatusOK, writer.Code)
resp := &GetBlockHeadersResponse{}
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
assert.Equal(t, true, resp.Finalized)
@ -2777,6 +2780,7 @@ func TestGetBlockHeaders(t *testing.T) {
writer.Body = &bytes.Buffer{}
bs.GetBlockHeaders(writer, request)
require.Equal(t, http.StatusOK, writer.Code)
resp := &GetBlockHeadersResponse{}
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
assert.Equal(t, false, resp.Finalized)
@ -2789,10 +2793,25 @@ func TestGetBlockHeaders(t *testing.T) {
writer.Body = &bytes.Buffer{}
bs.GetBlockHeaders(writer, request)
require.Equal(t, http.StatusOK, writer.Code)
resp := &GetBlockHeadersResponse{}
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), resp))
assert.Equal(t, false, resp.Finalized)
})
t.Run("no blocks found", func(t *testing.T) {
urlWithParams := fmt.Sprintf("%s?parent_root=%s", url, hexutil.Encode(bytes.Repeat([]byte{1}, 32)))
request := httptest.NewRequest(http.MethodGet, urlWithParams, nil)
writer := httptest.NewRecorder()
writer.Body = &bytes.Buffer{}
bs.GetBlockHeaders(writer, request)
require.Equal(t, http.StatusNotFound, writer.Code)
e := &http2.DefaultErrorJson{}
require.NoError(t, json.Unmarshal(writer.Body.Bytes(), e))
assert.Equal(t, http.StatusNotFound, e.Code)
assert.StringContains(t, "No blocks found", e.Message)
})
})
}