diff --git a/auth/callback.go b/auth/callback.go index 5917a73d2f91df643cc78eb3f3cf5ed14f9f9aff..f90b430f10fa58631020790bbb8548383972afa7 100644 --- a/auth/callback.go +++ b/auth/callback.go @@ -38,7 +38,7 @@ func (m *Manager) StartProcessies() stoppable.Stoppable { fp := cmixMsg.GetKeyFP() jww.INFO.Printf("RAW AUTH FP: %v", fp) // this takes the request lock if it is a specific fp, - // all exits after this need to call fail or Delete if it is + // all exits after this need to call fail or delete if it is // specific fpType, sr, myHistoricalPrivKey, err := authStore.GetFingerprint(fp) if err != nil { diff --git a/go.mod b/go.mod index 4190a0b4dd791a786b69229579056cbfcb0ea1c9..a263874a27be7fc551eb5d6dba19b9a227b2c315 100644 --- a/go.mod +++ b/go.mod @@ -17,10 +17,10 @@ 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.20210226175832-0cf63a696bf9 + gitlab.com/elixxir/comms v0.0.4-0.20210301202451-4cfc6b78e6d3 gitlab.com/elixxir/crypto v0.0.7-0.20210226175801-f490fc89ffdd gitlab.com/elixxir/ekv v0.1.4 - gitlab.com/elixxir/primitives v0.0.3-0.20210301183103-afcb5c703289 + gitlab.com/elixxir/primitives v0.0.3-0.20210301202753-0aead7f17423 gitlab.com/xx_network/comms v0.0.4-0.20210226175738-04b6c562dd2f gitlab.com/xx_network/crypto v0.0.5-0.20210226175725-80576a407b2d gitlab.com/xx_network/primitives v0.0.4-0.20210226175628-2b2742ebb772 diff --git a/go.sum b/go.sum index f9f06652e267f37bf3dfc6433f0b0875fdfd1cc5..7127a2a219cac6a3fdb959fdb982f28832f87a11 100644 --- a/go.sum +++ b/go.sum @@ -277,6 +277,8 @@ gitlab.com/elixxir/comms v0.0.4-0.20210226172424-ab8c1dc47e40 h1:IwqPqB2E0C/k1Tc gitlab.com/elixxir/comms v0.0.4-0.20210226172424-ab8c1dc47e40/go.mod h1:QpkiEhND2w37M5+FSkFIYT6Fs5Ygw8gkBftQlrua0qk= gitlab.com/elixxir/comms v0.0.4-0.20210226175832-0cf63a696bf9 h1:O+HQbCo9uQ6EDpysurBxr26cnPCeerZEPF2gJd4ZUFo= gitlab.com/elixxir/comms v0.0.4-0.20210226175832-0cf63a696bf9/go.mod h1:zcKyiRMYf9YVFiVapcKQ/qvSUg62UGLW/xH/2tDzwdI= +gitlab.com/elixxir/comms v0.0.4-0.20210301202451-4cfc6b78e6d3 h1:XBucipuu+S1iMOp/yeGTLO0C8lztscNKfD5uilRgPgg= +gitlab.com/elixxir/comms v0.0.4-0.20210301202451-4cfc6b78e6d3/go.mod h1:zcKyiRMYf9YVFiVapcKQ/qvSUg62UGLW/xH/2tDzwdI= gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4 h1:28ftZDeYEko7xptCZzeFWS1Iam95dj46TWFVVlKmw6A= 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= @@ -328,6 +330,8 @@ gitlab.com/elixxir/primitives v0.0.3-0.20210301181859-270c497da89f h1:VfEc9ghiqJ gitlab.com/elixxir/primitives v0.0.3-0.20210301181859-270c497da89f/go.mod h1:l4DczasdfnrLdf2qimxOlJjr0RpcMzSwVCZXN3wxSzM= gitlab.com/elixxir/primitives v0.0.3-0.20210301183103-afcb5c703289 h1:hIhtGuJX6nj1SPiztTo1lBUWc4qCE8leH1k6hVWE8h4= gitlab.com/elixxir/primitives v0.0.3-0.20210301183103-afcb5c703289/go.mod h1:l4DczasdfnrLdf2qimxOlJjr0RpcMzSwVCZXN3wxSzM= +gitlab.com/elixxir/primitives v0.0.3-0.20210301202753-0aead7f17423 h1:au+EX4Jd7SKIxp7WYxoRBg1Fa5hrQ/byCEvgM9mb9Pg= +gitlab.com/elixxir/primitives v0.0.3-0.20210301202753-0aead7f17423/go.mod h1:l4DczasdfnrLdf2qimxOlJjr0RpcMzSwVCZXN3wxSzM= gitlab.com/xx_network/comms v0.0.0-20200805174823-841427dd5023/go.mod h1:owEcxTRl7gsoM8c3RQ5KAm5GstxrJp5tn+6JfQ4z5Hw= gitlab.com/xx_network/comms v0.0.3/go.mod h1:YViGbRj7FjJYoaO4NpALGEd9dK/l8uUT000FEBbUTL8= gitlab.com/xx_network/comms v0.0.4-0.20210210215624-622913c0a215 h1:f+cobRegYJH2x6cFkf1RfJEGFnX/vq2hGLltFC+g4Bk= diff --git a/network/follow.go b/network/follow.go index 87b0aa0732189f0ef3b7dc8eb3fb42ed03ab67dc..9fec25f3b2035653f1e8336bc11aaee05fdaa975 100644 --- a/network/follow.go +++ b/network/follow.go @@ -38,6 +38,7 @@ import ( ) const debugTrackPeriod = 1*time.Minute +const maxChecked = 100000 //comms interface makes testing easier type followNetworkComms interface { @@ -177,7 +178,7 @@ func (m *manager) follow(rng csprng.Source, comms followNetworkComms) { update.State = uint32(states.FAILED) m.Instance.GetRoundEvents().TriggerRoundEvent(update) - // Delete all existing keys and trigger a re-registration with the relevant Node + // delete all existing keys and trigger a re-registration with the relevant Node m.Session.Cmix().Remove(nid) m.Instance.GetAddGatewayChan() <- nGw } @@ -229,9 +230,15 @@ func (m *manager) follow(rng csprng.Source, comms followNetworkComms) { return m.round.Checker(rid, filterList, identity) } + // move the earliest unknown round tracker forward to the earliest + // tracked round if it is behind + earliestTrackedRound := id.Round(pollResp.EarliestRound) + identity.UR.Set(earliestTrackedRound) + // loop through all rounds the client does not know about and the gateway // does, checking the bloom filter for the user to see if there are // messages for the user (bloom not implemented yet) - identity.KR.RangeUncheckedMaskedRange(gwRoundsState, roundChecker, - firstRound, lastRound+1, int(m.param.MaxCheckedRounds)) + earliestRemaining := gwRoundsState.RangeUnchecked(identity.UR.Get(), + maxChecked, roundChecker) + identity.UR.Set(earliestRemaining) } diff --git a/single/fingerprintMap.go b/single/fingerprintMap.go index 10230e7ec6297cc9380791e5e24fb637d8f06b45..27f7b54f1bb9b990097cc6cd4b36cb2ecd16f658 100644 --- a/single/fingerprintMap.go +++ b/single/fingerprintMap.go @@ -47,7 +47,7 @@ func (fpm *fingerprintMap) getKey(dhKey *cyclic.Int, fp format.Fingerprint) ([]b return nil, false } - // Delete found fingerprint + // delete found fingerprint delete(fpm.fps, fp) // Generate and return the key diff --git a/storage/auth/store.go b/storage/auth/store.go index 89aff5457a94b9ea2b9e79dcd772b3a1a11ead32..4d67ac6160e328fbb38bfd5c762d5c516d22b92e 100644 --- a/storage/auth/store.go +++ b/storage/auth/store.go @@ -248,7 +248,7 @@ func (s *Store) AddReceived(c contact.Contact) error { // GetFingerprint can return either a private key or a sentRequest if the // fingerprint is found. If it returns a sentRequest, then it takes the lock to // ensure there is only one operator at a time. The user of the API must release -// the lock by calling store.Delete() or store.Failed() with the partner ID. +// the lock by calling store.delete() or store.Failed() with the partner ID. func (s *Store) GetFingerprint(fp format.Fingerprint) (FingerprintType, *SentRequest, *cyclic.Int, error) { @@ -289,7 +289,7 @@ func (s *Store) GetFingerprint(fp format.Fingerprint) (FingerprintType, // GetReceivedRequest returns the contact representing the receive request, if // it exists. If it returns, then it takes the lock to ensure that there is only // one operator at a time. The user of the API must release the lock by calling -// store.Delete() or store.Failed() with the partner ID. +// store.delete() or store.Failed() with the partner ID. func (s *Store) GetReceivedRequest(partner *id.ID) (contact.Contact, error) { s.mux.RLock() r, ok := s.requests[*partner] @@ -372,7 +372,7 @@ func (s *Store) Fail(partner *id.ID) { r.mux.Unlock() } -// Delete is one of two calls after using a request. This one is to be used when +// delete is one of two calls after using a request. This one is to be used when // the use is unsuccessful. It deletes all references to the request associated // with the passed partner, if it exists. It will allow any thread waiting on // access to continue. They should fail due to the deletion of the structure. diff --git a/storage/auth/store_test.go b/storage/auth/store_test.go index 25fd59ccb8b70634486288f4adc6aba7f02389d5..591cc31b483e05fcee8bb8084416d3dc71b61c0d 100644 --- a/storage/auth/store_test.go +++ b/storage/auth/store_test.go @@ -560,11 +560,11 @@ func TestStore_Delete_ReceiveRequest(t *testing.T) { err := s.Delete(c.ID) if err != nil { - t.Errorf("Delete() returned an error: %+v", err) + t.Errorf("delete() returned an error: %+v", err) } if s.requests[*c.ID] != nil { - t.Errorf("Delete() failed to delete request for user %s.", c.ID) + t.Errorf("delete() failed to delete request for user %s.", c.ID) } } @@ -589,15 +589,15 @@ func TestStore_Delete_SentRequest(t *testing.T) { err := s.Delete(sr.partner) if err != nil { - t.Errorf("Delete() returned an error: %+v", err) + t.Errorf("delete() returned an error: %+v", err) } if s.requests[*sr.partner] != nil { - t.Errorf("Delete() failed to delete request for user %s.", sr.partner) + t.Errorf("delete() failed to delete request for user %s.", sr.partner) } if _, exists := s.fingerprints[sr.fingerprint]; exists { - t.Errorf("Delete() failed to delete fingerprint for fp %v.", sr.fingerprint) + t.Errorf("delete() failed to delete fingerprint for fp %v.", sr.fingerprint) } } @@ -607,7 +607,7 @@ func TestStore_Delete_RequestNotInMap(t *testing.T) { err := s.Delete(id.NewIdFromUInt(rand.Uint64(), id.User, t)) if err == nil { - t.Errorf("Delete() did not return an error when the request was not " + + t.Errorf("delete() did not return an error when the request was not " + "in the map.") } } diff --git a/storage/conversation/store.go b/storage/conversation/store.go index 9e6dfae045bed7339efcc357a9f5b35f3352f64a..662e6c1f2599816258e9e75f294ae7f27dc425cb 100644 --- a/storage/conversation/store.go +++ b/storage/conversation/store.go @@ -50,7 +50,7 @@ func (s *Store) Get(partner *id.ID) *Conversation { return c } -// Delete deletes the conversation with the given partner ID from memory and +// delete deletes the conversation with the given partner ID from memory and // storage. Panics if the object cannot be deleted from storage. func (s *Store) Delete(partner *id.ID) { s.mux.Lock() @@ -62,13 +62,13 @@ func (s *Store) Delete(partner *id.ID) { return } - // Delete contact from storage + // delete contact from storage err := c.delete() if err != nil { jww.FATAL.Panicf("Failed to remover conversation with ID %s from "+ "storage: %+v", partner, err) } - // Delete contact from memory + // delete contact from memory delete(s.loadedConversations, *partner) } diff --git a/storage/conversation/store_test.go b/storage/conversation/store_test.go index fe9882af5f2050f15596e9c721cb9281922684df..7505280e1c534486b60b47b76aac4858fdc1bc4a 100644 --- a/storage/conversation/store_test.go +++ b/storage/conversation/store_test.go @@ -44,7 +44,7 @@ func TestStore_Delete(t *testing.T) { store.Get(pid) } - // Delete conversations with IDs with even numbered indexes + // delete conversations with IDs with even numbered indexes for i := 0; i < len(pids); i += 2 { store.Delete(pids[i]) } @@ -54,11 +54,11 @@ func TestStore_Delete(t *testing.T) { _, exists := store.loadedConversations[*pid] if i%2 == 0 { if exists { - t.Errorf("%d. Delete() failed to delete the conversation "+ + t.Errorf("%d. delete() failed to delete the conversation "+ "(ID %s) from memory.", i, pid) } } else if !exists { - t.Errorf("%d. Delete() unexpetedly deleted the conversation "+ + t.Errorf("%d. delete() unexpetedly deleted the conversation "+ "(ID %s) from memory.", i, pid) } } diff --git a/storage/e2e/session.go b/storage/e2e/session.go index 1523cd88b9f5c9bf1a610a26debe80a9fcdc1d53..f339a4a6883f944c810efc1160424bd0449e45d7 100644 --- a/storage/e2e/session.go +++ b/storage/e2e/session.go @@ -188,7 +188,7 @@ func (s *Session) save() error { /*METHODS*/ // Done all unused key fingerprints -// Delete this session and its key states from the storage +// delete this session and its key states from the storage func (s *Session) Delete() { s.mux.Lock() defer s.mux.Unlock() diff --git a/storage/e2e/session_test.go b/storage/e2e/session_test.go index 6ced8eeb99679100c05e0f3b1957d4749443e35e..63126229c108d5b400b9cac34883a9b00efb510f 100644 --- a/storage/e2e/session_test.go +++ b/storage/e2e/session_test.go @@ -300,7 +300,7 @@ func TestSession_PopKey(t *testing.T) { } } -// Delete should remove unused keys from this session +// delete should remove unused keys from this session func TestSession_Delete(t *testing.T) { s, _ := makeTestSession() err := s.save() diff --git a/storage/partition/multiPartMessage.go b/storage/partition/multiPartMessage.go index 9aa583c7a78a9616035a8df43ba713b57a34a107..257013b8ea08249d7007cbb6e3f017299d787b05 100644 --- a/storage/partition/multiPartMessage.go +++ b/storage/partition/multiPartMessage.go @@ -177,7 +177,7 @@ func (mpm *multiPartMessage) IsComplete(relationshipFingerprint []byte) (message lenMsg += len(mpm.parts[i]) } - // Delete the multipart message + // delete the multipart message mpm.delete() mpm.mux.Unlock() diff --git a/storage/reception/IdentityUse.go b/storage/reception/IdentityUse.go index 4bbbc4ee4b7f9819b75aaf3efa093ddfb8f2a032..829425ea5e0c439701897543feaa14d71a273508 100644 --- a/storage/reception/IdentityUse.go +++ b/storage/reception/IdentityUse.go @@ -3,9 +3,7 @@ package reception import ( "github.com/pkg/errors" "gitlab.com/elixxir/crypto/hash" - "gitlab.com/elixxir/primitives/knownRounds" "gitlab.com/xx_network/crypto/randomness" - "gitlab.com/xx_network/primitives/id" "io" "math/big" "time" @@ -22,7 +20,7 @@ type IdentityUse struct { Fake bool // rounds data - KR KnownRounds + UR *UnknownRound } // setSamplingPeriod add the Request mask as a random buffer around the sampling @@ -47,15 +45,4 @@ func (iu IdentityUse) setSamplingPeriod(rng io.Reader) (IdentityUse, error) { iu.StartRequest = iu.StartValid.Add(-time.Duration(periodOffset)) iu.EndRequest = iu.EndValid.Add(iu.RequestMask - time.Duration(periodOffset)) return iu, nil -} - -type KnownRounds interface { - Checked(rid id.Round) bool - Check(rid id.Round) - Forward(rid id.Round) - RangeUnchecked(newestRid id.Round, roundCheck func(id id.Round) bool) - RangeUncheckedMasked(mask *knownRounds.KnownRounds, - roundCheck knownRounds.RoundCheckFunc, maxChecked int) - RangeUncheckedMaskedRange(mask *knownRounds.KnownRounds, - roundCheck knownRounds.RoundCheckFunc, start, end id.Round, maxChecked int) -} +} \ No newline at end of file diff --git a/storage/reception/fake_test.go b/storage/reception/fake_test.go index babf46ec2361207bf1872a21ca326e7b556dd209..1327f84a03f6f779a63f652584fdb4e17c2cb72b 100644 --- a/storage/reception/fake_test.go +++ b/storage/reception/fake_test.go @@ -24,7 +24,7 @@ func Test_generateFakeIdentity(t *testing.T) { "\"EndValid\":" + string(endValid) + "," + "\"RequestMask\":86400000000000,\"Ephemeral\":true," + "\"StartRequest\":\"0001-01-01T00:00:00Z\"," + - "\"EndRequest\":\"0001-01-01T00:00:00Z\",\"Fake\":true,\"KR\":null}" + "\"EndRequest\":\"0001-01-01T00:00:00Z\",\"Fake\":true,\"UR\":null}" timestamp := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) diff --git a/storage/reception/identity.go b/storage/reception/identity.go index e64c0dd33c172a5bc4a854c8f6379670d08316f1..3aded8192bcf2b35c798187eb55153bd35d0ee7e 100644 --- a/storage/reception/identity.go +++ b/storage/reception/identity.go @@ -74,10 +74,6 @@ func (i Identity) delete(kv *versioned.KV) error { return kv.Delete(identityStorageKey) } -func (i Identity) calculateKrSize() int { - return int(i.EndValid.Sub(i.StartValid).Seconds()+1) * maxRoundsPerSecond -} - func (i *Identity) String() string { return strconv.FormatInt(i.EphId.Int64(), 16) + " " + i.Source.String() } diff --git a/storage/reception/identity_test.go b/storage/reception/identity_test.go index 4657330ebb1cf3359eb3fb34cedba96c74e2e2f9..8499050e9e988b1a48b9e59ad8501ddd211a74c6 100644 --- a/storage/reception/identity_test.go +++ b/storage/reception/identity_test.go @@ -81,25 +81,6 @@ func TestIdentity_String(t *testing.T) { } } -func TestIdentity_CalculateKrSize(t *testing.T) { - deltas := []time.Duration{0, 2 * time.Second, 2 * time.Hour, 36 * time.Hour, - time.Duration(rand.Uint32()) * time.Millisecond} - for _, d := range deltas { - expected := int(d.Seconds()+1) * maxRoundsPerSecond - now := time.Now() - i := Identity{ - StartValid: now, - EndValid: now.Add(d), - } - - krSize := i.calculateKrSize() - if krSize != expected { - t.Errorf("krSize is not correct.\nexpected: %d\nrecieved: %d", - expected, krSize) - } - } -} - func TestIdentity_Equal(t *testing.T) { timestamp := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) a, _ := generateFakeIdentity(rand.New(rand.NewSource(42)), 15, timestamp) diff --git a/storage/reception/registration.go b/storage/reception/registration.go index bb5244aaa42bab38a5d06aae50beed0939b93ac3..a0c835cc482783b9e6da45ef0371bd1bc659c467 100644 --- a/storage/reception/registration.go +++ b/storage/reception/registration.go @@ -2,22 +2,18 @@ package reception import ( "github.com/pkg/errors" - "gitlab.com/elixxir/client/storage/utility" "gitlab.com/elixxir/client/storage/versioned" - "gitlab.com/elixxir/primitives/knownRounds" "gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id/ephemeral" "strconv" "time" ) -const maxRoundsPerSecond = 2 const knownRoundsStorageKey = "krStorage" type registration struct { Identity - knownRounds *knownRounds.KnownRounds - knownRoundsStorage *utility.KnownRounds + ur *UnknownRound kv *versioned.KV } @@ -40,7 +36,7 @@ func newRegistration(reg Identity, kv *versioned.KV) (*registration, error) { r := ®istration{ Identity: reg, - knownRounds: knownRounds.NewKnownRound(reg.calculateKrSize()), + ur: NewUnknownRound(!reg.Ephemeral, kv), kv: kv, } @@ -48,17 +44,14 @@ func newRegistration(reg Identity, kv *versioned.KV) (*registration, error) { if !reg.Ephemeral { // Store known rounds var err error - r.knownRoundsStorage, err = utility.NewKnownRounds(kv, knownRoundsStorageKey, r.knownRounds) - if err != nil { - return nil, errors.WithMessage(err, "failed to store known rounds") - } - // Store the registration if err = reg.store(kv); err != nil { return nil, errors.WithMessage(err, "failed to store registration") } } + + return r, nil } @@ -73,15 +66,11 @@ func loadRegistration(EphId ephemeral.Id, Source *id.ID, startValid time.Time, "for %s", regPrefix(EphId, Source, startValid)) } - kr, err := utility.LoadKnownRounds(kv, knownRoundsStorageKey, reg.calculateKrSize()) - if err != nil { - return nil, errors.WithMessagef(err, "Failed to load known "+ - "rounds for %s", regPrefix(EphId, Source, startValid)) - } + ur := LoadUnknownRound(kv) r := ®istration{ Identity: reg, - knownRoundsStorage: kr, + ur: ur, kv: kv, } @@ -90,10 +79,7 @@ func loadRegistration(EphId ephemeral.Id, Source *id.ID, startValid time.Time, func (r *registration) Delete() error { if !r.Ephemeral { - if err := r.knownRoundsStorage.Delete(); err != nil { - return errors.WithMessagef(err, "Failed to delete registration "+ - "known rounds %s", r) - } + r.ur.delete() if err := r.delete(r.kv); err != nil { return errors.WithMessagef(err, "Failed to delete registration "+ "public data %s", r) @@ -103,14 +89,6 @@ func (r *registration) Delete() error { return nil } -func (r registration) getKR() KnownRounds { - if r.Ephemeral { - return r.knownRounds - } else { - return r.knownRoundsStorage - } -} - func regPrefix(EphId ephemeral.Id, Source *id.ID, startTime time.Time) string { return "receptionRegistration_" + strconv.FormatInt(EphId.Int64(), 16) + Source.String() + diff --git a/storage/reception/registration_test.go b/storage/reception/registration_test.go index ffb155fe3b0e08949fd66b73975ac98cb4733692..4705be0c6b4b50bfca55abac809802b8992f45e0 100644 --- a/storage/reception/registration_test.go +++ b/storage/reception/registration_test.go @@ -1,7 +1,6 @@ package reception import ( - "gitlab.com/elixxir/client/storage/utility" "gitlab.com/elixxir/client/storage/versioned" "gitlab.com/elixxir/ekv" "math/rand" @@ -45,19 +44,10 @@ func TestNewRegistration_Ephemeral(t *testing.T) { "succeeded: %+v", err) } - if reg.knownRounds == nil { + if reg.ur == nil { t.Error("Ephemeral identity does not have a known rounds.") } - if reg.knownRoundsStorage != nil { - t.Error("Ephemeral identity has a known rounds storage.") - } - - // Check if the known rounds is stored, it should not be - if _, err = utility.LoadKnownRounds(reg.kv, knownRoundsStorageKey, id.calculateKrSize()); err == nil { - t.Error("Ephemeral identity stored the known rounds when it should not have.") - } - if _, err = reg.kv.Get(identityStorageKey); err == nil { t.Error("Ephemeral identity stored the identity when it should not have.") } @@ -81,22 +71,17 @@ func TestNewRegistration_Persistent(t *testing.T) { "succeeded: %+v", err) } - if reg.knownRounds == nil { + if reg.ur == nil { t.Error("Persistent identity does not have a known rounds.") } - if reg.knownRoundsStorage == nil { - t.Error("Persistent identity does not have a known rounds storage.") - } - - // Check if the known rounds is stored, it should not be - if _, err = utility.LoadKnownRounds(reg.kv, knownRoundsStorageKey, id.calculateKrSize()); err != nil { - t.Errorf("Persistent identity did not store known rounds when "+ - "it should: %+v", err) - } + // Check if the known rounds is stored, it should not be. this will panic + // if it isnt + LoadUnknownRound(reg.kv) if _, err = reg.kv.Get(identityStorageKey); err != nil { - t.Error("Persistent identity did not store the identity when it should.") + t.Errorf("Persistent identity did not store the identity when " + + "it should: %+v.", err) } } @@ -123,13 +108,10 @@ func TestLoadRegistration(t *testing.T) { t.Fatalf("Registration loading failed: %+v", err) } - if reg.knownRounds != nil { - t.Error("Loading has a separated known rounds, it should not have.") + if reg.ur == nil { + t.Error("Loading should have a UR.") } - if reg.knownRoundsStorage == nil { - t.Error("Loading identity does not have a known rounds storage.") - } } // TODO: finish @@ -153,6 +135,6 @@ func Test_registration_Delete(t *testing.T) { err = r.Delete() if err != nil { - t.Errorf("Delete() returned an error: %+v", err) + t.Errorf("delete() returned an error: %+v", err) } } diff --git a/storage/reception/store.go b/storage/reception/store.go index e95f9281f28a8a5294dba598bd19703a7e0ccfdf..8a55043b30e16ec20577418d54f3521241ca5db6 100644 --- a/storage/reception/store.go +++ b/storage/reception/store.go @@ -349,6 +349,6 @@ func (s *Store) selectIdentity(rng io.Reader, now time.Time) (IdentityUse, error return IdentityUse{ Identity: selected.Identity, Fake: false, - KR: selected.getKR(), + UR: selected.ur, }, nil } diff --git a/storage/reception/store_test.go b/storage/reception/store_test.go index bb6ad2b6fc395cd9324136383bb3c6065044621f..635ad0771250a91c2ca2e7d25cbb4f733e3e0cff 100644 --- a/storage/reception/store_test.go +++ b/storage/reception/store_test.go @@ -66,7 +66,7 @@ func TestLoadStore(t *testing.T) { testStore := LoadStore(kv) for i, active := range testStore.active { - s.active[i].knownRounds = nil + s.active[i].ur = nil if !s.active[i].Equal(active.Identity) { t.Errorf("Failed to generate expected Store."+ "\nexpected: %#v\nreceived: %#v", s.active[i], active) diff --git a/storage/reception/unknownRound.go b/storage/reception/unknownRound.go new file mode 100644 index 0000000000000000000000000000000000000000..b802f989705dab3fe7cb8c38f32dc60e585836ed --- /dev/null +++ b/storage/reception/unknownRound.go @@ -0,0 +1,97 @@ +package reception + +import ( + "encoding/json" + jww "github.com/spf13/jwalterweatherman" + "gitlab.com/elixxir/client/storage/versioned" + "gitlab.com/xx_network/primitives/id" + "sync" + "time" +) + +const unknownRoundStorageKey = "unknownRoundStorage" +const unknownRoundStorageVersion = 0 + +type UnknownRound struct{ + stored bool + kv *versioned.KV + rid id.Round + mux sync.Mutex +} + +func NewUnknownRound(stored bool, kv *versioned.KV)*UnknownRound{ + ur := &UnknownRound{ + stored: stored, + kv: kv, + rid: 0, + } + ur.save() + return ur +} + +func LoadUnknownRound(kv *versioned.KV)*UnknownRound{ + ur := &UnknownRound{ + stored: true, + kv: kv, + rid: 0, + } + + obj, err := kv.Get(unknownRoundStorageKey) + if err != nil { + jww.FATAL.Panicf("Failed to get the unknown round: %+v", err) + } + + + err = json.Unmarshal(obj.Data, &ur.rid) + if err != nil { + jww.FATAL.Panicf("Failed to unmarshal the unknown round: %+v", err) + } + return ur +} + +func (ur *UnknownRound)save() { + if ur.stored { + urStr, err := json.Marshal(&ur.rid) + if err != nil { + jww.FATAL.Panicf("Failed to marshal the unknown round: %+v", err) + } + + // Create versioned object with data + obj := &versioned.Object{ + Version: unknownRoundStorageVersion, + Timestamp: time.Now(), + Data: urStr, + } + + err = ur.kv.Set(unknownRoundStorageKey, obj) + if err != nil { + jww.FATAL.Panicf("Failed to store the unknown round: %+v", err) + } + + } +} + + +func (ur *UnknownRound)Set(rid id.Round){ + ur.mux.Lock() + defer ur.mux.Unlock() + if rid>ur.rid{ + ur.rid = rid + ur.save() + } +} + +func (ur *UnknownRound)Get()id.Round { + ur.mux.Lock() + defer ur.mux.Unlock() + return ur.rid +} + +func (ur *UnknownRound) delete() { + ur.mux.Lock() + defer ur.mux.Unlock() + err := ur.kv.Delete(unknownRoundStorageKey) + if err!=nil{ + jww.FATAL.Panicf("Failed to delete unknownRound storage: %+v", err) + } +} diff --git a/storage/session.go b/storage/session.go index be439f92c55d8474baceabdcd3ba400c8ffa19d9..de3768d13e941592da584439a3c15b1d38899a8c 100644 --- a/storage/session.go +++ b/storage/session.go @@ -263,7 +263,7 @@ func (s *Session) Set(key string, object *versioned.Object) error { return s.kv.Set(key, object) } -// Delete a value in the session +// delete a value in the session func (s *Session) Delete(key string) error { return s.kv.Delete(key) } diff --git a/storage/versioned/kv.go b/storage/versioned/kv.go index f9391b7a4acce3f3b7d1c15833561ed866b06654..72e0386de79cecd40b837b1081264d3bf6c6ea69 100644 --- a/storage/versioned/kv.go +++ b/storage/versioned/kv.go @@ -103,10 +103,10 @@ func (v *KV) Get(key string) (*Object, error) { return &result, nil } -// Delete removes a given key from the data store +// delete removes a given key from the data store func (v *KV) Delete(key string) error { key = v.prefix + key - jww.TRACE.Printf("Delete %p with key %v", v.r.data, key) + jww.TRACE.Printf("delete %p with key %v", v.r.data, key) return v.r.data.Delete(key) }