diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 343f8db7669105b8d56f446deb66f29838502562..51e3b61ecd7b4bb3ba4a755c5c232cf3d2a3d1b7 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,7 +3,7 @@ variables: REPO_DIR: gitlab.com/elixxir REPO_NAME: client - DOCKER_IMAGE: elixxirlabs/cuda-go:go1.13-cuda11.1-mc + DOCKER_IMAGE: elixxirlabs/cuda-go:go1.16-cuda11.1 MIN_CODE_COVERAGE: "35" before_script: @@ -68,7 +68,7 @@ build: - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' ./... - GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/client.linux64 main.go - GOOS=windows GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/client.win64 main.go - - GOOS=windows GOARCH=386 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/client.win32 main.go +# - GOOS=windows GOARCH=386 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/client.win32 main.go - GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -ldflags '-w -s' -o release/client.darwin64 main.go - /upload-artifacts.sh release/ artifacts: @@ -85,25 +85,33 @@ tag: - git tag $(release/client.linux64 version | grep "Elixxir Client v"| cut -d ' ' -f3) -f - git push origin_tags -f --tags -bindings: +bindings-ios: stage: build except: - tags tags: - ios script: - - export PATH="/usr/local/opt/go@1.13/bin:$PATH" - go get -u golang.org/x/mobile/cmd/gomobile - - go get -u golang.org/x/mobile/bind - - rm -rf $HOME/go/src/gitlab.com/elixxir/client/ - - mkdir -p $HOME/go/src/gitlab.com/elixxir/client/ - - cp -r * $HOME/go/src/gitlab.com/elixxir/client/ - - GO111MODULE=on gomobile bind -target android -androidapi 21 gitlab.com/elixxir/client/bindings - - GO111MODULE=on gomobile bind -target ios gitlab.com/elixxir/client/bindings + - gomobile bind -target ios gitlab.com/elixxir/client/bindings - zip -r iOS.zip Bindings.framework artifacts: paths: - iOS.zip + +bindings-android: + stage: build + except: + - tags + tags: + - android + script: + - export ANDROID_HOME=/android-sdk + - export PATH=$PATH:/android-sdk/platform-tools/:/usr/local/go/bin + - go get -u golang.org/x/mobile/cmd/gomobile + - gomobile bind -target android -androidapi 21 gitlab.com/elixxir/client/bindings + artifacts: + paths: - bindings.aar - bindings-sources.jar diff --git a/README.md b/README.md index 3827baea0e49a5792e8bd230f6e90230bcccca9d..497f2c99231e3e0aa8908f4680aa1b76af8f802e 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,9 @@ Flags: base64 representations) (default "0") --forceHistoricalRounds Force all rounds to be sent to historical round retrieval + --forceMessagePickupRetry Enable a mechanism which forces a 50% chance + of no message pickup, instead triggering the + message pickup retry mechanism -h, --help help for client -l, --log string Path to the log output path (- is stdout) (default "-") diff --git a/api/authenticatedChannel.go b/api/authenticatedChannel.go index 88682af983c107345b6415648bec9558f2eb4d6b..24b5a8e4af3c771d59dabc3e01d6ffa1b8125a17 100644 --- a/api/authenticatedChannel.go +++ b/api/authenticatedChannel.go @@ -125,3 +125,17 @@ func (c *Client) MakePrecannedContact(precannedID uint) contact.Contact { Facts: make([]fact.Fact, 0), } } + +// GetRelationshipFingerprint returns a unique 15 character fingerprint for an +// E2E relationship. An error is returned if no relationship with the partner +// is found. +func (c *Client) GetRelationshipFingerprint(partner *id.ID) (string, error) { + m, err := c.storage.E2e().GetPartner(partner) + if err != nil { + return "", errors.Errorf("could not get partner %s: %+v", partner, err) + } else if m == nil { + return "", errors.Errorf("manager for partner %s is nil.", partner) + } + + return m.GetRelationshipFingerprint(), nil +} diff --git a/api/client.go b/api/client.go index 57abc071a3ebc62da7c9c8d23c6be02f7a3aa340..a81bb25352dd90926a2cce68c6601a5f847fcc8e 100644 --- a/api/client.go +++ b/api/client.go @@ -8,10 +8,6 @@ package api import ( - "gitlab.com/xx_network/comms/connect" - "gitlab.com/xx_network/primitives/id" - "time" - "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/auth" @@ -28,12 +24,19 @@ import ( "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/fastRNG" "gitlab.com/elixxir/primitives/version" + "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/crypto/csprng" "gitlab.com/xx_network/crypto/large" "gitlab.com/xx_network/crypto/signature/rsa" + "gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/ndf" + "math" + "sync" + "time" ) +const followerStoppableName = "client" + type Client struct { //generic RNG for client rng *fastRNG.StreamGenerator @@ -64,6 +67,10 @@ type Client struct { services *serviceProcessiesList clientErrorChannel chan interfaces.ClientError + + //lock to ensure only once instance of stop/start network follower is + //going at a time + followerLock sync.Mutex } // NewClient creates client storage, generates keys, connects, and registers @@ -181,7 +188,7 @@ func OpenClient(storageDir string, password []byte, parameters params.Network) ( rng: rngStreamGen, comms: nil, network: nil, - runner: stoppable.NewMulti("client"), + runner: stoppable.NewMulti(followerStoppableName), status: newStatusTracker(), parameters: parameters, } @@ -213,7 +220,7 @@ func Login(storageDir string, password []byte, parameters params.Network) (*Clie } //get the NDF to pass into permissioning and the network manager - def := c.storage.GetBaseNDF() + def := c.storage.GetNDF() //initialize permissioning if def.Registration.Address != "" { @@ -229,6 +236,8 @@ func Login(storageDir string, password []byte, parameters params.Network) (*Clie if def.Notification.Address != "" { hp := connect.GetDefaultHostParams() + // Client will not send KeepAlive packets + hp.KaClientOpts.Time = time.Duration(math.MaxInt64) hp.AuthEnabled = false hp.MaxRetries = 5 _, err = c.comms.AddHost(&id.NotificationBot, def.Notification.Address, []byte(def.Notification.TlsCertificate), hp) @@ -280,7 +289,7 @@ func LoginWithNewBaseNDF_UNSAFE(storageDir string, password []byte, } //store the updated base NDF - c.storage.SetBaseNDF(def) + c.storage.SetNDF(def) //initialize permissioning if def.Registration.Address != "" { @@ -348,6 +357,7 @@ func (c *Client) initPermissioning(def *ndf.NetworkDefinition) error { } // ----- Client Functions ----- + // StartNetworkFollower kicks off the tracking of the network. It starts // long running network client threads and returns an object for checking // state and stopping those threads. @@ -378,11 +388,17 @@ func (c *Client) initPermissioning(def *ndf.NetworkDefinition) error { // Responds to confirmations of successful rekey operations // - Auth Callback (/auth/callback.go) // Handles both auth confirm and requests -func (c *Client) StartNetworkFollower() (<-chan interfaces.ClientError, error) { +func (c *Client) StartNetworkFollower(timeout time.Duration) (<-chan interfaces.ClientError, error) { + c.followerLock.Lock() + defer c.followerLock.Unlock() u := c.GetUser() jww.INFO.Printf("StartNetworkFollower() \n\tTransmisstionID: %s "+ "\n\tReceptionID: %s", u.TransmissionID, u.ReceptionID) + if status := c.status.get(); status != Stopped { + return nil, errors.Errorf("Cannot Stop the Network Follower when it is not running, status: %s", status) + } + c.clientErrorChannel = make(chan interfaces.ClientError, 1000) cer := func(source, message, trace string) { @@ -397,12 +413,21 @@ func (c *Client) StartNetworkFollower() (<-chan interfaces.ClientError, error) { } } - err := c.status.toStarting() + // Wait for any threads from the previous follower to close and then create + // a new stoppable + err := stoppable.WaitForStopped(c.runner, timeout) + if err != nil { + return nil, err + } else { + c.runner = stoppable.NewMulti(followerStoppableName) + } + + err = c.status.toStarting() if err != nil { return nil, errors.WithMessage(err, "Failed to Start the Network Follower") } - stopAuth := c.auth.StartProcessies() + stopAuth := c.auth.StartProcesses() c.runner.Add(stopAuth) stopFollow, err := c.network.Follow(cer) @@ -429,13 +454,19 @@ func (c *Client) StartNetworkFollower() (<-chan interfaces.ClientError, error) { // fails to stop it. // if the network follower is running and this fails, the client object will // most likely be in an unrecoverable state and need to be trashed. -func (c *Client) StopNetworkFollower(timeout time.Duration) error { +func (c *Client) StopNetworkFollower() error { + c.followerLock.Lock() + defer c.followerLock.Unlock() + + if status := c.status.get(); status != Running { + return errors.Errorf("Cannot Stop the Network Follower when it is not running, status: %s", status) + } + err := c.status.toStopping() if err != nil { return errors.WithMessage(err, "Failed to Stop the Network Follower") } - err = c.runner.Close(timeout) - c.runner = stoppable.NewMulti("client") + err = c.runner.Close() err2 := c.status.toStopped() if err2 != nil { if err == nil { @@ -593,7 +624,7 @@ func checkVersionAndSetupStorage(def *ndf.NetworkDefinition, storageDir string, } // Save NDF to be used in the future - storageSess.SetBaseNDF(def) + storageSess.SetNDF(def) if !isPrecanned { //store the registration code for later use diff --git a/api/results.go b/api/results.go index 590634b9646f2b92f3b56e29d422145806807d66..d87c538c38ecb9f8baebe489815cca148870d616 100644 --- a/api/results.go +++ b/api/results.go @@ -12,7 +12,6 @@ import ( jww "github.com/spf13/jwalterweatherman" pb "gitlab.com/elixxir/comms/mixmessages" - "gitlab.com/elixxir/comms/network" ds "gitlab.com/elixxir/comms/network/dataStructures" "gitlab.com/elixxir/primitives/states" "gitlab.com/xx_network/comms/connect" @@ -131,7 +130,7 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, // Find out what happened to old (historical) rounds if any are needed if len(historicalRequest.Rounds) > 0 { - go c.getHistoricalRounds(historicalRequest, networkInstance, sendResults, commsInterface) + go c.getHistoricalRounds(historicalRequest, sendResults, commsInterface) } // Determine the results of all rounds requested @@ -180,7 +179,7 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, // Helper function which asynchronously pings a random gateway until // it gets information on it's requested historical rounds func (c *Client) getHistoricalRounds(msg *pb.HistoricalRounds, - instance *network.Instance, sendResults chan ds.EventReturn, comms historicalRoundsComm) { + sendResults chan ds.EventReturn, comms historicalRoundsComm) { var resp *pb.HistoricalRoundsResponse @@ -189,7 +188,7 @@ func (c *Client) getHistoricalRounds(msg *pb.HistoricalRounds, // Find a gateway to request about the roundRequests result, err := c.GetNetworkInterface().GetSender().SendToAny(func(host *connect.Host) (interface{}, error) { return comms.RequestHistoricalRounds(host, msg) - }) + }, nil) // If an error, retry with (potentially) a different gw host. // If no error from received gateway request, exit loop diff --git a/api/send.go b/api/send.go index 19d6a54b853f911e47af7d2d9bcafa46e2b7bd4d..5ef62966daf88579091196898ce4e661583696e3 100644 --- a/api/send.go +++ b/api/send.go @@ -27,7 +27,7 @@ func (c *Client) SendE2E(m message.Send, param params.E2E) ([]id.Round, e2e.MessageID, error) { jww.INFO.Printf("SendE2E(%s, %d. %v)", m.Recipient, m.MessageType, m.Payload) - return c.network.SendE2E(m, param) + return c.network.SendE2E(m, param, nil) } // SendUnsafe sends an unencrypted payload to the provided recipient @@ -52,6 +52,14 @@ func (c *Client) SendCMIX(msg format.Message, recipientID *id.ID, return c.network.SendCMIX(msg, recipientID, param) } +// SendManyCMIX sends many "raw" CMIX message payloads to each of the +// provided recipients. Used for group chat functionality. Returns the +// round ID of the round the payload was sent or an error if it fails. +func (c *Client) SendManyCMIX(messages map[id.ID]format.Message, + params params.CMIX) (id.Round, []ephemeral.Id, error) { + return c.network.SendManyCMIX(messages, params) +} + // NewCMIXMessage Creates a new cMix message with the right properties // for the current cMix network. // FIXME: this is weird and shouldn't be necessary, but it is. diff --git a/api/utilsInterfaces_test.go b/api/utilsInterfaces_test.go index 62ab16f5231d0bdc81ab30f8e0a1b890fb0931c7..1faabce01353d23af8665858b4561e8aeb856836 100644 --- a/api/utilsInterfaces_test.go +++ b/api/utilsInterfaces_test.go @@ -93,7 +93,7 @@ func (t *testNetworkManagerGeneric) Follow(report interfaces.ClientErrorReport) func (t *testNetworkManagerGeneric) CheckGarbledMessages() { return } -func (t *testNetworkManagerGeneric) SendE2E(m message.Send, p params.E2E) ( +func (t *testNetworkManagerGeneric) SendE2E(message.Send, params.E2E, *stoppable.Single) ( []id.Round, cE2e.MessageID, error) { rounds := []id.Round{id.Round(0), id.Round(1), id.Round(2)} return rounds, cE2e.MessageID{}, nil @@ -105,6 +105,9 @@ func (t *testNetworkManagerGeneric) SendUnsafe(m message.Send, p params.Unsafe) func (t *testNetworkManagerGeneric) SendCMIX(message format.Message, rid *id.ID, p params.CMIX) (id.Round, ephemeral.Id, error) { return id.Round(0), ephemeral.Id{}, nil } +func (t *testNetworkManagerGeneric) SendManyCMIX(messages map[id.ID]format.Message, p params.CMIX) (id.Round, []ephemeral.Id, error) { + return 0, []ephemeral.Id{}, nil +} func (t *testNetworkManagerGeneric) GetInstance() *network.Instance { return t.instance } @@ -125,3 +128,11 @@ func (t *testNetworkManagerGeneric) InProgressRegistrations() int { func (t *testNetworkManagerGeneric) GetSender() *gateway.Sender { return t.sender } + +func (t *testNetworkManagerGeneric) GetAddressSize() uint8 { return 0 } + +func (t *testNetworkManagerGeneric) RegisterAddressSizeNotification(string) (chan uint8, error) { + return nil, nil +} + +func (t *testNetworkManagerGeneric) UnregisterAddressSizeNotification(string) {} diff --git a/api/version_vars.go b/api/version_vars.go index 25387358190275af6114f57baee417c297d9fb20..494f2d6a7cee377c706e92d3268951f85a464490 100644 --- a/api/version_vars.go +++ b/api/version_vars.go @@ -1,17 +1,17 @@ // Code generated by go generate; DO NOT EDIT. // This file was generated by robots at -// 2021-05-20 15:24:04.3341818 -0700 PDT m=+0.193117301 +// 2021-06-22 11:16:35.397077 -0500 CDT m=+0.025546669 package api -const GITVERSION = `4f3b5a13 update deps` -const SEMVER = "2.6.0" +const GITVERSION = `b7692cd7 added Keepalive opts` +const SEMVER = "2.7.0" const DEPENDENCIES = `module gitlab.com/elixxir/client go 1.13 require ( github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 - github.com/golang/protobuf v1.4.3 + github.com/golang/protobuf v1.5.2 github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect github.com/magiconair/properties v1.8.4 // indirect github.com/mitchellh/mapstructure v1.4.0 // indirect @@ -24,21 +24,18 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 github.com/spf13/viper v1.7.1 gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228 - gitlab.com/elixxir/comms v0.0.4-0.20210519214834-4b27f37412f1 - gitlab.com/elixxir/crypto v0.0.7-0.20210519214631-6e1aedaf8d0c + gitlab.com/elixxir/comms v0.0.4-0.20210622161439-b694033c9507 + gitlab.com/elixxir/crypto v0.0.7-0.20210614155844-c1e9c23a6ba7 gitlab.com/elixxir/ekv v0.1.5 - gitlab.com/elixxir/primitives v0.0.3-0.20210520220650-16cb34e6b7e3 - gitlab.com/xx_network/comms v0.0.4-0.20210517205649-06ddfa8d2a75 - gitlab.com/xx_network/crypto v0.0.5-0.20210517205543-4ae99cbb9063 - gitlab.com/xx_network/primitives v0.0.4-0.20210517202253-c7b4bd0087ea + gitlab.com/elixxir/primitives v0.0.3-0.20210614155726-ebcf2d47a527 + gitlab.com/xx_network/comms v0.0.4-0.20210622161535-4f3d927d4c8c + gitlab.com/xx_network/crypto v0.0.5-0.20210614155554-8c333814205b + gitlab.com/xx_network/primitives v0.0.4-0.20210617180018-6472489fd418 golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 - golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 - golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect + golang.org/x/net v0.0.0-20210525063256-abc453219eb5 google.golang.org/genproto v0.0.0-20210105202744-fe13368bc0e1 // indirect - google.golang.org/grpc v1.34.0 // indirect - google.golang.org/protobuf v1.26.0-rc.1 + google.golang.org/protobuf v1.26.0 gopkg.in/ini.v1 v1.62.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) - -replace google.golang.org/grpc => github.com/grpc/grpc-go v1.27.1 ` diff --git a/auth/callback.go b/auth/callback.go index 2c39824cb8631b222d6140fd66d2e9f143873639..107887b5542ed6f4184f4e07de858444ca0750f4 100644 --- a/auth/callback.go +++ b/auth/callback.go @@ -23,20 +23,21 @@ import ( "strings" ) -func (m *Manager) StartProcessies() stoppable.Stoppable { - +func (m *Manager) StartProcesses() stoppable.Stoppable { stop := stoppable.NewSingle("Auth") go func() { for { select { case <-stop.Quit(): + stop.ToStopped() return case msg := <-m.rawMessages: m.processAuthMessage(msg) } } }() + return stop } @@ -69,7 +70,7 @@ func (m *Manager) processAuthMessage(msg message.Receive) { case auth.Specific: // if it is specific, that means the original request was sent // by this users and a confirmation has been received - jww.INFO.Printf("Received AutConfirm from %s, msgDigest: %s", + jww.INFO.Printf("Received AuthConfirm from %s, msgDigest: %s", sr.GetPartner(), cmixMsg.Digest()) m.handleConfirm(cmixMsg, sr, grp) } @@ -132,7 +133,7 @@ func (m *Manager) handleRequest(cmixMsg format.Message, // confirmation in case there are state issues. // do not store if _, err := m.storage.E2e().GetPartner(partnerID); err == nil { - jww.WARN.Printf("Recieved Auth request for %s, "+ + jww.WARN.Printf("Received Auth request for %s, "+ "channel already exists. Ignoring", partnerID) //exit return @@ -140,8 +141,8 @@ func (m *Manager) handleRequest(cmixMsg format.Message, //check if the relationship already exists, rType, sr2, _, err := m.storage.Auth().GetRequest(partnerID) if err != nil && !strings.Contains(err.Error(), auth.NoRequest) { - // if another error is recieved, print it and exit - jww.WARN.Printf("Recieved new Auth request for %s, "+ + // if another error is received, print it and exit + jww.WARN.Printf("Received new Auth request for %s, "+ "internal lookup produced bad result: %+v", partnerID, err) return diff --git a/bindings/authenticatedChannels.go b/bindings/authenticatedChannels.go index 30b12f0a1b3039fba5b18eafeff874cc7a438a60..da1d0d7ea128c15a6e4d407f8a7a88430666fd16 100644 --- a/bindings/authenticatedChannels.go +++ b/bindings/authenticatedChannels.go @@ -11,6 +11,7 @@ import ( "errors" "fmt" "gitlab.com/elixxir/crypto/contact" + "gitlab.com/xx_network/primitives/id" ) // Create an insecure e2e relationship with a precanned user @@ -123,3 +124,15 @@ func (c *Client) VerifyOwnership(receivedMarshaled, verifiedMarshaled []byte) (b return c.api.VerifyOwnership(received, verified), nil } + +// GetRelationshipFingerprint returns a unique 15 character fingerprint for an +// E2E relationship. An error is returned if no relationship with the partner +// is found. +func (c *Client) GetRelationshipFingerprint(partnerID []byte) (string, error) { + partner, err := id.Unmarshal(partnerID) + if err != nil { + return "", err + } + + return c.api.GetRelationshipFingerprint(partner) +} diff --git a/bindings/callback.go b/bindings/callback.go index a6526d24d3ba961db2b79bdc2fbd6a1b950821dc..897ddcfc68ee19c9e967076ba52764f63fa5f667 100644 --- a/bindings/callback.go +++ b/bindings/callback.go @@ -16,7 +16,7 @@ import ( // Listener provides a callback to hear a message // An object implementing this interface can be called back when the client -// gets a message of the type that the regi sterer specified at registration +// gets a message of the type that the registerer specified at registration // time. type Listener interface { // Hear is called to receive a message in the UI diff --git a/bindings/client.go b/bindings/client.go index 33ef281983799bab25a50663a71b151749ba3535..432ba95160cf5373b8664e044b5d61e3098a60d5 100644 --- a/bindings/client.go +++ b/bindings/client.go @@ -198,8 +198,9 @@ func UnmarshalSendReport(b []byte) (*SendReport, error) { // Responds to sent rekeys and executes them // - KeyExchange Confirm (/keyExchange/confirm.go) // Responds to confirmations of successful rekey operations -func (c *Client) StartNetworkFollower(clientError ClientError) error { - errChan, err := c.api.StartNetworkFollower() +func (c *Client) StartNetworkFollower(clientError ClientError, timeoutMS int) error { + timeout := time.Duration(timeoutMS) * time.Millisecond + errChan, err := c.api.StartNetworkFollower(timeout) if err != nil { return errors.New(fmt.Sprintf("Failed to start the "+ "network follower: %+v", err)) @@ -218,9 +219,8 @@ func (c *Client) StartNetworkFollower(clientError ClientError) error { // fails to stop it. // if the network follower is running and this fails, the client object will // most likely be in an unrecoverable state and need to be trashed. -func (c *Client) StopNetworkFollower(timeoutMS int) error { - timeout := time.Duration(timeoutMS) * time.Millisecond - if err := c.api.StopNetworkFollower(timeout); err != nil { +func (c *Client) StopNetworkFollower() error { + if err := c.api.StopNetworkFollower(); err != nil { return errors.New(fmt.Sprintf("Failed to stop the "+ "network follower: %+v", err)) } @@ -232,7 +232,7 @@ func (c *Client) StopNetworkFollower(timeoutMS int) error { func (c *Client) WaitForNetwork(timeoutMS int) bool { start := netTime.Now() timeout := time.Duration(timeoutMS) * time.Millisecond - for netTime.Now().Sub(start) < timeout { + for netTime.Since(start) < timeout { if c.api.GetHealth().IsHealthy() { return true } @@ -256,10 +256,15 @@ func (c *Client) IsNetworkHealthy() bool { return c.api.GetHealth().IsHealthy() } -// registers the network health callback to be called any time the network -// health changes -func (c *Client) RegisterNetworkHealthCB(nhc NetworkHealthCallback) { - c.api.GetHealth().AddFunc(nhc.Callback) +// RegisterNetworkHealthCB registers the network health callback to be called +// any time the network health changes. Returns a unique ID that can be used to +// unregister the network health callback. +func (c *Client) RegisterNetworkHealthCB(nhc NetworkHealthCallback) int64 { + return int64(c.api.GetHealth().AddFunc(nhc.Callback)) +} + +func (c *Client) UnregisterNetworkHealthCB(funcID int64) { + c.api.GetHealth().RemoveFunc(uint64(funcID)) } // RegisterListener records and installs a listener for messages diff --git a/bindings/group.go b/bindings/group.go new file mode 100644 index 0000000000000000000000000000000000000000..8752c6665dac3461c8c452d9fbdb558a2f834899 --- /dev/null +++ b/bindings/group.go @@ -0,0 +1,298 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package bindings + +import ( + "github.com/pkg/errors" + gc "gitlab.com/elixxir/client/groupChat" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/primitives/id" +) + +// GroupChat object contains the group chat manager. +type GroupChat struct { + m *gc.Manager +} + +// GroupRequestFunc contains a function callback that is called when a group +// request is received. +type GroupRequestFunc interface { + GroupRequestCallback(g Group) +} + +// GroupReceiveFunc contains a function callback that is called when a group +// message is received. +type GroupReceiveFunc interface { + GroupReceiveCallback(msg GroupMessageReceive) +} + +// NewGroupManager creates a new group chat manager. +func NewGroupManager(client *Client, requestFunc GroupRequestFunc, + receiveFunc GroupReceiveFunc) (GroupChat, error) { + + requestCallback := func(g gs.Group) { + requestFunc.GroupRequestCallback(Group{g}) + } + receiveCallback := func(msg gc.MessageReceive) { + receiveFunc.GroupReceiveCallback(GroupMessageReceive{msg}) + } + + // Create a new group chat manager + m, err := gc.NewManager(&client.api, requestCallback, receiveCallback) + if err != nil { + return GroupChat{}, err + } + + // Start group request and message retrieval workers + client.api.AddService(m.StartProcesses) + + return GroupChat{m}, nil +} + +// MakeGroup creates a new group and sends a group request to all members in the +// group. The ID of the new group, the rounds the requests were sent on, and the +// status of the send are contained in NewGroupReport. +func (g GroupChat) MakeGroup(membership IdList, name, message []byte) (NewGroupReport, error) { + grp, rounds, status, err := g.m.MakeGroup(membership.list, name, message) + return NewGroupReport{Group{grp}, rounds, status}, err +} + +// ResendRequest resends a group request to all members in the group. The rounds +// they were sent on and the status of the send are contained in NewGroupReport. +func (g GroupChat) ResendRequest(groupIdBytes []byte) (NewGroupReport, error) { + groupID, err := id.Unmarshal(groupIdBytes) + if err != nil { + return NewGroupReport{}, + errors.Errorf("Failed to unmarshal group ID: %+v", err) + } + + rounds, status, err := g.m.ResendRequest(groupID) + + return NewGroupReport{Group{}, rounds, status}, nil +} + +// JoinGroup allows a user to join a group when they receive a request. The +// caller must pass in the serialized bytes of a Group. +func (g GroupChat) JoinGroup(serializedGroupData []byte) error { + grp, err := gs.DeserializeGroup(serializedGroupData) + if err != nil { + return err + } + return g.m.JoinGroup(grp) +} + +// LeaveGroup deletes a group so a user no longer has access. +func (g GroupChat) LeaveGroup(groupIdBytes []byte) error { + groupID, err := id.Unmarshal(groupIdBytes) + if err != nil { + return errors.Errorf("Failed to unmarshal group ID: %+v", err) + } + + return g.m.LeaveGroup(groupID) +} + +// Send sends the message to the specified group. Returns the round the messages +// were sent on. +func (g GroupChat) Send(groupIdBytes, message []byte) (int64, error) { + groupID, err := id.Unmarshal(groupIdBytes) + if err != nil { + return 0, errors.Errorf("Failed to unmarshal group ID: %+v", err) + } + + round, err := g.m.Send(groupID, message) + return int64(round), err +} + +// GetGroups returns an IdList containing a list of group IDs that the user is a +// part of. +func (g GroupChat) GetGroups() IdList { + return IdList{g.m.GetGroups()} +} + +// GetGroup returns the group with the group ID. If no group exists, then the +// error "failed to find group" is returned. +func (g GroupChat) GetGroup(groupIdBytes []byte) (Group, error) { + groupID, err := id.Unmarshal(groupIdBytes) + if err != nil { + return Group{}, errors.Errorf("Failed to unmarshal group ID: %+v", err) + } + + grp, exists := g.m.GetGroup(groupID) + if !exists { + return Group{}, errors.New("failed to find group") + } + + return Group{grp}, nil +} + +// NumGroups returns the number of groups the user is a part of. +func (g GroupChat) NumGroups() int { + return g.m.NumGroups() +} + +// NewGroupReport is returned when creating a new group and contains the ID of +// the group, a list of rounds that the group requests were sent on, and the +// status of the send. +type NewGroupReport struct { + group Group + rounds []id.Round + status gc.RequestStatus +} + +// GetGroup returns the Group. +func (ngr NewGroupReport) GetGroup() Group { + return ngr.group +} + +// GetRoundList returns the RoundList containing a list of rounds requests were +// sent on. +func (ngr NewGroupReport) GetRoundList() RoundList { + return RoundList{ngr.rounds} +} + +// GetStatus returns the status of the requests sent when creating a new group. +// status = 0 an error occurred before any requests could be sent +// 1 all requests failed to send +// 2 some request failed and some succeeded +// 3, all requests sent successfully +func (ngr NewGroupReport) GetStatus() int { + return int(ngr.status) +} + +//// +// Group Structure +//// + +// Group structure contains the identifying and membership information of a +// group chat. +type Group struct { + g gs.Group +} + +// GetName returns the name set by the user for the group. +func (g Group) GetName() []byte { + return g.g.Name +} + +// GetID return the 33-byte unique group ID. +func (g Group) GetID() []byte { + return g.g.ID.Bytes() +} + +// GetMembership returns a list of contacts, one for each member in the group. +// The list is in order; the first contact is the leader/creator of the group. +// All subsequent members are ordered by their ID. +func (g Group) GetMembership() GroupMembership { + return GroupMembership{g.g.Members} +} + +// Serialize serializes the Group. +func (g Group) Serialize() []byte { + return g.g.Serialize() +} + +//// +// Membership Structure +//// + +// GroupMembership structure contains a list of members that are part of a +// group. The first member is the group leader. +type GroupMembership struct { + m group.Membership +} + +// Len returns the number of members in the group membership. +func (gm GroupMembership) Len() int { + return gm.Len() +} + +// Get returns the member at the index. The member at index 0 is always the +// group leader. An error is returned if the index is out of range. +func (gm GroupMembership) Get(i int) (GroupMember, error) { + if i < 0 || i > gm.Len() { + return GroupMember{}, errors.Errorf("ID list index must be between %d "+ + "and the last element %d.", 0, gm.Len()) + } + return GroupMember{gm.m[i]}, nil +} + +//// +// Member Structure +//// +// GroupMember represents a member in the group membership list. +type GroupMember struct { + group.Member +} + +// GetID returns the 33-byte user ID of the member. +func (gm GroupMember) GetID() []byte { + return gm.ID.Bytes() +} + +// GetDhKey returns the byte representation of the public Diffie–Hellman key of +// the member. +func (gm GroupMember) GetDhKey() []byte { + return gm.DhKey.Bytes() +} + +//// +// Message Receive Structure +//// + +// GroupMessageReceive contains a group message, its ID, and its data that a +// user receives. +type GroupMessageReceive struct { + gc.MessageReceive +} + +// GetGroupID returns the 33-byte group ID. +func (gmr GroupMessageReceive) GetGroupID() []byte { + return gmr.GroupID.Bytes() +} + +// GetMessageID returns the message ID. +func (gmr GroupMessageReceive) GetMessageID() []byte { + return gmr.ID.Bytes() +} + +// GetPayload returns the message payload. +func (gmr GroupMessageReceive) GetPayload() []byte { + return gmr.Payload +} + +// GetSenderID returns the 33-byte user ID of the sender. +func (gmr GroupMessageReceive) GetSenderID() []byte { + return gmr.SenderID.Bytes() +} + +// GetRecipientID returns the 33-byte user ID of the recipient. +func (gmr GroupMessageReceive) GetRecipientID() []byte { + return gmr.RecipientID.Bytes() +} + +// GetEphemeralID returns the ephemeral ID of the recipient. +func (gmr GroupMessageReceive) GetEphemeralID() int64 { + return gmr.EphemeralID.Int64() +} + +// GetTimestampNano returns the message timestamp in nanoseconds. +func (gmr GroupMessageReceive) GetTimestampNano() int64 { + return gmr.Timestamp.UnixNano() +} + +// GetRoundID returns the ID of the round the message was sent on. +func (gmr GroupMessageReceive) GetRoundID() int64 { + return int64(gmr.RoundID) +} + +// GetRoundTimestampNano returns the timestamp, in nanoseconds, of the round the +// message was sent on. +func (gmr GroupMessageReceive) GetRoundTimestampNano() int64 { + return gmr.RoundTimestamp.UnixNano() +} diff --git a/bindings/list.go b/bindings/list.go index c44fb1679fc0e6492c7c711e7d610bab01198c78..a97df24f299d994380d46de8ae9971ad9133c1e7 100644 --- a/bindings/list.go +++ b/bindings/list.go @@ -8,7 +8,7 @@ package bindings import ( - "errors" + "github.com/pkg/errors" "gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/primitives/fact" "gitlab.com/xx_network/primitives/id" @@ -115,3 +115,41 @@ func (fl *FactList) Add(factData string, factType int) error { func (fl *FactList) Stringify() (string, error) { return fl.c.Facts.Stringify(), nil } + +/* ID list */ +// IdList contains a list of IDs. +type IdList struct { + list []*id.ID +} + +// MakeIdList creates a new empty IdList. +func MakeIdList() IdList { + return IdList{[]*id.ID{}} +} + +// Len returns the number of IDs in the list. +func (idl IdList) Len() int { + return len(idl.list) +} + +// Add appends the ID bytes to the end of the list. +func (idl IdList) Add(idBytes []byte) error { + newID, err := id.Unmarshal(idBytes) + if err != nil { + return err + } + + idl.list = append(idl.list, newID) + return nil +} + +// Get returns the ID at the index. An error is returned if the index is out of +// range. +func (idl IdList) Get(i int) ([]byte, error) { + if i < 0 || i > len(idl.list) { + return nil, errors.Errorf("ID list index must be between %d and the "+ + "last element %d.", 0, len(idl.list)) + } + + return idl.list[i].Bytes(), nil +} diff --git a/bindings/message.go b/bindings/message.go index 32947d5fc3b0fc7e87579bdacd39c1bc69edb404..44526fa2e3756868e1b895452d1484f1ff4366fa 100644 --- a/bindings/message.go +++ b/bindings/message.go @@ -17,33 +17,51 @@ type Message struct { r message.Receive } -//Returns the id of the message +// GetID returns the id of the message func (m *Message) GetID() []byte { return m.r.ID[:] } -// Returns the message's sender ID, if available +// GetSender returns the message's sender ID, if available func (m *Message) GetSender() []byte { return m.r.Sender.Bytes() } -// Returns the message's payload/contents +// GetPayload returns the message's payload/contents func (m *Message) GetPayload() []byte { return m.r.Payload } -// Returns the message's type +// GetMessageType returns the message's type func (m *Message) GetMessageType() int { return int(m.r.MessageType) } -// Returns the message's timestamp in ms +// GetTimestampMS returns the message's timestamp in milliseconds func (m *Message) GetTimestampMS() int64 { ts := m.r.Timestamp.UnixNano() ts = (ts + 999999) / 1000000 return ts } +// GetTimestampNano returns the message's timestamp in nanoseconds func (m *Message) GetTimestampNano() int64 { return m.r.Timestamp.UnixNano() } + +// GetRoundTimestampMS returns the message's round timestamp in milliseconds +func (m *Message) GetRoundTimestampMS() int64 { + ts := m.r.RoundTimestamp.UnixNano() + ts = (ts + 999999) / 1000000 + return ts +} + +// GetRoundTimestampNano returns the message's round timestamp in nanoseconds +func (m *Message) GetRoundTimestampNano() int64 { + return m.r.RoundTimestamp.UnixNano() +} + +// GetRoundId returns the message's round ID +func (m *Message) GetRoundId() int64 { + return int64(m.r.RoundId) +} diff --git a/bindings/params.go b/bindings/params.go index 35afbb8901cfd5e8309f8be7a30a14c907496c23..d4896a2e9e89c49c177689c85ff0a2cf410f9277 100644 --- a/bindings/params.go +++ b/bindings/params.go @@ -13,22 +13,22 @@ import ( "gitlab.com/elixxir/client/interfaces/params" ) -func (c *Client) GetCMIXParams() (string, error) { +func GetCMIXParams() (string, error) { p, err := params.GetDefaultCMIX().Marshal() return string(p), err } -func (c *Client) GetE2EParams() (string, error) { +func GetE2EParams() (string, error) { p, err := params.GetDefaultE2E().Marshal() return string(p), err } -func (c *Client) GetNetworkParams() (string, error) { +func GetNetworkParams() (string, error) { p, err := params.GetDefaultNetwork().Marshal() return string(p), err } -func (c *Client) GetUnsafeParams() (string, error) { +func GetUnsafeParams() (string, error) { p, err := params.GetDefaultUnsafe().Marshal() return string(p), err } diff --git a/bindings/send.go b/bindings/send.go index 886bf0ea819557c50ddef4b41846d0ae3d5a5d00..586100d051b0f4f0248b235630111476b87b930f 100644 --- a/bindings/send.go +++ b/bindings/send.go @@ -57,6 +57,53 @@ func (c *Client) SendCmix(recipient, contents []byte, parameters string) (int, e return int(rid), nil } +// SendManyCMIX sends many "raw" CMIX message payloads to each of the +// provided recipients. Used for group chat functionality. Returns the +// round ID of the round the payload was sent or an error if it fails. +// This will return an error if: +// - any recipient ID is invalid +// - any of the the message contents are too long for the message structure +// - the message cannot be sent + +// This will return the round the message was sent on if it is successfully sent +// This can be used to register a round event to learn about message delivery. +// on failure a round id of -1 is returned +// fixme: cannot use a slice of slices over bindings. Will need to modify this function once +// a proper input format has been specified +//func (c *Client) SendManyCMIX(recipients, contents [][]byte, parameters string) (int, error) { +// +// p, err := params.GetCMIXParameters(parameters) +// if err != nil { +// return -1, errors.New(fmt.Sprintf("Failed to sendCmix: %+v", +// err)) +// } +// +// // Build messages +// messages := make(map[id.ID]format.Message, len(contents)) +// for i := 0; i < len(contents); i++ { +// msg, err := c.api.NewCMIXMessage(contents[i]) +// if err != nil { +// return -1, errors.New(fmt.Sprintf("Failed to sendCmix: %+v", +// err)) +// } +// +// u, err := id.Unmarshal(recipients[i]) +// if err != nil { +// return -1, errors.New(fmt.Sprintf("Failed to sendCmix: %+v", +// err)) +// } +// +// messages[*u] = msg +// } +// +// rid, _, err := c.api.SendManyCMIX(messages, p) +// if err != nil { +// return -1, errors.New(fmt.Sprintf("Failed to sendCmix: %+v", +// err)) +// } +// return int(rid), nil +//} + // SendUnsafe sends an unencrypted payload to the provided recipient // with the provided msgType. Returns the list of rounds in which parts // of the message were sent or an error if it fails. diff --git a/cmd/getndf.go b/cmd/getndf.go index 7ac150020c911d695f646993e3a409c18d6cb8da..7562acd153adc1257667faacb4d4a0ccbfbc2e74 100644 --- a/cmd/getndf.go +++ b/cmd/getndf.go @@ -70,8 +70,8 @@ var getNDFCmd = &cobra.Command{ Partial: &pb.NDFHash{ Hash: nil, }, - LastUpdate: uint64(0), - ReceptionID: dummyID[:], + LastUpdate: uint64(0), + ReceptionID: dummyID[:], ClientVersion: []byte(api.SEMVER), } resp, err := comms.SendPoll(host, pollMsg) diff --git a/cmd/group.go b/cmd/group.go new file mode 100644 index 0000000000000000000000000000000000000000..b8064fcb67df8c94c334017023449d1c6e8f99e9 --- /dev/null +++ b/cmd/group.go @@ -0,0 +1,345 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +// The group subcommand allows creation and sending messages to groups + +package cmd + +import ( + "bufio" + "fmt" + "github.com/spf13/cobra" + jww "github.com/spf13/jwalterweatherman" + "github.com/spf13/viper" + "gitlab.com/elixxir/client/api" + "gitlab.com/elixxir/client/groupChat" + "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/xx_network/primitives/id" + "os" + "time" +) + +// groupCmd represents the base command when called without any subcommands +var groupCmd = &cobra.Command{ + Use: "group", + Short: "Group commands for cMix client", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + + client := initClient() + + // Print user's reception ID + user := client.GetUser() + jww.INFO.Printf("User: %s", user.ReceptionID) + + _, _ = initClientCallbacks(client) + + _, err := client.StartNetworkFollower(5 * time.Second) + if err != nil { + jww.FATAL.Panicf("%+v", err) + } + + // Initialize the group chat manager + groupManager, recChan, reqChan := initGroupManager(client) + + // Wait until connected or crash on timeout + connected := make(chan bool, 10) + client.GetHealth().AddChannel(connected) + waitUntilConnected(connected) + + // After connection, make sure we have registered with at least 85% of + // the nodes + for numReg, total := 1, 100; numReg < (total*3)/4; { + time.Sleep(1 * time.Second) + numReg, total, err = client.GetNodeRegistrationStatus() + if err != nil { + jww.FATAL.Panicf("%+v", err) + } + + jww.INFO.Printf("Registering with nodes (%d/%d)...", numReg, total) + } + + // Get group message and name + msgBody := []byte(viper.GetString("message")) + name := []byte(viper.GetString("name")) + timeout := viper.GetDuration("receiveTimeout") + + if viper.IsSet("create") { + filePath := viper.GetString("create") + createGroup(name, msgBody, filePath, groupManager) + } + + if viper.IsSet("resend") { + groupIdString := viper.GetString("resend") + resendRequests(groupIdString, groupManager) + } + + if viper.GetBool("join") { + joinGroup(reqChan, timeout, groupManager) + } + + if viper.IsSet("leave") { + groupIdString := viper.GetString("leave") + leaveGroup(groupIdString, groupManager) + } + + if viper.IsSet("sendMessage") { + groupIdString := viper.GetString("sendMessage") + sendGroup(groupIdString, msgBody, groupManager) + } + + if viper.IsSet("wait") { + numMessages := viper.GetUint("wait") + messageWait(numMessages, timeout, recChan) + } + + if viper.GetBool("list") { + listGroups(groupManager) + } + + if viper.IsSet("show") { + groupIdString := viper.GetString("show") + showGroup(groupIdString, groupManager) + } + }, +} + +// initGroupManager creates a new group chat manager and starts the process +// service. +func initGroupManager(client *api.Client) (*groupChat.Manager, + chan groupChat.MessageReceive, chan groupStore.Group) { + recChan := make(chan groupChat.MessageReceive, 10) + receiveCb := func(msg groupChat.MessageReceive) { + recChan <- msg + } + + reqChan := make(chan groupStore.Group, 10) + requestCb := func(g groupStore.Group) { + reqChan <- g + } + + jww.INFO.Print("Creating new group manager.") + manager, err := groupChat.NewManager(client, requestCb, receiveCb) + if err != nil { + jww.FATAL.Panicf("Failed to initialize group chat manager: %+v", err) + } + + // Start group request and message receiver + client.AddService(manager.StartProcesses) + + return manager, recChan, reqChan +} + +// createGroup creates a new group with the provided name and sends out requests +// to the list of user IDs found at the given file path. +func createGroup(name, msg []byte, filePath string, gm *groupChat.Manager) { + userIdStrings := ReadLines(filePath) + userIDs := make([]*id.ID, 0, len(userIdStrings)) + for _, userIdStr := range userIdStrings { + userID, _ := parseRecipient(userIdStr) + userIDs = append(userIDs, userID) + } + + grp, rids, status, err := gm.MakeGroup(userIDs, name, msg) + if err != nil { + jww.FATAL.Panicf("Failed to create new group: %+v", err) + } + + // Integration grabs the group ID from this line + jww.INFO.Printf("NewGroupID: b64:%s", grp.ID) + jww.INFO.Printf("Created Group: Requests:%s on rounds %#v, %v", status, rids, grp) + fmt.Printf("Created new group with name %q and message %q\n", grp.Name, + grp.InitMessage) +} + +// resendRequests resends group requests for the group ID. +func resendRequests(groupIdString string, gm *groupChat.Manager) { + groupID, _ := parseRecipient(groupIdString) + rids, status, err := gm.ResendRequest(groupID) + if err != nil { + jww.FATAL.Panicf("Failed to resend requests to group %s: %+v", + groupID, err) + } + + jww.INFO.Printf("Resending requests to group %s: %v, %s", groupID, rids, status) + fmt.Println("Resending group requests to group.") +} + +// joinGroup joins a group when a request is received on the group request +// channel. +func joinGroup(reqChan chan groupStore.Group, timeout time.Duration, gm *groupChat.Manager) { + jww.INFO.Print("Waiting for group request to be received.") + fmt.Println("Waiting for group request to be received.") + + select { + case grp := <-reqChan: + err := gm.JoinGroup(grp) + if err != nil { + jww.FATAL.Panicf("%+v", err) + } + + jww.INFO.Printf("Joined group: %s", grp.ID) + fmt.Printf("Joined group with name %q and message %q\n", + grp.Name, grp.InitMessage) + case <-time.NewTimer(timeout).C: + jww.INFO.Printf("Timed out after %s waiting for group request.", timeout) + fmt.Println("Timed out waiting for group request.") + return + } +} + +// leaveGroup leaves the group. +func leaveGroup(groupIdString string, gm *groupChat.Manager) { + groupID, _ := parseRecipient(groupIdString) + jww.INFO.Printf("Leaving group %s.", groupID) + + err := gm.LeaveGroup(groupID) + if err != nil { + jww.FATAL.Panicf("Failed to leave group %s: %+v", groupID, err) + } + + jww.INFO.Printf("Left group: %s", groupID) + fmt.Println("Left group.") +} + +// sendGroup send the message to the group. +func sendGroup(groupIdString string, msg []byte, gm *groupChat.Manager) { + groupID, _ := parseRecipient(groupIdString) + + jww.INFO.Printf("Sending to group %s message %q", groupID, msg) + + rid, err := gm.Send(groupID, msg) + if err != nil { + jww.FATAL.Panicf("Sending message to group %s: %+v", groupID, err) + } + + jww.INFO.Printf("Sent to group %s on round %d", groupID, rid) + fmt.Printf("Sent message %q to group.\n", msg) +} + +// messageWait waits for the given number of messages to be received on the +// groupChat.MessageReceive channel. +func messageWait(numMessages uint, timeout time.Duration, recChan chan groupChat.MessageReceive) { + jww.INFO.Printf("Waiting for %d group message(s) to be received.", numMessages) + fmt.Printf("Waiting for %d group message(s) to be received.\n", numMessages) + + for i := uint(0); i < numMessages; { + select { + case msg := <-recChan: + i++ + jww.INFO.Printf("Received group message %d/%d: %s", i, numMessages, msg) + fmt.Printf("Received group message: %q\n", msg.Payload) + case <-time.NewTimer(timeout).C: + jww.INFO.Printf("Timed out after %s waiting for group message.", timeout) + fmt.Printf("Timed out waiting for %d group message(s).\n", numMessages) + return + } + } +} + +// listGroups prints a list of all groups. +func listGroups(gm *groupChat.Manager) { + for i, gid := range gm.GetGroups() { + jww.INFO.Printf("Group %d: %s", i, gid) + } + + fmt.Printf("Printed list of %d groups.\n", gm.NumGroups()) +} + +// showGroup prints all the information of the group. +func showGroup(groupIdString string, gm *groupChat.Manager) { + groupID, _ := parseRecipient(groupIdString) + + grp, ok := gm.GetGroup(groupID) + if !ok { + jww.FATAL.Printf("Could not find group: %s", groupID) + } + + jww.INFO.Printf("Show group %#v", grp) + fmt.Printf("Got group with name %q and message %q\n", grp.Name, grp.InitMessage) +} + +// ReadLines returns each line in a file as a string. +func ReadLines(fileName string) []string { + file, err := os.Open(fileName) + if err != nil { + jww.FATAL.Panicf(err.Error()) + } + defer file.Close() + + var res []string + + scanner := bufio.NewScanner(file) + for scanner.Scan() { + res = append(res, scanner.Text()) + } + + if err := scanner.Err(); err != nil { + jww.FATAL.Panicf(err.Error()) + } + return res +} + +func init() { + groupCmd.Flags().String("create", "", + "Create a group with from the list of contact file paths.") + err := viper.BindPFlag("create", groupCmd.Flags().Lookup("create")) + checkBindErr(err, "create") + + groupCmd.Flags().String("name", "Group Name", + "The name of the new group to create.") + err = viper.BindPFlag("name", groupCmd.Flags().Lookup("name")) + checkBindErr(err, "name") + + groupCmd.Flags().String("resend", "", + "Resend invites for all users in this group ID.") + err = viper.BindPFlag("resend", groupCmd.Flags().Lookup("resend")) + checkBindErr(err, "resend") + + groupCmd.Flags().Bool("join", false, + "Waits for group request joins the group.") + err = viper.BindPFlag("join", groupCmd.Flags().Lookup("join")) + checkBindErr(err, "join") + + groupCmd.Flags().String("leave", "", + "Leave this group ID.") + err = viper.BindPFlag("leave", groupCmd.Flags().Lookup("leave")) + checkBindErr(err, "leave") + + groupCmd.Flags().String("sendMessage", "", + "Send message to this group ID.") + err = viper.BindPFlag("sendMessage", groupCmd.Flags().Lookup("sendMessage")) + checkBindErr(err, "sendMessage") + + groupCmd.Flags().Uint("wait", 0, + "Waits for number of messages to be received.") + err = viper.BindPFlag("wait", groupCmd.Flags().Lookup("wait")) + checkBindErr(err, "wait") + + groupCmd.Flags().Duration("receiveTimeout", time.Minute, + "Amount of time to wait for a group request or message before timing out.") + err = viper.BindPFlag("receiveTimeout", groupCmd.Flags().Lookup("receiveTimeout")) + checkBindErr(err, "receiveTimeout") + + groupCmd.Flags().Bool("list", false, + "Prints list all groups to which this client belongs.") + err = viper.BindPFlag("list", groupCmd.Flags().Lookup("list")) + checkBindErr(err, "list") + + groupCmd.Flags().String("show", "", + "Prints the members of this group ID.") + err = viper.BindPFlag("show", groupCmd.Flags().Lookup("show")) + checkBindErr(err, "show") + + rootCmd.AddCommand(groupCmd) +} + +func checkBindErr(err error, key string) { + if err != nil { + jww.ERROR.Printf("viper.BindPFlag failed for %s: %+v", key, err) + } +} diff --git a/cmd/root.go b/cmd/root.go index f4a03506ab5766a277c26cf351751f9a5dc347c1..720797971617d531dd0626649b04cbb49085f1d9 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -23,7 +23,9 @@ import ( "gitlab.com/elixxir/crypto/contact" "gitlab.com/xx_network/primitives/id" "io/ioutil" + "log" "os" + "runtime/pprof" "strconv" "strings" "time" @@ -45,6 +47,14 @@ var rootCmd = &cobra.Command{ Short: "Runs a client for cMix anonymous communication platform", Args: cobra.NoArgs, Run: func(cmd *cobra.Command, args []string) { + profileOut := viper.GetString("profile-cpu") + if profileOut != "" { + f, err := os.Create(profileOut) + if err != nil { + jww.FATAL.Panicf("%+v", err) + } + pprof.StartCPUProfile(f) + } client := initClient() @@ -70,42 +80,20 @@ var rootCmd = &cobra.Command{ recipientContact = user.GetContact() } - // Set up reception handler - swboard := client.GetSwitchboard() - recvCh := make(chan message.Receive, 10000) - listenerID := swboard.RegisterChannel("DefaultCLIReceiver", - switchboard.AnyUser(), message.Text, recvCh) - jww.INFO.Printf("Message ListenerID: %v", listenerID) + confCh, recvCh := initClientCallbacks(client) - // Set up auth request handler, which simply prints the - // user id of the requester. - authMgr := client.GetAuthRegistrar() - authMgr.AddGeneralRequestCallback(printChanRequest) - - // If unsafe channels, add auto-acceptor + // The following block is used to check if the request from + // a channel authorization is from the recipient we intend in + // this run. authConfirmed := false - authMgr.AddGeneralConfirmCallback(func( - partner contact.Contact) { - jww.INFO.Printf("Channel Confirmed: %s", - partner.ID) - authConfirmed = recipientID.Cmp(partner.ID) - }) - if viper.GetBool("unsafe-channel-creation") { - authMgr.AddGeneralRequestCallback(func( - requestor contact.Contact, message string) { - jww.INFO.Printf("Channel Request: %s", - requestor.ID) - _, err := client.ConfirmAuthenticatedChannel( - requestor) - if err != nil { - jww.FATAL.Panicf("%+v", err) - } - authConfirmed = recipientID.Cmp( - requestor.ID) - }) - } + go func() { + for { + requestor := <-confCh + authConfirmed = recipientID.Cmp(requestor) + } + }() - _, err := client.StartNetworkFollower() + _, err := client.StartNetworkFollower(5 * time.Second) if err != nil { jww.FATAL.Panicf("%+v", err) } @@ -255,15 +243,57 @@ var rootCmd = &cobra.Command{ } fmt.Printf("Received %d\n", receiveCnt) - err = client.StopNetworkFollower(5 * time.Second) + err = client.StopNetworkFollower() if err != nil { jww.WARN.Printf( "Failed to cleanly close threads: %+v\n", err) } + if profileOut != "" { + pprof.StopCPUProfile() + } + }, } +func initClientCallbacks(client *api.Client) (chan *id.ID, + chan message.Receive) { + // Set up reception handler + swboard := client.GetSwitchboard() + recvCh := make(chan message.Receive, 10000) + listenerID := swboard.RegisterChannel("DefaultCLIReceiver", + switchboard.AnyUser(), message.Text, recvCh) + jww.INFO.Printf("Message ListenerID: %v", listenerID) + + // Set up auth request handler, which simply prints the + // user id of the requester. + authMgr := client.GetAuthRegistrar() + authMgr.AddGeneralRequestCallback(printChanRequest) + + // If unsafe channels, add auto-acceptor + authConfirmed := make(chan *id.ID, 10) + authMgr.AddGeneralConfirmCallback(func( + partner contact.Contact) { + jww.INFO.Printf("Channel Confirmed: %s", + partner.ID) + authConfirmed <- partner.ID + }) + if viper.GetBool("unsafe-channel-creation") { + authMgr.AddGeneralRequestCallback(func( + requestor contact.Contact, message string) { + jww.INFO.Printf("Channel Request: %s", + requestor.ID) + _, err := client.ConfirmAuthenticatedChannel( + requestor) + if err != nil { + jww.FATAL.Panicf("%+v", err) + } + authConfirmed <- requestor.ID + }) + } + return authConfirmed, recvCh +} + // Helper function which prints the round resuls func printRoundResults(allRoundsSucceeded, timedOut bool, rounds map[id.Round]api.RoundResult, roundIDs []id.Round, msg message.Send) { @@ -333,7 +363,6 @@ func createClient() *api.Client { err = api.NewClient(string(ndfJSON), storeDir, []byte(pass), regCode) } - } if err != nil { @@ -348,6 +377,7 @@ func createClient() *api.Client { viper.GetUint("e2eNumReKeys")) netParams.ForceHistoricalRounds = viper.GetBool("forceHistoricalRounds") netParams.FastPolling = !viper.GetBool("slowPolling") + netParams.ForceMessagePickupRetry = viper.GetBool("forceMessagePickupRetry") client, err := api.OpenClient(storeDir, []byte(pass), netParams) if err != nil { @@ -369,6 +399,12 @@ func initClient() *api.Client { viper.GetUint("e2eNumReKeys")) netParams.ForceHistoricalRounds = viper.GetBool("forceHistoricalRounds") netParams.FastPolling = viper.GetBool(" slowPolling") + netParams.ForceMessagePickupRetry = viper.GetBool("forceMessagePickupRetry") + if netParams.ForceMessagePickupRetry { + period := 3 * time.Second + jww.INFO.Printf("Setting Uncheck Round Period to %v", period) + netParams.UncheckRoundPeriod = period + } //load the client client, err := api.Login(storeDir, []byte(pass), netParams) @@ -498,7 +534,7 @@ func waitUntilConnected(connected chan bool) { isConnected) break case <-timeoutTimer.C: - jww.FATAL.Panic("timeout on connection") + jww.FATAL.Panicf("timeout on connection after %s", waitTimeout*time.Second) } } @@ -613,34 +649,19 @@ func initLog(threshold uint, logPath string) { jww.INFO.Printf("log level set to: TRACE") jww.SetStdoutThreshold(jww.LevelTrace) jww.SetLogThreshold(jww.LevelTrace) + jww.SetFlags(log.LstdFlags | log.Lmicroseconds) } else if threshold == 1 { jww.INFO.Printf("log level set to: DEBUG") jww.SetStdoutThreshold(jww.LevelDebug) jww.SetLogThreshold(jww.LevelDebug) + jww.SetFlags(log.LstdFlags | log.Lmicroseconds) } else { - jww.INFO.Printf("log level set to: TRACE") + jww.INFO.Printf("log level set to: INFO") jww.SetStdoutThreshold(jww.LevelInfo) jww.SetLogThreshold(jww.LevelInfo) } } -func isValidUser(usr []byte) (bool, *id.ID) { - if len(usr) != id.ArrIDLen { - return false, nil - } - for _, b := range usr { - if b != 0 { - uid, err := id.Unmarshal(usr) - if err != nil { - jww.WARN.Printf("Could not unmarshal user: %s", err) - return false, nil - } - return true, uid - } - } - return false, nil -} - func askToCreateChannel(recipientID *id.ID) bool { for { fmt.Printf("This is the first time you have messaged %v, "+ @@ -769,6 +790,11 @@ func init() { "Enables polling for unfiltered network updates with RSA signatures") viper.BindPFlag("slowPolling", rootCmd.Flags().Lookup("slowPolling")) + rootCmd.Flags().Bool("forceMessagePickupRetry", false, + "Enable a mechanism which forces a 50% chance of no message pickup, "+ + "instead triggering the message pickup retry mechanism") + viper.BindPFlag("forceMessagePickupRetry", + rootCmd.Flags().Lookup("forceMessagePickupRetry")) // E2E Params defaultE2EParams := params.GetDefaultE2ESessionParams() @@ -784,6 +810,10 @@ func init() { "", uint(defaultE2EParams.NumRekeys), "Number of rekeys reserved for rekey operations") viper.BindPFlag("e2eNumReKeys", rootCmd.Flags().Lookup("e2eNumReKeys")) + + rootCmd.Flags().String("profile-cpu", "", + "Enable cpu profiling to this file") + viper.BindPFlag("profile-cpu", rootCmd.Flags().Lookup("profile-cpu")) } // initConfig reads in config file and ENV variables if set. diff --git a/cmd/single.go b/cmd/single.go index 15f803b11bec0f775845fdfdfb9f8292c75ea2a7..b827627fa2af0be3f5ec0fa338170f7af6a36493 100644 --- a/cmd/single.go +++ b/cmd/single.go @@ -62,7 +62,7 @@ var singleCmd = &cobra.Command{ }) } - _, err := client.StartNetworkFollower() + _, err := client.StartNetworkFollower(5 * time.Second) if err != nil { jww.FATAL.Panicf("%+v", err) } diff --git a/cmd/ud.go b/cmd/ud.go index 38cbecb2cd1c6572cd140b158f435fe4291500cd..2a3b3d31927ee62522444c179c7dab50c859f4cd 100644 --- a/cmd/ud.go +++ b/cmd/ud.go @@ -62,7 +62,7 @@ var udCmd = &cobra.Command{ }) } - _, err := client.StartNetworkFollower() + _, err := client.StartNetworkFollower(50 * time.Millisecond) if err != nil { jww.FATAL.Panicf("%+v", err) } @@ -176,7 +176,7 @@ var udCmd = &cobra.Command{ } if len(facts) == 0 { - err = client.StopNetworkFollower(10 * time.Second) + err = client.StopNetworkFollower() if err != nil { jww.WARN.Print(err) } @@ -196,7 +196,7 @@ var udCmd = &cobra.Command{ jww.FATAL.Panicf("%+v", err) } time.Sleep(91 * time.Second) - err = client.StopNetworkFollower(90 * time.Second) + err = client.StopNetworkFollower() if err != nil { jww.WARN.Print(err) } diff --git a/cmd/version.go b/cmd/version.go index 68e3f78210a7de2e059d35d4a877204fc1c56f38..257036e702c419e55e6354ce14ec88533ea21be0 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -18,7 +18,7 @@ import ( ) // Change this value to set the version for this build -const currentVersion = "2.6.0" +const currentVersion = "2.7.0" func Version() string { out := fmt.Sprintf("Elixxir Client v%s -- %s\n\n", api.SEMVER, diff --git a/go.mod b/go.mod index 7a624d8d898fb18f26f27efd965043689e775e82..a1e3dcadd27b247e95f93aa918215e8ac4abb852 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.13 require ( github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 - github.com/golang/protobuf v1.4.3 + github.com/golang/protobuf v1.5.2 github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect github.com/magiconair/properties v1.8.4 // indirect github.com/mitchellh/mapstructure v1.4.0 // indirect @@ -17,20 +17,17 @@ require ( github.com/spf13/jwalterweatherman v1.1.0 github.com/spf13/viper v1.7.1 gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228 - gitlab.com/elixxir/comms v0.0.4-0.20210520231539-ca1bbeb6e3ec - gitlab.com/elixxir/crypto v0.0.7-0.20210520231341-cc91d0be28ae + gitlab.com/elixxir/comms v0.0.4-0.20210622161439-b694033c9507 + gitlab.com/elixxir/crypto v0.0.7-0.20210614155844-c1e9c23a6ba7 gitlab.com/elixxir/ekv v0.1.5 - gitlab.com/elixxir/primitives v0.0.3-0.20210520220650-16cb34e6b7e3 - gitlab.com/xx_network/comms v0.0.4-0.20210517205649-06ddfa8d2a75 - gitlab.com/xx_network/crypto v0.0.5-0.20210517205543-4ae99cbb9063 - gitlab.com/xx_network/primitives v0.0.4-0.20210517202253-c7b4bd0087ea + gitlab.com/elixxir/primitives v0.0.3-0.20210614155726-ebcf2d47a527 + gitlab.com/xx_network/comms v0.0.4-0.20210622161535-4f3d927d4c8c + gitlab.com/xx_network/crypto v0.0.5-0.20210614155554-8c333814205b + gitlab.com/xx_network/primitives v0.0.4-0.20210617180018-6472489fd418 golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 - golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 - golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect + golang.org/x/net v0.0.0-20210525063256-abc453219eb5 google.golang.org/genproto v0.0.0-20210105202744-fe13368bc0e1 // indirect - google.golang.org/grpc v1.34.0 // indirect - google.golang.org/protobuf v1.26.0-rc.1 + google.golang.org/protobuf v1.26.0 gopkg.in/ini.v1 v1.62.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect ) - -replace google.golang.org/grpc => github.com/grpc/grpc-go v1.27.1 diff --git a/go.sum b/go.sum index c2dfb2c3875688101ee7be90c56f1cba175c27ac..be6929544fff09ff9ef8ff85bdda4fa4e8144b65 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= cloud.google.com/go v0.44.1/go.mod h1:iSa0KzasP4Uvy3f1mN/7PiObzGgflwredwwASm/v6AU= @@ -10,7 +11,6 @@ cloud.google.com/go/firestore v1.1.0/go.mod h1:ulACoGHTpvq5r8rxGJ4ddJZBZqakUQqCl cloud.google.com/go/pubsub v1.0.1/go.mod h1:R0Gpsv3s54REJCy4fxDixWD93lHJMoZTyQ2kNxGRt3I= cloud.google.com/go/storage v1.0.0/go.mod h1:IhtSnM/ZTZV8YYJWCY8RULGVqBDmpoyjwiyrjsg+URw= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= @@ -27,6 +27,9 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= +github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= @@ -39,10 +42,12 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98= +github.com/envoyproxy/go-control-plane v0.9.9-0.20210217033140-668b12f5399d/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= -github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= @@ -56,7 +61,6 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 h1:zN2lZNZRflqFyxVaTIU61KNKQ9C0055u9CAfpmqUvo4= github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3/go.mod h1:nPpo7qLxd6XL3hWJG/O60sR8ZKfMCiIoNap5GvD12KU= -github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= @@ -65,31 +69,31 @@ github.com/golang/mock v1.3.1/go.mod h1:sBzyDLLjw3U8JLTeZvSv8jJB+tU5PVekmnlKIyFU github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= -github.com/golang/protobuf v1.4.0 h1:oOuy+ugB+P/kBdUnG5QaMXSIyJ1q38wWSojYCb3z5VQ= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0= github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= -github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/protobuf v1.4.3 h1:JjCZWpVbqXDqFVmTfYWEVTMIYrL/NPdPSCHPJ0T/raM= -github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= @@ -99,8 +103,6 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= -github.com/grpc/grpc-go v1.27.1 h1:EluyjU5nlbuNJSEktNl600PIpzbO2OcvZWfWV1jFvKM= -github.com/grpc/grpc-go v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q= github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -144,7 +146,6 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/liyue201/goqr v0.0.0-20200803022322-df443203d4ea h1:uyJ13zfy6l79CM3HnVhDalIyZ4RJAyVfDrbnfFeJoC4= github.com/liyue201/goqr v0.0.0-20200803022322-df443203d4ea/go.mod h1:w4pGU9PkiX2hAWyF0yuHEHmYTQFAd6WHzp6+IY7JVjE= -github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4= github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.4 h1:8KGKTcQQGm0Kv7vEbKFErAoAOFyyacLStRtQSeYtvkY= github.com/magiconair/properties v1.8.4/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= @@ -160,17 +161,13 @@ github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eI github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg= github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.4.0 h1:7ks8ZkOP5/ujthUsT07rNv+nkLXCQWKNHuwzOAesEks= github.com/mitchellh/mapstructure v1.4.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= -github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= -github.com/nyaruka/phonenumbers v1.0.60 h1:nnAcNwmZflhegiImm6MkvjlRRyoaSw1ox/jGPAewWTg= -github.com/nyaruka/phonenumbers v1.0.60/go.mod h1:sDaTZ/KPX5f8qyV9qN+hIm+4ZBARJrupC6LuhshJq1U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= @@ -232,7 +229,6 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -243,56 +239,44 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q github.com/zeebo/assert v0.0.0-20181109011804-10f827ce2ed6/go.mod h1:yssERNPivllc1yU3BvpjYI5BUW+zglcz6QWqeVRL5t0= github.com/zeebo/assert v1.1.0 h1:hU1L1vLTHsnO8x8c9KAR5GmM5QscxHg5RNU5z5qbUWY= github.com/zeebo/assert v1.1.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0= -github.com/zeebo/blake3 v0.0.4 h1:vtZ4X8B2lKXZFg2Xyg6Wo36mvmnJvc2VQYTtA4RDCkI= github.com/zeebo/blake3 v0.0.4/go.mod h1:YOZo8A49yNqM0X/Y+JmDUZshJWLt1laHsNSn5ny2i34= github.com/zeebo/blake3 v0.1.1 h1:Nbsts7DdKThRHHd+YNlqiGlRqGEF2bE2eXN+xQ1hsEs= github.com/zeebo/blake3 v0.1.1/go.mod h1:G9pM4qQwjRzF1/v7+vabMj/c5mWpGZ2Wzo3Eb4z0pb4= -github.com/zeebo/pcg v0.0.0-20181207190024-3cdc6b625a05 h1:4pW5fMvVkrgkMXdvIsVRRTs69DWYA8uNNQsu1stfVKU= github.com/zeebo/pcg v0.0.0-20181207190024-3cdc6b625a05/go.mod h1:Gr+78ptB0MwXxm//LBaEvBiaXY7hXJ6KGe2V32X2F6E= github.com/zeebo/pcg v1.0.0 h1:dt+dx+HvX8g7Un32rY9XWoYnd0NmKmrIzpHF7qiTDj0= github.com/zeebo/pcg v1.0.0/go.mod h1:09F0S9iiKrwn9rlI5yjLkmrug154/YRW6KnnXVDM/l4= gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228 h1:Gi6rj4mAlK0BJIk1HIzBVMjWNjIUfstrsXC2VqLYPcA= gitlab.com/elixxir/bloomfilter v0.0.0-20200930191214-10e9ac31b228/go.mod h1:H6jztdm0k+wEV2QGK/KYA+MY9nj9Zzatux/qIvDDv3k= -gitlab.com/elixxir/comms v0.0.4-0.20210519214834-4b27f37412f1 h1:GauO2zR/wVc0zTTtJ8iX2Hzs3LFeLlQAiGfAZWDYssE= -gitlab.com/elixxir/comms v0.0.4-0.20210519214834-4b27f37412f1/go.mod h1:hdLuXAbKt9YzI637EZdoWpPC9C6hEsBXa21IsCBbZc8= -gitlab.com/elixxir/comms v0.0.4-0.20210520231539-ca1bbeb6e3ec h1:a9Ic9N0qZpuG4N9kIU+m9QcTXF7qh73+y46n/StCHBE= -gitlab.com/elixxir/comms v0.0.4-0.20210520231539-ca1bbeb6e3ec/go.mod h1:OE24JSQ7bMGCHvw75HIBr6o11onU8k43HYDbalm8muE= -gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4 h1:28ftZDeYEko7xptCZzeFWS1Iam95dj46TWFVVlKmw6A= +gitlab.com/elixxir/comms v0.0.4-0.20210622161439-b694033c9507 h1:uH64CKk3PVzo0v9FbII3XYNiZIoQz2pOb7Fnu5L/Ba8= +gitlab.com/elixxir/comms v0.0.4-0.20210622161439-b694033c9507/go.mod h1:vVAO+8dSm/sikL66Qx/+CEBVVzzDIdYru+VLcrIm+tA= gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo+K0N2GwRRpaNr+tKXMIOVWzmyUD0SbOu2c= -gitlab.com/elixxir/crypto v0.0.3 h1:znCt/x2bL4y8czTPaaFkwzdgSgW3BJc/1+dxyf1jqVw= gitlab.com/elixxir/crypto v0.0.3/go.mod h1:ZNgBOblhYToR4m8tj4cMvJ9UsJAUKq+p0gCp07WQmhA= -gitlab.com/elixxir/crypto v0.0.7-0.20210519214631-6e1aedaf8d0c h1:N/ErhUa4VfsAs3obrJfbrplnZm6jqCvdY55gMAGdQPo= -gitlab.com/elixxir/crypto v0.0.7-0.20210519214631-6e1aedaf8d0c/go.mod h1:Mjs4etcq67zC1Wa60RahhgAIavbtiK6zAuJVwsjGhrk= -gitlab.com/elixxir/crypto v0.0.7-0.20210520231341-cc91d0be28ae h1:EKA7z0CclY30bEnt+6ZOcyZrEwWoYVz9vn+cgpx9/Rk= -gitlab.com/elixxir/crypto v0.0.7-0.20210520231341-cc91d0be28ae/go.mod h1:dWTNCyFkDlUiJh/rHRV2PHnu/akReOoXSTjpEOs9Py0= +gitlab.com/elixxir/crypto v0.0.7-0.20210614155844-c1e9c23a6ba7 h1:UBq4/xMUWkYmEzUN2F7nLw5qQeiNKoLaoX3vZ/flz1c= +gitlab.com/elixxir/crypto v0.0.7-0.20210614155844-c1e9c23a6ba7/go.mod h1:FP848WCzyf81/Csz1lJpi3NXgIdpzJ4hoJam53xwCuo= gitlab.com/elixxir/ekv v0.1.5 h1:R8M1PA5zRU1HVnTyrtwybdABh7gUJSCvt1JZwUSeTzk= gitlab.com/elixxir/ekv v0.1.5/go.mod h1:e6WPUt97taFZe5PFLPb1Dupk7tqmDCTQu1kkstqJvw4= gitlab.com/elixxir/primitives v0.0.0-20200731184040-494269b53b4d/go.mod h1:OQgUZq7SjnE0b+8+iIAT2eqQF+2IFHn73tOo+aV11mg= gitlab.com/elixxir/primitives v0.0.0-20200804170709-a1896d262cd9/go.mod h1:p0VelQda72OzoUckr1O+vPW0AiFe0nyKQ6gYcmFSuF8= gitlab.com/elixxir/primitives v0.0.0-20200804182913-788f47bded40/go.mod h1:tzdFFvb1ESmuTCOl1z6+yf6oAICDxH2NPUemVgoNLxc= -gitlab.com/elixxir/primitives v0.0.1 h1:q61anawANlNAExfkeQEE1NCsNih6vNV1FFLoUQX6txQ= gitlab.com/elixxir/primitives v0.0.1/go.mod h1:kNp47yPqja2lHSiS4DddTvFpB/4D9dB2YKnw5c+LJCE= -gitlab.com/elixxir/primitives v0.0.3-0.20210519212350-6ad72cbae82c h1:jDLyFcsYAyHQEkwN7Mws1gX0PyBGwXoI7jdtU7Fz6DY= -gitlab.com/elixxir/primitives v0.0.3-0.20210519212350-6ad72cbae82c/go.mod h1:TR4FpK1b+IE4IlDmvMnCAM0qqtrF1aYNUd+bnhRiAKI= -gitlab.com/elixxir/primitives v0.0.3-0.20210520220650-16cb34e6b7e3 h1:ZhuILY/OhL8wEDulQVtgYJlC48jdWxAzarQW2xQF0JA= -gitlab.com/elixxir/primitives v0.0.3-0.20210520220650-16cb34e6b7e3/go.mod h1:TR4FpK1b+IE4IlDmvMnCAM0qqtrF1aYNUd+bnhRiAKI= +gitlab.com/elixxir/primitives v0.0.3-0.20210614155726-ebcf2d47a527 h1:kBNAGFy5Ylz7F0K3DmyzuHLf1npBg7a3t4qKvfqPL3Y= +gitlab.com/elixxir/primitives v0.0.3-0.20210614155726-ebcf2d47a527/go.mod h1:nSmBXcw4hkBLFdhu+araAPvf9szCDQF1fpRZ9/BgBec= gitlab.com/xx_network/comms v0.0.0-20200805174823-841427dd5023/go.mod h1:owEcxTRl7gsoM8c3RQ5KAm5GstxrJp5tn+6JfQ4z5Hw= -gitlab.com/xx_network/comms v0.0.4-0.20210517205649-06ddfa8d2a75 h1:l5szLkEfBQMa2eEt1mQf0B+Dp2pckT2y37at1MWfNj4= -gitlab.com/xx_network/comms v0.0.4-0.20210517205649-06ddfa8d2a75/go.mod h1:qxX3x7yCATvaK8hhFibl2Rnnb+xvLior/AJlx2dk1UM= +gitlab.com/xx_network/comms v0.0.4-0.20210617183321-d5f4fd71033c/go.mod h1:ehwxZxcAQHkJjP5BNkwPNK8/o6avUn0j0iDDiu+nMFc= +gitlab.com/xx_network/comms v0.0.4-0.20210622161535-4f3d927d4c8c h1:/vUWvCEGL9dI73Cv0eRIxgYnicSCwXYduV6PJVcdsus= +gitlab.com/xx_network/comms v0.0.4-0.20210622161535-4f3d927d4c8c/go.mod h1:ehwxZxcAQHkJjP5BNkwPNK8/o6avUn0j0iDDiu+nMFc= gitlab.com/xx_network/crypto v0.0.3/go.mod h1:DF2HYvvCw9wkBybXcXAgQMzX+MiGbFPjwt3t17VRqRE= -gitlab.com/xx_network/crypto v0.0.4 h1:lpKOL5mTJ2awWMfgBy30oD/UvJVrWZzUimSHlOdZZxo= gitlab.com/xx_network/crypto v0.0.4/go.mod h1:+lcQEy+Th4eswFgQDwT0EXKp4AXrlubxalwQFH5O0Mk= -gitlab.com/xx_network/crypto v0.0.5-0.20210517205543-4ae99cbb9063 h1:BW6kQCHP5dySBWuaKjfU6gP+Wbw5Dnr8GTRzaEbF1zU= -gitlab.com/xx_network/crypto v0.0.5-0.20210517205543-4ae99cbb9063/go.mod h1:AOUw4RJfBrKDXbe9nY/8bM3ID1czcooefUbVd82zrCY= +gitlab.com/xx_network/crypto v0.0.5-0.20210614155554-8c333814205b h1:X2Hhg9/IYowxMdI6TTnWj6WW3pnO2vMB/7f4mnu6Muw= +gitlab.com/xx_network/crypto v0.0.5-0.20210614155554-8c333814205b/go.mod h1:wiaQXyI9C9UGxxgLd+2lDmKyovO+PjFxaesCBgG0YDA= gitlab.com/xx_network/primitives v0.0.0-20200803231956-9b192c57ea7c/go.mod h1:wtdCMr7DPePz9qwctNoAUzZtbOSHSedcK++3Df3psjA= -gitlab.com/xx_network/primitives v0.0.0-20200804183002-f99f7a7284da h1:CCVslUwNC7Ul7NG5nu3ThGTSVUt1TxNRX+47f5TUwnk= gitlab.com/xx_network/primitives v0.0.0-20200804183002-f99f7a7284da/go.mod h1:OK9xevzWCaPO7b1wiluVJGk7R5ZsuC7pHY5hteZFQug= -gitlab.com/xx_network/primitives v0.0.2 h1:r45yKenJ9e7PylI1ZXJ1Es09oYNaYXjxVy9+uYlwo7Y= gitlab.com/xx_network/primitives v0.0.2/go.mod h1:cs0QlFpdMDI6lAo61lDRH2JZz+3aVkHy+QogOB6F/qc= -gitlab.com/xx_network/primitives v0.0.4-0.20210517202253-c7b4bd0087ea h1:yiFyOC+qhTLO/AjCOuL7hNNkD4VdPnnSmeYXXQwmGYE= -gitlab.com/xx_network/primitives v0.0.4-0.20210517202253-c7b4bd0087ea/go.mod h1:9imZHvYwNFobxueSvVtHneZLk9wTK7HQTzxPm+zhFhE= -gitlab.com/xx_network/ring v0.0.2 h1:TlPjlbFdhtJrwvRgIg4ScdngMTaynx/ByHBRZiXCoL0= -gitlab.com/xx_network/ring v0.0.2/go.mod h1:aLzpP2TiZTQut/PVHR40EJAomzugDdHXetbieRClXIM= +gitlab.com/xx_network/primitives v0.0.4-0.20210608160426-670aab2d82cf/go.mod h1:9imZHvYwNFobxueSvVtHneZLk9wTK7HQTzxPm+zhFhE= +gitlab.com/xx_network/primitives v0.0.4-0.20210617180018-6472489fd418 h1:F52R0wvFobjkmB8YaPNHZIu0VYqwjesMBCb9T14ygW8= +gitlab.com/xx_network/primitives v0.0.4-0.20210617180018-6472489fd418/go.mod h1:9imZHvYwNFobxueSvVtHneZLk9wTK7HQTzxPm+zhFhE= +gitlab.com/xx_network/ring v0.0.3-0.20210527191221-ce3f170aabd5 h1:FY+4Rh1Q2rgLyv10aKJjhWApuKRCR/054XhreudfAvw= +gitlab.com/xx_network/ring v0.0.3-0.20210527191221-ce3f170aabd5/go.mod h1:aLzpP2TiZTQut/PVHR40EJAomzugDdHXetbieRClXIM= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= @@ -305,16 +289,11 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190820162420-60c769a6c586/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw= golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20200707235045-ab33eee955e0 h1:eIYIE7EC5/Wv5Kbz8bJPaq+TN3kq3W8S+LSm62vM0DY= golang.org/x/crypto v0.0.0-20200707235045-ab33eee955e0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de h1:ikNHVSjEfnvz6sxdSPCaPt572qowuyMDMJLLm3Db3ig= golang.org/x/crypto v0.0.0-20200728195943-123391ffb6de/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM= golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY= golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w= golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= @@ -325,6 +304,7 @@ golang.org/x/exp v0.0.0-20190829153037-c13cbed26979/go.mod h1:86+5VVa7VpoJ4kLfm0 golang.org/x/exp v0.0.0-20191030013958-a1ab85dbe136/go.mod h1:JXzH8nQsPlswgeRAPE3MuO9GYsAcnJvJ4vnMwN/5qkY= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= @@ -336,6 +316,7 @@ golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCc golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181023162649-9b4f9f5ad519/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -348,19 +329,20 @@ golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200707034311-ab3426394381 h1:VXak5I6aEWmAXeQjA+QSZzlgNrpq9mjcfDemuexIKsU= golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20201029221708-28c70e62bb1d/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20210525063256-abc453219eb5 h1:wjuX4b5yYQnEQHzd+CBcrcC6OVR2J1CN6mUy0oSxIPo= +golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181026203630-95b1ffbd15a5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -376,29 +358,26 @@ golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200519105757-fe76b779f299 h1:DYfZAGf2WMFjMxbgTjaC+2HC7NkNAQs+6Q8b9WEB/F4= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200917073148-efd3b9a0ff20/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201014080544-cc95f250f6bc/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d h1:jbzgAvDZn8aEnytae+4ou0J0GwFZoHR0hOrTg4qH8GA= golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57iKi3297KFs0QDbGN60A= -golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.4 h1:0YWbFKbhXG/wIiuHDSKpS0Iy7FSA+u45VtBMfQcFTTc= -golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= @@ -415,7 +394,6 @@ golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -424,6 +402,7 @@ google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= google.golang.org/api v0.9.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= google.golang.org/api v0.13.0/go.mod h1:iLdEw5Ide6rF15KTC1Kkl0iskquN2gFfn9o9XIsbkAI= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.5.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= google.golang.org/appengine v1.6.1/go.mod h1:i06prIuMbXzDqacNJfV5OdTW448YApPu5ww/cMBSeb0= @@ -439,24 +418,33 @@ google.golang.org/genproto v0.0.0-20191108220845-16a3f7862a1a/go.mod h1:n3cpQtvx google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= google.golang.org/genproto v0.0.0-20210105202744-fe13368bc0e1 h1:Zk6zlGXdtYdcY5TL+VrbTfmifvk3VvsXopCpszsHPBA= google.golang.org/genproto v0.0.0-20210105202744-fe13368bc0e1/go.mod h1:FWY/as6DDZQgahTzZj3fqbO1CbirC29ZNUFHwi0/+no= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.20.1/go.mod h1:10oTOabMzJvdu6/UiuZezV6QK5dSlG84ov/aaiqXj38= +google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= +google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= +google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.38.0 h1:/9BgsAsa5nWe26HqOlvlgJnqBuktYOLCgjCPqsa56W0= +google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= -google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zimw= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.22.0 h1:cJv5/xdbk1NnMPR1VP9+HU6gupuG9MLBoH1r6RHZ2MY= google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4= -google.golang.org/protobuf v1.26.0-rc.1 h1:7QnIQpGRHE5RnLKnESfDoxm2dTapTZua5a0kS0A+VXQ= +google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= @@ -470,7 +458,6 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/groupChat/gcMessages.pb.go b/groupChat/gcMessages.pb.go new file mode 100644 index 0000000000000000000000000000000000000000..37b23167e412866c8012fd9f8c95bd99adab88b3 --- /dev/null +++ b/groupChat/gcMessages.pb.go @@ -0,0 +1,117 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: groupChat/gcMessages.proto + +package groupChat + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// Request to join the group sent from leader to all members. +type Request struct { + Name []byte `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + IdPreimage []byte `protobuf:"bytes,2,opt,name=idPreimage,proto3" json:"idPreimage,omitempty"` + KeyPreimage []byte `protobuf:"bytes,3,opt,name=keyPreimage,proto3" json:"keyPreimage,omitempty"` + Members []byte `protobuf:"bytes,4,opt,name=members,proto3" json:"members,omitempty"` + Message []byte `protobuf:"bytes,5,opt,name=message,proto3" json:"message,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *Request) Reset() { *m = Request{} } +func (m *Request) String() string { return proto.CompactTextString(m) } +func (*Request) ProtoMessage() {} +func (*Request) Descriptor() ([]byte, []int) { + return fileDescriptor_49d0b7a6ffb7e279, []int{0} +} + +func (m *Request) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_Request.Unmarshal(m, b) +} +func (m *Request) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_Request.Marshal(b, m, deterministic) +} +func (m *Request) XXX_Merge(src proto.Message) { + xxx_messageInfo_Request.Merge(m, src) +} +func (m *Request) XXX_Size() int { + return xxx_messageInfo_Request.Size(m) +} +func (m *Request) XXX_DiscardUnknown() { + xxx_messageInfo_Request.DiscardUnknown(m) +} + +var xxx_messageInfo_Request proto.InternalMessageInfo + +func (m *Request) GetName() []byte { + if m != nil { + return m.Name + } + return nil +} + +func (m *Request) GetIdPreimage() []byte { + if m != nil { + return m.IdPreimage + } + return nil +} + +func (m *Request) GetKeyPreimage() []byte { + if m != nil { + return m.KeyPreimage + } + return nil +} + +func (m *Request) GetMembers() []byte { + if m != nil { + return m.Members + } + return nil +} + +func (m *Request) GetMessage() []byte { + if m != nil { + return m.Message + } + return nil +} + +func init() { + proto.RegisterType((*Request)(nil), "gcRequestMessages.Request") +} + +func init() { + proto.RegisterFile("groupChat/gcMessages.proto", fileDescriptor_49d0b7a6ffb7e279) +} + +var fileDescriptor_49d0b7a6ffb7e279 = []byte{ + // 186 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x4a, 0x2f, 0xca, 0x2f, + 0x2d, 0x70, 0xce, 0x48, 0x2c, 0xd1, 0x4f, 0x4f, 0xf6, 0x4d, 0x2d, 0x2e, 0x4e, 0x4c, 0x4f, 0x2d, + 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x12, 0x4c, 0x4f, 0x0e, 0x4a, 0x2d, 0x2c, 0x4d, 0x2d, + 0x2e, 0x81, 0x49, 0x28, 0x4d, 0x66, 0xe4, 0x62, 0x87, 0x8a, 0x09, 0x09, 0x71, 0xb1, 0xe4, 0x25, + 0xe6, 0xa6, 0x4a, 0x30, 0x2a, 0x30, 0x6a, 0xf0, 0x04, 0x81, 0xd9, 0x42, 0x72, 0x5c, 0x5c, 0x99, + 0x29, 0x01, 0x45, 0xa9, 0x99, 0xb9, 0x89, 0xe9, 0xa9, 0x12, 0x4c, 0x60, 0x19, 0x24, 0x11, 0x21, + 0x05, 0x2e, 0xee, 0xec, 0xd4, 0x4a, 0xb8, 0x02, 0x66, 0xb0, 0x02, 0x64, 0x21, 0x21, 0x09, 0x2e, + 0xf6, 0xdc, 0xd4, 0xdc, 0xa4, 0xd4, 0xa2, 0x62, 0x09, 0x16, 0xb0, 0x2c, 0x8c, 0x0b, 0x91, 0x01, + 0xbb, 0x43, 0x82, 0x15, 0x26, 0x03, 0xe6, 0x3a, 0xa9, 0x46, 0x29, 0xa7, 0x67, 0x96, 0xe4, 0x24, + 0x26, 0xe9, 0x25, 0xe7, 0xe7, 0xea, 0xa7, 0xe6, 0x64, 0x56, 0x54, 0x64, 0x16, 0xe9, 0x27, 0xe7, + 0x64, 0xa6, 0xe6, 0x95, 0xe8, 0xc3, 0x3d, 0x98, 0xc4, 0x06, 0xf6, 0x96, 0x31, 0x20, 0x00, 0x00, + 0xff, 0xff, 0x6e, 0x63, 0x77, 0xd1, 0xf4, 0x00, 0x00, 0x00, +} diff --git a/groupChat/gcMessages.proto b/groupChat/gcMessages.proto new file mode 100644 index 0000000000000000000000000000000000000000..7ce1f3b40c5f34f61367dcec2e477dd8040faae5 --- /dev/null +++ b/groupChat/gcMessages.proto @@ -0,0 +1,20 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +syntax = "proto3"; +package gcRequestMessages; +option go_package = "gitlab.com/elixxir/client/groupChat"; + + +// Request to join the group sent from leader to all members. +message Request { + bytes name = 1; + bytes idPreimage = 2; + bytes keyPreimage = 3; + bytes members = 4; + bytes message = 5; +} \ No newline at end of file diff --git a/groupChat/generateProto.sh b/groupChat/generateProto.sh new file mode 100644 index 0000000000000000000000000000000000000000..43968a4aa112270ffb38ea9a2c5da91e309871f1 --- /dev/null +++ b/groupChat/generateProto.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +protoc --go_out=paths=source_relative:. groupChat/gcMessages.proto diff --git a/groupChat/group.go b/groupChat/group.go new file mode 100644 index 0000000000000000000000000000000000000000..5d67d19f1e52f80cf1628f2deece083b4d8f4568 --- /dev/null +++ b/groupChat/group.go @@ -0,0 +1,70 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +// Group chat is used to communicate the same content with multiple clients over +// cMix. A group chat is controlled by a group leader who creates the group, +// defines all group keys, and is responsible for key rotation. To create a +// group, the group leader must have an authenticated channel with all members +// of the group. +// +// Once a group is created, neither the leader nor other members can add or +// remove users to the group. Only members can leave a group themselves. +// +// When a message is sent to the group, the sender will send an individual +// message to every member of the group. + +package groupChat + +import ( + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/xx_network/primitives/id" +) + +// GroupChat is used to send and receive cMix messages to/from multiple users. +type GroupChat interface { + // MakeGroup sends GroupChat requests to all members over an authenticated + // channel. The leader of a GroupChat must have an authenticated channel + // with each member of the GroupChat to add them to the GroupChat. It blocks + // until all the GroupChat requests are sent. Returns the new group and the + // round IDs the requests were sent on. Returns an error if at least one + // request to a member fails to send. Also returns the status of the sent + // requests. + MakeGroup(membership []*id.ID, name, message []byte) (gs.Group, []id.Round, + RequestStatus, error) + + // ResendRequest allows a GroupChat request to be sent again. It returns + // the rounds that the requests were sent on and the status of the send. + ResendRequest(groupID *id.ID) ([]id.Round, RequestStatus, error) + + // JoinGroup allows a user to accept a GroupChat request and stores the + // GroupChat as active to allow receiving and sending of messages from/to + // the GroupChat. A user can only join a GroupChat once. + JoinGroup(g gs.Group) error + + // LeaveGroup removes a group from a list of groups the user is a part of. + LeaveGroup(groupID *id.ID) error + + // Send sends a message to all GroupChat members using Client.SendManyCMIX. + // The send fails if the message is too long. + Send(groupID *id.ID, message []byte) (id.Round, error) + + // GetGroups returns a list of all registered GroupChat IDs. + GetGroups() []*id.ID + + // GetGroup returns the group with the matching ID or returns false if none + // exist. + GetGroup(groupID *id.ID) (gs.Group, bool) + + // NumGroups returns the number of groups the user is a part of. + NumGroups() int +} + +// RequestCallback is called when a GroupChat request is received. +type RequestCallback func(g gs.Group) + +// ReceiveCallback is called when a GroupChat message is received. +type ReceiveCallback func(msg MessageReceive) diff --git a/groupChat/groupStore/dhKeyList.go b/groupChat/groupStore/dhKeyList.go new file mode 100644 index 0000000000000000000000000000000000000000..50c405c426d7a27f2f99767208d2b2915a4f93b5 --- /dev/null +++ b/groupChat/groupStore/dhKeyList.go @@ -0,0 +1,139 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupStore + +import ( + "bytes" + "encoding/binary" + "github.com/pkg/errors" + "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/diffieHellman" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/primitives/id" + "sort" + "strings" +) + +// Error messages. +const ( + idUnmarshalErr = "failed to unmarshal member ID: %+v" + dhKeyDecodeErr = "failed to decode member DH key: %+v" +) + +type DhKeyList map[id.ID]*cyclic.Int + +// GenerateDhKeyList generates the symmetric/DH key between the user and all +// group members. +func GenerateDhKeyList(userID *id.ID, privKey *cyclic.Int, + members group.Membership, grp *cyclic.Group) DhKeyList { + dkl := make(DhKeyList, len(members)-1) + + for _, m := range members { + if !userID.Cmp(m.ID) { + dkl.Add(privKey, m, grp) + } + } + + return dkl +} + +// Add generates DH key between the user and the group member. The +func (dkl DhKeyList) Add(privKey *cyclic.Int, m group.Member, grp *cyclic.Group) { + dkl[*m.ID] = diffieHellman.GenerateSessionKey(privKey, m.DhKey, grp) +} + +// DeepCopy returns a copy of the DhKeyList. +func (dkl DhKeyList) DeepCopy() DhKeyList { + newDkl := make(DhKeyList, len(dkl)) + for uid, key := range dkl { + newDkl[uid] = key.DeepCopy() + } + return newDkl +} + +// Serialize serializes the DhKeyList and returns the byte slice. +func (dkl DhKeyList) Serialize() []byte { + buff := bytes.NewBuffer(nil) + + for uid, key := range dkl { + // Write ID + buff.Write(uid.Marshal()) + + // Write DH key length + b := make([]byte, 8) + keyBytes := key.BinaryEncode() + binary.LittleEndian.PutUint64(b, uint64(len(keyBytes))) + buff.Write(b) + + // Write DH key + buff.Write(keyBytes) + } + + return buff.Bytes() +} + +// DeserializeDhKeyList deserializes the bytes into a DhKeyList. +func DeserializeDhKeyList(data []byte) (DhKeyList, error) { + if len(data) == 0 { + return nil, nil + } + + buff := bytes.NewBuffer(data) + dkl := make(DhKeyList) + + for n := buff.Next(id.ArrIDLen); len(n) == id.ArrIDLen; n = buff.Next(id.ArrIDLen) { + // Read and unmarshal ID + uid, err := id.Unmarshal(n) + if err != nil { + return nil, errors.Errorf(idUnmarshalErr, err) + } + + // Get length of DH key + keyLen := int(binary.LittleEndian.Uint64(buff.Next(8))) + + // Read and decode DH key + key := &cyclic.Int{} + err = key.BinaryDecode(buff.Next(keyLen)) + if err != nil { + return nil, errors.Errorf(dhKeyDecodeErr, err) + } + + dkl[*uid] = key + } + + return dkl, nil +} + +// GoString returns all the elements in the DhKeyList as text in sorted order. +// This functions satisfies the fmt.GoStringer interface. +func (dkl DhKeyList) GoString() string { + str := make([]string, 0, len(dkl)) + + unsorted := make([]struct { + uid *id.ID + key *cyclic.Int + }, 0, len(dkl)) + + for uid, key := range dkl { + unsorted = append(unsorted, struct { + uid *id.ID + key *cyclic.Int + }{uid: uid.DeepCopy(), key: key.DeepCopy()}) + } + + sort.Slice(unsorted, func(i, j int) bool { + return bytes.Compare(unsorted[i].uid.Bytes(), + unsorted[j].uid.Bytes()) == -1 + }) + + for _, val := range unsorted { + str = append(str, val.uid.String()+": "+val.key.Text(10)) + } + + return "{" + strings.Join(str, ", ") + "}" +} diff --git a/groupChat/groupStore/dhKeyList_test.go b/groupChat/groupStore/dhKeyList_test.go new file mode 100644 index 0000000000000000000000000000000000000000..eca03f45a187c63a13276b7f11edfda2010e7b4d --- /dev/null +++ b/groupChat/groupStore/dhKeyList_test.go @@ -0,0 +1,93 @@ +package groupStore + +import ( + "math/rand" + "reflect" + "strings" + "testing" +) + +// // Unit test of GenerateDhKeyList. +// func TestGenerateDhKeyList(t *testing.T) { +// prng := rand.New(rand.NewSource(42)) +// grp := getGroup() +// userID := id.NewIdFromString("userID", id.User, t) +// privKey := grp.NewInt(42) +// pubKey := grp.ExpG(privKey, grp.NewInt(1)) +// members := createMembership(prng, 10, t) +// members[2].ID = userID +// members[2].DhKey = pubKey +// +// dkl := GenerateDhKeyList(userID, privKey, members, grp) +// +// t.Log(dkl) +// } + +// Unit test of DhKeyList.DeepCopy. +func TestDhKeyList_DeepCopy(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + dkl := createDhKeyList(prng, 10, t) + newDkl := dkl.DeepCopy() + + if !reflect.DeepEqual(dkl, newDkl) { + t.Errorf("DeepCopy() failed to return a copy of the original."+ + "\nexpected: %#v\nrecevied: %#v", dkl, newDkl) + } + + if &dkl == &newDkl { + t.Errorf("DeepCopy returned a copy of the pointer."+ + "\nexpected: %p\nreceived: %p", &dkl, &newDkl) + } +} + +// Tests that a DhKeyList that is serialized and deserialized matches the +// original. +func TestDhKeyList_Serialize_DeserializeDhKeyList(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + dkl := createDhKeyList(prng, 10, t) + + data := dkl.Serialize() + newDkl, err := DeserializeDhKeyList(data) + if err != nil { + t.Errorf("DeserializeDhKeyList returned an error: %+v", err) + } + + if !reflect.DeepEqual(dkl, newDkl) { + t.Errorf("Failed to serialize and deserialize DhKeyList."+ + "\nexpected: %#v\nreceived: %#v", dkl, newDkl) + } +} + +// Error path: an error is returned when DeserializeDhKeyList encounters invalid +// cyclic int. +func TestDeserializeDhKeyList_DhKeyBinaryDecodeError(t *testing.T) { + expectedErr := strings.SplitN(dhKeyDecodeErr, "%", 2)[0] + + _, err := DeserializeDhKeyList(make([]byte, 41)) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("DeserializeDhKeyList failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of DhKeyList.GoString. +func TestDhKeyList_GoString(t *testing.T) { + grp := createTestGroup(rand.New(rand.NewSource(42)), t) + expected := "{Grcjbkt1IWKQzyvrQsPKJzKFYPGqwGfOpui/RtSrK0YD: 5170411903... in GRP: 6SsQ/HAHUn..., QCxg8d6XgoPUoJo2+WwglBdG4+1NpkaprotPp7T8OiAD: 1754900790... in GRP: 6SsQ/HAHUn..., invD4ElbVxL+/b4MECiH4QDazS2IX2kstgfaAKEcHHAD: 2926033432... in GRP: 6SsQ/HAHUn..., wRYCP6iJdLrAyv2a0FaSsTYZ5ziWTf3Hno1TQ3NmHP0D: 2297312580... in GRP: 6SsQ/HAHUn..., 15ufnw07pVsMwNYUTIiFNYQay+BwmwdYCD9h03W8ArQD: 6199513233... in GRP: 6SsQ/HAHUn..., 3RqsBM4ux44bC6+uiBuCp1EQikLtPJA8qkNGWnhiBhYD: 4604475835... in GRP: 6SsQ/HAHUn..., 55ai4SlwXic/BckjJoKOKwVuOBdljhBhSYlH/fNEQQ4D: 9940605492... in GRP: 6SsQ/HAHUn..., 9PkZKU50joHnnku9b+NM3LqEPujWPoxP/hzr6lRtj6wD: 2451667393... in GRP: 6SsQ/HAHUn..., +hp17fHP0rO1EhnqeVM6v0SNLEedMmB1M5BZFMjMHPAD: 6029441980... in GRP: 6SsQ/HAHUn...}" + + if grp.DhKeys.GoString() != expected { + t.Errorf("GoString failed to return the expected string."+ + "\nexpected: %s\nreceived: %s", expected, grp.DhKeys.GoString()) + } +} + +// Tests that DhKeyList.GoString. returns the expected string for a nil map. +func TestDhKeyList_GoString_NilMap(t *testing.T) { + dkl := DhKeyList{} + expected := "{}" + + if dkl.GoString() != expected { + t.Errorf("GoString failed to return the expected string."+ + "\nexpected: %s\nreceived: %s", expected, dkl.GoString()) + } +} diff --git a/groupChat/groupStore/group.go b/groupChat/groupStore/group.go new file mode 100644 index 0000000000000000000000000000000000000000..98d0b489de808cf1ed5ca31818f4bc7f203fe878 --- /dev/null +++ b/groupChat/groupStore/group.go @@ -0,0 +1,235 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupStore + +import ( + "bytes" + "encoding/binary" + "fmt" + "github.com/pkg/errors" + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/netTime" + "strings" +) + +// Storage values. +const ( + // Key that is prepended to group ID to create a unique key to identify a + // Group in storage. + groupStorageKey = "GroupChat/" + groupStoreVersion = 0 +) + +// Error messages. +const ( + kvGetGroupErr = "failed to get group %s from storage: %+v" + membershipErr = "failed to deserialize member list: %+v" + dhKeyListErr = "failed to deserialize DH key list: %+v" +) + +// Group contains the membership list, the cryptographic information, and the +// identifying information of a group chat. +type Group struct { + Name []byte // Name of the group set by the user + ID *id.ID // Group ID + Key group.Key // Group key + IdPreimage group.IdPreimage // 256-bit value from CRNG + KeyPreimage group.KeyPreimage // 256-bit value from CRNG + InitMessage []byte // The original invite message + Members group.Membership // Sorted list of members in group + DhKeys DhKeyList // List of shared DH keys +} + +// NewGroup creates a new Group from copies of the given data. +func NewGroup(name []byte, groupID *id.ID, groupKey group.Key, + idPreimage group.IdPreimage, keyPreimage group.KeyPreimage, + initMessage []byte, members group.Membership, dhKeys DhKeyList) Group { + g := Group{ + Name: make([]byte, len(name)), + ID: groupID.DeepCopy(), + Key: groupKey, + IdPreimage: idPreimage, + KeyPreimage: keyPreimage, + InitMessage: make([]byte, len(initMessage)), + Members: members.DeepCopy(), + DhKeys: dhKeys, + } + + copy(g.Name, name) + copy(g.InitMessage, initMessage) + + return g +} + +// DeepCopy returns a copy of the Group. +func (g Group) DeepCopy() Group { + newGrp := Group{ + Name: make([]byte, len(g.Name)), + ID: g.ID.DeepCopy(), + Key: g.Key, + IdPreimage: g.IdPreimage, + KeyPreimage: g.KeyPreimage, + InitMessage: make([]byte, len(g.InitMessage)), + Members: g.Members.DeepCopy(), + DhKeys: make(map[id.ID]*cyclic.Int, len(g.Members)-1), + } + + copy(newGrp.Name, g.Name) + copy(newGrp.InitMessage, g.InitMessage) + + for uid, key := range g.DhKeys { + newGrp.DhKeys[uid] = key.DeepCopy() + } + + return newGrp +} + +// store saves an individual Group to storage keying on the group ID. +func (g Group) store(kv *versioned.KV) error { + obj := &versioned.Object{ + Version: groupStoreVersion, + Timestamp: netTime.Now(), + Data: g.Serialize(), + } + + return kv.Set(groupStoreKey(g.ID), groupStoreVersion, obj) +} + +// loadGroup returns the group with the corresponding ID from storage. +func loadGroup(groupID *id.ID, kv *versioned.KV) (Group, error) { + obj, err := kv.Get(groupStoreKey(groupID), groupStoreVersion) + if err != nil { + return Group{}, errors.Errorf(kvGetGroupErr, groupID, err) + } + + return DeserializeGroup(obj.Data) +} + +// removeGroup deletes the given group from storage. +func removeGroup(groupID *id.ID, kv *versioned.KV) error { + return kv.Delete(groupStoreKey(groupID), groupStoreVersion) +} + +// Serialize serializes the Group and returns the byte slice. +func (g Group) Serialize() []byte { + buff := bytes.NewBuffer(nil) + + // Write length of name and name + b := make([]byte, 8) + binary.LittleEndian.PutUint64(b, uint64(len(g.Name))) + buff.Write(b) + buff.Write(g.Name) + + // Write group ID + if g.ID != nil { + buff.Write(g.ID.Marshal()) + } else { + buff.Write(make([]byte, id.ArrIDLen)) + } + + // Write group key and preimages + buff.Write(g.Key[:]) + buff.Write(g.IdPreimage[:]) + buff.Write(g.KeyPreimage[:]) + + // Write length of InitMessage and InitMessage + b = make([]byte, 8) + binary.LittleEndian.PutUint64(b, uint64(len(g.InitMessage))) + buff.Write(b) + buff.Write(g.InitMessage) + + // Write length of group membership and group membership + b = make([]byte, 8) + memberBytes := g.Members.Serialize() + binary.LittleEndian.PutUint64(b, uint64(len(memberBytes))) + buff.Write(b) + buff.Write(memberBytes) + + // Write DH key list + buff.Write(g.DhKeys.Serialize()) + + return buff.Bytes() +} + +// DeserializeGroup deserializes the bytes into a Group. +func DeserializeGroup(data []byte) (Group, error) { + buff := bytes.NewBuffer(data) + var g Group + var err error + + // Get name + nameLen := binary.LittleEndian.Uint64(buff.Next(8)) + if nameLen > 0 { + g.Name = buff.Next(int(nameLen)) + } + + // Get group ID + var groupID id.ID + copy(groupID[:], buff.Next(id.ArrIDLen)) + if groupID == [id.ArrIDLen]byte{} { + g.ID = nil + } else { + g.ID = &groupID + } + + // Get group key and preimages + copy(g.Key[:], buff.Next(group.KeyLen)) + copy(g.IdPreimage[:], buff.Next(group.IdPreimageLen)) + copy(g.KeyPreimage[:], buff.Next(group.KeyPreimageLen)) + + // Get InitMessage + initMessageLength := binary.LittleEndian.Uint64(buff.Next(8)) + if initMessageLength > 0 { + g.InitMessage = buff.Next(int(initMessageLength)) + } + + // Get member list + membersLength := binary.LittleEndian.Uint64(buff.Next(8)) + g.Members, err = group.DeserializeMembership(buff.Next(int(membersLength))) + if err != nil { + return Group{}, errors.Errorf(membershipErr, err) + } + + // Get DH key list + g.DhKeys, err = DeserializeDhKeyList(buff.Bytes()) + if err != nil { + return Group{}, errors.Errorf(dhKeyListErr, err) + } + + return g, err +} + +// groupStoreKey generates a unique key to save and load a Group to/from storage. +func groupStoreKey(groupID *id.ID) string { + return groupStorageKey + groupID.String() +} + +// GoString returns all the Group's fields as text. This functions satisfies the +// fmt.GoStringer interface. +func (g Group) GoString() string { + idString := "<nil>" + if g.ID != nil { + idString = g.ID.String() + } + + str := make([]string, 8) + + str[0] = "Name:" + fmt.Sprintf("%q", g.Name) + str[1] = "ID:" + idString + str[2] = "Key:" + g.Key.String() + str[3] = "IdPreimage:" + g.IdPreimage.String() + str[4] = "KeyPreimage:" + g.KeyPreimage.String() + str[5] = "InitMessage:" + fmt.Sprintf("%q", g.InitMessage) + str[6] = "Members:" + g.Members.String() + str[7] = "DhKeys:" + g.DhKeys.GoString() + + return "{" + strings.Join(str, ", ") + "}" +} diff --git a/groupChat/groupStore/group_test.go b/groupChat/groupStore/group_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b0f84761b39c17059635c592e2f6f271471dbab1 --- /dev/null +++ b/groupChat/groupStore/group_test.go @@ -0,0 +1,289 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupStore + +import ( + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/elixxir/ekv" + "gitlab.com/xx_network/primitives/id" + "math/rand" + "reflect" + "strings" + "testing" +) + +// Unit test of NewGroup. +func TestNewGroup(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + membership := createMembership(prng, 10, t) + dkl := GenerateDhKeyList(membership[0].ID, randCycInt(prng), membership, getGroup()) + + expectedGroup := Group{ + Name: []byte(groupName), + ID: id.NewIdFromUInt(uint64(42), id.Group, t), + Key: newKey(groupKey), + IdPreimage: newIdPreimage(groupIdPreimage), + KeyPreimage: newKeyPreimage(groupKeyPreimage), + InitMessage: []byte(initMessage), + Members: membership, + DhKeys: dkl, + } + + receivedGroup := NewGroup( + []byte(groupName), + id.NewIdFromUInt(uint64(42), id.Group, t), + newKey(groupKey), + newIdPreimage(groupIdPreimage), + newKeyPreimage(groupKeyPreimage), + []byte(initMessage), + membership, + dkl, + ) + + if !reflect.DeepEqual(receivedGroup, expectedGroup) { + t.Errorf("NewGroup did not return the expected Group."+ + "\nexpected: %#v\nreceived: %#v", expectedGroup, receivedGroup) + } +} + +// Unit test of Group.DeepCopy. +func TestGroup_DeepCopy(t *testing.T) { + grp := createTestGroup(rand.New(rand.NewSource(42)), t) + + newGrp := grp.DeepCopy() + + if !reflect.DeepEqual(grp, newGrp) { + t.Errorf("DeepCopy did not return a copy of the original Group."+ + "\nexpected: %#v\nreceived: %#v", grp, newGrp) + } + + if &grp.Name[0] == &newGrp.Name[0] { + t.Errorf("DeepCopy returned a copy of the pointer of Name."+ + "\nexpected: %p\nreceived: %p", &grp.Name[0], &newGrp.Name[0]) + } + + if &grp.ID[0] == &newGrp.ID[0] { + t.Errorf("DeepCopy returned a copy of the pointer of ID."+ + "\nexpected: %p\nreceived: %p", &grp.ID[0], &newGrp.ID[0]) + } + + if &grp.Key[0] == &newGrp.Key[0] { + t.Errorf("DeepCopy returned a copy of the pointer of Key."+ + "\nexpected: %p\nreceived: %p", &grp.Key[0], &newGrp.Key[0]) + } + + if &grp.IdPreimage[0] == &newGrp.IdPreimage[0] { + t.Errorf("DeepCopy returned a copy of the pointer of IdPreimage."+ + "\nexpected: %p\nreceived: %p", &grp.IdPreimage[0], &newGrp.IdPreimage[0]) + } + + if &grp.KeyPreimage[0] == &newGrp.KeyPreimage[0] { + t.Errorf("DeepCopy returned a copy of the pointer of KeyPreimage."+ + "\nexpected: %p\nreceived: %p", &grp.KeyPreimage[0], &newGrp.KeyPreimage[0]) + } + + if &grp.InitMessage[0] == &newGrp.InitMessage[0] { + t.Errorf("DeepCopy returned a copy of the pointer of InitMessage."+ + "\nexpected: %p\nreceived: %p", &grp.InitMessage[0], &newGrp.InitMessage[0]) + } + + if &grp.Members[0] == &newGrp.Members[0] { + t.Errorf("DeepCopy returned a copy of the pointer of Members."+ + "\nexpected: %p\nreceived: %p", &grp.Members[0], &newGrp.Members[0]) + } +} + +// Unit test of Group.store. +func TestGroup_store(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + g := createTestGroup(rand.New(rand.NewSource(42)), t) + + err := g.store(kv) + if err != nil { + t.Errorf("store returned an error: %+v", err) + } + + obj, err := kv.Get(groupStoreKey(g.ID), groupStoreVersion) + if err != nil { + t.Errorf("Failed to get group from storage: %+v", err) + } + + newGrp, err := DeserializeGroup(obj.Data) + if err != nil { + t.Errorf("Failed to deserialize group: %+v", err) + } + + if !reflect.DeepEqual(g, newGrp) { + t.Errorf("Failed to read correct group from storage."+ + "\nexpected: %#v\nreceived: %#v", g, newGrp) + } +} + +// Unit test of Group.loadGroup. +func Test_loadGroup(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + g := createTestGroup(rand.New(rand.NewSource(42)), t) + + err := g.store(kv) + if err != nil { + t.Errorf("store returned an error: %+v", err) + } + + newGrp, err := loadGroup(g.ID, kv) + if err != nil { + t.Errorf("loadGroup returned an error: %+v", err) + } + + if !reflect.DeepEqual(g, newGrp) { + t.Errorf("loadGroup failed to return the expected group."+ + "\nexpected: %#v\nreceived: %#v", g, newGrp) + } +} + +// Error path: an error is returned when no group with the ID exists in storage. +func Test_loadGroup_InvalidGroupIdError(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + g := createTestGroup(rand.New(rand.NewSource(42)), t) + expectedErr := strings.SplitN(kvGetGroupErr, "%", 2)[0] + + _, err := loadGroup(g.ID, kv) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("loadGroup failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of Group.removeGroup. +func Test_removeGroup(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + g := createTestGroup(rand.New(rand.NewSource(42)), t) + + err := g.store(kv) + if err != nil { + t.Errorf("store returned an error: %+v", err) + } + + err = removeGroup(g.ID, kv) + if err != nil { + t.Errorf("removeGroup returned an error: %+v", err) + } + + foundGrp, err := loadGroup(g.ID, kv) + if err == nil { + t.Errorf("loadGroup found group that should have been removed: %#v", + foundGrp) + } +} + +// Tests that a group that is serialized and deserialized matches the original. +func TestGroup_Serialize_DeserializeGroup(t *testing.T) { + grp := createTestGroup(rand.New(rand.NewSource(42)), t) + + grpBytes := grp.Serialize() + + newGrp, err := DeserializeGroup(grpBytes) + if err != nil { + t.Errorf("DeserializeGroup returned an error: %+v", err) + } + + if !reflect.DeepEqual(grp, newGrp) { + t.Errorf("Deserialized group does not match original."+ + "\nexpected: %#v\nreceived: %#v", grp, newGrp) + } +} + +// Tests that a group with nil fields that is serialized and deserialized +// matches the original. +func TestGroup_Serialize_DeserializeGroup_NilGroup(t *testing.T) { + grp := Group{Members: make(group.Membership, 3)} + + grpBytes := grp.Serialize() + + newGrp, err := DeserializeGroup(grpBytes) + if err != nil { + t.Errorf("DeserializeGroup returned an error: %+v", err) + } + + if !reflect.DeepEqual(grp, newGrp) { + t.Errorf("Deserialized group does not match original."+ + "\nexpected: %#v\nreceived: %#v", grp, newGrp) + } +} + +// Error path: error returned when the group membership is too small. +func TestDeserializeGroup_DeserializeMembershipError(t *testing.T) { + grp := Group{} + grpBytes := grp.Serialize() + expectedErr := strings.SplitN(membershipErr, "%", 2)[0] + + _, err := DeserializeGroup(grpBytes) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("DeserializeGroup failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +func Test_groupStoreKey(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + expectedKeys := []string{ + "GroupChat/U4x/lrFkvxuXu59LtHLon1sUhPJSCcnZND6SugndnVID", + "GroupChat/15tNdkKbYXoMn58NO6VbDMDWFEyIhTWEGsvgcJsHWAgD", + "GroupChat/YdN1vAK0HfT5GSnhj9qeb4LlTnSOgeeeS71v40zcuoQD", + "GroupChat/6NY+jE/+HOvqVG2PrBPdGqwEzi6ih3xVec+ix44bC68D", + "GroupChat/iBuCp1EQikLtPJA8qkNGWnhiBhaXiu0M48bE8657w+AD", + "GroupChat/W1cS/v2+DBAoh+EA2s0tiF9pLLYH2gChHBxwceeWotwD", + "GroupChat/wlpbdLLhKXBeJz8FySMmgo4rBW44F2WOEGFJiUf980QD", + "GroupChat/DtTBFgI/qONXa2/tJ/+JdLrAyv2a0FaSsTYZ5ziWTf0D", + "GroupChat/no1TQ3NmHP1m10/sHhuJSRq3I25LdSFikM8r60LDyicD", + "GroupChat/hWDxqsBnzqbov0bUqytGgEAsX7KCDohdMmDx3peCg9QD", + } + for i, expected := range expectedKeys { + newID, _ := id.NewRandomID(prng, id.User) + + key := groupStoreKey(newID) + + if key != expected { + t.Errorf("groupStoreKey did not return the expected key (%d)."+ + "\nexpected: %s\nreceived: %s", i, expected, key) + } + + // fmt.Printf("\"%s\",\n", key) + } +} + +// Unit test of Group.GoString. +func TestGroup_GoString(t *testing.T) { + grp := createTestGroup(rand.New(rand.NewSource(42)), t) + expected := "{Name:\"groupName\", ID:ISTkX+tNhfEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE, Key:a2V5AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=, IdPreimage:aWRQcmVpbWFnZQAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=, KeyPreimage:a2V5UHJlaW1hZ2UAAAAAAAAAAAAAAAAAAAAAAAAAAAA=, InitMessage:\"initMessage\", Members:{Leader: {U4x/lrFkvxuXu59LtHLon1sUhPJSCcnZND6SugndnVID, 3534334367... in GRP: 6SsQ/HAHUn...}, Participants: 0: {Grcjbkt1IWKQzyvrQsPKJzKFYPGqwGfOpui/RtSrK0YD, 5274380952... in GRP: 6SsQ/HAHUn...}, 1: {QCxg8d6XgoPUoJo2+WwglBdG4+1NpkaprotPp7T8OiAD, 1628829379... in GRP: 6SsQ/HAHUn...}, 2: {invD4ElbVxL+/b4MECiH4QDazS2IX2kstgfaAKEcHHAD, 4157513341... in GRP: 6SsQ/HAHUn...}, 3: {wRYCP6iJdLrAyv2a0FaSsTYZ5ziWTf3Hno1TQ3NmHP0D, 5785305945... in GRP: 6SsQ/HAHUn...}, 4: {15ufnw07pVsMwNYUTIiFNYQay+BwmwdYCD9h03W8ArQD, 2010156224... in GRP: 6SsQ/HAHUn...}, 5: {3RqsBM4ux44bC6+uiBuCp1EQikLtPJA8qkNGWnhiBhYD, 2643318057... in GRP: 6SsQ/HAHUn...}, 6: {55ai4SlwXic/BckjJoKOKwVuOBdljhBhSYlH/fNEQQ4D, 6482807720... in GRP: 6SsQ/HAHUn...}, 7: {9PkZKU50joHnnku9b+NM3LqEPujWPoxP/hzr6lRtj6wD, 6603068123... in GRP: 6SsQ/HAHUn...}, 8: {+hp17fHP0rO1EhnqeVM6v0SNLEedMmB1M5BZFMjMHPAD, 2628757933... in GRP: 6SsQ/HAHUn...}}, DhKeys:{Grcjbkt1IWKQzyvrQsPKJzKFYPGqwGfOpui/RtSrK0YD: 5170411903... in GRP: 6SsQ/HAHUn..., QCxg8d6XgoPUoJo2+WwglBdG4+1NpkaprotPp7T8OiAD: 1754900790... in GRP: 6SsQ/HAHUn..., invD4ElbVxL+/b4MECiH4QDazS2IX2kstgfaAKEcHHAD: 2926033432... in GRP: 6SsQ/HAHUn..., wRYCP6iJdLrAyv2a0FaSsTYZ5ziWTf3Hno1TQ3NmHP0D: 2297312580... in GRP: 6SsQ/HAHUn..., 15ufnw07pVsMwNYUTIiFNYQay+BwmwdYCD9h03W8ArQD: 6199513233... in GRP: 6SsQ/HAHUn..., 3RqsBM4ux44bC6+uiBuCp1EQikLtPJA8qkNGWnhiBhYD: 4604475835... in GRP: 6SsQ/HAHUn..., 55ai4SlwXic/BckjJoKOKwVuOBdljhBhSYlH/fNEQQ4D: 9940605492... in GRP: 6SsQ/HAHUn..., 9PkZKU50joHnnku9b+NM3LqEPujWPoxP/hzr6lRtj6wD: 2451667393... in GRP: 6SsQ/HAHUn..., +hp17fHP0rO1EhnqeVM6v0SNLEedMmB1M5BZFMjMHPAD: 6029441980... in GRP: 6SsQ/HAHUn...}}" + + if grp.GoString() != expected { + t.Errorf("GoString failed to return the expected string."+ + "\nexpected: %s\nreceived: %s", expected, grp.GoString()) + } +} + +// Test that Group.GoString returns the expected string for a nil group. +func TestGroup_GoString_NilGroup(t *testing.T) { + grp := Group{} + expected := "{" + + "Name:\"\", " + + "ID:<nil>, " + + "Key:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=, " + + "IdPreimage:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=, " + + "KeyPreimage:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=, " + + "InitMessage:\"\", " + + "Members:{<nil>}, " + + "DhKeys:{}" + + "}" + + if grp.GoString() != expected { + t.Errorf("GoString failed to return the expected string."+ + "\nexpected: %s\nreceived: %s", expected, grp.GoString()) + } +} diff --git a/groupChat/groupStore/store.go b/groupChat/groupStore/store.go new file mode 100644 index 0000000000000000000000000000000000000000..88e980603e323114b2a0c39a138f0e2977627053 --- /dev/null +++ b/groupChat/groupStore/store.go @@ -0,0 +1,302 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupStore + +import ( + "bytes" + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/netTime" + "sync" + "testing" +) + +// Storage values. +const ( + // Key used to identify the list of Groups in storage. + groupStoragePrefix = "GroupChatListStore" + groupListStorageKey = "GroupChatList" + groupListVersion = 0 +) + +// Error messages. +const ( + kvGetGroupListErr = "failed to get list of group IDs from storage: %+v" + groupLoadErr = "failed to load group %d/%d: %+v" + groupSaveErr = "failed to save group %s to storage: %+v" + maxGroupsErr = "failed to add new group, max number of groups (%d) reached" + groupExistsErr = "group with ID %s already exists" + groupRemoveErr = "failed to remove group with ID %s, group not found in memory" + saveListRemoveErr = "failed to save new group ID list after removing group %s" + setUserPanic = "Store.SetUser is for testing only. Got %T" +) + +// The maximum number of group chats that a user can be a part of at once. +const MaxGroupChats = 64 + +// Store stores the list of Groups that a user is a part of. +type Store struct { + list map[id.ID]Group + user group.Member + kv *versioned.KV + mux sync.RWMutex +} + +// NewStore constructs a new Store object for the user and saves it to storage. +func NewStore(kv *versioned.KV, user group.Member) (*Store, error) { + s := &Store{ + list: make(map[id.ID]Group), + user: user.DeepCopy(), + kv: kv.Prefix(groupStoragePrefix), + } + + return s, s.save() +} + +// NewOrLoadStore loads the group store from storage or makes a new one if it +// does not exist. +func NewOrLoadStore(kv *versioned.KV, user group.Member) (*Store, error) { + prefixKv := kv.Prefix(groupStoragePrefix) + + // Load the list of group IDs from file if they exist + vo, err := prefixKv.Get(groupListStorageKey, groupListVersion) + if err == nil { + return loadStore(vo.Data, prefixKv, user) + } + + // If there is no group list saved, then make a new one + return NewStore(kv, user) +} + +// LoadStore loads all the Groups from storage into memory and return them in +// a Store object. +func LoadStore(kv *versioned.KV, user group.Member) (*Store, error) { + kv = kv.Prefix(groupStoragePrefix) + + // Load the list of group IDs from file + vo, err := kv.Get(groupListStorageKey, groupListVersion) + if err != nil { + return nil, errors.Errorf(kvGetGroupListErr, err) + } + + return loadStore(vo.Data, kv, user) +} + +// loadStore builds the list of group IDs and loads the groups from storage. +func loadStore(data []byte, kv *versioned.KV, user group.Member) (*Store, error) { + // Deserialize list of group IDs + groupIDs := deserializeGroupIdList(data) + + // Initialize the Store + s := &Store{ + list: make(map[id.ID]Group, len(groupIDs)), + user: user.DeepCopy(), + kv: kv, + } + + // Load each Group from storage into the map + for i, grpID := range groupIDs { + grp, err := loadGroup(grpID, kv) + if err != nil { + return nil, errors.Errorf(groupLoadErr, i, len(grpID), err) + } + s.list[*grpID] = grp + } + + return s, nil +} + +// saveGroupList saves a list of group IDs to storage. +func (s *Store) saveGroupList() error { + // Create the versioned object + obj := &versioned.Object{ + Version: groupListVersion, + Timestamp: netTime.Now(), + Data: serializeGroupIdList(s.list), + } + + // Save to storage + return s.kv.Set(groupListStorageKey, groupListVersion, obj) +} + +// serializeGroupIdList serializes the list of group IDs. +func serializeGroupIdList(list map[id.ID]Group) []byte { + buff := bytes.NewBuffer(nil) + buff.Grow(id.ArrIDLen * len(list)) + + // Create list of IDs from map + for grpId := range list { + buff.Write(grpId.Marshal()) + } + + return buff.Bytes() +} + +// deserializeGroupIdList deserializes data into a list of group IDs. +func deserializeGroupIdList(data []byte) []*id.ID { + idLen := id.ArrIDLen + groupIDs := make([]*id.ID, 0, len(data)/idLen) + buff := bytes.NewBuffer(data) + + // Copy each set of data into a new ID and append to list + for n := buff.Next(idLen); len(n) == idLen; n = buff.Next(idLen) { + var newID id.ID + copy(newID[:], n) + groupIDs = append(groupIDs, &newID) + } + + return groupIDs +} + +// save saves the group ID list and each group individually to storage. +func (s *Store) save() error { + // Store group ID list + err := s.saveGroupList() + if err != nil { + return err + } + + // Store individual groups + for grpID, grp := range s.list { + if err := grp.store(s.kv); err != nil { + return errors.Errorf(groupSaveErr, grpID, err) + } + } + + return nil +} + +// Len returns the number of groups stored. +func (s *Store) Len() int { + s.mux.RLock() + defer s.mux.RUnlock() + + return len(s.list) +} + +// Add adds a new group to the group list and saves it to storage. An error is +// returned if the user has the max number of groups (MaxGroupChats). +func (s *Store) Add(g Group) error { + s.mux.Lock() + defer s.mux.Unlock() + + // Check if the group list is full. + if len(s.list) >= MaxGroupChats { + return errors.Errorf(maxGroupsErr, MaxGroupChats) + } + + // Return an error if the group already exists in the map + if _, exists := s.list[*g.ID]; exists { + return errors.Errorf(groupExistsErr, g.ID) + } + + // Add the group to the map + s.list[*g.ID] = g.DeepCopy() + + // Update the group list in storage + err := s.saveGroupList() + if err != nil { + return err + } + + // Store the group to storage + return g.store(s.kv) +} + +// Remove removes the group with the corresponding ID from memory and storage. +// An error is returned if the group cannot be found in memory or storage. +func (s *Store) Remove(groupID *id.ID) error { + s.mux.Lock() + defer s.mux.Unlock() + + // Exit if the Group does not exist in memory + if _, exists := s.list[*groupID]; !exists { + return errors.Errorf(groupRemoveErr, groupID) + } + + // Delete Group from memory + delete(s.list, *groupID) + + // Remove group ID from list in memory + err := s.saveGroupList() + if err != nil { + return errors.Errorf(saveListRemoveErr, groupID) + } + + // Delete Group from storage + return removeGroup(groupID, s.kv) +} + +// GroupIDs returns a list of all group IDs. +func (s *Store) GroupIDs() []*id.ID { + s.mux.RLock() + defer s.mux.RUnlock() + + idList := make([]*id.ID, 0, len(s.list)) + for gid := range s.list { + idList = append(idList, gid.DeepCopy()) + } + + return idList +} + +// Get returns the Group for the given group ID. Returns false if no Group is +// found. +func (s *Store) Get(groupID *id.ID) (Group, bool) { + s.mux.RLock() + defer s.mux.RUnlock() + + grp, exists := s.list[*groupID] + if !exists { + return Group{}, false + } + + return grp.DeepCopy(), exists +} + +// GetByKeyFp returns the group with the matching key fingerprint and salt. +// Returns false if no group is found. +func (s *Store) GetByKeyFp(keyFp format.Fingerprint, salt [group.SaltLen]byte) (Group, bool) { + s.mux.RLock() + defer s.mux.RUnlock() + + // Iterate through each group to check if the key fingerprint matches + for _, grp := range s.list { + if group.CheckKeyFingerprint(keyFp, grp.Key, salt, s.user.ID) { + return grp.DeepCopy(), true + } + } + + return Group{}, false +} + +// GetUser returns the group member for the current user. +func (s *Store) GetUser() group.Member { + s.mux.RLock() + defer s.mux.RUnlock() + return s.user.DeepCopy() +} + +// SetUser allows a user to be set. This function is for testing purposes only. +// It panics if the interface is not of a testing type. +func (s *Store) SetUser(user group.Member, x interface{}) { + switch x.(type) { + case *testing.T, *testing.M, *testing.B, *testing.PB: + break + default: + jww.FATAL.Panicf(setUserPanic, x) + } + + s.mux.Lock() + defer s.mux.Unlock() + s.user = user.DeepCopy() +} diff --git a/groupChat/groupStore/store_test.go b/groupChat/groupStore/store_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0e0917ff38ce33b83cbecbfafb4f960ea8c38447 --- /dev/null +++ b/groupChat/groupStore/store_test.go @@ -0,0 +1,576 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupStore + +import ( + "bytes" + "fmt" + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/elixxir/ekv" + "gitlab.com/xx_network/primitives/id" + "math/rand" + "reflect" + "sort" + "strings" + "testing" +) + +// Unit test of NewStore. +func TestNewStore(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + + expectedStore := &Store{ + list: make(map[id.ID]Group), + user: user, + kv: kv.Prefix(groupStoragePrefix), + } + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("NewStore returned an error: %+v", err) + } + + // Compare manually created object with NewUnknownRoundsStore + if !reflect.DeepEqual(expectedStore, store) { + t.Errorf("NewStore returned incorrect Store."+ + "\nexpected: %+v\nreceived: %+v", expectedStore, store) + } + + // Add information in store + testGroup := createTestGroup(prng, t) + + store.list[*testGroup.ID] = testGroup + + if err := store.save(); err != nil { + t.Fatalf("save() could not write to disk: %+v", err) + } + + groupIds := make([]id.ID, 0, len(store.list)) + for grpId := range store.list { + groupIds = append(groupIds, grpId) + } + + // Check that stored group Id list is expected value + expectedData := serializeGroupIdList(store.list) + + obj, err := store.kv.Get(groupListStorageKey, groupListVersion) + if err != nil { + t.Errorf("Could not get group list: %+v", err) + } + + // Check that the stored data is the data outputted by marshal + if !bytes.Equal(expectedData, obj.Data) { + t.Errorf("NewStore() returned incorrect Store."+ + "\nexpected: %+v\nreceived: %+v", expectedData, obj.Data) + } + + obj, err = store.kv.Get(groupStoreKey(testGroup.ID), groupListVersion) + if err != nil { + t.Errorf("Could not get group: %+v", err) + } + + newGrp, err := DeserializeGroup(obj.Data) + if err != nil { + t.Errorf("Failed to deserialize group: %+v", err) + } + + if !reflect.DeepEqual(testGroup, newGrp) { + t.Errorf("NewStore() returned incorrect Store."+ + "\nexpected: %#v\nreceived: %#v", testGroup, newGrp) + } +} + +func TestNewOrLoadStore(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + + store, err := NewOrLoadStore(kv, user) + if err != nil { + t.Fatalf("Failed to create new store: %+v", err) + } + + // Add group to store + testGroup := createTestGroup(prng, t) + if err = store.Add(testGroup); err != nil { + t.Fatalf("Failed to add test group: %+v", err) + } + + // Load the store from kv + receivedStore, err := NewOrLoadStore(kv, user) + if err != nil { + t.Fatalf("LoadStore returned an error: %+v", err) + } + + // Check that state in loaded store matches store that was saved + if len(receivedStore.list) != len(store.list) { + t.Errorf("LoadStore returned Store with incorrect number of groups."+ + "\nexpected len: %d\nreceived len: %d", + len(store.list), len(receivedStore.list)) + } + + if _, exists := receivedStore.list[*testGroup.ID]; !exists { + t.Fatalf("Failed to get group from loaded group map."+ + "\nexpected: %#v\nreceived: %#v", testGroup, receivedStore.list) + } +} + +// Unit test of LoadStore. +func TestLoadStore(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to create new store: %+v", err) + } + + // Add group to store + testGroup := createTestGroup(prng, t) + if err = store.Add(testGroup); err != nil { + t.Fatalf("Failed to add test group: %+v", err) + } + + // Load the store from kv + receivedStore, err := LoadStore(kv, user) + if err != nil { + t.Fatalf("LoadStore returned an error: %+v", err) + } + + // Check that state in loaded store matches store that was saved + if len(receivedStore.list) != len(store.list) { + t.Errorf("LoadStore returned Store with incorrect number of groups."+ + "\nexpected len: %d\nreceived len: %d", + len(store.list), len(receivedStore.list)) + } + + if _, exists := receivedStore.list[*testGroup.ID]; !exists { + t.Fatalf("Failed to get group from loaded group map."+ + "\nexpected: %#v\nreceived: %#v", testGroup, receivedStore.list) + } +} + +// Error path: show that LoadStore returns an error when no group store can be +// found in storage. +func TestLoadStore_GetError(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(rand.New(rand.NewSource(42))) + expectedErr := strings.SplitN(kvGetGroupListErr, "%", 2)[0] + + // Load the store from kv + _, err := LoadStore(kv, user) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("LoadStore did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: show that loadStore returns an error when no group can be found +// in storage. +func Test_loadStore_GetGroupError(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(rand.New(rand.NewSource(42))) + var idList []byte + for i := 0; i < 10; i++ { + idList = append(idList, id.NewIdFromUInt(uint64(i), id.Group, t).Marshal()...) + } + expectedErr := strings.SplitN(groupLoadErr, "%", 2)[0] + + // Load the groups from kv + _, err := loadStore(idList, kv, user) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("loadStore did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } + +} + +// Tests that a map of groups can be serialized and deserialized into a list +// that has the same group IDs. +func Test_serializeGroupIdList_deserializeGroupIdList(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + n := 10 + testMap := make(map[id.ID]Group, n) + expected := make([]*id.ID, n) + for i := 0; i < n; i++ { + grp := createTestGroup(prng, t) + expected[i] = grp.ID + testMap[*grp.ID] = grp + } + + // Serialize and deserialize map + data := serializeGroupIdList(testMap) + newList := deserializeGroupIdList(data) + + // Sort expected and received lists so they are in the same order + sort.Slice(expected, func(i, j int) bool { + return bytes.Compare(expected[i].Bytes(), expected[j].Bytes()) == -1 + }) + sort.Slice(newList, func(i, j int) bool { + return bytes.Compare(newList[i].Bytes(), newList[j].Bytes()) == -1 + }) + + // Check if they match + if !reflect.DeepEqual(expected, newList) { + t.Errorf("Failed to serialize and deserilize group map into list."+ + "\nexpected: %+v\nreceived: %+v", expected, newList) + } +} + +// Unit test of Store.Len. +func TestStore_Len(t *testing.T) { + s := Store{list: make(map[id.ID]Group)} + + if s.Len() != 0 { + t.Errorf("Len returned the wrong length.\nexpected: %d\nreceived: %d", + 0, s.Len()) + } + + n := 10 + for i := 0; i < n; i++ { + s.list[*id.NewIdFromUInt(uint64(i), id.Group, t)] = Group{} + } + + if s.Len() != n { + t.Errorf("Len returned the wrong length.\nexpected: %d\nreceived: %d", + n, s.Len()) + } +} + +// Unit test of Store.Add. +func TestStore_Add(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to create store: %+v", err) + } + + // Add maximum number of groups allowed + for i := 0; i < MaxGroupChats; i++ { + // Add group to store + grp := createTestGroup(prng, t) + err = store.Add(grp) + if err != nil { + t.Errorf("Add returned an error (%d): %v", i, err) + } + + if _, exists := store.list[*grp.ID]; !exists { + t.Errorf("Group %s was not added to the map (%d)", grp.ID, i) + } + } + + if len(store.list) != MaxGroupChats { + t.Errorf("Length of group map does not match number of groups added."+ + "\nexpected: %d\nreceived: %d", MaxGroupChats, len(store.list)) + } +} + +// Error path: shows that an error is returned when trying to add too many +// groups. +func TestStore_Add_MapFullError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + expectedErr := strings.SplitN(maxGroupsErr, "%", 2)[0] + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to create store: %+v", err) + } + + // Add maximum number of groups allowed + for i := 0; i < MaxGroupChats; i++ { + err = store.Add(createTestGroup(prng, t)) + if err != nil { + t.Errorf("Add returned an error (%d): %v", i, err) + } + } + + err = store.Add(createTestGroup(prng, t)) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("Add did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: show Store.Add returns an error when attempting to add a group +// that is already in the map. +func TestStore_Add_GroupExistsError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + expectedErr := strings.SplitN(groupExistsErr, "%", 2)[0] + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to create store: %+v", err) + } + + grp := createTestGroup(prng, t) + err = store.Add(grp) + if err != nil { + t.Errorf("Add returned an error: %+v", err) + } + + err = store.Add(grp) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("Add did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of Store.Remove. +func TestStore_Remove(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to create store: %+v", err) + } + + // Add maximum number of groups allowed + groups := make([]Group, MaxGroupChats) + for i := 0; i < MaxGroupChats; i++ { + groups[i] = createTestGroup(prng, t) + if err = store.Add(groups[i]); err != nil { + t.Errorf("Failed to add group (%d): %v", i, err) + } + } + + // Remove all groups + for i, grp := range groups { + err = store.Remove(grp.ID) + if err != nil { + t.Errorf("Remove returned an error (%d): %+v", i, err) + } + + if _, exists := store.list[*grp.ID]; exists { + t.Fatalf("Group %s still exists in map (%d).", grp.ID, i) + } + } + + // Check that the list is empty now + if len(store.list) != 0 { + t.Fatalf("Remove failed to remove all groups.."+ + "\nexpected: %d\nreceived: %d", 0, len(store.list)) + } +} + +// Error path: shows that Store.Remove returns an error when no group with the +// given ID is found in the map. +func TestStore_Remove_RemoveGroupNotInMemoryError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + expectedErr := strings.SplitN(groupRemoveErr, "%", 2)[0] + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to create store: %+v", err) + } + + grp := createTestGroup(prng, t) + err = store.Remove(grp.ID) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("Remove did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of Store.GroupIDs. +func TestStore_GroupIDs(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + n := 10 + store := Store{list: make(map[id.ID]Group, n)} + expected := make([]*id.ID, n) + for i := 0; i < n; i++ { + grp := createTestGroup(prng, t) + expected[i] = grp.ID + store.list[*grp.ID] = grp + } + + newList := store.GroupIDs() + + // Sort expected and received lists so they are in the same order + sort.Slice(expected, func(i, j int) bool { + return bytes.Compare(expected[i].Bytes(), expected[j].Bytes()) == -1 + }) + sort.Slice(newList, func(i, j int) bool { + return bytes.Compare(newList[i].Bytes(), newList[j].Bytes()) == -1 + }) + + // Check if they match + if !reflect.DeepEqual(expected, newList) { + t.Errorf("GroupIDs did not return the expected list."+ + "\nexpected: %+v\nreceived: %+v", expected, newList) + } +} + +// Unit test of Store.Get. +func TestStore_Get(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to make new Store: %+v", err) + } + + // Add group to store + grp := createTestGroup(prng, t) + if err = store.Add(grp); err != nil { + t.Errorf("Failed to add group to store: %+v", err) + } + + // Attempt to get group + retrieved, exists := store.Get(grp.ID) + if !exists { + t.Errorf("Get failed to return the expected group: %#v", grp) + } + + if !reflect.DeepEqual(grp, retrieved) { + t.Errorf("Get did not return the expected group."+ + "\nexpected: %#v\nreceived: %#v", grp, retrieved) + } +} + +// Error path: shows that Store.Get return false if no group is found. +func TestStore_Get_NoGroupError(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(rand.New(rand.NewSource(42))) + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to make new Store: %+v", err) + } + + // Attempt to get group + retrieved, exists := store.Get(id.NewIdFromString("testID", id.Group, t)) + if exists { + t.Errorf("Get returned a group that should not exist: %#v", retrieved) + } +} + +// Unit test of Store.GetByKeyFp. +func TestStore_GetByKeyFp(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to make new Store: %+v", err) + } + + // Add group to store + grp := createTestGroup(prng, t) + if err = store.Add(grp); err != nil { + t.Fatalf("Failed to add group: %+v", err) + } + + // Get group by fingerprint + salt := newSalt(groupSalt) + generatedFP := group.NewKeyFingerprint(grp.Key, salt, store.user.ID) + retrieved, exists := store.GetByKeyFp(generatedFP, salt) + if !exists { + t.Errorf("GetByKeyFp failed to find a group with the matching key "+ + "fingerprint: %#v", grp) + } + + // check that retrieved value match + if !reflect.DeepEqual(grp, retrieved) { + t.Errorf("GetByKeyFp failed to return the expected group."+ + "\nexpected: %#v\nreceived: %#v", grp, retrieved) + } +} + +// Error path: shows that Store.GetByKeyFp return false if no group is found. +func TestStore_GetByKeyFp_NoGroupError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(prng) + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to make new Store: %+v", err) + } + + // Get group by fingerprint + grp := createTestGroup(prng, t) + salt := newSalt(groupSalt) + generatedFP := group.NewKeyFingerprint(grp.Key, salt, store.user.ID) + retrieved, exists := store.GetByKeyFp(generatedFP, salt) + if exists { + t.Errorf("GetByKeyFp found a group when none should exist: %#v", + retrieved) + } +} + +// Unit test of Store.GetUser. +func TestStore_GetUser(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + user := randMember(rand.New(rand.NewSource(42))) + + store, err := NewStore(kv, user) + if err != nil { + t.Fatalf("Failed to make new Store: %+v", err) + } + + if !user.Equal(store.GetUser()) { + t.Errorf("GetUser() failed to return the expected member."+ + "\nexpected: %#v\nreceived: %#v", user, store.GetUser()) + } +} + +// Unit test of Store.SetUser. +func TestStore_SetUser(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + prng := rand.New(rand.NewSource(42)) + oldUser := randMember(prng) + newUser := randMember(prng) + + store, err := NewStore(kv, oldUser) + if err != nil { + t.Fatalf("Failed to make new Store: %+v", err) + } + + store.SetUser(newUser, t) + + if !newUser.Equal(store.user) { + t.Errorf("SetUser() failed to set the correct user."+ + "\nexpected: %#v\nreceived: %#v", newUser, store.user) + } +} + +// Panic path: show that Store.SetUser panics when the interface is not of a +// testing type. +func TestStore_SetUser_NonTestingInterfacePanic(t *testing.T) { + user := randMember(rand.New(rand.NewSource(42))) + store := &Store{} + nonTestingInterface := struct{}{} + expectedErr := fmt.Sprintf(setUserPanic, nonTestingInterface) + + defer func() { + if r := recover(); r == nil || r.(string) != expectedErr { + t.Errorf("SetUser failed to panic with the expected message."+ + "\nexpected: %s\nreceived: %+v", expectedErr, r) + } + }() + + store.SetUser(user, nonTestingInterface) +} diff --git a/groupChat/groupStore/utils_test.go b/groupChat/groupStore/utils_test.go new file mode 100644 index 0000000000000000000000000000000000000000..9a6460800498b11fdbcb8a4b9b5aa7d4e391b2f3 --- /dev/null +++ b/groupChat/groupStore/utils_test.go @@ -0,0 +1,139 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupStore + +import ( + "gitlab.com/elixxir/crypto/contact" + "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/crypto/large" + "gitlab.com/xx_network/primitives/id" + "math/rand" + "testing" +) + +const ( + groupName = "groupName" + groupSalt = "salt" + groupKey = "key" + groupIdPreimage = "idPreimage" + groupKeyPreimage = "keyPreimage" + initMessage = "initMessage" +) + +// createTestGroup generates a new group for testing. +func createTestGroup(rng *rand.Rand, t *testing.T) Group { + members := createMembership(rng, 10, t) + dkl := GenerateDhKeyList(members[0].ID, randCycInt(rng), members, getGroup()) + return NewGroup( + []byte(groupName), + id.NewIdFromUInt(rng.Uint64(), id.Group, t), + newKey(groupKey), + newIdPreimage(groupIdPreimage), + newKeyPreimage(groupKeyPreimage), + []byte(initMessage), + members, + dkl, + ) +} + +// createMembership creates a new membership with the specified number of +// randomly generated members. +func createMembership(rng *rand.Rand, size int, t *testing.T) group.Membership { + contacts := make([]contact.Contact, size) + for i := range contacts { + contacts[i] = randContact(rng) + } + + membership, err := group.NewMembership(contacts[0], contacts[1:]...) + if err != nil { + t.Errorf("Failed to create new membership: %+v", err) + } + + return membership +} + +// createDhKeyList creates a new DhKeyList with the specified number of randomly +// generated members. +func createDhKeyList(rng *rand.Rand, size int, _ *testing.T) DhKeyList { + dkl := make(DhKeyList, size) + for i := 0; i < size; i++ { + dkl[*randID(rng, id.User)] = randCycInt(rng) + } + + return dkl +} + +// randMember returns a Member with a random ID and DH public key. +func randMember(rng *rand.Rand) group.Member { + return group.Member{ + ID: randID(rng, id.User), + DhKey: randCycInt(rng), + } +} + +// randContact returns a contact with a random ID and DH public key. +func randContact(rng *rand.Rand) contact.Contact { + return contact.Contact{ + ID: randID(rng, id.User), + DhPubKey: randCycInt(rng), + } +} + +// randID returns a new random ID of the specified type. +func randID(rng *rand.Rand, t id.Type) *id.ID { + newID, _ := id.NewRandomID(rng, t) + return newID +} + +// randCycInt returns a random cyclic int. +func randCycInt(rng *rand.Rand) *cyclic.Int { + return getGroup().NewInt(rng.Int63()) +} + +func getGroup() *cyclic.Group { + return cyclic.NewGroup( + large.NewIntFromString("E2EE983D031DC1DB6F1A7A67DF0E9A8E5561DB8E8D4941"+ + "3394C049B7A8ACCEDC298708F121951D9CF920EC5D146727AA4AE535B0922C688"+ + "B55B3DD2AEDF6C01C94764DAB937935AA83BE36E67760713AB44A6337C20E7861"+ + "575E745D31F8B9E9AD8412118C62A3E2E29DF46B0864D0C951C394A5CBBDC6ADC"+ + "718DD2A3E041023DBB5AB23EBB4742DE9C1687B5B34FA48C3521632C4A530E8FF"+ + "B1BC51DADDF453B0B2717C2BC6669ED76B4BDD5C9FF558E88F26E5785302BEDBC"+ + "A23EAC5ACE92096EE8A60642FB61E8F3D24990B8CB12EE448EEF78E184C7242DD"+ + "161C7738F32BF29A841698978825B4111B4BC3E1E198455095958333D776D8B2B"+ + "EEED3A1A1A221A6E37E664A64B83981C46FFDDC1A45E3D5211AAF8BFBC072768C"+ + "4F50D7D7803D2D4F278DE8014A47323631D7E064DE81C0C6BFA43EF0E6998860F"+ + "1390B5D3FEACAF1696015CB79C3F9C2D93D961120CD0E5F12CBB687EAB045241F"+ + "96789C38E89D796138E6319BE62E35D87B1048CA28BE389B575E994DCA7554715"+ + "84A09EC723742DC35873847AEF49F66E43873", 16), + large.NewIntFromString("2", 16)) +} + +func newSalt(s string) [group.SaltLen]byte { + var salt [group.SaltLen]byte + copy(salt[:], s) + return salt +} + +func newKey(s string) group.Key { + var key group.Key + copy(key[:], s) + return key +} + +func newIdPreimage(s string) group.IdPreimage { + var preimage group.IdPreimage + copy(preimage[:], s) + return preimage +} + +func newKeyPreimage(s string) group.KeyPreimage { + var preimage group.KeyPreimage + copy(preimage[:], s) + return preimage +} diff --git a/groupChat/internalFormat.go b/groupChat/internalFormat.go new file mode 100644 index 0000000000000000000000000000000000000000..2502a9c8c29c9f940a93bebb1101c5d5a5ae8ef2 --- /dev/null +++ b/groupChat/internalFormat.go @@ -0,0 +1,156 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "encoding/binary" + "fmt" + "github.com/pkg/errors" + "gitlab.com/xx_network/primitives/id" + "strconv" + "time" +) + +// Sizes of marshaled data, in bytes. +const ( + timestampLen = 8 + idLen = id.ArrIDLen + internalPayloadSizeLen = 2 + internalMinLen = timestampLen + idLen + internalPayloadSizeLen +) + +// Error messages +const ( + newInternalSizeErr = "max message size %d < %d minimum required" + unmarshalInternalSizeErr = "size of data %d < %d minimum required" +) + +// internalMsg is the internal, unencrypted data in a group message. +// +// +-------------------------------------------+ +// | data | +// +-----------+----------+---------+----------+ +// | timestamp | senderID | size | payload | +// | 8 bytes | 32 bytes | 2 bytes | variable | +// +-----------+----------+---------+----------+ +type internalMsg struct { + data []byte // Serial of all the parts of the message + timestamp []byte // 64-bit Unix time timestamp stored in nanoseconds + senderID []byte // 264-bit sender ID + size []byte // Size of the payload + payload []byte // Message contents +} + +// newInternalMsg creates a new internalMsg of size maxDataSize. An error is +// returned if the maxDataSize is smaller than the minimum internalMsg size. +func newInternalMsg(maxDataSize int) (internalMsg, error) { + if maxDataSize < internalMinLen { + return internalMsg{}, + errors.Errorf(newInternalSizeErr, maxDataSize, internalMinLen) + } + + return mapInternalMsg(make([]byte, maxDataSize)), nil +} + +// mapInternalMsg maps all the parts of the internalMsg to the passed in data. +func mapInternalMsg(data []byte) internalMsg { + return internalMsg{ + data: data, + timestamp: data[:timestampLen], + senderID: data[timestampLen : timestampLen+idLen], + size: data[timestampLen+idLen : timestampLen+idLen+internalPayloadSizeLen], + payload: data[timestampLen+idLen+internalPayloadSizeLen:], + } +} + +// unmarshalInternalMsg unmarshal the data into an internalMsg. An error is +// returned if the data length is smaller than the minimum allowed size. +func unmarshalInternalMsg(data []byte) (internalMsg, error) { + if len(data) < internalMinLen { + return internalMsg{}, + errors.Errorf(unmarshalInternalSizeErr, len(data), internalMinLen) + } + + return mapInternalMsg(data), nil +} + +// Marshal returns the serial of the internalMsg. +func (im internalMsg) Marshal() []byte { + return im.data +} + +// GetTimestamp returns the timestamp as a time.Time. +func (im internalMsg) GetTimestamp() time.Time { + return time.Unix(0, int64(binary.LittleEndian.Uint64(im.timestamp))) +} + +// SetTimestamp converts the time.Time to Unix nano and save as bytes. +func (im internalMsg) SetTimestamp(t time.Time) { + binary.LittleEndian.PutUint64(im.timestamp, uint64(t.UnixNano())) +} + +// GetSenderID returns the sender ID bytes as a id.ID. +func (im internalMsg) GetSenderID() (*id.ID, error) { + return id.Unmarshal(im.senderID) +} + +// SetSenderID sets the sender ID. +func (im internalMsg) SetSenderID(sid *id.ID) { + copy(im.senderID, sid.Marshal()) +} + +// GetPayload returns the payload truncated to the correct size. +func (im internalMsg) GetPayload() []byte { + return im.payload[:im.GetPayloadSize()] +} + +// SetPayload sets the payload and saves it size. +func (im internalMsg) SetPayload(payload []byte) { + // Save size of payload + binary.LittleEndian.PutUint16(im.size, uint16(len(payload))) + + // Save payload + copy(im.payload, payload) +} + +// GetPayloadSize returns the length of the content in the payload. +func (im internalMsg) GetPayloadSize() int { + return int(binary.LittleEndian.Uint16(im.size)) +} + +// GetPayloadMaxSize returns the maximum size of the payload. +func (im internalMsg) GetPayloadMaxSize() int { + return len(im.payload) +} + +// String prints a string representation of internalMsg. This functions +// satisfies the fmt.Stringer interface. +func (im internalMsg) String() string { + timestamp := "<nil>" + if len(im.timestamp) > 0 { + timestamp = im.GetTimestamp().String() + } + + senderID := "<nil>" + if sid, _ := im.GetSenderID(); sid != nil { + senderID = sid.String() + } + + size := "<nil>" + if len(im.size) > 0 { + size = strconv.Itoa(im.GetPayloadSize()) + } + + payload := "<nil>" + if len(im.size) > 0 { + payload = fmt.Sprintf("%q", im.GetPayload()) + } + + return "{timestamp:" + timestamp + ", senderID:" + senderID + + ", size:" + size + ", payload:" + payload + "}" +} diff --git a/groupChat/internalFormat_test.go b/groupChat/internalFormat_test.go new file mode 100644 index 0000000000000000000000000000000000000000..984d11b8f35ec44935eaea48b5bbd76eec80bdb1 --- /dev/null +++ b/groupChat/internalFormat_test.go @@ -0,0 +1,211 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "bytes" + "encoding/binary" + "fmt" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/netTime" + "reflect" + "testing" + "time" +) + +// Unit test of newInternalMsg. +func Test_newInternalMsg(t *testing.T) { + maxDataSize := 2 * internalMinLen + im, err := newInternalMsg(maxDataSize) + if err != nil { + t.Errorf("newInternalMsg() returned an error: %+v", err) + } + + if len(im.data) != maxDataSize { + t.Errorf("newInternalMsg() set data to the wrong length."+ + "\nexpected: %d\nreceived: %d", maxDataSize, len(im.data)) + } +} + +// Error path: the maxDataSize is smaller than the minimum size. +func Test_newInternalMsg_PayloadSizeError(t *testing.T) { + maxDataSize := internalMinLen - 1 + expectedErr := fmt.Sprintf(newInternalSizeErr, maxDataSize, internalMinLen) + + _, err := newInternalMsg(maxDataSize) + if err == nil || err.Error() != expectedErr { + t.Errorf("newInternalMsg() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of mapInternalMsg. +func Test_mapInternalMsg(t *testing.T) { + // Create all the expected data + timestamp := make([]byte, timestampLen) + binary.LittleEndian.PutUint64(timestamp, uint64(netTime.Now().UnixNano())) + senderID := id.NewIdFromString("test sender ID", id.User, t).Marshal() + payload := []byte("Sample payload contents.") + size := make([]byte, internalPayloadSizeLen) + binary.LittleEndian.PutUint16(size, uint16(len(payload))) + + // Construct data into single slice + data := bytes.NewBuffer(nil) + data.Write(timestamp) + data.Write(senderID) + data.Write(size) + data.Write(payload) + + // Map data + im := mapInternalMsg(data.Bytes()) + + // Check that the mapped values match the expected values + if !bytes.Equal(timestamp, im.timestamp) { + t.Errorf("mapInternalMsg() did not correctly map timestamp."+ + "\nexpected: %+v\nreceived: %+v", timestamp, im.timestamp) + } + + if !bytes.Equal(senderID, im.senderID) { + t.Errorf("mapInternalMsg() did not correctly map senderID."+ + "\nexpected: %+v\nreceived: %+v", senderID, im.senderID) + } + + if !bytes.Equal(size, im.size) { + t.Errorf("mapInternalMsg() did not correctly map size."+ + "\nexpected: %+v\nreceived: %+v", size, im.size) + } + + if !bytes.Equal(payload, im.payload) { + t.Errorf("mapInternalMsg() did not correctly map payload."+ + "\nexpected: %+v\nreceived: %+v", payload, im.payload) + } +} + +// Tests that a marshaled and unmarshalled internalMsg matches the original. +func TestInternalMsg_Marshal_unmarshalInternalMsg(t *testing.T) { + im, _ := newInternalMsg(internalMinLen * 2) + im.SetTimestamp(netTime.Now()) + im.SetSenderID(id.NewIdFromString("test sender ID", id.User, t)) + im.SetPayload([]byte("Sample payload message.")) + + data := im.Marshal() + + newIm, err := unmarshalInternalMsg(data) + if err != nil { + t.Errorf("unmarshalInternalMsg() returned an error: %+v", err) + } + + if !reflect.DeepEqual(im, newIm) { + t.Errorf("unmarshalInternalMsg() did not return the expected internalMsg."+ + "\nexpected: %s\nreceived: %s", im, newIm) + } +} + +// Error path: error is returned when the data is too short. +func Test_unmarshalInternalMsg_DataLengthError(t *testing.T) { + expectedErr := fmt.Sprintf(unmarshalInternalSizeErr, 0, internalMinLen) + + _, err := unmarshalInternalMsg(nil) + if err == nil || err.Error() != expectedErr { + t.Errorf("unmarshalInternalMsg() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Happy path. +func TestInternalMsg_SetTimestamp_GetTimestamp(t *testing.T) { + im, _ := newInternalMsg(internalMinLen * 2) + timestamp := netTime.Now() + im.SetTimestamp(timestamp) + testTimestamp := im.GetTimestamp() + + if !timestamp.Equal(testTimestamp) { + t.Errorf("Failed to get original timestamp."+ + "\nexpected: %s\nreceived: %s", timestamp, testTimestamp) + } +} + +// Happy path. +func TestInternalMsg_SetSenderID_GetSenderID(t *testing.T) { + im, _ := newInternalMsg(internalMinLen * 2) + sid := id.NewIdFromString("testSenderID", id.User, t) + im.SetSenderID(sid) + testID, err := im.GetSenderID() + if err != nil { + t.Errorf("GetSenderID() returned an error: %+v", err) + } + + if !sid.Cmp(testID) { + t.Errorf("Failed to get original sender ID."+ + "\nexpected: %s\nreceived: %s", sid, testID) + } +} + +// Tests that the original payload matches the saved one. +func TestInternalMsg_SetPayload_GetPayload(t *testing.T) { + im, _ := newInternalMsg(internalMinLen * 2) + payload := []byte("Test payload message.") + im.SetPayload(payload) + testPayload := im.GetPayload() + + if !bytes.Equal(payload, testPayload) { + t.Errorf("Failed to get original sender payload."+ + "\nexpected: %s\nreceived: %s", payload, testPayload) + } +} + +// Happy path. +func TestInternalMsg_GetPayloadSize(t *testing.T) { + im, _ := newInternalMsg(internalMinLen * 2) + payload := []byte("Test payload message.") + im.SetPayload(payload) + + if len(payload) != im.GetPayloadSize() { + t.Errorf("GetPayloadSize() failed to return the correct size."+ + "\nexpected: %d\nreceived: %d", len(payload), im.GetPayloadSize()) + } +} + +// Happy path. +func TestInternalMsg_GetPayloadMaxSize(t *testing.T) { + im, _ := newInternalMsg(internalMinLen * 2) + + if internalMinLen != im.GetPayloadMaxSize() { + t.Errorf("GetPayloadSize() failed to return the correct size."+ + "\nexpected: %d\nreceived: %d", internalMinLen, im.GetPayloadMaxSize()) + } +} + +// Happy path. +func TestInternalMsg_String(t *testing.T) { + im, _ := newInternalMsg(internalMinLen * 2) + im.SetTimestamp(time.Date(1955, 11, 5, 12, 0, 0, 0, time.UTC)) + im.SetSenderID(id.NewIdFromString("test sender ID", id.User, t)) + payload := []byte("Sample payload message.") + payload = append(payload, 0, 1, 2) + im.SetPayload(payload) + + expected := `{timestamp:` + im.GetTimestamp().String() + `, senderID:dGVzdCBzZW5kZXIgSUQAAAAAAAAAAAAAAAAAAAAAAAAD, size:26, payload:"Sample payload message.\x00\x01\x02"}` + + if im.String() != expected { + t.Errorf("String() failed to return the expected value."+ + "\nexpected: %s\nreceived: %s", expected, im.String()) + } +} + +// Happy path: tests that String returns the expected string for a nil internalMsg. +func TestInternalMsg_String_NilInternalMessage(t *testing.T) { + im := internalMsg{} + + expected := "{timestamp:<nil>, senderID:<nil>, size:<nil>, payload:<nil>}" + + if im.String() != expected { + t.Errorf("String() failed to return the expected value."+ + "\nexpected: %s\nreceived: %s", expected, im.String()) + } +} diff --git a/groupChat/makeGroup.go b/groupChat/makeGroup.go new file mode 100644 index 0000000000000000000000000000000000000000..fa230fadcc99f61a4b4a44d0f62aa549ec9ec306 --- /dev/null +++ b/groupChat/makeGroup.go @@ -0,0 +1,190 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "github.com/pkg/errors" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/crypto/contact" + "gitlab.com/elixxir/crypto/fastRNG" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/primitives/id" + "strconv" +) + +// Error messages. +const ( + maxInitMsgSizeErr = "new group request message length %d > %d maximum size" + getPrivKeyErr = "failed to get private key from partner: %+v" + minMembersErr = "length of membership list %d < %d minimum allowed" + maxMembersErr = "length of membership list %d > %d maximum allowed" + getPartnerErr = "failed to get partner %s: %+v" + makeMembershipErr = "failed to assemble group chat membership: %+v" + newIdPreimageErr = "failed to create group ID preimage: %+v" + newKeyPreimageErr = "failed to create group key preimage: %+v" + addGroupErr = "failed to save new group: %+v" +) + +// MaxInitMessageSize is the maximum allowable length of the initial message +// sent in a group request. +const MaxInitMessageSize = 256 + +// RequestStatus signals the status of the group requests on group creation. +type RequestStatus int + +const ( + NotSent RequestStatus = iota // Error occurred before sending requests + AllFail // Sending of all requests failed + PartialSent // Sending of some request failed + AllSent // Sending of all request succeeded +) + +// MakeGroup sends groupChat requests to all members over an authenticated +// channel. The leader of a groupChat must have an authenticated channel with +// each member of the groupChat to add them to the groupChat. It blocks until +// all the groupChat requests are sent. Returns an error if at least one request +// to a member fails to send. +func (m Manager) MakeGroup(membership []*id.ID, name, msg []byte) (gs.Group, + []id.Round, RequestStatus, error) { + // Return an error if the message is too long + if len(msg) > MaxInitMessageSize { + return gs.Group{}, nil, NotSent, + errors.Errorf(maxInitMsgSizeErr, len(msg), MaxInitMessageSize) + } + + // Build membership and DH key list from list of IDs + mem, dkl, err := m.buildMembership(membership) + if err != nil { + return gs.Group{}, nil, NotSent, err + } + + // Generate ID and key preimages + idPreimage, keyPreimage, err := getPreimages(m.rng) + if err != nil { + return gs.Group{}, nil, NotSent, err + } + + // Create new group ID and key + groupID := group.NewID(idPreimage, mem) + groupKey := group.NewKey(keyPreimage, mem) + + // Create new group and add to manager + g := gs.NewGroup(name, groupID, groupKey, idPreimage, keyPreimage, msg, mem, dkl) + if err := m.gs.Add(g); err != nil { + return gs.Group{}, nil, NotSent, errors.Errorf(addGroupErr, err) + } + + // Send all group requests + roundIDs, status, err := m.sendRequests(g) + + return g, roundIDs, status, err +} + +// buildMembership retrieves the contact object for each member ID and creates a +// new membership from them. The caller is set as the leader. For a member to be +// added, the group leader must have an authenticated channel with the member. +func (m Manager) buildMembership(members []*id.ID) (group.Membership, gs.DhKeyList, error) { + // Return an error if the membership list has too few or too many members + if len(members) < group.MinParticipants { + return nil, nil, + errors.Errorf(minMembersErr, len(members), group.MinParticipants) + } else if len(members) > group.MaxParticipants { + return nil, nil, + errors.Errorf(maxMembersErr, len(members), group.MaxParticipants) + } + + grp := m.store.E2e().GetGroup() + dkl := make(gs.DhKeyList, len(members)) + + // Lookup partner contact objects from their ID + contacts := make([]contact.Contact, len(members)) + var err error + for i, uid := range members { + partner, err := m.store.E2e().GetPartner(uid) + if err != nil { + return nil, nil, errors.Errorf(getPartnerErr, uid, err) + } + + contacts[i] = contact.Contact{ + ID: partner.GetPartnerID(), + DhPubKey: partner.GetPartnerOriginPublicKey(), + } + + dkl.Add(partner.GetMyOriginPrivateKey(), group.Member{ + ID: partner.GetPartnerID(), + DhKey: partner.GetPartnerOriginPublicKey(), + }, grp) + } + + // Create new Membership from contact list and client's own contact. + user := m.gs.GetUser() + leader := contact.Contact{ID: user.ID, DhPubKey: user.DhKey} + mem, err := group.NewMembership(leader, contacts...) + if err != nil { + return nil, nil, errors.Errorf(makeMembershipErr, err) + } + + return mem, dkl, nil +} + +// getPreimages generates and returns the group ID preimage and the group key +// preimage. This function allows the stream to +func getPreimages(streamGen *fastRNG.StreamGenerator) (group.IdPreimage, + group.KeyPreimage, error) { + + // Get new stream and defer its close + rng := streamGen.GetStream() + defer rng.Close() + + idPreimage, err := group.NewIdPreimage(rng) + if err != nil { + return group.IdPreimage{}, group.KeyPreimage{}, + errors.Errorf(newIdPreimageErr, err) + } + + keyPreimage, err := group.NewKeyPreimage(rng) + if err != nil { + return group.IdPreimage{}, group.KeyPreimage{}, + errors.Errorf(newKeyPreimageErr, err) + } + + return idPreimage, keyPreimage, nil +} + +// String prints the description of the status code. This functions satisfies +// the fmt.Stringer interface. +func (rs RequestStatus) String() string { + switch rs { + case NotSent: + return "NotSent" + case AllFail: + return "AllFail" + case PartialSent: + return "PartialSent" + case AllSent: + return "AllSent" + default: + return "INVALID STATUS" + } +} + +// Message prints a full description of the status code. +func (rs RequestStatus) Message() string { + switch rs { + case NotSent: + return "an error occurred before sending any group requests" + case AllFail: + return "all group requests failed to send" + case PartialSent: + return "some group requests failed to send" + case AllSent: + return "all groups requests successfully sent" + default: + return "INVALID STATUS " + strconv.Itoa(int(rs)) + } +} diff --git a/groupChat/makeGroup_test.go b/groupChat/makeGroup_test.go new file mode 100644 index 0000000000000000000000000000000000000000..004bf452664984c8ec8651442ab10a7a64d48fee --- /dev/null +++ b/groupChat/makeGroup_test.go @@ -0,0 +1,302 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "bytes" + "fmt" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/interfaces/params" + "gitlab.com/elixxir/crypto/fastRNG" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/crypto/csprng" + "gitlab.com/xx_network/primitives/id" + "math/rand" + "reflect" + "strconv" + "strings" + "testing" +) + +// Tests that Manager.MakeGroup adds a group and returns the expected status. +func TestManager_MakeGroup(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + memberIDs, members, dkl := addPartners(m, t) + name := []byte("groupName") + message := []byte("Invite message.") + + g, _, status, err := m.MakeGroup(memberIDs, name, message) + if err != nil { + t.Errorf("MakeGroup() returned an error: %+v", err) + } + + if status != AllSent { + t.Errorf("MakeGroup() did not return the expected status."+ + "\nexpected: %s\nreceived: %s", AllSent, status) + } + + _, exists := m.gs.Get(g.ID) + if !exists { + t.Errorf("Failed to get group %#v.", g) + } + + if !reflect.DeepEqual(members, g.Members) { + t.Errorf("New group does not have expected membership."+ + "\nexpected: %s\nreceived: %s", members, g.Members) + } + + if !reflect.DeepEqual(dkl, g.DhKeys) { + t.Errorf("New group does not have expected DH key list."+ + "\nexpected: %#v\nreceived: %#v", dkl, g.DhKeys) + } + + if !g.ID.Cmp(g.ID) { + t.Errorf("New group does not have expected ID."+ + "\nexpected: %s\nreceived: %s", g.ID, g.ID) + } + + if !bytes.Equal(name, g.Name) { + t.Errorf("New group does not have expected name."+ + "\nexpected: %q\nreceived: %q", name, g.Name) + } + + if !bytes.Equal(message, g.InitMessage) { + t.Errorf("New group does not have expected message."+ + "\nexpected: %q\nreceived: %q", message, g.InitMessage) + } +} + +// Error path: make sure an error and the correct status is returned when the +// message is too large. +func TestManager_MakeGroup_MaxMessageSizeError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + expectedErr := fmt.Sprintf(maxInitMsgSizeErr, MaxInitMessageSize+1, MaxInitMessageSize) + + _, _, status, err := m.MakeGroup(nil, nil, make([]byte, MaxInitMessageSize+1)) + if err == nil || err.Error() != expectedErr { + t.Errorf("MakeGroup() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } + + if status != NotSent { + t.Errorf("MakeGroup() did not return the expected status."+ + "\nexpected: %s\nreceived: %s", NotSent, status) + } +} + +// Error path: make sure an error and the correct status is returned when the +// membership list is too small. +func TestManager_MakeGroup_MembershipSizeError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + expectedErr := fmt.Sprintf(maxMembersErr, group.MaxParticipants+1, group.MaxParticipants) + + _, _, status, err := m.MakeGroup(make([]*id.ID, group.MaxParticipants+1), + nil, []byte{}) + if err == nil || err.Error() != expectedErr { + t.Errorf("MakeGroup() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } + + if status != NotSent { + t.Errorf("MakeGroup() did not return the expected status."+ + "\nexpected: %s\nreceived: %s", NotSent, status) + } +} + +// Error path: make sure an error and the correct status is returned when adding +// a group failed because the user is a part of too many groups already. +func TestManager_MakeGroup_AddGroupError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, gs.MaxGroupChats, 0, nil, nil, t) + memberIDs, _, _ := addPartners(m, t) + expectedErr := strings.SplitN(addGroupErr, "%", 2)[0] + + _, _, _, err := m.MakeGroup(memberIDs, []byte{}, []byte{}) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("MakeGroup() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of Manager.buildMembership. +func TestManager_buildMembership(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManager(prng, t) + memberIDs, expected, expectedDKL := addPartners(m, t) + + membership, dkl, err := m.buildMembership(memberIDs) + if err != nil { + t.Errorf("buildMembership() returned an error: %+v", err) + } + + if !reflect.DeepEqual(expected, membership) { + t.Errorf("buildMembership() failed to return the expected membership."+ + "\nexpected: %s\nrecieved: %s", expected, membership) + } + + if !reflect.DeepEqual(expectedDKL, dkl) { + t.Errorf("buildMembership() failed to return the expected DH key list."+ + "\nexpected: %#v\nrecieved: %#v", expectedDKL, dkl) + } +} + +// Error path: an error is returned when the number of members in the membership +// list is too few. +func TestManager_buildMembership_MinParticipantsError(t *testing.T) { + m, _ := newTestManager(rand.New(rand.NewSource(42)), t) + memberIDs := make([]*id.ID, group.MinParticipants-1) + expectedErr := fmt.Sprintf(minMembersErr, len(memberIDs), group.MinParticipants) + + _, _, err := m.buildMembership(memberIDs) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("buildMembership() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: an error is returned when the number of members in the membership +// list is too many. +func TestManager_buildMembership_MaxParticipantsError(t *testing.T) { + m, _ := newTestManager(rand.New(rand.NewSource(42)), t) + memberIDs := make([]*id.ID, group.MaxParticipants+1) + expectedErr := fmt.Sprintf(maxMembersErr, len(memberIDs), group.MaxParticipants) + + _, _, err := m.buildMembership(memberIDs) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("buildMembership() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: error returned when a partner cannot be found +func TestManager_buildMembership_GetPartnerContactError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManager(prng, t) + memberIDs, _, _ := addPartners(m, t) + expectedErr := strings.SplitN(getPartnerErr, "%", 2)[0] + + // Replace a partner ID + memberIDs[len(memberIDs)/2] = id.NewIdFromString("nonPartnerID", id.User, t) + + _, _, err := m.buildMembership(memberIDs) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("buildMembership() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: error returned when a member ID appears twice on the list. +func TestManager_buildMembership_DuplicateContactError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManager(prng, t) + memberIDs, _, _ := addPartners(m, t) + expectedErr := strings.SplitN(makeMembershipErr, "%", 2)[0] + + // Replace a partner ID + memberIDs[5] = memberIDs[4] + + _, _, err := m.buildMembership(memberIDs) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("buildMembership() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Test that getPreimages produces unique preimages. +func Test_getPreimages_Unique(t *testing.T) { + streamGen := fastRNG.NewStreamGenerator(1000, 10, csprng.NewSystemRNG) + n := 100 + idPreimages := make(map[group.IdPreimage]bool, n) + keyPreimages := make(map[group.KeyPreimage]bool, n) + + for i := 0; i < n; i++ { + idPreimage, keyPreimage, err := getPreimages(streamGen) + if err != nil { + t.Errorf("getPreimages() returned an error: %+v", err) + } + + if idPreimages[idPreimage] { + t.Errorf("getPreimages() produced a duplicate idPreimage: %s", idPreimage) + } else { + idPreimages[idPreimage] = true + } + + if keyPreimages[keyPreimage] { + t.Errorf("getPreimages() produced a duplicate keyPreimage: %s", keyPreimage) + } else { + keyPreimages[keyPreimage] = true + } + } +} + +// Unit test of RequestStatus.String. +func TestRequestStatus_String(t *testing.T) { + statusCodes := map[RequestStatus]string{ + NotSent: "NotSent", + AllFail: "AllFail", + PartialSent: "PartialSent", + AllSent: "AllSent", + AllSent + 1: "INVALID STATUS", + } + + for status, expected := range statusCodes { + if status.String() != expected { + t.Errorf("String() failed to return the expected name."+ + "\nexpected: %s\nreceived: %s", expected, status.String()) + } + } +} + +// Unit test of RequestStatus.Message. +func TestRequestStatus_Message(t *testing.T) { + statusCodes := map[RequestStatus]string{ + NotSent: "an error occurred before sending any group requests", + AllFail: "all group requests failed to send", + PartialSent: "some group requests failed to send", + AllSent: "all groups requests successfully sent", + AllSent + 1: "INVALID STATUS " + strconv.Itoa(int(AllSent)+1), + } + + for status, expected := range statusCodes { + if status.Message() != expected { + t.Errorf("Message() failed to return the expected message."+ + "\nexpected: %s\nreceived: %s", expected, status.Message()) + } + } +} + +// addPartners returns a list of user IDs and their matching membership and adds +// them as partners. +func addPartners(m *Manager, t *testing.T) ([]*id.ID, group.Membership, gs.DhKeyList) { + memberIDs := make([]*id.ID, 10) + members := group.Membership{m.gs.GetUser()} + dkl := gs.DhKeyList{} + + for i := range memberIDs { + // Build member data + uid := id.NewIdFromUInt(uint64(i), id.User, t) + dhKey := m.store.E2e().GetGroup().NewInt(int64(i + 42)) + + // Add to lists + memberIDs[i] = uid + members = append(members, group.Member{ID: uid, DhKey: dhKey}) + dkl.Add(dhKey, group.Member{ID: uid, DhKey: dhKey}, m.store.E2e().GetGroup()) + + // Add partner + err := m.store.E2e().AddPartner(uid, dhKey, dhKey, + params.GetDefaultE2ESessionParams(), params.GetDefaultE2ESessionParams()) + if err != nil { + t.Errorf("Failed to add partner %d: %+v", i, err) + } + } + + return memberIDs, members, dkl +} diff --git a/groupChat/manager.go b/groupChat/manager.go new file mode 100644 index 0000000000000000000000000000000000000000..f6044ed2269ed83e2ee96899f2768b27ce76e6f6 --- /dev/null +++ b/groupChat/manager.go @@ -0,0 +1,156 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "github.com/pkg/errors" + "gitlab.com/elixxir/client/api" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/interfaces" + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" + "gitlab.com/elixxir/client/storage" + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/fastRNG" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/primitives/id" +) + +const ( + rawMessageBuffSize = 100 + receiveStoppableName = "GroupChatReceive" + receiveListenerName = "GroupChatReceiveListener" + requestStoppableName = "GroupChatRequest" + requestListenerName = "GroupChatRequestListener" + groupStoppableName = "GroupChat" +) + +// Error messages. +const ( + newGroupStoreErr = "failed to create new group store: %+v" + joinGroupErr = "failed to join new group %s: %+v" + leaveGroupErr = "failed to leave group %s: %+v" +) + +// Manager handles the list of groups a user is a part of. +type Manager struct { + client *api.Client + store *storage.Session + swb interfaces.Switchboard + net interfaces.NetworkManager + rng *fastRNG.StreamGenerator + gs *gs.Store + + requestFunc RequestCallback + receiveFunc ReceiveCallback +} + +// NewManager generates a new group chat manager. This functions satisfies the +// GroupChat interface. +func NewManager(client *api.Client, requestFunc RequestCallback, + receiveFunc ReceiveCallback) (*Manager, error) { + return newManager( + client, + client.GetUser().ReceptionID.DeepCopy(), + client.GetStorage().E2e().GetDHPublicKey(), + client.GetStorage(), + client.GetSwitchboard(), + client.GetNetworkInterface(), + client.GetRng(), + client.GetStorage().GetKV(), + requestFunc, + receiveFunc, + ) +} + +// newManager creates a new group chat manager from api.Client parts for easier +// testing. +func newManager(client *api.Client, userID *id.ID, userDhKey *cyclic.Int, + store *storage.Session, swb interfaces.Switchboard, + net interfaces.NetworkManager, rng *fastRNG.StreamGenerator, + kv *versioned.KV, requestFunc RequestCallback, + receiveFunc ReceiveCallback) (*Manager, error) { + + // Load the group chat storage or create one if one does not exist + gStore, err := gs.NewOrLoadStore(kv, group.Member{ID: userID, DhKey: userDhKey}) + if err != nil { + return nil, errors.Errorf(newGroupStoreErr, err) + } + + return &Manager{ + client: client, + store: store, + swb: swb, + net: net, + rng: rng, + gs: gStore, + requestFunc: requestFunc, + receiveFunc: receiveFunc, + }, nil +} + +// StartProcesses starts the reception worker. +func (m *Manager) StartProcesses() stoppable.Stoppable { + // Start group reception worker + receiveStop := stoppable.NewSingle(receiveStoppableName) + receiveChan := make(chan message.Receive, rawMessageBuffSize) + m.swb.RegisterChannel(receiveListenerName, &id.ID{}, + message.Raw, receiveChan) + go m.receive(receiveChan, receiveStop) + + // Start group request worker + requestStop := stoppable.NewSingle(requestStoppableName) + requestChan := make(chan message.Receive, rawMessageBuffSize) + m.swb.RegisterChannel(requestListenerName, &id.ID{}, + message.GroupCreationRequest, requestChan) + go m.receiveRequest(requestChan, requestStop) + + // Create a multi stoppable + multiStoppable := stoppable.NewMulti(groupStoppableName) + multiStoppable.Add(receiveStop) + multiStoppable.Add(requestStop) + + return multiStoppable +} + +// JoinGroup adds the group to the list of group chats the user is a part of. +// An error is returned if the user is already part of the group or if the +// maximum number of groups have already been joined. +func (m Manager) JoinGroup(g gs.Group) error { + if err := m.gs.Add(g); err != nil { + return errors.Errorf(joinGroupErr, g.ID, err) + } + + return nil +} + +// LeaveGroup removes a group from a list of groups the user is a part of. +func (m Manager) LeaveGroup(groupID *id.ID) error { + if err := m.gs.Remove(groupID); err != nil { + return errors.Errorf(leaveGroupErr, groupID, err) + } + + return nil +} + +// GetGroups returns a list of all registered groupChat IDs. +func (m Manager) GetGroups() []*id.ID { + return m.gs.GroupIDs() +} + +// GetGroup returns the group with the matching ID or returns false if none +// exist. +func (m Manager) GetGroup(groupID *id.ID) (gs.Group, bool) { + return m.gs.Get(groupID) +} + +// NumGroups returns the number of groups the user is a part of. +func (m Manager) NumGroups() int { + return m.gs.Len() +} diff --git a/groupChat/manager_test.go b/groupChat/manager_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0ea0f5341018fac4a24e72b999603d2a93086ed2 --- /dev/null +++ b/groupChat/manager_test.go @@ -0,0 +1,386 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/elixxir/ekv" + "gitlab.com/xx_network/primitives/id" + "math/rand" + "reflect" + "strings" + "testing" + "time" +) + +// Unit test of Manager.newManager. +func Test_newManager(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + user := group.Member{ + ID: id.NewIdFromString("userID", id.User, t), + DhKey: randCycInt(rand.New(rand.NewSource(42))), + } + requestChan := make(chan gs.Group) + requestFunc := func(g gs.Group) { requestChan <- g } + receiveChan := make(chan MessageReceive) + receiveFunc := func(msg MessageReceive) { receiveChan <- msg } + m, err := newManager(nil, user.ID, user.DhKey, nil, nil, nil, nil, kv, requestFunc, receiveFunc) + if err != nil { + t.Errorf("newManager() returned an error: %+v", err) + } + + if !m.gs.GetUser().Equal(user) { + t.Errorf("newManager() failed to create a store with the correct user."+ + "\nexpected: %s\nreceived: %s", user, m.gs.GetUser()) + } + + if m.gs.Len() != 0 { + t.Errorf("newManager() failed to create an empty store."+ + "\nexpected: %d\nreceived: %d", 0, m.gs.Len()) + } + + // Check if requestFunc works + go m.requestFunc(gs.Group{}) + select { + case <-requestChan: + case <-time.NewTimer(5 * time.Millisecond).C: + t.Errorf("Timed out waiting for requestFunc to be called.") + } + + // Check if receiveFunc works + go m.receiveFunc(MessageReceive{}) + select { + case <-receiveChan: + case <-time.NewTimer(5 * time.Millisecond).C: + t.Errorf("Timed out waiting for receiveFunc to be called.") + } +} + +// Tests that Manager.newManager loads a group storage when it exists. +func Test_newManager_LoadStorage(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := group.Member{ + ID: id.NewIdFromString("userID", id.User, t), + DhKey: randCycInt(rand.New(rand.NewSource(42))), + } + + gStore, err := gs.NewStore(kv, user) + if err != nil { + t.Errorf("Failed to create new group storage: %+v", err) + } + + for i := 0; i < 10; i++ { + err := gStore.Add(newTestGroup(getGroup(), getGroup().NewInt(42), prng, t)) + if err != nil { + t.Errorf("Failed to add group %d: %+v", i, err) + } + } + + m, err := newManager(nil, user.ID, user.DhKey, nil, nil, nil, nil, kv, nil, nil) + if err != nil { + t.Errorf("newManager() returned an error: %+v", err) + } + + if !reflect.DeepEqual(gStore, m.gs) { + t.Errorf("newManager() failed to load the expected storage."+ + "\nexpected: %+v\nreceived: %+v", gStore, m.gs) + } +} + +// Error path: an error is returned when a group cannot be loaded from storage. +func Test_newManager_LoadError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + kv := versioned.NewKV(make(ekv.Memstore)) + user := group.Member{ + ID: id.NewIdFromString("userID", id.User, t), + DhKey: randCycInt(rand.New(rand.NewSource(42))), + } + + gStore, err := gs.NewStore(kv, user) + if err != nil { + t.Errorf("Failed to create new group storage: %+v", err) + } + + g := newTestGroup(getGroup(), getGroup().NewInt(42), prng, t) + err = gStore.Add(g) + if err != nil { + t.Errorf("Failed to add group: %+v", err) + } + _ = kv.Prefix("GroupChatListStore").Delete("GroupChat/"+g.ID.String(), 0) + + expectedErr := strings.SplitN(newGroupStoreErr, "%", 2)[0] + + _, err = newManager(nil, user.ID, user.DhKey, nil, nil, nil, nil, kv, nil, nil) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("newManager() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// +// func TestManager_StartProcesses(t *testing.T) { +// jww.SetLogThreshold(jww.LevelTrace) +// jww.SetStdoutThreshold(jww.LevelTrace) +// prng := rand.New(rand.NewSource(42)) +// requestChan1 := make(chan gs.Group) +// requestFunc1 := func(g gs.Group) { requestChan1 <- g } +// receiveChan1 := make(chan MessageReceive) +// receiveFunc1 := func(msg MessageReceive) { receiveChan1 <- msg } +// requestChan2 := make(chan gs.Group) +// requestFunc2 := func(g gs.Group) { requestChan2 <- g } +// receiveChan2 := make(chan MessageReceive) +// receiveFunc2 := func(msg MessageReceive) { receiveChan2 <- msg } +// requestChan3 := make(chan gs.Group) +// requestFunc3 := func(g gs.Group) { requestChan3 <- g } +// receiveChan3 := make(chan MessageReceive) +// receiveFunc3 := func(msg MessageReceive) { receiveChan3 <- msg } +// +// m1, _ := newTestManagerWithStore(prng, 10, 0, requestFunc1, receiveFunc1, t) +// m2, _ := newTestManagerWithStore(prng, 10, 0, requestFunc2, receiveFunc2, t) +// m3, _ := newTestManagerWithStore(prng, 10, 0, requestFunc3, receiveFunc3, t) +// +// membership, err := group.NewMembership(m1.store.GetUser().GetContact(), +// m2.store.GetUser().GetContact(), m3.store.GetUser().GetContact()) +// if err != nil { +// t.Errorf("Failed to generate new membership: %+v", err) +// } +// +// dhKeys := gs.GenerateDhKeyList(m1.gs.GetUser().ID, +// m1.store.GetUser().E2eDhPrivateKey, membership, m1.store.E2e().GetGroup()) +// +// grp1 := newTestGroup(m1.store.E2e().GetGroup(), m1.store.GetUser().E2eDhPrivateKey, prng, t) +// grp1.Members = membership +// grp1.DhKeys = dhKeys +// grp1.ID = group.NewID(grp1.IdPreimage, grp1.Members) +// grp1.Key = group.NewKey(grp1.KeyPreimage, grp1.Members) +// grp2 := grp1.DeepCopy() +// grp2.DhKeys = gs.GenerateDhKeyList(m2.gs.GetUser().ID, +// m2.store.GetUser().E2eDhPrivateKey, membership, m2.store.E2e().GetGroup()) +// grp3 := grp1.DeepCopy() +// grp3.DhKeys = gs.GenerateDhKeyList(m3.gs.GetUser().ID, +// m3.store.GetUser().E2eDhPrivateKey, membership, m3.store.E2e().GetGroup()) +// +// err = m1.gs.Add(grp1) +// if err != nil { +// t.Errorf("Failed to add group to member 1: %+v", err) +// } +// err = m2.gs.Add(grp2) +// if err != nil { +// t.Errorf("Failed to add group to member 2: %+v", err) +// } +// err = m3.gs.Add(grp3) +// if err != nil { +// t.Errorf("Failed to add group to member 3: %+v", err) +// } +// +// _ = m1.StartProcesses() +// _ = m2.StartProcesses() +// _ = m3.StartProcesses() +// +// // Build request message +// requestMarshaled, err := proto.Marshal(&Request{ +// Name: grp1.Name, +// IdPreimage: grp1.IdPreimage.Bytes(), +// KeyPreimage: grp1.KeyPreimage.Bytes(), +// Members: grp1.Members.Serialize(), +// Message: grp1.InitMessage, +// }) +// if err != nil { +// t.Errorf("Failed to proto marshal message: %+v", err) +// } +// msg := message.Receive{ +// Payload: requestMarshaled, +// MessageType: message.GroupCreationRequest, +// Sender: m1.gs.GetUser().ID, +// } +// +// m2.swb.(*switchboard.Switchboard).Speak(msg) +// m3.swb.(*switchboard.Switchboard).Speak(msg) +// +// select { +// case received := <-requestChan2: +// if !reflect.DeepEqual(grp2, received) { +// t.Errorf("Failed to receive expected group on requestChan."+ +// "\nexpected: %#v\nreceived: %#v", grp2, received) +// } +// case <-time.NewTimer(5 * time.Millisecond).C: +// t.Error("Timed out waiting for request callback.") +// } +// +// select { +// case received := <-requestChan3: +// if !reflect.DeepEqual(grp3, received) { +// t.Errorf("Failed to receive expected group on requestChan."+ +// "\nexpected: %#v\nreceived: %#v", grp3, received) +// } +// case <-time.NewTimer(5 * time.Millisecond).C: +// t.Error("Timed out waiting for request callback.") +// } +// +// contents := []byte("Test group message.") +// timestamp := netTime.Now() +// +// // Create cMix message and get public message +// cMixMsg, err := m1.newCmixMsg(grp1, contents, timestamp, m2.gs.GetUser(), prng) +// if err != nil { +// t.Errorf("Failed to create new cMix message: %+v", err) +// } +// +// internalMsg, _ := newInternalMsg(cMixMsg.ContentsSize() - publicMinLen) +// internalMsg.SetTimestamp(timestamp) +// internalMsg.SetSenderID(m1.gs.GetUser().ID) +// internalMsg.SetPayload(contents) +// expectedMsgID := group.NewMessageID(grp1.ID, internalMsg.Marshal()) +// +// expectedMsg := MessageReceive{ +// GroupID: grp1.ID, +// ID: expectedMsgID, +// Payload: contents, +// SenderID: m1.gs.GetUser().ID, +// RoundTimestamp: timestamp.Local(), +// } +// +// msg = message.Receive{ +// Payload: cMixMsg.Marshal(), +// MessageType: message.Raw, +// Sender: m1.gs.GetUser().ID, +// RoundTimestamp: timestamp.Local(), +// } +// m2.swb.(*switchboard.Switchboard).Speak(msg) +// +// select { +// case received := <-receiveChan2: +// if !reflect.DeepEqual(expectedMsg, received) { +// t.Errorf("Failed to receive expected group on receiveChan."+ +// "\nexpected: %+v\nreceived: %+v", expectedMsg, received) +// } +// case <-time.NewTimer(5 * time.Millisecond).C: +// t.Error("Timed out waiting for receive callback.") +// } +// } + +// Unit test of Manager.JoinGroup. +func TestManager_JoinGroup(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + g := newTestGroup(m.store.E2e().GetGroup(), m.store.GetUser().E2eDhPrivateKey, prng, t) + + err := m.JoinGroup(g) + if err != nil { + t.Errorf("JoinGroup() returned an error: %+v", err) + } + + if _, exists := m.gs.Get(g.ID); !exists { + t.Errorf("JoinGroup() failed to add the group %s.", g.ID) + } +} + +// Error path: an error is returned when a group is joined twice. +func TestManager_JoinGroup_AddErr(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + expectedErr := strings.SplitN(joinGroupErr, "%", 2)[0] + + err := m.JoinGroup(g) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("JoinGroup() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of Manager.LeaveGroup. +func TestManager_LeaveGroup(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + err := m.LeaveGroup(g.ID) + if err != nil { + t.Errorf("LeaveGroup() returned an error: %+v", err) + } + + if _, exists := m.GetGroup(g.ID); exists { + t.Error("LeaveGroup() failed to delete the group.") + } +} + +// Error path: an error is returned when no group with the ID exists +func TestManager_LeaveGroup_NoGroupError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + expectedErr := strings.SplitN(leaveGroupErr, "%", 2)[0] + + err := m.LeaveGroup(id.NewIdFromString("invalidID", id.Group, t)) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("LeaveGroup() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of Manager.GetGroups. +func TestManager_GetGroups(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + list := m.GetGroups() + for i, gid := range list { + if err := m.gs.Remove(gid); err != nil { + t.Errorf("Group %s does not exist (%d): %+v", gid, i, err) + } + } + + if m.gs.Len() != 0 { + t.Errorf("GetGroups() returned %d IDs, which is %d less than is in "+ + "memory.", len(list), m.gs.Len()) + } +} + +// Unit test of Manager.GetGroup. +func TestManager_GetGroup(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + testGrp, exists := m.GetGroup(g.ID) + if !exists { + t.Error("GetGroup() failed to find a group that should exist.") + } + + if !reflect.DeepEqual(g, testGrp) { + t.Errorf("GetGroup() failed to return the expected group."+ + "\nexpected: %#v\nreceived: %#v", g, testGrp) + } + + testGrp, exists = m.GetGroup(id.NewIdFromString("invalidID", id.Group, t)) + if exists { + t.Errorf("GetGroup() returned a group that should not exist: %#v", testGrp) + } +} + +// Unit test of Manager.NumGroups. First a manager is created with 10 groups +// and the initial number is checked. Then the number of groups is checked after +// leaving each until the number left is 0. +func TestManager_NumGroups(t *testing.T) { + expectedNum := 10 + m, _ := newTestManagerWithStore(rand.New(rand.NewSource(42)), expectedNum, + 0, nil, nil, t) + + groups := append([]*id.ID{{}}, m.GetGroups()...) + + for i, gid := range groups { + _ = m.LeaveGroup(gid) + + if m.NumGroups() != expectedNum-i { + t.Errorf("NumGroups() failed to return the expected number of "+ + "groups (%d).\nexpected: %d\nreceived: %d", + i, expectedNum-i, m.NumGroups()) + } + } + +} diff --git a/groupChat/messageReceive.go b/groupChat/messageReceive.go new file mode 100644 index 0000000000000000000000000000000000000000..e607e7f01fcd1aa2e6bb4cee11356e3ea71814f5 --- /dev/null +++ b/groupChat/messageReceive.go @@ -0,0 +1,69 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "fmt" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + "strconv" + "strings" + "time" +) + +// MessageReceive contains the GroupChat message and associated data that a user +// receives when getting a group message. +type MessageReceive struct { + GroupID *id.ID + ID group.MessageID + Payload []byte + SenderID *id.ID + RecipientID *id.ID + EphemeralID ephemeral.Id + Timestamp time.Time + RoundID id.Round + RoundTimestamp time.Time +} + +// String returns the MessageReceive as readable text. This functions satisfies +// the fmt.Stringer interface. +func (mr MessageReceive) String() string { + groupID := "<nil>" + if mr.GroupID != nil { + groupID = mr.GroupID.String() + } + + payload := "<nil>" + if mr.Payload != nil { + payload = fmt.Sprintf("%q", mr.Payload) + } + + senderID := "<nil>" + if mr.SenderID != nil { + senderID = mr.SenderID.String() + } + + recipientID := "<nil>" + if mr.RecipientID != nil { + recipientID = mr.RecipientID.String() + } + + str := make([]string, 0, 9) + str = append(str, "GroupID:"+groupID) + str = append(str, "ID:"+mr.ID.String()) + str = append(str, "Payload:"+payload) + str = append(str, "SenderID:"+senderID) + str = append(str, "RecipientID:"+recipientID) + str = append(str, "EphemeralID:"+strconv.FormatInt(mr.EphemeralID.Int64(), 10)) + str = append(str, "Timestamp:"+mr.Timestamp.String()) + str = append(str, "RoundID:"+strconv.FormatUint(uint64(mr.RoundID), 10)) + str = append(str, "RoundTimestamp:"+mr.RoundTimestamp.String()) + + return "{" + strings.Join(str, " ") + "}" +} diff --git a/groupChat/messageReceive_test.go b/groupChat/messageReceive_test.go new file mode 100644 index 0000000000000000000000000000000000000000..343f53774a08e59caed53a37d31a1a35f7047cd7 --- /dev/null +++ b/groupChat/messageReceive_test.go @@ -0,0 +1,70 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// +package groupChat + +import ( + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + "testing" + "time" +) + +// Unit test of MessageReceive.String. +func TestMessageReceive_String(t *testing.T) { + msg := MessageReceive{ + GroupID: id.NewIdFromString("GroupID", id.Group, t), + ID: group.MessageID{0, 1, 2, 3}, + Payload: []byte("Group message."), + SenderID: id.NewIdFromString("SenderID", id.User, t), + RecipientID: id.NewIdFromString("RecipientID", id.User, t), + EphemeralID: ephemeral.Id{0, 1, 2, 3}, + Timestamp: time.Date(1955, 11, 5, 12, 0, 0, 0, time.UTC), + RoundID: 42, + RoundTimestamp: time.Date(1955, 11, 5, 12, 1, 0, 0, time.UTC), + } + + expected := "{" + + "GroupID:R3JvdXBJRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAE " + + "ID:AAECAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= " + + "Payload:\"Group message.\" " + + "SenderID:U2VuZGVySUQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD " + + "RecipientID:UmVjaXBpZW50SUQAAAAAAAAAAAAAAAAAAAAAAAAAAAAD " + + "EphemeralID:141843442434048 " + + "Timestamp:" + msg.Timestamp.String() + " " + + "RoundID:42 " + + "RoundTimestamp:" + msg.RoundTimestamp.String() + + "}" + + if msg.String() != expected { + t.Errorf("String() returned the incorrect string."+ + "\nexpected: %s\nreceived: %s", expected, msg.String()) + } +} + +// Tests that MessageReceive.String returns the expected value for a message +// with nil values. +func TestMessageReceive_String_NilMessageReceive(t *testing.T) { + msg := MessageReceive{} + + expected := "{" + + "GroupID:<nil> " + + "ID:AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= " + + "Payload:<nil> " + + "SenderID:<nil> " + + "RecipientID:<nil> " + + "EphemeralID:0 " + + "Timestamp:0001-01-01 00:00:00 +0000 UTC " + + "RoundID:0 " + + "RoundTimestamp:0001-01-01 00:00:00 +0000 UTC" + + "}" + + if msg.String() != expected { + t.Errorf("String() returned the incorrect string."+ + "\nexpected: %s\nreceived: %s", expected, msg.String()) + } +} diff --git a/groupChat/publicFormat.go b/groupChat/publicFormat.go new file mode 100644 index 0000000000000000000000000000000000000000..ab88d9e09f9c7e5110404fca5fc473070b45c088 --- /dev/null +++ b/groupChat/publicFormat.go @@ -0,0 +1,120 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "encoding/base64" + "fmt" + "github.com/pkg/errors" + "gitlab.com/elixxir/crypto/group" +) + +// Sizes of marshaled data, in bytes. +const ( + saltLen = group.SaltLen + publicMinLen = saltLen +) + +// Error messages +const ( + newPublicSizeErr = "max message size %d < %d minimum required" + unmarshalPublicSizeErr = "size of data %d < %d minimum required" +) + +// publicMsg is contains the salt and encrypted data in a group message. +// +// +---------------------+ +// | data | +// +----------+----------+ +// | salt | payload | +// | 32 bytes | variable | +// +----------+----------+ +type publicMsg struct { + data []byte // Serial of all the parts of the message + salt []byte // 256-bit sender salt + payload []byte // Encrypted internalMsg +} + +// newPublicMsg creates a new publicMsg of size maxDataSize. An error is +// returned if the maxDataSize is smaller than the minimum newPublicMsg size. +func newPublicMsg(maxDataSize int) (publicMsg, error) { + if maxDataSize < publicMinLen { + return publicMsg{}, + errors.Errorf(newPublicSizeErr, maxDataSize, publicMinLen) + } + + return mapPublicMsg(make([]byte, maxDataSize)), nil +} + +// mapPublicMsg maps all the parts of the publicMsg to the passed in data. +func mapPublicMsg(data []byte) publicMsg { + return publicMsg{ + data: data, + salt: data[:saltLen], + payload: data[saltLen:], + } +} + +// unmarshalPublicMsg unmarshal the data into an publicMsg. An error is +// returned if the data length is smaller than the minimum allowed size. +func unmarshalPublicMsg(data []byte) (publicMsg, error) { + if len(data) < publicMinLen { + return publicMsg{}, + errors.Errorf(unmarshalPublicSizeErr, len(data), publicMinLen) + } + + return mapPublicMsg(data), nil +} + +// Marshal returns the serial of the publicMsg. +func (pm publicMsg) Marshal() []byte { + return pm.data +} + +// GetSalt returns the 256-bit salt. +func (pm publicMsg) GetSalt() [group.SaltLen]byte { + var salt [group.SaltLen]byte + copy(salt[:], pm.salt) + return salt +} + +// SetSalt sets the 256-bit salt. +func (pm publicMsg) SetSalt(salt [group.SaltLen]byte) { + copy(pm.salt, salt[:]) +} + +// GetPayload returns the payload truncated to the correct size. +func (pm publicMsg) GetPayload() []byte { + return pm.payload +} + +// SetPayload sets the payload and saves it size. +func (pm publicMsg) SetPayload(payload []byte) { + copy(pm.payload, payload) +} + +// GetPayloadSize returns the maximum size of the payload. +func (pm publicMsg) GetPayloadSize() int { + return len(pm.payload) +} + +// String prints a string representation of publicMsg. This functions satisfies +// the fmt.Stringer interface. +func (pm publicMsg) String() string { + salt := "<nil>" + if len(pm.salt) > 0 { + salt = base64.StdEncoding.EncodeToString(pm.salt) + } + + payload := "<nil>" + if len(pm.payload) > 0 { + payload = fmt.Sprintf("%q", pm.GetPayload()) + } + + return "{salt:" + salt + ", payload:" + payload + "}" +} diff --git a/groupChat/publicFormat_test.go b/groupChat/publicFormat_test.go new file mode 100644 index 0000000000000000000000000000000000000000..69884ff76856562e0d6f9ee3af03ad63e6eecb74 --- /dev/null +++ b/groupChat/publicFormat_test.go @@ -0,0 +1,162 @@ +package groupChat + +import ( + "bytes" + "fmt" + "math/rand" + "reflect" + "testing" +) + +// Unit test of newPublicMsg. +func Test_newPublicMsg(t *testing.T) { + maxDataSize := 2 * publicMinLen + im, err := newPublicMsg(maxDataSize) + if err != nil { + t.Errorf("newPublicMsg() returned an error: %+v", err) + } + + if len(im.data) != maxDataSize { + t.Errorf("newPublicMsg() set data to the wrong length."+ + "\nexpected: %d\nreceived: %d", maxDataSize, len(im.data)) + } +} + +// Error path: the maxDataSize is smaller than the minimum size. +func Test_newPublicMsg_PayloadSizeError(t *testing.T) { + maxDataSize := publicMinLen - 1 + expectedErr := fmt.Sprintf(newPublicSizeErr, maxDataSize, publicMinLen) + + _, err := newPublicMsg(maxDataSize) + if err == nil || err.Error() != expectedErr { + t.Errorf("newPublicMsg() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of mapPublicMsg. +func Test_mapPublicMsg(t *testing.T) { + // Create all the expected data + var salt [saltLen]byte + rand.New(rand.NewSource(42)).Read(salt[:]) + payload := []byte("Sample payload contents.") + + // Construct data into single slice + data := bytes.NewBuffer(nil) + data.Write(salt[:]) + data.Write(payload) + + // Map data + im := mapPublicMsg(data.Bytes()) + + // Check that the mapped values match the expected values + if !bytes.Equal(salt[:], im.salt) { + t.Errorf("mapPublicMsg() did not correctly map salt."+ + "\nexpected: %+v\nreceived: %+v", salt, im.salt) + } + + if !bytes.Equal(payload, im.payload) { + t.Errorf("mapPublicMsg() did not correctly map payload."+ + "\nexpected: %+v\nreceived: %+v", payload, im.payload) + } +} + +// Tests that a marshaled and unmarshalled publicMsg matches the original. +func Test_publicMsg_Marshal_unmarshalPublicMsg(t *testing.T) { + pm, _ := newPublicMsg(publicMinLen * 2) + var salt [saltLen]byte + rand.New(rand.NewSource(42)).Read(salt[:]) + pm.SetSalt(salt) + pm.SetPayload([]byte("Sample payload message.")) + + data := pm.Marshal() + + newPm, err := unmarshalPublicMsg(data) + if err != nil { + t.Errorf("unmarshalPublicMsg() returned an error: %+v", err) + } + + if !reflect.DeepEqual(pm, newPm) { + t.Errorf("unmarshalPublicMsg() did not return the expected publicMsg."+ + "\nexpected: %s\nreceived: %s", pm, newPm) + } +} + +// Error path: error is returned when the data is too short. +func Test_unmarshalPublicMsg(t *testing.T) { + expectedErr := fmt.Sprintf(unmarshalPublicSizeErr, 0, publicMinLen) + + _, err := unmarshalPublicMsg(nil) + if err == nil || err.Error() != expectedErr { + t.Errorf("unmarshalPublicMsg() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Happy path. +func Test_publicMsg_SetSalt_GetSalt(t *testing.T) { + pm, _ := newPublicMsg(publicMinLen * 2) + var salt [saltLen]byte + rand.New(rand.NewSource(42)).Read(salt[:]) + pm.SetSalt(salt) + + testSalt := pm.GetSalt() + if salt != testSalt { + t.Errorf("Failed to get original salt."+ + "\nexpected: %+v\nreceived: %+v", salt, testSalt) + } +} + +// Tests that the original payload matches the saved one. +func Test_publicMsg_SetPayload_GetPayload(t *testing.T) { + pm, _ := newPublicMsg(publicMinLen * 2) + payload := make([]byte, pm.GetPayloadSize()) + copy(payload, "Test payload message.") + pm.SetPayload(payload) + testPayload := pm.GetPayload() + + if !bytes.Equal(payload, testPayload) { + t.Errorf("Failed to get original sender payload."+ + "\nexpected: %q\nreceived: %q", payload, testPayload) + } +} + +// Happy path. +func Test_publicMsg_GetPayloadSize(t *testing.T) { + pm, _ := newPublicMsg(publicMinLen * 2) + + if publicMinLen != pm.GetPayloadSize() { + t.Errorf("GetPayloadSize() failed to return the correct size."+ + "\nexpected: %d\nreceived: %d", publicMinLen, pm.GetPayloadSize()) + } +} + +// Happy path. +func Test_publicMsg_String(t *testing.T) { + pm, _ := newPublicMsg(publicMinLen * 2) + var salt [saltLen]byte + rand.New(rand.NewSource(42)).Read(salt[:]) + pm.SetSalt(salt) + payload := []byte("Sample payload message.") + payload = append(payload, 0, 1, 2) + pm.SetPayload(payload) + + expected := `{salt:U4x/lrFkvxuXu59LtHLon1sUhPJSCcnZND6SugndnVI=, payload:"Sample payload message.\x00\x01\x02\x00\x00\x00\x00\x00\x00"}` + + if pm.String() != expected { + t.Errorf("String() failed to return the expected value."+ + "\nexpected: %s\nreceived: %s", expected, pm.String()) + } +} + +// Happy path: tests that String returns the expected string for a nil publicMsg. +func Test_publicMsg_String_NilInternalMessage(t *testing.T) { + pm := publicMsg{} + + expected := "{salt:<nil>, payload:<nil>}" + + if pm.String() != expected { + t.Errorf("String() failed to return the expected value."+ + "\nexpected: %s\nreceived: %s", expected, pm.String()) + } +} diff --git a/groupChat/receive.go b/groupChat/receive.go new file mode 100644 index 0000000000000000000000000000000000000000..64cf10b789b3d07bf9d1dbd82d6d76b15c91fe53 --- /dev/null +++ b/groupChat/receive.go @@ -0,0 +1,168 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/xx_network/primitives/id" + "time" +) + +// Error messages. +const ( + newDecryptKeyErr = "failed to generate key for decrypting group payload: %+v" + unmarshalInternalMsgErr = "failed to unmarshal group internal message: %+v" + unmarshalSenderIdErr = "failed to unmarshal sender ID: %+v" + unmarshalPublicMsgErr = "failed to unmarshal group cMix message contents: %+v" + findGroupKeyFpErr = "failed to find group with key fingerprint matching %s" + genCryptKeyMacErr = "failed to generate encryption key for group " + + "cMix message because MAC verification failed (epoch %d could be off)" +) + +// receive starts the group message reception worker that waits for new group +// messages to arrive. +func (m Manager) receive(rawMsgs chan message.Receive, stop *stoppable.Single) { + jww.DEBUG.Print("Starting group message reception worker.") + + for { + select { + case <-stop.Quit(): + jww.DEBUG.Print("Stopping group message reception worker.") + stop.ToStopped() + return + case receiveMsg := <-rawMsgs: + jww.DEBUG.Print("Group message reception received cMix message.") + + // Attempt to read the message + g, msgID, timestamp, senderID, msg, err := m.readMessage(receiveMsg) + if err != nil { + jww.WARN.Printf("Group message reception failed to read cMix "+ + "message: %+v", err) + continue + } + + // If the message was read correctly, send it to the callback + go m.receiveFunc(MessageReceive{ + GroupID: g.ID, + ID: msgID, + Payload: msg, + SenderID: senderID, + RecipientID: receiveMsg.RecipientID, + EphemeralID: receiveMsg.EphemeralID, + Timestamp: receiveMsg.Timestamp, + RoundID: receiveMsg.RoundId, + RoundTimestamp: timestamp, + }) + } + } +} + +// readMessage returns the group, message ID, timestamp, sender ID, and message +// of a group message. The encrypted group message data is unmarshaled from a +// cMix message in the message.Receive and then decrypted and the MAC is +// verified. The group is found by finding the group with a matching key +// fingerprint. +func (m *Manager) readMessage(msg message.Receive) (gs.Group, group.MessageID, + time.Time, *id.ID, []byte, error) { + // Unmarshal payload into cMix message + cMixMsg := format.Unmarshal(msg.Payload) + + // Unmarshal cMix message contents to get public message format + publicMsg, err := unmarshalPublicMsg(cMixMsg.GetContents()) + if err != nil { + return gs.Group{}, group.MessageID{}, time.Time{}, nil, nil, + errors.Errorf(unmarshalPublicMsgErr, err) + } + + // Get the group from storage via key fingerprint lookup + g, exists := m.gs.GetByKeyFp(cMixMsg.GetKeyFP(), publicMsg.GetSalt()) + if !exists { + return gs.Group{}, group.MessageID{}, time.Time{}, nil, nil, + errors.Errorf(findGroupKeyFpErr, cMixMsg.GetKeyFP()) + } + + // Decrypt the payload and return the messages timestamp, sender ID, and + // message contents + messageID, timestamp, senderID, contents, err := m.decryptMessage( + g, cMixMsg, publicMsg, msg.RoundTimestamp) + return g, messageID, timestamp, senderID, contents, err +} + +// decryptMessage decrypts the group message payload and returns its message ID, +// timestamp, sender ID, and message contents. +func (m *Manager) decryptMessage(g gs.Group, cMixMsg format.Message, + publicMsg publicMsg, roundTimestamp time.Time) (group.MessageID, time.Time, + *id.ID, []byte, error) { + + key, err := getCryptKey(g.Key, publicMsg.GetSalt(), cMixMsg.GetMac(), + publicMsg.GetPayload(), g.DhKeys, roundTimestamp) + if err != nil { + return group.MessageID{}, time.Time{}, nil, nil, err + } + + // Decrypt internal message + decryptedPayload := group.Decrypt(key, cMixMsg.GetKeyFP(), + publicMsg.GetPayload()) + + // Unmarshal internal message + internalMsg, err := unmarshalInternalMsg(decryptedPayload) + if err != nil { + return group.MessageID{}, time.Time{}, nil, nil, + errors.Errorf(unmarshalInternalMsgErr, err) + } + + // Unmarshal sender ID + senderID, err := internalMsg.GetSenderID() + if err != nil { + return group.MessageID{}, time.Time{}, nil, nil, + errors.Errorf(unmarshalSenderIdErr, err) + } + + messageID := group.NewMessageID(g.ID, internalMsg.Marshal()) + + return messageID, internalMsg.GetTimestamp(), senderID, + internalMsg.GetPayload(), nil +} + +// getCryptKey generates the decryption key for a group internal message. The +// key is generated using the group key, an epoch, and a salt. The epoch is +// based off the round timestamp. So, to avoid missing the correct epoch, the +// current, past, and next epochs are checked until one of them produces a key +// that matches the message's MAC. The DH key is also unknown, so each member's +// DH key is tried until there is a match. +func getCryptKey(key group.Key, salt [group.SaltLen]byte, mac, payload []byte, + dhKeys gs.DhKeyList, roundTimestamp time.Time) (group.CryptKey, error) { + // Compute the current epoch + epoch := group.ComputeEpoch(roundTimestamp) + + for _, dhKey := range dhKeys { + + // Create a key with the correct epoch + for _, epoch := range []uint32{epoch, epoch - 1, epoch + 1} { + // Generate key + cryptKey, err := group.NewKdfKey(key, epoch, salt) + if err != nil { + return group.CryptKey{}, errors.Errorf(newDecryptKeyErr, err) + } + + // Return the key if the MAC matches + if group.CheckMAC(mac, cryptKey, payload, dhKey) { + return cryptKey, nil + } + } + } + + // Return an error if none of the epochs worked + return group.CryptKey{}, errors.Errorf(genCryptKeyMacErr, epoch) +} diff --git a/groupChat/receiveRequest.go b/groupChat/receiveRequest.go new file mode 100644 index 0000000000000000000000000000000000000000..e5c7576f174f793d29ef337e092c96e4d782c371 --- /dev/null +++ b/groupChat/receiveRequest.go @@ -0,0 +1,111 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "github.com/golang/protobuf/proto" + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" + "gitlab.com/elixxir/crypto/group" +) + +// Error message. +const ( + sendMessageTypeErr = "message not of type GroupCreationRequest" + protoUnmarshalErr = "failed to unmarshal request: %+v" + deserializeMembershipErr = "failed to deserialize membership: %+v" +) + +// receiveRequest starts the group request reception worker that waits for new +// group requests to arrive. +func (m Manager) receiveRequest(rawMsgs chan message.Receive, stop *stoppable.Single) { + jww.DEBUG.Print("Starting group message request reception worker.") + + for { + select { + case <-stop.Quit(): + jww.DEBUG.Print("Stopping group message request reception worker.") + stop.ToStopped() + return + case sendMsg := <-rawMsgs: + jww.DEBUG.Print("Group message request received send message.") + + // Generate the group from the request message + g, err := m.readRequest(sendMsg) + if err != nil { + jww.WARN.Printf("Failed to read message as group request: %+v", + err) + continue + } + + // Call request callback with the new group if it does not already + // exist + if _, exists := m.GetGroup(g.ID); !exists { + go m.requestFunc(g) + } + } + } +} + +// readRequest returns the group describes in the group request message. An +// error is returned if the request is of the wrong type or cannot be read. +func (m *Manager) readRequest(msg message.Receive) (gs.Group, error) { + // Return an error if the message is not of the right type + if msg.MessageType != message.GroupCreationRequest { + return gs.Group{}, errors.New(sendMessageTypeErr) + } + + // Unmarshal the request message + request := &Request{} + err := proto.Unmarshal(msg.Payload, request) + if err != nil { + return gs.Group{}, errors.Errorf(protoUnmarshalErr, err) + } + + // Deserialize membership list + membership, err := group.DeserializeMembership(request.Members) + if err != nil { + return gs.Group{}, errors.Errorf(deserializeMembershipErr, err) + } + + // Get the relationship with the group leader + partner, err := m.store.E2e().GetPartner(membership[0].ID) + if err != nil { + return gs.Group{}, errors.Errorf(getPrivKeyErr, err) + } + + // Replace leader's public key with the one from the partnership + leaderPubKey := membership[0].DhKey.DeepCopy() + membership[0].DhKey = partner.GetPartnerOriginPublicKey() + + // Generate the DH keys with each group member + privKey := partner.GetMyOriginPrivateKey() + grp := m.store.E2e().GetGroup() + dkl := gs.GenerateDhKeyList(m.gs.GetUser().ID, privKey, membership, grp) + + // Restore the original public key for the leader so that the membership + // digest generated later is correct + membership[0].DhKey = leaderPubKey + + // Copy preimages + var idPreimage group.IdPreimage + copy(idPreimage[:], request.IdPreimage) + var keyPreimage group.KeyPreimage + copy(keyPreimage[:], request.KeyPreimage) + + // Create group ID and key + groupID := group.NewID(idPreimage, membership) + groupKey := group.NewKey(keyPreimage, membership) + + // Return the new group + return gs.NewGroup(request.Name, groupID, groupKey, idPreimage, keyPreimage, + request.Message, membership, dkl), nil +} diff --git a/groupChat/receiveRequest_test.go b/groupChat/receiveRequest_test.go new file mode 100644 index 0000000000000000000000000000000000000000..6925853a325948b005c7c38e11f2a28621ab2b11 --- /dev/null +++ b/groupChat/receiveRequest_test.go @@ -0,0 +1,241 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "github.com/golang/protobuf/proto" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" + "math/rand" + "strings" + "testing" + "time" +) + +// // Tests that the correct group is received from the request. +// func TestManager_receiveRequest(t *testing.T) { +// prng := rand.New(rand.NewSource(42)) +// requestChan := make(chan gs.Group) +// requestFunc := func(g gs.Group) { requestChan <- g } +// m, _ := newTestManagerWithStore(prng, 10, 0, requestFunc, nil, t) +// g := newTestGroupWithUser(m.store.E2e().GetGroup(), +// m.store.GetUser().ReceptionID, m.store.GetUser().E2eDhPublicKey, +// m.store.GetUser().E2eDhPrivateKey, prng, t) +// +// requestMarshaled, err := proto.Marshal(&Request{ +// Name: g.Name, +// IdPreimage: g.IdPreimage.Bytes(), +// KeyPreimage: g.KeyPreimage.Bytes(), +// Members: g.Members.Serialize(), +// Message: g.InitMessage, +// }) +// if err != nil { +// t.Errorf("Failed to marshal proto message: %+v", err) +// } +// +// msg := message.Receive{ +// Payload: requestMarshaled, +// MessageType: message.GroupCreationRequest, +// } +// +// rawMessages := make(chan message.Receive) +// quit := make(chan struct{}) +// go m.receiveRequest(rawMessages, quit) +// rawMessages <- msg +// +// select { +// case receivedGrp := <-requestChan: +// if !reflect.DeepEqual(g, receivedGrp) { +// t.Errorf("receiveRequest() failed to return the expected group."+ +// "\nexpected: %#v\nreceived: %#v", g, receivedGrp) +// } +// case <-time.NewTimer(5 * time.Millisecond).C: +// t.Error("Timed out while waiting for callback.") +// } +// } + +// Tests that the callback is not called when the group already exists in the +// manager. +func TestManager_receiveRequest_GroupExists(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + requestChan := make(chan gs.Group) + requestFunc := func(g gs.Group) { requestChan <- g } + m, g := newTestManagerWithStore(prng, 10, 0, requestFunc, nil, t) + + requestMarshaled, err := proto.Marshal(&Request{ + Name: g.Name, + IdPreimage: g.IdPreimage.Bytes(), + KeyPreimage: g.KeyPreimage.Bytes(), + Members: g.Members.Serialize(), + Message: g.InitMessage, + }) + if err != nil { + t.Errorf("Failed to marshal proto message: %+v", err) + } + + msg := message.Receive{ + Payload: requestMarshaled, + MessageType: message.GroupCreationRequest, + } + + rawMessages := make(chan message.Receive) + stop := stoppable.NewSingle("testStoppable") + go m.receiveRequest(rawMessages, stop) + rawMessages <- msg + + select { + case <-requestChan: + t.Error("receiveRequest() called the callback when the group already " + + "exists in the list.") + case <-time.NewTimer(5 * time.Millisecond).C: + } +} + +// Tests that the quit channel quits the worker. +func TestManager_receiveRequest_QuitChan(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + requestChan := make(chan gs.Group) + requestFunc := func(g gs.Group) { requestChan <- g } + m, _ := newTestManagerWithStore(prng, 10, 0, requestFunc, nil, t) + + rawMessages := make(chan message.Receive) + stop := stoppable.NewSingle("testStoppable") + done := make(chan struct{}) + go func() { + m.receiveRequest(rawMessages, stop) + done <- struct{}{} + }() + if err := stop.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } + + select { + case <-done: + case <-time.NewTimer(5 * time.Millisecond).C: + t.Error("receiveRequest() failed to close when the quit.") + } +} + +// Tests that the callback is not called when the send message is not of the +// correct type. +func TestManager_receiveRequest_SendMessageTypeError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + requestChan := make(chan gs.Group) + requestFunc := func(g gs.Group) { requestChan <- g } + m, _ := newTestManagerWithStore(prng, 10, 0, requestFunc, nil, t) + + msg := message.Receive{ + MessageType: message.NoType, + } + + rawMessages := make(chan message.Receive) + stop := stoppable.NewSingle("singleStoppable") + go m.receiveRequest(rawMessages, stop) + rawMessages <- msg + + select { + case receivedGrp := <-requestChan: + t.Errorf("Callback called when the message should have been skipped: %#v", + receivedGrp) + case <-time.NewTimer(5 * time.Millisecond).C: + } +} + +// // Unit test of readRequest. +// func TestManager_readRequest(t *testing.T) { +// m, g := newTestManager(rand.New(rand.NewSource(42)), t) +// _ = m.store.E2e().AddPartner( +// g.Members[0].ID, +// g.Members[0].DhKey, +// m.store.E2e().GetGroup().NewInt(43), +// params.GetDefaultE2ESessionParams(), +// params.GetDefaultE2ESessionParams(), +// ) +// +// requestMarshaled, err := proto.Marshal(&Request{ +// Name: g.Name, +// IdPreimage: g.IdPreimage.Bytes(), +// KeyPreimage: g.KeyPreimage.Bytes(), +// Members: g.Members.Serialize(), +// Message: g.InitMessage, +// }) +// if err != nil { +// t.Errorf("Failed to marshal proto message: %+v", err) +// } +// +// msg := message.Receive{ +// Payload: requestMarshaled, +// MessageType: message.GroupCreationRequest, +// } +// +// newGrp, err := m.readRequest(msg) +// if err != nil { +// t.Errorf("readRequest() returned an error: %+v", err) +// } +// +// if !reflect.DeepEqual(g, newGrp) { +// t.Errorf("readRequest() returned the wrong group."+ +// "\nexpected: %#v\nreceived: %#v", g, newGrp) +// } +// } + +// Error path: an error is returned if the message type is incorrect. +func TestManager_readRequest_MessageTypeError(t *testing.T) { + m, _ := newTestManager(rand.New(rand.NewSource(42)), t) + expectedErr := sendMessageTypeErr + msg := message.Receive{ + MessageType: message.NoType, + } + + _, err := m.readRequest(msg) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("readRequest() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: an error is returned if the proto message cannot be unmarshalled. +func TestManager_readRequest_ProtoUnmarshalError(t *testing.T) { + expectedErr := strings.SplitN(deserializeMembershipErr, "%", 2)[0] + m, _ := newTestManager(rand.New(rand.NewSource(42)), t) + + requestMarshaled, err := proto.Marshal(&Request{ + Members: []byte("Invalid membership serial."), + }) + if err != nil { + t.Errorf("Failed to marshal proto message: %+v", err) + } + + msg := message.Receive{ + Payload: requestMarshaled, + MessageType: message.GroupCreationRequest, + } + + _, err = m.readRequest(msg) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("readRequest() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: an error is returned if the membership cannot be deserialized. +func TestManager_readRequest_DeserializeMembershipError(t *testing.T) { + m, _ := newTestManager(rand.New(rand.NewSource(42)), t) + expectedErr := strings.SplitN(protoUnmarshalErr, "%", 2)[0] + msg := message.Receive{ + Payload: []byte("Invalid message."), + MessageType: message.GroupCreationRequest, + } + + _, err := m.readRequest(msg) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("readRequest() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} diff --git a/groupChat/receive_test.go b/groupChat/receive_test.go new file mode 100644 index 0000000000000000000000000000000000000000..36ea8ed2dbad4c10630630f198d07d4b6a96bf54 --- /dev/null +++ b/groupChat/receive_test.go @@ -0,0 +1,409 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "bytes" + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" + "gitlab.com/elixxir/crypto/e2e" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/xx_network/primitives/netTime" + "math/rand" + "reflect" + "strings" + "testing" + "time" +) + +// Tests that Manager.receive returns the correct message on the callback. +func TestManager_receive(t *testing.T) { + // Setup callback + msgChan := make(chan MessageReceive) + receiveFunc := func(msg MessageReceive) { msgChan <- msg } + + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, receiveFunc, t) + + // Create test parameters + contents := []byte("Test group message.") + timestamp := netTime.Now() + sender := m.gs.GetUser() + + expectedMsg := MessageReceive{ + GroupID: g.ID, + ID: group.MessageID{0, 1, 2, 3}, + Payload: contents, + SenderID: sender.ID, + RoundTimestamp: timestamp.Local(), + } + + // Create cMix message and get public message + cMixMsg, err := m.newCmixMsg(g, contents, timestamp, g.Members[4], prng) + if err != nil { + t.Errorf("Failed to create new cMix message: %+v", err) + } + + internalMsg, _ := newInternalMsg(cMixMsg.ContentsSize() - publicMinLen) + internalMsg.SetTimestamp(timestamp) + internalMsg.SetSenderID(m.gs.GetUser().ID) + internalMsg.SetPayload(contents) + expectedMsg.ID = group.NewMessageID(g.ID, internalMsg.Marshal()) + + receiveChan := make(chan message.Receive, 1) + stop := stoppable.NewSingle("singleStoppable") + + m.gs.SetUser(g.Members[4], t) + go m.receive(receiveChan, stop) + + receiveChan <- message.Receive{ + Payload: cMixMsg.Marshal(), + RoundTimestamp: timestamp, + } + + select { + case msg := <-msgChan: + if !reflect.DeepEqual(expectedMsg, msg) { + t.Errorf("Failed to received expected message."+ + "\nexpected: %+v\nreceived: %+v", expectedMsg, msg) + } + case <-time.NewTimer(10 * time.Millisecond).C: + t.Errorf("Timed out waiting to receive group message.") + } +} + +// Tests that the callback is not called when the message cannot be read. +func TestManager_receive_ReadMessageError(t *testing.T) { + // Setup callback + msgChan := make(chan MessageReceive) + receiveFunc := func(msg MessageReceive) { msgChan <- msg } + + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, receiveFunc, t) + + receiveChan := make(chan message.Receive, 1) + stop := stoppable.NewSingle("singleStoppable") + + go m.receive(receiveChan, stop) + + receiveChan <- message.Receive{ + Payload: make([]byte, format.MinimumPrimeSize*2), + } + + select { + case <-msgChan: + t.Error("Callback called when message should have errored.") + case <-time.NewTimer(5 * time.Millisecond).C: + } +} + +// Tests that the quit channel exits the function. +func TestManager_receive_QuitChan(t *testing.T) { + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + receiveChan := make(chan message.Receive, 1) + stop := stoppable.NewSingle("singleStoppable") + doneChan := make(chan struct{}) + + go func() { + m.receive(receiveChan, stop) + doneChan <- struct{}{} + }() + + if err := stop.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } + + select { + case <-doneChan: + case <-time.NewTimer(10 * time.Millisecond).C: + t.Errorf("Timed out waiting for thread to quit.") + } +} + +// Tests that Manager.readMessage returns the message data for the correct group. +func TestManager_readMessage(t *testing.T) { + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, expectedGrp := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + // Create test parameters + expectedContents := []byte("Test group message.") + expectedTimestamp := netTime.Now() + sender := m.gs.GetUser() + + // Create cMix message and get public message + cMixMsg, err := m.newCmixMsg(expectedGrp, expectedContents, + expectedTimestamp, expectedGrp.Members[4], prng) + if err != nil { + t.Errorf("Failed to create new cMix message: %+v", err) + } + + internalMsg, _ := newInternalMsg(cMixMsg.ContentsSize() - publicMinLen) + internalMsg.SetTimestamp(expectedTimestamp) + internalMsg.SetSenderID(sender.ID) + internalMsg.SetPayload(expectedContents) + expectedMsgID := group.NewMessageID(expectedGrp.ID, internalMsg.Marshal()) + + // Build message.Receive + receiveMsg := message.Receive{ + ID: e2e.MessageID{}, + Payload: cMixMsg.Marshal(), + RoundTimestamp: expectedTimestamp, + } + + m.gs.SetUser(expectedGrp.Members[4], t) + g, messageID, timestamp, senderID, contents, err := m.readMessage(receiveMsg) + if err != nil { + t.Errorf("readMessage() returned an error: %+v", err) + } + + if !reflect.DeepEqual(expectedGrp, g) { + t.Errorf("readMessage() returned incorrect group."+ + "\nexpected: %#v\nreceived: %#v", expectedGrp, g) + } + + if expectedMsgID != messageID { + t.Errorf("readMessage() returned incorrect message ID."+ + "\nexpected: %s\nreceived: %s", expectedMsgID, messageID) + } + + if !expectedTimestamp.Equal(timestamp) { + t.Errorf("readMessage() returned incorrect timestamp."+ + "\nexpected: %s\nreceived: %s", expectedTimestamp, timestamp) + } + + if !sender.ID.Cmp(senderID) { + t.Errorf("readMessage() returned incorrect sender ID."+ + "\nexpected: %s\nreceived: %s", sender.ID, senderID) + } + + if !bytes.Equal(expectedContents, contents) { + t.Errorf("readMessage() returned incorrect message."+ + "\nexpected: %s\nreceived: %s", expectedContents, contents) + } +} + +// Error path: an error is returned when a group with a matching group +// fingerprint cannot be found. +func TestManager_readMessage_FindGroupKpError(t *testing.T) { + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + // Create test parameters + expectedContents := []byte("Test group message.") + expectedTimestamp := netTime.Now() + + // Create cMix message and get public message + cMixMsg, err := m.newCmixMsg(g, expectedContents, expectedTimestamp, g.Members[4], prng) + if err != nil { + t.Errorf("Failed to create new cMix message: %+v", err) + } + + cMixMsg.SetKeyFP(format.NewFingerprint([]byte("invalid Fingerprint"))) + + // Build message.Receive + receiveMsg := message.Receive{ + ID: e2e.MessageID{}, + Payload: cMixMsg.Marshal(), + RoundTimestamp: expectedTimestamp, + } + + expectedErr := strings.SplitN(findGroupKeyFpErr, "%", 2)[0] + + m.gs.SetUser(g.Members[4], t) + _, _, _, _, _, err = m.readMessage(receiveMsg) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("readMessage() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Tests that a cMix message created by Manager.newCmixMsg can be read by +// Manager.readMessage. +func TestManager_decryptMessage(t *testing.T) { + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, g := newTestManager(prng, t) + + // Create test parameters + expectedContents := []byte("Test group message.") + expectedTimestamp := netTime.Now() + + // Create cMix message and get public message + msg, err := m.newCmixMsg(g, expectedContents, expectedTimestamp, g.Members[4], prng) + if err != nil { + t.Errorf("Failed to create new cMix message: %+v", err) + } + publicMsg, err := unmarshalPublicMsg(msg.GetContents()) + if err != nil { + t.Errorf("Failed to unmarshal publicMsg: %+v", err) + } + + internalMsg, _ := newInternalMsg(publicMsg.GetPayloadSize()) + internalMsg.SetTimestamp(expectedTimestamp) + internalMsg.SetSenderID(m.gs.GetUser().ID) + internalMsg.SetPayload(expectedContents) + expectedMsgID := group.NewMessageID(g.ID, internalMsg.Marshal()) + + // Read message and check if the outputs are correct + messageID, timestamp, senderID, contents, err := m.decryptMessage(g, msg, + publicMsg, expectedTimestamp) + if err != nil { + t.Errorf("decryptMessage() returned an error: %+v", err) + } + + if expectedMsgID != messageID { + t.Errorf("decryptMessage() returned incorrect message ID."+ + "\nexpected: %s\nreceived: %s", expectedMsgID, messageID) + } + + if !expectedTimestamp.Equal(timestamp) { + t.Errorf("decryptMessage() returned incorrect timestamp."+ + "\nexpected: %s\nreceived: %s", expectedTimestamp, timestamp) + } + + if !m.gs.GetUser().ID.Cmp(senderID) { + t.Errorf("decryptMessage() returned incorrect sender ID."+ + "\nexpected: %s\nreceived: %s", m.gs.GetUser().ID, senderID) + } + + if !bytes.Equal(expectedContents, contents) { + t.Errorf("decryptMessage() returned incorrect message."+ + "\nexpected: %s\nreceived: %s", expectedContents, contents) + } +} + +// Error path: an error is returned when the wrong timestamp is passed in and +// the decryption key cannot be generated because of the wrong epoch. +func TestManager_decryptMessage_GetCryptKeyError(t *testing.T) { + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, g := newTestManager(prng, t) + + // Create test parameters + contents := []byte("Test group message.") + timestamp := netTime.Now() + + // Create cMix message and get public message + msg, err := m.newCmixMsg(g, contents, timestamp, g.Members[4], prng) + if err != nil { + t.Errorf("Failed to create new cMix message: %+v", err) + } + publicMsg, err := unmarshalPublicMsg(msg.GetContents()) + if err != nil { + t.Errorf("Failed to unmarshal publicMsg: %+v", err) + } + + // Check if error is correct + expectedErr := strings.SplitN(genCryptKeyMacErr, "%", 2)[0] + _, _, _, _, err = m.decryptMessage(g, msg, publicMsg, timestamp.Add(time.Hour)) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("decryptMessage() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: an error is returned when the decrypted payload cannot be +// unmarshaled. +func TestManager_decryptMessage_UnmarshalInternalMsgError(t *testing.T) { + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, g := newTestManager(prng, t) + + // Create test parameters + contents := []byte("Test group message.") + timestamp := netTime.Now() + + // Create cMix message and get public message + msg, err := m.newCmixMsg(g, contents, timestamp, g.Members[4], prng) + if err != nil { + t.Errorf("Failed to create new cMix message: %+v", err) + } + publicMsg, err := unmarshalPublicMsg(msg.GetContents()) + if err != nil { + t.Errorf("Failed to unmarshal publicMsg: %+v", err) + } + + // Modify publicMsg to have invalid payload + publicMsg = mapPublicMsg(publicMsg.Marshal()[:33]) + key, err := group.NewKdfKey(g.Key, group.ComputeEpoch(timestamp), publicMsg.GetSalt()) + if err != nil { + t.Errorf("failed to create new key: %+v", err) + } + msg.SetMac(group.NewMAC(key, publicMsg.GetPayload(), g.DhKeys[*g.Members[4].ID])) + + // Check if error is correct + expectedErr := strings.SplitN(unmarshalInternalMsgErr, "%", 2)[0] + _, _, _, _, err = m.decryptMessage(g, msg, publicMsg, timestamp) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("decryptMessage() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of getCryptKey. +func Test_getCryptKey(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + g := newTestGroup(getGroup(), getGroup().NewInt(42), prng, t) + salt, err := newSalt(prng) + if err != nil { + t.Errorf("failed to create new salt: %+v", err) + } + payload := []byte("payload") + ts := netTime.Now() + + expectedKey, err := group.NewKdfKey(g.Key, group.ComputeEpoch(ts.Add(5*time.Minute)), salt) + if err != nil { + t.Errorf("failed to create new key: %+v", err) + } + mac := group.NewMAC(expectedKey, payload, g.DhKeys[*g.Members[4].ID]) + + key, err := getCryptKey(g.Key, salt, mac, payload, g.DhKeys, ts) + if err != nil { + t.Errorf("getCryptKey() returned an error: %+v", err) + } + + if expectedKey != key { + t.Errorf("getCryptKey() did not return the expected key."+ + "\nexpected: %v\nreceived: %v", expectedKey, key) + } +} + +// Error path: return an error when the MAC cannot be verified because the +// timestamp is incorrect and generates the wrong epoch. +func Test_getCryptKey_EpochError(t *testing.T) { + expectedErr := strings.SplitN(genCryptKeyMacErr, "%", 2)[0] + + prng := rand.New(rand.NewSource(42)) + g := newTestGroup(getGroup(), getGroup().NewInt(42), prng, t) + salt, err := newSalt(prng) + if err != nil { + t.Errorf("failed to create new salt: %+v", err) + } + payload := []byte("payload") + ts := netTime.Now() + + key, err := group.NewKdfKey(g.Key, group.ComputeEpoch(ts), salt) + if err != nil { + t.Errorf("getCryptKey() returned an error: %+v", err) + } + mac := group.NewMAC(key, payload, g.Members[4].DhKey) + + _, err = getCryptKey(g.Key, salt, mac, payload, g.DhKeys, ts.Add(time.Hour)) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("getCryptKey() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} diff --git a/groupChat/send.go b/groupChat/send.go new file mode 100644 index 0000000000000000000000000000000000000000..f2baa0953555b13f335111af4d1dd7ae0e01731e --- /dev/null +++ b/groupChat/send.go @@ -0,0 +1,222 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "github.com/pkg/errors" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/interfaces/params" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/netTime" + "io" + "time" +) + +// Error messages. +const ( + newCmixMsgErr = "failed to generate cMix messages for group chat: %+v" + sendManyCmixErr = "failed to send group chat message from member %s to group %s: %+v" + newCmixErr = "failed to generate cMix message for member %d with ID %s in group %s: %+v" + messageLenErr = "message length %d is greater than maximum message space %d" + newNoGroupErr = "failed to create message for group %s that cannot be found" + newKeyErr = "failed to generate key for encrypting group payload" + newPublicMsgErr = "failed to create new public group message for cMix message: %+v" + newInternalMsgErr = "failed to create new internal group message for cMix message: %+v" + saltReadErr = "failed to generate salt for group message: %+v" + saltReadLengthErr = "length of generated salt %d != %d required" +) + +// Send sends a message to all group members using Client.SendManyCMIX. The +// send fails if the message is too long. +func (m *Manager) Send(groupID *id.ID, message []byte) (id.Round, error) { + + // Create a cMix message for each group member + messages, err := m.createMessages(groupID, message) + if err != nil { + return 0, errors.Errorf(newCmixMsgErr, err) + } + + rid, _, err := m.net.SendManyCMIX(messages, params.GetDefaultCMIX()) + if err != nil { + return 0, errors.Errorf(sendManyCmixErr, m.gs.GetUser().ID, groupID, err) + } + + return rid, nil +} + +// createMessages generates a list of cMix messages and a list of corresponding +// recipient IDs. +func (m *Manager) createMessages(groupID *id.ID, msg []byte) (map[id.ID]format.Message, error) { + timeNow := netTime.Now() + + g, exists := m.gs.Get(groupID) + if !exists { + return map[id.ID]format.Message{}, errors.Errorf(newNoGroupErr, groupID) + } + + return m.newMessages(g, msg, timeNow) +} + +// newMessages is a private function that allows the passing in of a timestamp +// and streamGen instead of a fastRNG.StreamGenerator for easier testing. +func (m *Manager) newMessages(g gs.Group, msg []byte, + timestamp time.Time) (map[id.ID]format.Message, error) { + // Create list of cMix messages + messages := make(map[id.ID]format.Message) + + // Create channels to receive messages and errors on + type msgInfo struct { + msg format.Message + id *id.ID + } + msgChan := make(chan msgInfo, len(g.Members)-1) + errChan := make(chan error, len(g.Members)-1) + + // Create cMix messages in parallel + for i, member := range g.Members { + // Do not send to the sender + if m.gs.GetUser().ID.Cmp(member.ID) { + continue + } + + // Start thread to build cMix message + go func(member group.Member, i int) { + // Create new stream + rng := m.rng.GetStream() + defer rng.Close() + + // Add cMix message to list + cMixMsg, err := m.newCmixMsg(g, msg, timestamp, member, rng) + if err != nil { + errChan <- errors.Errorf(newCmixErr, i, member.ID, g.ID, err) + } + msgChan <- msgInfo{cMixMsg, member.ID} + + }(member, i) + } + + // Wait for messages or errors + for len(messages) < len(g.Members)-1 { + select { + case err := <-errChan: + // Return on the first error that occurs + return nil, err + case info := <-msgChan: + messages[*info.id] = info.msg + } + } + + return messages, nil +} + +// newCmixMsg generates a new cMix message to be sent to a group member. +func (m *Manager) newCmixMsg(g gs.Group, msg []byte, timestamp time.Time, + mem group.Member, rng io.Reader) (format.Message, error) { + + // Create three message layers + cmixMsg := format.NewMessage(m.store.Cmix().GetGroup().GetP().ByteLen()) + publicMsg, internalMsg, err := newMessageParts(cmixMsg.ContentsSize()) + if err != nil { + return cmixMsg, err + } + + // Return an error if the message is too large to fit in the payload + if internalMsg.GetPayloadMaxSize() < len(msg) { + return cmixMsg, errors.Errorf(messageLenErr, len(msg), + internalMsg.GetPayloadMaxSize()) + } + + // Generate 256-bit salt + salt, err := newSalt(rng) + if err != nil { + return cmixMsg, err + } + + // Generate key fingerprint + keyFp := group.NewKeyFingerprint(g.Key, salt, mem.ID) + + // Generate key + key, err := group.NewKdfKey(g.Key, group.ComputeEpoch(timestamp), salt) + if err != nil { + return cmixMsg, errors.WithMessage(err, newKeyErr) + } + + // Generate internal message + payload := setInternalPayload(internalMsg, timestamp, m.gs.GetUser().ID, msg) + + // Encrypt internal message + encryptedPayload := group.Encrypt(key, keyFp, payload) + + // Generate public message + publicPayload := setPublicPayload(publicMsg, salt, encryptedPayload) + + // Generate MAC + mac := group.NewMAC(key, encryptedPayload, g.DhKeys[*mem.ID]) + + // Construct cMix message + cmixMsg.SetContents(publicPayload) + cmixMsg.SetKeyFP(keyFp) + cmixMsg.SetMac(mac) + + return cmixMsg, nil +} + +// newMessageParts generates a public payload message and the internal payload +// message. An error is returned if the messages cannot fit in the payloadSize. +func newMessageParts(payloadSize int) (publicMsg, internalMsg, error) { + publicMsg, err := newPublicMsg(payloadSize) + if err != nil { + return publicMsg, internalMsg{}, errors.Errorf(newPublicMsgErr, err) + } + + internalMsg, err := newInternalMsg(publicMsg.GetPayloadSize()) + if err != nil { + return publicMsg, internalMsg, errors.Errorf(newInternalMsgErr, err) + } + + return publicMsg, internalMsg, nil +} + +// newSalt generates a new salt of the specified size. +func newSalt(rng io.Reader) ([group.SaltLen]byte, error) { + var salt [group.SaltLen]byte + n, err := rng.Read(salt[:]) + if err != nil { + return salt, errors.Errorf(saltReadErr, err) + } else if n != group.SaltLen { + return salt, errors.Errorf(saltReadLengthErr, group.SaltLen, n) + } + + return salt, nil +} + +// setInternalPayload sets the timestamp, sender ID, and message of the +// internalMsg and returns the marshal bytes. +func setInternalPayload(internalMsg internalMsg, timestamp time.Time, + sender *id.ID, msg []byte) []byte { + // Set timestamp, sender ID, and message to the internalMsg + internalMsg.SetTimestamp(timestamp) + internalMsg.SetSenderID(sender) + internalMsg.SetPayload(msg) + + // Return the payload marshaled + return internalMsg.Marshal() +} + +// setPublicPayload sets the salt and encrypted payload of the publicMsg and +// returns the marshal bytes. +func setPublicPayload(publicMsg publicMsg, salt [group.SaltLen]byte, + encryptedPayload []byte) []byte { + // Set salt and payload + publicMsg.SetSalt(salt) + publicMsg.SetPayload(encryptedPayload) + + return publicMsg.Marshal() +} diff --git a/groupChat/sendRequests.go b/groupChat/sendRequests.go new file mode 100644 index 0000000000000000000000000000000000000000..3e4cc39f2995dbe870b4caa771cbd3a075c77eb7 --- /dev/null +++ b/groupChat/sendRequests.go @@ -0,0 +1,129 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "github.com/golang/protobuf/proto" + "github.com/pkg/errors" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/interfaces/params" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/xx_network/primitives/id" + "strings" +) + +// Error messages. +const ( + resendGroupIdErr = "cannot resend request to nonexistent group with ID %s" + protoMarshalErr = "failed to form outgoing group chat request: %+v" + sendE2eErr = "failed to send group request via E2E to member %s: %+v" + sendRequestAllErr = "failed to send all %d group request messages: %s" + sendRequestPartialErr = "failed to send %d/%d group request messages: %s" +) + +// ResendRequest allows a groupChat request to be sent again. +func (m Manager) ResendRequest(groupID *id.ID) ([]id.Round, RequestStatus, error) { + g, exists := m.gs.Get(groupID) + if !exists { + return nil, NotSent, errors.Errorf(resendGroupIdErr, groupID) + } + + return m.sendRequests(g) +} + +// sendRequests sends group requests to each member in the group except for the +// leader/sender +func (m Manager) sendRequests(g gs.Group) ([]id.Round, RequestStatus, error) { + // Build request message + requestMarshaled, err := proto.Marshal(&Request{ + Name: g.Name, + IdPreimage: g.IdPreimage.Bytes(), + KeyPreimage: g.KeyPreimage.Bytes(), + Members: g.Members.Serialize(), + Message: g.InitMessage, + }) + if err != nil { + return nil, NotSent, errors.Errorf(protoMarshalErr, err) + } + + // Create channel to return the results of each send on + n := len(g.Members) - 1 + type sendResults struct { + rounds []id.Round + err error + } + resultsChan := make(chan sendResults, n) + + // Send request to each member in the group except the leader/sender + for _, member := range g.Members[1:] { + go func(member group.Member) { + rounds, err := m.sendRequest(member.ID, requestMarshaled) + resultsChan <- sendResults{rounds, err} + }(member) + } + + // Block until each send returns + roundIDs := make(map[id.Round]struct{}) + var errs []string + for i := 0; i < n; { + select { + case results := <-resultsChan: + for _, rid := range results.rounds { + roundIDs[rid] = struct{}{} + } + if results.err != nil { + errs = append(errs, results.err.Error()) + } + i++ + } + } + + // If all sends returned an error, then return AllFail with a list of errors + if len(errs) == n { + return nil, AllFail, + errors.Errorf(sendRequestAllErr, len(errs), strings.Join(errs, "\n")) + } + + // If some sends returned an error, then return a list of round IDs for the + // successful sends and a list of errors for the failed ones + if len(errs) > 0 { + return roundIdMap2List(roundIDs), PartialSent, + errors.Errorf(sendRequestPartialErr, len(errs), n, + strings.Join(errs, "\n")) + } + + // If all sends succeeded, return a list of roundIDs + return roundIdMap2List(roundIDs), AllSent, nil +} + +// sendRequest sends the group request to the user via E2E. +func (m Manager) sendRequest(memberID *id.ID, request []byte) ([]id.Round, error) { + sendMsg := message.Send{ + Recipient: memberID, + Payload: request, + MessageType: message.GroupCreationRequest, + } + + rounds, _, err := m.net.SendE2E(sendMsg, params.GetDefaultE2E(), nil) + if err != nil { + return nil, errors.Errorf(sendE2eErr, memberID, err) + } + + return rounds, nil +} + +// roundIdMap2List converts the map of round IDs to a list of round IDs. +func roundIdMap2List(m map[id.Round]struct{}) []id.Round { + roundIDs := make([]id.Round, 0, len(m)) + for rid := range m { + roundIDs = append(roundIDs, rid) + } + + return roundIDs +} diff --git a/groupChat/sendRequests_test.go b/groupChat/sendRequests_test.go new file mode 100644 index 0000000000000000000000000000000000000000..56ca284fbc66cb78622388bd11d0454db3280bce --- /dev/null +++ b/groupChat/sendRequests_test.go @@ -0,0 +1,274 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "fmt" + "github.com/golang/protobuf/proto" + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/xx_network/primitives/id" + "math/rand" + "reflect" + "sort" + "strings" + "testing" +) + +// Tests that Manager.ResendRequest sends all expected requests successfully. +func TestManager_ResendRequest(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + expected := &Request{ + Name: g.Name, + IdPreimage: g.IdPreimage.Bytes(), + KeyPreimage: g.KeyPreimage.Bytes(), + Members: g.Members.Serialize(), + Message: g.InitMessage, + } + + _, status, err := m.ResendRequest(g.ID) + if err != nil { + t.Errorf("ResendRequest() returned an error: %+v", err) + } + + if status != AllSent { + t.Errorf("ResendRequest() failed to return the expected status."+ + "\nexpected: %s\nreceived: %s", AllSent, status) + } + + if len(m.net.(*testNetworkManager).e2eMessages) < len(g.Members)-1 { + t.Errorf("ResendRequest() failed to send the correct number of requests."+ + "\nexpected: %d\nreceived: %d", len(g.Members)-1, + len(m.net.(*testNetworkManager).e2eMessages)) + } + + for i := 0; i < len(m.net.(*testNetworkManager).e2eMessages); i++ { + msg := m.net.(*testNetworkManager).GetE2eMsg(i) + + // Check if the message recipient is a member in the group + matchesMember := false + for j, m := range g.Members { + if msg.Recipient.Cmp(m.ID) { + matchesMember = true + g.Members = append(g.Members[:j], g.Members[j+1:]...) + break + } + } + if !matchesMember { + t.Errorf("Message %d has recipient ID %s that is not in membership.", + i, msg.Recipient) + } + + testRequest := &Request{} + err = proto.Unmarshal(msg.Payload, testRequest) + if err != nil { + t.Errorf("Failed to unmarshal proto message (%d): %+v", i, err) + } + + if expected.String() != testRequest.String() { + t.Errorf("Message %d has unexpected payload."+ + "\nexpected: %s\nreceived: %s", i, expected, testRequest) + } + } +} + +// Error path: an error is returned when no group with the corresponding group +// ID exists. +func TestManager_ResendRequest_GetGroupError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + expectedErr := strings.SplitN(resendGroupIdErr, "%", 2)[0] + + _, status, err := m.ResendRequest(id.NewIdFromString("invalidID", id.Group, t)) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("ResendRequest() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } + + if status != NotSent { + t.Errorf("ResendRequest() failed to return the expected status."+ + "\nexpected: %s\nreceived: %s", NotSent, status) + } +} + +// Tests that Manager.sendRequests sends all expected requests successfully. +func TestManager_sendRequests(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + expected := &Request{ + Name: g.Name, + IdPreimage: g.IdPreimage.Bytes(), + KeyPreimage: g.KeyPreimage.Bytes(), + Members: g.Members.Serialize(), + Message: g.InitMessage, + } + + _, status, err := m.sendRequests(g) + if err != nil { + t.Errorf("sendRequests() returned an error: %+v", err) + } + + if status != AllSent { + t.Errorf("sendRequests() failed to return the expected status."+ + "\nexpected: %s\nreceived: %s", AllSent, status) + } + + if len(m.net.(*testNetworkManager).e2eMessages) < len(g.Members)-1 { + t.Errorf("sendRequests() failed to send the correct number of requests."+ + "\nexpected: %d\nreceived: %d", len(g.Members)-1, + len(m.net.(*testNetworkManager).e2eMessages)) + } + + for i := 0; i < len(m.net.(*testNetworkManager).e2eMessages); i++ { + msg := m.net.(*testNetworkManager).GetE2eMsg(i) + + // Check if the message recipient is a member in the group + matchesMember := false + for j, m := range g.Members { + if msg.Recipient.Cmp(m.ID) { + matchesMember = true + g.Members = append(g.Members[:j], g.Members[j+1:]...) + break + } + } + if !matchesMember { + t.Errorf("Message %d has recipient ID %s that is not in membership.", + i, msg.Recipient) + } + + testRequest := &Request{} + err = proto.Unmarshal(msg.Payload, testRequest) + if err != nil { + t.Errorf("Failed to unmarshal proto message (%d): %+v", i, err) + } + + if expected.String() != testRequest.String() { + t.Errorf("Message %d has unexpected payload."+ + "\nexpected: %s\nreceived: %s", i, expected, testRequest) + } + } +} + +// Tests that Manager.sendRequests returns the correct status when all sends +// fail. +func TestManager_sendRequests_SendAllFail(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 1, nil, nil, t) + expectedErr := fmt.Sprintf(sendRequestAllErr, len(g.Members)-1, "") + + rounds, status, err := m.sendRequests(g) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("sendRequests() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } + + if status != AllFail { + t.Errorf("sendRequests() failed to return the expected status."+ + "\nexpected: %s\nreceived: %s", AllFail, status) + } + + if rounds != nil { + t.Errorf("sendRequests() returned rounds on failure."+ + "\nexpected: %v\nreceived: %v", nil, rounds) + } + + if len(m.net.(*testNetworkManager).e2eMessages) != 0 { + t.Errorf("sendRequests() sent %d messages when sending should have failed.", + len(m.net.(*testNetworkManager).e2eMessages)) + } +} + +// Tests that Manager.sendRequests returns the correct status when some of the +// sends fail. +func TestManager_sendRequests_SendPartialSent(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 2, nil, nil, t) + expectedErr := fmt.Sprintf(sendRequestPartialErr, (len(g.Members)-1)/2, + len(g.Members)-1, "") + + _, status, err := m.sendRequests(g) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("sendRequests() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } + + if status != PartialSent { + t.Errorf("sendRequests() failed to return the expected status."+ + "\nexpected: %s\nreceived: %s", PartialSent, status) + } + + if len(m.net.(*testNetworkManager).e2eMessages) != (len(g.Members)-1)/2+1 { + t.Errorf("sendRequests() sent %d out of %d expected messages.", + len(m.net.(*testNetworkManager).e2eMessages), (len(g.Members)-1)/2+1) + } +} + +// Unit test of Manager.sendRequest. +func TestManager_sendRequest(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + expected := message.Send{ + Recipient: g.Members[0].ID, + Payload: []byte("request message"), + MessageType: message.GroupCreationRequest, + } + _, err := m.sendRequest(expected.Recipient, expected.Payload) + if err != nil { + t.Errorf("sendRequest() returned an error: %+v", err) + } + + received := m.net.(*testNetworkManager).GetE2eMsg(0) + + if !reflect.DeepEqual(expected, received) { + t.Errorf("sendRequest() did not send the correct message."+ + "\nexpected: %+v\nreceived: %+v", expected, received) + } +} + +// Error path: an error is returned when SendE2E fails +func TestManager_sendRequest_SendE2eError(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 1, nil, nil, t) + expectedErr := strings.SplitN(sendE2eErr, "%", 2)[0] + + _, err := m.sendRequest(id.NewIdFromString("memberID", id.User, t), nil) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("sendRequest() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Unit test of roundIdMap2List. +func Test_roundIdMap2List(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + + // Construct map and expected list + n := 100 + expected := make([]id.Round, n) + ridMap := make(map[id.Round]struct{}, n) + for i := 0; i < n; i++ { + expected[i] = id.Round(prng.Uint64()) + ridMap[expected[i]] = struct{}{} + } + + // Create list of IDs from map + ridList := roundIdMap2List(ridMap) + + // Sort expected and received slices to see if they match + sort.Slice(expected, func(i, j int) bool { return expected[i] < expected[j] }) + sort.Slice(ridList, func(i, j int) bool { return ridList[i] < ridList[j] }) + + if !reflect.DeepEqual(expected, ridList) { + t.Errorf("roundIdMap2List() failed to return the expected list."+ + "\nexpected: %v\nreceived: %v", expected, ridList) + } + +} diff --git a/groupChat/send_test.go b/groupChat/send_test.go new file mode 100644 index 0000000000000000000000000000000000000000..1db5cec791e7790bf519ab98030a088f423b076e --- /dev/null +++ b/groupChat/send_test.go @@ -0,0 +1,557 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "bytes" + "encoding/base64" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/storage" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/netTime" + "math/rand" + "strings" + "testing" + "time" +) + +// Unit test of Manager.Send. +func TestManager_Send(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + message := []byte("Group chat message.") + sender := m.gs.GetUser().DeepCopy() + + _, err := m.Send(g.ID, message) + if err != nil { + t.Errorf("Send() returned an error: %+v", err) + } + + // Get messages sent with or return an error if no messages were sent + var messages map[id.ID]format.Message + if len(m.net.(*testNetworkManager).messages) > 0 { + messages = m.net.(*testNetworkManager).GetMsgMap(0) + } else { + t.Error("No group cMix messages received.") + } + + timeNow := netTime.Now() + + // Loop through each message and make sure the recipient ID matches a member + // in the group and that each message can be decrypted and have the expected + // values + for rid, msg := range messages { + // Check if recipient ID is in member list + var foundMember group.Member + for _, mem := range g.Members { + if rid.Cmp(mem.ID) { + foundMember = mem + } + } + + // Error if the recipient ID is not found in the member list + if foundMember == (group.Member{}) { + t.Errorf("Failed to find ID %s in memorship list.", rid) + continue + } + + publicMsg, err := unmarshalPublicMsg(msg.GetContents()) + if err != nil { + t.Errorf("Failed to unmarshal publicMsg: %+v", err) + } + // Attempt to read the message + messageID, timestamp, senderID, readMsg, err := m.decryptMessage( + g, msg, publicMsg, timeNow) + if err != nil { + t.Errorf("Failed to read message for %s: %+v", rid.String(), err) + } + + internalMsg, _ := newInternalMsg(publicMsg.GetPayloadSize()) + internalMsg.SetTimestamp(timestamp) + internalMsg.SetSenderID(m.gs.GetUser().ID) + internalMsg.SetPayload(message) + expectedMsgID := group.NewMessageID(g.ID, internalMsg.Marshal()) + + if expectedMsgID != messageID { + t.Errorf("Message ID received for %s too different from expected."+ + "\nexpected: %s\nreceived: %s", &rid, expectedMsgID, messageID) + } + + if !timestamp.Round(5 * time.Second).Equal(timeNow.Round(5 * time.Second)) { + t.Errorf("Timestamp received for %s too different from expected."+ + "\nexpected: %s\nreceived: %s", &rid, timeNow, timestamp) + } + + if !senderID.Cmp(sender.ID) { + t.Errorf("Sender ID received for %s incorrect."+ + "\nexpected: %s\nreceived: %s", &rid, sender.ID, senderID) + } + + if !bytes.Equal(readMsg, message) { + t.Errorf("Message received for %s incorrect."+ + "\nexpected: %q\nreceived: %q", &rid, message, readMsg) + } + } +} + +// Error path: error is returned when the message is too large. +func TestManager_Send_CmixMessageError(t *testing.T) { + // Set up new test manager that will make SendManyCMIX error + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + expectedErr := strings.SplitN(newCmixMsgErr, "%", 2)[0] + + // Send message + _, err := m.Send(g.ID, make([]byte, 400)) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("Send() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: SendManyCMIX returns an error. +func TestManager_Send_SendManyCMIXError(t *testing.T) { + // Set up new test manager that will make SendManyCMIX error + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 1, nil, nil, t) + expectedErr := strings.SplitN(sendManyCmixErr, "%", 2)[0] + + // Send message + _, err := m.Send(g.ID, []byte("message")) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("Send() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } + + // If messages were added, then error + if len(m.net.(*testNetworkManager).messages) > 0 { + t.Error("Group cMix messages received when SendManyCMIX errors.") + } +} + +// Tests that Manager.createMessages generates the messages for the correct group. +func TestManager_createMessages(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + message := []byte("Test group message.") + sender := m.gs.GetUser() + messages, err := m.createMessages(g.ID, message) + if err != nil { + t.Errorf("createMessages() returned an error: %+v", err) + } + + recipients := append(g.Members[:2], g.Members[3:]...) + + i := 0 + for rid, msg := range messages { + for _, recipient := range recipients { + if !rid.Cmp(recipient.ID) { + continue + } + + publicMsg, err := unmarshalPublicMsg(msg.GetContents()) + if err != nil { + t.Errorf("Failed to unmarshal publicMsg: %+v", err) + } + + messageID, timestamp, testSender, testMessage, err := m.decryptMessage( + g, msg, publicMsg, netTime.Now()) + if err != nil { + t.Errorf("Failed to find member to read message %d: %+v", i, err) + } + + internalMsg, _ := newInternalMsg(publicMsg.GetPayloadSize()) + internalMsg.SetTimestamp(timestamp) + internalMsg.SetSenderID(m.gs.GetUser().ID) + internalMsg.SetPayload(message) + expectedMsgID := group.NewMessageID(g.ID, internalMsg.Marshal()) + + if messageID != expectedMsgID { + t.Errorf("Failed to read correct message ID for message %d."+ + "\nexpected: %s\nreceived: %s", i, expectedMsgID, messageID) + } + + if !sender.ID.Cmp(testSender) { + t.Errorf("Failed to read correct sender ID for message %d."+ + "\nexpected: %s\nreceived: %s", i, sender.ID, testSender) + } + + if !bytes.Equal(message, testMessage) { + t.Errorf("Failed to read correct message for message %d."+ + "\nexpected: %s\nreceived: %s", i, message, testMessage) + } + } + i++ + } +} + +// Error path: test that an error is returned when the group ID does not match a +// group in storage. +func TestManager_createMessages_InvalidGroupIdError(t *testing.T) { + expectedErr := strings.SplitN(newNoGroupErr, "%", 2)[0] + + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, _ := newTestManagerWithStore(prng, 10, 0, nil, nil, t) + + // Read message and make sure the error is expected + _, err := m.createMessages(id.NewIdFromString("invalidID", id.Group, t), nil) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("createMessages() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Tests that Manager.newMessage returns messages with correct data. +func TestGroup_newMessages(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + m, g := newTestManager(prng, t) + + message := []byte("Test group message.") + sender := m.gs.GetUser() + timestamp := netTime.Now() + messages, err := m.newMessages(g, message, timestamp) + if err != nil { + t.Errorf("newMessages() returned an error: %+v", err) + } + + recipients := append(g.Members[:2], g.Members[3:]...) + + i := 0 + for rid, msg := range messages { + for _, recipient := range recipients { + if !rid.Cmp(recipient.ID) { + continue + } + + publicMsg, err := unmarshalPublicMsg(msg.GetContents()) + if err != nil { + t.Errorf("Failed to unmarshal publicMsg: %+v", err) + } + + messageID, testTimestamp, testSender, testMessage, err := m.decryptMessage( + g, msg, publicMsg, netTime.Now()) + if err != nil { + t.Errorf("Failed to find member to read message %d.", i) + } + + internalMsg, _ := newInternalMsg(publicMsg.GetPayloadSize()) + internalMsg.SetTimestamp(timestamp) + internalMsg.SetSenderID(m.gs.GetUser().ID) + internalMsg.SetPayload(message) + expectedMsgID := group.NewMessageID(g.ID, internalMsg.Marshal()) + + if messageID != expectedMsgID { + t.Errorf("Failed to read correct message ID for message %d."+ + "\nexpected: %s\nreceived: %s", i, expectedMsgID, messageID) + } + + if !timestamp.Equal(testTimestamp) { + t.Errorf("Failed to read correct timeout for message %d."+ + "\nexpected: %s\nreceived: %s", i, timestamp, testTimestamp) + } + + if !sender.ID.Cmp(testSender) { + t.Errorf("Failed to read correct sender ID for message %d."+ + "\nexpected: %s\nreceived: %s", i, sender.ID, testSender) + } + + if !bytes.Equal(message, testMessage) { + t.Errorf("Failed to read correct message for message %d."+ + "\nexpected: %s\nreceived: %s", i, message, testMessage) + } + } + i++ + } +} + +// Error path: an error is returned when Manager.neCmixMsg returns an error. +func TestGroup_newMessages_NewCmixMsgError(t *testing.T) { + expectedErr := strings.SplitN(newCmixErr, "%", 2)[0] + prng := rand.New(rand.NewSource(42)) + m, g := newTestManager(prng, t) + + _, err := m.newMessages(g, make([]byte, 1000), netTime.Now()) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("newMessages() failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Tests that the message returned by newCmixMsg has all the expected parts. +func TestGroup_newCmixMsg(t *testing.T) { + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, g := newTestManager(prng, t) + + // Create test parameters + message := []byte("Test group message.") + mem := g.Members[3] + timeNow := netTime.Now() + + // Create cMix message + prng = rand.New(rand.NewSource(42)) + msg, err := m.newCmixMsg(g, message, timeNow, mem, prng) + if err != nil { + t.Errorf("newCmixMsg() returned an error: %+v", err) + } + + // Create expected salt + prng = rand.New(rand.NewSource(42)) + var salt [group.SaltLen]byte + prng.Read(salt[:]) + + // Create expected key + key, _ := group.NewKdfKey(g.Key, group.ComputeEpoch(timeNow), salt) + + // Create expected messages + cmixMsg := format.NewMessage(m.store.Cmix().GetGroup().GetP().ByteLen()) + publicMsg, _ := newPublicMsg(cmixMsg.ContentsSize()) + internalMsg, _ := newInternalMsg(publicMsg.GetPayloadSize()) + internalMsg.SetTimestamp(timeNow) + internalMsg.SetSenderID(m.gs.GetUser().ID) + internalMsg.SetPayload(message) + payload := internalMsg.Marshal() + + // Check if key fingerprint is correct + expectedFp := group.NewKeyFingerprint(g.Key, salt, mem.ID) + if expectedFp != msg.GetKeyFP() { + t.Errorf("newCmixMsg() returned message with wrong key fingerprint."+ + "\nexpected: %s\nreceived: %s", expectedFp, msg.GetKeyFP()) + } + + // Check if key MAC is correct + encryptedPayload := group.Encrypt(key, expectedFp, payload) + expectedMAC := group.NewMAC(key, encryptedPayload, g.DhKeys[*mem.ID]) + if !bytes.Equal(expectedMAC, msg.GetMac()) { + t.Errorf("newCmixMsg() returned message with wrong MAC."+ + "\nexpected: %+v\nreceived: %+v", expectedMAC, msg.GetMac()) + } + + // Attempt to unmarshal public group message + publicMsg, err = unmarshalPublicMsg(msg.GetContents()) + if err != nil { + t.Errorf("Failed to unmarshal cMix message contents: %+v", err) + } + + // Attempt to decrypt payload + decryptedPayload := group.Decrypt(key, expectedFp, publicMsg.GetPayload()) + internalMsg, err = unmarshalInternalMsg(decryptedPayload) + if err != nil { + t.Errorf("Failed to unmarshal decrypted payload contents: %+v", err) + } + + // Check for expected values in internal message + if !internalMsg.GetTimestamp().Equal(timeNow) { + t.Errorf("Internal message has wrong timestamp."+ + "\nexpected: %s\nreceived: %s", timeNow, internalMsg.GetTimestamp()) + } + sid, err := internalMsg.GetSenderID() + if err != nil { + t.Fatalf("Failed to get sender ID from internal message: %+v", err) + } + if !sid.Cmp(m.gs.GetUser().ID) { + t.Errorf("Internal message has wrong sender ID."+ + "\nexpected: %s\nreceived: %s", m.gs.GetUser().ID, sid) + } + if !bytes.Equal(internalMsg.GetPayload(), message) { + t.Errorf("Internal message has wrong payload."+ + "\nexpected: %s\nreceived: %s", message, internalMsg.GetPayload()) + } +} + +// Error path: reader returns an error. +func TestGroup_newCmixMsg_SaltReaderError(t *testing.T) { + expectedErr := strings.SplitN(saltReadErr, "%", 2)[0] + m := &Manager{store: storage.InitTestingSession(t)} + + _, err := m.newCmixMsg(gs.Group{}, []byte{}, time.Time{}, group.Member{}, strings.NewReader("")) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("newCmixMsg() failed to return the expected error"+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: size of message is too large for the internalMsg. +func TestGroup_newCmixMsg_InternalMsgSizeError(t *testing.T) { + expectedErr := strings.SplitN(messageLenErr, "%", 2)[0] + + // Create new test Manager and Group + prng := rand.New(rand.NewSource(42)) + m, g := newTestManager(prng, t) + + // Create test parameters + message := make([]byte, 341) + mem := group.Member{ID: id.NewIdFromString("memberID", id.User, t)} + + // Create cMix message + prng = rand.New(rand.NewSource(42)) + _, err := m.newCmixMsg(g, message, netTime.Now(), mem, prng) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("newCmixMsg() failed to return the expected error"+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: payload size too small to fit publicMsg. +func Test_newMessageParts_PublicMsgSizeErr(t *testing.T) { + expectedErr := strings.SplitN(newPublicMsgErr, "%", 2)[0] + + _, _, err := newMessageParts(publicMinLen - 1) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("newMessageParts() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: payload size too small to fit internalMsg. +func Test_newMessageParts_InternalMsgSizeErr(t *testing.T) { + expectedErr := strings.SplitN(newInternalMsgErr, "%", 2)[0] + + _, _, err := newMessageParts(publicMinLen) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("newMessageParts() did not return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Tests the consistency of newSalt. +func Test_newSalt_Consistency(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + expectedSalts := []string{ + "U4x/lrFkvxuXu59LtHLon1sUhPJSCcnZND6SugndnVI=", + "39ebTXZCm2F6DJ+fDTulWwzA1hRMiIU1hBrL4HCbB1g=", + "CD9h03W8ArQd9PkZKeGP2p5vguVOdI6B555LvW/jTNw=", + "uoQ+6NY+jE/+HOvqVG2PrBPdGqwEzi6ih3xVec+ix44=", + "GwuvrogbgqdREIpC7TyQPKpDRlp4YgYWl4rtDOPGxPM=", + "rnvD4ElbVxL+/b4MECiH4QDazS2IX2kstgfaAKEcHHA=", + "ceeWotwtwlpbdLLhKXBeJz8FySMmgo4rBW44F2WOEGE=", + "SYlH/fNEQQ7UwRYCP6jjV2tv7Sf/iXS6wMr9mtBWkrE=", + "NhnnOJZN/ceejVNDc2Yc/WbXT+weG4lJGrcjbkt1IWI=", + } + + for i, expected := range expectedSalts { + salt, err := newSalt(prng) + if err != nil { + t.Errorf("newSalt() returned an error (%d): %+v", i, err) + } + + saltString := base64.StdEncoding.EncodeToString(salt[:]) + + if expected != saltString { + t.Errorf("newSalt() did not return the expected salt (%d)."+ + "\nexpected: %s\nreceived: %s", i, expected, saltString) + } + + // fmt.Printf("\"%s\",\n", saltString) + } +} + +// Error path: reader returns an error. +func Test_newSalt_ReadError(t *testing.T) { + expectedErr := strings.SplitN(saltReadErr, "%", 2)[0] + + _, err := newSalt(strings.NewReader("")) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("newSalt() failed to return the expected error"+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Error path: reader fails to return enough bytes. +func Test_newSalt_ReadLengthError(t *testing.T) { + expectedErr := strings.SplitN(saltReadLengthErr, "%", 2)[0] + + _, err := newSalt(strings.NewReader("A")) + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("newSalt() failed to return the expected error"+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Tests that the marshaled internalMsg can be unmarshaled and has all the +// original values. +func Test_setInternalPayload(t *testing.T) { + internalMsg, err := newInternalMsg(internalMinLen * 2) + if err != nil { + t.Errorf("Failed to create a new internalMsg: %+v", err) + } + + timestamp := netTime.Now() + sender := id.NewIdFromString("sender ID", id.User, t) + message := []byte("This is an internal message.") + + payload := setInternalPayload(internalMsg, timestamp, sender, message) + if err != nil { + t.Errorf("setInternalPayload() returned an error: %+v", err) + } + + // Attempt to unmarshal and check all values + unmarshalled, err := unmarshalInternalMsg(payload) + if err != nil { + t.Errorf("Failed to unmarshal internalMsg: %+v", err) + } + + if !timestamp.Equal(unmarshalled.GetTimestamp()) { + t.Errorf("Timestamp does not match original.\nexpected: %s\nreceived: %s", + timestamp, unmarshalled.GetTimestamp()) + } + + testSender, err := unmarshalled.GetSenderID() + if err != nil { + t.Errorf("Failed to get sender ID: %+v", err) + } + if !sender.Cmp(testSender) { + t.Errorf("Sender ID does not match original.\nexpected: %s\nreceived: %s", + sender, testSender) + } + + if !bytes.Equal(message, unmarshalled.GetPayload()) { + t.Errorf("Payload does not match original.\nexpected: %v\nreceived: %v", + message, unmarshalled.GetPayload()) + } +} + +// Tests that the marshaled publicMsg can be unmarshaled and has all the +// original values. +func Test_setPublicPayload(t *testing.T) { + prng := rand.New(rand.NewSource(42)) + publicMsg, err := newPublicMsg(publicMinLen * 2) + if err != nil { + t.Errorf("Failed to create a new publicMsg: %+v", err) + } + + var salt [group.SaltLen]byte + prng.Read(salt[:]) + encryptedPayload := make([]byte, publicMsg.GetPayloadSize()) + copy(encryptedPayload, "This is an internal message.") + + payload := setPublicPayload(publicMsg, salt, encryptedPayload) + if err != nil { + t.Errorf("setPublicPayload() returned an error: %+v", err) + } + + // Attempt to unmarshal and check all values + unmarshalled, err := unmarshalPublicMsg(payload) + if err != nil { + t.Errorf("Failed to unmarshal publicMsg: %+v", err) + } + + if salt != unmarshalled.GetSalt() { + t.Errorf("Salt does not match original.\nexpected: %v\nreceived: %v", + salt, unmarshalled.GetSalt()) + } + + if !bytes.Equal(encryptedPayload, unmarshalled.GetPayload()) { + t.Errorf("Payload does not match original.\nexpected: %v\nreceived: %v", + encryptedPayload, unmarshalled.GetPayload()) + } +} diff --git a/groupChat/utils_test.go b/groupChat/utils_test.go new file mode 100644 index 0000000000000000000000000000000000000000..eac797af004142994c37598337b4b98d79299b75 --- /dev/null +++ b/groupChat/utils_test.go @@ -0,0 +1,334 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package groupChat + +import ( + "encoding/base64" + "github.com/pkg/errors" + gs "gitlab.com/elixxir/client/groupChat/groupStore" + "gitlab.com/elixxir/client/interfaces" + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/interfaces/params" + "gitlab.com/elixxir/client/network/gateway" + "gitlab.com/elixxir/client/stoppable" + "gitlab.com/elixxir/client/storage" + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/client/switchboard" + "gitlab.com/elixxir/comms/network" + "gitlab.com/elixxir/crypto/contact" + "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/e2e" + "gitlab.com/elixxir/crypto/fastRNG" + "gitlab.com/elixxir/crypto/group" + "gitlab.com/elixxir/ekv" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/xx_network/comms/connect" + "gitlab.com/xx_network/crypto/csprng" + "gitlab.com/xx_network/crypto/large" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + "gitlab.com/xx_network/primitives/ndf" + "math/rand" + "sync" + "testing" +) + +// newTestManager creates a new Manager for testing. +func newTestManager(rng *rand.Rand, t *testing.T) (*Manager, gs.Group) { + store := storage.InitTestingSession(t) + user := group.Member{ + ID: store.GetUser().ReceptionID, + DhKey: store.GetUser().E2eDhPublicKey, + } + + g := newTestGroupWithUser(store.E2e().GetGroup(), user.ID, user.DhKey, + store.GetUser().E2eDhPrivateKey, rng, t) + gStore, err := gs.NewStore(versioned.NewKV(make(ekv.Memstore)), user) + if err != nil { + t.Fatalf("Failed to create new group store: %+v", err) + } + m := &Manager{ + store: store, + rng: fastRNG.NewStreamGenerator(1000, 10, csprng.NewSystemRNG), + gs: gStore, + } + return m, g +} + +// newTestManager creates a new Manager that has groups stored for testing. One +// of the groups in the list is also returned. +func newTestManagerWithStore(rng *rand.Rand, numGroups int, sendErr int, + requestFunc RequestCallback, receiveFunc ReceiveCallback, + t *testing.T) (*Manager, gs.Group) { + + store := storage.InitTestingSession(t) + + user := group.Member{ + ID: store.GetUser().ReceptionID, + DhKey: store.GetUser().E2eDhPublicKey, + } + + gStore, err := gs.NewStore(versioned.NewKV(make(ekv.Memstore)), user) + if err != nil { + t.Fatalf("Failed to create new group store: %+v", err) + } + + var g gs.Group + for i := 0; i < numGroups; i++ { + g = newTestGroupWithUser(store.E2e().GetGroup(), user.ID, user.DhKey, + store.GetUser().E2eDhPrivateKey, rng, t) + if err = gStore.Add(g); err != nil { + t.Fatalf("Failed to add group %d to group store: %+v", i, err) + } + } + + m := &Manager{ + store: store, + swb: switchboard.New(), + net: newTestNetworkManager(sendErr, t), + rng: fastRNG.NewStreamGenerator(1000, 10, csprng.NewSystemRNG), + gs: gStore, + requestFunc: requestFunc, + receiveFunc: receiveFunc, + } + return m, g +} + +// getMembership returns a Membership with random members for testing. +func getMembership(size int, uid *id.ID, pubKey *cyclic.Int, grp *cyclic.Group, prng *rand.Rand, t *testing.T) group.Membership { + contacts := make([]contact.Contact, size) + for i := range contacts { + randId, _ := id.NewRandomID(prng, id.User) + contacts[i] = contact.Contact{ + ID: randId, + DhPubKey: grp.NewInt(int64(prng.Int31() + 1)), + } + } + + contacts[2].ID = uid + contacts[2].DhPubKey = pubKey + + membership, err := group.NewMembership(contacts[0], contacts[1:]...) + if err != nil { + t.Errorf("Failed to create new membership: %+v", err) + } + + return membership +} + +// newTestGroup generates a new group with random values for testing. +func newTestGroup(grp *cyclic.Group, privKey *cyclic.Int, rng *rand.Rand, t *testing.T) gs.Group { + // Generate name from base 64 encoded random data + nameBytes := make([]byte, 16) + rng.Read(nameBytes) + name := []byte(base64.StdEncoding.EncodeToString(nameBytes)) + + // Generate the message from base 64 encoded random data + msgBytes := make([]byte, 128) + rng.Read(msgBytes) + msg := []byte(base64.StdEncoding.EncodeToString(msgBytes)) + + membership := getMembership(10, id.NewIdFromString("userID", id.User, t), + randCycInt(rng), grp, rng, t) + + dkl := gs.GenerateDhKeyList(id.NewIdFromString("userID", id.User, t), privKey, membership, grp) + + idPreimage, err := group.NewIdPreimage(rng) + if err != nil { + t.Fatalf("Failed to generate new group ID preimage: %+v", err) + } + + keyPreimage, err := group.NewKeyPreimage(rng) + if err != nil { + t.Fatalf("Failed to generate new group key preimage: %+v", err) + } + + groupID := group.NewID(idPreimage, membership) + groupKey := group.NewKey(keyPreimage, membership) + + return gs.NewGroup(name, groupID, groupKey, idPreimage, keyPreimage, msg, + membership, dkl) +} + +// newTestGroup generates a new group with random values for testing. +func newTestGroupWithUser(grp *cyclic.Group, uid *id.ID, pubKey, + privKey *cyclic.Int, rng *rand.Rand, t *testing.T) gs.Group { + // Generate name from base 64 encoded random data + nameBytes := make([]byte, 16) + rng.Read(nameBytes) + name := []byte(base64.StdEncoding.EncodeToString(nameBytes)) + + // Generate the message from base 64 encoded random data + msgBytes := make([]byte, 128) + rng.Read(msgBytes) + msg := []byte(base64.StdEncoding.EncodeToString(msgBytes)) + + membership := getMembership(10, uid, pubKey, grp, rng, t) + + dkl := gs.GenerateDhKeyList(uid, privKey, membership, grp) + + idPreimage, err := group.NewIdPreimage(rng) + if err != nil { + t.Fatalf("Failed to generate new group ID preimage: %+v", err) + } + + keyPreimage, err := group.NewKeyPreimage(rng) + if err != nil { + t.Fatalf("Failed to generate new group key preimage: %+v", err) + } + + groupID := group.NewID(idPreimage, membership) + groupKey := group.NewKey(keyPreimage, membership) + + return gs.NewGroup(name, groupID, groupKey, idPreimage, keyPreimage, msg, + membership, dkl) +} + +// randCycInt returns a random cyclic int. +func randCycInt(rng *rand.Rand) *cyclic.Int { + return getGroup().NewInt(int64(rng.Int31() + 1)) +} + +func getGroup() *cyclic.Group { + return cyclic.NewGroup( + large.NewIntFromString(getNDF().E2E.Prime, 16), + large.NewIntFromString(getNDF().E2E.Generator, 16)) +} + +func newTestNetworkManager(sendErr int, t *testing.T) interfaces.NetworkManager { + instanceComms := &connect.ProtoComms{ + Manager: connect.NewManagerTesting(t), + } + + thisInstance, err := network.NewInstanceTesting(instanceComms, getNDF(), + getNDF(), nil, nil, t) + if err != nil { + t.Fatalf("Failed to create new test instance: %v", err) + } + + return &testNetworkManager{ + instance: thisInstance, + messages: []map[id.ID]format.Message{}, + sendErr: sendErr, + } +} + +// testNetworkManager is a test implementation of NetworkManager interface. +type testNetworkManager struct { + instance *network.Instance + messages []map[id.ID]format.Message + e2eMessages []message.Send + errSkip int + sendErr int + sync.RWMutex +} + +func (tnm *testNetworkManager) GetMsgMap(i int) map[id.ID]format.Message { + tnm.RLock() + defer tnm.RUnlock() + return tnm.messages[i] +} + +func (tnm *testNetworkManager) GetE2eMsg(i int) message.Send { + tnm.RLock() + defer tnm.RUnlock() + return tnm.e2eMessages[i] +} + +func (tnm *testNetworkManager) SendE2E(msg message.Send, _ params.E2E, _ *stoppable.Single) ([]id.Round, e2e.MessageID, error) { + tnm.Lock() + defer tnm.Unlock() + + tnm.errSkip++ + if tnm.sendErr == 1 { + return nil, e2e.MessageID{}, errors.New("SendE2E error") + } else if tnm.sendErr == 2 && tnm.errSkip%2 == 0 { + return nil, e2e.MessageID{}, errors.New("SendE2E error") + } + + tnm.e2eMessages = append(tnm.e2eMessages, msg) + + return []id.Round{0, 1, 2, 3}, e2e.MessageID{}, nil +} + +func (tnm *testNetworkManager) SendUnsafe(message.Send, params.Unsafe) ([]id.Round, error) { + return []id.Round{}, nil +} + +func (tnm *testNetworkManager) SendCMIX(format.Message, *id.ID, params.CMIX) (id.Round, ephemeral.Id, error) { + return 0, ephemeral.Id{}, nil +} + +func (tnm *testNetworkManager) SendManyCMIX(messages map[id.ID]format.Message, _ params.CMIX) (id.Round, []ephemeral.Id, error) { + if tnm.sendErr == 1 { + return 0, nil, errors.New("SendManyCMIX error") + } + + tnm.Lock() + defer tnm.Unlock() + + tnm.messages = append(tnm.messages, messages) + + return 0, nil, nil +} + +func (tnm *testNetworkManager) GetInstance() *network.Instance { return tnm.instance } +func (tnm *testNetworkManager) GetHealthTracker() interfaces.HealthTracker { return nil } +func (tnm *testNetworkManager) Follow(interfaces.ClientErrorReport) (stoppable.Stoppable, error) { + return nil, nil +} +func (tnm *testNetworkManager) CheckGarbledMessages() {} +func (tnm *testNetworkManager) InProgressRegistrations() int { return 0 } +func (tnm *testNetworkManager) GetSender() *gateway.Sender { return nil } +func (tnm *testNetworkManager) GetAddressSize() uint8 { return 0 } +func (tnm *testNetworkManager) RegisterAddressSizeNotification(string) (chan uint8, error) { + return nil, nil +} +func (tnm *testNetworkManager) UnregisterAddressSizeNotification(string) {} + +func getNDF() *ndf.NetworkDefinition { + return &ndf.NetworkDefinition{ + E2E: ndf.Group{ + Prime: "E2EE983D031DC1DB6F1A7A67DF0E9A8E5561DB8E8D49413394C049B7A" + + "8ACCEDC298708F121951D9CF920EC5D146727AA4AE535B0922C688B55B3D" + + "D2AEDF6C01C94764DAB937935AA83BE36E67760713AB44A6337C20E78615" + + "75E745D31F8B9E9AD8412118C62A3E2E29DF46B0864D0C951C394A5CBBDC" + + "6ADC718DD2A3E041023DBB5AB23EBB4742DE9C1687B5B34FA48C3521632C" + + "4A530E8FFB1BC51DADDF453B0B2717C2BC6669ED76B4BDD5C9FF558E88F2" + + "6E5785302BEDBCA23EAC5ACE92096EE8A60642FB61E8F3D24990B8CB12EE" + + "448EEF78E184C7242DD161C7738F32BF29A841698978825B4111B4BC3E1E" + + "198455095958333D776D8B2BEEED3A1A1A221A6E37E664A64B83981C46FF" + + "DDC1A45E3D5211AAF8BFBC072768C4F50D7D7803D2D4F278DE8014A47323" + + "631D7E064DE81C0C6BFA43EF0E6998860F1390B5D3FEACAF1696015CB79C" + + "3F9C2D93D961120CD0E5F12CBB687EAB045241F96789C38E89D796138E63" + + "19BE62E35D87B1048CA28BE389B575E994DCA755471584A09EC723742DC3" + + "5873847AEF49F66E43873", + Generator: "2", + }, + CMIX: ndf.Group{ + Prime: "9DB6FB5951B66BB6FE1E140F1D2CE5502374161FD6538DF1648218642" + + "F0B5C48C8F7A41AADFA187324B87674FA1822B00F1ECF8136943D7C55757" + + "264E5A1A44FFE012E9936E00C1D3E9310B01C7D179805D3058B2A9F4BB6F" + + "9716BFE6117C6B5B3CC4D9BE341104AD4A80AD6C94E005F4B993E14F091E" + + "B51743BF33050C38DE235567E1B34C3D6A5C0CEAA1A0F368213C3D19843D" + + "0B4B09DCB9FC72D39C8DE41F1BF14D4BB4563CA28371621CAD3324B6A2D3" + + "92145BEBFAC748805236F5CA2FE92B871CD8F9C36D3292B5509CA8CAA77A" + + "2ADFC7BFD77DDA6F71125A7456FEA153E433256A2261C6A06ED3693797E7" + + "995FAD5AABBCFBE3EDA2741E375404AE25B", + Generator: "5C7FF6B06F8F143FE8288433493E4769C4D988ACE5BE25A0E2480" + + "9670716C613D7B0CEE6932F8FAA7C44D2CB24523DA53FBE4F6EC3595892D" + + "1AA58C4328A06C46A15662E7EAA703A1DECF8BBB2D05DBE2EB956C142A33" + + "8661D10461C0D135472085057F3494309FFA73C611F78B32ADBB5740C361" + + "C9F35BE90997DB2014E2EF5AA61782F52ABEB8BD6432C4DD097BC5423B28" + + "5DAFB60DC364E8161F4A2A35ACA3A10B1C4D203CC76A470A33AFDCBDD929" + + "59859ABD8B56E1725252D78EAC66E71BA9AE3F1DD2487199874393CD4D83" + + "2186800654760E1E34C09E4D155179F9EC0DC4473F996BDCE6EED1CABED8" + + "B6F116F7AD9CF505DF0F998E34AB27514B0FFE7", + }, + } +} diff --git a/interfaces/IsRunning.go b/interfaces/IsRunning.go deleted file mode 100644 index 5198434a8391a1de5c704f392f641fbaea50ae93..0000000000000000000000000000000000000000 --- a/interfaces/IsRunning.go +++ /dev/null @@ -1,8 +0,0 @@ -package interfaces - -// this interface is used to allow the follower to to be stopped later if it -// fails - -type Running interface { - IsRunning() bool -} diff --git a/interfaces/healthTracker.go b/interfaces/healthTracker.go index 39441984ee0088b9e82e33e7b7a11ab689288f00..0d746d50f73bc1215201c413e5d6d83e54bbbb55 100644 --- a/interfaces/healthTracker.go +++ b/interfaces/healthTracker.go @@ -8,8 +8,10 @@ package interfaces type HealthTracker interface { - AddChannel(chan bool) - AddFunc(f func(bool)) + AddChannel(chan bool) uint64 + RemoveChannel(uint64) + AddFunc(f func(bool)) uint64 + RemoveFunc(uint64) IsHealthy() bool WasHealthy() bool } diff --git a/interfaces/message/receiveMessage.go b/interfaces/message/receiveMessage.go index fad6fb750ccc21cc9f03391056a8559a53cd5159..d11f8880865e8c6db95086c054f554126835b5c2 100644 --- a/interfaces/message/receiveMessage.go +++ b/interfaces/message/receiveMessage.go @@ -15,12 +15,14 @@ import ( ) type Receive struct { - ID e2e.MessageID - Payload []byte - MessageType Type - Sender *id.ID - RecipientID *id.ID - EphemeralID ephemeral.Id - Timestamp time.Time - Encryption EncryptionType + ID e2e.MessageID + Payload []byte + MessageType Type + Sender *id.ID + RecipientID *id.ID + EphemeralID ephemeral.Id + RoundId id.Round + RoundTimestamp time.Time + Timestamp time.Time // Message timestamp of when the user sent + Encryption EncryptionType } diff --git a/interfaces/message/type.go b/interfaces/message/type.go index 71a1c72ff431ab1c6164856fe892e72af8c21a68..5c8012fea55a6f7c6afcc953ef37dbf0daa7b6e0 100644 --- a/interfaces/message/type.go +++ b/interfaces/message/type.go @@ -49,4 +49,8 @@ const ( KeyExchangeTrigger = 30 // Rekey confirmation message. Sent by partner to confirm completion of a rekey KeyExchangeConfirm = 31 + + /* Group chat message types */ + // A group chat request message sent to all members in a group. + GroupCreationRequest = 40 ) diff --git a/interfaces/networkManager.go b/interfaces/networkManager.go index 1daa7d38104731870459b1323c8d918401a398fd..710700144bc2641b0f3caf87c00b65879cc0cb1a 100644 --- a/interfaces/networkManager.go +++ b/interfaces/networkManager.go @@ -20,16 +20,31 @@ import ( ) type NetworkManager interface { - SendE2E(m message.Send, p params.E2E) ([]id.Round, e2e.MessageID, error) + // The stoppable can be nil. + SendE2E(m message.Send, p params.E2E, stop *stoppable.Single) ([]id.Round, e2e.MessageID, error) SendUnsafe(m message.Send, p params.Unsafe) ([]id.Round, error) SendCMIX(message format.Message, recipient *id.ID, p params.CMIX) (id.Round, ephemeral.Id, error) + SendManyCMIX(messages map[id.ID]format.Message, p params.CMIX) (id.Round, []ephemeral.Id, error) GetInstance() *network.Instance GetHealthTracker() HealthTracker GetSender() *gateway.Sender Follow(report ClientErrorReport) (stoppable.Stoppable, error) CheckGarbledMessages() InProgressRegistrations() int + + // GetAddressSize returns the current address size of IDs. Blocks until an + // address size is known. + GetAddressSize() uint8 + + // RegisterAddressSizeNotification returns a channel that will trigger for + // every address space size update. The provided tag is the unique ID for + // the channel. Returns an error if the tag is already used. + RegisterAddressSizeNotification(tag string) (chan uint8, error) + + // UnregisterAddressSizeNotification stops broadcasting address space size + // updates on the channel with the specified tag. + UnregisterAddressSizeNotification(tag string) } //for use in key exchange which needs to be callable inside of network -type SendE2E func(m message.Send, p params.E2E) ([]id.Round, e2e.MessageID, error) +type SendE2E func(m message.Send, p params.E2E, stop *stoppable.Single) ([]id.Round, e2e.MessageID, error) diff --git a/interfaces/params/message.go b/interfaces/params/message.go index acecde4ef9861ab722ff05c7e645272f20d21694..fbf9779829b939145cf7bc1277fa79b5617b826a 100644 --- a/interfaces/params/message.go +++ b/interfaces/params/message.go @@ -16,8 +16,6 @@ type Messages struct { MessageReceptionWorkerPoolSize uint MaxChecksGarbledMessage uint GarbledMessageWait time.Duration - // Use proxied (rather than direct) message sending - ProxySending bool } func GetDefaultMessage() Messages { @@ -26,6 +24,5 @@ func GetDefaultMessage() Messages { MessageReceptionWorkerPoolSize: 4, MaxChecksGarbledMessage: 10, GarbledMessageWait: 15 * time.Minute, - ProxySending: false, } } diff --git a/interfaces/params/rounds.go b/interfaces/params/rounds.go index 07c4c3c25d83f3c115263bef0269ab1c9226c7ee..3e39ad47827e5f5f6f9dc526fcfffa200f5cf5a8 100644 --- a/interfaces/params/rounds.go +++ b/interfaces/params/rounds.go @@ -31,6 +31,14 @@ type Rounds struct { // Maximum number of times a historical round lookup will be attempted MaxHistoricalRoundsRetries uint + + // Interval between checking for rounds in UncheckedRoundStore + // due for a message retrieval retry + UncheckRoundPeriod time.Duration + + // Toggles if message pickup retrying mechanism if forced + // by intentionally not looking up messages + ForceMessagePickupRetry bool } func GetDefaultRounds() Rounds { @@ -43,5 +51,7 @@ func GetDefaultRounds() Rounds { LookupRoundsBufferLen: 2000, ForceHistoricalRounds: false, MaxHistoricalRoundsRetries: 3, + UncheckRoundPeriod: 20 * time.Second, + ForceMessagePickupRetry: false, } } diff --git a/keyExchange/confirm.go b/keyExchange/confirm.go index 01c43c5d42dd602fb314815d993b20ab7d16e401..ce1bec2baf28d92502c494ec94e659ba1e7c48c1 100644 --- a/keyExchange/confirm.go +++ b/keyExchange/confirm.go @@ -23,6 +23,7 @@ func startConfirm(sess *storage.Session, c chan message.Receive, select { case <-stop.Quit(): cleanup() + stop.ToStopped() return case confirmation := <-c: handleConfirm(sess, confirmation) diff --git a/keyExchange/rekey.go b/keyExchange/rekey.go index f2aab4c42ebedebd258e83168461b719821e1ef2..f4caacd2586bd7a8c0c8c296ba7f8ae19377db4f 100644 --- a/keyExchange/rekey.go +++ b/keyExchange/rekey.go @@ -15,6 +15,7 @@ import ( "gitlab.com/elixxir/client/interfaces/message" "gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/interfaces/utility" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/client/storage" "gitlab.com/elixxir/client/storage/e2e" "gitlab.com/elixxir/comms/network" @@ -25,10 +26,11 @@ import ( ) func CheckKeyExchanges(instance *network.Instance, sendE2E interfaces.SendE2E, - sess *storage.Session, manager *e2e.Manager, sendTimeout time.Duration) { + sess *storage.Session, manager *e2e.Manager, sendTimeout time.Duration, + stop *stoppable.Single) { sessions := manager.TriggerNegotiations() for _, session := range sessions { - go trigger(instance, sendE2E, sess, manager, session, sendTimeout) + go trigger(instance, sendE2E, sess, manager, session, sendTimeout, stop) } } @@ -38,7 +40,7 @@ func CheckKeyExchanges(instance *network.Instance, sendE2E interfaces.SendE2E, // session while the latter on an extant session func trigger(instance *network.Instance, sendE2E interfaces.SendE2E, sess *storage.Session, manager *e2e.Manager, session *e2e.Session, - sendTimeout time.Duration) { + sendTimeout time.Duration, stop *stoppable.Single) { var negotiatingSession *e2e.Session jww.INFO.Printf("Negotation triggered for session %s with "+ "status: %s", session, session.NegotiationStatus()) @@ -61,7 +63,7 @@ func trigger(instance *network.Instance, sendE2E interfaces.SendE2E, } // send the rekey notification to the partner - err := negotiate(instance, sendE2E, sess, negotiatingSession, sendTimeout) + err := negotiate(instance, sendE2E, sess, negotiatingSession, sendTimeout, stop) // if sending the negotiation fails, revert the state of the session to // unconfirmed so it will be triggered in the future if err != nil { @@ -71,8 +73,8 @@ func trigger(instance *network.Instance, sendE2E interfaces.SendE2E, } func negotiate(instance *network.Instance, sendE2E interfaces.SendE2E, - sess *storage.Session, session *e2e.Session, - sendTimeout time.Duration) error { + sess *storage.Session, session *e2e.Session, sendTimeout time.Duration, + stop *stoppable.Single) error { e2eStore := sess.E2e() //generate public key @@ -102,7 +104,7 @@ func negotiate(instance *network.Instance, sendE2E interfaces.SendE2E, e2eParams := params.GetDefaultE2E() e2eParams.Type = params.KeyExchange - rounds, _, err := sendE2E(m, e2eParams) + rounds, _, err := sendE2E(m, e2eParams, stop) // If the send fails, returns the error so it can be handled. The caller // should ensure the calling session is in a state where the Rekey will // be triggered next time a key is used diff --git a/keyExchange/trigger.go b/keyExchange/trigger.go index e22dd76f0b30974254de9fb6bd356e8e3f69d88e..c88d068fe8d1463ea4c2b5408dd9b18c4030e488 100644 --- a/keyExchange/trigger.go +++ b/keyExchange/trigger.go @@ -32,14 +32,15 @@ const ( func startTrigger(sess *storage.Session, net interfaces.NetworkManager, c chan message.Receive, stop *stoppable.Single, params params.Rekey, cleanup func()) { - for true { + for { select { case <-stop.Quit(): cleanup() + stop.ToStopped() return case request := <-c: go func() { - err := handleTrigger(sess, net, request, params) + err := handleTrigger(sess, net, request, params, stop) if err != nil { jww.ERROR.Printf(errFailed, err) } @@ -49,7 +50,7 @@ func startTrigger(sess *storage.Session, net interfaces.NetworkManager, } func handleTrigger(sess *storage.Session, net interfaces.NetworkManager, - request message.Receive, param params.Rekey) error { + request message.Receive, param params.Rekey, stop *stoppable.Single) error { //ensure the message was encrypted properly if request.Encryption != message.E2E { errMsg := fmt.Sprintf(errBadTrigger, request.Sender) @@ -126,7 +127,10 @@ func handleTrigger(sess *storage.Session, net interfaces.NetworkManager, // send fails sess.GetCriticalMessages().AddProcessing(m, e2eParams) - rounds, _, err := net.SendE2E(m, e2eParams) + rounds, _, err := net.SendE2E(m, e2eParams, stop) + if err != nil { + return err + } //Register the event for all rounds sendResults := make(chan ds.EventReturn, len(rounds)) diff --git a/keyExchange/trigger_test.go b/keyExchange/trigger_test.go index 5dce2d6a7c50c8e774c3ecd88dd00f5e3db1cacc..d01f9101396d573c8c97892aa3256ede2ba91d49 100644 --- a/keyExchange/trigger_test.go +++ b/keyExchange/trigger_test.go @@ -10,6 +10,7 @@ package keyExchange import ( "gitlab.com/elixxir/client/interfaces/message" "gitlab.com/elixxir/client/interfaces/params" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/client/storage/e2e" dh "gitlab.com/elixxir/crypto/diffieHellman" "gitlab.com/xx_network/crypto/csprng" @@ -66,8 +67,9 @@ func TestHandleTrigger(t *testing.T) { // Handle the trigger and check for an error rekeyParams := params.GetDefaultRekey() + stop := stoppable.NewSingle("stoppable") rekeyParams.RoundTimeout = 0 * time.Second - err = handleTrigger(aliceSession, aliceManager, receiveMsg, rekeyParams) + err = handleTrigger(aliceSession, aliceManager, receiveMsg, rekeyParams, stop) if err != nil { t.Errorf("Handle trigger error: %v", err) } diff --git a/keyExchange/utils_test.go b/keyExchange/utils_test.go index 58c21e7fef3e722ce5377217e36edc72248ffb31..876a16576bfd95871fca6e099fb77caff25f6c02 100644 --- a/keyExchange/utils_test.go +++ b/keyExchange/utils_test.go @@ -66,7 +66,7 @@ func (t *testNetworkManagerGeneric) CheckGarbledMessages() { return } -func (t *testNetworkManagerGeneric) SendE2E(m message.Send, p params.E2E) ( +func (t *testNetworkManagerGeneric) SendE2E(message.Send, params.E2E, *stoppable.Single) ( []id.Round, cE2e.MessageID, error) { rounds := []id.Round{id.Round(0), id.Round(1), id.Round(2)} return rounds, cE2e.MessageID{}, nil @@ -84,6 +84,10 @@ func (t *testNetworkManagerGeneric) SendCMIX(message format.Message, rid *id.ID, } +func (t *testNetworkManagerGeneric) SendManyCMIX(messages map[id.ID]format.Message, p params.CMIX) (id.Round, []ephemeral.Id, error) { + return id.Round(0), []ephemeral.Id{}, nil +} + func (t *testNetworkManagerGeneric) GetInstance() *network.Instance { return t.instance @@ -108,6 +112,14 @@ func (t *testNetworkManagerGeneric) GetSender() *gateway.Sender { return nil } +func (t *testNetworkManagerGeneric) GetAddressSize() uint8 { return 0 } + +func (t *testNetworkManagerGeneric) RegisterAddressSizeNotification(string) (chan uint8, error) { + return nil, nil +} + +func (t *testNetworkManagerGeneric) UnregisterAddressSizeNotification(string) {} + func InitTestingContextGeneric(i interface{}) (*storage.Session, interfaces.NetworkManager, error) { switch i.(type) { case *testing.T, *testing.M, *testing.B, *testing.PB: @@ -155,7 +167,7 @@ func (t *testNetworkManagerFullExchange) CheckGarbledMessages() { // Intended for alice to send to bob. Trigger's Bob's confirmation, chaining the operation // together -func (t *testNetworkManagerFullExchange) SendE2E(m message.Send, p params.E2E) ( +func (t *testNetworkManagerFullExchange) SendE2E(message.Send, params.E2E, *stoppable.Single) ( []id.Round, cE2e.MessageID, error) { rounds := []id.Round{id.Round(0), id.Round(1), id.Round(2)} @@ -181,18 +193,18 @@ func (t *testNetworkManagerFullExchange) SendE2E(m message.Send, p params.E2E) ( bobSwitchboard.Speak(confirmMessage) return rounds, cE2e.MessageID{}, nil - } func (t *testNetworkManagerFullExchange) SendUnsafe(m message.Send, p params.Unsafe) ([]id.Round, error) { - return nil, nil } func (t *testNetworkManagerFullExchange) SendCMIX(message format.Message, eid *id.ID, p params.CMIX) (id.Round, ephemeral.Id, error) { - return id.Round(0), ephemeral.Id{}, nil +} +func (t *testNetworkManagerFullExchange) SendManyCMIX(messages map[id.ID]format.Message, p params.CMIX) (id.Round, []ephemeral.Id, error) { + return id.Round(0), []ephemeral.Id{}, nil } func (t *testNetworkManagerFullExchange) GetInstance() *network.Instance { @@ -219,6 +231,14 @@ func (t *testNetworkManagerFullExchange) GetSender() *gateway.Sender { return nil } +func (t *testNetworkManagerFullExchange) GetAddressSize() uint8 { return 0 } + +func (t *testNetworkManagerFullExchange) RegisterAddressSizeNotification(string) (chan uint8, error) { + return nil, nil +} + +func (t *testNetworkManagerFullExchange) UnregisterAddressSizeNotification(string) {} + func InitTestingContextFullExchange(i interface{}) (*storage.Session, *switchboard.Switchboard, interfaces.NetworkManager) { switch i.(type) { case *testing.T, *testing.M, *testing.B, *testing.PB: diff --git a/network/ephemeral/addressSpace.go b/network/ephemeral/addressSpace.go new file mode 100644 index 0000000000000000000000000000000000000000..f942df77df38bfa4455bdf096ad2d4e0add40b49 --- /dev/null +++ b/network/ephemeral/addressSpace.go @@ -0,0 +1,138 @@ +package ephemeral + +import ( + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + "sync" + "testing" +) + +const ( + // The initial value for the address space size. This value signifies that + // the address space size has not yet been updated. + initSize = 1 +) + +// AddressSpace contains the current address space size used for creating +// ephemeral IDs and the infrastructure to alert other processes when an Update +// occurs. +type AddressSpace struct { + size uint8 + notifyMap map[string]chan uint8 + cond *sync.Cond +} + +// NewAddressSpace initialises a new AddressSpace and returns it. +func NewAddressSpace() *AddressSpace { + return &AddressSpace{ + size: initSize, + notifyMap: make(map[string]chan uint8), + cond: sync.NewCond(&sync.Mutex{}), + } +} + +// Get returns the current address space size. It blocks until an address space +// size is set. +func (as *AddressSpace) Get() uint8 { + as.cond.L.Lock() + defer as.cond.L.Unlock() + + // If the size has been set, then return the current size + if as.size != initSize { + return as.size + } + + // If the size is not set, then block until it is set + as.cond.Wait() + + return as.size +} + +// GetWithoutWait returns the current address space size regardless if it has +// been set yet. +func (as *AddressSpace) GetWithoutWait() uint8 { + as.cond.L.Lock() + defer as.cond.L.Unlock() + return as.size +} + +// Update updates the address space size to the new size, if it is larger. Then, +// each registered channel is notified of the Update. If this was the first time +// that the address space size was set, then the conditional broadcasts to stop +// blocking for all threads waiting on Get. +func (as *AddressSpace) Update(newSize uint8) { + as.cond.L.Lock() + defer as.cond.L.Unlock() + + // Skip Update if the address space size is unchanged + if as.size >= newSize { + return + } + + // Update address space size + oldSize := as.size + as.size = newSize + 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 { + as.cond.Broadcast() + } else { + // Broadcast the new address space size to all registered channels + for chanID, sizeChan := range as.notifyMap { + select { + case sizeChan <- as.size: + default: + jww.ERROR.Printf("Failed to send address space Update of %d on "+ + "channel with ID %s", as.size, chanID) + } + } + } +} + +// RegisterNotification returns a channel that will trigger for every address +// space size Update. The provided tag is the unique ID for the channel. +// Returns an error if the tag is already used. +func (as *AddressSpace) RegisterNotification(tag string) (chan uint8, error) { + as.cond.L.Lock() + defer as.cond.L.Unlock() + + if _, exists := as.notifyMap[tag]; exists { + return nil, errors.Errorf("tag \"%s\" already exists in notify map", tag) + } + + as.notifyMap[tag] = make(chan uint8, 1) + + return as.notifyMap[tag], nil +} + +// UnregisterNotification stops broadcasting address space size updates on the +// channel with the specified tag. +func (as *AddressSpace) UnregisterNotification(tag string) { + as.cond.L.Lock() + defer as.cond.L.Unlock() + + delete(as.notifyMap, tag) +} + +// NewTestAddressSpace initialises a new AddressSpace for testing with the given +// size. +func NewTestAddressSpace(newSize uint8, x interface{}) *AddressSpace { + switch x.(type) { + case *testing.T, *testing.M, *testing.B, *testing.PB: + break + default: + jww.FATAL.Panicf("NewTestAddressSpace is restricted to testing only. "+ + "Got %T", x) + } + + as := &AddressSpace{ + size: initSize, + notifyMap: make(map[string]chan uint8), + cond: sync.NewCond(&sync.Mutex{}), + } + + as.Update(newSize) + + return as +} diff --git a/network/ephemeral/addressSpace_test.go b/network/ephemeral/addressSpace_test.go new file mode 100644 index 0000000000000000000000000000000000000000..03cca463bc829e9db4ff6bb916fb6738e9b12e19 --- /dev/null +++ b/network/ephemeral/addressSpace_test.go @@ -0,0 +1,291 @@ +package ephemeral + +import ( + "reflect" + "strconv" + "sync" + "testing" + "time" +) + +// Unit test of NewAddressSpace. +func Test_newAddressSpace(t *testing.T) { + expected := &AddressSpace{ + size: initSize, + notifyMap: make(map[string]chan uint8), + cond: sync.NewCond(&sync.Mutex{}), + } + + as := NewAddressSpace() + + if !reflect.DeepEqual(expected, as) { + t.Errorf("NewAddressSpace failed to return the expected AddressSpace."+ + "\nexpected: %+v\nreceived: %+v", expected, as) + } +} + +// Test that AddressSpace.Get blocks when the address space size has not been +// set and that it does not block when it has been set. +func Test_addressSpace_Get(t *testing.T) { + as := NewAddressSpace() + expectedSize := uint8(42) + + // Call Get and error if it does not block + wait := make(chan uint8) + go func() { wait <- as.Get() }() + select { + case size := <-wait: + t.Errorf("Get failed to block and returned size %d.", size) + case <-time.NewTimer(10 * time.Millisecond).C: + } + + // Update address size + as.cond.L.Lock() + as.size = expectedSize + as.cond.L.Unlock() + + // Call Get and error if it does block + wait = make(chan uint8) + go func() { wait <- as.Get() }() + select { + case size := <-wait: + if size != expectedSize { + t.Errorf("Get returned the wrong size.\nexpected: %d\nreceived: %d", + expectedSize, size) + } + case <-time.NewTimer(15 * time.Millisecond).C: + t.Error("Get blocking when the size has been updated.") + } +} + +// Test that AddressSpace.Get blocks until the condition broadcasts. +func Test_addressSpace_Get_WaitBroadcast(t *testing.T) { + as := NewAddressSpace() + + wait := make(chan uint8) + go func() { wait <- as.Get() }() + + go func() { + select { + case size := <-wait: + if size != initSize { + t.Errorf("Get returned the wrong size.\nexpected: %d\nreceived: %d", + initSize, size) + } + case <-time.NewTimer(25 * time.Millisecond).C: + t.Error("Get blocking when the Cond has broadcast.") + } + }() + + time.Sleep(5 * time.Millisecond) + + as.cond.Broadcast() +} + +// Unit test of AddressSpace.GetWithoutWait. +func Test_addressSpace_GetWithoutWait(t *testing.T) { + as := NewAddressSpace() + + size := as.GetWithoutWait() + if size != initSize { + t.Errorf("GetWithoutWait returned the wrong size."+ + "\nexpected: %d\nreceived: %d", initSize, size) + } +} + +// Tests that AddressSpace.Update only updates the size when it is larger. +func Test_addressSpace_update(t *testing.T) { + as := NewAddressSpace() + expectedSize := uint8(42) + + // Attempt to Update to larger size + as.Update(expectedSize) + if as.size != expectedSize { + t.Errorf("Update failed to set the new size."+ + "\nexpected: %d\nreceived: %d", expectedSize, as.size) + } + + // Attempt to Update to smaller size + as.Update(expectedSize - 1) + if as.size != expectedSize { + t.Errorf("Update failed to set the new size."+ + "\nexpected: %d\nreceived: %d", expectedSize, as.size) + } +} + +// Tests that AddressSpace.Update sends the new size to all registered channels. +func Test_addressSpace_update_GetAndChannels(t *testing.T) { + as := NewAddressSpace() + var wg sync.WaitGroup + expectedSize := uint8(42) + + // Start threads that are waiting for an Update + wait := []chan uint8{make(chan uint8), make(chan uint8), make(chan uint8)} + for _, waitChan := range wait { + go func(waitChan chan uint8) { waitChan <- as.Get() }(waitChan) + } + + // Wait on threads + for i, waitChan := range wait { + go func(i int, waitChan chan uint8) { + wg.Add(1) + defer wg.Done() + + select { + case size := <-waitChan: + if size != expectedSize { + t.Errorf("Thread %d received unexpected size."+ + "\nexpected: %d\nreceived: %d", i, expectedSize, size) + } + case <-time.NewTimer(20 * time.Millisecond).C: + t.Errorf("Timed out waiting for Get to return on thread %d.", i) + } + }(i, waitChan) + } + + // Register channels + notifyChannels := make(map[string]chan uint8) + var notifyChan chan uint8 + var err error + var chanID string + for i := 0; i < 3; i++ { + chanID = strconv.Itoa(i) + notifyChannels[chanID], err = as.RegisterNotification(chanID) + if err != nil { + t.Errorf("Failed to regisdter channel: %+v", err) + } + } + + // Wait for new size on channels + for chanID, notifyChan := range notifyChannels { + go func(chanID string, notifyChan chan uint8) { + wg.Add(1) + defer wg.Done() + + select { + case size := <-notifyChan: + t.Errorf("Received size %d on channel %s when it should not have.", + size, chanID) + case <-time.NewTimer(20 * time.Millisecond).C: + } + }(chanID, notifyChan) + } + + time.Sleep(5 * time.Millisecond) + + // Attempt to Update to larger size + as.Update(expectedSize) + + wg.Wait() + + // Unregistered one channel and make sure it will not receive + delete(notifyChannels, chanID) + as.UnregisterNotification(chanID) + + expectedSize++ + + // Wait for new size on channels + for chanID, notifyChan := range notifyChannels { + go func(chanID string, notifyChan chan uint8) { + wg.Add(1) + defer wg.Done() + + select { + case size := <-notifyChan: + if size != expectedSize { + t.Errorf("Failed to receive expected size on channel %s."+ + "\nexpected: %d\nreceived: %d", chanID, expectedSize, size) + } + case <-time.NewTimer(20 * time.Millisecond).C: + t.Errorf("Timed out waiting on channel %s", chanID) + } + }(chanID, notifyChan) + } + + // Wait for timeout on unregistered channel + go func() { + wg.Add(1) + defer wg.Done() + + select { + case size := <-notifyChan: + t.Errorf("Received size %d on channel %s when it should not have.", + size, chanID) + case <-time.NewTimer(20 * time.Millisecond).C: + } + }() + + time.Sleep(5 * time.Millisecond) + + // Attempt to Update to larger size + as.Update(expectedSize) + + wg.Wait() +} + +// Tests that a channel created by AddressSpace.RegisterNotification receives +// the expected size when triggered. +func Test_addressSpace_RegisterNotification(t *testing.T) { + as := NewAddressSpace() + expectedSize := uint8(42) + + // Register channel + chanID := "chanID" + sizeChan, err := as.RegisterNotification(chanID) + if err != nil { + t.Errorf("RegisterNotification returned an error: %+v", err) + } + + // Wait on channel or error after timing out + go func() { + select { + case size := <-sizeChan: + if size != expectedSize { + t.Errorf("received wrong size on channel."+ + "\nexpected: %d\nreceived: %d", expectedSize, size) + } + case <-time.NewTimer(10 * time.Millisecond).C: + t.Error("Timed out waiting on channel.") + } + }() + + // Send on channel + select { + case as.notifyMap[chanID] <- expectedSize: + default: + t.Errorf("Sent on channel %s that should not be in map.", chanID) + } +} + +// Tests that when AddressSpace.UnregisterNotification unregisters a channel, +// it no longer can be triggered from the map. +func Test_addressSpace_UnregisterNotification(t *testing.T) { + as := NewAddressSpace() + expectedSize := uint8(42) + + // Register channel and then unregister it + chanID := "chanID" + sizeChan, err := as.RegisterNotification(chanID) + if err != nil { + t.Errorf("RegisterNotification returned an error: %+v", err) + } + as.UnregisterNotification(chanID) + + // Wait for timeout or error if the channel receives + go func() { + select { + case size := <-sizeChan: + t.Errorf("Received %d on channel %s that should not be in map.", + size, chanID) + case <-time.NewTimer(10 * time.Millisecond).C: + } + }() + + // Send on channel + select { + case as.notifyMap[chanID] <- expectedSize: + t.Errorf("Sent size %d on channel %s that should not be in map.", + expectedSize, chanID) + default: + } +} diff --git a/network/ephemeral/testutil.go b/network/ephemeral/testutil.go index d2708b0c71c7d112151e4494d3ef631a78adaf62..92eb68c408a0ecee0071651b5660c922d731934d 100644 --- a/network/ephemeral/testutil.go +++ b/network/ephemeral/testutil.go @@ -33,7 +33,7 @@ type testNetworkManager struct { msg message.Send } -func (t *testNetworkManager) SendE2E(m message.Send, _ params.E2E) ([]id.Round, +func (t *testNetworkManager) SendE2E(m message.Send, _ params.E2E, _ *stoppable.Single) ([]id.Round, e2e.MessageID, error) { rounds := []id.Round{ id.Round(0), @@ -62,6 +62,10 @@ func (t *testNetworkManager) SendCMIX(format.Message, *id.ID, params.CMIX) (id.R return 0, ephemeral.Id{}, nil } +func (t *testNetworkManager) SendManyCMIX(messages map[id.ID]format.Message, p params.CMIX) (id.Round, []ephemeral.Id, error) { + return 0, []ephemeral.Id{}, nil +} + func (t *testNetworkManager) GetInstance() *network.Instance { return t.instance } @@ -70,7 +74,7 @@ func (t *testNetworkManager) GetHealthTracker() interfaces.HealthTracker { return nil } -func (t *testNetworkManager) Follow(report interfaces.ClientErrorReport) (stoppable.Stoppable, error) { +func (t *testNetworkManager) Follow(_ interfaces.ClientErrorReport) (stoppable.Stoppable, error) { return nil, nil } @@ -84,12 +88,19 @@ func (t *testNetworkManager) GetSender() *gateway.Sender { return nil } +func (t *testNetworkManager) GetAddressSize() uint8 { return 15 } +func (t *testNetworkManager) RegisterAddressSizeNotification(string) (chan uint8, error) { + return nil, nil +} + +func (t *testNetworkManager) UnregisterAddressSizeNotification(string) {} + func NewTestNetworkManager(i interface{}) interfaces.NetworkManager { switch i.(type) { case *testing.T, *testing.M, *testing.B: break default: - jww.FATAL.Panicf("initTesting is restricted to testing only."+ + jww.FATAL.Panicf("NewTestNetworkManager is restricted to testing only."+ "Got %T", i) } @@ -97,17 +108,22 @@ func NewTestNetworkManager(i interface{}) interfaces.NetworkManager { cert, err := utils.ReadFile(testkeys.GetNodeCertPath()) if err != nil { - jww.FATAL.Panicf("Failed to create new test Instance: %v", err) + jww.FATAL.Panicf("Failed to create new test Instance: %+v", err) } - commsManager.AddHost(&id.Permissioning, "", cert, connect.GetDefaultHostParams()) + _, err = commsManager.AddHost( + &id.Permissioning, "", cert, connect.GetDefaultHostParams()) + if err != nil { + jww.FATAL.Panicf("Failed to add host: %+v", err) + } instanceComms := &connect.ProtoComms{ Manager: commsManager, } - thisInstance, err := network.NewInstanceTesting(instanceComms, getNDF(), getNDF(), nil, nil, i) + thisInstance, err := network.NewInstanceTesting( + instanceComms, getNDF(), getNDF(), nil, nil, i) if err != nil { - jww.FATAL.Panicf("Failed to create new test Instance: %v", err) + jww.FATAL.Panicf("Failed to create new test Instance: %+v", err) } thisManager := &testNetworkManager{instance: thisInstance} diff --git a/network/ephemeral/tracker.go b/network/ephemeral/tracker.go index 9a1ca5e59682e4d2907bb7ac0ff46f5daca0b204..5480125492017b62b6eaf8e14f7bbd6ab384f1f7 100644 --- a/network/ephemeral/tracker.go +++ b/network/ephemeral/tracker.go @@ -22,150 +22,159 @@ import ( const validityGracePeriod = 5 * time.Minute const TimestampKey = "IDTrackingTimestamp" +const TimestampStoreVersion = 0 const ephemeralStoppable = "EphemeralCheck" +const addressSpaceSizeChanTag = "ephemeralTracker" -// Track runs a thread which checks for past and present ephemeral ids -func Track(session *storage.Session, ourId *id.ID) stoppable.Stoppable { +// Track runs a thread which checks for past and present ephemeral ID. +func Track(session *storage.Session, addrSpace *AddressSpace, ourId *id.ID) stoppable.Stoppable { stop := stoppable.NewSingle(ephemeralStoppable) - go track(session, ourId, stop) + go track(session, addrSpace, ourId, stop) return stop } -// track is a thread which continuously processes ephemeral ids. -// If any error occurs, the thread crashes -func track(session *storage.Session, ourId *id.ID, stop *stoppable.Single) { +// track is a thread which continuously processes ephemeral IDs. Panics if any +// error occurs. +func track(session *storage.Session, addrSpace *AddressSpace, ourId *id.ID, stop *stoppable.Single) { // Check that there is a timestamp in store at all err := checkTimestampStore(session) if err != nil { - jww.FATAL.Panicf("Could not store timestamp "+ - "for ephemeral ID tracking: %v", err) + jww.FATAL.Panicf("Could not store timestamp for ephemeral ID "+ + "tracking: %+v", err) } // Get the latest timestamp from store lastTimestampObj, err := session.Get(TimestampKey) if err != nil { - jww.FATAL.Panicf("Could not get timestamp: %v", err) + jww.FATAL.Panicf("Could not get timestamp: %+v", err) } lastCheck, err := unmarshalTimestamp(lastTimestampObj) if err != nil { - jww.FATAL.Panicf("Could not parse stored timestamp: %v", err) + jww.FATAL.Panicf("Could not parse stored timestamp: %+v", err) } - // Wait until we get the id size from the network + // Wait until we get the ID size from the network receptionStore := session.Reception() - receptionStore.WaitForIdSizeUpdate() + addressSizeUpdate, err := addrSpace.RegisterNotification(addressSpaceSizeChanTag) + if err != nil { + jww.FATAL.Panicf("failed to register address size notification "+ + "channel: %+v", err) + } + addressSize := addrSpace.Get() - for true { + for { now := netTime.Now() - //hack for inconsistent time on android - if now.Sub(lastCheck) <= 0 { + // Hack for inconsistent time on android + if now.Before(lastCheck) || now.Equal(lastCheck) { now = lastCheck.Add(time.Nanosecond) } // Generates the IDs since the last track - protoIds, err := ephemeral.GetIdsByRange(ourId, receptionStore.GetIDSize(), - now, now.Sub(lastCheck)) + protoIds, err := ephemeral.GetIdsByRange( + ourId, uint(addressSize), now, now.Sub(lastCheck)) jww.DEBUG.Printf("Now: %s, LastCheck: %s, Different: %s", now, lastCheck, now.Sub(lastCheck)) - jww.DEBUG.Printf("protoIds Count: %d", len(protoIds)) if err != nil { - jww.FATAL.Panicf("Could not generate "+ - "upcoming IDs: %v", err) + jww.FATAL.Panicf("Could not generate upcoming IDs: %+v", err) } // Generate identities off of that list - identities := generateIdentities(protoIds, ourId) - - jww.INFO.Printf("Number of Identities Generated: %d", - len(identities)) + identities := generateIdentities(protoIds, ourId, addressSize) + jww.INFO.Printf("Number of Identities Generated: %d", len(identities)) jww.INFO.Printf("Current Identity: %d (source: %s), Start: %s, End: %s", - identities[len(identities)-1].EphId.Int64(), identities[len(identities)-1].Source, - identities[len(identities)-1].StartValid, identities[len(identities)-1].EndValid) + identities[len(identities)-1].EphId.Int64(), + identities[len(identities)-1].Source, + identities[len(identities)-1].StartValid, + identities[len(identities)-1].EndValid) - // Add identities to storage if unique + // Add identities to storage, if unique for _, identity := range identities { if err = receptionStore.AddIdentity(identity); err != nil { - jww.FATAL.Panicf("Could not insert "+ - "identity: %v", err) + jww.FATAL.Panicf("Could not insert identity: %+v", err) } } - // Generate the time stamp for storage + // Generate the timestamp for storage vo, err := marshalTimestamp(now) if err != nil { - jww.FATAL.Panicf("Could not marshal "+ - "timestamp for storage: %v", err) + jww.FATAL.Panicf("Could not marshal timestamp for storage: %+v", err) } // Store the timestamp if err = session.Set(TimestampKey, vo); err != nil { - jww.FATAL.Panicf("Could not store timestamp: %v", err) + jww.FATAL.Panicf("Could not store timestamp: %+v", err) } - // Sleep until the last Id has expired + // Sleep until the last ID has expired timeToSleep := calculateTickerTime(protoIds) - t := time.NewTimer(timeToSleep) select { - case <-t.C: + case <-time.NewTimer(timeToSleep).C: + case addressSize = <-addressSizeUpdate: + receptionStore.SetToExpire(addressSize) case <-stop.Quit(): + addrSpace.UnregisterNotification(addressSpaceSizeChanTag) + stop.ToStopped() return } } } -// generateIdentities is a constructor which generates a list of -// identities off of the list of protoIdentities passed in -func generateIdentities(protoIds []ephemeral.ProtoIdentity, - ourId *id.ID) []reception.Identity { +// generateIdentities generates a list of identities off of the list of passed +// in ProtoIdentity. +func generateIdentities(protoIds []ephemeral.ProtoIdentity, ourId *id.ID, + addressSize uint8) []reception.Identity { - identities := make([]reception.Identity, 0) + identities := make([]reception.Identity, len(protoIds)) - // Add identities for every ephemeral id - for _, eid := range protoIds { + // Add identities for every ephemeral ID + for i, eid := range protoIds { // Expand the grace period for both start and end eid.End.Add(validityGracePeriod) eid.Start.Add(-validityGracePeriod) - identities = append(identities, reception.Identity{ - EphId: eid.Id, - Source: ourId, - End: eid.End, - StartValid: eid.Start, - EndValid: eid.End, - Ephemeral: false, - }) + identities[i] = reception.Identity{ + EphId: eid.Id, + Source: ourId, + AddressSize: addressSize, + End: eid.End, + StartValid: eid.Start, + EndValid: eid.End, + Ephemeral: false, + } } return identities } -// Sanitation check of timestamp store. If a value has not been stored yet -// then the current time is stored +// checkTimestampStore performs a sanitation check of timestamp store. If a +// value has not been stored yet, then the current time is stored. func checkTimestampStore(session *storage.Session) error { if _, err := session.Get(TimestampKey); err != nil { - // only generate from the last hour because this is a new id, it - // couldn't receive messages yet + // Only generate from the last hour because this is a new ID; it could + // not yet receive messages now, err := marshalTimestamp(netTime.Now().Add(-1 * time.Hour)) if err != nil { - return errors.Errorf("Could not marshal new timestamp for storage: %v", err) + return errors.Errorf("Could not marshal new timestamp for "+ + "storage: %+v", err) } + return session.Set(TimestampKey, now) } return nil } -// Takes the stored timestamp and unmarshal into a time object +// unmarshalTimestamp unmarshal the stored timestamp into a time.Time. func unmarshalTimestamp(lastTimestampObj *versioned.Object) (time.Time, error) { if lastTimestampObj == nil || lastTimestampObj.Data == nil { return netTime.Now(), nil @@ -176,27 +185,29 @@ func unmarshalTimestamp(lastTimestampObj *versioned.Object) (time.Time, error) { return lastTimestamp, err } -// Marshals the timestamp for ekv storage. Generates a storable object +// marshalTimestamp marshals the timestamp and generates a storable object for +// ekv storage. func marshalTimestamp(timeToStore time.Time) (*versioned.Object, error) { data, err := timeToStore.MarshalBinary() return &versioned.Object{ - Version: 0, + Version: TimestampStoreVersion, Timestamp: netTime.Now(), Data: data, }, err } -// Helper function which calculates the time for the ticker based -// off of the last ephemeral ID to expire +// calculateTickerTime calculates the time for the ticker based off of the last +// ephemeral ID to expire. func calculateTickerTime(baseIDs []ephemeral.ProtoIdentity) time.Duration { if len(baseIDs) == 0 { return time.Duration(0) } + // Get the last identity in the list lastIdentity := baseIDs[len(baseIDs)-1] - // Factor out the grace period previously expanded upon. + // Factor out the grace period previously expanded upon // Calculate and return that duration gracePeriod := lastIdentity.End.Add(-validityGracePeriod) return lastIdentity.End.Sub(gracePeriod) diff --git a/network/ephemeral/tracker_test.go b/network/ephemeral/tracker_test.go index e6c3e11f8fd3b8a9e120f3634cac636b385f66c6..3307446bc17f25cbf8d8bc119e01a2889b1c2ae2 100644 --- a/network/ephemeral/tracker_test.go +++ b/network/ephemeral/tracker_test.go @@ -28,35 +28,34 @@ func TestCheck(t *testing.T) { session := storage.InitTestingSession(t) instance := NewTestNetworkManager(t) if err := setupInstance(instance); err != nil { - t.Errorf("Could not set up instance: %v", err) + t.Errorf("Could not set up instance: %+v", err) } - /// Store a mock initial timestamp the store + // Store a mock initial timestamp the store now := netTime.Now() twoDaysAgo := now.Add(-2 * 24 * time.Hour) twoDaysTimestamp, err := marshalTimestamp(twoDaysAgo) if err != nil { - t.Errorf("Could not marshal timestamp for test setup: %v", err) + t.Errorf("Could not marshal timestamp for test setup: %+v", err) } + err = session.Set(TimestampKey, twoDaysTimestamp) if err != nil { - t.Errorf("Could not set mock timestamp for test setup: %v", err) + t.Errorf("Could not set mock timestamp for test setup: %+v", err) } ourId := id.NewIdFromBytes([]byte("Sauron"), t) - stop := Track(session, ourId) - session.Reception().MarkIdSizeAsSet() + stop := Track(session, NewTestAddressSpace(15, t), ourId) - err = stop.Close(3 * time.Second) + err = stop.Close() if err != nil { - t.Errorf("Could not close thread: %v", err) + t.Errorf("Could not close thread: %+v", err) } } -// Unit test for track +// Unit test for track. func TestCheck_Thread(t *testing.T) { - session := storage.InitTestingSession(t) instance := NewTestNetworkManager(t) if err := setupInstance(instance); err != nil { @@ -65,27 +64,26 @@ func TestCheck_Thread(t *testing.T) { ourId := id.NewIdFromBytes([]byte("Sauron"), t) stop := stoppable.NewSingle(ephemeralStoppable) - /// Store a mock initial timestamp the store + // Store a mock initial timestamp the store now := netTime.Now() yesterday := now.Add(-24 * time.Hour) yesterdayTimestamp, err := marshalTimestamp(yesterday) if err != nil { - t.Errorf("Could not marshal timestamp for test setup: %v", err) + t.Errorf("Could not marshal timestamp for test setup: %+v", err) } + err = session.Set(TimestampKey, yesterdayTimestamp) if err != nil { - t.Errorf("Could not set mock timestamp for test setup: %v", err) + t.Errorf("Could not set mock timestamp for test setup: %+v", err) } // Run the tracker go func() { - track(session, ourId, stop) + track(session, NewTestAddressSpace(15, t), ourId, stop) }() time.Sleep(3 * time.Second) - session.Reception().MarkIdSizeAsSet() - - err = stop.Close(3 * time.Second) + err = stop.Close() if err != nil { t.Errorf("Could not close thread: %v", err) } @@ -95,7 +93,7 @@ func TestCheck_Thread(t *testing.T) { func setupInstance(instance interfaces.NetworkManager) error { cert, err := utils.ReadFile(testkeys.GetNodeKeyPath()) if err != nil { - return errors.Errorf("Failed to read cert from from file: %v", err) + return errors.Errorf("Failed to read cert from from file: %+v", err) } ri := &mixmessages.RoundInfo{ ID: 1, @@ -103,20 +101,20 @@ func setupInstance(instance interfaces.NetworkManager) error { testCert, err := rsa.LoadPrivateKeyFromPem(cert) if err != nil { - return errors.Errorf("Failed to load cert from from file: %v", err) + return errors.Errorf("Failed to load cert from from file: %+v", err) } if err = signature.SignRsa(ri, testCert); err != nil { - return errors.Errorf("Failed to sign round info: %v", err) + return errors.Errorf("Failed to sign round info: %+v", err) } if err = instance.GetInstance().RoundUpdate(ri); err != nil { - return errors.Errorf("Failed to RoundUpdate from from file: %v", err) + return errors.Errorf("Failed to RoundUpdate from from file: %+v", err) } ri = &mixmessages.RoundInfo{ ID: 2, } if err = signature.SignRsa(ri, testCert); err != nil { - return errors.Errorf("Failed to sign round info: %v", err) + return errors.Errorf("Failed to sign round info: %+v", err) } if err = instance.GetInstance().RoundUpdate(ri); err != nil { return errors.Errorf("Failed to RoundUpdate from from file: %v", err) diff --git a/network/follow.go b/network/follow.go index a6c4008cb85d703f2034de2343cb9c6168facbd8..3d5d8734b83c5892aae387e55e9e4ab6dba23e38 100644 --- a/network/follow.go +++ b/network/follow.go @@ -28,6 +28,7 @@ import ( jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/interfaces" "gitlab.com/elixxir/client/network/rounds" + "gitlab.com/elixxir/client/stoppable" pb "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/primitives/knownRounds" "gitlab.com/elixxir/primitives/states" @@ -49,19 +50,20 @@ type followNetworkComms interface { // followNetwork polls the network to get updated on the state of nodes, the // round status, and informs the client when messages can be retrieved. -func (m *manager) followNetwork(report interfaces.ClientErrorReport, quitCh <-chan struct{}, isRunning interfaces.Running) { +func (m *manager) followNetwork(report interfaces.ClientErrorReport, + stop *stoppable.Single) { ticker := time.NewTicker(m.param.TrackNetworkPeriod) TrackTicker := time.NewTicker(debugTrackPeriod) rng := m.Rng.GetStream() - done := false - for !done { + for { select { - case <-quitCh: + case <-stop.Quit(): rng.Close() - done = true + stop.ToStopped() + return case <-ticker.C: - m.follow(report, rng, m.Comms, isRunning) + m.follow(report, rng, m.Comms, stop) case <-TrackTicker.C: numPolls := atomic.SwapUint64(m.tracker, 0) if m.numLatencies != 0 { @@ -75,22 +77,16 @@ func (m *manager) followNetwork(report interfaces.ClientErrorReport, quitCh <-ch jww.INFO.Printf("Polled the network %d times in the "+ "last %s", numPolls, debugTrackPeriod) } - - } - if !isRunning.IsRunning() { - jww.ERROR.Printf("Killing network follower " + - "due to failed exit") - return } } } // executes each iteration of the follower func (m *manager) follow(report interfaces.ClientErrorReport, rng csprng.Source, - comms followNetworkComms, isRunning interfaces.Running) { + comms followNetworkComms, stop *stoppable.Single) { - //get the identity we will poll for - identity, err := m.Session.Reception().GetIdentity(rng) + //Get the identity we will poll for + identity, err := m.Session.Reception().GetIdentity(rng, m.addrSpace.GetWithoutWait()) if err != nil { jww.FATAL.Panicf("Failed to get an identity, this should be "+ "impossible: %+v", err) @@ -119,10 +115,11 @@ func (m *manager) follow(report interfaces.ClientErrorReport, rng csprng.Source, identity.EphId.Int64(), identity.Source, identity.StartRequest, identity.EndRequest, identity.EndRequest.Sub(identity.StartRequest), host.GetId()) return comms.SendPoll(host, &pollReq) - }) - if !isRunning.IsRunning() { - jww.ERROR.Printf("Killing network follower " + - "due to failed exit") + }, stop) + + // Exit if the thread has been stopped + if stoppable.CheckErr(err) { + jww.INFO.Print(err) return } @@ -162,12 +159,18 @@ func (m *manager) follow(report interfaces.ClientErrorReport, rng csprng.Source, // update gateway connections m.GetSender().UpdateNdf(m.GetInstance().GetPartialNdf().Get()) + m.Session.SetNDF(m.GetInstance().GetPartialNdf().Get()) + } + + // Update the address space size + // todo: this is a fix for incompatibility with the live network + // remove once the live network has been pushed to + if len(m.Instance.GetPartialNdf().Get().AddressSpace) != 0 { + m.addrSpace.Update(m.Instance.GetPartialNdf().Get().AddressSpace[0].Size) + } else { + m.addrSpace.Update(18) } - //check that the stored address space is correct - m.Session.Reception().UpdateIdSize(uint(m.Instance.GetPartialNdf().Get().AddressSpaceSize)) - // Updates any id size readers of a network compliant id size - m.Session.Reception().MarkIdSizeAsSet() // NOTE: this updates rounds and updates the tracking of the health of the // network if pollResp.Updates != nil { diff --git a/network/gateway/hostPool.go b/network/gateway/hostPool.go index ba8198da839c4e9d2693d866be165f6afaf08d47..1447608bfcc825f5e1269b2941d617718461662d 100644 --- a/network/gateway/hostPool.go +++ b/network/gateway/hostPool.go @@ -13,7 +13,6 @@ package gateway import ( "encoding/binary" - "fmt" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/storage" @@ -81,7 +80,7 @@ func DefaultPoolParams() PoolParams { p.HostParams.EnableCoolOff = true p.HostParams.NumSendsBeforeCoolOff = 1 p.HostParams.CoolOffTimeout = 5 * time.Minute - p.HostParams.SendTimeout = 3500 * time.Millisecond + p.HostParams.SendTimeout = 2000 * time.Millisecond return p } @@ -115,8 +114,24 @@ func newHostPool(poolParams PoolParams, rng *fastRNG.StreamGenerator, ndf *ndf.N return nil, err } + // Get the last used list of hosts and use it to seed the host pool list + hostList, err := storage.HostList().Get() + numHostsAdded := 0 + if err == nil { + for _, hid := range hostList { + err := result.replaceHostNoStore(hid, uint32(numHostsAdded)) + if err != nil { + jww.WARN.Printf("Unable to add stored host %s: %s", hid, err.Error()) + } else { + numHostsAdded++ + } + } + } else { + jww.WARN.Printf("Building new HostPool because no HostList stored: %+v", err) + } + // Build the initial HostPool and return - for i := 0; i < len(result.hostList); i++ { + for i := numHostsAdded; i < len(result.hostList); i++ { err := result.forceReplace(uint32(i)) if err != nil { return nil, err @@ -279,8 +294,29 @@ func (h *HostPool) forceReplace(oldPoolIndex uint32) error { } } -// Replace the given slot in the HostPool with a new Gateway with the specified ID +// replaceHost replaces the given slot in the HostPool with a new Gateway with +// the specified ID. The resulting host list is saved to storage. func (h *HostPool) replaceHost(newId *id.ID, oldPoolIndex uint32) error { + err := h.replaceHostNoStore(newId, oldPoolIndex) + if err != nil { + return err + } + + // Convert list of of non-nil and non-zero hosts to ID list + idList := make([]*id.ID, 0, len(h.hostList)) + for _, host := range h.hostList { + if host.GetId() != nil && !host.GetId().Cmp(&id.ID{}) { + idList = append(idList, host.GetId()) + } + } + + // Save the list to storage + return h.storage.HostList().Store(idList) +} + +// replaceHostNoStore replaces the given slot in the HostPool with a new Gateway +// with the specified ID. +func (h *HostPool) replaceHostNoStore(newId *id.ID, oldPoolIndex uint32) error { // Obtain that GwId's Host object newHost, ok := h.manager.GetHost(newId) if !ok { @@ -291,7 +327,8 @@ func (h *HostPool) replaceHost(newId *id.ID, oldPoolIndex uint32) error { // Keep track of oldHost for cleanup oldHost := h.hostList[oldPoolIndex] - // Use the poolIdx to overwrite the random Host in the corresponding index in the hostList + // Use the poolIdx to overwrite the random Host in the corresponding index + // in the hostList h.hostList[oldPoolIndex] = newHost // Use the GwId to keep track of the new random Host's index in the hostList h.hostMap[*newId] = oldPoolIndex @@ -301,7 +338,9 @@ func (h *HostPool) replaceHost(newId *id.ID, oldPoolIndex uint32) error { delete(h.hostMap, *oldHost.GetId()) go oldHost.Disconnect() } - jww.DEBUG.Printf("Replaced Host at %d with new Host %s", oldPoolIndex, newId.String()) + jww.DEBUG.Printf("Replaced Host at %d with new Host %s", oldPoolIndex, + newId.String()) + return nil } @@ -388,7 +427,7 @@ func (h *HostPool) removeGateway(gwId *id.ID) { func (h *HostPool) addGateway(gwId *id.ID, ndfIndex int) { gw := h.ndf.Gateways[ndfIndex] - //check if the host exists + // Check if the host exists host, ok := h.manager.GetHost(gwId) if !ok { @@ -443,7 +482,7 @@ func readUint32(rng io.Reader) uint32 { var rndBytes [4]byte i, err := rng.Read(rndBytes[:]) if i != 4 || err != nil { - panic(fmt.Sprintf("cannot read from rng: %+v", err)) + jww.FATAL.Panicf("cannot read from rng: %+v", err) } return binary.BigEndian.Uint32(rndBytes[:]) } diff --git a/network/gateway/hostpool_test.go b/network/gateway/hostpool_test.go index 49dcacf6b8231647b8c0cbc0ab6e6f84b0c7d126..f88bdf49a5c02e757781f3c452b9eef2759862bb 100644 --- a/network/gateway/hostpool_test.go +++ b/network/gateway/hostpool_test.go @@ -54,6 +54,49 @@ func TestNewHostPool(t *testing.T) { } } +// Tests that the hosts are loaded from storage, if they exist. +func TestNewHostPool_HostListStore(t *testing.T) { + manager := newMockManager() + rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) + testNdf := getTestNdf(t) + testStorage := storage.InitTestingSession(t) + addGwChan := make(chan network.NodeGateway) + params := DefaultPoolParams() + params.MaxPoolSize = uint32(len(testNdf.Gateways)) + + addedIDs := []*id.ID{ + id.NewIdFromString("testID0", id.Gateway, t), + id.NewIdFromString("testID1", id.Gateway, t), + id.NewIdFromString("testID2", id.Gateway, t), + id.NewIdFromString("testID3", id.Gateway, t), + } + err := testStorage.HostList().Store(addedIDs) + if err != nil { + t.Fatalf("Failed to store host list: %+v", err) + } + + for i, hid := range addedIDs { + testNdf.Gateways[i].ID = hid.Marshal() + } + + // Call the constructor + hp, err := newHostPool(params, rng, testNdf, manager, testStorage, addGwChan) + if err != nil { + t.Fatalf("Failed to create mock host pool: %v", err) + } + + // Check that the host list was saved to storage + hostList, err := hp.storage.HostList().Get() + if err != nil { + t.Errorf("Failed to get host list: %+v", err) + } + + if !reflect.DeepEqual(addedIDs, hostList) { + t.Errorf("Failed to save expected host list to storage."+ + "\nexpected: %+v\nreceived: %+v", addedIDs, hostList) + } +} + // Unit test func TestHostPool_ManageHostPool(t *testing.T) { manager := newMockManager() @@ -115,7 +158,7 @@ func TestHostPool_ManageHostPool(t *testing.T) { for _, ndfGw := range testNdf.Gateways { gwId, err := id.Unmarshal(ndfGw.ID) if err != nil { - t.Errorf("Failed to marshal gateway id for %v", ndfGw) + t.Fatalf("Failed to marshal gateway id for %v", ndfGw) } if _, ok := testPool.hostMap[*gwId]; ok { t.Errorf("Expected gateway %v to be removed from pool", gwId) @@ -135,6 +178,7 @@ func TestHostPool_ReplaceHost(t *testing.T) { hostList: make([]*connect.Host, newIndex+1), hostMap: make(map[id.ID]uint32), ndf: testNdf, + storage: storage.InitTestingSession(t), } /* "Replace" a host with no entry */ @@ -228,6 +272,18 @@ func TestHostPool_ReplaceHost(t *testing.T) { "\n\tReceived: %d", newIndex, retrievedIndex) } + // Check that the host list was saved to storage + hostList, err := hostPool.storage.HostList().Get() + if err != nil { + t.Errorf("Failed to get host list: %+v", err) + } + + expectedList := []*id.ID{gwIdTwo} + + if !reflect.DeepEqual(expectedList, hostList) { + t.Errorf("Failed to save expected host list to storage."+ + "\nexpected: %+v\nreceived: %+v", expectedList, hostList) + } } // Error path, could not get host @@ -754,7 +810,7 @@ func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { for _, ndfGw := range testNdf.Gateways { gwId, err := id.Unmarshal(ndfGw.ID) if err != nil { - t.Errorf("Failed to marshal gateway id for %v", ndfGw) + t.Fatalf("Failed to marshal gateway id for %v", ndfGw) } if _, ok := testPool.hostMap[*gwId]; ok { t.Errorf("Expected gateway %v to be removed from pool", gwId) diff --git a/network/gateway/sender.go b/network/gateway/sender.go index 619581cd7c9d4289ef3a88faeff48eff2a67bb6d..e8943e21549e34bdbe692a689f6f789e7b01af73 100644 --- a/network/gateway/sender.go +++ b/network/gateway/sender.go @@ -11,10 +11,10 @@ package gateway import ( "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/client/storage" "gitlab.com/elixxir/comms/network" "gitlab.com/elixxir/crypto/fastRNG" - "gitlab.com/elixxir/crypto/shuffle" "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/ndf" @@ -36,51 +36,15 @@ func NewSender(poolParams PoolParams, rng *fastRNG.StreamGenerator, ndf *ndf.Net return &Sender{hostPool}, nil } -// SendToSpecific Call given sendFunc to a specific Host in the HostPool, -// attempting with up to numProxies destinations in case of failure -func (s *Sender) SendToSpecific(target *id.ID, - sendFunc func(host *connect.Host, target *id.ID) (interface{}, bool, error)) (interface{}, error) { - host, ok := s.getSpecific(target) - if ok { - result, didAbort, err := sendFunc(host, target) - if err == nil { - return result, s.forceAdd(target) - } else { - if didAbort { - return nil, errors.WithMessagef(err, "Aborted SendToSpecific gateway %s", host.GetId().String()) - } - jww.WARN.Printf("Unable to SendToSpecific %s: %s", host.GetId().String(), err) - } - } - - proxies := s.getAny(s.poolParams.ProxyAttempts, []*id.ID{target}) - for i := range proxies { - result, didAbort, err := sendFunc(proxies[i], target) - if err == nil { - return result, nil - } else { - if didAbort { - return nil, errors.WithMessagef(err, "Aborted SendToSpecific gateway proxy %s", - host.GetId().String()) - } - jww.WARN.Printf("Unable to SendToSpecific proxy %s: %s", proxies[i].GetId().String(), err) - _, err = s.checkReplace(proxies[i].GetId(), err) - if err != nil { - jww.ERROR.Printf("Unable to checkReplace: %+v", err) - } - } - } - - return nil, errors.Errorf("Unable to send to specific with proxies") -} - // SendToAny Call given sendFunc to any Host in the HostPool, attempting with up to numProxies destinations -func (s *Sender) SendToAny(sendFunc func(host *connect.Host) (interface{}, error)) (interface{}, error) { +func (s *Sender) SendToAny(sendFunc func(host *connect.Host) (interface{}, error), stop *stoppable.Single) (interface{}, error) { proxies := s.getAny(s.poolParams.ProxyAttempts, nil) for i := range proxies { result, err := sendFunc(proxies[i]) - if err == nil { + if stop != nil && !stop.IsRunning() { + return nil, errors.Errorf(stoppable.ErrMsg, stop.Name(), "SendToAny") + } else if err == nil { return result, nil } else { jww.WARN.Printf("Unable to SendToAny %s: %s", proxies[i].GetId().String(), err) @@ -96,27 +60,24 @@ func (s *Sender) SendToAny(sendFunc func(host *connect.Host) (interface{}, error // SendToPreferred Call given sendFunc to any Host in the HostPool, attempting with up to numProxies destinations func (s *Sender) SendToPreferred(targets []*id.ID, - sendFunc func(host *connect.Host, target *id.ID) (interface{}, error)) (interface{}, error) { + sendFunc func(host *connect.Host, target *id.ID) (interface{}, bool, error), + stop *stoppable.Single) (interface{}, error) { // Get the hosts and shuffle randomly targetHosts := s.getPreferred(targets) - var rndBytes [32]byte - stream := s.rng.GetStream() - _, err := stream.Read(rndBytes[:]) - stream.Close() - if err != nil { - return nil, err - } - shuffle.ShuffleSwap(rndBytes[:], len(targetHosts), func(i, j int) { - targetHosts[i], targetHosts[j] = targetHosts[j], targetHosts[i] - }) // Attempt to send directly to targets if they are in the HostPool for i := range targetHosts { - result, err := sendFunc(targetHosts[i], targets[i]) - if err == nil { + result, didAbort, err := sendFunc(targetHosts[i], targets[i]) + if stop != nil && !stop.IsRunning() { + return nil, errors.Errorf(stoppable.ErrMsg, stop.Name(), "SendToPreferred") + } else if err == nil { return result, nil } else { + if didAbort { + return nil, errors.WithMessagef(err, "Aborted SendToPreferred gateway %s", + targetHosts[i].GetId().String()) + } jww.WARN.Printf("Unable to SendToPreferred %s via %s: %s", targets[i], targetHosts[i].GetId(), err) _, err = s.checkReplace(targetHosts[i].GetId(), err) @@ -147,10 +108,16 @@ func (s *Sender) SendToPreferred(targets []*id.ID, continue } - result, err := sendFunc(targetProxies[proxyIdx], target) - if err == nil { + result, didAbort, err := sendFunc(targetProxies[proxyIdx], target) + if stop != nil && !stop.IsRunning() { + return nil, errors.Errorf(stoppable.ErrMsg, stop.Name(), "SendToPreferred") + } else if err == nil { return result, nil } else { + if didAbort { + return nil, errors.WithMessagef(err, "Aborted SendToPreferred gateway proxy %s", + proxy.GetId().String()) + } jww.WARN.Printf("Unable to SendToPreferred %s via proxy "+ "%s: %s", target, proxy.GetId(), err) wasReplaced, err := s.checkReplace(proxy.GetId(), err) diff --git a/network/gateway/sender_test.go b/network/gateway/sender_test.go index 4dd5d49c02ea525e547164f8c2c6392b526b30de..d8dbf16227e8724ef509589bb7133efea888cc26 100644 --- a/network/gateway/sender_test.go +++ b/network/gateway/sender_test.go @@ -78,7 +78,7 @@ func TestSender_SendToAny(t *testing.T) { } // Test sendToAny with test interfaces - result, err := sender.SendToAny(SendToAny_HappyPath) + result, err := sender.SendToAny(SendToAny_HappyPath, nil) if err != nil { t.Errorf("Should not error in SendToAny happy path: %v", err) } @@ -89,12 +89,12 @@ func TestSender_SendToAny(t *testing.T) { "\n\tReceived: %v", happyPathReturn, result) } - _, err = sender.SendToAny(SendToAny_KnownError) + _, err = sender.SendToAny(SendToAny_KnownError, nil) if err == nil { t.Fatalf("Expected error path did not receive error") } - _, err = sender.SendToAny(SendToAny_UnknownError) + _, err = sender.SendToAny(SendToAny_UnknownError, nil) if err == nil { t.Fatalf("Expected error path did not receive error") } @@ -139,7 +139,7 @@ func TestSender_SendToPreferred(t *testing.T) { preferredHost := sender.hostList[preferredIndex] // Happy path - result, err := sender.SendToPreferred([]*id.ID{preferredHost.GetId()}, SendToPreferred_HappyPath) + result, err := sender.SendToPreferred([]*id.ID{preferredHost.GetId()}, SendToPreferred_HappyPath, nil) if err != nil { t.Errorf("Should not error in SendToPreferred happy path: %v", err) } @@ -151,7 +151,7 @@ func TestSender_SendToPreferred(t *testing.T) { } // Call a send which returns an error which triggers replacement - _, err = sender.SendToPreferred([]*id.ID{preferredHost.GetId()}, SendToPreferred_KnownError) + _, err = sender.SendToPreferred([]*id.ID{preferredHost.GetId()}, SendToPreferred_KnownError, nil) if err == nil { t.Fatalf("Expected error path did not receive error") } @@ -171,7 +171,7 @@ func TestSender_SendToPreferred(t *testing.T) { preferredHost = sender.hostList[preferredIndex] // Unknown error return will not trigger replacement - _, err = sender.SendToPreferred([]*id.ID{preferredHost.GetId()}, SendToPreferred_UnknownError) + _, err = sender.SendToPreferred([]*id.ID{preferredHost.GetId()}, SendToPreferred_UnknownError, nil) if err == nil { t.Fatalf("Expected error path did not receive error") } @@ -187,63 +187,3 @@ func TestSender_SendToPreferred(t *testing.T) { } } - -func TestSender_SendToSpecific(t *testing.T) { - manager := newMockManager() - rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) - testNdf := getTestNdf(t) - testStorage := storage.InitTestingSession(t) - addGwChan := make(chan network.NodeGateway) - params := DefaultPoolParams() - params.MaxPoolSize = uint32(len(testNdf.Gateways)) - 5 - - // Do not test proxy attempts code in this test - // (self contain to code specific in sendPreferred) - params.ProxyAttempts = 0 - - // Pull all gateways from ndf into host manager - for _, gw := range testNdf.Gateways { - - gwId, err := id.Unmarshal(gw.ID) - if err != nil { - t.Fatalf("Failed to unmarshal ID in mock ndf: %v", err) - } - // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) - if err != nil { - t.Fatalf("Could not add mock host to manager: %v", err) - } - - } - - sender, err := NewSender(params, rng, testNdf, manager, testStorage, addGwChan) - if err != nil { - t.Fatalf("Failed to create mock sender: %v", err) - } - - preferredIndex := 0 - preferredHost := sender.hostList[preferredIndex] - - // Happy path - result, err := sender.SendToSpecific(preferredHost.GetId(), SendToSpecific_HappyPath) - if err != nil { - t.Errorf("Should not error in SendToSpecific happy path: %v", err) - } - - if !reflect.DeepEqual(result, happyPathReturn) { - t.Errorf("Expected result not returnev via SendToSpecific interface."+ - "\n\tExpected: %v"+ - "\n\tReceived: %v", happyPathReturn, result) - } - - // Ensure host is now in map - if _, ok := sender.hostMap[*preferredHost.GetId()]; !ok { - t.Errorf("Failed to forcefully add new gateway ID: %v", preferredHost.GetId()) - } - - _, err = sender.SendToSpecific(preferredHost.GetId(), SendToSpecific_Abort) - if err == nil { - t.Errorf("Expected sendSpecific to return an abort") - } - -} diff --git a/network/gateway/utils_test.go b/network/gateway/utils_test.go index 0ec7dc11f8edde72a82affd3b718bcec121bf60c..9f75ace1429e947ef7c8a399f3f088e2583777de 100644 --- a/network/gateway/utils_test.go +++ b/network/gateway/utils_test.go @@ -129,16 +129,16 @@ func getTestNdf(face interface{}) *ndf.NetworkDefinition { const happyPathReturn = "happyPathReturn" -func SendToPreferred_HappyPath(host *connect.Host, target *id.ID) (interface{}, error) { - return happyPathReturn, nil +func SendToPreferred_HappyPath(host *connect.Host, target *id.ID) (interface{}, bool, error) { + return happyPathReturn, false, nil } -func SendToPreferred_KnownError(host *connect.Host, target *id.ID) (interface{}, error) { - return nil, fmt.Errorf(errorsList[0]) +func SendToPreferred_KnownError(host *connect.Host, target *id.ID) (interface{}, bool, error) { + return nil, false, fmt.Errorf(errorsList[0]) } -func SendToPreferred_UnknownError(host *connect.Host, target *id.ID) (interface{}, error) { - return nil, fmt.Errorf("Unexpected error: Oopsie") +func SendToPreferred_UnknownError(host *connect.Host, target *id.ID) (interface{}, bool, error) { + return nil, false, fmt.Errorf("Unexpected error: Oopsie") } func SendToAny_HappyPath(host *connect.Host) (interface{}, error) { diff --git a/network/health/tracker.go b/network/health/tracker.go index ff53904f2015fe81af4938efd9ca8222fd68eba1..1e6dfdd97a0087d304f942ae2c6c3c0edf8f1991 100644 --- a/network/health/tracker.go +++ b/network/health/tracker.go @@ -5,7 +5,8 @@ // LICENSE file // /////////////////////////////////////////////////////////////////////////////// -// Contains functionality related to the event model driven network health tracker +// Contains functionality related to the event model driven network health +// tracker. package health @@ -23,70 +24,108 @@ type Tracker struct { heartbeat chan network.Heartbeat - channels []chan bool - funcs []func(isHealthy bool) + channels map[uint64]chan bool + funcs map[uint64]func(isHealthy bool) + channelsID uint64 + funcsID uint64 running bool // Determines the current health status isHealthy bool - // Denotes the past health status - // wasHealthy is true if isHealthy has ever been true + + // Denotes that the past health status wasHealthy is true if isHealthy has + // ever been true wasHealthy bool mux sync.RWMutex } -// Creates a single HealthTracker thread, starts it, and returns a tracker and a stoppable +// Init creates a single HealthTracker thread, starts it, and returns a tracker +// and a stoppable. func Init(instance *network.Instance, timeout time.Duration) *Tracker { - tracker := newTracker(timeout) instance.SetNetworkHealthChan(tracker.heartbeat) return tracker } -// Builds and returns a new Tracker object given a Context +// newTracker builds and returns a new Tracker object given a Context. func newTracker(timeout time.Duration) *Tracker { return &Tracker{ timeout: timeout, - channels: make([]chan bool, 0), + channels: map[uint64]chan bool{}, + funcs: map[uint64]func(isHealthy bool){}, heartbeat: make(chan network.Heartbeat, 100), isHealthy: false, running: false, } } -// Add a channel to the list of Tracker channels -// such that each channel can be notified of network changes -func (t *Tracker) AddChannel(c chan bool) { +// AddChannel adds a channel to the list of Tracker channels such that each +// channel can be notified of network changes. Returns a unique ID for the +// channel. +func (t *Tracker) AddChannel(c chan bool) uint64 { + var currentID uint64 + t.mux.Lock() - t.channels = append(t.channels, c) + t.channels[t.channelsID] = c + currentID = t.channelsID + t.channelsID++ t.mux.Unlock() + select { case c <- t.IsHealthy(): default: } + + return currentID } -// Add a function to the list of Tracker function -// such that each function can be run after network changes -func (t *Tracker) AddFunc(f func(isHealthy bool)) { +// RemoveChannel removes the channel with the given ID from the list of Tracker +// channels so that it will not longer be notified of network changes. +func (t *Tracker) RemoveChannel(chanID uint64) { t.mux.Lock() - t.funcs = append(t.funcs, f) + delete(t.channels, chanID) t.mux.Unlock() +} + +// AddFunc adds a function to the list of Tracker functions such that each +// function can be run after network changes. Returns a unique ID for the +// function. +func (t *Tracker) AddFunc(f func(isHealthy bool)) uint64 { + var currentID uint64 + + t.mux.Lock() + t.funcs[t.funcsID] = f + currentID = t.funcsID + t.funcsID++ + t.mux.Unlock() + go f(t.IsHealthy()) + + return currentID +} + +// RemoveFunc removes the function with the given ID from the list of Tracker +// functions so that it will not longer be run. +func (t *Tracker) RemoveFunc(chanID uint64) { + t.mux.Lock() + delete(t.channels, chanID) + t.mux.Unlock() } func (t *Tracker) IsHealthy() bool { t.mux.RLock() defer t.mux.RUnlock() + return t.isHealthy } -// Returns true if isHealthy has ever been true +// WasHealthy returns true if isHealthy has ever been true. func (t *Tracker) WasHealthy() bool { t.mux.RLock() defer t.mux.RUnlock() + return t.wasHealthy } @@ -94,10 +133,11 @@ func (t *Tracker) setHealth(h bool) { t.mux.Lock() // Only set wasHealthy to true if either // wasHealthy is true or - // wasHealthy false but h value is true + // wasHealthy is false but h value is true t.wasHealthy = t.wasHealthy || h t.isHealthy = h t.mux.Unlock() + t.transmit(h) } @@ -114,25 +154,29 @@ func (t *Tracker) Start() (stoppable.Stoppable, error) { stop := stoppable.NewSingle("Health Tracker") - go t.start(stop.Quit()) + go t.start(stop) return stop, nil } -// Long-running thread used to monitor and report on network health -func (t *Tracker) start(quitCh <-chan struct{}) { +// start starts a long-running thread used to monitor and report on network +// health. +func (t *Tracker) start(stop *stoppable.Single) { timer := time.NewTimer(t.timeout) for { var heartbeat network.Heartbeat select { - case <-quitCh: + case <-stop.Quit(): t.mux.Lock() t.isHealthy = false t.running = false t.mux.Unlock() + t.transmit(false) - break + stop.ToStopped() + + return case heartbeat = <-t.heartbeat: if healthy(heartbeat) { // Stop and reset timer @@ -146,10 +190,9 @@ func (t *Tracker) start(quitCh <-chan struct{}) { timer.Reset(t.timeout) t.setHealth(true) } - break case <-timer.C: t.setHealth(false) - break + return } } } diff --git a/network/health/tracker_test.go b/network/health/tracker_test.go index 4a10843c36ef23bcd3dc8cfbb5699e71a9643e78..a2e20651adaa06781f4d685cb5502cd5b56faae0 100644 --- a/network/health/tracker_test.go +++ b/network/health/tracker_test.go @@ -9,12 +9,11 @@ package health import ( "gitlab.com/elixxir/comms/network" - // "gitlab.com/elixxir/comms/network" "testing" "time" ) -// Happy path smoke test +// Happy path smoke test. func TestNewTracker(t *testing.T) { // Initialize required variables timeout := 250 * time.Millisecond @@ -49,8 +48,7 @@ func TestNewTracker(t *testing.T) { // Begin the health tracker _, err := tracker.Start() if err != nil { - t.Errorf("Unable to start tracker: %+v", err) - return + t.Fatalf("Unable to start tracker: %+v", err) } // Send a positive health heartbeat @@ -68,14 +66,12 @@ func TestNewTracker(t *testing.T) { // Verify the network was marked as healthy if !tracker.IsHealthy() { - t.Errorf("Tracker did not become healthy") - return + t.Fatal("Tracker did not become healthy.") } // Check if the tracker was ever healthy if !tracker.WasHealthy() { - t.Errorf("Tracker did not become healthy") - return + t.Fatal("Tracker did not become healthy.") } // Verify the heartbeat triggered the listening chan/func @@ -89,15 +85,12 @@ func TestNewTracker(t *testing.T) { // Verify the network was marked as NOT healthy if tracker.IsHealthy() { - t.Errorf("Tracker should not report healthy") - return + t.Fatal("Tracker should not report healthy.") } - // Check if the tracker was ever healthy, - // after setting healthy to false + // Check if the tracker was ever healthy, after setting healthy to false if !tracker.WasHealthy() { - t.Errorf("Tracker was healthy previously but not reported healthy") - return + t.Fatal("Tracker was healthy previously but not reported healthy.") } // Verify the timeout triggered the listening chan/func diff --git a/network/manager.go b/network/manager.go index 87b35f02230429f7e41786f4ff25c9ee6c4300e3..d480f4afe6c2b23da229c4c20b0b752c3e4d936f 100644 --- a/network/manager.go +++ b/network/manager.go @@ -28,6 +28,8 @@ import ( "gitlab.com/elixxir/comms/network" "gitlab.com/elixxir/crypto/fastRNG" "gitlab.com/xx_network/primitives/ndf" + "math" + "time" ) // Manager implements the NetworkManager interface inside context. It @@ -50,6 +52,9 @@ type manager struct { tracker *uint64 latencySum uint64 numLatencies uint64 + + // Address space size + addrSpace *ephemeral.AddressSpace } // NewManager builds a new reception manager object using inputted key fields @@ -71,10 +76,11 @@ func NewManager(session *storage.Session, switchboard *switchboard.Switchboard, tracker := uint64(0) - //create manager object + // create manager object m := manager{ - param: params, - tracker: &tracker, + param: params, + tracker: &tracker, + addrSpace: ephemeral.NewAddressSpace(), } m.Internal = internal.Internal{ @@ -91,6 +97,8 @@ func NewManager(session *storage.Session, switchboard *switchboard.Switchboard, // Set up gateway.Sender poolParams := gateway.DefaultPoolParams() + // Client will not send KeepAlive packets + poolParams.HostParams.KaClientOpts.Time = time.Duration(math.MaxInt64) m.sender, err = gateway.NewSender(poolParams, rng, ndf, comms, session, m.NodeRegistration) if err != nil { @@ -132,7 +140,7 @@ func (m *manager) Follow(report interfaces.ClientErrorReport) (stoppable.Stoppab // Start the Network Tracker trackNetworkStopper := stoppable.NewSingle("TrackNetwork") - go m.followNetwork(report, trackNetworkStopper.Quit(), trackNetworkStopper) + go m.followNetwork(report, trackNetworkStopper) multi.Add(trackNetworkStopper) // Message reception @@ -141,7 +149,7 @@ func (m *manager) Follow(report interfaces.ClientErrorReport) (stoppable.Stoppab // Round processing multi.Add(m.round.StartProcessors()) - multi.Add(ephemeral.Track(m.Session, m.ReceptionID)) + multi.Add(ephemeral.Track(m.Session, m.addrSpace, m.ReceptionID)) return multi, nil } @@ -173,3 +181,22 @@ func (m *manager) CheckGarbledMessages() { func (m *manager) InProgressRegistrations() int { return len(m.Internal.NodeRegistration) } + +// GetAddressSize returns the current address space size. It blocks until an +// address space size is set. +func (m *manager) GetAddressSize() uint8 { + return m.addrSpace.Get() +} + +// RegisterAddressSizeNotification returns a channel that will trigger for every +// address space size update. The provided tag is the unique ID for the channel. +// Returns an error if the tag is already used. +func (m *manager) RegisterAddressSizeNotification(tag string) (chan uint8, error) { + return m.addrSpace.RegisterNotification(tag) +} + +// UnregisterAddressSizeNotification stops broadcasting address space size +// updates on the channel with the specified tag. +func (m *manager) UnregisterAddressSizeNotification(tag string) { + m.addrSpace.UnregisterNotification(tag) +} diff --git a/network/message/bundle.go b/network/message/bundle.go index 56f1618d643641da6e9c2550e998a37a1269344c..81c649bd3798d693cd451dd7322d9d83e95510d9 100644 --- a/network/message/bundle.go +++ b/network/message/bundle.go @@ -9,13 +9,15 @@ package message import ( "gitlab.com/elixxir/client/storage/reception" + pb "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/primitives/format" "gitlab.com/xx_network/primitives/id" ) type Bundle struct { - Round id.Round - Messages []format.Message - Finish func() - Identity reception.IdentityUse + Round id.Round + RoundInfo *pb.RoundInfo + Messages []format.Message + Finish func() + Identity reception.IdentityUse } diff --git a/network/message/critical.go b/network/message/critical.go index 384ac9981f1dec0471b72aa3083880c26fc56382..84b88da602ac8fb87a1562d25ab8a52c6f1c23ed 100644 --- a/network/message/critical.go +++ b/network/message/critical.go @@ -12,6 +12,7 @@ import ( "gitlab.com/elixxir/client/interfaces/message" "gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/interfaces/utility" + "gitlab.com/elixxir/client/stoppable" ds "gitlab.com/elixxir/comms/network/dataStructures" "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/states" @@ -27,22 +28,22 @@ import ( // Tracker (/network/Health/Tracker.g0) //Thread loop for processing critical messages -func (m *Manager) processCriticalMessages(quitCh <-chan struct{}) { - done := false - for !done { +func (m *Manager) processCriticalMessages(stop *stoppable.Single) { + for { select { - case <-quitCh: - done = true + case <-stop.Quit(): + stop.ToStopped() + return case isHealthy := <-m.networkIsHealthy: if isHealthy { - m.criticalMessages() + m.criticalMessages(stop) } } } } // processes all critical messages -func (m *Manager) criticalMessages() { +func (m *Manager) criticalMessages(stop *stoppable.Single) { critMsgs := m.Session.GetCriticalMessages() // try to send every message in the critical messages and the raw critical // messages buffer in parallel @@ -53,7 +54,7 @@ func (m *Manager) criticalMessages() { jww.INFO.Printf("Resending critical message to %s ", msg.Recipient) //send the message - rounds, _, err := m.SendE2E(msg, param) + rounds, _, err := m.SendE2E(msg, param, stop) //if the message fail to send, notify the buffer so it can be handled //in the future and exit if err != nil { @@ -95,7 +96,7 @@ func (m *Manager) criticalMessages() { jww.INFO.Printf("Resending critical raw message to %s "+ "(msgDigest: %s)", rid, msg.Digest()) //send the message - round, _, err := m.SendCMIX(m.sender, msg, rid, param) + round, _, err := m.SendCMIX(m.sender, msg, rid, param, stop) //if the message fail to send, notify the buffer so it can be handled //in the future and exit if err != nil { diff --git a/network/message/garbled.go b/network/message/garbled.go index d9e1ac6be427f2ca0e9c8e89572fb6eed2285483..e8fb10cbe49845f7370639877c4dcdba45342a39 100644 --- a/network/message/garbled.go +++ b/network/message/garbled.go @@ -10,8 +10,9 @@ package message import ( jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/primitives/format" - "time" + "gitlab.com/xx_network/primitives/netTime" ) // Messages can arrive in the network out of order. When message handling fails @@ -33,12 +34,12 @@ func (m *Manager) CheckGarbledMessages() { } //long running thread which processes garbled messages -func (m *Manager) processGarbledMessages(quitCh <-chan struct{}) { - done := false - for !done { +func (m *Manager) processGarbledMessages(stop *stoppable.Single) { + for { select { - case <-quitCh: - done = true + case <-stop.Quit(): + stop.ToStopped() + return case <-m.triggerGarbled: m.handleGarbledMessages() } @@ -80,7 +81,7 @@ func (m *Manager) handleGarbledMessages() { // unless it is the last attempts and has been in the buffer long // enough, in which case remove it if count == m.param.MaxChecksGarbledMessage && - time.Since(timestamp) > m.param.GarbledMessageWait { + netTime.Since(timestamp) > m.param.GarbledMessageWait { garbledMsgs.Remove(grbldMsg) } else { failedMsgs = append(failedMsgs, grbldMsg) diff --git a/network/message/garbled_test.go b/network/message/garbled_test.go index ab4919f16d674ea10a609ca6d0e46a442259789f..d254c56c48329187955596c8367b106fab77cc08 100644 --- a/network/message/garbled_test.go +++ b/network/message/garbled_test.go @@ -7,6 +7,7 @@ import ( "gitlab.com/elixxir/client/network/gateway" "gitlab.com/elixxir/client/network/internal" "gitlab.com/elixxir/client/network/message/parse" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/client/storage" "gitlab.com/elixxir/client/switchboard" "gitlab.com/elixxir/comms/client" @@ -120,8 +121,8 @@ func TestManager_CheckGarbledMessages(t *testing.T) { encryptedMsg := key.Encrypt(msg) i.Session.GetGarbledMessages().Add(encryptedMsg) - quitch := make(chan struct{}) - go m.processGarbledMessages(quitch) + stop := stoppable.NewSingle("stop") + go m.processGarbledMessages(stop) m.CheckGarbledMessages() diff --git a/network/message/handler.go b/network/message/handler.go index b8892cff56f51009558998fcd74b1fa027c4a712..060332e8cb982744e9925c539ac455a499bf2e18 100644 --- a/network/message/handler.go +++ b/network/message/handler.go @@ -10,23 +10,24 @@ package message import ( jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/interfaces/message" - "gitlab.com/elixxir/client/storage/reception" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/crypto/e2e" fingerprint2 "gitlab.com/elixxir/crypto/fingerprint" "gitlab.com/elixxir/primitives/format" + "gitlab.com/elixxir/primitives/states" "gitlab.com/xx_network/primitives/id" "time" ) -func (m *Manager) handleMessages(quitCh <-chan struct{}) { - done := false - for !done { +func (m *Manager) handleMessages(stop *stoppable.Single) { + for { select { - case <-quitCh: - done = true + case <-stop.Quit(): + stop.ToStopped() + return case bundle := <-m.messageReception: for _, msg := range bundle.Messages { - m.handleMessage(msg, bundle.Identity) + m.handleMessage(msg, bundle) } bundle.Finish() } @@ -34,10 +35,11 @@ func (m *Manager) handleMessages(quitCh <-chan struct{}) { } -func (m *Manager) handleMessage(ecrMsg format.Message, identity reception.IdentityUse) { +func (m *Manager) handleMessage(ecrMsg format.Message, bundle Bundle) { // We've done all the networking, now process the message fingerprint := ecrMsg.GetKeyFP() msgDigest := ecrMsg.Digest() + identity := bundle.Identity e2eKv := m.Session.E2e() @@ -90,18 +92,16 @@ func (m *Manager) handleMessage(ecrMsg format.Message, identity reception.Identi // if it doesnt match any form of encrypted, hear it as a raw message // and add it to garbled messages to be handled later msg = ecrMsg - if err != nil { - jww.DEBUG.Printf("Failed to unmarshal ephemeral ID "+ - "on unknown message: %+v", err) - } raw := message.Receive{ - Payload: msg.Marshal(), - MessageType: message.Raw, - Sender: &id.ID{}, - EphemeralID: identity.EphId, - Timestamp: time.Time{}, - Encryption: message.None, - RecipientID: identity.Source, + Payload: msg.Marshal(), + MessageType: message.Raw, + Sender: &id.ID{}, + EphemeralID: identity.EphId, + Timestamp: time.Time{}, + Encryption: message.None, + RecipientID: identity.Source, + RoundId: id.Round(bundle.RoundInfo.ID), + RoundTimestamp: time.Unix(0, int64(bundle.RoundInfo.Timestamps[states.QUEUED])), } jww.INFO.Printf("Garbled/RAW Message: keyFP: %v, msgDigest: %s", msg.GetKeyFP(), msg.Digest()) @@ -124,6 +124,8 @@ func (m *Manager) handleMessage(ecrMsg format.Message, identity reception.Identi xxMsg.RecipientID = identity.Source xxMsg.EphemeralID = identity.EphId xxMsg.Encryption = encTy + xxMsg.RoundId = id.Round(bundle.RoundInfo.ID) + xxMsg.RoundTimestamp = time.Unix(0, int64(bundle.RoundInfo.Timestamps[states.QUEUED])) if xxMsg.MessageType == message.Raw { jww.WARN.Panicf("Recieved a message of type 'Raw' from %s."+ "Message Ignored, 'Raw' is a reserved type. Message supressed.", diff --git a/network/message/manager.go b/network/message/manager.go index 7728910aa7e62186c682dcb97b297cb470dcac58..d5bf21a10db947a10f6566c93d005481770789ef 100644 --- a/network/message/manager.go +++ b/network/message/manager.go @@ -58,19 +58,19 @@ func (m *Manager) StartProcessies() stoppable.Stoppable { //create the message handler workers for i := uint(0); i < m.param.MessageReceptionWorkerPoolSize; i++ { stop := stoppable.NewSingle(fmt.Sprintf("MessageReception Worker %v", i)) - go m.handleMessages(stop.Quit()) + go m.handleMessages(stop) multi.Add(stop) } //create the critical messages thread critStop := stoppable.NewSingle("CriticalMessages") - go m.processCriticalMessages(critStop.Quit()) + go m.processCriticalMessages(critStop) m.Health.AddChannel(m.networkIsHealthy) multi.Add(critStop) //create the garbled messages thread garbledStop := stoppable.NewSingle("GarbledMessages") - go m.processGarbledMessages(garbledStop.Quit()) + go m.processGarbledMessages(garbledStop) multi.Add(garbledStop) return multi diff --git a/network/message/sendCmix.go b/network/message/sendCmix.go index 8f55fcf56305a239a985bf5834a398b4b3d5ebf1..1c1e7c1f3d3b0af1e55d549c156d93d82a5a6b40 100644 --- a/network/message/sendCmix.go +++ b/network/message/sendCmix.go @@ -13,37 +13,30 @@ import ( jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/network/gateway" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/client/storage" pb "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/comms/network" "gitlab.com/elixxir/crypto/fastRNG" - "gitlab.com/elixxir/crypto/fingerprint" "gitlab.com/elixxir/primitives/format" - "gitlab.com/elixxir/primitives/states" "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id/ephemeral" "gitlab.com/xx_network/primitives/netTime" "strings" - "time" ) -// interface for SendCMIX comms; allows mocking this in testing -type sendCmixCommsInterface interface { - SendPutMessage(host *connect.Host, message *pb.GatewaySlot) (*pb.GatewaySlotResponse, error) -} - -// 1.5 seconds -const sendTimeBuffer = 2500 * time.Millisecond - // WARNING: Potentially Unsafe // Public manager function to send a message over CMIX -func (m *Manager) SendCMIX(sender *gateway.Sender, msg format.Message, recipient *id.ID, param params.CMIX) (id.Round, ephemeral.Id, error) { +func (m *Manager) SendCMIX(sender *gateway.Sender, msg format.Message, + recipient *id.ID, param params.CMIX, stop *stoppable.Single) (id.Round, ephemeral.Id, error) { msgCopy := msg.Copy() - return sendCmixHelper(sender, msgCopy, recipient, m.param, param, m.Instance, m.Session, m.nodeRegistration, m.Rng, m.TransmissionID, m.Comms) + return sendCmixHelper(sender, msgCopy, recipient, param, m.Instance, + m.Session, m.nodeRegistration, m.Rng, m.TransmissionID, m.Comms, stop) } -// Payloads send are not End to End encrypted, MetaData is NOT protected with +// Helper function for sendCmix +// NOTE: Payloads send are not End to End encrypted, MetaData is NOT protected with // this call, see SendE2E for End to End encryption and full privacy protection // Internal SendCmix which bypasses the network check, will attempt to send to // the network without checking state. It has a built in retry system which can @@ -51,9 +44,11 @@ func (m *Manager) SendCMIX(sender *gateway.Sender, msg format.Message, recipient // If the message is successfully sent, the id of the round sent it is returned, // which can be registered with the network instance to get a callback on // its status -func sendCmixHelper(sender *gateway.Sender, msg format.Message, recipient *id.ID, messageParams params.Messages, cmixParams params.CMIX, instance *network.Instance, - session *storage.Session, nodeRegistration chan network.NodeGateway, rng *fastRNG.StreamGenerator, senderId *id.ID, - comms sendCmixCommsInterface) (id.Round, ephemeral.Id, error) { +func sendCmixHelper(sender *gateway.Sender, msg format.Message, + recipient *id.ID, cmixParams params.CMIX, instance *network.Instance, + session *storage.Session, nodeRegistration chan network.NodeGateway, + rng *fastRNG.StreamGenerator, senderId *id.ID, comms sendCmixCommsInterface, + stop *stoppable.Single) (id.Round, ephemeral.Id, error) { timeStart := netTime.Now() attempted := set.New() @@ -62,7 +57,7 @@ func sendCmixHelper(sender *gateway.Sender, msg format.Message, recipient *id.ID "(msgDigest: %s)", recipient, msg.Digest()) for numRoundTries := uint(0); numRoundTries < cmixParams.RoundTries; numRoundTries++ { - elapsed := netTime.Now().Sub(timeStart) + elapsed := netTime.Since(timeStart) if elapsed > cmixParams.Timeout { jww.INFO.Printf("No rounds to send to %s (msgDigest: %s) "+ @@ -78,7 +73,10 @@ func sendCmixHelper(sender *gateway.Sender, msg format.Message, recipient *id.ID remainingTime := cmixParams.Timeout - elapsed //find the best round to send to, excluding attempted rounds - bestRound, _ := instance.GetWaitingRounds().GetUpcomingRealtime(remainingTime, attempted, sendTimeBuffer) + bestRound, err := instance.GetWaitingRounds().GetUpcomingRealtime(remainingTime, attempted, sendTimeBuffer) + if err != nil { + jww.WARN.Printf("Failed to GetUpcomingRealtime (msgDigest: %s): %+v", msg.Digest(), err) + } if bestRound == nil { continue } @@ -86,89 +84,24 @@ func sendCmixHelper(sender *gateway.Sender, msg format.Message, recipient *id.ID //add the round on to the list of attempted so it is not tried again attempted.Insert(bestRound) - //set the ephemeral ID - ephID, _, _, err := ephemeral.GetId(recipient, - uint(bestRound.AddressSpaceSize), - int64(bestRound.Timestamps[states.QUEUED])) + // Retrieve host and key information from round + firstGateway, roundKeys, err := processRound(instance, session, nodeRegistration, bestRound, recipient.String(), msg.Digest()) if err != nil { - jww.FATAL.Panicf("Failed to generate ephemeral ID when "+ - "sending to %s (msgDigest: %s): %+v", err, recipient, - msg.Digest()) + jww.WARN.Printf("SendCmix failed to process round (will retry): %v", err) + continue } + // Build the messages to send stream := rng.GetStream() - ephIdFilled, err := ephID.Fill(uint(bestRound.AddressSpaceSize), stream) - if err != nil { - jww.FATAL.Panicf("Failed to obfuscate the ephemeralID when "+ - "sending to %s (msgDigest: %s): %+v", recipient, msg.Digest(), - err) - } - stream.Close() - msg.SetEphemeralRID(ephIdFilled[:]) - - //set the identity fingerprint - ifp := fingerprint.IdentityFP(msg.GetContents(), recipient) - msg.SetIdentityFP(ifp) - - //build the topology - idList, err := id.NewIDListFromBytes(bestRound.Topology) + wrappedMsg, encMsg, ephID, err := buildSlotMessage(msg, recipient, + firstGateway, stream, senderId, bestRound, roundKeys) if err != nil { - jww.ERROR.Printf("Failed to use topology for round %d when "+ - "sending to %s (msgDigest: %s): %+v", bestRound.ID, - recipient, msg.Digest(), err) - continue - } - topology := connect.NewCircuit(idList) - //get they keys for the round, reject if any nodes do not have - //keying relationships - roundKeys, missingKeys := session.Cmix().GetRoundKeys(topology) - if len(missingKeys) > 0 { - jww.WARN.Printf("Failed to send on round %d to %s "+ - "(msgDigest: %s) due to missing relationships with nodes: %s", - bestRound.ID, recipient, msg.Digest(), missingKeys) - go handleMissingNodeKeys(instance, nodeRegistration, missingKeys) - time.Sleep(cmixParams.RetryDelay) - continue + stream.Close() + return 0, ephemeral.Id{}, err } - - //get the gateway to transmit to - firstGateway := topology.GetNodeAtIndex(0).DeepCopy() - firstGateway.SetType(id.Gateway) - - //encrypt the message - stream = rng.GetStream() - salt := make([]byte, 32) - _, err = stream.Read(salt) stream.Close() - if err != nil { - jww.ERROR.Printf("Failed to generate salt when sending to "+ - "%s (msgDigest: %s): %+v", recipient, msg.Digest(), err) - return 0, ephemeral.Id{}, errors.WithMessage(err, - "Failed to generate salt, this should never happen") - } - - encMsg, kmacs := roundKeys.Encrypt(msg, salt, id.Round(bestRound.ID)) - - //build the message payload - msgPacket := &pb.Slot{ - SenderID: senderId.Bytes(), - PayloadA: encMsg.GetPayloadA(), - PayloadB: encMsg.GetPayloadB(), - Salt: salt, - KMACs: kmacs, - } - - //create the wrapper to the gateway - wrappedMsg := &pb.GatewaySlot{ - Message: msgPacket, - RoundID: bestRound.ID, - } - //Add the mac proving ownership - wrappedMsg.MAC = roundKeys.MakeClientGatewayKey(salt, - network.GenerateSlotDigest(wrappedMsg)) - jww.INFO.Printf("Sending to EphID %d (%s) on round %d, "+ "(msgDigest: %s, ecrMsgDigest: %s) via gateway %s", ephID.Int64(), recipient, bestRound.ID, msg.Digest(), @@ -179,47 +112,36 @@ func sendCmixHelper(sender *gateway.Sender, msg format.Message, recipient *id.ID wrappedMsg.Target = target.Marshal() result, err := comms.SendPutMessage(host, wrappedMsg) if err != nil { - if strings.Contains(err.Error(), - "try a different round.") { - jww.WARN.Printf("Failed to send to %s (msgDigest: %s) "+ - "due to round error with round %d, retrying: %+v", - recipient, msg.Digest(), bestRound.ID, err) - return nil, true, err - } else if strings.Contains(err.Error(), - "Could not authenticate client. Is the client registered "+ - "with this node?") { - jww.WARN.Printf("Failed to send to %s (msgDigest: %s) "+ - "via %s due to failed authentication: %s", - recipient, msg.Digest(), firstGateway.String(), err) - //if we failed to send due to the gateway not recognizing our - // authorization, renegotiate with the node to refresh it - nodeID := firstGateway.DeepCopy() - nodeID.SetType(id.Node) - //delete the keys - session.Cmix().Remove(nodeID) - //trigger - go handleMissingNodeKeys(instance, nodeRegistration, []*id.ID{nodeID}) - return nil, true, err + // fixme: should we provide as a slice the whole topology? + warn, err := handlePutMessageError(firstGateway, instance, session, nodeRegistration, recipient.String(), bestRound, err) + if warn { + jww.WARN.Printf("SendCmix Failed: %+v", err) + } else { + return result, false, errors.WithMessagef(err, "SendCmix %s", unrecoverableError) } } return result, false, err } - var result interface{} - if messageParams.ProxySending { - result, err = sender.SendToSpecific(firstGateway, sendFunc) - } else { - result, err = sender.SendToSpecific(firstGateway, sendFunc) + result, err := sender.SendToPreferred([]*id.ID{firstGateway}, sendFunc, stop) + + // Exit if the thread has been stopped + if stoppable.CheckErr(err) { + return 0, ephemeral.Id{}, err } //if the comm errors or the message fails to send, continue retrying. - //return if it sends properly if err != nil { - jww.ERROR.Printf("Failed to send to EphID %d (%s) on "+ - "round %d, trying a new round: %+v", ephID.Int64(), recipient, - bestRound.ID, err) - continue + if !strings.Contains(err.Error(), unrecoverableError) { + jww.ERROR.Printf("SendCmix failed to send to EphID %d (%s) on "+ + "round %d, trying a new round: %+v", ephID.Int64(), recipient, + bestRound.ID, err) + continue + } + + return 0, ephemeral.Id{}, err } + // Return if it sends properly gwSlotResp := result.(*pb.GatewaySlotResponse) if gwSlotResp.Accepted { jww.INFO.Printf("Successfully sent to EphID %v (source: %s) "+ @@ -228,29 +150,10 @@ func sendCmixHelper(sender *gateway.Sender, msg format.Message, recipient *id.ID } else { jww.FATAL.Panicf("Gateway %s returned no error, but failed "+ "to accept message when sending to EphID %d (%s) on round %d", - firstGateway.String(), ephID.Int64(), recipient, bestRound.ID) + firstGateway, ephID.Int64(), recipient, bestRound.ID) } + } return 0, ephemeral.Id{}, errors.New("failed to send the message, " + "unknown error") } - -// Signals to the node registration thread to register a node if keys are -// missing. Identity is triggered automatically when the node is first seen, -// so this should on trigger on rare events. -func handleMissingNodeKeys(instance *network.Instance, - newNodeChan chan network.NodeGateway, nodes []*id.ID) { - for _, n := range nodes { - ng, err := instance.GetNodeAndGateway(n) - if err != nil { - jww.ERROR.Printf("Node contained in round cannot be found: %s", err) - continue - } - select { - case newNodeChan <- ng: - default: - jww.ERROR.Printf("Failed to send node registration for %s", n) - } - - } -} diff --git a/network/message/sendCmixUtils.go b/network/message/sendCmixUtils.go new file mode 100644 index 0000000000000000000000000000000000000000..d3f2c74a01725e191403748fb51608ccf36b7d8c --- /dev/null +++ b/network/message/sendCmixUtils.go @@ -0,0 +1,231 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package message + +import ( + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/client/storage" + "gitlab.com/elixxir/client/storage/cmix" + pb "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/comms/network" + "gitlab.com/elixxir/crypto/fastRNG" + "gitlab.com/elixxir/crypto/fingerprint" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/elixxir/primitives/states" + "gitlab.com/xx_network/comms/connect" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + "strconv" + "strings" + "time" +) + +// Interface for SendCMIX comms; allows mocking this in testing. +type sendCmixCommsInterface interface { + SendPutMessage(host *connect.Host, message *pb.GatewaySlot) (*pb.GatewaySlotResponse, error) + SendPutManyMessages(host *connect.Host, messages *pb.GatewaySlots) (*pb.GatewaySlotResponse, error) +} + +// how much in the future a round needs to be to send to it +const sendTimeBuffer = 1000 * time.Millisecond +const unrecoverableError = "failed with an unrecoverable error" + +// handlePutMessageError handles errors received from a PutMessage or a +// PutManyMessage network call. A printable error will be returned giving more +// context. If the error is not among recoverable errors, then the recoverable +// boolean will be returned false. If the error is among recoverable errors, +// then the boolean will return true. +func handlePutMessageError(firstGateway *id.ID, instance *network.Instance, + session *storage.Session, nodeRegistration chan network.NodeGateway, + recipientString string, bestRound *pb.RoundInfo, + err error) (recoverable bool, returnErr error) { + + // If the comm errors or the message fails to send, then continue retrying; + // otherwise, return if it sends properly + if strings.Contains(err.Error(), "try a different round.") { + return true, errors.WithMessagef(err, "Failed to send to [%s] due to "+ + "round error with round %d, retrying...", + recipientString, bestRound.ID) + } else if strings.Contains(err.Error(), "Could not authenticate client. "+ + "Is the client registered with this node?") { + // If send failed due to the gateway not recognizing the authorization, + // then renegotiate with the node to refresh it + nodeID := firstGateway.DeepCopy() + nodeID.SetType(id.Node) + + // Delete the keys + session.Cmix().Remove(nodeID) + + // Trigger + go handleMissingNodeKeys(instance, nodeRegistration, []*id.ID{nodeID}) + + return true, errors.WithMessagef(err, "Failed to send to [%s] via %s "+ + "due to failed authentication, retrying...", + recipientString, firstGateway) + } + + return false, errors.WithMessage(err, "Failed to put cmix message") + +} + +// processRound is a helper function that determines the gateway to send to for +// a round and retrieves the round keys. +func processRound(instance *network.Instance, session *storage.Session, + nodeRegistration chan network.NodeGateway, bestRound *pb.RoundInfo, + recipientString, messageDigest string) (*id.ID, *cmix.RoundKeys, error) { + + // Build the topology + idList, err := id.NewIDListFromBytes(bestRound.Topology) + if err != nil { + return nil, nil, errors.WithMessagef(err, "Failed to use topology for "+ + "round %d when sending to [%s] (msgDigest(s): %s)", + bestRound.ID, recipientString, messageDigest) + } + topology := connect.NewCircuit(idList) + + // Get the keys for the round, reject if any nodes do not have keying + // relationships + roundKeys, missingKeys := session.Cmix().GetRoundKeys(topology) + if len(missingKeys) > 0 { + go handleMissingNodeKeys(instance, nodeRegistration, missingKeys) + + return nil, nil, errors.Errorf("Failed to send on round %d to [%s] "+ + "(msgDigest(s): %s) due to missing relationships with nodes: %s", + bestRound.ID, recipientString, messageDigest, missingKeys) + } + + // Get the gateway to transmit to + firstGateway := topology.GetNodeAtIndex(0).DeepCopy() + firstGateway.SetType(id.Gateway) + + return firstGateway, roundKeys, nil +} + +// buildSlotMessage is a helper function which forms a slotted message to send +// to a gateway. It encrypts passed in message and generates an ephemeral ID for +// the recipient. +func buildSlotMessage(msg format.Message, recipient *id.ID, target *id.ID, + stream *fastRNG.Stream, senderId *id.ID, bestRound *pb.RoundInfo, + roundKeys *cmix.RoundKeys) (*pb.GatewaySlot, format.Message, ephemeral.Id, + error) { + + // Set the ephemeral ID + ephID, _, _, err := ephemeral.GetId(recipient, + uint(bestRound.AddressSpaceSize), + int64(bestRound.Timestamps[states.QUEUED])) + if err != nil { + jww.FATAL.Panicf("Failed to generate ephemeral ID when sending to %s "+ + "(msgDigest: %s): %+v", err, recipient, msg.Digest()) + } + + ephIdFilled, err := ephID.Fill(uint(bestRound.AddressSpaceSize), stream) + if err != nil { + jww.FATAL.Panicf("Failed to obfuscate the ephemeralID when sending "+ + "to %s (msgDigest: %s): %+v", recipient, msg.Digest(), err) + } + + msg.SetEphemeralRID(ephIdFilled[:]) + + // Set the identity fingerprint + ifp := fingerprint.IdentityFP(msg.GetContents(), recipient) + + msg.SetIdentityFP(ifp) + + // Encrypt the message + salt := make([]byte, 32) + _, err = stream.Read(salt) + if err != nil { + jww.ERROR.Printf("Failed to generate salt when sending to %s "+ + "(msgDigest: %s): %+v", recipient, msg.Digest(), err) + return nil, format.Message{}, ephemeral.Id{}, errors.WithMessage(err, + "Failed to generate salt, this should never happen") + } + + encMsg, kmacs := roundKeys.Encrypt(msg, salt, id.Round(bestRound.ID)) + + // Build the message payload + msgPacket := &pb.Slot{ + SenderID: senderId.Bytes(), + PayloadA: encMsg.GetPayloadA(), + PayloadB: encMsg.GetPayloadB(), + Salt: salt, + KMACs: kmacs, + } + + // Create the wrapper to the gateway + slot := &pb.GatewaySlot{ + Message: msgPacket, + RoundID: bestRound.ID, + Target: target.Bytes(), + } + + // Add the mac proving ownership + slot.MAC = roundKeys.MakeClientGatewayKey(salt, + network.GenerateSlotDigest(slot)) + + return slot, encMsg, ephID, nil +} + +// handleMissingNodeKeys signals to the node registration thread to register a +// node if keys are missing. Identity is triggered automatically when the node +// is first seen, so this should on trigger on rare events. +func handleMissingNodeKeys(instance *network.Instance, + newNodeChan chan network.NodeGateway, nodes []*id.ID) { + for _, n := range nodes { + ng, err := instance.GetNodeAndGateway(n) + if err != nil { + jww.ERROR.Printf("Node contained in round cannot be found: %s", err) + continue + } + + select { + case newNodeChan <- ng: + default: + jww.ERROR.Printf("Failed to send node registration for %s", n) + } + + } +} + +// messageMapToStrings serializes a map of IDs and messages into a string of IDs +// and a string of message digests. Intended for use in printing to logs. +func messageMapToStrings(msgList map[id.ID]format.Message) (string, string) { + idStrings := make([]string, 0, len(msgList)) + msgDigests := make([]string, 0, len(msgList)) + for uid, msg := range msgList { + idStrings = append(idStrings, uid.String()) + msgDigests = append(msgDigests, msg.Digest()) + } + + return strings.Join(idStrings, ","), strings.Join(msgDigests, ",") +} + +// messagesToDigestString serializes a list of messages into a string of message +// digests. Intended for use in printing to the logs. +func messagesToDigestString(msgs []format.Message) string { + msgDigests := make([]string, 0, len(msgs)) + for _, msg := range msgs { + msgDigests = append(msgDigests, msg.Digest()) + } + + return strings.Join(msgDigests, ",") +} + +// ephemeralIdListToString serializes a list of ephemeral IDs into a human- +// readable format. Intended for use in printing to logs. +func ephemeralIdListToString(idList []ephemeral.Id) string { + idStrings := make([]string, 0, len(idList)) + + for i := 0; i < len(idList); i++ { + ephIdStr := strconv.FormatInt(idList[i].Int64(), 10) + idStrings = append(idStrings, ephIdStr) + } + + return strings.Join(idStrings, ",") +} diff --git a/network/message/sendCmix_test.go b/network/message/sendCmix_test.go index f3182a8a2407c4f29c1c615160a28fd793ab4ee6..28acd56525e7393500819539561cda4f8160ba07 100644 --- a/network/message/sendCmix_test.go +++ b/network/message/sendCmix_test.go @@ -18,47 +18,15 @@ import ( "gitlab.com/elixxir/crypto/fastRNG" "gitlab.com/elixxir/primitives/format" "gitlab.com/elixxir/primitives/states" - "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/crypto/csprng" "gitlab.com/xx_network/crypto/large" "gitlab.com/xx_network/primitives/id" - "gitlab.com/xx_network/primitives/ndf" "gitlab.com/xx_network/primitives/netTime" "testing" "time" ) -type MockSendCMIXComms struct { - t *testing.T -} - -func (mc *MockSendCMIXComms) GetHost(hostId *id.ID) (*connect.Host, bool) { - nid1 := id.NewIdFromString("zezima", id.Node, mc.t) - gwid := nid1.DeepCopy() - gwid.SetType(id.Gateway) - h, _ := connect.NewHost(gwid, "0.0.0.0", []byte(""), connect.HostParams{ - MaxRetries: 0, - AuthEnabled: false, - }) - return h, true -} - -func (mc *MockSendCMIXComms) AddHost(hid *id.ID, address string, cert []byte, params connect.HostParams) (host *connect.Host, err error) { - host, _ = mc.GetHost(nil) - return host, nil -} - -func (mc *MockSendCMIXComms) RemoveHost(hid *id.ID) { - -} - -func (mc *MockSendCMIXComms) SendPutMessage(host *connect.Host, message *mixmessages.GatewaySlot) (*mixmessages.GatewaySlotResponse, error) { - return &mixmessages.GatewaySlotResponse{ - Accepted: true, - RoundID: 3, - }, nil -} - +// Unit test func Test_attemptSendCmix(t *testing.T) { sess1 := storage.InitTestingSession(t) @@ -143,68 +111,12 @@ func Test_attemptSendCmix(t *testing.T) { msgCmix := format.NewMessage(m.Session.Cmix().GetGroup().GetP().ByteLen()) msgCmix.SetContents([]byte("test")) e2e.SetUnencrypted(msgCmix, m.Session.User().GetCryptographicIdentity().GetTransmissionID()) - _, _, err = sendCmixHelper(sender, msgCmix, sess2.GetUser().ReceptionID, params.GetDefaultMessage(), params.GetDefaultCMIX(), - m.Instance, m.Session, m.nodeRegistration, m.Rng, - m.TransmissionID, &MockSendCMIXComms{t: t}) + _, _, err = sendCmixHelper(sender, msgCmix, sess2.GetUser().ReceptionID, + params.GetDefaultCMIX(), m.Instance, m.Session, m.nodeRegistration, + m.Rng, m.TransmissionID, &MockSendCMIXComms{t: t}, nil) if err != nil { t.Errorf("Failed to sendcmix: %+v", err) panic("t") return } } - -func getNDF() *ndf.NetworkDefinition { - nodeId := id.NewIdFromString("zezima", id.Node, &testing.T{}) - gwId := nodeId.DeepCopy() - gwId.SetType(id.Gateway) - return &ndf.NetworkDefinition{ - E2E: ndf.Group{ - Prime: "E2EE983D031DC1DB6F1A7A67DF0E9A8E5561DB8E8D49413394C049B" + - "7A8ACCEDC298708F121951D9CF920EC5D146727AA4AE535B0922C688B55B3DD2AE" + - "DF6C01C94764DAB937935AA83BE36E67760713AB44A6337C20E7861575E745D31F" + - "8B9E9AD8412118C62A3E2E29DF46B0864D0C951C394A5CBBDC6ADC718DD2A3E041" + - "023DBB5AB23EBB4742DE9C1687B5B34FA48C3521632C4A530E8FFB1BC51DADDF45" + - "3B0B2717C2BC6669ED76B4BDD5C9FF558E88F26E5785302BEDBCA23EAC5ACE9209" + - "6EE8A60642FB61E8F3D24990B8CB12EE448EEF78E184C7242DD161C7738F32BF29" + - "A841698978825B4111B4BC3E1E198455095958333D776D8B2BEEED3A1A1A221A6E" + - "37E664A64B83981C46FFDDC1A45E3D5211AAF8BFBC072768C4F50D7D7803D2D4F2" + - "78DE8014A47323631D7E064DE81C0C6BFA43EF0E6998860F1390B5D3FEACAF1696" + - "015CB79C3F9C2D93D961120CD0E5F12CBB687EAB045241F96789C38E89D796138E" + - "6319BE62E35D87B1048CA28BE389B575E994DCA755471584A09EC723742DC35873" + - "847AEF49F66E43873", - Generator: "2", - }, - CMIX: ndf.Group{ - Prime: "9DB6FB5951B66BB6FE1E140F1D2CE5502374161FD6538DF1648218642F0B5C48" + - "C8F7A41AADFA187324B87674FA1822B00F1ECF8136943D7C55757264E5A1A44F" + - "FE012E9936E00C1D3E9310B01C7D179805D3058B2A9F4BB6F9716BFE6117C6B5" + - "B3CC4D9BE341104AD4A80AD6C94E005F4B993E14F091EB51743BF33050C38DE2" + - "35567E1B34C3D6A5C0CEAA1A0F368213C3D19843D0B4B09DCB9FC72D39C8DE41" + - "F1BF14D4BB4563CA28371621CAD3324B6A2D392145BEBFAC748805236F5CA2FE" + - "92B871CD8F9C36D3292B5509CA8CAA77A2ADFC7BFD77DDA6F71125A7456FEA15" + - "3E433256A2261C6A06ED3693797E7995FAD5AABBCFBE3EDA2741E375404AE25B", - Generator: "5C7FF6B06F8F143FE8288433493E4769C4D988ACE5BE25A0E24809670716C613" + - "D7B0CEE6932F8FAA7C44D2CB24523DA53FBE4F6EC3595892D1AA58C4328A06C4" + - "6A15662E7EAA703A1DECF8BBB2D05DBE2EB956C142A338661D10461C0D135472" + - "085057F3494309FFA73C611F78B32ADBB5740C361C9F35BE90997DB2014E2EF5" + - "AA61782F52ABEB8BD6432C4DD097BC5423B285DAFB60DC364E8161F4A2A35ACA" + - "3A10B1C4D203CC76A470A33AFDCBDD92959859ABD8B56E1725252D78EAC66E71" + - "BA9AE3F1DD2487199874393CD4D832186800654760E1E34C09E4D155179F9EC0" + - "DC4473F996BDCE6EED1CABED8B6F116F7AD9CF505DF0F998E34AB27514B0FFE7", - }, - Gateways: []ndf.Gateway{ - { - ID: gwId.Marshal(), - Address: "0.0.0.0", - TlsCertificate: "", - }, - }, - Nodes: []ndf.Node{ - { - ID: nodeId.Marshal(), - Address: "0.0.0.0", - TlsCertificate: "", - }, - }, - } -} diff --git a/network/message/sendE2E.go b/network/message/sendE2E.go index b09e96c4a19598e7724ccf0bd5781ff60745b3d1..7b468ad1a30c818396d631b4b9f85b5945dd88ab 100644 --- a/network/message/sendE2E.go +++ b/network/message/sendE2E.go @@ -13,6 +13,7 @@ import ( "gitlab.com/elixxir/client/interfaces/message" "gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/keyExchange" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/crypto/e2e" "gitlab.com/elixxir/primitives/format" "gitlab.com/xx_network/primitives/id" @@ -21,7 +22,8 @@ import ( "time" ) -func (m *Manager) SendE2E(msg message.Send, param params.E2E) ([]id.Round, e2e.MessageID, error) { +func (m *Manager) SendE2E(msg message.Send, param params.E2E, + stop *stoppable.Single) ([]id.Round, e2e.MessageID, error) { if msg.MessageType == message.Raw { return nil, e2e.MessageID{}, errors.Errorf("Raw (%d) is a reserved "+ "message type", msg.MessageType) @@ -58,7 +60,7 @@ func (m *Manager) SendE2E(msg message.Send, param params.E2E) ([]id.Round, e2e.M if msg.MessageType != message.KeyExchangeTrigger { // check if any rekeys need to happen and trigger them keyExchange.CheckKeyExchanges(m.Instance, m.SendE2E, - m.Session, partner, 1*time.Minute) + m.Session, partner, 1*time.Minute, stop) } //create the cmix message @@ -96,7 +98,7 @@ func (m *Manager) SendE2E(msg message.Send, param params.E2E) ([]id.Round, e2e.M go func(i int) { var err error roundIds[i], _, err = m.SendCMIX(m.sender, msgEnc, msg.Recipient, - param.CMIX) + param.CMIX, stop) if err != nil { errCh <- err } diff --git a/network/message/sendManyCmix.go b/network/message/sendManyCmix.go new file mode 100644 index 0000000000000000000000000000000000000000..d9e098715a5032c90ed2bef7438f15ce2714f890 --- /dev/null +++ b/network/message/sendManyCmix.go @@ -0,0 +1,184 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package message + +import ( + "github.com/golang-collections/collections/set" + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/client/interfaces/params" + "gitlab.com/elixxir/client/network/gateway" + "gitlab.com/elixxir/client/storage" + pb "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/comms/network" + "gitlab.com/elixxir/crypto/fastRNG" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/xx_network/comms/connect" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + "gitlab.com/xx_network/primitives/netTime" + "strings" +) + +// SendManyCMIX sends many "raw" cMix message payloads to each of the provided +// recipients. Used to send messages in group chats. Metadata is NOT protected +// with this call and can leak data about yourself. Returns the round ID of the +// round the payload was sent or an error if it fails. +// WARNING: Potentially Unsafe +func (m *Manager) SendManyCMIX(sender *gateway.Sender, + messages map[id.ID]format.Message, p params.CMIX) (id.Round, []ephemeral.Id, + error) { + + // Create message copies + messagesCopy := make(map[id.ID]format.Message, len(messages)) + for rid, msg := range messages { + messagesCopy[rid] = msg.Copy() + } + + return sendManyCmixHelper(sender, messagesCopy, p, m.Instance, m.Session, + m.nodeRegistration, m.Rng, m.TransmissionID, m.Comms) +} + +// sendManyCmixHelper is a helper function for Manager.SendManyCMIX. +// +// NOTE: Payloads sent are not end to end encrypted, metadata is NOT protected +// with this call; see SendE2E for end to end encryption and full privacy +// protection. Internal SendManyCMIX, which bypasses the network check, will +// attempt to send to the network without checking state. It has a built in +// retry system which can be configured through the params object. +// +// If the message is successfully sent, the ID of the round sent it is returned, +// which can be registered with the network instance to get a callback on its +// status. +func sendManyCmixHelper(sender *gateway.Sender, msgs map[id.ID]format.Message, + param params.CMIX, instance *network.Instance, session *storage.Session, + nodeRegistration chan network.NodeGateway, rng *fastRNG.StreamGenerator, + senderId *id.ID, comms sendCmixCommsInterface) (id.Round, []ephemeral.Id, error) { + + timeStart := netTime.Now() + attempted := set.New() + stream := rng.GetStream() + defer stream.Close() + + recipientString, msgDigests := messageMapToStrings(msgs) + + jww.INFO.Printf("Looking for round to send cMix messages to [%s] "+ + "(msgDigest: %s)", recipientString, msgDigests) + + for numRoundTries := uint(0); numRoundTries < param.RoundTries; numRoundTries++ { + elapsed := netTime.Since(timeStart) + + if elapsed > param.Timeout { + jww.INFO.Printf("No rounds to send to %s (msgDigest: %s) were found "+ + "before timeout %s", recipientString, msgDigests, param.Timeout) + return 0, []ephemeral.Id{}, + errors.New("sending cMix message timed out") + } + + if numRoundTries > 0 { + jww.INFO.Printf("Attempt %d to find round to send message to %s "+ + "(msgDigest: %s)", numRoundTries+1, recipientString, msgDigests) + } + + remainingTime := param.Timeout - elapsed + + // Find the best round to send to, excluding attempted rounds + bestRound, _ := instance.GetWaitingRounds().GetUpcomingRealtime( + remainingTime, attempted, sendTimeBuffer) + if bestRound == nil { + continue + } + + // Add the round on to the list of attempted so it is not tried again + attempted.Insert(bestRound) + + // Retrieve host and key information from round + firstGateway, roundKeys, err := processRound(instance, session, + nodeRegistration, bestRound, recipientString, msgDigests) + if err != nil { + jww.WARN.Printf("SendManyCMIX failed to process round %d "+ + "(will retry): %+v", bestRound.ID, err) + continue + } + + // Build a slot for every message and recipient + slots := make([]*pb.GatewaySlot, len(msgs)) + ephemeralIds := make([]ephemeral.Id, len(msgs)) + encMsgs := make([]format.Message, len(msgs)) + i := 0 + for recipient, msg := range msgs { + slots[i], encMsgs[i], ephemeralIds[i], err = buildSlotMessage( + msg, &recipient, firstGateway, stream, senderId, bestRound, roundKeys) + if err != nil { + return 0, []ephemeral.Id{}, errors.Errorf("failed to build "+ + "slot message for %s: %+v", recipient, err) + } + i++ + } + + // Serialize lists into a printable format + ephemeralIdsString := ephemeralIdListToString(ephemeralIds) + encMsgsDigest := messagesToDigestString(encMsgs) + + jww.INFO.Printf("Sending to EphIDs [%s] (%s) on round %d, "+ + "(msgDigest: %s, ecrMsgDigest: %s) via gateway %s", + ephemeralIdsString, recipientString, bestRound.ID, msgDigests, + encMsgsDigest, firstGateway) + + // Wrap slots in the proper message type + wrappedMessage := &pb.GatewaySlots{ + Messages: slots, + RoundID: bestRound.ID, + } + + // Send the payload + sendFunc := func(host *connect.Host, target *id.ID) (interface{}, bool, error) { + wrappedMessage.Target = target.Marshal() + result, err := comms.SendPutManyMessages(host, wrappedMessage) + if err != nil { + warn, err := handlePutMessageError(firstGateway, instance, + session, nodeRegistration, recipientString, bestRound, err) + if warn { + jww.WARN.Printf("SendManyCMIX Failed: %+v", err) + } else { + return result, false, errors.WithMessagef(err, + "SendManyCMIX %s", unrecoverableError) + } + } + return result, false, err + } + result, err := sender.SendToPreferred([]*id.ID{firstGateway}, sendFunc, nil) + + // If the comm errors or the message fails to send, continue retrying + if err != nil { + if !strings.Contains(err.Error(), unrecoverableError) { + jww.ERROR.Printf("SendManyCMIX failed to send to EphIDs [%s] "+ + "(sources: %s) on round %d, trying a new round %+v", + ephemeralIdsString, recipientString, bestRound.ID, err) + continue + } + + return 0, []ephemeral.Id{}, err + } + + // Return if it sends properly + gwSlotResp := result.(*pb.GatewaySlotResponse) + if gwSlotResp.Accepted { + jww.INFO.Printf("Successfully sent to EphIDs %v (sources: [%s]) in "+ + "round %d", ephemeralIdsString, recipientString, bestRound.ID) + return id.Round(bestRound.ID), ephemeralIds, nil + } else { + jww.FATAL.Panicf("Gateway %s returned no error, but failed to "+ + "accept message when sending to EphIDs [%s] (%s) on round %d", + firstGateway, ephemeralIdsString, recipientString, bestRound.ID) + } + } + + return 0, []ephemeral.Id{}, + errors.New("failed to send the message, unknown error") +} diff --git a/network/message/sendManyCmix_test.go b/network/message/sendManyCmix_test.go new file mode 100644 index 0000000000000000000000000000000000000000..2d07cf456e4177af469a44360132590bb4fc62d3 --- /dev/null +++ b/network/message/sendManyCmix_test.go @@ -0,0 +1,134 @@ +package message + +import ( + "github.com/pkg/errors" + "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/interfaces/params" + "gitlab.com/elixxir/client/network/gateway" + "gitlab.com/elixxir/client/network/internal" + "gitlab.com/elixxir/client/storage" + "gitlab.com/elixxir/client/switchboard" + "gitlab.com/elixxir/comms/client" + "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/comms/network" + ds "gitlab.com/elixxir/comms/network/dataStructures" + "gitlab.com/elixxir/comms/testutils" + "gitlab.com/elixxir/crypto/cyclic" + "gitlab.com/elixxir/crypto/e2e" + "gitlab.com/elixxir/crypto/fastRNG" + "gitlab.com/elixxir/primitives/format" + "gitlab.com/elixxir/primitives/states" + "gitlab.com/xx_network/crypto/csprng" + "gitlab.com/xx_network/crypto/large" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/netTime" + "testing" + "time" +) + +// Unit test +func Test_attemptSendManyCmix(t *testing.T) { + sess1 := storage.InitTestingSession(t) + + numRecipients := 3 + recipients := make([]*id.ID, numRecipients) + sw := switchboard.New() + l := TestListener{ + ch: make(chan bool), + } + for i := 0; i < numRecipients; i++ { + sess := storage.InitTestingSession(t) + sw.RegisterListener(sess.GetUser().TransmissionID, message.Raw, l) + recipients[i] = sess.GetUser().ReceptionID + } + + comms, err := client.NewClientComms(sess1.GetUser().TransmissionID, nil, nil, nil) + if err != nil { + t.Errorf("Failed to start client comms: %+v", err) + } + inst, err := network.NewInstanceTesting(comms.ProtoComms, getNDF(), nil, nil, nil, t) + if err != nil { + t.Errorf("Failed to start instance: %+v", err) + } + now := netTime.Now() + nid1 := id.NewIdFromString("zezima", id.Node, t) + nid2 := id.NewIdFromString("jakexx360", id.Node, t) + nid3 := id.NewIdFromString("westparkhome", id.Node, t) + grp := cyclic.NewGroup(large.NewInt(7), large.NewInt(13)) + sess1.Cmix().Add(nid1, grp.NewInt(1)) + sess1.Cmix().Add(nid2, grp.NewInt(2)) + sess1.Cmix().Add(nid3, grp.NewInt(3)) + + timestamps := []uint64{ + uint64(now.Add(-30 * time.Second).UnixNano()), // PENDING + uint64(now.Add(-25 * time.Second).UnixNano()), // PRECOMPUTING + uint64(now.Add(-5 * time.Second).UnixNano()), // STANDBY + uint64(now.Add(5 * time.Second).UnixNano()), // QUEUED + 0} // REALTIME + + ri := &mixmessages.RoundInfo{ + ID: 3, + UpdateID: 0, + State: uint32(states.QUEUED), + BatchSize: 0, + Topology: [][]byte{nid1.Marshal(), nid2.Marshal(), nid3.Marshal()}, + Timestamps: timestamps, + Errors: nil, + ClientErrors: nil, + ResourceQueueTimeoutMillis: 0, + Signature: nil, + AddressSpaceSize: 4, + } + + if err = testutils.SignRoundInfoRsa(ri, t); err != nil { + t.Errorf("Failed to sign mock round info: %v", err) + } + + pubKey, err := testutils.LoadPublicKeyTesting(t) + if err != nil { + t.Errorf("Failed to load a key for testing: %v", err) + } + rnd := ds.NewRound(ri, pubKey, nil) + inst.GetWaitingRounds().Insert(rnd) + i := internal.Internal{ + Session: sess1, + Switchboard: sw, + Rng: fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG), + Comms: comms, + Health: nil, + TransmissionID: sess1.GetUser().TransmissionID, + Instance: inst, + NodeRegistration: nil, + } + p := gateway.DefaultPoolParams() + p.MaxPoolSize = 1 + sender, err := gateway.NewSender(p, i.Rng, getNDF(), &MockSendCMIXComms{t: t}, i.Session, nil) + if err != nil { + t.Errorf("%+v", errors.New(err.Error())) + return + } + m := NewManager(i, params.Messages{ + MessageReceptionBuffLen: 20, + MessageReceptionWorkerPoolSize: 20, + MaxChecksGarbledMessage: 20, + GarbledMessageWait: time.Hour, + }, nil, sender) + msgCmix := format.NewMessage(m.Session.Cmix().GetGroup().GetP().ByteLen()) + msgCmix.SetContents([]byte("test")) + e2e.SetUnencrypted(msgCmix, m.Session.User().GetCryptographicIdentity().GetTransmissionID()) + messages := make([]format.Message, numRecipients) + for i := 0; i < numRecipients; i++ { + messages[i] = msgCmix + } + + msgMap := make(map[id.ID]format.Message, numRecipients) + for i := 0; i < numRecipients; i++ { + msgMap[*recipients[i]] = msgCmix + } + + _, _, err = sendManyCmixHelper(sender, msgMap, params.GetDefaultCMIX(), m.Instance, + m.Session, m.nodeRegistration, m.Rng, m.TransmissionID, &MockSendCMIXComms{t: t}) + if err != nil { + t.Errorf("Failed to sendcmix: %+v", err) + } +} diff --git a/network/message/sendUnsafe.go b/network/message/sendUnsafe.go index 19f7d5d5ce0bfe6dd14df77b76f0a21c824b2404..938bcc07c8bab9bd24e1bdd53cb1c38c3b4111c4 100644 --- a/network/message/sendUnsafe.go +++ b/network/message/sendUnsafe.go @@ -64,7 +64,7 @@ func (m *Manager) SendUnsafe(msg message.Send, param params.Unsafe) ([]id.Round, wg.Add(1) go func(i int) { var err error - roundIds[i], _, err = m.SendCMIX(m.sender, msgCmix, msg.Recipient, param.CMIX) + roundIds[i], _, err = m.SendCMIX(m.sender, msgCmix, msg.Recipient, param.CMIX, nil) if err != nil { errCh <- err } diff --git a/network/message/utils_test.go b/network/message/utils_test.go new file mode 100644 index 0000000000000000000000000000000000000000..18284ad0bb6b6693db062f2fb5b048b5e18ca08f --- /dev/null +++ b/network/message/utils_test.go @@ -0,0 +1,106 @@ +package message + +import ( + "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/xx_network/comms/connect" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/ndf" + "testing" +) + +type MockSendCMIXComms struct { + t *testing.T +} + +func (mc *MockSendCMIXComms) GetHost(*id.ID) (*connect.Host, bool) { + nid1 := id.NewIdFromString("zezima", id.Node, mc.t) + gwID := nid1.DeepCopy() + gwID.SetType(id.Gateway) + h, _ := connect.NewHost(gwID, "0.0.0.0", []byte(""), connect.HostParams{ + MaxRetries: 0, + AuthEnabled: false, + }) + return h, true +} + +func (mc *MockSendCMIXComms) AddHost(*id.ID, string, []byte, connect.HostParams) (host *connect.Host, err error) { + host, _ = mc.GetHost(nil) + return host, nil +} + +func (mc *MockSendCMIXComms) RemoveHost(*id.ID) { + +} + +func (mc *MockSendCMIXComms) SendPutMessage(*connect.Host, *mixmessages.GatewaySlot) (*mixmessages.GatewaySlotResponse, error) { + return &mixmessages.GatewaySlotResponse{ + Accepted: true, + RoundID: 3, + }, nil +} + +func (mc *MockSendCMIXComms) SendPutManyMessages(*connect.Host, *mixmessages.GatewaySlots) (*mixmessages.GatewaySlotResponse, error) { + return &mixmessages.GatewaySlotResponse{ + Accepted: true, + RoundID: 3, + }, nil +} + +func getNDF() *ndf.NetworkDefinition { + nodeId := id.NewIdFromString("zezima", id.Node, &testing.T{}) + gwId := nodeId.DeepCopy() + gwId.SetType(id.Gateway) + return &ndf.NetworkDefinition{ + E2E: ndf.Group{ + Prime: "E2EE983D031DC1DB6F1A7A67DF0E9A8E5561DB8E8D49413394C049B7A" + + "8ACCEDC298708F121951D9CF920EC5D146727AA4AE535B0922C688B55B3D" + + "D2AEDF6C01C94764DAB937935AA83BE36E67760713AB44A6337C20E78615" + + "75E745D31F8B9E9AD8412118C62A3E2E29DF46B0864D0C951C394A5CBBDC" + + "6ADC718DD2A3E041023DBB5AB23EBB4742DE9C1687B5B34FA48C3521632C" + + "4A530E8FFB1BC51DADDF453B0B2717C2BC6669ED76B4BDD5C9FF558E88F2" + + "6E5785302BEDBCA23EAC5ACE92096EE8A60642FB61E8F3D24990B8CB12EE" + + "448EEF78E184C7242DD161C7738F32BF29A841698978825B4111B4BC3E1E" + + "198455095958333D776D8B2BEEED3A1A1A221A6E37E664A64B83981C46FF" + + "DDC1A45E3D5211AAF8BFBC072768C4F50D7D7803D2D4F278DE8014A47323" + + "631D7E064DE81C0C6BFA43EF0E6998860F1390B5D3FEACAF1696015CB79C" + + "3F9C2D93D961120CD0E5F12CBB687EAB045241F96789C38E89D796138E63" + + "19BE62E35D87B1048CA28BE389B575E994DCA755471584A09EC723742DC3" + + "5873847AEF49F66E43873", + Generator: "2", + }, + CMIX: ndf.Group{ + Prime: "9DB6FB5951B66BB6FE1E140F1D2CE5502374161FD6538DF1648218642" + + "F0B5C48C8F7A41AADFA187324B87674FA1822B00F1ECF8136943D7C55757" + + "264E5A1A44FFE012E9936E00C1D3E9310B01C7D179805D3058B2A9F4BB6F" + + "9716BFE6117C6B5B3CC4D9BE341104AD4A80AD6C94E005F4B993E14F091E" + + "B51743BF33050C38DE235567E1B34C3D6A5C0CEAA1A0F368213C3D19843D" + + "0B4B09DCB9FC72D39C8DE41F1BF14D4BB4563CA28371621CAD3324B6A2D3" + + "92145BEBFAC748805236F5CA2FE92B871CD8F9C36D3292B5509CA8CAA77A" + + "2ADFC7BFD77DDA6F71125A7456FEA153E433256A2261C6A06ED3693797E7" + + "995FAD5AABBCFBE3EDA2741E375404AE25B", + Generator: "5C7FF6B06F8F143FE8288433493E4769C4D988ACE5BE25A0E2480" + + "9670716C613D7B0CEE6932F8FAA7C44D2CB24523DA53FBE4F6EC3595892D" + + "1AA58C4328A06C46A15662E7EAA703A1DECF8BBB2D05DBE2EB956C142A33" + + "8661D10461C0D135472085057F3494309FFA73C611F78B32ADBB5740C361" + + "C9F35BE90997DB2014E2EF5AA61782F52ABEB8BD6432C4DD097BC5423B28" + + "5DAFB60DC364E8161F4A2A35ACA3A10B1C4D203CC76A470A33AFDCBDD929" + + "59859ABD8B56E1725252D78EAC66E71BA9AE3F1DD2487199874393CD4D83" + + "2186800654760E1E34C09E4D155179F9EC0DC4473F996BDCE6EED1CABED8" + + "B6F116F7AD9CF505DF0F998E34AB27514B0FFE7", + }, + Gateways: []ndf.Gateway{ + { + ID: gwId.Marshal(), + Address: "0.0.0.0", + TlsCertificate: "", + }, + }, + Nodes: []ndf.Node{ + { + ID: nodeId.Marshal(), + Address: "0.0.0.0", + TlsCertificate: "", + }, + }, + } +} diff --git a/network/node/register.go b/network/node/register.go index 82ab2cc3d937245adbc63812d6e875bc63657845..a16e1cd08486b3bd45abd59a3f3061b7cd43a3bd 100644 --- a/network/node/register.go +++ b/network/node/register.go @@ -55,7 +55,8 @@ func StartRegistration(sender *gateway.Sender, session *storage.Session, rngGen return multi } -func registerNodes(sender *gateway.Sender, session *storage.Session, rngGen *fastRNG.StreamGenerator, comms RegisterNodeCommsInterface, +func registerNodes(sender *gateway.Sender, session *storage.Session, + rngGen *fastRNG.StreamGenerator, comms RegisterNodeCommsInterface, stop *stoppable.Single, c chan network.NodeGateway) { u := session.User() regSignature := u.GetTransmissionRegistrationValidationSignature() @@ -67,13 +68,15 @@ func registerNodes(sender *gateway.Sender, session *storage.Session, rngGen *fas rng := rngGen.GetStream() interval := time.Duration(500) * time.Millisecond t := time.NewTicker(interval) - for true { + for { select { case <-stop.Quit(): t.Stop() + stop.ToStopped() return case gw := <-c: - err := registerWithNode(sender, comms, gw, regSignature, regTimestamp, uci, cmix, rng) + err := registerWithNode(sender, comms, gw, regSignature, + regTimestamp, uci, cmix, rng, stop) if err != nil { jww.ERROR.Printf("Failed to register node: %+v", err) } @@ -84,9 +87,10 @@ func registerNodes(sender *gateway.Sender, session *storage.Session, rngGen *fas //registerWithNode serves as a helper for RegisterWithNodes // It registers a user with a specific in the client's ndf. -func registerWithNode(sender *gateway.Sender, comms RegisterNodeCommsInterface, ngw network.NodeGateway, - regSig []byte, registrationTimestampNano int64, uci *user.CryptographicIdentity, - store *cmix.Store, rng csprng.Source) error { +func registerWithNode(sender *gateway.Sender, comms RegisterNodeCommsInterface, + ngw network.NodeGateway, regSig []byte, registrationTimestampNano int64, + uci *user.CryptographicIdentity, store *cmix.Store, rng csprng.Source, + stop *stoppable.Single) error { nodeID, err := ngw.Node.GetNodeId() if err != nil { @@ -122,7 +126,9 @@ func registerWithNode(sender *gateway.Sender, comms RegisterNodeCommsInterface, // keys transmissionHash, _ := hash.NewCMixHash() - nonce, dhPub, err := requestNonce(sender, comms, gatewayID, regSig, registrationTimestampNano, uci, store, rng) + nonce, dhPub, err := requestNonce(sender, comms, gatewayID, regSig, + registrationTimestampNano, uci, store, rng, stop) + if err != nil { return errors.Errorf("Failed to request nonce: %+v", err) } @@ -133,7 +139,8 @@ func registerWithNode(sender *gateway.Sender, comms RegisterNodeCommsInterface, // Confirm received nonce jww.INFO.Printf("Register: Confirming received nonce from node %s", nodeID.String()) err = confirmNonce(sender, comms, uci.GetTransmissionID().Bytes(), - nonce, uci.GetTransmissionRSA(), gatewayID) + nonce, uci.GetTransmissionRSA(), gatewayID, stop) + if err != nil { errMsg := fmt.Sprintf("Register: Unable to confirm nonce: %v", err) return errors.New(errMsg) @@ -151,7 +158,7 @@ func registerWithNode(sender *gateway.Sender, comms RegisterNodeCommsInterface, func requestNonce(sender *gateway.Sender, comms RegisterNodeCommsInterface, gwId *id.ID, regSig []byte, registrationTimestampNano int64, uci *user.CryptographicIdentity, - store *cmix.Store, rng csprng.Source) ([]byte, []byte, error) { + store *cmix.Store, rng csprng.Source, stop *stoppable.Single) ([]byte, []byte, error) { dhPub := store.GetDHPublicKey().Bytes() opts := rsa.NewDefaultOptions() @@ -195,7 +202,8 @@ func requestNonce(sender *gateway.Sender, comms RegisterNodeCommsInterface, gwId return nil, err } return nonceResponse, nil - }) + }, stop) + if err != nil { return nil, nil, err } @@ -208,8 +216,9 @@ func requestNonce(sender *gateway.Sender, comms RegisterNodeCommsInterface, gwId // confirmNonce is a helper for the Register function // It signs a nonce and sends it for confirmation // Returns nil if successful, error otherwise -func confirmNonce(sender *gateway.Sender, comms RegisterNodeCommsInterface, UID, nonce []byte, - privateKeyRSA *rsa.PrivateKey, gwID *id.ID) error { +func confirmNonce(sender *gateway.Sender, comms RegisterNodeCommsInterface, UID, + nonce []byte, privateKeyRSA *rsa.PrivateKey, gwID *id.ID, + stop *stoppable.Single) error { opts := rsa.NewDefaultOptions() opts.Hash = hash.CMixHash h, _ := hash.NewCMixHash() @@ -248,6 +257,7 @@ func confirmNonce(sender *gateway.Sender, comms RegisterNodeCommsInterface, UID, return nil, err } return confirmResponse, nil - }) + }, stop) + return err } diff --git a/network/rounds/historical.go b/network/rounds/historical.go index 45aed7fdfaa7b296a46de79645e19fd010f88634..b4920335458283243fe7f4b77d4777bde2efb4e7 100644 --- a/network/rounds/historical.go +++ b/network/rounds/historical.go @@ -9,6 +9,7 @@ package rounds import ( jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/client/storage/reception" pb "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/xx_network/comms/connect" @@ -41,19 +42,18 @@ type historicalRoundRequest struct { // Long running thread which process historical rounds // Can be killed by sending a signal to the quit channel // takes a comms interface to aid in testing -func (m *Manager) processHistoricalRounds(comm historicalRoundsComms, quitCh <-chan struct{}) { +func (m *Manager) processHistoricalRounds(comm historicalRoundsComms, stop *stoppable.Single) { timerCh := make(<-chan time.Time) rng := m.Rng.GetStream() var roundRequests []historicalRoundRequest - done := false - for !done { + for { shouldProcess := false // wait for a quit or new round to check select { - case <-quitCh: + case <-stop.Quit(): rng.Close() // return all roundRequests in the queue to the input channel so they can // be checked in the future. If the queue is full, disable them as @@ -64,7 +64,8 @@ func (m *Manager) processHistoricalRounds(comm historicalRoundsComms, quitCh <-c default: } } - done = true + stop.ToStopped() + return // if the timer elapses process roundRequests to ensure the delay isn't too long case <-timerCh: if len(roundRequests) > 0 { @@ -100,7 +101,7 @@ func (m *Manager) processHistoricalRounds(comm historicalRoundsComms, quitCh <-c jww.DEBUG.Printf("Requesting Historical rounds %v from "+ "gateway %s", rounds, host.GetId()) return comm.RequestHistoricalRounds(host, hr) - }) + }, stop) if err != nil { jww.ERROR.Printf("Failed to request historical roundRequests "+ diff --git a/network/rounds/manager.go b/network/rounds/manager.go index 942e86319efe8ab05711b901360edbcd37865978..c695cd7a8c78be622769a4388a669eda5757b304 100644 --- a/network/rounds/manager.go +++ b/network/rounds/manager.go @@ -8,17 +8,16 @@ package rounds import ( - "fmt" "gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/network/gateway" "gitlab.com/elixxir/client/network/internal" "gitlab.com/elixxir/client/network/message" "gitlab.com/elixxir/client/stoppable" + "strconv" ) type Manager struct { params params.Rounds - internal.Internal sender *gateway.Sender @@ -48,14 +47,20 @@ func (m *Manager) StartProcessors() stoppable.Stoppable { //start the historical rounds thread historicalRoundsStopper := stoppable.NewSingle("ProcessHistoricalRounds") - go m.processHistoricalRounds(m.Comms, historicalRoundsStopper.Quit()) + go m.processHistoricalRounds(m.Comms, historicalRoundsStopper) multi.Add(historicalRoundsStopper) //start the message retrieval worker pool for i := uint(0); i < m.params.NumMessageRetrievalWorkers; i++ { - stopper := stoppable.NewSingle(fmt.Sprintf("Messager Retriever %v", i)) - go m.processMessageRetrieval(m.Comms, stopper.Quit()) + stopper := stoppable.NewSingle("Message Retriever " + strconv.Itoa(int(i))) + go m.processMessageRetrieval(m.Comms, stopper) multi.Add(stopper) } + + // Start the periodic unchecked round worker + stopper := stoppable.NewSingle("UncheckRound") + go m.processUncheckedRounds(m.params.UncheckRoundPeriod, backOffTable, stopper) + multi.Add(stopper) + return multi } diff --git a/network/rounds/retrieve.go b/network/rounds/retrieve.go index 0d90355c64bb2adaff003442737e09468a5b05f8..caf5ce0e70d2b688a2045feb8c0dd21bcecdee6a 100644 --- a/network/rounds/retrieve.go +++ b/network/rounds/retrieve.go @@ -8,14 +8,18 @@ package rounds import ( + "encoding/binary" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/network/message" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/client/storage/reception" pb "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/crypto/shuffle" "gitlab.com/elixxir/primitives/format" "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/primitives/id" + "time" ) type messageRetrievalComms interface { @@ -29,20 +33,27 @@ type roundLookup struct { identity reception.IdentityUse } -const noRoundError = "does not have round" +const noRoundError = "does not have round %d" // processMessageRetrieval received a roundLookup request and pings the gateways // of that round for messages for the requested identity in the roundLookup func (m *Manager) processMessageRetrieval(comms messageRetrievalComms, - quitCh <-chan struct{}) { + stop *stoppable.Single) { - done := false - for !done { + for { select { - case <-quitCh: - done = true + case <-stop.Quit(): + stop.ToStopped() + return case rl := <-m.lookupRoundMessages: ri := rl.roundInfo + jww.DEBUG.Printf("Checking for messages in round %d", ri.ID) + err := m.Session.UncheckedRounds().AddRound(rl.roundInfo, + rl.identity.EphId, rl.identity.Source) + if err != nil { + jww.ERROR.Printf("Could not add round %d in unchecked rounds store: %v", + rl.roundInfo.ID, err) + } // Convert gateways in round to proper ID format gwIds := make([]*id.ID, len(ri.Topology)) @@ -54,21 +65,70 @@ func (m *Manager) processMessageRetrieval(comms messageRetrievalComms, gwId.SetType(id.Gateway) gwIds[i] = gwId } - - // Attempt to request for this gateway - bundle, err := m.getMessagesFromGateway(id.Round(ri.ID), rl.identity, comms, gwIds) - - // After trying all gateways, if none returned we mark the round as a - // failure and print out the last error + // Target the last node in the team first because it has + // messages first, randomize other members of the team + var rndBytes [32]byte + stream := m.Rng.GetStream() + _, err = stream.Read(rndBytes[:]) + stream.Close() if err != nil { - jww.ERROR.Printf("Failed to get pickup round %d "+ + jww.FATAL.Panicf("Failed to randomize shuffle in round %d "+ "from all gateways (%v): %s", id.Round(ri.ID), gwIds, err) } + gwIds[0], gwIds[len(gwIds)-1] = gwIds[len(gwIds)-1], gwIds[0] + shuffle.ShuffleSwap(rndBytes[:], len(gwIds)-1, func(i, j int) { + gwIds[i+1], gwIds[j+1] = gwIds[j+1], gwIds[i+1] + }) + + // If ForceMessagePickupRetry, we are forcing processUncheckedRounds by + // randomly not picking up messages (FOR INTEGRATION TEST). Only done if + // round has not been ignored before + var bundle message.Bundle + if m.params.ForceMessagePickupRetry { + bundle, err = m.forceMessagePickupRetry(ri, rl, comms, gwIds, stop) + + // Exit if the thread has been stopped + if stoppable.CheckErr(err) { + jww.ERROR.Print(err) + continue + } + if err != nil { + jww.ERROR.Printf("Failed to get pickup round %d "+ + "from all gateways (%v): %s", + id.Round(ri.ID), gwIds, err) + } + } else { + // Attempt to request for this gateway + bundle, err = m.getMessagesFromGateway(id.Round(ri.ID), rl.identity, comms, gwIds, stop) + + // Exit if the thread has been stopped + if stoppable.CheckErr(err) { + jww.ERROR.Print(err) + continue + } + + // After trying all gateways, if none returned we mark the round as a + // failure and print out the last error + if err != nil { + jww.ERROR.Printf("Failed to get pickup round %d "+ + "from all gateways (%v): %s", + id.Round(ri.ID), gwIds, err) + } + + } if len(bundle.Messages) != 0 { + jww.DEBUG.Printf("Removing round %d from unchecked store", ri.ID) + err = m.Session.UncheckedRounds().Remove(id.Round(ri.ID)) + if err != nil { + jww.ERROR.Printf("Could not remove round %d "+ + "from unchecked rounds store: %v", ri.ID, err) + } + // If successful and there are messages, we send them to another thread bundle.Identity = rl.identity + bundle.RoundInfo = rl.roundInfo m.messageBundles <- bundle } @@ -78,11 +138,12 @@ func (m *Manager) processMessageRetrieval(comms messageRetrievalComms, // getMessagesFromGateway attempts to get messages from their assigned // gateway host in the round specified. If successful -func (m *Manager) getMessagesFromGateway(roundID id.Round, identity reception.IdentityUse, - comms messageRetrievalComms, gwIds []*id.ID) (message.Bundle, error) { - +func (m *Manager) getMessagesFromGateway(roundID id.Round, + identity reception.IdentityUse, comms messageRetrievalComms, gwIds []*id.ID, + stop *stoppable.Single) (message.Bundle, error) { + start := time.Now() // Send to the gateways using backup proxies - result, err := m.sender.SendToPreferred(gwIds, func(host *connect.Host, target *id.ID) (interface{}, error) { + result, err := m.sender.SendToPreferred(gwIds, func(host *connect.Host, target *id.ID) (interface{}, bool, error) { jww.DEBUG.Printf("Trying to get messages for round %v for ephemeralID %d (%v) "+ "via Gateway: %s", roundID, identity.EphId.Int64(), identity.Source.String(), host.GetId()) @@ -96,12 +157,13 @@ func (m *Manager) getMessagesFromGateway(roundID id.Round, identity reception.Id // If the gateway doesnt have the round, return an error msgResp, err := comms.RequestMessages(host, msgReq) if err == nil && !msgResp.GetHasRound() { - return message.Bundle{}, errors.Errorf(noRoundError) + jww.INFO.Printf("No round error for round %d received from %s", roundID, target) + return message.Bundle{}, false, errors.Errorf(noRoundError, roundID) } - return msgResp, err - }) - + return msgResp, false, err + }, stop) + jww.INFO.Printf("Received message for round %d, processing...", roundID) // Fail the round if an error occurs so it can be tried again later if err != nil { return message.Bundle{}, errors.WithMessagef(err, "Failed to "+ @@ -120,8 +182,8 @@ func (m *Manager) getMessagesFromGateway(roundID id.Round, identity reception.Id return message.Bundle{}, nil } - jww.INFO.Printf("Received %d messages in Round %v for %d (%s)", - len(msgs), roundID, identity.EphId.Int64(), identity.Source) + jww.INFO.Printf("Received %d messages in Round %v for %d (%s) in %s", + len(msgs), roundID, identity.EphId.Int64(), identity.Source, time.Now().Sub(start)) //build the bundle of messages to send to the message processor bundle := message.Bundle{ @@ -142,3 +204,32 @@ func (m *Manager) getMessagesFromGateway(roundID id.Round, identity reception.Id return bundle, nil } + +// Helper function which forces processUncheckedRounds by randomly +// not looking up messages +func (m *Manager) forceMessagePickupRetry(ri *pb.RoundInfo, rl roundLookup, + comms messageRetrievalComms, gwIds []*id.ID, + stop *stoppable.Single) (bundle message.Bundle, err error) { + rnd, _ := m.Session.UncheckedRounds().GetRound(id.Round(ri.ID)) + if rnd.NumChecks == 0 { + // Flip a coin to determine whether to pick up message + stream := m.Rng.GetStream() + defer stream.Close() + b := make([]byte, 8) + _, err = stream.Read(b) + if err != nil { + jww.FATAL.Panic(err.Error()) + } + result := binary.BigEndian.Uint64(b) + if result%2 == 0 { + jww.INFO.Printf("Forcing a message pickup retry for round %d", ri.ID) + // Do not call get message, leaving the round to be picked up + // in unchecked round scheduler process + return + } + + } + + // Attempt to request for this gateway + return m.getMessagesFromGateway(id.Round(ri.ID), rl.identity, comms, gwIds, stop) +} diff --git a/network/rounds/retrieve_test.go b/network/rounds/retrieve_test.go index abd79533dd9189cdef93f8f37aba4b8ad810b8d2..856fa05fa3e9b911f6180b0ed368031993c907a1 100644 --- a/network/rounds/retrieve_test.go +++ b/network/rounds/retrieve_test.go @@ -10,6 +10,7 @@ import ( "bytes" "gitlab.com/elixxir/client/network/gateway" "gitlab.com/elixxir/client/network/message" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/client/storage/reception" pb "gitlab.com/elixxir/comms/mixmessages" "gitlab.com/elixxir/crypto/fastRNG" @@ -28,18 +29,22 @@ func TestManager_ProcessMessageRetrieval(t *testing.T) { testManager := newManager(t) roundId := id.Round(5) mockComms := &mockMessageRetrievalComms{testingSignature: t} - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") testNdf := getNDF() nodeId := id.NewIdFromString(ReturningGateway, id.Node, &testing.T{}) gwId := nodeId.DeepCopy() gwId.SetType(id.Gateway) testNdf.Gateways = []ndf.Gateway{{ID: gwId.Marshal()}} + testManager.Rng = fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) p := gateway.DefaultPoolParams() p.MaxPoolSize = 1 - testManager.sender, _ = gateway.NewSender(p, - fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG), + var err error + testManager.sender, err = gateway.NewSender(p, testManager.Rng, testNdf, mockComms, testManager.Session, nil) + if err != nil { + t.Errorf(err.Error()) + } // Create a local channel so reception is possible (testManager.messageBundles is // send only via newManager call above) @@ -47,7 +52,7 @@ func TestManager_ProcessMessageRetrieval(t *testing.T) { testManager.messageBundles = messageBundleChan // Initialize the message retrieval - go testManager.processMessageRetrieval(mockComms, quitChan) + go testManager.processMessageRetrieval(mockComms, stop) // Construct expected values for checking expectedEphID := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} @@ -88,8 +93,10 @@ func TestManager_ProcessMessageRetrieval(t *testing.T) { testBundle = <-messageBundleChan // Close the process - quitChan <- struct{}{} - + err := stop.Close() + if err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } }() // Ensure bundle received and has expected values @@ -127,11 +134,12 @@ func TestManager_ProcessMessageRetrieval_NoRound(t *testing.T) { gwId := nodeId.DeepCopy() gwId.SetType(id.Gateway) testNdf.Gateways = []ndf.Gateway{{ID: gwId.Marshal()}} + testManager.Rng = fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) testManager.sender, _ = gateway.NewSender(p, - fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG), + testManager.Rng, testNdf, mockComms, testManager.Session, nil) - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") // Create a local channel so reception is possible (testManager.messageBundles is // send only via newManager call above) @@ -139,7 +147,7 @@ func TestManager_ProcessMessageRetrieval_NoRound(t *testing.T) { testManager.messageBundles = messageBundleChan // Initialize the message retrieval - go testManager.processMessageRetrieval(mockComms, quitChan) + go testManager.processMessageRetrieval(mockComms, stop) expectedEphID := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} @@ -178,8 +186,9 @@ func TestManager_ProcessMessageRetrieval_NoRound(t *testing.T) { testBundle = <-messageBundleChan // Close the process - quitChan <- struct{}{} - + if err := stop.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } }() time.Sleep(2 * time.Second) @@ -197,17 +206,18 @@ func TestManager_ProcessMessageRetrieval_FalsePositive(t *testing.T) { testManager := newManager(t) roundId := id.Round(5) mockComms := &mockMessageRetrievalComms{testingSignature: t} - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") testNdf := getNDF() nodeId := id.NewIdFromString(FalsePositive, id.Node, &testing.T{}) gwId := nodeId.DeepCopy() gwId.SetType(id.Gateway) testNdf.Gateways = []ndf.Gateway{{ID: gwId.Marshal()}} + testManager.Rng = fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) p := gateway.DefaultPoolParams() p.MaxPoolSize = 1 testManager.sender, _ = gateway.NewSender(p, - fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG), + testManager.Rng, testNdf, mockComms, testManager.Session, nil) // Create a local channel so reception is possible (testManager.messageBundles is @@ -216,7 +226,7 @@ func TestManager_ProcessMessageRetrieval_FalsePositive(t *testing.T) { testManager.messageBundles = messageBundleChan // Initialize the message retrieval - go testManager.processMessageRetrieval(mockComms, quitChan) + go testManager.processMessageRetrieval(mockComms, stop) // Construct expected values for checking expectedEphID := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} @@ -257,8 +267,9 @@ func TestManager_ProcessMessageRetrieval_FalsePositive(t *testing.T) { testBundle = <-messageBundleChan // Close the process - quitChan <- struct{}{} - + if err := stop.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } }() // Ensure no bundle was received due to false positive test @@ -276,7 +287,7 @@ func TestManager_ProcessMessageRetrieval_Quit(t *testing.T) { testManager := newManager(t) roundId := id.Round(5) mockComms := &mockMessageRetrievalComms{testingSignature: t} - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") // Create a local channel so reception is possible (testManager.messageBundles is // send only via newManager call above) @@ -284,10 +295,16 @@ func TestManager_ProcessMessageRetrieval_Quit(t *testing.T) { testManager.messageBundles = messageBundleChan // Initialize the message retrieval - go testManager.processMessageRetrieval(mockComms, quitChan) + go testManager.processMessageRetrieval(mockComms, stop) // Close the process early, before any logic below can be completed - quitChan <- struct{}{} + if err := stop.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } + + if err := stoppable.WaitForStopped(stop, 300*time.Millisecond); err != nil { + t.Fatalf("Failed to stop stoppable: %+v", err) + } // Construct expected values for checking expectedEphID := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} @@ -342,17 +359,18 @@ func TestManager_ProcessMessageRetrieval_MultipleGateways(t *testing.T) { testManager := newManager(t) roundId := id.Round(5) mockComms := &mockMessageRetrievalComms{testingSignature: t} - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") testNdf := getNDF() nodeId := id.NewIdFromString(ReturningGateway, id.Node, &testing.T{}) gwId := nodeId.DeepCopy() gwId.SetType(id.Gateway) testNdf.Gateways = []ndf.Gateway{{ID: gwId.Marshal()}} + testManager.Rng = fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) p := gateway.DefaultPoolParams() p.MaxPoolSize = 1 testManager.sender, _ = gateway.NewSender(p, - fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG), + testManager.Rng, testNdf, mockComms, testManager.Session, nil) // Create a local channel so reception is possible (testManager.messageBundles is @@ -361,7 +379,7 @@ func TestManager_ProcessMessageRetrieval_MultipleGateways(t *testing.T) { testManager.messageBundles = messageBundleChan // Initialize the message retrieval - go testManager.processMessageRetrieval(mockComms, quitChan) + go testManager.processMessageRetrieval(mockComms, stop) // Construct expected values for checking expectedEphID := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} @@ -403,8 +421,9 @@ func TestManager_ProcessMessageRetrieval_MultipleGateways(t *testing.T) { testBundle = <-messageBundleChan // Close the process - quitChan <- struct{}{} - + if err := stop.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } }() // Ensure that expected bundle is still received from happy comm diff --git a/network/rounds/unchecked.go b/network/rounds/unchecked.go new file mode 100644 index 0000000000000000000000000000000000000000..e62bff0c6885d71080b4544cf6602ec684fa2a9a --- /dev/null +++ b/network/rounds/unchecked.go @@ -0,0 +1,101 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package rounds + +import ( + jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/client/stoppable" + "gitlab.com/elixxir/client/storage/reception" + "gitlab.com/xx_network/primitives/netTime" + "time" +) + +// Constants for message retrieval backoff delays +const ( + tryZero = 10 * time.Second + tryOne = 30 * time.Second + tryTwo = 5 * time.Minute + tryThree = 30 * time.Minute + tryFour = 3 * time.Hour + tryFive = 12 * time.Hour + trySix = 24 * time.Hour + // Amount of tries past which the + // backoff will not increase + cappedTries = 7 +) + +var backOffTable = [cappedTries]time.Duration{tryZero, tryOne, tryTwo, tryThree, tryFour, tryFive, trySix} + +// processUncheckedRounds will (periodically) check every checkInterval +// for rounds that failed message retrieval in processMessageRetrieval. +// Rounds will have a backoff duration in which they will be tried again. +// If a round is found to be due on a periodical check, the round is sent +// back to processMessageRetrieval. +func (m *Manager) processUncheckedRounds(checkInterval time.Duration, backoffTable [cappedTries]time.Duration, + stop *stoppable.Single) { + ticker := time.NewTicker(checkInterval) + uncheckedRoundStore := m.Session.UncheckedRounds() + for { + select { + case <-stop.Quit(): + stop.ToStopped() + return + + case <-ticker.C: + // Pull and iterate through uncheckedRound list + roundList := m.Session.UncheckedRounds().GetList() + for rid, rnd := range roundList { + // If this round is due for a round check, send the round over + // to the retrieval thread. If not due, check next round. + if isRoundCheckDue(rnd.NumChecks, rnd.LastCheck, backoffTable) { + jww.INFO.Printf("Round %d due for a message lookup, retrying...", rid) + // Construct roundLookup object to send + rl := roundLookup{ + roundInfo: rnd.Info, + identity: reception.IdentityUse{ + Identity: reception.Identity{ + EphId: rnd.EpdId, + Source: rnd.Source, + }, + }, + } + + // Send to processMessageRetrieval + select { + case m.lookupRoundMessages <- rl: + case <-time.After(1 * time.Second): + jww.WARN.Printf("Timing out, not retrying round %d", rl.roundInfo.ID) + } + + // Update the state of the round for next look-up (if needed) + err := uncheckedRoundStore.IncrementCheck(rid) + if err != nil { + jww.ERROR.Printf("processUncheckedRounds error: Could not "+ + "increment check attempts for round %d: %v", rid, err) + } + + } + + } + } + } +} + +// isRoundCheckDue given the amount of tries and the timestamp the round +// was stored, determines whether this round is due for another check. +// Returns true if a new check is due +func isRoundCheckDue(tries uint64, ts time.Time, backoffTable [cappedTries]time.Duration) bool { + now := netTime.Now() + + if tries > cappedTries { + tries = cappedTries + } + roundCheckTime := ts.Add(backoffTable[tries]) + + return now.After(roundCheckTime) +} diff --git a/network/rounds/unchecked_test.go b/network/rounds/unchecked_test.go new file mode 100644 index 0000000000000000000000000000000000000000..980ca1e6157a583cc239cab4332e358c5b04e84a --- /dev/null +++ b/network/rounds/unchecked_test.go @@ -0,0 +1,104 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package rounds + +import ( + "gitlab.com/elixxir/client/network/gateway" + "gitlab.com/elixxir/client/network/message" + "gitlab.com/elixxir/client/stoppable" + pb "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/crypto/fastRNG" + "gitlab.com/xx_network/crypto/csprng" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + "gitlab.com/xx_network/primitives/ndf" + "reflect" + "testing" + "time" +) + +// Happy path +func TestUncheckedRoundScheduler(t *testing.T) { + // General initializations + testManager := newManager(t) + roundId := id.Round(5) + mockComms := &mockMessageRetrievalComms{testingSignature: t} + stop1 := stoppable.NewSingle("singleStoppable1") + stop2 := stoppable.NewSingle("singleStoppable2") + testNdf := getNDF() + nodeId := id.NewIdFromString(ReturningGateway, id.Node, &testing.T{}) + gwId := nodeId.DeepCopy() + gwId.SetType(id.Gateway) + testNdf.Gateways = []ndf.Gateway{{ID: gwId.Marshal()}} + p := gateway.DefaultPoolParams() + p.MaxPoolSize = 1 + testManager.sender, _ = gateway.NewSender(p, + fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG), + testNdf, mockComms, testManager.Session, nil) + + // Create a local channel so reception is possible (testManager.messageBundles is + // send only via newManager call above) + messageBundleChan := make(chan message.Bundle) + testManager.messageBundles = messageBundleChan + + testBackoffTable := newTestBackoffTable(t) + checkInterval := 250 * time.Millisecond + // Initialize the message retrieval + go testManager.processMessageRetrieval(mockComms, stop1) + go testManager.processUncheckedRounds(checkInterval, testBackoffTable, stop2) + + requestGateway := id.NewIdFromString(ReturningGateway, id.Gateway, t) + + // Construct expected values for checking + expectedEphID := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} + idList := [][]byte{requestGateway.Bytes()} + roundInfo := &pb.RoundInfo{ + ID: uint64(roundId), + Topology: idList, + } + + // Add round ot check + err := testManager.Session.UncheckedRounds().AddRound(roundInfo, expectedEphID, requestGateway) + if err != nil { + t.Fatalf("Could not add round to session: %v", err) + } + + var testBundle message.Bundle + go func() { + // Receive the bundle over the channel + time.Sleep(1 * time.Second) + testBundle = <-messageBundleChan + + // Close the process + if err := stop1.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } + if err := stop2.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } + + }() + + // Ensure bundle received and has expected values + time.Sleep(2 * time.Second) + if reflect.DeepEqual(testBundle, message.Bundle{}) { + t.Fatalf("Did not receive a message bundle over the channel") + } + + if testBundle.Identity.EphId.Int64() != expectedEphID.Int64() { + t.Errorf("Unexpected ephemeral ID in bundle."+ + "\n\tExpected: %v"+ + "\n\tReceived: %v", expectedEphID, testBundle.Identity.EphId) + } + + _, exists := testManager.Session.UncheckedRounds().GetRound(roundId) + if exists { + t.Fatalf("Expected round %d to be removed after being processed", roundId) + } + +} diff --git a/network/rounds/utils_test.go b/network/rounds/utils_test.go index d352078dcd1219b188c8c7fde0b807748d8c3521..e4c41bbf3fe5c69e76ac2aeda3d8766a7f4aaf82 100644 --- a/network/rounds/utils_test.go +++ b/network/rounds/utils_test.go @@ -8,14 +8,18 @@ package rounds import ( "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/network/internal" "gitlab.com/elixxir/client/network/message" "gitlab.com/elixxir/client/storage" pb "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/crypto/fastRNG" "gitlab.com/xx_network/comms/connect" + "gitlab.com/xx_network/crypto/csprng" "gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/ndf" "testing" + "time" ) func newManager(face interface{}) *Manager { @@ -27,6 +31,7 @@ func newManager(face interface{}) *Manager { Internal: internal.Internal{ Session: sess1, TransmissionID: sess1.GetUser().TransmissionID, + Rng: fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG), }, } return testManager @@ -102,6 +107,23 @@ func (mmrc *mockMessageRetrievalComms) RequestMessages(host *connect.Host, return nil, nil } +func newTestBackoffTable(face interface{}) [cappedTries]time.Duration { + switch face.(type) { + case *testing.T, *testing.M, *testing.B, *testing.PB: + break + default: + jww.FATAL.Panicf("newTestBackoffTable is restricted to testing only. Got %T", face) + } + + var backoff [cappedTries]time.Duration + for i := 0; i < cappedTries; i++ { + backoff[uint64(i)] = 1 * time.Millisecond + } + + return backoff + +} + func getNDF() *ndf.NetworkDefinition { return &ndf.NetworkDefinition{ E2E: ndf.Group{ diff --git a/network/send.go b/network/send.go index d70a0c6661c2ee6f4c40f313219baa7cbb86fd52..d54e478c7c705f4995c451b1e302e5ae77d292f7 100644 --- a/network/send.go +++ b/network/send.go @@ -12,6 +12,7 @@ import ( jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/interfaces/message" "gitlab.com/elixxir/client/interfaces/params" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/crypto/e2e" "gitlab.com/elixxir/primitives/format" "gitlab.com/xx_network/primitives/id" @@ -28,7 +29,16 @@ func (m *manager) SendCMIX(msg format.Message, recipient *id.ID, param params.CM "network is not healthy") } - return m.message.SendCMIX(m.GetSender(), msg, recipient, param) + return m.message.SendCMIX(m.GetSender(), msg, recipient, param, nil) +} + +// SendManyCMIX sends many "raw" CMIX message payloads to each of the +// provided recipients. Used for group chat functionality. Returns the +// round ID of the round the payload was sent or an error if it fails. +func (m *manager) SendManyCMIX(messages map[id.ID]format.Message, + p params.CMIX) (id.Round, []ephemeral.Id, error) { + + return m.message.SendManyCMIX(m.sender, messages, p) } // SendUnsafe sends an unencrypted payload to the provided recipient @@ -52,7 +62,7 @@ func (m *manager) SendUnsafe(msg message.Send, param params.Unsafe) ([]id.Round, // SendE2E sends an end-to-end payload to the provided recipient with // the provided msgType. Returns the list of rounds in which parts of // the message were sent or an error if it fails. -func (m *manager) SendE2E(msg message.Send, e2eP params.E2E) ( +func (m *manager) SendE2E(msg message.Send, e2eP params.E2E, stop *stoppable.Single) ( []id.Round, e2e.MessageID, error) { if !m.Health.IsHealthy() { @@ -60,5 +70,5 @@ func (m *manager) SendE2E(msg message.Send, e2eP params.E2E) ( "message when the network is not healthy") } - return m.message.SendE2E(msg, e2eP) + return m.message.SendE2E(msg, e2eP, stop) } diff --git a/permissioning/permissioning.go b/permissioning/permissioning.go index ebe00b3c3d41c3c9fcaf844e65d87fc6033dd829..87c3b6fff40c147c59a4534f7e52f588a9ad45fd 100644 --- a/permissioning/permissioning.go +++ b/permissioning/permissioning.go @@ -13,6 +13,8 @@ import ( "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/ndf" + "math" + "time" ) type Permissioning struct { @@ -31,7 +33,8 @@ func Init(comms *client.Comms, def *ndf.NetworkDefinition) (*Permissioning, erro //add the permissioning host to comms hParam := connect.GetDefaultHostParams() hParam.AuthEnabled = false - + // Client will not send KeepAlive packets + hParam.KaClientOpts.Time = time.Duration(math.MaxInt64) perm.host, err = comms.AddHost(&id.Permissioning, def.Registration.Address, []byte(def.Registration.TlsCertificate), hParam) diff --git a/single/manager.go b/single/manager.go index 78481829180017f242ca45e43b2bacc4175cf4dd..063f7e8541e0f8f3f9063228fbc33d618247e2b7 100644 --- a/single/manager.go +++ b/single/manager.go @@ -74,13 +74,13 @@ func (m *Manager) StartProcesses() stoppable.Stoppable { transmissionStop := stoppable.NewSingle(singleUseTransmission) transmissionChan := make(chan message.Receive, rawMessageBuffSize) m.swb.RegisterChannel(singleUseReceiveTransmission, &id.ID{}, message.Raw, transmissionChan) - go m.receiveTransmissionHandler(transmissionChan, transmissionStop.Quit()) + go m.receiveTransmissionHandler(transmissionChan, transmissionStop) // Start waiting for single-use response responseStop := stoppable.NewSingle(singleUseResponse) responseChan := make(chan message.Receive, rawMessageBuffSize) m.swb.RegisterChannel(singleUseReceiveResponse, &id.ID{}, message.Raw, responseChan) - go m.receiveResponseHandler(responseChan, responseStop.Quit()) + go m.receiveResponseHandler(responseChan, responseStop) // Create a multi stoppable singleUseMulti := stoppable.NewMulti(singleUseStop) diff --git a/single/manager_test.go b/single/manager_test.go index 2ce788f7b86ba44fd61a2aaa68814d1d62a49a02..4d570c35e900e2f36d1ee77bb76a298e927f09a7 100644 --- a/single/manager_test.go +++ b/single/manager_test.go @@ -181,7 +181,7 @@ func TestManager_StartProcesses_Stop(t *testing.T) { t.Error("Stoppable is not running.") } - err = stop.Close(1 * time.Millisecond) + err = stop.Close() if err != nil { t.Errorf("Failed to close: %+v", err) } @@ -283,8 +283,8 @@ func (tnm *testNetworkManager) GetMsg(i int) format.Message { return tnm.msgs[i] } -func (tnm *testNetworkManager) SendE2E(_ message.Send, _ params.E2E) ([]id.Round, e2e.MessageID, error) { - return nil, [32]byte{}, nil +func (tnm *testNetworkManager) SendE2E(message.Send, params.E2E, *stoppable.Single) ([]id.Round, e2e.MessageID, error) { + return nil, e2e.MessageID{}, nil } func (tnm *testNetworkManager) SendUnsafe(_ message.Send, _ params.Unsafe) ([]id.Round, error) { @@ -306,6 +306,23 @@ func (tnm *testNetworkManager) SendCMIX(msg format.Message, _ *id.ID, _ params.C return id.Round(rand.Uint64()), ephemeral.Id{}, nil } +func (tnm *testNetworkManager) SendManyCMIX(messages map[id.ID]format.Message, p params.CMIX) (id.Round, []ephemeral.Id, error) { + if tnm.cmixTimeout != 0 { + time.Sleep(tnm.cmixTimeout) + } else if tnm.cmixErr { + return 0, []ephemeral.Id{}, errors.New("sendCMIX error") + } + + tnm.Lock() + defer tnm.Unlock() + + for _, msg := range messages { + tnm.msgs = append(tnm.msgs, msg) + } + + return id.Round(rand.Uint64()), []ephemeral.Id{}, nil +} + func (tnm *testNetworkManager) GetInstance() *network.Instance { return tnm.instance } @@ -324,10 +341,18 @@ func (tnm *testNetworkManager) InProgressRegistrations() int { return 0 } -func (t *testNetworkManager) GetSender() *gateway.Sender { +func (tnm *testNetworkManager) GetSender() *gateway.Sender { return nil } +func (tnm *testNetworkManager) GetAddressSize() uint8 { return 16 } + +func (tnm *testNetworkManager) RegisterAddressSizeNotification(string) (chan uint8, error) { + return nil, nil +} + +func (tnm *testNetworkManager) UnregisterAddressSizeNotification(string) {} + func getNDF() *ndf.NetworkDefinition { return &ndf.NetworkDefinition{ E2E: ndf.Group{ diff --git a/single/receiveResponse.go b/single/receiveResponse.go index d25880dae86dee70d882463aeba08d0310daa08b..6a6fa94fddbc10f71a6e311fea944a71ea8f35eb 100644 --- a/single/receiveResponse.go +++ b/single/receiveResponse.go @@ -11,6 +11,7 @@ import ( "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/crypto/e2e/auth" "gitlab.com/elixxir/crypto/e2e/singleUse" "gitlab.com/elixxir/primitives/format" @@ -20,13 +21,14 @@ import ( // receiveResponseHandler handles the reception of single-use response messages. func (m *Manager) receiveResponseHandler(rawMessages chan message.Receive, - quitChan <-chan struct{}) { + stop *stoppable.Single) { jww.DEBUG.Print("Waiting to receive single-use response messages.") for { select { - case <-quitChan: + case <-stop.Quit(): jww.DEBUG.Printf("Stopping waiting to receive single-use " + "response message.") + stop.ToStopped() return case msg := <-rawMessages: jww.DEBUG.Printf("Received CMIX message; checking if it is a " + diff --git a/single/receiveResponse_test.go b/single/receiveResponse_test.go index 4e7e8c352e8196f0ff24d5a0d60633f78439e8dd..ea75c5e3ec10cb0af1971af162b4e0fc69b78871 100644 --- a/single/receiveResponse_test.go +++ b/single/receiveResponse_test.go @@ -10,6 +10,7 @@ package single import ( "bytes" "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/crypto/e2e/auth" "gitlab.com/elixxir/crypto/e2e/singleUse" "gitlab.com/elixxir/primitives/format" @@ -26,7 +27,7 @@ import ( func TestManager_ReceiveResponseHandler(t *testing.T) { m := newTestManager(0, false, t) rawMessages := make(chan message.Receive, rawMessageBuffSize) - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") partner := NewContact(id.NewIdFromString("recipientID", id.User, t), m.store.E2e().GetGroup().NewInt(43), m.store.E2e().GetGroup().NewInt(42), singleUse.TagFP{}, 8) @@ -52,7 +53,7 @@ func TestManager_ReceiveResponseHandler(t *testing.T) { } }() - go m.receiveResponseHandler(rawMessages, quitChan) + go m.receiveResponseHandler(rawMessages, stop) for _, msg := range msgs { rawMessages <- message.Receive{ @@ -78,14 +79,16 @@ func TestManager_ReceiveResponseHandler(t *testing.T) { t.Errorf("Callback failed to be called.") } - quitChan <- struct{}{} + if err := stop.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } } // Error path: invalid CMIX message. func TestManager_ReceiveResponseHandler_CmixMessageError(t *testing.T) { m := newTestManager(0, false, t) rawMessages := make(chan message.Receive, rawMessageBuffSize) - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") partner := NewContact(id.NewIdFromString("recipientID", id.User, t), m.store.E2e().GetGroup().NewInt(43), m.store.E2e().GetGroup().NewInt(42), singleUse.TagFP{}, 8) @@ -106,7 +109,7 @@ func TestManager_ReceiveResponseHandler_CmixMessageError(t *testing.T) { } }() - go m.receiveResponseHandler(rawMessages, quitChan) + go m.receiveResponseHandler(rawMessages, stop) rawMessages <- message.Receive{ Payload: make([]byte, format.MinimumPrimeSize*2), @@ -124,7 +127,9 @@ func TestManager_ReceiveResponseHandler_CmixMessageError(t *testing.T) { case <-timer.C: } - quitChan <- struct{}{} + if err := stop.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } } // Happy path. diff --git a/single/reception.go b/single/reception.go index 53b6c12eda52386a3544b547bee0b54b91bc1d2a..23ca6f8bef891b8ae4426fe24172d4a88c2510c5 100644 --- a/single/reception.go +++ b/single/reception.go @@ -11,6 +11,7 @@ import ( "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" cAuth "gitlab.com/elixxir/crypto/e2e/auth" "gitlab.com/elixxir/crypto/e2e/singleUse" "gitlab.com/elixxir/primitives/format" @@ -19,14 +20,15 @@ import ( // receiveTransmissionHandler waits to receive single-use transmissions. When // a message is received, its is returned via its registered callback. func (m *Manager) receiveTransmissionHandler(rawMessages chan message.Receive, - quitChan <-chan struct{}) { + stop *stoppable.Single) { fp := singleUse.NewTransmitFingerprint(m.store.E2e().GetDHPublicKey()) jww.DEBUG.Print("Waiting to receive single-use transmission messages.") for { select { - case <-quitChan: + case <-stop.Quit(): jww.DEBUG.Printf("Stopping waiting to receive single-use " + "transmission message.") + stop.ToStopped() return case msg := <-rawMessages: jww.DEBUG.Printf("Received CMIX message; checking if it is a " + diff --git a/single/reception_test.go b/single/reception_test.go index 3266d9904b2fb51dd592c766b61a9c40409e0925..27a87b9a181e94437b8a3b471492123c2451547b 100644 --- a/single/reception_test.go +++ b/single/reception_test.go @@ -3,6 +3,7 @@ package single import ( "bytes" "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/stoppable" contact2 "gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/crypto/e2e/singleUse" "gitlab.com/elixxir/primitives/format" @@ -17,7 +18,6 @@ import ( func TestManager_receiveTransmissionHandler(t *testing.T) { m := newTestManager(0, false, t) rawMessages := make(chan message.Receive, rawMessageBuffSize) - quitChan := make(chan struct{}) partner := contact2.Contact{ ID: id.NewIdFromString("recipientID", id.User, t), DhPubKey: m.store.E2e().GetDHPublicKey(), @@ -35,7 +35,7 @@ func TestManager_receiveTransmissionHandler(t *testing.T) { m.callbackMap.registerCallback(tag, callback) - go m.receiveTransmissionHandler(rawMessages, quitChan) + go m.receiveTransmissionHandler(rawMessages, stoppable.NewSingle("singleStoppable")) rawMessages <- message.Receive{ Payload: msg.Marshal(), } @@ -57,7 +57,7 @@ func TestManager_receiveTransmissionHandler(t *testing.T) { func TestManager_receiveTransmissionHandler_QuitChan(t *testing.T) { m := newTestManager(0, false, t) rawMessages := make(chan message.Receive, rawMessageBuffSize) - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") tag := "Test tag" payload := make([]byte, 132) rand.New(rand.NewSource(42)).Read(payload) @@ -65,8 +65,11 @@ func TestManager_receiveTransmissionHandler_QuitChan(t *testing.T) { m.callbackMap.registerCallback(tag, callback) - go m.receiveTransmissionHandler(rawMessages, quitChan) - quitChan <- struct{}{} + go m.receiveTransmissionHandler(rawMessages, stop) + + if err := stop.Close(); err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } timer := time.NewTimer(50 * time.Millisecond) @@ -82,7 +85,7 @@ func TestManager_receiveTransmissionHandler_QuitChan(t *testing.T) { func TestManager_receiveTransmissionHandler_FingerPrintError(t *testing.T) { m := newTestManager(0, false, t) rawMessages := make(chan message.Receive, rawMessageBuffSize) - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") partner := contact2.Contact{ ID: id.NewIdFromString("recipientID", id.User, t), DhPubKey: m.store.E2e().GetGroup().NewInt(42), @@ -100,7 +103,7 @@ func TestManager_receiveTransmissionHandler_FingerPrintError(t *testing.T) { m.callbackMap.registerCallback(tag, callback) - go m.receiveTransmissionHandler(rawMessages, quitChan) + go m.receiveTransmissionHandler(rawMessages, stop) rawMessages <- message.Receive{ Payload: msg.Marshal(), } @@ -119,7 +122,7 @@ func TestManager_receiveTransmissionHandler_FingerPrintError(t *testing.T) { func TestManager_receiveTransmissionHandler_ProcessMessageError(t *testing.T) { m := newTestManager(0, false, t) rawMessages := make(chan message.Receive, rawMessageBuffSize) - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") partner := contact2.Contact{ ID: id.NewIdFromString("recipientID", id.User, t), DhPubKey: m.store.E2e().GetDHPublicKey(), @@ -139,7 +142,7 @@ func TestManager_receiveTransmissionHandler_ProcessMessageError(t *testing.T) { m.callbackMap.registerCallback(tag, callback) - go m.receiveTransmissionHandler(rawMessages, quitChan) + go m.receiveTransmissionHandler(rawMessages, stop) rawMessages <- message.Receive{ Payload: msg.Marshal(), } @@ -158,7 +161,7 @@ func TestManager_receiveTransmissionHandler_ProcessMessageError(t *testing.T) { func TestManager_receiveTransmissionHandler_TagFpError(t *testing.T) { m := newTestManager(0, false, t) rawMessages := make(chan message.Receive, rawMessageBuffSize) - quitChan := make(chan struct{}) + stop := stoppable.NewSingle("singleStoppable") partner := contact2.Contact{ ID: id.NewIdFromString("recipientID", id.User, t), DhPubKey: m.store.E2e().GetDHPublicKey(), @@ -173,7 +176,7 @@ func TestManager_receiveTransmissionHandler_TagFpError(t *testing.T) { t.Fatalf("Failed to create tranmission CMIX message: %+v", err) } - go m.receiveTransmissionHandler(rawMessages, quitChan) + go m.receiveTransmissionHandler(rawMessages, stop) rawMessages <- message.Receive{ Payload: msg.Marshal(), } diff --git a/single/singleUseMap_test.go b/single/singleUseMap_test.go index 3b4a18171640935735b5d3b28042141c77ded768..1f91c755f048fa2a2444109a521931b21cdc6867 100644 --- a/single/singleUseMap_test.go +++ b/single/singleUseMap_test.go @@ -117,7 +117,7 @@ func Test_pending_addState_TimeoutError(t *testing.T) { *expectedState, *state) } - timer := time.NewTimer(timeout * 2) + timerTimeout := timeout * 4 select { case results := <-callbackChan: @@ -132,8 +132,8 @@ func Test_pending_addState_TimeoutError(t *testing.T) { if results.err == nil || !strings.Contains(results.err.Error(), "timed out") { t.Errorf("Callback did not return a time out error on return: %+v", results.err) } - case <-timer.C: - t.Error("Failed to time out.") + case <-time.NewTimer(timerTimeout).C: + t.Errorf("Failed to time out after %s.", timerTimeout) } } diff --git a/single/transmission.go b/single/transmission.go index d5289f339b1120c87256228d87fd7b7514c0a9d7..c896eb7e954e55fddc7b99513635461dc755de2f 100644 --- a/single/transmission.go +++ b/single/transmission.go @@ -64,12 +64,9 @@ type roundEvents interface { func (m *Manager) transmitSingleUse(partner contact2.Contact, payload []byte, tag string, MaxMsgs uint8, rng io.Reader, callback ReplyComm, timeout time.Duration, roundEvents roundEvents) error { - // Get ephemeral ID address size; this will block until the client knows the - // address size if it is currently unknown - if m.store.Reception().IsIdSizeDefault() { - m.store.Reception().WaitForIdSizeUpdate() - } - addressSize := m.store.Reception().GetIDSize() + // Get ephemeral ID address space size; this blocks until the address space + // size is set for the first time + addressSize := m.net.GetAddressSize() // Create new CMIX message containing the transmission payload cmixMsg, dhKey, rid, ephID, err := m.makeTransmitCmixMessage(partner, @@ -93,6 +90,7 @@ func (m *Manager) transmitSingleUse(partner contact2.Contact, payload []byte, err = m.reception.AddIdentity(reception.Identity{ EphId: ephID, Source: rid, + AddressSize: addressSize, End: timeStart.Add(2 * timeout), ExtraChecks: 10, StartValid: timeStart.Add(-2 * timeout), @@ -140,7 +138,7 @@ func (m *Manager) transmitSingleUse(partner contact2.Contact, payload []byte, } // Update the timeout for the elapsed time - roundEventTimeout := timeout - netTime.Now().Sub(timeStart) - time.Millisecond + roundEventTimeout := timeout - netTime.Since(timeStart) - time.Millisecond // Check message delivery sendResults := make(chan ds.EventReturn, 1) @@ -175,7 +173,7 @@ func (m *Manager) transmitSingleUse(partner contact2.Contact, payload []byte, // makeTransmitCmixMessage generates a CMIX message containing the transmission message, // which contains the encrypted payload. func (m *Manager) makeTransmitCmixMessage(partner contact2.Contact, - payload []byte, tag string, maxMsgs uint8, addressSize uint, + payload []byte, tag string, maxMsgs uint8, addressSize uint8, timeout time.Duration, timeNow time.Time, rng io.Reader) (format.Message, *cyclic.Int, *id.ID, ephemeral.Id, error) { e2eGrp := m.store.E2e().GetGroup() @@ -255,8 +253,9 @@ func generateDhKeys(grp *cyclic.Group, dhPubKey *cyclic.Int, // contains a nonce. If the generated ephemeral ID has a window that is not // within +/- the given 2*timeout from now, then the IDs are generated again // using a new nonce. -func makeIDs(msg *transmitMessagePayload, publicKey *cyclic.Int, addressSize uint, - timeout time.Duration, timeNow time.Time, rng io.Reader) (*id.ID, ephemeral.Id, error) { +func makeIDs(msg *transmitMessagePayload, publicKey *cyclic.Int, + addressSize uint8, timeout time.Duration, timeNow time.Time, + rng io.Reader) (*id.ID, ephemeral.Id, error) { var rid *id.ID var ephID ephemeral.Id @@ -277,7 +276,7 @@ func makeIDs(msg *transmitMessagePayload, publicKey *cyclic.Int, addressSize uin rid = msg.GetRID(publicKey) // Generate the ephemeral ID - ephID, start, end, err = ephemeral.GetId(rid, addressSize, timeNow.UnixNano()) + ephID, start, end, err = ephemeral.GetId(rid, uint(addressSize), timeNow.UnixNano()) if err != nil { return nil, ephemeral.Id{}, errors.Errorf("failed to generate "+ "ephemeral ID from newly generated ID: %+v", err) diff --git a/single/transmission_test.go b/single/transmission_test.go index 367a8b3bfa4bd07df0e266045281624934ea3ee3..761f65d9fc5e757aa84710cf7f3ef476f0ae91ac 100644 --- a/single/transmission_test.go +++ b/single/transmission_test.go @@ -366,7 +366,7 @@ func Test_makeIDs_Consistency(t *testing.T) { if err != nil { t.Fatalf("Failed to generate public key: %+v", err) } - addressSize := uint(32) + addressSize := uint8(32) expectedPayload, err := unmarshalTransmitMessagePayload(msgPayload.Marshal()) if err != nil { @@ -397,7 +397,7 @@ func Test_makeIDs_Consistency(t *testing.T) { } expectedEphID, _, _, err := ephemeral.GetId(expectedPayload.GetRID(publicKey), - addressSize, timeNow.UnixNano()) + uint(addressSize), timeNow.UnixNano()) if err != nil { t.Fatalf("Failed to generate expected ephemeral ID: %+v", err) } diff --git a/stoppable/bindings.go b/stoppable/bindings.go deleted file mode 100644 index 55784d6522bcb0567d8eb86b282bb9895302ab57..0000000000000000000000000000000000000000 --- a/stoppable/bindings.go +++ /dev/null @@ -1,37 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// Copyright © 2020 xx network SEZC // -// // -// Use of this source code is governed by a license that can be found in the // -// LICENSE file // -/////////////////////////////////////////////////////////////////////////////// - -package stoppable - -import "time" - -type Bindings interface { - Close(timeoutMS int) error - IsRunning() bool - Name() string -} - -func WrapForBindings(s Stoppable) Bindings { - return &bindingsStoppable{s: s} -} - -type bindingsStoppable struct { - s Stoppable -} - -func (bs *bindingsStoppable) Close(timeoutMS int) error { - timeout := time.Duration(timeoutMS) * time.Millisecond - return bs.s.Close(timeout) -} - -func (bs *bindingsStoppable) IsRunning() bool { - return bs.s.IsRunning() -} - -func (bs *bindingsStoppable) Name() string { - return bs.s.Name() -} diff --git a/stoppable/cleanup.go b/stoppable/cleanup.go deleted file mode 100644 index b1e2c4561dc87c2af1ec654d48b787d47ca8182e..0000000000000000000000000000000000000000 --- a/stoppable/cleanup.go +++ /dev/null @@ -1,95 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// Copyright © 2020 xx network SEZC // -// // -// Use of this source code is governed by a license that can be found in the // -// LICENSE file // -/////////////////////////////////////////////////////////////////////////////// - -package stoppable - -import ( - "github.com/pkg/errors" - jww "github.com/spf13/jwalterweatherman" - "gitlab.com/xx_network/primitives/netTime" - "sync" - "sync/atomic" - "time" -) - -// Cleanup wraps any stoppable and runs a callback after to stop for cleanup -// behavior. The cleanup is run under the remainder of the timeout but will not -// be canceled if the timeout runs out. The cleanup function does not run if the -// thread does not stop. -type Cleanup struct { - stop Stoppable - // the clean function receives how long it has to run before the timeout, - // this is nto expected to be used in most cases - clean func(duration time.Duration) error - running uint32 - once sync.Once -} - -// NewCleanup creates a new Cleanup from the passed stoppable and function. -func NewCleanup(stop Stoppable, clean func(duration time.Duration) error) *Cleanup { - return &Cleanup{ - stop: stop, - clean: clean, - running: 0, - } -} - -// IsRunning returns true if the thread is still running and its cleanup has -// completed. -func (c *Cleanup) IsRunning() bool { - return atomic.LoadUint32(&c.running) == 1 -} - -// Name returns the name of the stoppable denoting it has cleanup. -func (c *Cleanup) Name() string { - return c.stop.Name() + " with cleanup" -} - -// Close stops the contained stoppable and runs the cleanup function after. The -// cleanup function does not run if the thread does not stop. -func (c *Cleanup) Close(timeout time.Duration) error { - var err error - - c.once.Do( - func() { - defer atomic.StoreUint32(&c.running, 0) - start := netTime.Now() - - // Run the stoppable - if err := c.stop.Close(timeout); err != nil { - err = errors.WithMessagef(err, "Cleanup for %s not executed", - c.stop.Name()) - return - } - - // Run the cleanup function with the remaining time as a timeout - elapsed := time.Since(start) - - complete := make(chan error, 1) - go func() { - complete <- c.clean(elapsed) - }() - - timer := time.NewTimer(elapsed) - - select { - case err := <-complete: - if err != nil { - err = errors.WithMessagef(err, "Cleanup for %s failed", - c.stop.Name()) - } - case <-timer.C: - err = errors.Errorf("Clean up for %s timeout", c.stop.Name()) - } - }) - - if err != nil { - jww.ERROR.Printf(err.Error()) - } - - return err -} diff --git a/stoppable/cleanup_test.go b/stoppable/cleanup_test.go deleted file mode 100644 index 8bc7fe0be09e69b0809cbd97194dbbe58902918f..0000000000000000000000000000000000000000 --- a/stoppable/cleanup_test.go +++ /dev/null @@ -1,62 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// Copyright © 2020 xx network SEZC // -// // -// Use of this source code is governed by a license that can be found in the // -// LICENSE file // -/////////////////////////////////////////////////////////////////////////////// - -package stoppable - -import ( - "testing" -) - -// Tests happy path of NewCleanup(). -func TestNewCleanup(t *testing.T) { - single := NewSingle("test name") - cleanup := NewCleanup(single, single.Close) - - if cleanup.stop != single || cleanup.running != 0 { - t.Errorf("NewCleanup() returned Single with incorrect values."+ - "\n\texpected: stop: %v running: %d\n\treceived: stop: %v running: %d", - single, cleanup.stop, 0, cleanup.running) - } -} - -// Tests happy path of Cleanup.IsRunning(). -func TestCleanup_IsRunning(t *testing.T) { - single := NewSingle("test name") - cleanup := NewCleanup(single, single.Close) - - if cleanup.IsRunning() { - t.Errorf("IsRunning() returned false when it should be running.") - } - - cleanup.running = 1 - if !cleanup.IsRunning() { - t.Errorf("IsRunning() returned true when it should not be running.") - } -} - -// Tests happy path of Cleanup.Name(). -func TestCleanup_Name(t *testing.T) { - name := "test name" - single := NewSingle(name) - cleanup := NewCleanup(single, single.Close) - - if name+" with cleanup" != cleanup.Name() { - t.Errorf("Name() returned the incorrect string."+ - "\n\texpected: %s\n\treceived: %s", name+" with cleanup", cleanup.Name()) - } -} - -// Tests happy path of Cleanup.Close(). -func TestCleanup_Close(t *testing.T) { - single := NewSingle("test name") - cleanup := NewCleanup(single, single.Close) - - err := cleanup.Close(0) - if err != nil { - t.Errorf("Close() returned an error: %v", err) - } -} diff --git a/stoppable/multi.go b/stoppable/multi.go index 0636b84fa6e4a3d17e5a74431e42e796d84de45b..60d1c8530300f2d52babf469d923627f236a6f1d 100644 --- a/stoppable/multi.go +++ b/stoppable/multi.go @@ -8,91 +8,120 @@ package stoppable import ( - "fmt" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" + "strings" "sync" "sync/atomic" - "time" ) +// Error message. +const closeMultiErr = "multi stoppable %q failed to close %d/%d stoppables" + type Multi struct { stoppables []Stoppable name string - running uint32 mux sync.RWMutex once sync.Once } -// NewMulti returns a new multi stoppable. +// NewMulti returns a new multi Stoppable. func NewMulti(name string) *Multi { return &Multi{ - name: name, - running: 1, + name: name, } } -// IsRunning returns true if the thread is still running. -func (m *Multi) IsRunning() bool { - return atomic.LoadUint32(&m.running) == 1 -} - -// Add adds the given stoppable to the list of stoppables. +// Add adds the given Stoppable to the list of stoppables. func (m *Multi) Add(stoppable Stoppable) { m.mux.Lock() m.stoppables = append(m.stoppables, stoppable) m.mux.Unlock() } -// Name returns the name of the multi stoppable and the names of all stoppables +// Name returns the name of the Multi Stoppable and the names of all stoppables // it contains. func (m *Multi) Name() string { m.mux.RLock() - names := m.name + ": {" - for _, s := range m.stoppables { - names += s.Name() + ", " + + names := make([]string, len(m.stoppables)) + for i, s := range m.stoppables { + names[i] = s.Name() } - if len(m.stoppables) > 0 { - names = names[:len(names)-2] + + m.mux.RUnlock() + + return m.name + "{" + strings.Join(names, ", ") + "}" +} + +// GetStatus returns the lowest status of all of the Stoppable children. The +// status is not the status of all Stoppables, but the status of the Stoppable +// with the lowest status. +func (m *Multi) GetStatus() Status { + lowestStatus := Stopped + m.mux.RLock() + + for _, s := range m.stoppables { + status := s.GetStatus() + if status < lowestStatus { + lowestStatus = status + } } - names += "}" + m.mux.RUnlock() - return names + return lowestStatus } -// Close closes all child stoppers. It does not return their errors and assumes -// they print them to the log. -func (m *Multi) Close(timeout time.Duration) error { - var err error - m.once.Do( - func() { - atomic.StoreUint32(&m.running, 0) - - numErrors := uint32(0) - wg := &sync.WaitGroup{} - - m.mux.Lock() - for _, stoppable := range m.stoppables { - wg.Add(1) - go func(stoppable Stoppable) { - if stoppable.Close(timeout) != nil { - atomic.AddUint32(&numErrors, 1) - } - wg.Done() - }(stoppable) - } - m.mux.Unlock() - - wg.Wait() - - if numErrors > 0 { - errStr := fmt.Sprintf("MultiStopper %s failed to close "+ - "%v/%v stoppers", m.name, numErrors, len(m.stoppables)) - jww.ERROR.Println(errStr) - err = errors.New(errStr) - } - }) - - return err +// IsRunning returns true if Stoppable is marked as running. +func (m *Multi) IsRunning() bool { + return m.GetStatus() == Running +} + +// IsStopping returns true if Stoppable is marked as stopping. +func (m *Multi) IsStopping() bool { + return m.GetStatus() == Stopping +} + +// IsStopped returns true if Stoppable is marked as stopped. +func (m *Multi) IsStopped() bool { + return m.GetStatus() == Stopped +} + +// Close issues a close signal to all child stoppables and marks the status of +// the Multi Stoppable as stopping. Returns an error if one or more child +// stoppables failed to close but it does not return their specific errors and +// assumes they print them to the log. +func (m *Multi) Close() error { + var numErrors uint32 + + m.once.Do(func() { + var wg sync.WaitGroup + + jww.TRACE.Printf("Sending on quit channel to multi stoppable %q.", + m.Name()) + + m.mux.Lock() + // Attempt to stop each stoppable in its own goroutine + for _, stoppable := range m.stoppables { + wg.Add(1) + go func(stoppable Stoppable) { + if stoppable.Close() != nil { + atomic.AddUint32(&numErrors, 1) + } + wg.Done() + }(stoppable) + } + m.mux.Unlock() + + wg.Wait() + }) + + if numErrors > 0 { + err := errors.Errorf(closeMultiErr, m.name, numErrors, len(m.stoppables)) + jww.ERROR.Print(err.Error()) + return err + } + + return nil } diff --git a/stoppable/multi_test.go b/stoppable/multi_test.go index 5999f838ae44d0b64a96cd27c0fc1d8e28ee49fd..4a7eaf0873019d7ab9bd89e4f6aac2ba8f1661c9 100644 --- a/stoppable/multi_test.go +++ b/stoppable/multi_test.go @@ -8,114 +8,349 @@ package stoppable import ( + "fmt" "reflect" + "strconv" + "strings" + "sync" + "sync/atomic" "testing" "time" ) -// Tests happy path of NewMulti(). +// Tests that NewMulti returns a Multi that is running with the given name. func TestNewMulti(t *testing.T) { - name := "test name" + name := "testMulti" multi := NewMulti(name) - if multi.name != name || multi.running != 1 { - t.Errorf("NewMulti() returned Multi with incorrect values."+ - "\n\texpected: name: %s running: %d\n\treceived: name: %s running: %d", - name, 1, multi.name, multi.running) + if multi.name != name { + t.Errorf("NewMulti returned Multi with incorrect name."+ + "\nexpected: %s\nreceived: %s", name, multi.name) } } -// Tests happy path of Multi.IsRunning(). +// Tests that Multi.Add adds all the stoppables to the list. +func TestMulti_Add(t *testing.T) { + multi := NewMulti("testMulti") + expected := []Stoppable{ + NewSingle("testSingle0"), + NewMulti("testMulti0"), + NewSingle("testSingle1"), + NewMulti("testMulti1"), + } + + for _, stoppable := range expected { + multi.Add(stoppable) + } + + if !reflect.DeepEqual(multi.stoppables, expected) { + t.Errorf("Add did not add the correct Stoppables."+ + "\nexpected: %+v\nreceived: %+v", multi.stoppables, expected) + } +} + +// Unit test of Multi.Name. +func TestMulti_Name(t *testing.T) { + name := "testMulti" + multi := NewMulti(name) + + // Add stoppables and created list of their names + var nameList []string + for i := 0; i < 10; i++ { + newName := "" + if i%2 == 0 { + newName = "single" + strconv.Itoa(i) + multi.Add(NewSingle(newName)) + } else { + newMulti := NewMulti("multi" + strconv.Itoa(i)) + if i != 5 { + newMulti.Add(NewMulti("multiA")) + newMulti.Add(NewMulti("multiB")) + } + multi.Add(newMulti) + newName = newMulti.Name() + } + nameList = append(nameList, newName) + } + + expected := name + "{" + strings.Join(nameList, ", ") + "}" + + if multi.Name() != expected { + t.Errorf("Name failed to return the expected string."+ + "\nexpected: %s\nreceived: %s", expected, multi.Name()) + } +} + +// Tests that Multi.Name returns the expected string when it has no stoppables. +func TestMulti_Name_NoStoppables(t *testing.T) { + name := "testMulti" + multi := NewMulti(name) + + expected := name + "{}" + + if multi.Name() != expected { + t.Errorf("Name failed to return the expected string."+ + "\nexpected: %s\nreceived: %s", expected, multi.Name()) + } +} + +// Tests that Multi.GetStatus returns the expected Status. +func TestMulti_GetStatus(t *testing.T) { + multi := NewMulti("testMulti") + single1 := NewSingle("testSingle1") + single2 := NewSingle("testSingle2") + atomic.StoreUint32((*uint32)(&single2.status), uint32(Stopped)) + multi.Add(single1) + multi.Add(single2) + + status := multi.GetStatus() + if status != Running { + t.Errorf("GetStatus returned the wrong status."+ + "\nexpected: %s\nreceived: %s", Running, status) + } + + atomic.StoreUint32((*uint32)(&single1.status), uint32(Stopping)) + status = multi.GetStatus() + if status != Stopping { + t.Errorf("GetStatus returned the wrong status."+ + "\nexpected: %s\nreceived: %s", Stopping, status) + } + + atomic.StoreUint32((*uint32)(&single1.status), uint32(Stopped)) + status = multi.GetStatus() + if status != Stopped { + t.Errorf("GetStatus returned the wrong status."+ + "\nexpected: %s\nreceived: %s", Stopped, status) + } +} + +// Tests that Multi.GetStatus returns the expected Status when it has no +// children. +func TestMulti_GetStatus_NoChildren(t *testing.T) { + multi := NewMulti("testMulti") + + status := multi.GetStatus() + if status != Stopped { + t.Errorf("GetStatus returned the wrong status."+ + "\nexpected: %s\nreceived: %s", Stopped, status) + } +} + +// Tests that Multi.IsRunning returns the expected value when the Multi is +// marked as running, stopping, and stopped. func TestMulti_IsRunning(t *testing.T) { - multi := NewMulti("name") + multi := NewMulti("testMulti") + single1 := NewSingle("testSingle1") + single2 := NewSingle("testSingle2") + atomic.StoreUint32((*uint32)(&single2.status), uint32(Stopping)) + multi.Add(single1) + multi.Add(single2) + + if result := multi.IsRunning(); !result { + t.Errorf("IsRunning returned the wrong value when running."+ + "\nexpected: %t\nreceived: %t", true, result) + } - if !multi.IsRunning() { - t.Errorf("IsRunning() returned false when it should be running.") + atomic.StoreUint32((*uint32)(&single1.status), uint32(Stopping)) + atomic.StoreUint32((*uint32)(&single2.status), uint32(Stopped)) + if result := multi.IsRunning(); result { + t.Errorf("IsRunning returned the wrong value when stopping."+ + "\nexpected: %t\nreceived: %t", false, result) } - multi.running = 0 - if multi.IsRunning() { - t.Errorf("IsRunning() returned true when it should not be running.") + atomic.StoreUint32((*uint32)(&single2.status), uint32(Stopped)) + if result := multi.IsRunning(); result { + t.Errorf("IsRunning returned the wrong value when stopped."+ + "\nexpected: %t\nreceived: %t", false, result) } } -// Tests happy path of Multi.Add(). -func TestMulti_Add(t *testing.T) { - multi := NewMulti("multi name") - singles := []*Single{ - NewSingle("single name 1"), - NewSingle("single name 2"), - NewSingle("single name 3"), +// Tests that Multi.IsStopping returns the expected value when the Multi is +// marked as running, stopping, and stopped. +func TestMulti_IsStopping(t *testing.T) { + multi := NewMulti("testMulti") + single1 := NewSingle("testSingle1") + single2 := NewSingle("testSingle2") + atomic.StoreUint32((*uint32)(&single2.status), uint32(Stopped)) + multi.Add(single1) + multi.Add(single2) + + if result := multi.IsStopping(); result { + t.Errorf("IsStopping returned the wrong value when running."+ + "\nexpected: %t\nreceived: %t", true, result) } - for _, single := range singles { - multi.Add(single) + atomic.StoreUint32((*uint32)(&single1.status), uint32(Stopping)) + if result := multi.IsStopping(); !result { + t.Errorf("IsStopping returned the wrong value when stopping."+ + "\nexpected: %t\nreceived: %t", false, result) } - for i, single := range singles { - if !reflect.DeepEqual(single, multi.stoppables[i]) { - t.Errorf("Add() did not add the correct Stoppables."+ - "\n\texpected: %#v\n\treceived: %#v", single, multi.stoppables[i]) - } + atomic.StoreUint32((*uint32)(&single1.status), uint32(Stopped)) + if result := multi.IsStopping(); result { + t.Errorf("IsStopping returned the wrong value when stopped."+ + "\nexpected: %t\nreceived: %t", false, result) } } -// Tests happy path of Multi.Name(). -func TestMulti_Name(t *testing.T) { - name := "test name" - multi := NewMulti(name) +// Tests that Multi.IsStopped returns the expected value when the Multi is +// marked as running, stopping, and stopped. +func TestMulti_IsStopped(t *testing.T) { + multi := NewMulti("testMulti") + single1 := NewSingle("testSingle1") + single2 := NewSingle("testSingle2") + atomic.StoreUint32((*uint32)(&single2.status), uint32(Stopped)) + multi.Add(single1) + multi.Add(single2) + + if result := multi.IsStopped(); result { + t.Errorf("IsStopped returned the wrong value when running."+ + "\nexpected: %t\nreceived: %t", true, result) + } + + atomic.StoreUint32((*uint32)(&single1.status), uint32(Stopping)) + if result := multi.IsStopped(); result { + t.Errorf("IsStopped returned the wrong value when stopping."+ + "\nexpected: %t\nreceived: %t", false, result) + } + + atomic.StoreUint32((*uint32)(&single1.status), uint32(Stopped)) + if result := multi.IsStopped(); !result { + t.Errorf("IsStopped returned the wrong value when stopped."+ + "\nexpected: %t\nreceived: %t", false, result) + } +} + +// Tests that Multi.IsStopped returns true when all of the child stoppables are +// stopped. +func TestMulti_IsStopped_StoppedStatus(t *testing.T) { + multi := NewMulti("testMulti") singles := []*Single{ - NewSingle("single name 1"), - NewSingle("single name 2"), - NewSingle("single name 3"), + NewSingle("testSingle0"), + NewSingle("testSingle1"), + NewSingle("testSingle2"), + NewSingle("testSingle3"), + NewSingle("testSingle4"), + } + for _, single := range singles[:3] { + atomic.StoreUint32((*uint32)(&single.status), uint32(Stopped)) + multi.Add(single) } - expectedNames := []string{ - name + ": {}", - name + ": {" + singles[0].name + "}", - name + ": {" + singles[0].name + ", " + singles[1].name + "}", - name + ": {" + singles[0].name + ", " + singles[1].name + ", " + singles[2].name + "}", + subMulti := NewMulti("subMulti") + for _, single := range singles[3:] { + atomic.StoreUint32((*uint32)(&single.status), uint32(Stopped)) + subMulti.Add(single) } + multi.Add(subMulti) - for i, single := range singles { - if expectedNames[i] != multi.Name() { - t.Errorf("Name() returned the incorrect string."+ - "\n\texpected: %s\n\treceived: %s", expectedNames[0], multi.Name()) - } + if !multi.IsStopped() { + t.Error("IsStopped did not find all stoppables as stopped.") + } +} + +// Error path: tests that Multi.IsStopped returns false when not all of the +// child stoppables are stopped. +func TestMulti_IsStopped_NotStoppedError(t *testing.T) { + multi := NewMulti("testMulti") + singles := []*Single{ + NewSingle("testSingle0"), + NewSingle("testSingle1"), + NewSingle("testSingle2"), + NewSingle("testSingle3"), + NewSingle("testSingle4"), + } + for _, single := range singles { multi.Add(single) } + + for _, single := range singles[:4] { + atomic.StoreUint32((*uint32)(&single.status), uint32(Stopped)) + } + + if multi.IsStopped() { + t.Error("IsStopped found all the stoppables as stopped when some are " + + "still running") + } } -// Tests happy path of Multi.Close(). +// Tests that Multi.Close sends on all Single quit channels. func TestMulti_Close(t *testing.T) { - // Create new Multi and add Singles to it - multi := NewMulti("name") + multi := NewMulti("testMulti") singles := []*Single{ - NewSingle("single name 1"), - NewSingle("single name 2"), - NewSingle("single name 3"), + NewSingle("testSingle0"), + NewSingle("testSingle1"), + NewSingle("testSingle2"), + NewSingle("testSingle3"), + NewSingle("testSingle4"), } - for _, single := range singles { + for _, single := range singles[:3] { multi.Add(single) } + subMulti := NewMulti("subMulti") + for _, single := range singles[3:] { + subMulti.Add(single) + } + multi.Add(subMulti) - go func() { - select { - case <-singles[0].quit: - } - select { - case <-singles[1].quit: - } - select { - case <-singles[2].quit: - } - }() + for _, single := range singles { + go func(single *Single) { + select { + case <-time.NewTimer(5 * time.Millisecond).C: + t.Errorf("Single %s failed to quit.", single.Name()) + case <-single.Quit(): + } + }(single) + } + + err := multi.Close() + if err != nil { + t.Errorf("Close() returned an error: %v", err) + } - err := multi.Close(5 * time.Millisecond) + err = multi.Close() if err != nil { t.Errorf("Close() returned an error: %v", err) } +} + +// Error path: tests that Multi.Close returns the expected error when the Single +// stoppables are not running. +func TestMulti_Close_StoppableCloseError(t *testing.T) { + multi := NewMulti("testMulti") + var singles []*Single + for i := 0; i < 5; i++ { + single := NewSingle("testSingle" + strconv.Itoa(i)) + singles = append(singles, single) + multi.Add(single) + atomic.StoreUint32((*uint32)(&single.status), uint32(Stopped)) + } + + var wg sync.WaitGroup + for _, single := range singles { + wg.Add(1) + go func(single *Single) { + select { + case <-time.NewTimer(15 * time.Millisecond).C: + case <-single.Quit(): + t.Errorf("Single %s to quit when it should have failed.", + single.Name()) + } + wg.Done() + }(single) + } + + expectedErr := fmt.Sprintf(closeMultiErr, multi.name, 0, 0) + expectedErr = strings.SplitN(expectedErr, " 0/0", 2)[0] + + err := multi.Close() + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("Close() did not return the expected error."+ + "\nexpected: %s\nreceived: %v", expectedErr, err) + } + + wg.Wait() - err = multi.Close(0) + err = multi.Close() if err != nil { t.Errorf("Close() returned an error: %v", err) } diff --git a/stoppable/single.go b/stoppable/single.go index 2e8fa78a2a1c3f4f12752f045cb93c541da8412b..dfde7242ed83f0af975efa0656933b56d0b8145a 100644 --- a/stoppable/single.go +++ b/stoppable/single.go @@ -12,55 +12,109 @@ import ( jww "github.com/spf13/jwalterweatherman" "sync" "sync/atomic" - "time" ) -// Single allows stopping a single goroutine using a channel. -// It adheres to the stoppable interface. +// Error message. +const toStoppingErr = "failed to set the status of single stoppable %q to " + + "stopped when status is %s instead of %s" + +// Single allows stopping a single goroutine using a channel. It adheres to the +// Stoppable interface. type Single struct { - name string - quit chan struct{} - running uint32 - once sync.Once + name string + quit chan struct{} + status Status + once sync.Once } -// NewSingle returns a new single stoppable. +// NewSingle returns a new single Stoppable. func NewSingle(name string) *Single { return &Single{ - name: name, - quit: make(chan struct{}), - running: 1, + name: name, + quit: make(chan struct{}, 1), + status: Running, } } -// IsRunning returns true if the thread is still running. +// Name returns the name of the Single Stoppable. +func (s *Single) Name() string { + return s.name +} + +// GetStatus returns the status of the Stoppable. +func (s *Single) GetStatus() Status { + return Status(atomic.LoadUint32((*uint32)(&s.status))) +} + +// IsRunning returns true if Stoppable is marked as running. func (s *Single) IsRunning() bool { - return atomic.LoadUint32(&s.running) == 1 + return s.GetStatus() == Running } -// Quit returns the read only channel it will send the stop signal on. -func (s *Single) Quit() <-chan struct{} { - return s.quit +// IsStopping returns true if Stoppable is marked as stopping. +func (s *Single) IsStopping() bool { + return s.GetStatus() == Stopping } -// Name returns the name of the thread. This is designed to be -func (s *Single) Name() string { - return s.name +// IsStopped returns true if Stoppable is marked as stopped. +func (s *Single) IsStopped() bool { + return s.GetStatus() == Stopped +} + +// toStopping changes the status from running to stopping. An error is returned +// if the status is not already set to running. +func (s *Single) toStopping() error { + if !atomic.CompareAndSwapUint32((*uint32)(&s.status), uint32(Running), uint32(Stopping)) { + return errors.Errorf(toStoppingErr, s.Name(), s.GetStatus(), Running) + } + + jww.TRACE.Printf("Switched status of single stoppable %q from %s to %s.", + s.Name(), Running, Stopping) + + return nil +} + +// ToStopped changes the status from stopping to stopped. Panics if the status +// is not already set to stopping. +func (s *Single) ToStopped() { + if !atomic.CompareAndSwapUint32((*uint32)(&s.status), uint32(Stopping), uint32(Stopped)) { + jww.FATAL.Panicf("Failed to set the status of single stoppable %q to "+ + "stopped when status is %s instead of %s.", + s.Name(), s.GetStatus(), Stopping) + } + + jww.TRACE.Printf("Switched status of single stoppable %q from %s to %s.", + s.Name(), Stopping, Stopped) } -// Close signals the thread to time out and closes if it is still running. -func (s *Single) Close(timeout time.Duration) error { +// Quit returns a receive-only channel that will be triggered when the Stoppable +// quits. +func (s *Single) Quit() <-chan struct{} { + return s.quit +} + +// Close signals the Single to close via the quit channel. Returns an error if +// the status of the Single is not Running. +func (s *Single) Close() error { var err error + s.once.Do(func() { - timer := time.NewTimer(timeout) - select { - case <-timer.C: - jww.ERROR.Printf("Stopper for %s failed to stop after "+ - "timeout of %s", s.name, timeout) - err = errors.Errorf("%s failed to close", s.name) - case s.quit <- struct{}{}: + // Attempt to set status to stopping or return an error if unable + err = s.toStopping() + if err != nil { + return } - atomic.StoreUint32(&s.running, 0) + + jww.TRACE.Printf("Sending on quit channel to single stoppable %q.", + s.Name()) + + // Send on quit channel + s.quit <- struct{}{} }) + + if err != nil { + jww.ERROR.Print(err.Error()) + } + return err } diff --git a/stoppable/single_test.go b/stoppable/single_test.go index ceb5a9ecf6235a5de1884ed4d469f0a46aa0c0f4..c93a1ebfcc1815b2d5c82e7f2e2dc43ff96a076c 100644 --- a/stoppable/single_test.go +++ b/stoppable/single_test.go @@ -8,96 +8,254 @@ package stoppable import ( + "fmt" + "sync/atomic" "testing" "time" ) -// Tests happy path of NewSingle(). +// Tests that NewSingle returns a Single with the correct name and running. func TestNewSingle(t *testing.T) { - name := "test name" + name := "threadName" single := NewSingle(name) - if single.name != name || single.running != 1 { - t.Errorf("NewSingle() returned Single with incorrect values."+ - "\n\texpected: name: %s running: %d\n\treceived: name: %s running: %d", - name, 1, single.name, single.running) + if single.name != name { + t.Errorf("NewSingle returned Single with incorrect name."+ + "\nexpected: %s\nreceived: %s", name, single.name) + } + + if single.status != Running { + t.Errorf("NewSingle returned Single with incorrect status."+ + "\nexpected: %s\nreceived: %s", Running, single.status) + } +} + +// Unit test of Single.Name. +func TestSingle_Name(t *testing.T) { + name := "threadName" + single := NewSingle(name) + + if name != single.Name() { + t.Errorf("Name did not return the expected name."+ + "\nexpected: %s\nreceived: %s", name, single.Name()) } } -// Tests happy path of Single.IsRunning(). +// Tests that Single.GetStatus returns the expected Status. +func TestSingle_GetStatus(t *testing.T) { + single := NewSingle("threadName") + + status := single.GetStatus() + if status != Running { + t.Errorf("GetStatus returned the wrong status."+ + "\nexpected: %s\nreceived: %s", Running, status) + } + + atomic.StoreUint32((*uint32)(&single.status), uint32(Stopping)) + status = single.GetStatus() + if status != Stopping { + t.Errorf("GetStatus returned the wrong status."+ + "\nexpected: %s\nreceived: %s", Stopping, status) + } + + atomic.StoreUint32((*uint32)(&single.status), uint32(Stopped)) + status = single.GetStatus() + if status != Stopped { + t.Errorf("GetStatus returned the wrong status."+ + "\nexpected: %s\nreceived: %s", Stopped, status) + } +} + +// Tests that Single.IsRunning returns the expected value when the Single is +// marked as running, stopping, and stopped. func TestSingle_IsRunning(t *testing.T) { - single := NewSingle("name") + single := NewSingle("threadName") - if !single.IsRunning() { - t.Errorf("IsRunning() returned false when it should be running.") + if result := single.IsRunning(); !result { + t.Errorf("IsRunning returned the wrong value when running."+ + "\nexpected: %t\nreceived: %t", true, result) } - single.running = 0 - if single.IsRunning() { - t.Errorf("IsRunning() returned true when it should not be running.") + single.status = Stopping + if result := single.IsRunning(); result { + t.Errorf("IsRunning returned the wrong value when stopping."+ + "\nexpected: %t\nreceived: %t", false, result) + } + + single.status = Stopped + if result := single.IsRunning(); result { + t.Errorf("IsRunning returned the wrong value when stopped."+ + "\nexpected: %t\nreceived: %t", false, result) } } -// Tests happy path of Single.Quit(). -func TestSingle_Quit(t *testing.T) { - single := NewSingle("name") +// Tests that Single.IsStopping returns the expected value when the Single is +// marked as running, stopping, and stopped. +func TestSingle_IsStopping(t *testing.T) { + single := NewSingle("threadName") - go func() { - time.Sleep(150 * time.Nanosecond) - single.quit <- struct{}{} - }() + if result := single.IsStopping(); result { + t.Errorf("IsStopping returned the wrong value when running."+ + "\nexpected: %t\nreceived: %t", true, result) + } - timer := time.NewTimer(2 * time.Millisecond) - select { - case <-timer.C: - t.Errorf("Quit signal not received.") - case <-single.quit: + single.status = Stopping + if result := single.IsStopping(); !result { + t.Errorf("IsStopping returned the wrong value when stopping."+ + "\nexpected: %t\nreceived: %t", false, result) + } + + single.status = Stopped + if result := single.IsStopping(); result { + t.Errorf("IsStopping returned the wrong value when stopped."+ + "\nexpected: %t\nreceived: %t", false, result) } } -// Tests happy path of Single.Name(). -func TestSingle_Name(t *testing.T) { - name := "test name" - single := NewSingle(name) +// Tests that Single.IsStopped returns the expected value when the Single is +// marked as running, stopping, and stopped. +func TestSingle_IsStopped(t *testing.T) { + single := NewSingle("threadName") - if name != single.Name() { - t.Errorf("Name() returned the incorrect string."+ - "\n\texpected: %s\n\treceived: %s", name, single.Name()) + if result := single.IsStopped(); result { + t.Errorf("IsStopped returned the wrong value when running."+ + "\nexpected: %t\nreceived: %t", true, result) + } + + single.status = Stopping + if result := single.IsStopped(); result { + t.Errorf("IsStopped returned the wrong value when stopping."+ + "\nexpected: %t\nreceived: %t", false, result) + } + + single.status = Stopped + if result := single.IsStopped(); !result { + t.Errorf("IsStopped returned the wrong value when stopped."+ + "\nexpected: %t\nreceived: %t", false, result) } } -// Test happy path of Single.Close(). -func TestSingle_Close(t *testing.T) { - single := NewSingle("name") +// Tests that Single.toStopping changes the status to stopping. +func TestSingle_toStopping(t *testing.T) { + single := NewSingle("threadName") + + err := single.toStopping() + if err != nil { + t.Errorf("toStopping returned an error: %+v", err) + } + + if single.status != Stopping { + t.Errorf("toStopping failed to set the status correctly."+ + "\nexpected: %s\nreceived: %s", Stopping, single.status) + } +} + +// Error path: tests that Single.toStopping returns an error when failing to +// change the status to stopping when the current status is not running. +func TestSingle_toStopping_StatusError(t *testing.T) { + single := NewSingle("threadName") + single.status = Stopped + expectedErr := fmt.Sprintf( + toStoppingErr, single.Name(), single.GetStatus(), Running) + + err := single.toStopping() + if err == nil || err.Error() != expectedErr { + t.Errorf("toStopping failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } + + if single.status != Stopped { + t.Errorf("toStopping changed the status when the compare failed."+ + "\nexpected: %s\nreceived: %s", Stopped, single.status) + } +} + +// Tests that Single.ToStopped changes the status to stopped. +func TestSingle_ToStopped(t *testing.T) { + single := NewSingle("threadName") + + single.status = Stopping + single.ToStopped() + + if single.status != Stopped { + t.Errorf("ToStopped failed to set the status correctly."+ + "\nexpected: %s\nreceived: %s", Stopped, single.status) + } +} + +// Panic path: tests that Single.ToStopped panics when failing to change the +// status to stopped when the current status is not stopping. +func TestSingle_ToStopped_StatusPanic(t *testing.T) { + single := NewSingle("threadName") + + defer func() { + if r := recover(); r == nil { + t.Errorf("ToStopped failed to panic when the status should not " + + "have changed.") + } else { + if single.status != Running { + t.Errorf("ToStopped changed the status when the compare failed."+ + "\nexpected: %s\nreceived: %s", Running, single.status) + } + } + }() + + single.status = Running + single.ToStopped() +} + +// Tests that Single.Quit returns a channel that is triggered when the Single +// quit channel is triggered. +func TestSingle_Quit(t *testing.T) { + single := NewSingle("threadName") go func() { - time.Sleep(150 * time.Nanosecond) select { - case <-single.quit: + case <-time.NewTimer(5 * time.Millisecond).C: + t.Error("Timed out waiting for quit channel.") + case <-single.Quit(): } }() - err := single.Close(5 * time.Millisecond) - if err != nil { - t.Errorf("Close() returned an error: %v", err) - } + single.quit <- struct{}{} } -// Tests that Single.Close() returns an error when the timeout is reached. -func TestSingle_Close_Error(t *testing.T) { - single := NewSingle("name") - expectedErr := single.name + " failed to close" +// Test happy path of Single.Close(). +func TestSingle_Close(t *testing.T) { + single := NewSingle("threadName") + timeout := 10 * time.Millisecond go func() { - time.Sleep(3 * time.Millisecond) select { - case <-single.quit: + case <-time.NewTimer(timeout).C: + t.Errorf("Timed out waiting to receive on quit channel after %s.", + timeout) + case <-single.Quit(): + if !single.IsStopping() { + t.Errorf("Status of stoppable incorrect."+ + "\nexpected: %s\nreceived: %s", Stopping, single.status) + } + atomic.StoreUint32((*uint32)(&single.status), uint32(Stopped)) } }() - err := single.Close(2 * time.Millisecond) - if err == nil { - t.Errorf("Close() did not return the expected error."+ - "\n\texpected: %v\n\treceived: %v", expectedErr, err) + err := single.Close() + if err != nil { + t.Errorf("Close returned an error: %v", err) + } +} + +// Error path: tests that Single.Close returns an error when the status fails +// to change to stopping. +func TestSingle_Close_Error(t *testing.T) { + single := NewSingle("threadName") + single.status = Stopped + expectedErr := fmt.Sprintf( + toStoppingErr, single.Name(), single.GetStatus(), Running) + + err := single.Close() + if err == nil || err.Error() != expectedErr { + t.Errorf("Close did not return the expected error."+ + "\nexpected: %s\nreceived: %v", expectedErr, err) } } diff --git a/stoppable/status.go b/stoppable/status.go new file mode 100644 index 0000000000000000000000000000000000000000..1b306bd69a13394d8321c52b742ef5ecf82a83a9 --- /dev/null +++ b/stoppable/status.go @@ -0,0 +1,36 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +package stoppable + +import ( + "strconv" +) + +const ( + Running Status = iota + Stopping + Stopped +) + +// Status holds the current status of a Stoppable. +type Status uint32 + +// String prints a string representation of the current Status. This functions +// satisfies the fmt.Stringer interface. +func (s Status) String() string { + switch s { + case Running: + return "running" + case Stopping: + return "stopping" + case Stopped: + return "stopped" + default: + return "INVALID STATUS: " + strconv.FormatUint(uint64(s), 10) + } +} diff --git a/stoppable/status_test.go b/stoppable/status_test.go new file mode 100644 index 0000000000000000000000000000000000000000..c2f4bcd05f106e1f0bc6e6083a88096a3cdde1f7 --- /dev/null +++ b/stoppable/status_test.go @@ -0,0 +1,32 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +package stoppable + +import ( + "testing" +) + +// Unit test of Status.String. +func TestStatus_String(t *testing.T) { + testValues := []struct { + status Status + expected string + }{ + {Running, "running"}, + {Stopping, "stopping"}, + {Stopped, "stopped"}, + {100, "INVALID STATUS: 100"}, + } + + for i, val := range testValues { + if val.status.String() != val.expected { + t.Errorf("String did not return the expected value (%d)."+ + "\nexpected: %s\nreceived: %s", i, val.status.String(), val.expected) + } + } +} diff --git a/stoppable/stoppable.go b/stoppable/stoppable.go index 06947eb3bf5ff5ae3523b927e8fb7792848fd762..b5b072d1424feddf97cd029fa08d519ef2998c01 100644 --- a/stoppable/stoppable.go +++ b/stoppable/stoppable.go @@ -7,11 +7,80 @@ package stoppable -import "time" +import ( + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + "strings" + "time" +) -// Interface for stopping a goroutine. +// Error message returned after a comms operations ends and finds that its +// parent thread is stopping or stopped. +const ( + errKey = "[StoppableNotRunning]" + ErrMsg = "stoppable %q is not running, exiting %s early " + errKey + timeoutErr = "timed out after %s waiting for the stoppable to stop for %q" +) + +// pollPeriod is the duration to wait between polls to see of stoppables are +// stopped. +const pollPeriod = 100 * time.Millisecond + +// Stoppable interface for stopping a goroutine. All functions are thread safe. type Stoppable interface { - Close(timeout time.Duration) error - IsRunning() bool + // Name returns the name of the Stoppable. Name() string + + // GetStatus returns the status of the Stoppable. + GetStatus() Status + + // IsRunning returns true if the Stoppable is running. + IsRunning() bool + + // IsStopping returns true if Stoppable is marked as stopping. + IsStopping() bool + + // IsStopped returns true if Stoppable is marked as stopped. + IsStopped() bool + + // Close marks the Stoppable as stopping and issues a close signal to the + // Stoppable or any children it may have. + Close() error +} + +// WaitForStopped polls the stoppable and all its children to see if they are +// stopped. Returns an error if its times out waiting for all children to stop. +func WaitForStopped(s Stoppable, timeout time.Duration) error { + done := make(chan struct{}) + + // Launch the processes to check if all stoppables are stopped in separate + // goroutine so that when the timeout is reached, no time is wasted exiting + go func() { + for !s.IsStopped() { + time.Sleep(pollPeriod) + } + + select { + case done <- struct{}{}: + case <-time.NewTimer(50 * time.Millisecond).C: + } + }() + + select { + case <-done: + jww.INFO.Printf("All stoppables have stopped for %q.", s.Name()) + return nil + case <-time.NewTimer(timeout).C: + return errors.Errorf(timeoutErr, timeout, s.Name()) + } +} + +// CheckErr returns true if the error contains a stoppable error message. This +// function is used by callers to determine if a sub function quit due to a +// stoppable closing and tells the caller to exit. +func CheckErr(err error) bool { + if err == nil { + return false + } + return strings.Contains(err.Error(), errKey) } diff --git a/stoppable/stoppable_test.go b/stoppable/stoppable_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0f070317aedef9fca29a0b66d023b4e7d151517f --- /dev/null +++ b/stoppable/stoppable_test.go @@ -0,0 +1,123 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package stoppable + +import ( + "fmt" + "github.com/pkg/errors" + jww "github.com/spf13/jwalterweatherman" + "os" + "strconv" + "testing" + "time" +) + +func TestMain(m *testing.M) { + jww.SetStdoutThreshold(jww.LevelTrace) + + os.Exit(m.Run()) +} + +// Tests that WaitForStopped does not return an error when all children are +// stopped. +func TestWaitForStopped(t *testing.T) { + m := newTestMulti() + + err := m.Close() + if err != nil { + t.Errorf("Failed to close multi stoppable: %+v", err) + } + + err = WaitForStopped(m, 2*time.Second) + if err != nil { + t.Errorf("WaitForStopped returned an error: %+v", err) + } +} + +// Error path: tests that WaitForStopped returns an error if the timeout is +// reached before all stoppables are checked. +func TestWaitForStopped_TimeoutError(t *testing.T) { + m := newTestMulti() + + err := m.Close() + if err != nil { + t.Errorf("Failed to close multi stoppable: %+v", err) + } + + expectedErr := fmt.Sprintf(timeoutErr, time.Duration(0), m.Name()) + + err = WaitForStopped(m, 0) + if err == nil || err.Error() != expectedErr { + t.Errorf("WaitForStopped did not return the expected error."+ + "\nexpected: %s\nrecieved: %+v", expectedErr, err) + } +} + +// Tests that TestCheckErr returns true for stoppable errors and false for all +// other errors +func TestCheckErr(t *testing.T) { + testValues := []struct { + err error + expected bool + }{ + {errors.Errorf(ErrMsg, "testThre", "testFunc"), true}, + {errors.Errorf(ErrMsg, "", ""), true}, + {errors.Errorf(errKey), true}, + {errors.Errorf("Random error"), false}, + {errors.Errorf(""), false}, + {nil, false}, + } + + for i, val := range testValues { + result := CheckErr(val.err) + if result != val.expected { + t.Errorf("CheckErr failed to return the expected value (%d)."+ + "\nexpected: %t\nreceived: %t", i, val.expected, result) + } + } +} + +// newTestMulti creates a new Multi Stoppable that has many Single and Multi +// stoppable children. +func newTestMulti() *Multi { + singles := make([]*Single, 15) + for i := range singles { + singles[i] = NewSingle("testSingle_" + strconv.Itoa(i)) + go func(single *Single) { + <-single.Quit() + time.Sleep(600 * time.Millisecond) + single.ToStopped() + }(singles[i]) + } + + m := NewMulti("testMulti") + for _, s := range singles[:5] { + m.Add(s) + } + m0 := NewMulti("testMulti_0") + for _, s := range singles[5:8] { + m0.Add(s) + } + m.Add(m0) + m1 := NewMulti("testMulti_1") + for _, s := range singles[8:10] { + m1.Add(s) + } + m2 := NewMulti("testMulti_2") + for _, s := range singles[10:13] { + m2.Add(s) + } + m1.Add(m2) + m.Add(m1) + for _, s := range singles[13:] { + m.Add(s) + } + m.Add(NewMulti("testMulti_3")) + + return m +} diff --git a/storage/cmix/roundKeys_test.go b/storage/cmix/roundKeys_test.go index 6f69bd6628cdbe2e7bbda3a58b42adcdc01f2bce..0b701375bab6fee74bd509bab1d208f33dcde61b 100644 --- a/storage/cmix/roundKeys_test.go +++ b/storage/cmix/roundKeys_test.go @@ -23,40 +23,28 @@ import ( func TestRoundKeys_Encrypt_Consistency(t *testing.T) { const numKeys = 5 - expectedPayload := []byte{107, 20, 177, 34, 255, 243, 201, 126, 124, 105, 4, - 62, 204, 52, 56, 2, 60, 196, 105, 167, 80, 78, 189, 83, 248, 113, 207, - 34, 255, 55, 37, 48, 75, 130, 200, 218, 88, 16, 29, 171, 26, 26, 77, 59, - 244, 111, 117, 236, 102, 86, 32, 31, 223, 26, 151, 112, 191, 183, 152, - 18, 104, 58, 49, 42, 77, 233, 163, 193, 36, 7, 44, 173, 99, 65, 24, 127, - 197, 96, 51, 69, 8, 154, 35, 119, 147, 80, 113, 55, 173, 129, 151, 195, - 56, 11, 92, 2, 181, 135, 1, 114, 12, 197, 55, 252, 123, 89, 92, 185, 87, - 215, 193, 203, 199, 224, 58, 173, 193, 159, 166, 22, 60, 138, 97, 15, - 173, 213, 45, 236, 7, 66, 39, 168, 21, 26, 210, 66, 176, 135, 131, 113, - 157, 53, 120, 128, 187, 167, 127, 170, 248, 215, 158, 18, 61, 158, 137, - 62, 120, 254, 114, 93, 78, 11, 13, 104, 94, 232, 98, 108, 238, 42, 181, - 221, 128, 124, 188, 119, 13, 101, 7, 61, 85, 19, 20, 140, 32, 101, 39, - 151, 93, 134, 78, 155, 100, 110, 192, 76, 62, 249, 91, 105, 225, 180, - 95, 197, 101, 80, 8, 93, 139, 78, 109, 197, 255, 218, 6, 167, 49, 61, - 184, 178, 174, 155, 147, 238, 228, 169, 27, 175, 119, 76, 217, 240, 1, - 134, 114, 3, 179, 223, 152, 68, 152, 221, 44, 128, 55, 165, 206, 116, - 88, 188, 72, 41, 41, 9, 67, 188, 182, 118, 213, 25, 237, 146, 170, 80, - 42, 101, 230, 87, 244, 170, 176, 110, 94, 43, 110, 200, 54, 126, 206, - 252, 182, 21, 207, 142, 170, 150, 34, 155, 99, 110, 131, 120, 137, 255, - 200, 132, 249, 213, 180, 121, 235, 126, 30, 149, 18, 8, 159, 153, 73, - 71, 104, 246, 231, 168, 201, 108, 42, 10, 110, 35, 183, 160, 15, 11, - 171, 117, 0, 87, 251, 218, 121, 155, 237, 58, 24, 139, 217, 62, 238, - 255, 116, 172, 135, 221, 207, 163, 214, 62, 1, 144, 245, 233, 147, 188, - 67, 97, 161, 79, 109, 129, 114, 21, 183, 66, 54, 242, 120, 91, 158, 35, - 110, 167, 44, 54, 87, 208, 145, 212, 59, 160, 115, 214, 146, 201, 199, - 104, 86, 140, 131, 189, 146, 47, 165, 197, 90, 100, 105, 16, 223, 96, - 86, 132, 221, 190, 175, 241, 121, 157, 19, 190, 243, 191, 116, 92, 31, - 209, 147, 7, 233, 188, 114, 88, 225, 180, 52, 139, 70, 88, 193, 111, - 49, 209, 4, 19, 135, 206, 56, 164, 230, 222, 219, 153, 94, 163, 168, - 181, 185, 206, 124, 13, 179, 32, 93, 85, 6, 179, 57, 197, 89, 254, - 180, 133, 147, 174, 182, 38, 8, 127, 20, 133, 100, 20, 228, 62, 252, - 175, 50, 239, 179, 108, 59, 222, 29, 113, 140, 2, 104, 167, 175, 193, - 208, 149, 24, 135, 165, 106, 249, 164, 122, 139, 169, 193, 39, 209, 132, - 238, 23, 153, 115, 200, 104, 31} + expectedPayload := []byte{240, 199, 83, 226, 28, 164, 104, 139, 171, 255, 234, 86, 170, 65, 29, 254, 100, 4, 81, + 112, 154, 115, 224, 245, 29, 60, 226, 209, 135, 75, 108, 62, 95, 185, 211, 56, 83, 55, 250, 159, 173, 176, 137, + 181, 1, 155, 228, 223, 170, 232, 71, 225, 55, 27, 189, 218, 146, 74, 134, 133, 105, 17, 69, 105, 160, 60, 206, + 32, 244, 175, 98, 142, 217, 27, 92, 132, 225, 146, 171, 59, 2, 191, 220, 125, 212, 81, 114, 98, 75, 253, 93, + 126, 48, 230, 249, 118, 215, 90, 231, 126, 43, 235, 151, 191, 23, 77, 147, 98, 212, 86, 89, 42, 189, 24, 124, + 189, 201, 184, 82, 152, 255, 137, 119, 21, 74, 118, 157, 114, 229, 232, 36, 185, 104, 101, 132, 23, 79, 65, 195, + 53, 222, 27, 66, 80, 123, 252, 109, 254, 44, 120, 114, 126, 237, 159, 252, 185, 187, 95, 255, 31, 41, 245, 225, + 95, 101, 118, 190, 233, 44, 5, 42, 239, 140, 70, 216, 211, 129, 43, 189, 1, 11, 111, 2, 64, 254, 44, 87, 164, + 28, 188, 227, 1, 32, 134, 183, 156, 84, 222, 79, 27, 210, 124, 46, 153, 56, 122, 117, 17, 171, 85, 232, 112, + 170, 10, 31, 115, 17, 119, 233, 150, 200, 183, 198, 74, 70, 179, 135, 27, 195, 190, 56, 126, 143, 226, 93, 16, + 46, 147, 248, 128, 124, 182, 254, 187, 223, 187, 54, 181, 62, 89, 202, 176, 25, 249, 139, 167, 26, 98, 143, 3, + 78, 54, 116, 201, 6, 33, 158, 225, 254, 106, 15, 6, 175, 96, 2, 63, 0, 59, 188, 124, 120, 147, 95, 24, 26, 115, + 235, 154, 240, 65, 226, 133, 91, 249, 223, 55, 122, 0, 76, 225, 104, 101, 242, 46, 136, 122, 127, 159, 0, 9, + 210, 42, 181, 31, 94, 20, 106, 175, 195, 56, 223, 165, 217, 164, 93, 55, 190, 253, 192, 249, 117, 226, 222, 65, + 82, 136, 36, 58, 3, 246, 76, 101, 24, 20, 50, 89, 22, 144, 184, 38, 82, 103, 2, 48, 59, 73, 75, 58, 33, 206, 49, + 88, 201, 44, 176, 242, 248, 254, 127, 101, 62, 57, 103, 75, 213, 73, 30, 146, 223, 118, 104, 126, 189, 179, 132, + 25, 183, 178, 65, 131, 72, 121, 42, 170, 40, 186, 65, 73, 175, 234, 52, 10, 171, 36, 165, 24, 156, 12, 198, 100, + 77, 137, 91, 221, 152, 219, 207, 244, 44, 126, 178, 119, 133, 147, 158, 54, 188, 52, 10, 63, 138, 180, 44, 29, + 40, 236, 255, 163, 208, 2, 212, 184, 50, 157, 82, 199, 90, 1, 205, 214, 143, 123, 92, 210, 88, 98, 182, 197, 49, + 170, 100, 143, 145, 9, 156, 0, 45, 59, 196, 6, 8, 157, 98, 15, 111, 162, 51, 12, 223, 0, 173, 187, 178, 1, 156, + 68, 183, 64, 178, 250, 40, 65, 50, 161, 96, 163, 106, 14, 43, 179, 75, 199, 15, 223, 192, 121, 144, 223, 167, + 254, 150, 188} expectedKmacs := [][]byte{{110, 235, 79, 128, 16, 94, 181, 95, 101, 152, 187, 204, 87, 236, 211, 102, 88, 130, 191, 103, 23, 229, diff --git a/storage/e2e/manager.go b/storage/e2e/manager.go index 802e81ebe2329f5bd3714500e0229071eaea3bd3..6d7691b2b8f402c92ea6d4615a175b0b14b4b61e 100644 --- a/storage/e2e/manager.go +++ b/storage/e2e/manager.go @@ -8,6 +8,8 @@ package e2e import ( + "bytes" + "encoding/base64" "fmt" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" @@ -17,6 +19,8 @@ import ( "gitlab.com/elixxir/crypto/cyclic" dh "gitlab.com/elixxir/crypto/diffieHellman" "gitlab.com/xx_network/primitives/id" + "golang.org/x/crypto/blake2b" + "sort" ) const managerPrefix = "Manager{partner:%s}" @@ -198,3 +202,24 @@ func (m *Manager) GetMyOriginPrivateKey() *cyclic.Int { func (m *Manager) GetPartnerOriginPublicKey() *cyclic.Int { return m.originPartnerPubKey.DeepCopy() } + +const relationshipFpLength = 15 + +// GetRelationshipFingerprint returns a unique fingerprint for an E2E +// relationship. The fingerprint is a base 64 encoded hash of of the two +// relationship fingerprints truncated to 15 characters. +func (m *Manager) GetRelationshipFingerprint() string { + // Sort fingerprints + fps := [][]byte{m.receive.fingerprint, m.send.fingerprint} + less := func(i, j int) bool { return bytes.Compare(fps[i], fps[j]) == -1 } + sort.Slice(fps, less) + + // Hash fingerprints + h, _ := blake2b.New256(nil) + for _, fp := range fps { + h.Write(fp) + } + + // Base 64 encode hash and truncate + return base64.StdEncoding.EncodeToString(h.Sum(nil))[:relationshipFpLength] +} diff --git a/storage/e2e/manager_test.go b/storage/e2e/manager_test.go index 907bbff91b561d2267b71371356ae7bf6f332b86..195b0752a603002dcb395507fc0b7d361a0b235a 100644 --- a/storage/e2e/manager_test.go +++ b/storage/e2e/manager_test.go @@ -9,12 +9,14 @@ package e2e import ( "bytes" + "encoding/base64" "fmt" "gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/storage/versioned" "gitlab.com/elixxir/ekv" "gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/netTime" + "golang.org/x/crypto/blake2b" "math/rand" "reflect" "testing" @@ -283,3 +285,65 @@ func managersEqual(expected, received *Manager, t *testing.T) bool { return equal } + +// Unit test of Manager.GetRelationshipFingerprint. +func TestManager_GetRelationshipFingerprint(t *testing.T) { + m, _ := newTestManager(t) + m.receive.fingerprint = []byte{5} + m.send.fingerprint = []byte{10} + h, _ := blake2b.New256(nil) + h.Write(append(m.receive.fingerprint, m.send.fingerprint...)) + expected := base64.StdEncoding.EncodeToString(h.Sum(nil))[:relationshipFpLength] + + fp := m.GetRelationshipFingerprint() + if fp != expected { + t.Errorf("GetRelationshipFingerprint did not return the expected "+ + "fingerprint.\nexpected: %s\nreceived: %s", expected, fp) + } + + // Flip the order and show that the output is the same. + m.receive.fingerprint, m.send.fingerprint = m.send.fingerprint, m.receive.fingerprint + + fp = m.GetRelationshipFingerprint() + if fp != expected { + t.Errorf("GetRelationshipFingerprint did not return the expected "+ + "fingerprint.\nexpected: %s\nreceived: %s", expected, fp) + } +} + +// Tests the consistency of the output of Manager.GetRelationshipFingerprint. +func TestManager_GetRelationshipFingerprint_Consistency(t *testing.T) { + m, _ := newTestManager(t) + prng := rand.New(rand.NewSource(42)) + expectedFps := []string{ + "GmeTCfxGOqRqeID", "gbpJjHd3tIe8BKy", "2/ZdG+WNzODJBiF", + "+V1ySeDLQfQNSkv", "23OMC+rBmCk+gsu", "qHu5MUVs83oMqy8", + "kuXqxsezI0kS9Bc", "SlEhsoZ4BzAMTtr", "yG8m6SPQfV/sbTR", + "j01ZSSm762TH7mj", "SKFDbFvsPcohKPw", "6JB5HK8DHGwS4uX", + "dU3mS1ujduGD+VY", "BDXAy3trbs8P4mu", "I4HoXW45EwWR0oD", + "661YH2l2jfOkHbA", "cSS9ZyTOQKVx67a", "ojfubzDIsMNYc/t", + "2WrEw83Yz6Rhq9I", "TQILxBIUWMiQS2j", "rEqdieDTXJfCQ6I", + } + + for i, expected := range expectedFps { + prng.Read(m.receive.fingerprint) + prng.Read(m.send.fingerprint) + + fp := m.GetRelationshipFingerprint() + if fp != expected { + t.Errorf("GetRelationshipFingerprint did not return the expected "+ + "fingerprint (%d).\nexpected: %s\nreceived: %s", i, expected, fp) + } + + // Flip the order and show that the output is the same. + m.receive.fingerprint, m.send.fingerprint = m.send.fingerprint, m.receive.fingerprint + + fp = m.GetRelationshipFingerprint() + if fp != expected { + t.Errorf("GetRelationshipFingerprint did not return the expected "+ + "fingerprint (%d).\nexpected: %s\nreceived: %s", i, expected, fp) + } + + // fmt.Printf("\"%s\",\n", fp) // Uncomment to reprint expected values + } +} diff --git a/storage/e2e/relationship.go b/storage/e2e/relationship.go index a492a72ed51d0f48f08657d786bc8dc6a3c861ed..f92e12b6901b206dcdf2cae00200555eb762894b 100644 --- a/storage/e2e/relationship.go +++ b/storage/e2e/relationship.go @@ -71,7 +71,7 @@ func NewRelationship(manager *Manager, t RelationshipType, if err := s.save(); err != nil { jww.FATAL.Panicf("Failed to Send session after setting to "+ - "confimred: %+v", err) + "confirmed: %+v", err) } r.addSession(s) diff --git a/storage/e2e/session.go b/storage/e2e/session.go index ad684725a04918389f706566e44642c6c1fbaf16..6e85d873e36768d2372b5f713c45527625233c3c 100644 --- a/storage/e2e/session.go +++ b/storage/e2e/session.go @@ -105,8 +105,8 @@ func newSession(ship *relationship, t RelationshipType, myPrivKey, partnerPubKey negotiationStatus Negotiation, e2eParams params.E2ESessionParams) *Session { if e2eParams.MinKeys < 10 { - jww.FATAL.Panicf("Cannot create a session with a minnimum number " + - "of keys less than 10") + jww.FATAL.Panicf("Cannot create a session with a minimum number "+ + "of keys (%d) less than 10", e2eParams.MinKeys) } session := &Session{ diff --git a/storage/e2e/store.go b/storage/e2e/store.go index e647cacab042388b667d383f670c8082c450b591..b6dc289c9b83edfa42e1468a015203d67dd7c1cd 100644 --- a/storage/e2e/store.go +++ b/storage/e2e/store.go @@ -14,6 +14,7 @@ import ( "gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/storage/utility" "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/diffieHellman" "gitlab.com/elixxir/crypto/fastRNG" @@ -180,7 +181,7 @@ func (s *Store) AddPartner(partnerID *id.ID, partnerPubKey, myPrivKey *cyclic.In s.managers[*partnerID] = m if err := s.save(); err != nil { - jww.FATAL.Printf("Failed to add Parter %s: Save of store failed: %s", + jww.FATAL.Printf("Failed to add Partner %s: Save of store failed: %s", partnerID, err) } @@ -200,6 +201,28 @@ func (s *Store) GetPartner(partnerID *id.ID) (*Manager, error) { return m, nil } +// GetPartnerContact find the partner with the given ID and assembles and +// returns a contact.Contact with their ID and DH key. An error is returned if +// no partner exists for the given ID. +func (s *Store) GetPartnerContact(partnerID *id.ID) (contact.Contact, error) { + s.mux.RLock() + defer s.mux.RUnlock() + + // Get partner + m, exists := s.managers[*partnerID] + if !exists { + return contact.Contact{}, errors.New(NoPartnerErrorStr) + } + + // Assemble Contact + c := contact.Contact{ + ID: m.GetPartnerID(), + DhPubKey: m.GetPartnerOriginPublicKey(), + } + + return c, nil +} + // PopKey pops a key for use based upon its fingerprint. func (s *Store) PopKey(f format.Fingerprint) (*Key, bool) { return s.fingerprints.Pop(f) diff --git a/storage/e2e/store_test.go b/storage/e2e/store_test.go index 944b2517df666e536db085d6ce3f0a001316aa2d..ef23f381f0370afec21f22c75d65dee892c93163 100644 --- a/storage/e2e/store_test.go +++ b/storage/e2e/store_test.go @@ -11,6 +11,7 @@ import ( "bytes" "gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/crypto/contact" "gitlab.com/elixxir/crypto/cyclic" "gitlab.com/elixxir/crypto/diffieHellman" "gitlab.com/elixxir/crypto/fastRNG" @@ -118,7 +119,7 @@ func TestStore_GetPartner(t *testing.T) { p := params.GetDefaultE2ESessionParams() expectedManager := newManager(s.context, s.kv, partnerID, s.dhPrivateKey, pubKey, p, p) - s.AddPartner(partnerID, pubKey, s.dhPrivateKey, p, p) + _ = s.AddPartner(partnerID, pubKey, s.dhPrivateKey, p, p) m, err := s.GetPartner(partnerID) if err != nil { @@ -147,6 +148,41 @@ func TestStore_GetPartner_Error(t *testing.T) { } } +// Tests happy path of Store.GetPartnerContact. +func TestStore_GetPartnerContact(t *testing.T) { + s, _, _ := makeTestStore() + partnerID := id.NewIdFromUInt(rand.Uint64(), id.User, t) + pubKey := diffieHellman.GeneratePublicKey(s.dhPrivateKey, s.grp) + p := params.GetDefaultE2ESessionParams() + expected := contact.Contact{ + ID: partnerID, + DhPubKey: pubKey, + } + _ = s.AddPartner(partnerID, pubKey, s.dhPrivateKey, p, p) + + c, err := s.GetPartnerContact(partnerID) + if err != nil { + t.Errorf("GetPartnerContact() produced an error: %+v", err) + } + + if !reflect.DeepEqual(expected, c) { + t.Errorf("GetPartnerContact() returned wrong Contact."+ + "\nexpected: %s\nreceived: %s", expected, c) + } +} + +// Tests that Store.GetPartnerContact returns an error for non existent partnerID. +func TestStore_GetPartnerContact_Error(t *testing.T) { + s, _, _ := makeTestStore() + partnerID := id.NewIdFromUInt(rand.Uint64(), id.User, t) + + _, err := s.GetPartnerContact(partnerID) + if err == nil || err.Error() != NoPartnerErrorStr { + t.Errorf("GetPartnerContact() did not produce the expected error."+ + "\nexpected: %s\nreceived: %+v", NoPartnerErrorStr, err) + } +} + // Tests happy path of Store.PopKey. func TestStore_PopKey(t *testing.T) { s, _, _ := makeTestStore() diff --git a/storage/hostList/hostList.go b/storage/hostList/hostList.go new file mode 100644 index 0000000000000000000000000000000000000000..5242e7b8483ace3820111aa6c22455c7ba5a0257 --- /dev/null +++ b/storage/hostList/hostList.go @@ -0,0 +1,116 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +package hostList + +import ( + "bytes" + "github.com/pkg/errors" + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/netTime" +) + +// Storage values. +const ( + hostListPrefix = "hostLists" + hostListKey = "hostListIDs" + hostListVersion = 0 +) + +// Error messages. +const ( + getStorageErr = "failed to get host list from storage: %+v" + unmarshallIdErr = "unmarshal host list error: %+v" + unmarshallLenErr = "malformed data: length of data %d incorrect" +) + +type Store struct { + kv *versioned.KV +} + +// NewStore creates a new Store with a prefixed KV. +func NewStore(kv *versioned.KV) *Store { + return &Store{ + kv: kv.Prefix(hostListPrefix), + } +} + +// Store saves the list of host IDs to storage. +func (s *Store) Store(list []*id.ID) error { + obj := &versioned.Object{ + Version: hostListVersion, + Data: marshalHostList(list), + Timestamp: netTime.Now(), + } + + return s.kv.Set(hostListKey, hostListVersion, obj) +} + +// Get returns the host list from storage. +func (s *Store) Get() ([]*id.ID, error) { + obj, err := s.kv.Get(hostListKey, hostListVersion) + if err != nil { + return nil, errors.Errorf(getStorageErr, err) + } + + return unmarshalHostList(obj.Data) +} + +// marshalHostList marshals the list of IDs into a byte slice. +func marshalHostList(list []*id.ID) []byte { + buff := bytes.NewBuffer(nil) + buff.Grow(len(list) * id.ArrIDLen) + + for _, hid := range list { + if hid != nil { + buff.Write(hid.Marshal()) + } else { + buff.Write((&id.ID{}).Marshal()) + } + } + + return buff.Bytes() +} + +// unmarshalHostList unmarshal the host list data into an ID list. An error is +// returned if an ID cannot be unmarshalled or if the data is not of the correct +// length. +func unmarshalHostList(data []byte) ([]*id.ID, error) { + // Return an error if the data is not of the required length + if len(data)%id.ArrIDLen != 0 { + return nil, errors.Errorf(unmarshallLenErr, len(data)) + } + + buff := bytes.NewBuffer(data) + list := make([]*id.ID, 0, len(data)/id.ArrIDLen) + + // Read each ID from data, unmarshal, and add to list + length := id.ArrIDLen + for n := buff.Next(length); len(n) == length; n = buff.Next(length) { + hid, err := id.Unmarshal(n) + if err != nil { + return nil, errors.Errorf(unmarshallIdErr, err) + } + + // If the ID is all zeroes, then treat it as a nil ID. + if *hid == (id.ID{}) { + hid = nil + } + + list = append(list, hid) + } + + return list, nil +} diff --git a/storage/hostList/hostList_test.go b/storage/hostList/hostList_test.go new file mode 100644 index 0000000000000000000000000000000000000000..32780fae0a09db487520596e3748611a1bc5636c --- /dev/null +++ b/storage/hostList/hostList_test.go @@ -0,0 +1,114 @@ +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +//////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +//////////////////////////////////////////////////////////////////////////////// + +package hostList + +import ( + "fmt" + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/elixxir/ekv" + "gitlab.com/xx_network/primitives/id" + "reflect" + "strings" + "testing" +) + +// Unit test of NewStore. +func TestNewStore(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + expected := &Store{kv: kv.Prefix(hostListPrefix)} + + s := NewStore(kv) + + if !reflect.DeepEqual(expected, s) { + t.Errorf("NewStore did not return the expected object."+ + "\nexpected: %+v\nreceived: %+v", expected, s) + } +} + +// Tests that a host list saved by Store.Store matches the host list returned +// by Store.Get. +func TestStore_Store_Get(t *testing.T) { + s := NewStore(versioned.NewKV(make(ekv.Memstore))) + list := []*id.ID{ + id.NewIdFromString("histID_1", id.Node, t), + nil, + id.NewIdFromString("histID_2", id.Node, t), + id.NewIdFromString("histID_3", id.Node, t), + } + + err := s.Store(list) + if err != nil { + t.Errorf("Store returned an error: %+v", err) + } + + newList, err := s.Get() + if err != nil { + t.Errorf("Get returned an error: %+v", err) + } + + if !reflect.DeepEqual(list, newList) { + t.Errorf("Failed to save and load host list."+ + "\nexpected: %+v\nreceived: %+v", list, newList) + } +} + +// Error path: tests that Store.Get returns an error if not host list is +// saved in storage. +func TestStore_Get_StorageError(t *testing.T) { + s := NewStore(versioned.NewKV(make(ekv.Memstore))) + expectedErr := strings.SplitN(getStorageErr, "%", 2)[0] + + _, err := s.Get() + if err == nil || !strings.Contains(err.Error(), expectedErr) { + t.Errorf("Get failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} + +// Tests that a list of IDs that is marshalled using marshalHostList and +// unmarshalled using unmarshalHostList matches the original. +func Test_marshalHostList_unmarshalHostList(t *testing.T) { + list := []*id.ID{ + id.NewIdFromString("histID_1", id.Node, t), + nil, + id.NewIdFromString("histID_2", id.Node, t), + id.NewIdFromString("histID_3", id.Node, t), + } + + data := marshalHostList(list) + + newList, err := unmarshalHostList(data) + if err != nil { + t.Errorf("unmarshalHostList produced an error: %+v", err) + } + + if !reflect.DeepEqual(list, newList) { + t.Errorf("Failed to marshal and unmarshal ID list."+ + "\nexpected: %+v\nreceived: %+v", list, newList) + } +} + +// Error path: tests that unmarshalHostList returns an error if the data is not +// of the correct length. +func Test_unmarshalHostList_InvalidDataErr(t *testing.T) { + data := []byte("Invalid Data") + expectedErr := fmt.Sprintf(unmarshallLenErr, len(data)) + + _, err := unmarshalHostList(data) + if err == nil || err.Error() != expectedErr { + t.Errorf("unmarshalHostList failed to return the expected error."+ + "\nexpected: %s\nreceived: %+v", expectedErr, err) + } +} diff --git a/storage/ndf.go b/storage/ndf.go index 14cb4147b89a4fa53de52e505627c12dbd3abc15..1b081fd0f68c32b140492cc5a61b4f8052eb2134 100644 --- a/storage/ndf.go +++ b/storage/ndf.go @@ -13,25 +13,25 @@ import ( "gitlab.com/xx_network/primitives/ndf" ) -const baseNdfKey = "baseNdf" +const ndfKey = "ndf" -func (s *Session) SetBaseNDF(def *ndf.NetworkDefinition) { - err := utility.SaveNDF(s.kv, baseNdfKey, def) +func (s *Session) SetNDF(def *ndf.NetworkDefinition) { + err := utility.SaveNDF(s.kv, ndfKey, def) if err != nil { - jww.FATAL.Printf("Failed to dave the base NDF: %s", err) + jww.FATAL.Printf("Failed to dave the NDF: %+v", err) } - s.baseNdf = def + s.ndf = def } -func (s *Session) GetBaseNDF() *ndf.NetworkDefinition { - if s.baseNdf != nil { - return s.baseNdf +func (s *Session) GetNDF() *ndf.NetworkDefinition { + if s.ndf != nil { + return s.ndf } - def, err := utility.LoadNDF(s.kv, baseNdfKey) + def, err := utility.LoadNDF(s.kv, ndfKey) if err != nil { - jww.FATAL.Printf("Could not load the base NDF: %s", err) + jww.FATAL.Printf("Could not load the NDF: %+v", err) } - s.baseNdf = def + s.ndf = def return def } diff --git a/storage/reception/IdentityUse.go b/storage/reception/IdentityUse.go index 2c8b9df3b64601651d7489e96935ff1a3d360db9..08f1a070aadc0ac5271156516e860371c0ca7daa 100644 --- a/storage/reception/IdentityUse.go +++ b/storage/reception/IdentityUse.go @@ -1,12 +1,15 @@ package reception import ( + "fmt" "github.com/pkg/errors" "gitlab.com/elixxir/client/storage/rounds" "gitlab.com/elixxir/crypto/hash" "gitlab.com/xx_network/crypto/randomness" "io" "math/big" + "strconv" + "strings" "time" ) @@ -48,3 +51,17 @@ func (iu IdentityUse) setSamplingPeriod(rng io.Reader) (IdentityUse, error) { iu.EndRequest = iu.EndValid.Add(iu.RequestMask - time.Duration(periodOffset)) return iu, nil } + +func (iu IdentityUse) GoString() string { + str := make([]string, 0, 7) + + str = append(str, "Identity:"+iu.Identity.GoString()) + str = append(str, "StartRequest:"+iu.StartRequest.String()) + str = append(str, "EndRequest:"+iu.EndRequest.String()) + str = append(str, "Fake:"+strconv.FormatBool(iu.Fake)) + str = append(str, "UR:"+fmt.Sprintf("%+v", iu.UR)) + str = append(str, "ER:"+fmt.Sprintf("%+v", iu.ER)) + str = append(str, "CR:"+fmt.Sprintf("%+v", iu.CR)) + + return "{" + strings.Join(str, ", ") + "}" +} diff --git a/storage/reception/fake.go b/storage/reception/fake.go index 38cb63a241cc25122b98ff8b5a1762da0f3994a5..3e6ba00fc711c097b7b765027962e60dc81d98bb 100644 --- a/storage/reception/fake.go +++ b/storage/reception/fake.go @@ -10,7 +10,8 @@ import ( // generateFakeIdentity generates a fake identity of the given size with the // given random number generator -func generateFakeIdentity(rng io.Reader, idSize uint, now time.Time) (IdentityUse, error) { +func generateFakeIdentity(rng io.Reader, addressSize uint8, + now time.Time) (IdentityUse, error) { // Randomly generate an identity randIdBytes := make([]byte, id.ArrIDLen-1) if _, err := rng.Read(randIdBytes); err != nil { @@ -23,7 +24,8 @@ func generateFakeIdentity(rng io.Reader, idSize uint, now time.Time) (IdentityUs randID.SetType(id.User) // Generate the current ephemeral ID from the random identity - ephID, start, end, err := ephemeral.GetId(randID, idSize, now.UnixNano()) + ephID, start, end, err := ephemeral.GetId( + randID, uint(addressSize), now.UnixNano()) if err != nil { return IdentityUse{}, errors.WithMessage(err, "failed to generate an "+ "ephemeral ID for random identity when none is available") @@ -33,6 +35,7 @@ func generateFakeIdentity(rng io.Reader, idSize uint, now time.Time) (IdentityUs Identity: Identity{ EphId: ephID, Source: randID, + AddressSize: addressSize, End: end, ExtraChecks: 0, StartValid: start, diff --git a/storage/reception/fake_test.go b/storage/reception/fake_test.go index 2c748ac2258e0950e8901a2748c0057c498770ef..0cb52480ac8eb3b713f9334eefa1dad9b4351dc3 100644 --- a/storage/reception/fake_test.go +++ b/storage/reception/fake_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "math" "math/rand" + "strconv" "strings" "testing" "time" @@ -13,12 +14,14 @@ import ( func Test_generateFakeIdentity(t *testing.T) { rng := rand.New(rand.NewSource(42)) + addressSize := uint8(15) end, _ := json.Marshal(time.Unix(0, 1258494203759765625)) startValid, _ := json.Marshal(time.Unix(0, 1258407803759765625)) endValid, _ := json.Marshal(time.Unix(0, 1258494203759765625)) expected := "{\"EphId\":[0,0,0,0,0,0,46,197]," + "\"Source\":[83,140,127,150,177,100,191,27,151,187,159,75,180,114," + "232,159,91,20,132,242,82,9,201,217,52,62,146,186,9,221,157,82,3]," + + "\"AddressSize\":" + strconv.Itoa(int(addressSize)) + "," + "\"End\":" + string(end) + ",\"ExtraChecks\":0," + "\"StartValid\":" + string(startValid) + "," + "\"EndValid\":" + string(endValid) + "," + @@ -28,7 +31,7 @@ func Test_generateFakeIdentity(t *testing.T) { timestamp := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) - received, err := generateFakeIdentity(rng, 15, timestamp) + received, err := generateFakeIdentity(rng, addressSize, timestamp) if err != nil { t.Errorf("generateFakeIdentity() returned an error: %+v", err) } @@ -58,7 +61,7 @@ func Test_generateFakeIdentity_GetEphemeralIdError(t *testing.T) { rng := rand.New(rand.NewSource(42)) timestamp := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) - _, err := generateFakeIdentity(rng, math.MaxUint64, timestamp) + _, err := generateFakeIdentity(rng, math.MaxInt8, timestamp) if err == nil || !strings.Contains(err.Error(), "ephemeral ID") { t.Errorf("generateFakeIdentity() did not return the correct error on "+ "failure to generate ephemeral ID: %+v", err) diff --git a/storage/reception/identity.go b/storage/reception/identity.go index 4c65d9696ba4a6d0fd567395a4d86e8bd9145b45..5d0721e5c0830edbb01b6e95ea8a6f63ad0bf638 100644 --- a/storage/reception/identity.go +++ b/storage/reception/identity.go @@ -8,6 +8,7 @@ import ( "gitlab.com/xx_network/primitives/id/ephemeral" "gitlab.com/xx_network/primitives/netTime" "strconv" + "strings" "time" ) @@ -16,8 +17,9 @@ const identityStorageVersion = 0 type Identity struct { // Identity - EphId ephemeral.Id - Source *id.ID + EphId ephemeral.Id + Source *id.ID + AddressSize uint8 // Usage variables End time.Time // Timestamp when active polling will stop @@ -76,13 +78,30 @@ func (i Identity) delete(kv *versioned.KV) error { return kv.Delete(identityStorageKey, identityStorageVersion) } -func (i *Identity) String() string { +func (i Identity) String() string { return strconv.FormatInt(i.EphId.Int64(), 16) + " " + i.Source.String() } +func (i Identity) GoString() string { + str := make([]string, 0, 9) + + str = append(str, "EphId:"+strconv.FormatInt(i.EphId.Int64(), 16)) + str = append(str, "Source:"+i.Source.String()) + str = append(str, "AddressSize:"+strconv.FormatUint(uint64(i.AddressSize), 10)) + str = append(str, "End:"+i.End.String()) + str = append(str, "ExtraChecks:"+strconv.FormatUint(uint64(i.ExtraChecks), 10)) + str = append(str, "StartValid:"+i.StartValid.String()) + str = append(str, "EndValid:"+i.EndValid.String()) + str = append(str, "RequestMask:"+i.RequestMask.String()) + str = append(str, "Ephemeral:"+strconv.FormatBool(i.Ephemeral)) + + return "{" + strings.Join(str, ", ") + "}" +} + func (i Identity) Equal(b Identity) bool { return i.EphId == b.EphId && i.Source.Cmp(b.Source) && + i.AddressSize == b.AddressSize && i.End.Equal(b.End) && i.ExtraChecks == b.ExtraChecks && i.StartValid.Equal(b.StartValid) && diff --git a/storage/reception/identity_test.go b/storage/reception/identity_test.go index d80b9501486c94045209da5c99dc0b77a07306f8..1fada0d707c848b5d2ea9f66398b4c1cc2c5a0c7 100644 --- a/storage/reception/identity_test.go +++ b/storage/reception/identity_test.go @@ -16,6 +16,7 @@ func TestIdentity_EncodeDecode(t *testing.T) { r := Identity{ EphId: ephemeral.Id{}, Source: &id.Permissioning, + AddressSize: 15, End: netTime.Now().Round(0), ExtraChecks: 12, StartValid: netTime.Now().Round(0), @@ -45,6 +46,7 @@ func TestIdentity_Delete(t *testing.T) { r := Identity{ EphId: ephemeral.Id{}, Source: &id.Permissioning, + AddressSize: 15, End: netTime.Now().Round(0), ExtraChecks: 12, StartValid: netTime.Now().Round(0), @@ -90,11 +92,11 @@ func TestIdentity_Equal(t *testing.T) { if !a.Identity.Equal(b.Identity) { t.Errorf("Equal() found two equal identities as unequal."+ - "\na: %s\nb: %s", a.String(), b.String()) + "\na: %s\nb: %s", a, b) } if a.Identity.Equal(c.Identity) { t.Errorf("Equal() found two unequal identities as equal."+ - "\na: %s\nc: %s", a.String(), c.String()) + "\na: %s\nc: %s", a, c) } } diff --git a/storage/reception/store.go b/storage/reception/store.go index 08498aa5c0aa856727fe60c989419f2aeb18616a..b6bdf41f7db520e5e9d88880a1246ced9dc2b3b0 100644 --- a/storage/reception/store.go +++ b/storage/reception/store.go @@ -1,7 +1,6 @@ package reception import ( - "bytes" "encoding/json" "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" @@ -12,7 +11,6 @@ import ( "gitlab.com/xx_network/primitives/netTime" "golang.org/x/crypto/blake2b" "io" - "strconv" "sync" "time" ) @@ -20,17 +18,11 @@ import ( const receptionPrefix = "reception" const receptionStoreStorageKey = "receptionStoreKey" const receptionStoreStorageVersion = 0 -const receptionIDSizeStorageKey = "receptionIDSizeKey" -const receptionIDSizeStorageVersion = 0 -const defaultIDSize = 12 type Store struct { // Identities which are being actively checked - active []*registration - present map[idHash]interface{} - idSize int - idSizeCond *sync.Cond - isIdSizeSet bool + active []*registration + present map[idHash]struct{} kv *versioned.KV @@ -56,13 +48,10 @@ func makeIdHash(ephID ephemeral.Id, source *id.ID) idHash { // NewStore creates a new reception store that starts empty. func NewStore(kv *versioned.KV) *Store { - kv = kv.Prefix(receptionPrefix) s := &Store{ - active: make([]*registration, 0), - present: make(map[idHash]interface{}), - idSize: defaultIDSize * 2, - kv: kv, - idSizeCond: sync.NewCond(&sync.Mutex{}), + active: []*registration{}, + present: make(map[idHash]struct{}), + kv: kv.Prefix(receptionPrefix), } // Store the empty list @@ -70,53 +59,37 @@ func NewStore(kv *versioned.KV) *Store { jww.FATAL.Panicf("Failed to save new reception store: %+v", err) } - // Update the size so queries can be made - s.UpdateIdSize(defaultIDSize) - return s } func LoadStore(kv *versioned.KV) *Store { kv = kv.Prefix(receptionPrefix) - s := &Store{ - kv: kv, - present: make(map[idHash]interface{}), - idSizeCond: sync.NewCond(&sync.Mutex{}), - } // Load the versioned object for the reception list - vo, err := kv.Get(receptionStoreStorageKey, - receptionStoreStorageVersion) + vo, err := kv.Get(receptionStoreStorageKey, receptionStoreStorageVersion) if err != nil { jww.FATAL.Panicf("Failed to get the reception storage list: %+v", err) } - identities := make([]storedReference, len(s.active)) - err = json.Unmarshal(vo.Data, &identities) - if err != nil { - jww.FATAL.Panicf("Failed to unmarshal the reception storage list: %+v", err) + // JSON unmarshal identities list + var identities []storedReference + if err = json.Unmarshal(vo.Data, &identities); err != nil { + jww.FATAL.Panicf("Failed to unmarshal the stored identity list: %+v", err) + } + + s := &Store{ + active: make([]*registration, len(identities)), + present: make(map[idHash]struct{}, len(identities)), + kv: kv, } - s.active = make([]*registration, len(identities)) for i, sr := range identities { s.active[i], err = loadRegistration(sr.Eph, sr.Source, sr.StartValid, s.kv) if err != nil { jww.FATAL.Panicf("Failed to load registration for %s: %+v", regPrefix(sr.Eph, sr.Source, sr.StartValid), err) } - s.present[makeIdHash(sr.Eph, sr.Source)] = nil - } - - // Load the ephemeral ID length - vo, err = kv.Get(receptionIDSizeStorageKey, - receptionIDSizeStorageVersion) - if err != nil { - jww.FATAL.Panicf("Failed to get the reception ID size: %+v", err) - } - - if s.idSize, err = strconv.Atoi(string(vo.Data)); err != nil { - jww.FATAL.Panicf("Failed to unmarshal the reception ID size: %+v", - err) + s.present[makeIdHash(sr.Eph, sr.Source)] = struct{}{} } return s @@ -124,7 +97,6 @@ func LoadStore(kv *versioned.KV) *Store { func (s *Store) save() error { identities := s.makeStoredReferences() - data, err := json.Marshal(&identities) if err != nil { return errors.WithMessage(err, "failed to store reception store") @@ -165,7 +137,7 @@ func (s *Store) makeStoredReferences() []storedReference { return identities[:i] } -func (s *Store) GetIdentity(rng io.Reader) (IdentityUse, error) { +func (s *Store) GetIdentity(rng io.Reader, addressSize uint8) (IdentityUse, error) { s.mux.Lock() defer s.mux.Unlock() @@ -181,7 +153,7 @@ func (s *Store) GetIdentity(rng io.Reader) (IdentityUse, error) { // poll with so we can continue tracking the network and to further // obfuscate network identities. if len(s.active) == 0 { - identity, err = generateFakeIdentity(rng, uint(s.idSize), now) + identity, err = generateFakeIdentity(rng, addressSize, now) if err != nil { jww.FATAL.Panicf("Failed to generate a new ID when none "+ "available: %+v", err) @@ -196,20 +168,18 @@ func (s *Store) GetIdentity(rng io.Reader) (IdentityUse, error) { // Calculate the sampling period identity, err = identity.setSamplingPeriod(rng) if err != nil { - jww.FATAL.Panicf("Failed to calculate the sampling period: "+ - "%+v", err) + jww.FATAL.Panicf("Failed to calculate the sampling period: %+v", err) } return identity, nil } func (s *Store) AddIdentity(identity Identity) error { - idH := makeIdHash(identity.EphId, identity.Source) s.mux.Lock() defer s.mux.Unlock() - //do not make duplicates of IDs + // Do not make duplicates of IDs if _, ok := s.present[idH]; ok { jww.DEBUG.Printf("Ignoring duplicate identity for %d (%s)", identity.EphId, identity.Source) @@ -218,7 +188,7 @@ func (s *Store) AddIdentity(identity Identity) error { if identity.StartValid.After(identity.EndValid) { return errors.Errorf("Cannot add an identity which start valid "+ - "time (%s) is after its end valid time(%s)", identity.StartValid, + "time (%s) is after its end valid time (%s)", identity.StartValid, identity.EndValid) } @@ -229,11 +199,11 @@ func (s *Store) AddIdentity(identity Identity) error { } s.active = append(s.active, reg) - s.present[idH] = nil + s.present[idH] = struct{}{} if !identity.Ephemeral { if err := s.save(); err != nil { - jww.FATAL.Panicf("Failed to save reception store after identity " + - "addition") + jww.FATAL.Panicf("Failed to save reception store after identity "+ + "addition: %+v", err) } } @@ -244,86 +214,44 @@ func (s *Store) RemoveIdentity(ephID ephemeral.Id) { s.mux.Lock() defer s.mux.Unlock() - for i := 0; i < len(s.active); i++ { - inQuestion := s.active[i] - if bytes.Equal(inQuestion.EphId[:], ephID[:]) { + for i, inQuestion := range s.active { + if inQuestion.EphId == ephID { s.active = append(s.active[:i], s.active[i+1:]...) + err := inQuestion.Delete() if err != nil { jww.FATAL.Panicf("Failed to delete identity: %+v", err) } + if !inQuestion.Ephemeral { if err := s.save(); err != nil { - jww.FATAL.Panicf("Failed to save reception store after " + - "identity removal") + jww.FATAL.Panicf("Failed to save reception store after "+ + "identity removal: %+v", err) } } + return } } } -// Returns whether idSize is set to default -func (s *Store) IsIdSizeDefault() bool { - s.mux.Lock() - defer s.mux.Unlock() - return s.isIdSizeSet -} - -// Updates idSize boolean and broadcasts to any waiting -// idSize readers that id size is now updated with the network -func (s *Store) MarkIdSizeAsSet() { - s.mux.Lock() - s.idSizeCond.L.Lock() - defer s.mux.Unlock() - defer s.idSizeCond.L.Unlock() - s.isIdSizeSet = true - s.idSizeCond.Broadcast() -} - -// Wrapper function which calls a -// sync.Cond wait. Used on any reader of idSize -// who cannot use the default id size -func (s *Store) WaitForIdSizeUpdate() { - s.idSizeCond.L.Lock() - defer s.idSizeCond.L.Unlock() - for !s.IsIdSizeDefault() { - - s.idSizeCond.Wait() - } -} - -func (s *Store) UpdateIdSize(idSize uint) { +func (s *Store) SetToExpire(addressSize uint8) { s.mux.Lock() defer s.mux.Unlock() - if s.idSize == int(idSize) { - return - } - jww.INFO.Printf("Updating address space size to %v", idSize) - - s.idSize = int(idSize) - - // Store the ID size - obj := &versioned.Object{ - Version: receptionIDSizeStorageVersion, - Timestamp: netTime.Now(), - Data: []byte(strconv.Itoa(s.idSize)), - } + expire := netTime.Now().Add(5 * time.Minute) - err := s.kv.Set(receptionIDSizeStorageKey, - receptionIDSizeStorageVersion, obj) - if err != nil { - jww.FATAL.Panicf("Failed to store reception ID size: %+v", err) + for i, active := range s.active { + if active.AddressSize < addressSize && active.EndValid.After(expire) { + s.active[i].EndValid = expire + err := s.active[i].store(s.kv) + if err != nil { + jww.ERROR.Printf("Failed to store identity %d: %+v", i, err) + } + } } } -func (s *Store) GetIDSize() uint { - s.mux.Lock() - defer s.mux.Unlock() - return uint(s.idSize) -} - func (s *Store) prune(now time.Time) { lengthBefore := len(s.active) @@ -332,8 +260,8 @@ func (s *Store) prune(now time.Time) { inQuestion := s.active[i] if now.After(inQuestion.End) && inQuestion.ExtraChecks == 0 { if err := inQuestion.Delete(); err != nil { - jww.ERROR.Printf("Failed to delete Identity for %s: "+ - "%+v", inQuestion, err) + jww.ERROR.Printf("Failed to delete Identity for %s: %+v", + inQuestion, err) } s.active = append(s.active[:i], s.active[i+1:]...) @@ -346,7 +274,7 @@ func (s *Store) prune(now time.Time) { if lengthBefore != len(s.active) { jww.INFO.Printf("Pruned %d identities", lengthBefore-len(s.active)) if err := s.save(); err != nil { - jww.FATAL.Panicf("Failed to store reception storage") + jww.FATAL.Panicf("Failed to store reception storage: %+v", err) } } } @@ -360,11 +288,15 @@ func (s *Store) selectIdentity(rng io.Reader, now time.Time) (IdentityUse, error } else { seed := make([]byte, 32) if _, err := rng.Read(seed); err != nil { - return IdentityUse{}, errors.WithMessage(err, "Failed to "+ - "choose ID due to rng failure") + return IdentityUse{}, errors.WithMessage(err, "Failed to choose "+ + "ID due to RNG failure") } - selectedNum := large.NewInt(1).Mod(large.NewIntFromBytes(seed), large.NewInt(int64(len(s.active)))) + selectedNum := large.NewInt(1).Mod( + large.NewIntFromBytes(seed), + large.NewInt(int64(len(s.active))), + ) + selected = s.active[selectedNum.Uint64()] } @@ -372,9 +304,12 @@ func (s *Store) selectIdentity(rng io.Reader, now time.Time) (IdentityUse, error selected.ExtraChecks-- } - jww.TRACE.Printf("Selected identity: EphId: %d ID: %s End: %s StartValid: %s EndValid: %s", - selected.EphId.Int64(), selected.Source, selected.End.Format("01/02/06 03:04:05 pm"), - selected.StartValid.Format("01/02/06 03:04:05 pm"), selected.EndValid.Format("01/02/06 03:04:05 pm")) + jww.TRACE.Printf("Selected identity: EphId: %d ID: %s End: %s "+ + "StartValid: %s EndValid: %s", + selected.EphId.Int64(), selected.Source, + selected.End.Format("01/02/06 03:04:05 pm"), + selected.StartValid.Format("01/02/06 03:04:05 pm"), + selected.EndValid.Format("01/02/06 03:04:05 pm")) return IdentityUse{ Identity: selected.Identity, diff --git a/storage/reception/store_test.go b/storage/reception/store_test.go index 8779e8f54cedacd24ae9e4c892fb3b19c279b814..b969c9680a0f4e0885808df8b9144db6ca82d819 100644 --- a/storage/reception/store_test.go +++ b/storage/reception/store_test.go @@ -16,13 +16,12 @@ func TestNewStore(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) expected := &Store{ active: make([]*registration, 0), - idSize: defaultIDSize, kv: kv, } s := NewStore(kv) - if !reflect.DeepEqual([]*registration{}, s.active) || s.idSize != defaultIDSize { + if !reflect.DeepEqual([]*registration{}, s.active) { t.Errorf("NewStore() failed to return the expected Store."+ "\nexpected: %+v\nreceived: %+v", expected, s) } @@ -154,14 +153,14 @@ func TestStore_GetIdentity(t *testing.T) { t.Errorf("AddIdentity() produced an error: %+v", err) } - idu, err := s.GetIdentity(prng) + idu, err := s.GetIdentity(prng, 15) if err != nil { t.Errorf("GetIdentity() produced an error: %+v", err) } if !testID.Equal(idu.Identity) { t.Errorf("GetIdentity() did not return the expected Identity."+ - "\nexpected: %s\nreceived: %s", testID.String(), idu.String()) + "\nexpected: %s\nreceived: %s", testID, idu) } } @@ -181,7 +180,7 @@ func TestStore_AddIdentity(t *testing.T) { if !s.active[0].Identity.Equal(testID.Identity) { t.Errorf("Failed to get expected Identity.\nexpected: %s\nreceived: %s", - testID.Identity.String(), s.active[0]) + testID.Identity, s.active[0]) } } @@ -204,19 +203,6 @@ func TestStore_RemoveIdentity(t *testing.T) { } } -func TestStore_UpdateIdSize(t *testing.T) { - kv := versioned.NewKV(make(ekv.Memstore)) - s := NewStore(kv) - newSize := s.idSize * 2 - - s.UpdateIdSize(uint(newSize)) - - if s.idSize != newSize { - t.Errorf("UpdateIdSize() failed to update the size."+ - "\nexpected: %d\nrecieved: %d", newSize, s.idSize) - } -} - func TestStore_prune(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) s := NewStore(kv) diff --git a/storage/rounds/uncheckedRounds.go b/storage/rounds/uncheckedRounds.go new file mode 100644 index 0000000000000000000000000000000000000000..898222b71556ef86146d996e23c9804bff356f85 --- /dev/null +++ b/storage/rounds/uncheckedRounds.go @@ -0,0 +1,314 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package rounds + +import ( + "bytes" + "encoding/binary" + "github.com/golang/protobuf/proto" + "github.com/pkg/errors" + "gitlab.com/elixxir/client/storage/versioned" + pb "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + "gitlab.com/xx_network/primitives/netTime" + "sync" + "time" +) + +const ( + uncheckedRoundVersion = 0 + uncheckedRoundPrefix = "uncheckedRoundPrefix" + // Key to store rounds + uncheckedRoundKey = "uncheckRounds" + // Key to store individual round + // Housekeeping constant (used for serializing uint64 ie id.Round) + uint64Size = 8 + // Maximum checks that can be performed on a round. Intended so that + // a round is checked no more than 1 week approximately (network/rounds.cappedTries + 7) + maxChecks = 14 +) + +// Round identity information used in message retrieval +// Derived from reception.Identity saving data needed +// for message retrieval +type Identity struct { + EpdId ephemeral.Id + Source *id.ID +} + +// Unchecked round structure is rounds which failed on message retrieval +// These rounds are stored for retry of message retrieval +type UncheckedRound struct { + Info *pb.RoundInfo + Identity + // Timestamp in which round has last been checked + LastCheck time.Time + // Number of times a round has been checked + NumChecks uint64 +} + +// marshal serializes UncheckedRound r into a byte slice +func (r UncheckedRound) marshal() ([]byte, error) { + buf := bytes.NewBuffer(nil) + // Write the round info + b := make([]byte, uint64Size) + infoBytes, err := proto.Marshal(r.Info) + binary.LittleEndian.PutUint64(b, uint64(len(infoBytes))) + buf.Write(b) + buf.Write(infoBytes) + + b = make([]byte, uint64Size) + + // Write the round identity info + buf.Write(r.Identity.EpdId[:]) + if r.Source != nil { + buf.Write(r.Identity.Source.Marshal()) + } else { + buf.Write(make([]byte, id.ArrIDLen)) + } + + // Write the time stamp bytes + tsBytes, err := r.LastCheck.MarshalBinary() + if err != nil { + return nil, errors.WithMessage(err, "Could not marshal timestamp ") + } + b = make([]byte, uint64Size) + binary.LittleEndian.PutUint64(b, uint64(len(tsBytes))) + buf.Write(b) + buf.Write(tsBytes) + + // Write the number of tries for this round + b = make([]byte, uint64Size) + binary.LittleEndian.PutUint64(b, r.NumChecks) + buf.Write(b) + + return buf.Bytes(), nil +} + +// unmarshal deserializes round data from buff into UncheckedRound r +func (r *UncheckedRound) unmarshal(buff *bytes.Buffer) error { + // Deserialize the roundInfo + roundInfoLen := binary.LittleEndian.Uint64(buff.Next(uint64Size)) + roundInfoBytes := buff.Next(int(roundInfoLen)) + ri := &pb.RoundInfo{} + if err := proto.Unmarshal(roundInfoBytes, ri); err != nil { + return errors.WithMessagef(err, "Failed to unmarshal roundInfo") + } + r.Info = ri + + // Deserialize the round identity information + copy(r.EpdId[:], buff.Next(uint64Size)) + + sourceId, err := id.Unmarshal(buff.Next(id.ArrIDLen)) + if err != nil { + return errors.WithMessage(err, "Failed to unmarshal round identity.source") + } + + r.Source = sourceId + + // Deserialize the timestamp bytes + timestampLen := binary.LittleEndian.Uint64(buff.Next(uint64Size)) + tsByes := buff.Next(int(uint64(timestampLen))) + if err = r.LastCheck.UnmarshalBinary(tsByes); err != nil { + return errors.WithMessage(err, "Failed to unmarshal round timestamp") + } + + r.NumChecks = binary.LittleEndian.Uint64(buff.Next(uint64Size)) + + return nil +} + +// Storage object saving rounds to retry for message retrieval +type UncheckedRoundStore struct { + list map[id.Round]UncheckedRound + mux sync.RWMutex + kv *versioned.KV +} + +// Constructor for a UncheckedRoundStore +func NewUncheckedStore(kv *versioned.KV) (*UncheckedRoundStore, error) { + kv = kv.Prefix(uncheckedRoundPrefix) + + urs := &UncheckedRoundStore{ + list: make(map[id.Round]UncheckedRound, 0), + kv: kv, + } + + return urs, urs.save() + +} + +// Loads an deserializes a UncheckedRoundStore from memory +func LoadUncheckedStore(kv *versioned.KV) (*UncheckedRoundStore, error) { + + kv = kv.Prefix(uncheckedRoundPrefix) + vo, err := kv.Get(uncheckedRoundKey, uncheckedRoundVersion) + if err != nil { + return nil, err + } + + urs := &UncheckedRoundStore{ + list: make(map[id.Round]UncheckedRound), + kv: kv, + } + + err = urs.unmarshal(vo.Data) + if err != nil { + return nil, errors.WithMessage(err, "Failed to load rounds from storage") + } + + return urs, err +} + +// Adds a round to check on the list and saves to memory +func (s *UncheckedRoundStore) AddRound(ri *pb.RoundInfo, ephID ephemeral.Id, source *id.ID) error { + s.mux.Lock() + defer s.mux.Unlock() + rid := id.Round(ri.ID) + + if _, exists := s.list[rid]; !exists { + newUncheckedRound := UncheckedRound{ + Info: ri, + Identity: Identity{ + EpdId: ephID, + Source: source, + }, + LastCheck: netTime.Now(), + NumChecks: 0, + } + + s.list[rid] = newUncheckedRound + + return s.save() + } + + return nil +} + +// Retrieves an UncheckedRound from the map, if it exists +func (s *UncheckedRoundStore) GetRound(rid id.Round) (UncheckedRound, bool) { + s.mux.RLock() + defer s.mux.RUnlock() + rnd, exists := s.list[rid] + return rnd, exists +} + +// Retrieves the list of rounds +func (s *UncheckedRoundStore) GetList() map[id.Round]UncheckedRound { + s.mux.RLock() + defer s.mux.RUnlock() + return s.list +} + +// Increments the amount of checks performed on this stored round +func (s *UncheckedRoundStore) IncrementCheck(rid id.Round) error { + s.mux.Lock() + defer s.mux.Unlock() + rnd, exists := s.list[rid] + if !exists { + return errors.Errorf("round %d could not be found in RAM", rid) + } + + // If a round has been checked the maximum amount of times, + // we bail the round by removing it from store and no longer checking + if rnd.NumChecks >= maxChecks { + if err := s.remove(rid); err != nil { + return errors.WithMessagef(err, "Round %d reached maximum checks "+ + "but could not be removed", rid) + } + return nil + } + + // Update the rounds state + rnd.LastCheck = netTime.Now() + rnd.NumChecks++ + s.list[rid] = rnd + return s.save() +} + +// Remove deletes a round from UncheckedRoundStore's list and from storage +func (s *UncheckedRoundStore) Remove(rid id.Round) error { + s.mux.Lock() + defer s.mux.Unlock() + return s.remove(rid) +} + +// Remove is a helper function which removes the round from UncheckedRoundStore's list +// Note this method is unsafe and should only be used by methods with a lock +func (s *UncheckedRoundStore) remove(rid id.Round) error { + if _, exists := s.list[rid]; !exists { + return errors.Errorf("round %d does not exist in store", rid) + } + delete(s.list, rid) + return s.save() +} + +// save stores the information from the round list into storage +func (s *UncheckedRoundStore) save() error { + // Store list of rounds + data, err := s.marshal() + if err != nil { + return errors.WithMessagef(err, "Could not marshal data for unchecked rounds") + } + + // Create the versioned object + obj := &versioned.Object{ + Version: uncheckedRoundVersion, + Timestamp: netTime.Now(), + Data: data, + } + + // Save to storage + err = s.kv.Set(uncheckedRoundKey, uncheckedRoundVersion, obj) + if err != nil { + return errors.WithMessagef(err, "Could not store data for unchecked rounds") + } + + return nil +} + +// marshal is a helper function which serializes all rounds in list to bytes +func (s *UncheckedRoundStore) marshal() ([]byte, error) { + buf := bytes.NewBuffer(nil) + // Write number of rounds the buffer + b := make([]byte, 8) + binary.PutVarint(b, int64(len(s.list))) + buf.Write(b) + + for rid, rnd := range s.list { + rndData, err := rnd.marshal() + if err != nil { + return nil, errors.WithMessagef(err, "Failed to marshal round %d", rid) + } + + buf.Write(rndData) + + } + + return buf.Bytes(), nil +} + +// unmarshal deserializes an UncheckedRound from its stored byte data +func (s *UncheckedRoundStore) unmarshal(data []byte) error { + buff := bytes.NewBuffer(data) + // Get number of rounds in list + length, _ := binary.Varint(buff.Next(8)) + + for i := 0; i < int(length); i++ { + rnd := UncheckedRound{} + err := rnd.unmarshal(buff) + if err != nil { + return errors.WithMessage(err, "Failed to unmarshal rounds in storage") + } + + s.list[id.Round(rnd.Info.ID)] = rnd + } + + return nil +} diff --git a/storage/rounds/uncheckedRounds_test.go b/storage/rounds/uncheckedRounds_test.go new file mode 100644 index 0000000000000000000000000000000000000000..63e8c195f4f2ffa752979e5f5780acdc6d2d3f16 --- /dev/null +++ b/storage/rounds/uncheckedRounds_test.go @@ -0,0 +1,371 @@ +/////////////////////////////////////////////////////////////////////////////// +// Copyright © 2020 xx network SEZC // +// // +// Use of this source code is governed by a license that can be found in the // +// LICENSE file // +/////////////////////////////////////////////////////////////////////////////// + +package rounds + +import ( + "bytes" + "gitlab.com/elixxir/client/storage/versioned" + pb "gitlab.com/elixxir/comms/mixmessages" + "gitlab.com/elixxir/ekv" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + "gitlab.com/xx_network/primitives/netTime" + "reflect" + "testing" +) + +// Unit test +func TestNewUncheckedStore(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + + testStore := &UncheckedRoundStore{ + list: make(map[id.Round]UncheckedRound), + kv: kv.Prefix(uncheckedRoundPrefix), + } + + store, err := NewUncheckedStore(kv) + if err != nil { + t.Fatalf("NewUncheckedStore error: "+ + "Could not create unchecked stor: %v", err) + } + + // Compare manually created object with NewUnknownRoundsStore + if !reflect.DeepEqual(testStore, store) { + t.Fatalf("NewUncheckedStore error: "+ + "Returned incorrect Store."+ + "\n\texpected: %+v\n\treceived: %+v", testStore, store) + } + + rid := id.Round(1) + roundInfo := &pb.RoundInfo{ + ID: uint64(rid), + } + uncheckedRound := UncheckedRound{ + Info: roundInfo, + LastCheck: netTime.Now(), + NumChecks: 0, + } + + store.list[rid] = uncheckedRound + if err = store.save(); err != nil { + t.Fatalf("NewUncheckedStore error: "+ + "Could not save store: %v", err) + } + + // Test if round list data matches + expectedRoundData, err := store.marshal() + if err != nil { + t.Fatalf("NewUncheckedStore error: "+ + "Could not marshal data: %v", err) + } + roundData, err := store.kv.Get(uncheckedRoundKey, uncheckedRoundVersion) + if err != nil { + t.Fatalf("NewUncheckedStore error: "+ + "Could not retrieve round list form storage: %v", err) + } + + if !bytes.Equal(expectedRoundData, roundData.Data) { + t.Fatalf("NewUncheckedStore error: "+ + "Data from store was not expected"+ + "\n\tExpected %v\n\tReceived: %v", expectedRoundData, roundData.Data) + } + +} + +// Unit test +func TestLoadUncheckedStore(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + + testStore, err := NewUncheckedStore(kv) + if err != nil { + t.Fatalf("LoadUncheckedStore error: "+ + "Could not call constructor NewUncheckedStore: %v", err) + } + + // Add round to store + rid := id.Round(0) + roundInfo := &pb.RoundInfo{ + ID: uint64(rid), + } + + ephId := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} + source := id.NewIdFromBytes([]byte("Sauron"), t) + err = testStore.AddRound(roundInfo, ephId, source) + if err != nil { + t.Fatalf("LoadUncheckedStore error: "+ + "Could not add round to store: %v", err) + } + + // Load store + loadedStore, err := LoadUncheckedStore(kv) + if err != nil { + t.Fatalf("LoadUncheckedStore error: "+ + "Could not call LoadUncheckedStore: %v", err) + } + + // Check if round is in loaded store + rnd, exists := loadedStore.list[rid] + if !exists { + t.Fatalf("LoadUncheckedStore error: "+ + "Added round %d not found in loaded store", rid) + } + + // Check if set values are expected + if !bytes.Equal(rnd.EpdId[:], ephId[:]) || + !source.Cmp(rnd.Source) { + t.Fatalf("LoadUncheckedStore error: "+ + "Values in loaded round %d are not expected."+ + "\n\tExpected ephemeral: %v"+ + "\n\tReceived ephemeral: %v"+ + "\n\tExpected source: %v"+ + "\n\tReceived source: %v", rid, + ephId, rnd.EpdId, + source, rnd.Source) + } + +} + +// Unit test +func TestUncheckedRoundStore_AddRound(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + + testStore, err := NewUncheckedStore(kv) + if err != nil { + t.Fatalf("AddRound error: "+ + "Could not call constructor NewUncheckedStore: %v", err) + } + + // Add round to store + rid := id.Round(0) + roundInfo := &pb.RoundInfo{ + ID: uint64(rid), + } + ephId := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} + source := id.NewIdFromBytes([]byte("Sauron"), t) + err = testStore.AddRound(roundInfo, ephId, source) + if err != nil { + t.Fatalf("AddRound error: "+ + "Could not add round to store: %v", err) + } + + if _, exists := testStore.list[rid]; !exists { + t.Errorf("AddRound error: " + + "Could not find added round in list") + } + +} + +// Unit test +func TestUncheckedRoundStore_GetRound(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + + testStore, err := NewUncheckedStore(kv) + if err != nil { + t.Fatalf("GetRound error: "+ + "Could not call constructor NewUncheckedStore: %v", err) + } + + // Add round to store + rid := id.Round(0) + roundInfo := &pb.RoundInfo{ + ID: uint64(rid), + } + ephId := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} + source := id.NewIdFromBytes([]byte("Sauron"), t) + err = testStore.AddRound(roundInfo, ephId, source) + if err != nil { + t.Fatalf("GetRound error: "+ + "Could not add round to store: %v", err) + } + + // Retrieve round that was inserted + retrievedRound, exists := testStore.GetRound(rid) + if !exists { + t.Fatalf("GetRound error: " + + "Could not get round from store") + } + + if !bytes.Equal(retrievedRound.EpdId[:], ephId[:]) || + !source.Cmp(retrievedRound.Source) { + t.Fatalf("GetRound error: "+ + "Values in loaded round %d are not expected."+ + "\n\tExpected ephemeral: %v"+ + "\n\tReceived ephemeral: %v"+ + "\n\tExpected source: %v"+ + "\n\tReceived source: %v", rid, + ephId, retrievedRound.EpdId, + source, retrievedRound.Source) + } + + // Try to pull unknown round from store + unknownRound := id.Round(1) + _, exists = testStore.GetRound(unknownRound) + if exists { + t.Fatalf("GetRound error: " + + "Should not find unknown round in store.") + } + +} + +// Unit test +func TestUncheckedRoundStore_GetList(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + + testStore, err := NewUncheckedStore(kv) + if err != nil { + t.Fatalf("GetList error: "+ + "Could not call constructor NewUncheckedStore: %v", err) + } + + // Add rounds to store + numRounds := 10 + for i := 0; i < numRounds; i++ { + rid := id.Round(i) + roundInfo := &pb.RoundInfo{ + ID: uint64(rid), + } + ephId := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} + source := id.NewIdFromUInt(uint64(i), id.User, t) + err = testStore.AddRound(roundInfo, ephId, source) + if err != nil { + t.Errorf("GetList error: "+ + "Could not add round to store: %v", err) + } + } + + // Retrieve list + retrievedList := testStore.GetList() + if len(retrievedList) != numRounds { + t.Errorf("GetList error: "+ + "List returned is not of expected size."+ + "\n\tExpected: %v\n\tReceived: %v", numRounds, len(retrievedList)) + } + + for i := 0; i < numRounds; i++ { + rid := id.Round(i) + if _, exists := retrievedList[rid]; !exists { + t.Errorf("GetList error: "+ + "Retrieved list does not contain expected round %d.", rid) + } + } + +} + +// Unit test +func TestUncheckedRoundStore_IncrementCheck(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + + testStore, err := NewUncheckedStore(kv) + if err != nil { + t.Fatalf("IncrementCheck error: "+ + "Could not call constructor NewUncheckedStore: %v", err) + } + + // Add rounds to store + numRounds := 10 + for i := 0; i < numRounds; i++ { + rid := id.Round(i) + roundInfo := &pb.RoundInfo{ + ID: uint64(rid), + } + ephId := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} + source := id.NewIdFromUInt(uint64(i), id.User, t) + err = testStore.AddRound(roundInfo, ephId, source) + if err != nil { + t.Errorf("IncrementCheck error: "+ + "Could not add round to store: %v", err) + } + } + + testRound := id.Round(3) + numChecks := 4 + for i := 0; i < numChecks; i++ { + err = testStore.IncrementCheck(testRound) + if err != nil { + t.Errorf("IncrementCheck error: "+ + "Could not increment check for round %d: %v", testRound, err) + } + } + + rnd, _ := testStore.GetRound(testRound) + if rnd.NumChecks != uint64(numChecks) { + t.Errorf("IncrementCheck error: "+ + "Round %d did not have expected number of checks."+ + "\n\tExpected: %v\n\tReceived: %v", testRound, numChecks, rnd.NumChecks) + } + + // Error path: check unknown round can not be incremented + unknownRound := id.Round(numRounds + 5) + err = testStore.IncrementCheck(unknownRound) + if err == nil { + t.Errorf("IncrementCheck error: "+ + "Should not find round %d which was not added to store", unknownRound) + } + + // Reach max checks, ensure that round is removed + maxRound := id.Round(7) + for i := 0; i < maxChecks+1; i++ { + err = testStore.IncrementCheck(maxRound) + if err != nil { + t.Errorf("IncrementCheck error: "+ + "Could not increment check for round %d: %v", maxRound, err) + } + + } + +} + +// Unit test +func TestUncheckedRoundStore_Remove(t *testing.T) { + kv := versioned.NewKV(make(ekv.Memstore)) + testStore, err := NewUncheckedStore(kv) + if err != nil { + t.Fatalf("Remove error: "+ + "Could not call constructor NewUncheckedStore: %v", err) + } + + // Add rounds to store + numRounds := 10 + for i := 0; i < numRounds; i++ { + rid := id.Round(i) + roundInfo := &pb.RoundInfo{ + ID: uint64(rid), + } + ephId := ephemeral.Id{1, 2, 3, 4, 5, 6, 7, 8} + source := id.NewIdFromUInt(uint64(i), id.User, t) + err = testStore.AddRound(roundInfo, ephId, source) + if err != nil { + t.Errorf("Remove error: "+ + "Could not add round to store: %v", err) + } + } + + // Remove round from storage + removedRound := id.Round(1) + err = testStore.Remove(removedRound) + if err != nil { + t.Errorf("Remove error: "+ + "Could not removed round %d from storage: %v", removedRound, err) + } + + // Check that round was removed + _, exists := testStore.GetRound(removedRound) + if exists { + t.Errorf("Remove error: "+ + "Round %d expected to be removed from storage", removedRound) + } + + // Error path: attempt to remove unknown round + unknownRound := id.Round(numRounds + 5) + err = testStore.Remove(unknownRound) + if err == nil { + t.Errorf("Remove error: "+ + "Should not removed round %d which is not in storage", unknownRound) + } + +} diff --git a/storage/session.go b/storage/session.go index 053edfd224baf9a6c377cef9a3882b07b62c4fde..3c4c1932baf747b080a5272b3702876abb57c652 100644 --- a/storage/session.go +++ b/storage/session.go @@ -10,6 +10,8 @@ package storage import ( + "gitlab.com/elixxir/client/storage/hostList" + "gitlab.com/elixxir/client/storage/rounds" "sync" "testing" "time" @@ -49,7 +51,7 @@ type Session struct { //memoized data regStatus RegistrationStatus - baseNdf *ndf.NetworkDefinition + ndf *ndf.NetworkDefinition //sub-stores e2e *e2e.Store @@ -63,6 +65,8 @@ type Session struct { garbledMessages *utility.MeteredCmixMessageBuffer reception *reception.Store clientVersion *clientVersion.Store + uncheckedRounds *rounds.UncheckedRoundStore + hostList *hostList.Store } // Initialize a new Session object @@ -142,6 +146,13 @@ func New(baseDir, password string, u userInterface.User, currentVersion version. return nil, errors.WithMessage(err, "Failed to create client version store.") } + s.uncheckedRounds, err = rounds.NewUncheckedStore(s.kv) + if err != nil { + return nil, errors.WithMessage(err, "Failed to create unchecked round store") + } + + s.hostList = hostList.NewStore(s.kv) + return s, nil } @@ -213,6 +224,13 @@ func Load(baseDir, password string, currentVersion version.Version, s.reception = reception.LoadStore(s.kv) + s.uncheckedRounds, err = rounds.LoadUncheckedStore(s.kv) + if err != nil { + return nil, errors.WithMessage(err, "Failed to load unchecked round store") + } + + s.hostList = hostList.NewStore(s.kv) + return s, nil } @@ -283,6 +301,18 @@ func (s *Session) Partition() *partition.Store { return s.partition } +func (s *Session) UncheckedRounds() *rounds.UncheckedRoundStore { + s.mux.RLock() + defer s.mux.RUnlock() + return s.uncheckedRounds +} + +func (s *Session) HostList() *hostList.Store { + s.mux.RLock() + defer s.mux.RUnlock() + return s.hostList +} + // Get an object from the session func (s *Session) Get(key string) (*versioned.Object, error) { return s.kv.Get(key, currentSessionVersion) @@ -298,6 +328,13 @@ func (s *Session) Delete(key string) error { return s.kv.Delete(key, currentSessionVersion) } +// GetKV returns the Session versioned.KV. +func (s *Session) GetKV() *versioned.KV { + s.mux.RLock() + defer s.mux.RUnlock() + return s.kv +} + // Initializes a Session object wrapped around a MemStore object. // FOR TESTING ONLY func InitTestingSession(i interface{}) *Session { @@ -372,5 +409,12 @@ func InitTestingSession(i interface{}) *Session { s.reception = reception.NewStore(s.kv) + s.uncheckedRounds, err = rounds.NewUncheckedStore(s.kv) + if err != nil { + jww.FATAL.Panicf("Failed to create uncheckRound store: %v", err) + } + + s.hostList = hostList.NewStore(s.kv) + return s } diff --git a/ud/manager.go b/ud/manager.go index 842d8d353a6d05ab523db2f887c2036d169f976c..1b80b1ad22bcb07fb94a5944a78000c2ef56f45d 100644 --- a/ud/manager.go +++ b/ud/manager.go @@ -15,6 +15,7 @@ import ( "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/crypto/signature/rsa" "gitlab.com/xx_network/primitives/id" + "math" "time" ) @@ -50,9 +51,9 @@ type Manager struct { // updated NDF is available and will error if one is not. func NewManager(client *api.Client, single *single.Manager) (*Manager, error) { jww.INFO.Println("ud.NewManager()") - if !client.GetHealth().IsHealthy() { - return nil, errors.New("cannot start UD Manager when network was " + - "never healthy.") + if client.NetworkFollowerStatus() != api.Running { + return nil, errors.New("cannot start UD Manager when network follower is not " + + "running.") } m := &Manager{ @@ -89,6 +90,10 @@ func NewManager(client *api.Client, single *single.Manager) (*Manager, error) { // Create the user discovery host object hp := connect.GetDefaultHostParams() + // Client will not send KeepAlive packets + hp.KaClientOpts.Time = time.Duration(math.MaxInt64) + hp.MaxRetries = 3 + hp.SendTimeout = 3 * time.Second m.host, err = m.comms.AddHost(&id.UDB, def.UDB.Address, []byte(def.UDB.Cert), hp) if err != nil { return nil, errors.WithMessage(err, "User Discovery host object could "+