From 46d2c3527c3277bc0ed6355e115e9982a19c35a8 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Wed, 18 Nov 2020 10:10:38 -0800
Subject: [PATCH] Fix formatting and comm ID generation

---
 go.mod         |   4 +-
 go.sum         |   4 ++
 ud/commID.go   |  52 +++++++++++++++++++
 ud/lookup.go   | 136 ++++++++++++++++++++++++-------------------------
 ud/manager.go  |  30 ++++-------
 ud/register.go |  29 ++++++-----
 ud/remove.go   |   2 +-
 7 files changed, 150 insertions(+), 107 deletions(-)
 create mode 100644 ud/commID.go

diff --git a/go.mod b/go.mod
index f1c2cc7ad..80036c5f9 100644
--- a/go.mod
+++ b/go.mod
@@ -17,13 +17,13 @@ require (
 	github.com/spf13/jwalterweatherman v1.1.0
 	github.com/spf13/pflag v1.0.5 // indirect
 	github.com/spf13/viper v1.7.1
-	gitlab.com/elixxir/comms v0.0.4-0.20201111205457-b073b28c367a
+	gitlab.com/elixxir/comms v0.0.4-0.20201116233755-b476dea10095
 	gitlab.com/elixxir/crypto v0.0.5-0.20201110193609-6b5e881867b4
 	gitlab.com/elixxir/ekv v0.1.3
 	gitlab.com/elixxir/primitives v0.0.3-0.20201116174806-97f190989704
 	gitlab.com/xx_network/comms v0.0.4-0.20201110022115-4a6171cad07d
 	gitlab.com/xx_network/crypto v0.0.4
-	gitlab.com/xx_network/primitives v0.0.2
+	gitlab.com/xx_network/primitives v0.0.3-0.20201116234927-44e42fc91e7c
 	golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
 	google.golang.org/protobuf v1.25.0
 	gopkg.in/ini.v1 v1.61.0 // indirect
diff --git a/go.sum b/go.sum
index bbf4e62ea..cee607cb4 100644
--- a/go.sum
+++ b/go.sum
@@ -256,6 +256,8 @@ gitlab.com/elixxir/comms v0.0.4-0.20201111205457-b073b28c367a h1:GnZLdgn5ZSDtPFs
 gitlab.com/elixxir/comms v0.0.4-0.20201111205457-b073b28c367a/go.mod h1:5p7oz4yFrK037rPap6ooaWrloJrzuVZ4jnzOdvgyqnU=
 gitlab.com/elixxir/comms v0.0.4-0.20201112000135-4147e270e129 h1:d4s9JGimUVmqdKlK4NhICayzWBUSM2XmKta821ZJTF0=
 gitlab.com/elixxir/comms v0.0.4-0.20201112000135-4147e270e129/go.mod h1:5p7oz4yFrK037rPap6ooaWrloJrzuVZ4jnzOdvgyqnU=
+gitlab.com/elixxir/comms v0.0.4-0.20201116233755-b476dea10095 h1:YptJAYLxqy5CAJIcM9kOwfxmJ2D2A8uXWTT8rgXYG+E=
+gitlab.com/elixxir/comms v0.0.4-0.20201116233755-b476dea10095/go.mod h1:spFKl7jsMy8M6NDvhJ27IJ+CnZ/07JHJCYpYsG8JQ4o=
 gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4 h1:28ftZDeYEko7xptCZzeFWS1Iam95dj46TWFVVlKmw6A=
 gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo+K0N2GwRRpaNr+tKXMIOVWzmyUD0SbOu2c=
 gitlab.com/elixxir/crypto v0.0.3 h1:znCt/x2bL4y8czTPaaFkwzdgSgW3BJc/1+dxyf1jqVw=
@@ -290,6 +292,8 @@ gitlab.com/xx_network/primitives v0.0.0-20200804183002-f99f7a7284da h1:CCVslUwNC
 gitlab.com/xx_network/primitives v0.0.0-20200804183002-f99f7a7284da/go.mod h1:OK9xevzWCaPO7b1wiluVJGk7R5ZsuC7pHY5hteZFQug=
 gitlab.com/xx_network/primitives v0.0.2 h1:r45yKenJ9e7PylI1ZXJ1Es09oYNaYXjxVy9+uYlwo7Y=
 gitlab.com/xx_network/primitives v0.0.2/go.mod h1:cs0QlFpdMDI6lAo61lDRH2JZz+3aVkHy+QogOB6F/qc=
+gitlab.com/xx_network/primitives v0.0.3-0.20201116234927-44e42fc91e7c h1:mYId667WIN97E6KhPw4HDYyCjWzsG7gCM/HLTNTCXZQ=
+gitlab.com/xx_network/primitives v0.0.3-0.20201116234927-44e42fc91e7c/go.mod h1:cs0QlFpdMDI6lAo61lDRH2JZz+3aVkHy+QogOB6F/qc=
 gitlab.com/xx_network/ring v0.0.2 h1:TlPjlbFdhtJrwvRgIg4ScdngMTaynx/ByHBRZiXCoL0=
 gitlab.com/xx_network/ring v0.0.2/go.mod h1:aLzpP2TiZTQut/PVHR40EJAomzugDdHXetbieRClXIM=
 go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
diff --git a/ud/commID.go b/ud/commID.go
new file mode 100644
index 000000000..712ef213b
--- /dev/null
+++ b/ud/commID.go
@@ -0,0 +1,52 @@
+package ud
+
+import (
+	"encoding/binary"
+	jww "github.com/spf13/jwalterweatherman"
+	"gitlab.com/elixxir/client/storage/versioned"
+	"time"
+)
+
+const commIDKey = "commIDKey"
+const commIDVersion = 0
+
+// getCommID returns the ID for the next comm. IDs are generated sequentially.
+func (m *Manager) getCommID() uint64 {
+
+	m.commIDLock.Lock()
+	defer m.commIDLock.Unlock()
+	returnedID := m.commID
+
+	// Increment ID for next get
+	m.commID++
+
+	// Save ID storage
+	data := make([]byte, 8)
+	binary.BigEndian.PutUint64(data, m.commID)
+
+	obj := &versioned.Object{
+		Version:   commIDVersion,
+		Timestamp: time.Now(),
+		Data:      data,
+	}
+
+	if err := m.storage.Set(commIDKey, obj); err != nil {
+		jww.FATAL.Panicf("Failed to store the next commID: %+v", err)
+	}
+
+	return returnedID
+}
+
+// loadCommID retrieves the next comm ID from storage.
+func (m *Manager) loadCommID() {
+	m.commIDLock.Lock()
+	defer m.commIDLock.Unlock()
+
+	obj, err := m.storage.Get(commIDKey)
+	if err != nil {
+		jww.WARN.Printf("Failed to get the commID; restarting at zero: %+v", err)
+		return
+	}
+
+	m.commID = binary.BigEndian.Uint64(obj.Data)
+}
diff --git a/ud/lookup.go b/ud/lookup.go
index 1032c6a3e..f766b838a 100644
--- a/ud/lookup.go
+++ b/ud/lookup.go
@@ -3,82 +3,75 @@ package ud
 import (
 	"github.com/golang/protobuf/proto"
 	"github.com/pkg/errors"
+	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/interfaces/contact"
 	"gitlab.com/elixxir/client/interfaces/message"
 	"gitlab.com/elixxir/client/interfaces/params"
-	"gitlab.com/elixxir/client/interfaces/utility"
 	"gitlab.com/elixxir/comms/network/dataStructures"
 	"gitlab.com/elixxir/primitives/states"
 	"gitlab.com/xx_network/primitives/id"
-	"google.golang.org/protobuf/runtime/protoimpl"
 	"time"
-	jww "github.com/spf13/jwalterweatherman"
 )
 
 type lookupCallback func(contact.Contact, error)
 
-
-func (m *Manager)lookupProcess(c chan message.Receive, quitCh <-chan struct{}){
+func (m *Manager) lookupProcess(c chan message.Receive, quitCh <-chan struct{}) {
 	for true {
 		select {
 		case <-quitCh:
 			return
 		case response := <-c:
-			// edge check the encryption
-			if response.Encryption!=message.E2E{
+			// Edge check the encryption
+			if response.Encryption != message.E2E {
 				jww.WARN.Printf("Dropped a lookup response from user " +
 					"discovery due to incorrect encryption")
 			}
 
-			// unmarshal the message
+			// Unmarshal the message
 			lookupResponse := &LookupResponse{}
-			if err :=proto.Unmarshal(response.Payload, lookupResponse); err!=nil{
-				jww.WARN.Printf("Dropped a lookup response from user " +
+			if err := proto.Unmarshal(response.Payload, lookupResponse); err != nil {
+				jww.WARN.Printf("Dropped a lookup response from user "+
 					"discovery due to failed unmarshal: %s", err)
 			}
 
-			// get the appropriate channel from the lookup
+			// Get the appropriate channel from the lookup
 			m.inProgressMux.RLock()
 			ch, ok := m.inProgressLookup[lookupResponse.CommID]
 			m.inProgressMux.RUnlock()
-			if !ok{
-				jww.WARN.Printf("Dropped a lookup response from user " +
+			if !ok {
+				jww.WARN.Printf("Dropped a lookup response from user "+
 					"discovery due to unknown comm ID: %d",
 					lookupResponse.CommID)
 			}
 
-			// send the response on the correct channel
-			// drop if the send cannot be completed
-			select{
-				case ch<-lookupResponse:
-				default:
-					jww.WARN.Printf("Dropped a lookup response from user " +
-						"discovery due failure to transmit to handling thread: " +
-						"commID: %d", lookupResponse.CommID)
+			// Send the response on the correct channel
+			// Drop if the send cannot be completed
+			select {
+			case ch <- lookupResponse:
+			default:
+				jww.WARN.Printf("Dropped a lookup response from user "+
+					"discovery due to failure to transmit to handling thread: "+
+					"commID: %d", lookupResponse.CommID)
 			}
 		}
 	}
 }
 
+// Lookup returns the public key of the passed ID as known by the user discovery
+// system or returns by the timeout.
+func (m *Manager) Lookup(uid *id.ID, callback lookupCallback, timeout time.Duration) error {
 
-// returns the public key of the passed id as known by the user discovery system
-// or returns by the timeout
-func (m *Manager)Lookup(id *id.ID, callback lookupCallback, timeout time.Duration)error{
-
-	//get the id of this comm so it can be connected to its responce
-	commID, err := m.getCommID()
-	if err!=nil{
-		return errors.WithMessage(err, "Random generation failed")
-	}
+	// Get the ID of this comm so it can be connected to its response
+	commID := m.getCommID()
 
-	//build the request
+	// Build the request
 	request := &LookupSend{
-		UserID:        id.Marshal(),
-		CommID:        commID,
+		UserID: uid.Marshal(),
+		CommID: commID,
 	}
 
 	requestMarshaled, err := proto.Marshal(request)
-	if err!=nil{
+	if err != nil {
 		return errors.WithMessage(err, "Failed to form outgoing request")
 	}
 
@@ -88,68 +81,71 @@ func (m *Manager)Lookup(id *id.ID, callback lookupCallback, timeout time.Duratio
 		MessageType: message.UdLookup,
 	}
 
-	//register the request in the responce map so it can be procesed on return
+	// Register the request in the response map so it can be processed on return
 	responseChan := make(chan *LookupResponse, 1)
 	m.inProgressMux.Lock()
 	m.inProgressLookup[commID] = responseChan
 	m.inProgressMux.Unlock()
 
-	//send the request
+	// Send the request
 	rounds, _, err := m.net.SendE2E(msg, params.GetDefaultE2E())
 
-	if err!=nil{
-		return errors.WithMessage(err, "Failed to send the lookup " +
-			"request")
+	if err != nil {
+		return errors.WithMessage(err, "Failed to send the lookup request")
 	}
 
-	//register the round event to capture if the round fails
+	// Register the round event to capture if the round fails
 	roundFailChan := make(chan dataStructures.EventReturn, len(rounds))
 
-	for _, round := range rounds{
-		//subtract a millisecond to ensure this timeout will trigger before
-		// the one below
+	for _, round := range rounds {
+		// Subtract a millisecond to ensure this timeout will trigger before the
+		// one below
 		m.net.GetInstance().GetRoundEvents().AddRoundEventChan(round,
 			roundFailChan, timeout-1*time.Millisecond, states.FAILED)
 	}
 
-	//start the go routine which will trigger the callback
-	go func(){
+	// Start the go routine which will trigger the callback
+	go func() {
 		timer := time.NewTimer(timeout)
 
 		var err error
 		var c contact.Contact
 
-		select{
-			//return an error if the round fails
-			case <-roundFailChan:
-				err= errors.New("One or more rounds failed to " +
-					"resolve, lookup not delivered")
-			//return an error if the timeout is reached
-			case <-timer.C:
-				err= errors.New("Response from User Discovery" +
-					" did not come before timeout")
-			//return the contact if one is returned
-			case response := <-responseChan:
-				if response.Error!=""{
-					err = errors.Errorf("User Discovery returned an " +
-						"error on Lookup: %s", response.Error)
-				}else{
-					pubkey := m.grp.NewIntFromBytes(response.PubKey)
-					c = contact.Contact{
-						ID:             id,
-						DhPubKey:       pubkey,
-						OwnershipProof: nil,
-						Facts:          nil,
-					}
+		select {
+		// Return an error if the round fails
+		case <-roundFailChan:
+			err = errors.New("One or more rounds failed to resolved; " +
+				"lookup not delivered")
+
+		// Return an error if the timeout is reached
+		case <-timer.C:
+			err = errors.New("Response from User Discovery did not come " +
+				"before timeout")
+
+		// Return the contact if one is returned
+		case response := <-responseChan:
+			if response.Error != "" {
+				err = errors.Errorf("User Discovery returned an error on "+
+					"Lookup: %s", response.Error)
+			} else {
+				pubkey := m.grp.NewIntFromBytes(response.PubKey)
+				c = contact.Contact{
+					ID:             uid,
+					DhPubKey:       pubkey,
+					OwnershipProof: nil,
+					Facts:          nil,
 				}
+			}
 		}
-		//delete the response channel from the map
+
+		// Delete the response channel from the map
 		m.inProgressMux.Lock()
 		delete(m.inProgressLookup, commID)
 		m.inProgressMux.Unlock()
-		//call the callback last in case it is blocking
+
+		// Call the callback last in case it is blocking
 		callback(c, err)
 	}()
 
 	return nil
-}
\ No newline at end of file
+}
diff --git a/ud/manager.go b/ud/manager.go
index a12e5f1fc..10dcc2a81 100644
--- a/ud/manager.go
+++ b/ud/manager.go
@@ -1,10 +1,10 @@
 package ud
 
 import (
-	"encoding/binary"
 	"gitlab.com/elixxir/client/interfaces"
 	"gitlab.com/elixxir/client/interfaces/message"
 	"gitlab.com/elixxir/client/stoppable"
+	"gitlab.com/elixxir/client/storage"
 	"gitlab.com/elixxir/comms/client"
 	"gitlab.com/elixxir/crypto/cyclic"
 	"gitlab.com/elixxir/crypto/fastRNG"
@@ -18,40 +18,30 @@ type Manager struct {
 	comms   *client.Comms
 	host    *connect.Host
 	privKey *rsa.PrivateKey
-	rng *fastRNG.StreamGenerator
-	grp *cyclic.Group
-	sw interfaces.Switchboard
+	rng     *fastRNG.StreamGenerator
+	grp     *cyclic.Group
+	sw      interfaces.Switchboard
+	storage *storage.Session
 
 	udID *id.ID
 
-
 	inProgressLookup map[uint64]chan *LookupResponse
-	inProgressMux sync.RWMutex
+	inProgressMux    sync.RWMutex
 
 	net interfaces.NetworkManager
-}
-
-func (m *Manager)getCommID()(uint64, error){
-	//fixme: this should use incremenetation
-	stream := m.rng.GetStream()
 
-	idBytes := make([]byte, 8)
-	if _, err := stream.Read(idBytes); err!=nil{
-		return 0, err
-	}
-
-	return binary.BigEndian.Uint64(idBytes), nil
+	commID     uint64
+	commIDLock sync.Mutex
 }
 
-func (m *Manager)StartProcessies()stoppable.Stoppable{
+func (m *Manager) StartProcesses() stoppable.Stoppable {
 
 	lookupStop := stoppable.NewSingle("UDLookup")
 	lookupChan := make(chan message.Receive, 100)
 	m.sw.RegisterChannel("UDLookupResponse", m.udID, message.UdLookupResponse, lookupChan)
 	go m.lookupProcess(lookupChan, lookupStop.Quit())
 
-
 	udMulti := stoppable.NewMulti("UD")
 	udMulti.Add(lookupStop)
 	return lookupStop
-}
\ No newline at end of file
+}
diff --git a/ud/register.go b/ud/register.go
index 6fdd2b54e..84aec1a92 100644
--- a/ud/register.go
+++ b/ud/register.go
@@ -1,24 +1,25 @@
 package ud
 
 import (
-	"gitlab.com/xx_network/comms/messages"
+	pb "gitlab.com/elixxir/comms/mixmessages"
 	"gitlab.com/xx_network/primitives/id"
 )
 
-func (m *Manager)Register(myID *id.ID, username string)error{
+func (m *Manager) Register(myID *id.ID, username string) error {
 
-	msg := &messages.AuthenticatedMessage{
-		ID:                   myID.Bytes(),
-		Signature:            nil,
-		Token:                nil,
-		Client:               nil,
-		Message:              nil,
-		XXX_NoUnkeyedLiteral: struct{}{},
-		XXX_unrecognized:     nil,
-		XXX_sizecache:        0,
+	msg := &pb.UDBUserRegistration{
+		PermissioningSignature: nil,
+		RSAPublicPem:           "",
+		IdentityRegistration:   nil,
+		IdentitySignature:      nil,
+		Frs:                    nil,
+		UID:                    myID.Bytes(),
+		XXX_NoUnkeyedLiteral:   struct{}{},
+		XXX_unrecognized:       nil,
+		XXX_sizecache:          0,
 	}
 
-	m.comms.SendRegisterUser(m.host)
+	_, _ = m.comms.SendRegisterUser(m.host, msg)
 
-
-}
\ No newline at end of file
+	return nil
+}
diff --git a/ud/remove.go b/ud/remove.go
index a3895ae0a..6d7994d66 100644
--- a/ud/remove.go
+++ b/ud/remove.go
@@ -17,7 +17,7 @@ type removeFactComms interface {
 }
 
 func (m *Manager) RemoveFact(fact contact.Fact) error {
-	return m.removeFact(fact, m.comms)
+	return m.removeFact(fact, nil)
 }
 
 func (m *Manager) removeFact(fact contact.Fact, rFC removeFactComms) error {
-- 
GitLab