From 9bbff0f6d924b3259728f744b3c05b594425be61 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Mon, 8 Aug 2022 18:38:31 +0000
Subject: [PATCH] XX-4013 / Bindings RoundList

---
 bindings/broadcast.go      | 16 ++++++++--------
 bindings/broadcast_test.go |  8 ++++----
 bindings/connect.go        |  2 +-
 bindings/connect_test.go   |  2 +-
 bindings/delivery.go       | 26 +++++++++++++++-----------
 bindings/e2eHandler.go     |  2 +-
 bindings/group.go          | 24 ++++++++++++------------
 bindings/single.go         |  6 +++---
 bindings/single_test.go    |  2 +-
 bindings/ud.go             |  4 ++--
 10 files changed, 48 insertions(+), 44 deletions(-)

diff --git a/bindings/broadcast.go b/bindings/broadcast.go
index d27739043..aa29611d4 100644
--- a/bindings/broadcast.go
+++ b/bindings/broadcast.go
@@ -58,8 +58,8 @@ type BroadcastMessage struct {
 //   "EphID":[0,0,0,0,0,0,24,61]
 //  }
 type BroadcastReport struct {
-	RoundID int
-	EphID   ephemeral.Id
+	RoundsList
+	EphID ephemeral.Id
 }
 
 // BroadcastListener is the public function type bindings can use to listen for broadcast messages.
@@ -120,8 +120,8 @@ func (c *Channel) Listen(l BroadcastListener, method int) error {
 		receptionID receptionID.EphemeralIdentity, round rounds.Round) {
 		l.Callback(json.Marshal(&BroadcastMessage{
 			BroadcastReport: BroadcastReport{
-				RoundID: int(round.ID),
-				EphID:   receptionID.EphId,
+				RoundsList: makeRoundsList(round.ID),
+				EphID:      receptionID.EphId,
 			},
 			Payload: payload,
 		}))
@@ -136,8 +136,8 @@ func (c *Channel) Broadcast(payload []byte) ([]byte, error) {
 		return nil, err
 	}
 	return json.Marshal(BroadcastReport{
-		RoundID: int(rid),
-		EphID:   eid,
+		RoundsList: makeRoundsList(rid),
+		EphID:      eid,
 	})
 }
 
@@ -153,8 +153,8 @@ func (c *Channel) BroadcastAsymmetric(payload, pk []byte) ([]byte, error) {
 		return nil, err
 	}
 	return json.Marshal(BroadcastReport{
-		RoundID: int(rid),
-		EphID:   eid,
+		RoundsList: makeRoundsList(rid),
+		EphID:      eid,
 	})
 }
 
diff --git a/bindings/broadcast_test.go b/bindings/broadcast_test.go
index 48fa0bed5..5c12dff82 100644
--- a/bindings/broadcast_test.go
+++ b/bindings/broadcast_test.go
@@ -37,8 +37,8 @@ func TestBroadcastMessage_JSON(t *testing.T) {
 	}
 	bm := BroadcastMessage{
 		BroadcastReport: BroadcastReport{
-			RoundID: 42,
-			EphID:   eid,
+			RoundsList: makeRoundsList(42),
+			EphID:      eid,
 		},
 		Payload: []byte("Hello, broadcast friends!"),
 	}
@@ -56,8 +56,8 @@ func TestBroadcastReport_JSON(t *testing.T) {
 		t.Errorf("Failed to form ephemeral ID: %+v", err)
 	}
 	br := BroadcastReport{
-		RoundID: 42,
-		EphID:   eid,
+		RoundsList: makeRoundsList(42),
+		EphID:      eid,
 	}
 
 	brJson, err := json.Marshal(br)
diff --git a/bindings/connect.go b/bindings/connect.go
index 57cb090bd..a384ff464 100644
--- a/bindings/connect.go
+++ b/bindings/connect.go
@@ -93,7 +93,7 @@ func (c *Connection) SendE2E(mt int, payload []byte) ([]byte, error) {
 		Timestamp: ts.UnixNano(),
 	}
 
-	sr.RoundsList = makeRoundsList(rounds)
+	sr.RoundsList = makeRoundsList(rounds...)
 
 	return json.Marshal(&sr)
 }
diff --git a/bindings/connect_test.go b/bindings/connect_test.go
index c23027b10..e0f8b38cd 100644
--- a/bindings/connect_test.go
+++ b/bindings/connect_test.go
@@ -23,7 +23,7 @@ func TestE2ESendReport_JSON(t *testing.T) {
 	mid := e2e.MessageID{}
 	_, _ = rng.Read(mid[:])
 	origRL := []id.Round{1, 5, 9}
-	rl := makeRoundsList(origRL)
+	rl := makeRoundsList(origRL...)
 	mrl, _ := json.Marshal(&rl)
 	sr := E2ESendReport{
 		RoundsList: rl,
diff --git a/bindings/delivery.go b/bindings/delivery.go
index a160813bf..62647578d 100644
--- a/bindings/delivery.go
+++ b/bindings/delivery.go
@@ -22,7 +22,17 @@ import (
 // Example marshalled roundList object:
 //  [1001,1003,1006]
 type RoundsList struct {
-	Rounds []int
+	Rounds []uint64
+}
+
+// makeRoundsList converts a list of id.Round into a binding-compatable
+// RoundsList.
+func makeRoundsList(rounds ...id.Round) RoundsList {
+	rl := RoundsList{make([]uint64, len(rounds))}
+	for i, rid := range rounds {
+		rl.Rounds[i] = uint64(rid)
+	}
+	return rl
 }
 
 // Marshal JSON marshals the RoundsList.
@@ -46,15 +56,6 @@ func unmarshalRoundsList(marshaled []byte) ([]id.Round, error) {
 	}
 
 	return realRl, nil
-
-}
-
-func makeRoundsList(rounds []id.Round) RoundsList {
-	rl := RoundsList{}
-	for _, rid := range rounds {
-		rl.Rounds = append(rl.Rounds, int(rid))
-	}
-	return rl
 }
 
 // MessageDeliveryCallback gets called on the determination if all events
@@ -79,9 +80,12 @@ type MessageDeliveryCallback interface {
 // This function takes the marshaled send report to ensure a memory leak does
 // not occur as a result of both sides of the bindings holding a reference to
 // the same pointer.
+//
+// roundList is a JSON marshalled RoundsList or any JSON marshalled send report
+// that inherits a RoundsList object.
 func (c *Cmix) WaitForMessageDelivery(
 	roundList []byte, mdc MessageDeliveryCallback, timeoutMS int) error {
-	jww.INFO.Printf("WaitForMessageDelivery(%v, _, %v)", roundList, timeoutMS)
+	jww.INFO.Printf("WaitForMessageDelivery(%s, _, %d)", roundList, timeoutMS)
 	rl, err := unmarshalRoundsList(roundList)
 	if err != nil {
 		return errors.Errorf("Failed to WaitForMessageDelivery callback due "+
diff --git a/bindings/e2eHandler.go b/bindings/e2eHandler.go
index 52e0c830f..5cac142e3 100644
--- a/bindings/e2eHandler.go
+++ b/bindings/e2eHandler.go
@@ -138,7 +138,7 @@ func (e *E2e) SendE2E(messageType int, recipientId, payload,
 	}
 
 	result := E2ESendReport{
-		RoundsList: makeRoundsList(roundIds),
+		RoundsList: makeRoundsList(roundIds...),
 		MessageID:  messageId.Marshal(),
 		Timestamp:  ts.UnixNano(),
 	}
diff --git a/bindings/group.go b/bindings/group.go
index 4bae70534..fa1fcc78d 100644
--- a/bindings/group.go
+++ b/bindings/group.go
@@ -150,9 +150,9 @@ func (g *GroupChat) MakeGroup(membership IdList, message, name []byte) (
 
 	// Construct the group report
 	report := GroupReport{
-		Id:     grp.ID.Bytes(),
-		Rounds: makeRoundsList(rounds).Rounds,
-		Status: int(status),
+		Id:         grp.ID.Bytes(),
+		RoundsList: makeRoundsList(rounds...),
+		Status:     int(status),
 	}
 
 	// Marshal the report
@@ -190,9 +190,9 @@ func (g *GroupChat) ResendRequest(groupId []byte) ([]byte, error) {
 
 	// Construct the group report on resent request
 	report := &GroupReport{
-		Id:     grp.ID.Bytes(),
-		Rounds: makeRoundsList(rnds).Rounds,
-		Status: int(status),
+		Id:         grp.ID.Bytes(),
+		RoundsList: makeRoundsList(rnds...),
+		Status:     int(status),
 	}
 
 	// Marshal the report
@@ -261,9 +261,9 @@ func (g *GroupChat) Send(groupId,
 
 	// Construct send report
 	sendReport := &GroupSendReport{
-		RoundID:   uint64(round),
-		Timestamp: timestamp.UnixNano(),
-		MessageID: msgID.Bytes(),
+		RoundsList: makeRoundsList(round),
+		Timestamp:  timestamp.UnixNano(),
+		MessageID:  msgID.Bytes(),
 	}
 
 	return json.Marshal(sendReport)
@@ -426,15 +426,15 @@ func (gcp *groupChatProcessor) String() string {
 // the group, a list of rounds that the group requests were sent on, and the
 // status of the send operation.
 type GroupReport struct {
-	Id     []byte
-	Rounds []int
+	Id []byte
+	RoundsList
 	Status int
 }
 
 // GroupSendReport is returned when sending a group message. It contains the
 // round ID sent on and the timestamp of the send operation.
 type GroupSendReport struct {
-	RoundID   uint64
+	RoundsList
 	Timestamp int64
 	MessageID []byte
 }
diff --git a/bindings/single.go b/bindings/single.go
index ee1ea6fb7..f246f920f 100644
--- a/bindings/single.go
+++ b/bindings/single.go
@@ -62,7 +62,7 @@ func TransmitSingleUse(e2eID int, recipient []byte, tag string, payload,
 	sr := SingleUseSendReport{
 		EphID:       eid.EphId.Int64(),
 		ReceptionID: eid.Source.Marshal(),
-		RoundsList:  makeRoundsList(rids),
+		RoundsList:  makeRoundsList(rids...),
 	}
 	return json.Marshal(sr)
 }
@@ -201,7 +201,7 @@ func (sl singleUseListener) Callback(
 	// Todo: what other info from req needs to get to bindings
 	scr := SingleUseCallbackReport{
 		Payload:     req.GetPayload(),
-		RoundsList:  makeRoundsList(rids),
+		RoundsList:  makeRoundsList(rids...),
 		Partner:     req.GetPartner(),
 		EphID:       eid.EphId.Int64(),
 		ReceptionID: eid.Source.Marshal(),
@@ -239,7 +239,7 @@ func (sr singleUseResponse) Callback(payload []byte,
 		rids = append(rids, r.ID)
 	}
 	sendReport := SingleUseResponseReport{
-		RoundsList:  makeRoundsList(rids),
+		RoundsList:  makeRoundsList(rids...),
 		ReceptionID: receptionID.Source.Marshal(),
 		EphID:       receptionID.EphId.Int64(),
 		Payload:     payload,
diff --git a/bindings/single_test.go b/bindings/single_test.go
index 863ff7c71..a262a4073 100644
--- a/bindings/single_test.go
+++ b/bindings/single_test.go
@@ -20,7 +20,7 @@ import (
 
 func TestSingleUseJsonMarshals(t *testing.T) {
 	rids := []id.Round{1, 5, 9}
-	rl := makeRoundsList(rids)
+	rl := makeRoundsList(rids...)
 	rid := id.NewIdFromString("zezima", id.User, t)
 	eid, _, _, err := ephemeral.GetId(rid, 16, time.Now().UnixNano())
 	if err != nil {
diff --git a/bindings/ud.go b/bindings/ud.go
index c5d8fc2e7..dffd1ed43 100644
--- a/bindings/ud.go
+++ b/bindings/ud.go
@@ -343,7 +343,7 @@ func LookupUD(e2eID int, udContact []byte, cb UdLookupCallback,
 	sr := SingleUseSendReport{
 		EphID:       eid.EphId.Int64(),
 		ReceptionID: eid.Source.Marshal(),
-		RoundsList:  makeRoundsList(rids),
+		RoundsList:  makeRoundsList(rids...),
 	}
 
 	return json.Marshal(sr)
@@ -424,7 +424,7 @@ func SearchUD(e2eID int, udContact []byte, cb UdSearchCallback,
 	sr := SingleUseSendReport{
 		EphID:       eid.EphId.Int64(),
 		ReceptionID: eid.Source.Marshal(),
-		RoundsList:  makeRoundsList(rids),
+		RoundsList:  makeRoundsList(rids...),
 	}
 
 	return json.Marshal(sr)
-- 
GitLab