Skip to content

Commit

Permalink
Return syncing status when node is optimistic
Browse files Browse the repository at this point in the history
  • Loading branch information
rkapka committed Apr 12, 2024
1 parent 8cd05f0 commit fcb7d95
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 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() {
if rawSyncingStatus != "" {
w.WriteHeader(intSyncingStatus)
} else {
w.WriteHeader(http.StatusPartialContent)
}
return
}

w.WriteHeader(http.StatusServiceUnavailable)
}
19 changes: 11 additions & 8 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,13 +103,7 @@ 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)
writer = httptest.NewRecorder()
writer.Body = &bytes.Buffer{}
s.GetHealth(writer, request)
assert.Equal(t, http.StatusPartialContent, writer.Code)

checker.IsSyncing = true
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{}
Expand All @@ -120,6 +116,13 @@ func TestGetHealth(t *testing.T) {
writer.Body = &bytes.Buffer{}
s.GetHealth(writer, request)
assert.Equal(t, http.StatusOK, writer.Code)

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.StatusPartialContent, writer.Code)
}

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

0 comments on commit fcb7d95

Please sign in to comment.