From e7b87424a6b1a7d780f1f52f4ee70652896784a9 Mon Sep 17 00:00:00 2001
From: josh <josh@elixxir.io>
Date: Wed, 15 Jun 2022 11:08:18 -0700
Subject: [PATCH] Better documentation/clean duplicate code

---
 cmd/root.go  | 47 +++++++++++++++++------------------------------
 cmd/utils.go | 10 ++++++++++
 2 files changed, 27 insertions(+), 30 deletions(-)

diff --git a/cmd/root.go b/cmd/root.go
index 6a1b3d88f..690e7490b 100644
--- a/cmd/root.go
+++ b/cmd/root.go
@@ -265,27 +265,17 @@ var rootCmd = &cobra.Command{
 		paramsE2E := e2e.GetDefaultParams()
 		roundTimeout := paramsE2E.CMIXParams.SendTimeout
 		if viper.GetBool("accept-channel") {
-			done := make(chan struct{}, 1)
-			retryChan := make(chan struct{}, 1)
-
+			// Verify that the confirmation message makes it to the
+			// original sender
 			if viper.GetBool("verify-sends") {
+				done := make(chan struct{}, 1)
+				retryChan := make(chan struct{}, 1)
 				for {
 					rid := acceptChannel(client, recipientID)
-					// Verify message sends were successful
-
-					// Construct the callback function which
-					// verifies successful message send or retries
-					f := func(allRoundsSucceeded, timedOut bool,
-						rounds map[id.Round]cmix.RoundResult) {
-						if !allRoundsSucceeded {
-							retryChan <- struct{}{}
-						} else {
-							done <- struct{}{}
-						}
-					}
 
 					// Monitor rounds for results
-					err = client.GetCmix().GetRoundResults(roundTimeout, f, rid)
+					err = client.GetCmix().GetRoundResults(roundTimeout,
+						makeVerifySendsCallback(retryChan, done), rid)
 					if err != nil {
 						jww.DEBUG.Printf("Could not verify "+
 							"confirmation message for relationship with %s were sent "+
@@ -309,6 +299,7 @@ var rootCmd = &cobra.Command{
 					break
 				}
 			} else {
+				// Accept channel, agnostic of round result
 				acceptChannel(client, recipientID)
 			}
 
@@ -802,22 +793,15 @@ func addAuthenticatedChannel(client *messenger.Client, recipientID *id.ID,
 		me := client.GetUser().GetContact()
 		jww.INFO.Printf("Requesting auth channel from: %s",
 			recipientID)
-		retryChan := make(chan struct{}, 1)
-		done := make(chan struct{}, 1)
 		paramsE2E := e2e.GetDefaultParams()
 		roundTimeout := paramsE2E.CMIXParams.SendTimeout
+
+		// Verify that the auth request makes it to the recipient
+		// by monitoring the round result
 		if viper.GetBool("verify-sends") {
+			retryChan := make(chan struct{}, 1)
+			done := make(chan struct{}, 1)
 			for {
-				// Construct the callback function which
-				// verifies successful message send or retries
-				f := func(allRoundsSucceeded, timedOut bool, rounds map[id.Round]cmix.RoundResult) {
-					if !allRoundsSucceeded {
-						retryChan <- struct{}{}
-					} else {
-						done <- struct{}{}
-					}
-				}
-
 				rid, err := client.GetAuth().Request(recipientContact,
 					me.Facts)
 				if err != nil {
@@ -825,7 +809,9 @@ func addAuthenticatedChannel(client *messenger.Client, recipientID *id.ID,
 				}
 
 				// Monitor rounds for results
-				err = client.GetCmix().GetRoundResults(roundTimeout, f, rid)
+				err = client.GetCmix().GetRoundResults(roundTimeout,
+					makeVerifySendsCallback(retryChan, done),
+					rid)
 				if err != nil {
 					jww.DEBUG.Printf("Could not verify auth request was sent " +
 						"successfully, resending...")
@@ -835,7 +821,7 @@ func addAuthenticatedChannel(client *messenger.Client, recipientID *id.ID,
 				select {
 				case <-retryChan:
 					// On a retry, go to the top of the loop
-					jww.DEBUG.Printf("Auth Request was not sent " +
+					jww.DEBUG.Printf("Auth request was not sent " +
 						"successfully, resending...")
 					continue
 				case <-done:
@@ -847,6 +833,7 @@ func addAuthenticatedChannel(client *messenger.Client, recipientID *id.ID,
 				break
 			}
 		} else {
+			// Just call Request, agnostic of round result
 			_, err := client.GetAuth().Request(recipientContact,
 				me.Facts)
 			if err != nil {
diff --git a/cmd/utils.go b/cmd/utils.go
index 60976096b..fdc1ae8fa 100644
--- a/cmd/utils.go
+++ b/cmd/utils.go
@@ -108,3 +108,13 @@ func readContact() contact.Contact {
 	jww.INFO.Printf("Contact ID: %s", c.ID)
 	return c
 }
+
+func makeVerifySendsCallback(retryChan, done chan struct{}) cmix.RoundEventCallback {
+	return func(allRoundsSucceeded, timedOut bool, rounds map[id.Round]cmix.RoundResult) {
+		if !allRoundsSucceeded {
+			retryChan <- struct{}{}
+		} else {
+			done <- struct{}{}
+		}
+	}
+}
-- 
GitLab