Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Nov 21, 2024
1 parent f1b0000 commit c0aee23
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 16 deletions.
12 changes: 6 additions & 6 deletions api/admin/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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()

Expand All @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions api/admin/health/health_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions api/admin/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}
Expand All @@ -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,
},
}
Expand Down
1 change: 0 additions & 1 deletion api/admin/loglevel/log_level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"testing"

"github.com/gorilla/mux"

"github.com/stretchr/testify/assert"
)

Expand Down

0 comments on commit c0aee23

Please sign in to comment.