diff --git a/network/ephemeral/check.go b/network/ephemeral/check.go new file mode 100644 index 0000000000000000000000000000000000000000..57195ee82a09f8be8f41a874372da700a9e8a2d1 --- /dev/null +++ b/network/ephemeral/check.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 ephemeral + +import ( + "github.com/pkg/errors" + "gitlab.com/elixxir/client/globals" + "gitlab.com/elixxir/client/stoppable" + "gitlab.com/elixxir/client/storage" + "gitlab.com/elixxir/client/storage/reception" + "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/id/ephemeral" + ephemeralStore "gitlab.com/elixxir/client/storage/ephemeral" + "time" +) + +const checkInterval = time.Duration(500) * time.Second +const ephemeralIdSie = 64 +const validityGracePeriod = 5 * time.Minute + +// Check runs a thread which checks for past and present ephemeral ids +func Check(session *storage.Session, ourId *id.ID) stoppable.Stoppable { + stop := stoppable.NewSingle("EphemeralCheck") + + go check(session, ourId, stop) + + return stop + +} + +// check is a thread which continuously processes ephemeral ids. If any error occurs, +// the thread crashes +func check(session *storage.Session, ourId *id.ID, stop *stoppable.Single) { + t := time.NewTicker(checkInterval) + ephStore := session.Ephemeral() + identityStore := session.Reception() + for true { + select { + case <-t.C: + err := processEphemeralIds(ourId, ephStore, identityStore) + if err != nil { + globals.Log.FATAL.Panicf("Could not " + + "process ephemeral ids: %v", err) + } + + err = ephStore.UpdateTimestamp(time.Now()) + if err != nil { + break + } + + + case <-stop.Quit(): + break + } + + + } + +} + +// processEphemeralIds periodically checks for past and present ephemeral ids. +// It then adds identities for these ids if needed +func processEphemeralIds(ourId *id.ID, ephemeralStore *ephemeralStore.Store, + identityStore *reception.Store) error { + // Get the timestamp of the last check + lastCheck, err := ephemeralStore.GetTimestamp() + if err != nil { + return errors.Errorf("Could not get time stamp in " + + "ephemeral store: %v", err) + } + + // Find out how long that last check was + timeSinceLastCheck := time.Now().Sub(lastCheck) + + // Generate ephemeral ids in the range of the last check + eids, err := ephemeral.GetIdsByRange(ourId, ephemeralIdSie, + time.Now().UnixNano(), timeSinceLastCheck) + if err != nil { + return errors.Errorf("Could not generate ephemeral ids: %v", err) + } + + // Add identities for every ephemeral id + for _, eid := range eids { + err = identityStore.AddIdentity(reception.Identity{ + EphId: eid.Id, + Source: ourId, + End: time.Now().Add(validityGracePeriod), + StartValid: eid.Start, + EndValid: eid.End, + Ephemeral: false, + }) + if err != nil { + return errors.Errorf("Could not add identity for " + + "generated ephemeral ID: %v", err) + } + } + + return nil +} diff --git a/network/manager.go b/network/manager.go index d10622db534a2050750235d79421e1f05410855f..fe1438705500fb699ddea3028f44d5b23a058d51 100644 --- a/network/manager.go +++ b/network/manager.go @@ -7,13 +7,14 @@ package network -// manager.go controls access to network resources. Interprocess communications +// check.go controls access to network resources. Interprocess communications // and intraclient state are accessible through the context object. import ( "github.com/pkg/errors" "gitlab.com/elixxir/client/interfaces" "gitlab.com/elixxir/client/interfaces/params" + "gitlab.com/elixxir/client/network/ephemeral" "gitlab.com/elixxir/client/network/health" "gitlab.com/elixxir/client/network/internal" "gitlab.com/elixxir/client/network/message" @@ -44,7 +45,6 @@ type manager struct { //sub-managers round *rounds.Manager message *message.Manager - //atomic denotes if the network is running running *uint32 } @@ -126,6 +126,9 @@ func (m *manager) Follow() (stoppable.Stoppable, error) { // Round processing multi.Add(m.round.StartProcessors()) + m.Session.Ephemeral() + // Ephemeral ID tracking + multi.Add(ephemeral.Check(m.Session, m.Comms.Id)) //set the running status back to 0 so it can be started again closer := stoppable.NewCleanup(multi, func(time.Duration) error { diff --git a/storage/ephemeral/store.go b/storage/ephemeral/store.go index 8cbbfc7b827de361a6761db4fd241f8827b5a2e5..6dbff903235cc6e342cd7fd88bc9f224b4e207ae 100644 --- a/storage/ephemeral/store.go +++ b/storage/ephemeral/store.go @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////// -// Copyright © 2021 xx network SEZC // +// Copyright © 2020 xx network SEZC // // // // Use of this source code is governed by a license that can be found in the // // LICENSE file // diff --git a/storage/ephemeral/store_test.go b/storage/ephemeral/store_test.go index 5ee0fb85bfc360fe2692a94f25606db8b0a31a47..bd94ae90d7d39564c195788ccb042a8e313069d8 100644 --- a/storage/ephemeral/store_test.go +++ b/storage/ephemeral/store_test.go @@ -1,5 +1,5 @@ /////////////////////////////////////////////////////////////////////////////// -// Copyright © 2021 xx network SEZC // +// Copyright © 2020 xx network SEZC // // // // Use of this source code is governed by a license that can be found in the // // LICENSE file // diff --git a/storage/session.go b/storage/session.go index 44278489c94d868f1b76556c2375c8fe8981d298..0773cccf3d952c9f07f02e1336caf639efed1e58 100644 --- a/storage/session.go +++ b/storage/session.go @@ -17,7 +17,9 @@ import ( "gitlab.com/elixxir/client/storage/cmix" "gitlab.com/elixxir/client/storage/conversation" "gitlab.com/elixxir/client/storage/e2e" + "gitlab.com/elixxir/client/storage/ephemeral" "gitlab.com/elixxir/client/storage/partition" + "gitlab.com/elixxir/client/storage/reception" "gitlab.com/elixxir/client/storage/user" "gitlab.com/elixxir/client/storage/utility" "gitlab.com/elixxir/client/storage/versioned" @@ -56,6 +58,9 @@ type Session struct { criticalRawMessages *utility.CmixMessageBuffer garbledMessages *utility.MeteredCmixMessageBuffer checkedRounds *utility.KnownRounds + ephemeral *ephemeral.Store + reception *reception.Store + } // Initialize a new Session object @@ -136,6 +141,13 @@ func New(baseDir, password string, u userInterface.User, cmixGrp, s.conversations = conversation.NewStore(s.kv) s.partition = partition.New(s.kv) + s.ephemeral, err = ephemeral.NewStore(s.kv) + if err != nil { + return nil, errors.WithMessage(err, "Failed to ephemeralId tracking store") + } + + s.reception = reception.NewStore(s.kv) + return s, nil } @@ -197,6 +209,11 @@ func Load(baseDir, password string, rng *fastRNG.StreamGenerator) (*Session, err s.conversations = conversation.NewStore(s.kv) s.partition = partition.New(s.kv) + s.ephemeral, err = ephemeral.LoadStore(s.kv) + if err != nil { + return nil, errors.WithMessage(err, "Failed to ephemeral store") + } + return s, nil } @@ -260,6 +277,18 @@ func (s *Session) Partition() *partition.Store { return s.partition } +func (s *Session) Ephemeral() *ephemeral.Store { + s.mux.RLock() + defer s.mux.RUnlock() + return s.ephemeral +} + +func (s *Session) Reception() *reception.Store { + s.mux.RLock() + defer s.mux.RUnlock() + return s.reception +} + // Get an object from the session func (s *Session) Get(key string) (*versioned.Object, error) { return s.kv.Get(key) @@ -344,5 +373,10 @@ func InitTestingSession(i interface{}) *Session { s.conversations = conversation.NewStore(s.kv) s.partition = partition.New(s.kv) + s.ephemeral, err = ephemeral.NewStore(s.kv) + if err != nil { + globals.Log.FATAL.Panicf("Failed to create ephemeral store: %+v", err) + } + return s }