Skip to content

Commit

Permalink
Return syncing status when node is optimistic (#13875)
Browse files Browse the repository at this point in the history
  • Loading branch information
rkapka authored Apr 12, 2024
1 parent 8cd05f0 commit ba6dff3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
11 changes: 8 additions & 3 deletions beacon-chain/rpc/eth/node/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (*Server) GetVersion(w http.ResponseWriter, r *http.Request) {

// GetHealth returns node health status in http status codes. Useful for load balancers.
func (s *Server) GetHealth(w http.ResponseWriter, r *http.Request) {
_, span := trace.StartSpan(r.Context(), "node.GetHealth")
ctx, span := trace.StartSpan(r.Context(), "node.GetHealth")
defer span.End()

rawSyncingStatus, syncingStatus, ok := shared.UintFromQuery(w, r, "syncing_status", false)
Expand All @@ -119,16 +119,21 @@ func (s *Server) GetHealth(w http.ResponseWriter, r *http.Request) {
return
}

if s.SyncChecker.Synced() {
optimistic, err := s.OptimisticModeFetcher.IsOptimistic(ctx)
if err != nil {
httputil.HandleError(w, "Could not check optimistic status: "+err.Error(), http.StatusInternalServerError)
}
if s.SyncChecker.Synced() && !optimistic {
return
}
if s.SyncChecker.Syncing() || s.SyncChecker.Initialized() {
if s.SyncChecker.Syncing() || optimistic {
if rawSyncingStatus != "" {
w.WriteHeader(intSyncingStatus)
} else {
w.WriteHeader(http.StatusPartialContent)
}
return
}

w.WriteHeader(http.StatusServiceUnavailable)
}
21 changes: 14 additions & 7 deletions beacon-chain/rpc/eth/node/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ func TestGetVersion(t *testing.T) {

func TestGetHealth(t *testing.T) {
checker := &syncmock.Sync{}
optimisticFetcher := &mock.ChainService{Optimistic: false}
s := &Server{
SyncChecker: checker,
SyncChecker: checker,
OptimisticModeFetcher: optimisticFetcher,
}

request := httptest.NewRequest(http.MethodGet, "http://example.com/eth/v1/node/health", nil)
Expand All @@ -101,25 +103,30 @@ func TestGetHealth(t *testing.T) {
s.GetHealth(writer, request)
assert.Equal(t, http.StatusServiceUnavailable, writer.Code)

checker.IsInitialized = true
request = httptest.NewRequest(http.MethodGet, "http://example.com/eth/v1/node/health", nil)
checker.IsSyncing = true
checker.IsSynced = false
request = httptest.NewRequest(http.MethodGet, fmt.Sprintf("http://example.com/eth/v1/node/health?syncing_status=%d", http.StatusPaymentRequired), nil)
writer = httptest.NewRecorder()
writer.Body = &bytes.Buffer{}
s.GetHealth(writer, request)
assert.Equal(t, http.StatusPartialContent, writer.Code)
assert.Equal(t, http.StatusPaymentRequired, writer.Code)

request = httptest.NewRequest(http.MethodGet, fmt.Sprintf("http://example.com/eth/v1/node/health?syncing_status=%d", http.StatusPaymentRequired), nil)
checker.IsSyncing = false
checker.IsSynced = true
request = httptest.NewRequest(http.MethodGet, "http://example.com/eth/v1/node/health", nil)
writer = httptest.NewRecorder()
writer.Body = &bytes.Buffer{}
s.GetHealth(writer, request)
assert.Equal(t, http.StatusPaymentRequired, writer.Code)
assert.Equal(t, http.StatusOK, writer.Code)

checker.IsSyncing = false
checker.IsSynced = true
optimisticFetcher.Optimistic = true
request = httptest.NewRequest(http.MethodGet, "http://example.com/eth/v1/node/health", nil)
writer = httptest.NewRecorder()
writer.Body = &bytes.Buffer{}
s.GetHealth(writer, request)
assert.Equal(t, http.StatusOK, writer.Code)
assert.Equal(t, http.StatusPartialContent, writer.Code)
}

func TestGetIdentity(t *testing.T) {
Expand Down

0 comments on commit ba6dff3

Please sign in to comment.