From 1a8e02eb41297707badd6b7ea23e94a572740f54 Mon Sep 17 00:00:00 2001
From: jbhusson <jonah@elixxir.io>
Date: Fri, 6 Mar 2020 16:48:10 -0800
Subject: [PATCH] Add a New func to datastructures

---
 go.mod                                      | 10 ++++++--
 go.sum                                      | 15 ++++++------
 network/dataStructures/roundData.go         | 26 +++++++++++----------
 network/dataStructures/roundData_test.go    |  6 ++---
 network/dataStructures/roundUpdates.go      | 25 ++++++++++----------
 network/dataStructures/roundUpdates_test.go |  6 ++---
 network/instance.go                         |  4 ++--
 network/instance_test.go                    | 10 ++++----
 network/securedNdf.go                       |  2 --
 network/securedNdf_test.go                  |  2 ++
 10 files changed, 57 insertions(+), 49 deletions(-)

diff --git a/go.mod b/go.mod
index 4905dff9..b7397e1e 100644
--- a/go.mod
+++ b/go.mod
@@ -5,14 +5,20 @@ go 1.13
 require (
 	github.com/golang/protobuf v1.3.4
 	github.com/google/go-cmp v0.4.0 // indirect
+	github.com/kr/text v0.2.0 // indirect
+	github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
 	github.com/pkg/errors v0.9.1
 	github.com/spf13/jwalterweatherman v1.1.0
-	gitlab.com/elixxir/crypto v0.0.0-20200229000841-b1ee7117a1d0
-	gitlab.com/elixxir/primitives v0.0.0-20200306214728-35300c4b5152
+	github.com/stretchr/testify v1.5.1 // indirect
+	gitlab.com/elixxir/crypto v0.0.0-20200206203107-b8926242da23
+	gitlab.com/elixxir/primitives v0.0.0-20200226191544-59ff3303abb3
+	golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 // indirect
 	golang.org/x/net v0.0.0-20200301022130-244492dfa37a
+	golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 // indirect
 	golang.org/x/text v0.3.2 // indirect
 	google.golang.org/genproto v0.0.0-20200306153348-d950eab6f860 // indirect
 	google.golang.org/grpc v1.27.1
+	gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
 )
 
 replace google.golang.org/grpc => github.com/grpc/grpc-go v1.27.1
diff --git a/go.sum b/go.sum
index 2b28d624..81f8f485 100644
--- a/go.sum
+++ b/go.sum
@@ -48,16 +48,15 @@ github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJy
 github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
 github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
-gitlab.com/elixxir/crypto v0.0.0-20200229000841-b1ee7117a1d0 h1:2oq/Y1pA3bwCdlRjhFjoOtb2somDY+wYz2vuiOQkVVA=
-gitlab.com/elixxir/crypto v0.0.0-20200229000841-b1ee7117a1d0/go.mod h1:QPClJr3F90ejz6iHaCZuhexytd6PP97dDnt93iRCTDo=
-gitlab.com/elixxir/primitives v0.0.0-20200218211222-4193179f359c/go.mod h1:REJMcwIcyxh74VSHqy4S9yYiaEsQYObOPglRExDpk14=
-gitlab.com/elixxir/primitives v0.0.0-20200306214728-35300c4b5152 h1:RqT4XSphs+73DWvdKaWlTI8qY6eUdkG9LfTli+qukjU=
-gitlab.com/elixxir/primitives v0.0.0-20200306214728-35300c4b5152/go.mod h1:vx+QS3daQGu4WrHMU6IOuddbpJQnEOhbDyhYdHm/nNQ=
+gitlab.com/elixxir/crypto v0.0.0-20200206203107-b8926242da23 h1:J9MKdOxLGzDZoLy2Q0CAxPlPjSH+k4NG3JhgvatAZjo=
+gitlab.com/elixxir/crypto v0.0.0-20200206203107-b8926242da23/go.mod h1:wWulHuSqxiGhvasduZrtyTTqy+7y5ebe440GdORhzig=
+gitlab.com/elixxir/primitives v0.0.0-20200131183153-e93c6b75019f/go.mod h1:REJMcwIcyxh74VSHqy4S9yYiaEsQYObOPglRExDpk14=
+gitlab.com/elixxir/primitives v0.0.0-20200226191544-59ff3303abb3 h1:572R0Ap/rYmC6XNNQeoZfvfqXyuiRENidgUzdpCbFHw=
+gitlab.com/elixxir/primitives v0.0.0-20200226191544-59ff3303abb3/go.mod h1:REJMcwIcyxh74VSHqy4S9yYiaEsQYObOPglRExDpk14=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg=
 golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
-golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw=
-golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
+golang.org/x/crypto v0.0.0-20200204104054-c9f3fb736b72/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM=
 golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
 golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -78,7 +77,7 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20200122134326-e047566fdf82 h1:ywK/j/KkyTHcdyYSZNXGjMwgmDSfjglYZ3vStQ/gSCU=
 golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
-golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
 golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
diff --git a/network/dataStructures/roundData.go b/network/dataStructures/roundData.go
index 2ab96084..116d0876 100644
--- a/network/dataStructures/roundData.go
+++ b/network/dataStructures/roundData.go
@@ -20,6 +20,20 @@ type Data struct {
 	rounds *ring.Buff
 }
 
+func NewData() *Data {
+	// We want data using the round ID as its primary
+	idFunc := func(val interface{}) int {
+		if val == nil {
+			return -1
+		}
+		return int(val.(*mixmessages.RoundInfo).ID)
+	}
+
+	return &Data{
+		rounds: ring.NewBuff(RoundInfoBufLen, idFunc),
+	}
+}
+
 // Upsert a round into the ring bugger
 func (d *Data) UpsertRound(r *mixmessages.RoundInfo) error {
 	// comparison here should ensure that either the current round is nil or has a lower update id than the new round
@@ -33,18 +47,6 @@ func (d *Data) UpsertRound(r *mixmessages.RoundInfo) error {
 		return false
 	}
 
-	// We want data using the round ID as its primary
-	id := func(val interface{}) int {
-		if val == nil {
-			return -1
-		}
-		return int(val.(*mixmessages.RoundInfo).ID)
-	}
-
-	if d.rounds == nil {
-		d.rounds = ring.NewBuff(RoundInfoBufLen, id)
-	}
-
 	//find the round location
 	//check the new state is newer then the current
 	//replace the round info object
diff --git a/network/dataStructures/roundData_test.go b/network/dataStructures/roundData_test.go
index 50afa1f5..3a47e0ab 100644
--- a/network/dataStructures/roundData_test.go
+++ b/network/dataStructures/roundData_test.go
@@ -12,7 +12,7 @@ import (
 )
 
 func TestData_UpsertRound(t *testing.T) {
-	d := Data{}
+	d := NewData()
 	err := d.UpsertRound(&mixmessages.RoundInfo{
 		ID:       0,
 		UpdateID: 0,
@@ -23,7 +23,7 @@ func TestData_UpsertRound(t *testing.T) {
 }
 
 func TestData_GetRound(t *testing.T) {
-	d := Data{}
+	d := NewData()
 	_ = d.UpsertRound(&mixmessages.RoundInfo{
 		ID:       0,
 		UpdateID: 3,
@@ -35,7 +35,7 @@ func TestData_GetRound(t *testing.T) {
 }
 
 func TestData_ComparisonFunc(t *testing.T) {
-	d := Data{}
+	d := NewData()
 	_ = d.UpsertRound(&mixmessages.RoundInfo{
 		ID:       2,
 		UpdateID: 3,
diff --git a/network/dataStructures/roundUpdates.go b/network/dataStructures/roundUpdates.go
index c0557f5d..689b83e3 100644
--- a/network/dataStructures/roundUpdates.go
+++ b/network/dataStructures/roundUpdates.go
@@ -22,6 +22,19 @@ type Updates struct {
 	updates *ring.Buff
 }
 
+func NewUpdates() *Updates {
+	// we want each updateId stored in this structure
+	idFunc := func(val interface{}) int {
+		if val == nil {
+			return -1
+		}
+		return int(val.(*pb.RoundInfo).UpdateID)
+	}
+	return &Updates{
+		updates: ring.NewBuff(RoundUpdatesBufLen, idFunc),
+	}
+}
+
 // Add a round to the ring buffer
 func (u *Updates) AddRound(info *pb.RoundInfo) error {
 
@@ -33,18 +46,6 @@ func (u *Updates) AddRound(info *pb.RoundInfo) error {
 		return false
 	}
 
-	// we want each updateId stored in this structure
-	id := func(val interface{}) int {
-		if val == nil {
-			return -1
-		}
-		return int(val.(*pb.RoundInfo).UpdateID)
-	}
-
-	if u.updates == nil {
-		u.updates = ring.NewBuff(RoundUpdatesBufLen, id)
-	}
-
 	return u.updates.UpsertById(info, comp)
 }
 
diff --git a/network/dataStructures/roundUpdates_test.go b/network/dataStructures/roundUpdates_test.go
index 4239fd91..494903c4 100644
--- a/network/dataStructures/roundUpdates_test.go
+++ b/network/dataStructures/roundUpdates_test.go
@@ -12,7 +12,7 @@ import (
 )
 
 func TestUpdates_AddRound(t *testing.T) {
-	u := Updates{}
+	u := NewUpdates()
 	err := u.AddRound(&mixmessages.RoundInfo{
 		ID:       0,
 		UpdateID: 0,
@@ -23,7 +23,7 @@ func TestUpdates_AddRound(t *testing.T) {
 }
 
 func TestUpdates_GetUpdate(t *testing.T) {
-	u := Updates{}
+	u := NewUpdates()
 	_ = u.AddRound(&mixmessages.RoundInfo{
 		ID:       0,
 		UpdateID: 3,
@@ -35,7 +35,7 @@ func TestUpdates_GetUpdate(t *testing.T) {
 }
 
 func TestUpdates_GetUpdates(t *testing.T) {
-	u := Updates{}
+	u := NewUpdates()
 	_ = u.AddRound(&mixmessages.RoundInfo{
 		ID:       0,
 		UpdateID: 3,
diff --git a/network/instance.go b/network/instance.go
index 27d297e9..65fd24e0 100644
--- a/network/instance.go
+++ b/network/instance.go
@@ -72,8 +72,8 @@ func NewInstance(c *connect.ProtoComms, partial, full *ndf.NetworkDefinition) (*
 		comm:         c,
 		partial:      partialNdf,
 		full:         fullNdf,
-		roundUpdates: &ds.Updates{},
-		roundData:    &ds.Data{},
+		roundUpdates: ds.NewUpdates(),
+		roundData:    ds.NewData(),
 		cmixGroup:    ds.NewGroup(),
 		e2eGroup:     ds.NewGroup(),
 	}, nil
diff --git a/network/instance_test.go b/network/instance_test.go
index f03f5623..17dfbdf9 100644
--- a/network/instance_test.go
+++ b/network/instance_test.go
@@ -67,7 +67,7 @@ func TestInstance_GetPartialNdf(t *testing.T) {
 
 func TestInstance_GetRound(t *testing.T) {
 	i := Instance{
-		roundData: &ds.Data{},
+		roundData: ds.NewData(),
 	}
 	_ = i.roundData.UpsertRound(&mixmessages.RoundInfo{ID: uint64(1)})
 	r, err := i.GetRound(id.Round(1))
@@ -78,7 +78,7 @@ func TestInstance_GetRound(t *testing.T) {
 
 func TestInstance_GetRoundUpdate(t *testing.T) {
 	i := Instance{
-		roundUpdates: &ds.Updates{},
+		roundUpdates: ds.NewUpdates(),
 	}
 	_ = i.roundUpdates.AddRound(&mixmessages.RoundInfo{ID: uint64(1), UpdateID: uint64(1)})
 	r, err := i.GetRoundUpdate(1)
@@ -89,7 +89,7 @@ func TestInstance_GetRoundUpdate(t *testing.T) {
 
 func TestInstance_GetRoundUpdates(t *testing.T) {
 	i := Instance{
-		roundUpdates: &ds.Updates{},
+		roundUpdates: ds.NewUpdates(),
 	}
 	_ = i.roundUpdates.AddRound(&mixmessages.RoundInfo{ID: uint64(1), UpdateID: uint64(1)})
 	_ = i.roundUpdates.AddRound(&mixmessages.RoundInfo{ID: uint64(1), UpdateID: uint64(2)})
@@ -184,7 +184,7 @@ func TestInstance_UpdatePartialNdf(t *testing.T) {
 
 func TestInstance_GetLastRoundID(t *testing.T) {
 	i := Instance{
-		roundData: &ds.Data{},
+		roundData: ds.NewData(),
 	}
 	_ = i.roundData.UpsertRound(&mixmessages.RoundInfo{ID: uint64(1)})
 	i.GetLastRoundID()
@@ -192,7 +192,7 @@ func TestInstance_GetLastRoundID(t *testing.T) {
 
 func TestInstance_GetLastUpdateID(t *testing.T) {
 	i := Instance{
-		roundUpdates: &ds.Updates{},
+		roundUpdates: ds.NewUpdates(),
 	}
 	_ = i.roundUpdates.AddRound(&mixmessages.RoundInfo{ID: uint64(1), UpdateID: uint64(1)})
 	i.GetLastUpdateID()
diff --git a/network/securedNdf.go b/network/securedNdf.go
index 13f18c75..34cc6a4b 100644
--- a/network/securedNdf.go
+++ b/network/securedNdf.go
@@ -9,7 +9,6 @@
 package network
 
 import (
-	"fmt"
 	"github.com/pkg/errors"
 	pb "gitlab.com/elixxir/comms/mixmessages"
 	ds "gitlab.com/elixxir/comms/network/dataStructures"
@@ -39,7 +38,6 @@ func NewSecuredNdf(definition *ndf.NetworkDefinition) (*SecuredNdf, error) {
 func (sndf *SecuredNdf) update(m *pb.NDF, key *rsa.PublicKey) error {
 	err := signature.Verify(m, key)
 	if err != nil {
-		fmt.Printf("err: %+v", err)
 		return errors.WithMessage(err, "Could not validate NDF")
 	}
 
diff --git a/network/securedNdf_test.go b/network/securedNdf_test.go
index e03e7e70..98a1b3d9 100644
--- a/network/securedNdf_test.go
+++ b/network/securedNdf_test.go
@@ -7,6 +7,7 @@
 package network
 
 import (
+	"fmt"
 	pb "gitlab.com/elixxir/comms/mixmessages"
 	ds "gitlab.com/elixxir/comms/network/dataStructures"
 	"gitlab.com/elixxir/comms/testutils"
@@ -51,6 +52,7 @@ func TestSecuredNdf_update(t *testing.T) {
 		t.Errorf("Could not generate rsa key: %s", err)
 	}
 	badPub := badPriv.GetPublic()
+	fmt.Println(badPub)
 
 	f := pb.NDF{}
 
-- 
GitLab