From c0aee23192ee04f8b4abaeada83d7d22bf0a5552 Mon Sep 17 00:00:00 2001 From: otherview Date: Thu, 21 Nov 2024 12:27:00 +0000 Subject: [PATCH] pr comments --- api/admin/health/health.go | 12 ++++++------ api/admin/health/health_api.go | 10 +++++----- api/admin/health/health_test.go | 8 ++++---- api/admin/loglevel/log_level_test.go | 1 - 4 files changed, 15 insertions(+), 16 deletions(-) diff --git a/api/admin/health/health.go b/api/admin/health/health.go index 6db522976..373bac476 100644 --- a/api/admin/health/health.go +++ b/api/admin/health/health.go @@ -34,8 +34,8 @@ type Health struct { } const ( - defaultMaxTimeBetweenSlots = time.Duration(2*thor.BlockInterval) * time.Second - defaultMinPeerCount = 2 + defaultBlockTolerance = time.Duration(2*thor.BlockInterval) * time.Second // 2 blocks tolerance + defaultMinPeerCount = 2 ) func New(repo *chain.Repository, p2p *comm.Communicator) *Health { @@ -46,8 +46,8 @@ func New(repo *chain.Repository, p2p *comm.Communicator) *Health { } // isNetworkProgressing checks if the network is producing new blocks within the allowed interval. -func (h *Health) isNetworkProgressing(now time.Time, bestBlockTimestamp time.Time, maxTimeBetweenSlots time.Duration) bool { - return now.Sub(bestBlockTimestamp) <= maxTimeBetweenSlots +func (h *Health) isNetworkProgressing(now time.Time, bestBlockTimestamp time.Time, blockTolerance time.Duration) bool { + return now.Sub(bestBlockTimestamp) <= blockTolerance } // hasNodeBootstrapped checks if the node has bootstrapped by comparing the block interval. @@ -70,7 +70,7 @@ func (h *Health) isNodeConnectedP2P(peerCount int, minPeerCount int) bool { return peerCount >= minPeerCount } -func (h *Health) Status(maxTimeBetweenSlots time.Duration, minPeerCount int) (*Status, error) { +func (h *Health) Status(blockTolerance time.Duration, minPeerCount int) (*Status, error) { h.lock.RLock() defer h.lock.RUnlock() @@ -90,7 +90,7 @@ func (h *Health) Status(maxTimeBetweenSlots time.Duration, minPeerCount int) (*S now := time.Now() // Perform the checks - networkProgressing := h.isNetworkProgressing(now, bestBlockTimestamp, maxTimeBetweenSlots) + networkProgressing := h.isNetworkProgressing(now, bestBlockTimestamp, blockTolerance) nodeBootstrapped := h.hasNodeBootstrapped(now, bestBlockTimestamp) nodeConnected := h.isNodeConnectedP2P(connectedPeerCount, minPeerCount) diff --git a/api/admin/health/health_api.go b/api/admin/health/health_api.go index 9363f82d0..3bad13f07 100644 --- a/api/admin/health/health_api.go +++ b/api/admin/health/health_api.go @@ -29,13 +29,13 @@ func (h *API) handleGetHealth(w http.ResponseWriter, r *http.Request) error { query := r.URL.Query() // Default to constants if query parameters are not provided - maxTimeBetweenSlots := defaultMaxTimeBetweenSlots + blockTolerance := defaultBlockTolerance minPeerCount := defaultMinPeerCount // Override with query parameters if they exist - if queryMaxTimeBetweenSlots := query.Get("maxTimeBetweenSlots"); queryMaxTimeBetweenSlots != "" { - if parsed, err := time.ParseDuration(queryMaxTimeBetweenSlots); err == nil { - maxTimeBetweenSlots = parsed + if queryBlockTolerance := query.Get("blockTolerance"); queryBlockTolerance != "" { + if parsed, err := time.ParseDuration(queryBlockTolerance); err == nil { + blockTolerance = parsed } } @@ -45,7 +45,7 @@ func (h *API) handleGetHealth(w http.ResponseWriter, r *http.Request) error { } } - acc, err := h.healthStatus.Status(maxTimeBetweenSlots, minPeerCount) + acc, err := h.healthStatus.Status(blockTolerance, minPeerCount) if err != nil { return err } diff --git a/api/admin/health/health_test.go b/api/admin/health/health_test.go index 034c4e64e..35d73fe2e 100644 --- a/api/admin/health/health_test.go +++ b/api/admin/health/health_test.go @@ -36,7 +36,7 @@ func TestHealth_isNetworkProgressing(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - isProgressing := h.isNetworkProgressing(now, tt.bestBlockTimestamp, defaultMaxTimeBetweenSlots) + isProgressing := h.isNetworkProgressing(now, tt.bestBlockTimestamp, defaultBlockTolerance) assert.Equal(t, tt.expectedProgressing, isProgressing, "isNetworkProgressing result mismatch") }) } @@ -54,17 +54,17 @@ func TestHealth_hasNodeBootstrapped(t *testing.T) { // keep the order as it matters for health state { name: "Not Bootstrapped - block timestamp outside interval", - bestBlockTimestamp: now.Add(-defaultMaxTimeBetweenSlots + 1), + bestBlockTimestamp: now.Add(-defaultBlockTolerance + 1), expectedBootstrap: false, }, { name: "Bootstrapped - block timestamp within interval", - bestBlockTimestamp: now.Add(defaultMaxTimeBetweenSlots), + bestBlockTimestamp: now.Add(defaultBlockTolerance), expectedBootstrap: true, }, { name: "Bootstrapped only once", - bestBlockTimestamp: now.Add(-defaultMaxTimeBetweenSlots + 1), + bestBlockTimestamp: now.Add(-defaultBlockTolerance + 1), expectedBootstrap: true, }, } diff --git a/api/admin/loglevel/log_level_test.go b/api/admin/loglevel/log_level_test.go index d04320ce3..3d1a8a960 100644 --- a/api/admin/loglevel/log_level_test.go +++ b/api/admin/loglevel/log_level_test.go @@ -15,7 +15,6 @@ import ( "testing" "github.com/gorilla/mux" - "github.com/stretchr/testify/assert" )