Skip to content

Commit

Permalink
fix(util): add more ntp pool (#1328)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ja7ad authored Jun 9, 2024
1 parent 19cc2c5 commit bc8e5af
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 45 deletions.
32 changes: 16 additions & 16 deletions cmd/gtk/assets/ui/widget_node.ui
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Working directory:</property>
<property name="label" translatable="yes">📂 Working directory:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -100,7 +100,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Last block height:</property>
<property name="label" translatable="yes">🥡 Last block height:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -129,7 +129,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Last block time:</property>
<property name="label" translatable="yes">🕔 Last block time:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -158,7 +158,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Number of blocks left:</property>
<property name="label" translatable="yes">📦 Number of blocks left:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -187,7 +187,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Syncing Progress:</property>
<property name="label" translatable="yes">🔄 Syncing Progress:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -215,7 +215,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Network:</property>
<property name="label" translatable="yes">🌐 Network:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -245,7 +245,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Network ID:</property>
<property name="label" translatable="yes">🧾 Network ID:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -275,7 +275,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Clock offset:</property>
<property name="label" translatable="yes">Clock offset:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -304,7 +304,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Connections</property>
<property name="label" translatable="yes">📡 Connections</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -334,7 +334,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Moniker:</property>
<property name="label" translatable="yes">🔰 Moniker:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -364,7 +364,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Reachability:</property>
<property name="label" translatable="yes">🔍 Reachability:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -442,7 +442,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Committee size:</property>
<property name="label" translatable="yes">👥 Committee size:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand All @@ -457,7 +457,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">In committee now:</property>
<property name="label" translatable="yes">💎 In committee now:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand All @@ -472,7 +472,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Committee power:</property>
<property name="label" translatable="yes">⚡️ Committee power:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down Expand Up @@ -547,7 +547,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Total power:</property>
<property name="label" translatable="yes">Total power:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand All @@ -562,7 +562,7 @@
<property name="valign">start</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="label" translatable="yes">Number of validators:</property>
<property name="label" translatable="yes">Number of validators:</property>
</object>
<packing>
<property name="left-attach">0</property>
Expand Down
11 changes: 10 additions & 1 deletion cmd/gtk/widget_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package main
import (
_ "embed"
"fmt"
"math"
"os"
"strconv"
"time"
Expand Down Expand Up @@ -141,11 +142,14 @@ func (wn *widgetNode) timeout10() bool {
styleContext, err := wn.labelClockOffset.GetStyleContext()
fatalErrorCheck(err)

wn.labelClockOffset.SetTooltipText("Difference between time of your machine and " +
"network time( (NTP) for synchronization.")

if offsetErr != nil {
styleContext.AddClass("warning")
wn.labelClockOffset.SetText("Error response from NTP server.")
} else {
wn.labelClockOffset.SetText(offset.String())
wn.labelClockOffset.SetText(fmt.Sprintf("%v second(s)", math.Round(offset.Seconds())))

if wn.model.node.Sync().OutOfSync(offset) {
styleContext.AddClass("warning")
Expand All @@ -159,6 +163,11 @@ func (wn *widgetNode) timeout10() bool {
wn.labelCommitteeStake.SetText(amount.Amount(committeePower).String())
wn.labelTotalStake.SetText(amount.Amount(totalPower).String())
wn.labelInCommittee.SetText(isInCommittee)
if isInCommittee == "Yes" {
wn.labelInCommittee.SetMarkup(fmt.Sprintf("<span foreground=\"#10c92f\">%s</span>", isInCommittee))
} else {
wn.labelInCommittee.SetText(isInCommittee)
}
wn.labelNumConnections.SetText(numConnections)
wn.labelReachability.SetText(reachability)

Expand Down
66 changes: 39 additions & 27 deletions util/ntp/ntp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ const (
maxClockOffset = time.Duration(math.MinInt64)
)

var _pools = []string{
"pool.ntp.org",
"time.google.com",
"time.cloudflare.com",
"time.apple.com",
"time.windows.com",
"ntp.ubuntu.com",
}

type Checker struct {
lock sync.RWMutex
ctx context.Context
Expand All @@ -39,25 +48,7 @@ func NewNtpChecker(interval, threshold time.Duration) *Checker {
return server
}

func (*Checker) clockOffset() time.Duration {
server := "pool.ntp.org"
response, err := ntp.Query(server)
if err != nil {
logger.Error("ntp error", "server", server, "error", err)

return maxClockOffset
}

if err := response.Validate(); err != nil {
logger.Error("ntp error", "server", server, "error", err)

return maxClockOffset
}

return response.ClockOffset
}

func (c *Checker) checkClockOffset() {
func (c *Checker) Start() {
for {
select {
case <-c.ctx.Done():
Expand All @@ -76,25 +67,23 @@ func (c *Checker) checkClockOffset() {
"The node is out of sync with the network time",
"threshold", c.threshold,
"offset", offset,
"threshold(secs)", c.threshold.Seconds(),
"offset(secs)", offset.Seconds(),
)
}
}
}
}

func (c *Checker) OutOfSync(offset time.Duration) bool {
return math.Abs(float64(offset)) > float64(c.threshold)
}

func (c *Checker) Start() {
go c.checkClockOffset()
}

func (c *Checker) Stop() {
c.cancel()
c.ticker.Stop()
}

func (c *Checker) OutOfSync(offset time.Duration) bool {
return math.Abs(float64(offset)) > float64(c.threshold)
}

func (c *Checker) GetClockOffset() (time.Duration, error) {
c.lock.RLock()
defer c.lock.RUnlock()
Expand All @@ -107,3 +96,26 @@ func (c *Checker) GetClockOffset() (time.Duration, error) {

return offset, nil
}

func (*Checker) clockOffset() time.Duration {
for _, server := range _pools {
response, err := ntp.Query(server)
if err != nil {
logger.Warn("ntp error", "server", server, "error", err)

continue
}

if err := response.Validate(); err != nil {
logger.Warn("ntp validate error", "server", server, "error", err)

continue
}

return response.ClockOffset
}

logger.Error("failed to get ntp query from all pool, set default max clock offset")

return maxClockOffset
}
63 changes: 63 additions & 0 deletions util/ntp/ntp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package ntp

import (
"sync"
"testing"
"time"

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

func TestNewNtpChecker(t *testing.T) {
checker := NewNtpChecker(1*time.Second, 500*time.Millisecond)
assert.NotNil(t, checker)
assert.Equal(t, 1*time.Second, checker.interval)
assert.Equal(t, 500*time.Millisecond, checker.threshold)
}

func TestOutOfSync(t *testing.T) {
checker := NewNtpChecker(1*time.Second, 500*time.Millisecond)
assert.False(t, checker.OutOfSync(300*time.Millisecond))
assert.True(t, checker.OutOfSync(600*time.Millisecond))
}

func TestGetClockOffset(t *testing.T) {
checker := NewNtpChecker(1*time.Second, 500*time.Millisecond)
checker.lock.Lock()
checker.offset = 300 * time.Millisecond
checker.lock.Unlock()

offset, err := checker.GetClockOffset()
assert.NoError(t, err)
assert.Equal(t, 300*time.Millisecond, offset)

checker.lock.Lock()
checker.offset = maxClockOffset
checker.lock.Unlock()

offset, err = checker.GetClockOffset()
assert.Error(t, err)
assert.Equal(t, 0*time.Millisecond, offset)
}

func TestStartAndStop(t *testing.T) {
checker := NewNtpChecker(1*time.Second, 500*time.Millisecond)
assert.NotNil(t, checker)

// Using a WaitGroup to wait for the goroutine to start
var wg sync.WaitGroup
wg.Add(1)

go func() {
wg.Done()
checker.Start()
}()

// Wait for the goroutine to start
wg.Wait()

// Let the checker run for a short period
time.Sleep(2 * time.Second)

checker.Stop()
}
3 changes: 2 additions & 1 deletion www/grpc/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package grpc

import (
"context"
"math"

"github.com/fxamacker/cbor/v2"
"github.com/pactus-project/pactus/sync/peerset/peer"
Expand Down Expand Up @@ -48,7 +49,7 @@ func (s *networkServer) GetNodeInfo(_ context.Context,
Protocols: s.net.Protocols(),
Services: services,
ServicesNames: servicesNames,
ClockOffset: clockOffset.Seconds(),
ClockOffset: math.Round(clockOffset.Seconds()),
ConnectionInfo: &pactus.ConnectionInfo{
Connections: uint64(s.net.NumConnectedPeers()),
InboundConnections: uint64(s.net.NumInbound()),
Expand Down

0 comments on commit bc8e5af

Please sign in to comment.