Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Dec 6, 2024
1 parent 34a8adc commit 93a222a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 68 deletions.
44 changes: 11 additions & 33 deletions api/admin/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package health

import (
"sync"
"time"

"github.com/vechain/thor/v2/chain"
Expand All @@ -20,17 +19,15 @@ type BlockIngestion struct {
}

type Status struct {
Healthy bool `json:"healthy"`
BestBlockTimestamp *time.Time `json:"bestBlockTimestamp"`
WasChainSynced bool `json:"wasChainSynced"`
PeerCount int `json:"peerCount"`
Healthy bool `json:"healthy"`
BestBlockTime *time.Time `json:"bestBlockTime"`
PeerCount int `json:"peerCount"`
IsNetworkProgressing bool `json:"isNetworkProgressing"`
}

type Health struct {
lock sync.RWMutex
repo *chain.Repository
p2p *comm.Communicator
isNodeBootstrapped bool
repo *chain.Repository
p2p *comm.Communicator
}

const (
Expand All @@ -50,30 +47,12 @@ func (h *Health) isNetworkProgressing(now time.Time, bestBlockTimestamp time.Tim
return now.Sub(bestBlockTimestamp) <= blockTolerance
}

// hasNodeBootstrapped checks if the node has bootstrapped by comparing the block interval.
// Once it's marked as done, it never reverts.
func (h *Health) hasNodeBootstrapped(now time.Time, bestBlockTimestamp time.Time) bool {
if h.isNodeBootstrapped {
return true
}

blockInterval := time.Duration(thor.BlockInterval) * time.Second
if bestBlockTimestamp.Add(blockInterval).After(now) {
h.isNodeBootstrapped = true
}

return h.isNodeBootstrapped
}

// isNodeConnectedP2P checks if the node is connected to peers
func (h *Health) isNodeConnectedP2P(peerCount int, minPeerCount int) bool {
return peerCount >= minPeerCount
}

func (h *Health) Status(blockTolerance time.Duration, minPeerCount int) (*Status, error) {
h.lock.RLock()
defer h.lock.RUnlock()

// Fetch the best block details
bestBlock := h.repo.BestBlockSummary()
bestBlockTimestamp := time.Unix(int64(bestBlock.Header.Timestamp()), 0)
Expand All @@ -90,17 +69,16 @@ func (h *Health) Status(blockTolerance time.Duration, minPeerCount int) (*Status

// Perform the checks
networkProgressing := h.isNetworkProgressing(now, bestBlockTimestamp, blockTolerance)
wasChainSynced := h.hasNodeBootstrapped(now, bestBlockTimestamp)
nodeConnected := h.isNodeConnectedP2P(connectedPeerCount, minPeerCount)

// Calculate overall health status
healthy := networkProgressing && wasChainSynced && nodeConnected
healthy := networkProgressing && nodeConnected

// Return the current status
return &Status{
Healthy: healthy,
BestBlockTimestamp: &bestBlockTimestamp,
WasChainSynced: wasChainSynced,
PeerCount: connectedPeerCount,
Healthy: healthy,
BestBlockTime: &bestBlockTimestamp,
IsNetworkProgressing: networkProgressing,
PeerCount: connectedPeerCount,
}, nil
}
35 changes: 0 additions & 35 deletions api/admin/health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,6 @@ func TestHealth_isNetworkProgressing(t *testing.T) {
}
}

func TestHealth_hasNodeBootstrapped(t *testing.T) {
h := &Health{}
now := time.Now()

tests := []struct {
name string
bestBlockTimestamp time.Time
expectedBootstrap bool
}{
// keep the order as it matters for health state
{
name: "Not Bootstrapped - block timestamp outside interval",
bestBlockTimestamp: now.Add(-defaultBlockTolerance + 1),
expectedBootstrap: false,
},
{
name: "Bootstrapped - block timestamp within interval",
bestBlockTimestamp: now.Add(defaultBlockTolerance),
expectedBootstrap: true,
},
{
name: "Bootstrapped only once",
bestBlockTimestamp: now.Add(-defaultBlockTolerance + 1),
expectedBootstrap: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isBootstrapped := h.hasNodeBootstrapped(now, tt.bestBlockTimestamp)
assert.Equal(t, tt.expectedBootstrap, isBootstrapped, "hasNodeBootstrapped result mismatch")
})
}
}

func TestHealth_isNodeConnectedP2P(t *testing.T) {
h := &Health{}

Expand Down

0 comments on commit 93a222a

Please sign in to comment.