From 6ec3fcdde997e60288a54d507d11e7182b0cf12d Mon Sep 17 00:00:00 2001 From: josh <josh@elixxir.io> Date: Fri, 6 May 2022 16:19:05 -0700 Subject: [PATCH] Rearrage and clean up connect.go --- {connect => connections/connect}/connect.go | 119 ++++++++++++-------- 1 file changed, 72 insertions(+), 47 deletions(-) rename {connect => connections/connect}/connect.go (73%) diff --git a/connect/connect.go b/connections/connect/connect.go similarity index 73% rename from connect/connect.go rename to connections/connect/connect.go index 6c9fb120e..1282668df 100644 --- a/connect/connect.go +++ b/connections/connect/connect.go @@ -33,7 +33,8 @@ import ( // Connection is a wrapper for the E2E and auth packages. // It can be used to automatically establish an E2E partnership // with a partner.Manager, or be built from an existing E2E partnership. -// You can then use this interface to send to and receive from the newly-established partner.Manager. +// You can then use this interface to send to and receive from the +// newly-established partner.Manager. type Connection interface { // Closer deletes this Connection's partner.Manager and releases resources io.Closer @@ -41,7 +42,8 @@ type Connection interface { // GetPartner returns the partner.Manager for this Connection GetPartner() partner.Manager - // SendE2E is a wrapper for sending specifically to the Connection's partner.Manager + // SendE2E is a wrapper for sending specifically to the Connection's + // partner.Manager SendE2E(mt catalog.MessageType, payload []byte, params clientE2e.Params) ( []id.Round, e2e.MessageID, time.Time, error) @@ -53,24 +55,19 @@ type Connection interface { Unregister(listenerID receive.ListenerID) } -// Callback is the callback format required to retrieve new Connection objects as they are established +// Callback is the callback format required to retrieve +// new Connection objects as they are established. type Callback func(connection Connection) -// handler provides an implementation for the Connection interface -type handler struct { - partner partner.Manager - e2e clientE2e.Handler - params Params -} - -// Params for managing Connection objects +// Params for managing Connection objects. type Params struct { - Auth auth.Param - Rekey rekey.Params - Event event.Reporter + Auth auth.Param + Rekey rekey.Params + Event event.Reporter + Timeout time.Duration } -// GetDefaultParams returns a usable set of default Connection parameters +// GetDefaultParams returns a usable set of default Connection parameters. func GetDefaultParams() Params { return Params{ Auth: auth.GetDefaultParams(), @@ -81,9 +78,11 @@ func GetDefaultParams() Params { // Connect performs auth key negotiation with the given recipient, // and returns a Connection object for the newly-created partner.Manager -// This function is to be used sender-side and will block until the partner.Manager is confirmed -func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int, rng *fastRNG.StreamGenerator, - grp *cyclic.Group, net cmix.Client, p Params) (Connection, error) { +// This function is to be used sender-side and will block until the +// partner.Manager is confirmed. +func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int, + rng *fastRNG.StreamGenerator, grp *cyclic.Group, net cmix.Client, + p Params) (Connection, error) { // Build an ephemeral KV kv := versioned.NewKV(ekv.MakeMemstore()) @@ -119,14 +118,17 @@ func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int, rng *f } // Block waiting for auth to confirm - jww.DEBUG.Printf("Connection waiting for auth request for %s to be confirmed...", recipient.ID.String()) + jww.DEBUG.Printf("Connection waiting for auth request "+ + "for %s to be confirmed...", recipient.ID.String()) newConnection := <-signalChannel // Verify the Connection is complete if newConnection == nil { - return nil, errors.Errorf("Unable to complete connection with partner %s", recipient.ID.String()) + return nil, errors.Errorf("Unable to complete connection "+ + "with partner %s", recipient.ID.String()) } - jww.DEBUG.Printf("Connection auth request for %s confirmed", recipient.ID.String()) + jww.DEBUG.Printf("Connection auth request for %s confirmed", + recipient.ID.String()) return newConnection, nil } @@ -134,8 +136,9 @@ func Connect(recipient contact.Contact, myId *id.ID, privKey *cyclic.Int, rng *f // RegisterConnectionCallback assembles a Connection object on the reception-side // and feeds it into the given Callback whenever an incoming request // for an E2E partnership with a partner.Manager is confirmed. -func RegisterConnectionCallback(cb Callback, myId *id.ID, privKey *cyclic.Int, rng *fastRNG.StreamGenerator, - grp *cyclic.Group, net cmix.Client, p Params) error { +func RegisterConnectionCallback(cb Callback, myId *id.ID, privKey *cyclic.Int, + rng *fastRNG.StreamGenerator, grp *cyclic.Group, net cmix.Client, + p Params) error { // Build an ephemeral KV kv := versioned.NewKV(ekv.MakeMemstore()) @@ -159,9 +162,18 @@ func RegisterConnectionCallback(cb Callback, myId *id.ID, privKey *cyclic.Int, r return err } +// handler provides an implementation for the Connection interface. +type handler struct { + partner partner.Manager + e2e clientE2e.Handler + params Params +} + // BuildConnection assembles a Connection object -// after an E2E partnership has already been confirmed with the given partner.Manager -func BuildConnection(partner partner.Manager, e2eHandler clientE2e.Handler, p Params) Connection { +// after an E2E partnership has already been confirmed with the given +// partner.Manager. +func BuildConnection(partner partner.Manager, e2eHandler clientE2e.Handler, + p Params) Connection { return &handler{ partner: partner, params: p, @@ -169,36 +181,41 @@ func BuildConnection(partner partner.Manager, e2eHandler clientE2e.Handler, p Pa } } -// Close deletes this Connection's partner.Manager and releases resources +// Close deletes this Connection's partner.Manager and releases resources. func (h *handler) Close() error { return h.e2e.DeletePartner(h.partner.PartnerId()) } -// GetPartner returns the partner.Manager for this Connection +// GetPartner returns the partner.Manager for this Connection. func (h *handler) GetPartner() partner.Manager { return h.partner } -// SendE2E is a wrapper for sending specifically to the Connection's partner.Manager -func (h *handler) SendE2E(mt catalog.MessageType, payload []byte, params clientE2e.Params) ( +// SendE2E is a wrapper for sending specifically to the Connection's +// partner.Manager. +func (h *handler) SendE2E(mt catalog.MessageType, payload []byte, + params clientE2e.Params) ( []id.Round, e2e.MessageID, time.Time, error) { return h.e2e.SendE2E(mt, h.partner.PartnerId(), payload, params) } // RegisterListener is used for E2E reception -// and allows for reading data sent from the partner.Manager -func (h *handler) RegisterListener(messageType catalog.MessageType, newListener receive.Listener) receive.ListenerID { - return h.e2e.RegisterListener(h.partner.PartnerId(), messageType, newListener) +// and allows for reading data sent from the partner.Manager. +func (h *handler) RegisterListener(messageType catalog.MessageType, + newListener receive.Listener) receive.ListenerID { + return h.e2e.RegisterListener(h.partner.PartnerId(), + messageType, newListener) } -// Unregister listener for E2E reception +// Unregister listener for E2E reception. func (h *handler) Unregister(listenerID receive.ListenerID) { h.e2e.Unregister(listenerID) } -// authCallback provides callback functionality for interfacing between auth.State and Connection -// This is used both for blocking creation of a Connection object until the auth Request is confirmed -// and for dynamically building new Connection objects when an auth Request is received. +// authCallback provides callback functionality for interfacing between +// auth.State and Connection. This is used both for blocking creation of a +// Connection object until the auth Request is confirmed and for dynamically +// building new Connection objects when an auth Request is received. type authCallback struct { // Used for signaling confirmation of E2E partnership connectionCallback Callback @@ -208,8 +225,10 @@ type authCallback struct { connectionParams Params } -// getAuthCallback returns a callback interface to be passed into the creation of an auth.State object. -func getAuthCallback(cb Callback, e2e clientE2e.Handler, params Params) authCallback { +// getAuthCallback returns a callback interface to be passed into the creation +// of an auth.State object. +func getAuthCallback(cb Callback, e2e clientE2e.Handler, + params Params) authCallback { return authCallback{ connectionCallback: cb, connectionE2e: e2e, @@ -217,27 +236,33 @@ func getAuthCallback(cb Callback, e2e clientE2e.Handler, params Params) authCall } } -// Confirm will be called when an auth Confirm message is processed -func (a authCallback) Confirm(requestor contact.Contact, receptionID receptionID.EphemeralIdentity, round rounds.Round) { - jww.DEBUG.Printf("Connection auth request for %s confirmed", requestor.ID.String()) +// Confirm will be called when an auth Confirm message is processed. +func (a authCallback) Confirm(requestor contact.Contact, + receptionID receptionID.EphemeralIdentity, round rounds.Round) { + jww.DEBUG.Printf("Connection auth request for %s confirmed", + requestor.ID.String()) // After confirmation, get the new partner newPartner, err := a.connectionE2e.GetPartner(requestor.ID) if err != nil { - jww.ERROR.Printf("Unable to build connection with partner %s: %+v", requestor.ID, err) + jww.ERROR.Printf("Unable to build connection with "+ + "partner %s: %+v", requestor.ID, err) // Send a nil connection to avoid hold-ups down the line a.connectionCallback(nil) return } // Return the new Connection object - a.connectionCallback(BuildConnection(newPartner, a.connectionE2e, a.connectionParams)) + a.connectionCallback(BuildConnection(newPartner, a.connectionE2e, + a.connectionParams)) } -// Request will be called when an auth Request message is processed -func (a authCallback) Request(requestor contact.Contact, receptionID receptionID.EphemeralIdentity, round rounds.Round) { +// Request will be called when an auth Request message is processed. +func (a authCallback) Request(requestor contact.Contact, + receptionID receptionID.EphemeralIdentity, round rounds.Round) { } -// Reset will be called when an auth Reset operation occurs -func (a authCallback) Reset(requestor contact.Contact, receptionID receptionID.EphemeralIdentity, round rounds.Round) { +// Reset will be called when an auth Reset operation occurs. +func (a authCallback) Reset(requestor contact.Contact, + receptionID receptionID.EphemeralIdentity, round rounds.Round) { } -- GitLab