From 4c36bb47fa9622a0348b81476b53cb4229d6b5de Mon Sep 17 00:00:00 2001
From: josh <josh@elixxir.io>
Date: Fri, 12 Feb 2021 11:44:14 -0800
Subject: [PATCH] Implement and test GetOldestRoundID

---
 network/dataStructures/roundData.go |  5 ++++
 network/instance.go                 |  5 ++++
 network/instance_test.go            | 43 +++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+)

diff --git a/network/dataStructures/roundData.go b/network/dataStructures/roundData.go
index 986ab960..2c98af73 100644
--- a/network/dataStructures/roundData.go
+++ b/network/dataStructures/roundData.go
@@ -57,3 +57,8 @@ func (d *Data) GetRound(id int) (*mixmessages.RoundInfo, error) {
 func (d *Data) GetLastRoundID() id.Round {
 	return id.Round(d.rounds.GetNewestId())
 }
+
+// Gets the ID of the oldest roundd in the buffer
+func (d *Data) GetOldestRoundID() id.Round  {
+	return id.Round(d.rounds.GetOldestId())
+}
diff --git a/network/instance.go b/network/instance.go
index ae67a606..2891bed0 100644
--- a/network/instance.go
+++ b/network/instance.go
@@ -519,6 +519,11 @@ func (i *Instance) GetLastRoundID() id.Round {
 	return i.roundData.GetLastRoundID() - 1
 }
 
+// Get the oldest round id
+func (i *Instance) GetOldestRoundID() id.Round  {
+	return i.roundData.GetOldestRoundID()
+}
+
 // Update gateway hosts based on most complete ndf
 func (i *Instance) UpdateGatewayConnections() error {
 	if i.full != nil {
diff --git a/network/instance_test.go b/network/instance_test.go
index 80f5563a..a5c9d5fa 100644
--- a/network/instance_test.go
+++ b/network/instance_test.go
@@ -316,6 +316,49 @@ func TestInstance_GetLastUpdateID(t *testing.T) {
 	i.GetLastUpdateID()
 }
 
+func TestInstance_GetOldestRoundID(t *testing.T) {
+	i := Instance{
+		roundData: ds.NewData(),
+	}
+
+	expectedOldRound := id.Round(0)
+	_ = i.roundData.UpsertRound(&mixmessages.RoundInfo{ID: uint64(expectedOldRound)})
+	_ = i.roundData.UpsertRound(&mixmessages.RoundInfo{ID:uint64(2)})
+
+
+	returned := i.GetOldestRoundID()
+	if returned != expectedOldRound {
+		t.Errorf("Failed to get oldest round from buffer." +
+			"\n\tExpected: %v" +
+			"\n\tReceived: %v", expectedOldRound, returned)
+	}
+}
+
+// Test which forces a full buffer, causing overwriting of old rounds
+func TestInstance_GetOldestRoundID_ManyRounds(t *testing.T) {
+	testInstance := Instance{
+		roundData: ds.NewData(),
+	}
+
+	// Ensure a circle back in the round buffer
+	for i := 1; i <= ds.RoundInfoBufLen ; i++ {
+		_ = testInstance.roundData.UpsertRound(&mixmessages.RoundInfo{ID: uint64(i)})
+
+	}
+
+	// This will have oldest round as 0, until we reach RoundInfoBufLen, then
+	// round 0 will be overwritten by the newest round,
+	// moving the oldest round to round 1
+	expected := id.Round(1)
+	returned := testInstance.GetOldestRoundID()
+	if returned !=  expected {
+		t.Errorf("Failed to get oldest round from buffer." +
+			"\n\tExpected: %v" +
+			"\n\tReceived: %v", 1, returned)
+	}
+}
+
+
 func TestInstance_UpdateGatewayConnections(t *testing.T) {
 	secured, _ := NewSecuredNdf(testutils.NDF)
 	testManager := connect.NewManagerTesting(t)
-- 
GitLab