Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
Giulio2002 committed Jan 5, 2024
1 parent 56531d9 commit 46f266b
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 5 deletions.
4 changes: 3 additions & 1 deletion cl/beacon/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ func (a *ApiHandler) init() {
// otterscn specific ones are commented as such
r.Route("/eth", func(r chi.Router) {
r.Route("/v1", func(r chi.Router) {

r.Get("/events", http.NotFound)
r.Route("/node", func(r chi.Router) {
r.Get("/health", a.GetEthV1NodeHealth)
})
r.Route("/config", func(r chi.Router) {
r.Get("/spec", beaconhttp.HandleEndpointFunc(a.getSpec))
r.Get("/deposit_contract", beaconhttp.HandleEndpointFunc(a.getDepositContract))
Expand Down
20 changes: 20 additions & 0 deletions cl/beacon/handler/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package handler

import "net/http"

func (a *ApiHandler) GetEthV1NodeHealth(w http.ResponseWriter, r *http.Request) {
syncingStatus, err := uint64FromQueryParams(r, "syncing_status")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
syncingCode := http.StatusOK
if syncingStatus != nil {
syncingCode = int(*syncingStatus)
}
if a.syncedData.Syncing() {
w.WriteHeader(syncingCode)
return
}
w.WriteHeader(http.StatusOK)
}
47 changes: 47 additions & 0 deletions cl/beacon/handler/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package handler

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/stretchr/testify/require"
)

func TestNodeSyncing(t *testing.T) {
// i just want the correct schema to be generated
_, _, _, _, _, handler, _, _, _ := setupTestingHandler(t, clparams.Phase0Version)

// Call GET /eth/v1/node/health
server := httptest.NewServer(handler.mux)
defer server.Close()

req, err := http.NewRequest("GET", server.URL+"/eth/v1/node/health?syncing_status=666", nil)
require.NoError(t, err)

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, 666, resp.StatusCode)
}

func TestNodeSyncingTip(t *testing.T) {
// i just want the correct schema to be generated
_, _, _, _, post, handler, _, sm, _ := setupTestingHandler(t, clparams.Phase0Version)

// Call GET /eth/v1/node/health
server := httptest.NewServer(handler.mux)
defer server.Close()

req, err := http.NewRequest("GET", server.URL+"/eth/v1/node/health?syncing_status=666", nil)
require.NoError(t, err)

require.NoError(t, sm.OnHeadState(post))
s, cancel := sm.HeadState()
s.SetSlot(999999999999999)
cancel()

resp, err := http.DefaultClient.Do(req)
require.NoError(t, err)
require.Equal(t, 200, resp.StatusCode)
}
5 changes: 4 additions & 1 deletion cl/beacon/synced_data/synced_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func (s *SyncedDataManager) OnHeadState(newState *state.CachingBeaconState) (err
defer s.mu.Unlock()
if s.headState == nil {
s.headState, err = newState.Copy()
if err != nil {
return err
}
}
err = newState.CopyInto(s.headState)
if err != nil {
Expand All @@ -56,7 +59,7 @@ func (s *SyncedDataManager) Syncing() bool {
s.mu.RLock()
defer s.mu.RUnlock()
if s.headState == nil {
return false
return true
}

headEpoch := utils.GetCurrentEpoch(s.headState.GenesisTime(), s.cfg.SecondsPerSlot, s.cfg.SlotsPerEpoch)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ func runTest(t *testing.T, blocks []*cltypes.SignedBeaconBlock, preState, postSt
}

func TestStateAntiquaryCapella(t *testing.T) {
//t.Skip()
t.Skip()
blocks, preState, postState := tests.GetCapellaRandom()
runTest(t, blocks, preState, postState)
}

func TestStateAntiquaryPhase0(t *testing.T) {
//t.Skip()
t.Skip()
blocks, preState, postState := tests.GetPhase0Random()
runTest(t, blocks, preState, postState)
}

func TestStateAntiquaryBellatrix(t *testing.T) {
//t.Skip()
t.Skip()
blocks, preState, postState := tests.GetBellatrixRandom()
runTest(t, blocks, preState, postState)
}

0 comments on commit 46f266b

Please sign in to comment.