Skip to content
This repository has been archived by the owner on Jan 2, 2025. It is now read-only.

Add check on latest_block_time #51

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions td2/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (cc *ChainConfig) newRpc() error {
l(msg)
return
}
if status.SyncInfo.CatchingUp {
if status.SyncInfo.CatchingUp || !isLatestBlockTimeCurrent(status.SyncInfo.LatestBlockTime, cc.Alerts.Stalled) {
msg = fmt.Sprint("🐢 node is not synced, skipping ", u)
syncing = true
down = true
Expand Down Expand Up @@ -162,7 +162,7 @@ func (cc *ChainConfig) monitorHealth(ctx context.Context, chainName string) {
alert("on the wrong network")
return
}
if status.SyncInfo.CatchingUp {
if status.SyncInfo.CatchingUp || !isLatestBlockTimeCurrent(status.SyncInfo.LatestBlockTime, cc.Alerts.Stalled) {
alert("not synced")
node.syncing = true
return
Expand All @@ -181,7 +181,6 @@ func (cc *ChainConfig) monitorHealth(ctx context.Context, chainName string) {
l(fmt.Sprintf("🟢 %-12s node %s is healthy", chainName, node.Url))
}(node)
}

if cc.client == nil {
e := cc.newRpc()
if e != nil {
Expand Down Expand Up @@ -238,3 +237,8 @@ func guessPublicEndpoint(u string) string {
}
return proto + matches[1] + port
}

// isLatestBlockTimeCurrent checks to see if the `latest_block_time` is within stalledMinutes minutes of UTC time.
func isLatestBlockTimeCurrent(blockTime time.Time, stalledMinutes int) bool {
return time.Now().UTC().Sub(blockTime) < time.Minute*time.Duration(stalledMinutes)
}
27 changes: 27 additions & 0 deletions td2/tenderduty_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tenderduty

import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)

func TestIsLatestBlockTimeCurrent(t *testing.T) {
currentTime := time.Now().UTC()

// Check current time
blockTime := currentTime
assert.True(t, isLatestBlockTimeCurrent(blockTime, 10))

// Check time from two minutes ago
blockTime = currentTime.Add(-time.Minute * 2)
assert.True(t, isLatestBlockTimeCurrent(blockTime, 10))

// Check time from two hours ago
blockTime = currentTime.Add(-time.Hour * 2)
assert.False(t, isLatestBlockTimeCurrent(blockTime, 10))

// Check time two minutes in the future
blockTime = currentTime.Add(time.Minute * 2)
assert.True(t, isLatestBlockTimeCurrent(blockTime, 10))
}