From b546741aa8d58a4f932d5868dfcad51bd3403e9d Mon Sep 17 00:00:00 2001
From: "Richard T. Carback III" <rick.carback@gmail.com>
Date: Thu, 21 Apr 2022 18:10:43 +0000
Subject: [PATCH] Fix pickup to use the right ephemeral IDs from the network

---
 cmix/address/addressSpace.go          | 19 ++++++++-----------
 cmix/client.go                        |  6 +++---
 cmix/follow.go                        |  2 ++
 cmix/identity/receptionID/identity.go | 11 ++++++++---
 cmix/identity/tracker.go              |  8 +++++---
 cmix/pickup/pickup.go                 |  5 ++++-
 cmix/sendCmixUtils.go                 | 10 +++++++---
 7 files changed, 37 insertions(+), 24 deletions(-)

diff --git a/cmix/address/addressSpace.go b/cmix/address/addressSpace.go
index c596893aa..be6cd94ab 100644
--- a/cmix/address/addressSpace.go
+++ b/cmix/address/addressSpace.go
@@ -1,15 +1,10 @@
 package address
 
 import (
-	"github.com/pkg/errors"
-	jww "github.com/spf13/jwalterweatherman"
 	"sync"
-)
 
-const (
-	// The initial value for the address space size. This value signifies that
-	// the address space size has not yet been updated.
-	initSize = 1
+	"github.com/pkg/errors"
+	jww "github.com/spf13/jwalterweatherman"
 )
 
 // Space contains the current address space size used for creating address IDs
@@ -24,12 +19,13 @@ type Space interface {
 
 type space struct {
 	size      uint8
+	set       bool
 	notifyMap map[string]chan uint8
 	cond      *sync.Cond
 }
 
 // NewAddressSpace initialises a new Space and returns it.
-func NewAddressSpace() Space {
+func NewAddressSpace(initSize uint8) Space {
 	return &space{
 		size:      initSize,
 		notifyMap: make(map[string]chan uint8),
@@ -44,7 +40,7 @@ func (as *space) GetAddressSpace() uint8 {
 	defer as.cond.L.Unlock()
 
 	// If the size has been set, then return the current size
-	if as.size != initSize {
+	if as.set {
 		return as.size
 	}
 
@@ -71,7 +67,7 @@ func (as *space) UpdateAddressSpace(newSize uint8) {
 	defer as.cond.L.Unlock()
 
 	// Skip Update if the address space size is unchanged
-	if as.size >= newSize {
+	if as.size >= newSize && as.set {
 		return
 	}
 
@@ -81,7 +77,8 @@ func (as *space) UpdateAddressSpace(newSize uint8) {
 	jww.INFO.Printf("Updated address space size from %d to %d", oldSize, as.size)
 
 	// Broadcast that the address space size is set, if set for the first time
-	if oldSize == initSize {
+	if !as.set {
+		as.set = true
 		as.cond.Broadcast()
 	} else {
 		// Broadcast the new address space size to all registered channels
diff --git a/cmix/client.go b/cmix/client.go
index 116d3cbc1..402a5660b 100644
--- a/cmix/client.go
+++ b/cmix/client.go
@@ -106,12 +106,13 @@ func NewClient(params Params, comms *commClient.Comms, session storage.Session,
 
 	tracker := uint64(0)
 	earliest := uint64(0)
+	addrSize := ndf.AddressSpace[len(ndf.AddressSpace)-1].Size
 
 	// Create client object
 	c := &client{
 		param:         params,
 		tracker:       &tracker,
-		Space:         address.NewAddressSpace(),
+		Space:         address.NewAddressSpace(addrSize),
 		events:        events,
 		earliestRound: &earliest,
 		session:       session,
@@ -120,7 +121,6 @@ func NewClient(params Params, comms *commClient.Comms, session storage.Session,
 		instance:      instance,
 		maxMsgLen:     tmpMsg.ContentsSize(),
 	}
-	c.UpdateAddressSpace(18)
 
 	if params.VerboseRoundTracking {
 		c.verboseRounds = NewRoundTracker()
@@ -162,7 +162,7 @@ func NewClient(params Params, comms *commClient.Comms, session storage.Session,
 	// Set up round handler
 	c.Pickup = pickup.NewPickup(
 		params.Pickup, c.Handler.GetMessageReceptionChannel(), c.Sender,
-		c.Retriever, c.rng, c.instance, c.session)
+		c.Retriever, c.comms, c.rng, c.instance, c.session)
 
 	// Add the identity system
 	c.Tracker = identity.NewOrLoadTracker(c.session, c.Space)
diff --git a/cmix/follow.go b/cmix/follow.go
index b3a5bc662..58c693d09 100644
--- a/cmix/follow.go
+++ b/cmix/follow.go
@@ -56,6 +56,8 @@ type followNetworkComms interface {
 	GetHost(hostId *id.ID) (*connect.Host, bool)
 	SendPoll(host *connect.Host, message *pb.GatewayPoll) (
 		*pb.GatewayPollResponse, error)
+	RequestMessages(host *connect.Host, message *pb.GetMessages) (
+		*pb.GetMessagesResponse, error)
 }
 
 // followNetwork polls the network to get updated on the state of nodes, the
diff --git a/cmix/identity/receptionID/identity.go b/cmix/identity/receptionID/identity.go
index 3c7526be3..96767b8a7 100644
--- a/cmix/identity/receptionID/identity.go
+++ b/cmix/identity/receptionID/identity.go
@@ -2,16 +2,18 @@ package receptionID
 
 import (
 	"encoding/json"
+	"strconv"
+	"strings"
+	"time"
+
 	"github.com/pkg/errors"
+	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/cmix/rounds"
 	"gitlab.com/elixxir/client/storage/versioned"
 	"gitlab.com/elixxir/primitives/states"
 	"gitlab.com/xx_network/primitives/id"
 	"gitlab.com/xx_network/primitives/id/ephemeral"
 	"gitlab.com/xx_network/primitives/netTime"
-	"strconv"
-	"strings"
-	"time"
 )
 
 const identityStorageKey = "IdentityStorage"
@@ -123,6 +125,9 @@ func BuildIdentityFromRound(source *id.ID,
 	round rounds.Round) EphemeralIdentity {
 	ephID, _, _, _ := ephemeral.GetId(source, uint(round.AddressSpaceSize),
 		round.Timestamps[states.QUEUED].UnixNano())
+	jww.INFO.Printf("BuildIdentityFromRound for %s: %d %d %d",
+		source, ephID.Int64(), round.AddressSpaceSize,
+		round.Timestamps[states.QUEUED].UnixNano())
 	return EphemeralIdentity{
 		EphId:  ephID,
 		Source: source,
diff --git a/cmix/identity/tracker.go b/cmix/identity/tracker.go
index 0ff74557c..1022940ee 100644
--- a/cmix/identity/tracker.go
+++ b/cmix/identity/tracker.go
@@ -9,12 +9,13 @@ package identity
 
 import (
 	"encoding/json"
-	"github.com/pkg/errors"
 	"io"
 	"io/fs"
 	"sync"
 	"time"
 
+	"github.com/pkg/errors"
+
 	jww "github.com/spf13/jwalterweatherman"
 
 	"gitlab.com/elixxir/client/cmix/address"
@@ -347,11 +348,12 @@ func (t *manager) generateIdentitiesOverRange(inQuestion TrackedID, addressSize
 
 		// Print debug information and set return value
 		if isLastIdentity := i == len(protoIds)-1; isLastIdentity {
-			jww.INFO.Printf("Current Identity: %d (source: %s), Start: %s, End: %s",
+			jww.INFO.Printf("Current Identity: %d (source: %s), Start: %s, End: %s, addrSize: %d",
 				newIdentity.EphId.Int64(),
 				newIdentity.Source,
 				newIdentity.StartValid,
-				newIdentity.EndValid)
+				newIdentity.EndValid,
+				addressSize)
 			lastIdentityEnd = newIdentity.End
 		}
 	}
diff --git a/cmix/pickup/pickup.go b/cmix/pickup/pickup.go
index 73706f1fb..649ab4ae0 100644
--- a/cmix/pickup/pickup.go
+++ b/cmix/pickup/pickup.go
@@ -8,6 +8,8 @@
 package pickup
 
 import (
+	"strconv"
+
 	"gitlab.com/elixxir/client/cmix/gateway"
 	"gitlab.com/elixxir/client/cmix/identity/receptionID"
 	"gitlab.com/elixxir/client/cmix/message"
@@ -17,7 +19,6 @@ import (
 	"gitlab.com/elixxir/client/storage"
 	"gitlab.com/elixxir/crypto/fastRNG"
 	"gitlab.com/xx_network/primitives/id"
-	"strconv"
 )
 
 type Pickup interface {
@@ -46,6 +47,7 @@ type pickup struct {
 
 func NewPickup(params Params, bundles chan<- message.Bundle,
 	sender gateway.Sender, historical rounds.Retriever,
+	comms MessageRetrievalComms,
 	rng *fastRNG.StreamGenerator, instance RoundGetter,
 	session storage.Session) Pickup {
 	unchecked := store.NewOrLoadUncheckedStore(session.GetKV())
@@ -59,6 +61,7 @@ func NewPickup(params Params, bundles chan<- message.Bundle,
 		instance:            instance,
 		unchecked:           unchecked,
 		session:             session,
+		comms:               comms,
 	}
 
 	return m
diff --git a/cmix/sendCmixUtils.go b/cmix/sendCmixUtils.go
index fd3ad4bc7..e1db33471 100644
--- a/cmix/sendCmixUtils.go
+++ b/cmix/sendCmixUtils.go
@@ -8,6 +8,10 @@
 package cmix
 
 import (
+	"strconv"
+	"strings"
+	"time"
+
 	"github.com/pkg/errors"
 	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/cmix/nodes"
@@ -20,9 +24,6 @@ import (
 	"gitlab.com/xx_network/primitives/id"
 	"gitlab.com/xx_network/primitives/id/ephemeral"
 	"gitlab.com/xx_network/primitives/netTime"
-	"strconv"
-	"strings"
-	"time"
 )
 
 // SendCmixCommsInterface is the interface for Send comms; allows mocking
@@ -118,6 +119,9 @@ func buildSlotMessage(msg format.Message, recipient *id.ID, target *id.ID,
 	ephID, _, _, err := ephemeral.GetId(recipient,
 		uint(bestRound.AddressSpaceSize),
 		int64(bestRound.Timestamps[states.QUEUED]))
+	jww.INFO.Printf("buildSlotMessage EphID for %s: %d %d %d", recipient,
+		ephID.Int64(), bestRound.AddressSpaceSize,
+		bestRound.Timestamps[states.QUEUED])
 	if err != nil {
 		jww.FATAL.Panicf("Failed to generate address ID when sending to %s "+
 			"(msgDigest: %s): %+v", err, recipient, msg.Digest())
-- 
GitLab