From cd4201fd4648826cf0842c75419bf99c99aa0e84 Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Mon, 23 May 2022 15:21:03 +0000
Subject: [PATCH] Add payload sizing functions to e2e and connect.

---
 connect/connect.go     | 45 ++++++++++++++++++++++++++++++++++++++++--
 e2e/interface.go       | 16 +++++++++++++++
 e2e/parse/partition.go | 18 +++++++++++++++++
 e2e/sendE2E.go         | 31 +++++++++++++++++++++++++++++
 4 files changed, 108 insertions(+), 2 deletions(-)

diff --git a/connect/connect.go b/connect/connect.go
index 1f0a6db74..00a1daa84 100644
--- a/connect/connect.go
+++ b/connect/connect.go
@@ -7,6 +7,9 @@
 package connect
 
 import (
+	"io"
+	"time"
+
 	"github.com/pkg/errors"
 	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/auth"
@@ -26,8 +29,6 @@ import (
 	"gitlab.com/elixxir/crypto/fastRNG"
 	"gitlab.com/elixxir/ekv"
 	"gitlab.com/xx_network/primitives/id"
-	"io"
-	"time"
 )
 
 const (
@@ -59,6 +60,22 @@ type Connection interface {
 		newListener receive.Listener) receive.ListenerID
 	// Unregister listener for E2E reception
 	Unregister(listenerID receive.ListenerID)
+
+	// FirstPartitionSize returns the max partition payload size for the
+	// first payload
+	FirstPartitionSize() uint
+
+	// SecondPartitionSize returns the max partition payload size for all
+	// payloads after the first payload
+	SecondPartitionSize() uint
+
+	// PartitionSize returns the partition payload size for the given
+	// payload index. The first payload is index 0.
+	PartitionSize(payloadIndex uint) uint
+
+	// PayloadSize Returns the max payload size for a partitionable E2E
+	// message
+	PayloadSize(payloadIndex uint) uint
 }
 
 // Callback is the callback format required to retrieve
@@ -287,3 +304,27 @@ func (a authCallback) Request(requestor contact.Contact,
 func (a authCallback) Reset(requestor contact.Contact,
 	receptionID receptionID.EphemeralIdentity, round rounds.Round) {
 }
+
+// FirstPartitionSize returns the max partition payload size for the
+// first payload
+func (h *handler) FirstPartitionSize() uint {
+	return h.e2e.FirstPartitionSize()
+}
+
+// SecondPartitionSize returns the max partition payload size for all
+// payloads after the first payload
+func (h *handler) SecondPartitionSize() uint {
+	return h.e2e.SecondPartitionSize()
+}
+
+// PartitionSize returns the partition payload size for the given
+// payload index. The first payload is index 0.
+func (h *handler) PartitionSize(payloadIndex uint) uint {
+	return h.e2e.PartitionSize(payloadIndex)
+}
+
+// PayloadSize Returns the max payload size for a partitionable E2E
+// message
+func (h *handler) PayloadSize() uint {
+	return h.e2e.PayloadSize()
+}
diff --git a/e2e/interface.go b/e2e/interface.go
index a24f285ce..40d7ea169 100644
--- a/e2e/interface.go
+++ b/e2e/interface.go
@@ -173,4 +173,20 @@ type Handler interface {
 
 	// GetReceptionID returns the default IDs
 	GetReceptionID() *id.ID
+
+	// FirstPartitionSize returns the max partition payload size for the
+	// first payload
+	FirstPartitionSize() uint
+
+	// SecondPartitionSize returns the max partition payload size for all
+	// payloads after the first payload
+	SecondPartitionSize() uint
+
+	// PartitionSize returns the partition payload size for the given
+	// payload index. The first payload is index 0.
+	PartitionSize(payloadIndex uint) uint
+
+	// PayloadSize Returns the max payload size for a partitionable E2E
+	// message
+	PayloadSize() uint
 }
diff --git a/e2e/parse/partition.go b/e2e/parse/partition.go
index e23649d3a..c0f5c0b95 100644
--- a/e2e/parse/partition.go
+++ b/e2e/parse/partition.go
@@ -104,6 +104,24 @@ func (p Partitioner) HandlePartition(sender *id.ID,
 	}
 }
 
+// FirstPartitionSize returns the max partition payload size for the
+// first payload
+func (p Partitioner) FirstPartitionSize() uint {
+	return uint(p.firstContentsSize)
+}
+
+// SecondPartitionSize returns the max partition payload size for all
+// payloads after the first payload
+func (p Partitioner) SecondPartitionSize() uint {
+	return uint(p.partContentsSize)
+}
+
+// PayloadSize Returns the max payload size for a partitionable E2E
+// message
+func (p Partitioner) PayloadSize() uint {
+	return uint(p.maxSize)
+}
+
 func splitPayload(payload []byte, length int) ([]byte, []byte) {
 	if len(payload) < length {
 		return payload, payload
diff --git a/e2e/sendE2E.go b/e2e/sendE2E.go
index 2a50201de..65cf98cdf 100644
--- a/e2e/sendE2E.go
+++ b/e2e/sendE2E.go
@@ -9,6 +9,7 @@ import (
 	"gitlab.com/elixxir/client/catalog"
 	"gitlab.com/elixxir/client/cmix"
 	"gitlab.com/elixxir/client/cmix/message"
+	"gitlab.com/elixxir/client/e2e/parse"
 	"gitlab.com/elixxir/client/e2e/ratchet/partner/session"
 	"gitlab.com/elixxir/client/e2e/rekey"
 	"gitlab.com/elixxir/client/stoppable"
@@ -199,3 +200,33 @@ func getSendErrors(c chan error) (numFail int, errRtn string) {
 		}
 	}
 }
+
+// FirstPartitionSize returns the max partition payload size for the
+// first payload
+func (m *manager) FirstPartitionSize() uint {
+	return m.partitioner.FirstPartitionSize()
+}
+
+// SecondPartitionSize returns the max partition payload size for all
+// payloads after the first payload
+func (m *manager) SecondPartitionSize() uint {
+	return m.partitioner.SecondPartitionSize()
+}
+
+// PartitionSize returns the partition payload size for the given
+// payload index. The first payload is index 0.
+func (m *manager) PartitionSize(payloadIndex uint) uint {
+	if payloadIndex == 0 {
+		return m.FirstPartitionSize()
+	}
+	if payloadIndex > parse.MaxMessageParts {
+		return 0
+	}
+	return m.SecondPartitionSize()
+}
+
+// PayloadSize Returns the max payload size for a partitionable E2E
+// message
+func (m *manager) PayloadSize() uint {
+	return m.partitioner.PayloadSize()
+}
-- 
GitLab