From a93ded09b1ff01d4e6eaf4b2900ccdb3458b6630 Mon Sep 17 00:00:00 2001
From: Jake Taylor <jake@elixxir.io>
Date: Wed, 5 May 2021 15:46:21 -0500
Subject: [PATCH] protected context building

---
 connect/auth.go                 | 4 ++--
 connect/context.go              | 4 ++--
 connect/host.go                 | 9 +++++----
 interconnect/consensusClient.go | 2 +-
 4 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/connect/auth.go b/connect/auth.go
index c442549..e11622e 100644
--- a/connect/auth.go
+++ b/connect/auth.go
@@ -44,7 +44,7 @@ func (c *ProtoComms) clientHandshake(host *Host) (err error) {
 
 	// Set up the context
 	client := pb.NewGenericClient(host.connection)
-	ctx, cancel := MessagingContext(host.GetSendTimeout())
+	ctx, cancel := host.GetMessagingContext()
 	defer cancel()
 
 	// Send the token request message
@@ -75,7 +75,7 @@ func (c *ProtoComms) clientHandshake(host *Host) (err error) {
 	}
 
 	// Set up the context
-	ctx, cancel = MessagingContext(host.GetSendTimeout())
+	ctx, cancel = host.GetMessagingContext()
 	defer cancel()
 
 	// Send the authenticate token message
diff --git a/connect/context.go b/connect/context.go
index 362a862..ece8d4d 100644
--- a/connect/context.go
+++ b/connect/context.go
@@ -17,8 +17,8 @@ import (
 	"time"
 )
 
-// MessagingContext builds a context for connections and message sending with the given timeout
-func MessagingContext(waitingPeriod time.Duration) (context.Context, context.CancelFunc) {
+// newContext builds a context for connections and message sending with the given timeout
+func newContext(waitingPeriod time.Duration) (context.Context, context.CancelFunc) {
 	jww.DEBUG.Printf("Timing out in: %s", waitingPeriod)
 	ctx, cancel := context.WithTimeout(context.Background(),
 		waitingPeriod)
diff --git a/connect/host.go b/connect/host.go
index 449fd9e..140bcb0 100644
--- a/connect/host.go
+++ b/connect/host.go
@@ -10,6 +10,7 @@
 package connect
 
 import (
+	"context"
 	"fmt"
 	"github.com/pkg/errors"
 	jww "github.com/spf13/jwalterweatherman"
@@ -167,9 +168,9 @@ func (h *Host) Connected() (bool, uint64) {
 	return h.isAlive() && !h.authenticationRequired(), h.connectionCount
 }
 
-// GetSendTimeout returns the timeout for message sending
-func (h *Host) GetSendTimeout() time.Duration {
-	return h.params.SendTimeout
+// GetMessagingContext returns a context object for message sending configured according to HostParams
+func (h *Host) GetMessagingContext() (context.Context, context.CancelFunc) {
+	return newContext(h.params.SendTimeout)
 }
 
 // GetId returns the id of the host
@@ -375,7 +376,7 @@ func (h *Host) connectHelper() (err error) {
 		if backoffTime > 15000 {
 			backoffTime = 15000
 		}
-		ctx, cancel := MessagingContext(time.Duration(backoffTime) * time.Millisecond)
+		ctx, cancel := newContext(time.Duration(backoffTime) * time.Millisecond)
 
 		// Create the connection
 		h.connection, err = grpc.DialContext(ctx, h.GetAddress(),
diff --git a/interconnect/consensusClient.go b/interconnect/consensusClient.go
index 4065074..d4d0efd 100644
--- a/interconnect/consensusClient.go
+++ b/interconnect/consensusClient.go
@@ -26,7 +26,7 @@ func (c *CMixServer) GetNdf(host *connect.Host,
 	// Create the Send Function
 	f := func(conn *grpc.ClientConn) (*any.Any, error) {
 		// Set up the context
-		ctx, cancel := connect.MessagingContext(host.GetSendTimeout())
+		ctx, cancel := host.GetMessagingContext()
 		defer cancel()
 		//Format to authenticated message type
 		// Send the message
-- 
GitLab