From dba46a5a775a9c429f0849e6e4408dd4aa97f936 Mon Sep 17 00:00:00 2001
From: benjamin <ben@elixxir.io>
Date: Tue, 11 Oct 2022 14:39:29 -0700
Subject: [PATCH] made edits to how the clock skew clap is implemented

---
 cmix/client.go                     |  2 +-
 cmix/clockSkew/timeTracker.go      | 11 +++++++++--
 cmix/clockSkew/timeTracker_test.go |  2 +-
 cmix/follow.go                     |  6 +-----
 4 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/cmix/client.go b/cmix/client.go
index b83c8425f..406fe0181 100644
--- a/cmix/client.go
+++ b/cmix/client.go
@@ -113,7 +113,7 @@ func NewClient(params Params, comms *commClient.Comms, session storage.Session,
 		rng:           rng,
 		comms:         comms,
 		maxMsgLen:     tmpMsg.ContentsSize(),
-		skewTracker:   clockSkew.New(),
+		skewTracker:   clockSkew.New(params.ClockSkewClamp),
 	}
 
 	if params.VerboseRoundTracking {
diff --git a/cmix/clockSkew/timeTracker.go b/cmix/clockSkew/timeTracker.go
index 1d9b729a4..9e4c54fff 100644
--- a/cmix/clockSkew/timeTracker.go
+++ b/cmix/clockSkew/timeTracker.go
@@ -66,10 +66,11 @@ type timeOffsetTracker struct {
 	lock         sync.RWMutex
 	offsets      []*time.Duration
 	currentIndex int
+	clamp        time.Duration
 }
 
 // New returns an implementation of Tracker.
-func New() Tracker {
+func New(clamp time.Duration) Tracker {
 	t := &timeOffsetTracker{
 		gatewayClockDelays: new(sync.Map),
 		offsets:            make([]*time.Duration, maxHistogramSize),
@@ -108,7 +109,13 @@ func (t *timeOffsetTracker) Aggregate() time.Duration {
 	t.lock.RLock()
 	defer t.lock.RUnlock()
 
-	return average(t.offsets)
+	avg := average(t.offsets)
+	if avg < (-t.clamp) || avg > t.clamp {
+		return avg
+	} else {
+		return 0
+	}
+
 }
 
 func average(durations []*time.Duration) time.Duration {
diff --git a/cmix/clockSkew/timeTracker_test.go b/cmix/clockSkew/timeTracker_test.go
index b21be2bb3..408727215 100644
--- a/cmix/clockSkew/timeTracker_test.go
+++ b/cmix/clockSkew/timeTracker_test.go
@@ -20,7 +20,7 @@ import (
 )
 
 func TestTimeTrackerSmokeTest(t *testing.T) {
-	tracker := New()
+	tracker := New(0)
 	gwID := &id.ID{}
 	_, err := rand.Read(gwID[:])
 	require.NoError(t, err)
diff --git a/cmix/follow.go b/cmix/follow.go
index 84bb62b72..e03b55160 100644
--- a/cmix/follow.go
+++ b/cmix/follow.go
@@ -127,11 +127,7 @@ func (c *client) followNetwork(report ClientErrorReport,
 
 			//update clock skew
 			estimatedSkew := c.skewTracker.Aggregate()
-			if estimatedSkew < (-c.param.ClockSkewClamp) || estimatedSkew > c.param.ClockSkewClamp {
-				netTime.SetOffset(estimatedSkew)
-			} else {
-				netTime.SetOffset(0)
-			}
+			netTime.SetOffset(estimatedSkew)
 
 			if err != nil {
 				jww.ERROR.Printf("failed to operate on identities to "+
-- 
GitLab