diff --git a/bindings/notifications.go b/bindings/notifications.go index 745dafb1b32d4b7aa00f3ab29043caee19cfe4bd..1741003694c41364baf1ee5f0c211241e4c8528c 100644 --- a/bindings/notifications.go +++ b/bindings/notifications.go @@ -48,7 +48,7 @@ func NotificationForMe(messageHash, idFP string, preimages string) (*Notificatio } //handle deserialization of preimages - var preimageList edge.Preimages + var preimageList []edge.Preimage if err := json.Unmarshal([]byte(preimages),&preimageList); err!=nil{ return nil, errors.WithMessagef(err,"Failed to unmarshal the preimages list, " + "cannot check if notification is for me") diff --git a/network/message/handler.go b/network/message/handler.go index a9a387b2c51e2324ffc6c0aafc73b79e79737465..4dc26f2f29f5c44c337afa276177bd68a47459a4 100644 --- a/network/message/handler.go +++ b/network/message/handler.go @@ -11,6 +11,7 @@ import ( "fmt" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/interfaces/message" + "gitlab.com/elixxir/client/interfaces/preimage" "gitlab.com/elixxir/client/stoppable" "gitlab.com/elixxir/client/storage/edge" "gitlab.com/elixxir/crypto/e2e" @@ -52,22 +53,12 @@ func (m *Manager) handleMessage(ecrMsg format.Message, bundle Bundle, edge *edge var err error var relationshipFingerprint []byte - //check if the identity fingerprint matches - //first check if a list is present in store for the receiving source - forMe := false - preimagelist, exist := edge.Get(identity.Source) - if exist{ - //if it exists, check against all in the list - for key := range preimagelist{ - if forMe = fingerprint2.CheckIdentityFP(ecrMsg.GetIdentityFP(), - ecrMsg.GetContents(), preimagelist[key].Data); forMe{ - break - } - } - }else{ + //if it exists, check against all in the list + has, forMe, _ := m.Session.GetEdge().Check(identity.Source,fingerprint[:],ecrMsg.GetContents()) + if !has{ //if it doesnt exist, check against the default fingerprint for the identity forMe = fingerprint2.CheckIdentityFP(ecrMsg.GetIdentityFP(), - ecrMsg.GetContents(), identity.Source[:]) + ecrMsg.GetContents(),preimage.MakeDefault(identity.Source)) } if !forMe { diff --git a/storage/edge/edge.go b/storage/edge/edge.go index 095780591630850d8e609e495c1c52647ae01648..d1bc30bb0e5c33d5a739ba63a8cf7952753f4fe5 100644 --- a/storage/edge/edge.go +++ b/storage/edge/edge.go @@ -5,6 +5,7 @@ import ( "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/storage/versioned" + fingerprint2 "gitlab.com/elixxir/crypto/fingerprint" "gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/netTime" "sync" @@ -146,12 +147,42 @@ func (s *Store) Remove(preimage Preimage, identity *id.ID) error { } // Get returns the Preimages list for the given identity. -func (s *Store) Get(identity *id.ID) (Preimages, bool) { +func (s *Store) Get(identity *id.ID) ([]Preimage, bool) { s.mux.RLock() defer s.mux.RUnlock() preimages, exists := s.edge[*identity] - return preimages, exists + if !exists{ + return nil, false + } + + preiamgesSlice := make([]Preimage, 0, len(preimages)) + + for _, preimage := range preimages{ + preiamgesSlice = append(preiamgesSlice,preimage) + } + return preiamgesSlice, exists +} + +// Check looks checks if the identity fingerprint matches for any of +// the stored preimages. It returns the preimage it hit with if it +// finds one. +func (s *Store) Check(identity *id.ID, identityFP []byte, messageContents []byte) (bool, bool, Preimage) { + s.mux.RLock() + defer s.mux.RUnlock() + + preimages, exists := s.edge[*identity] + if !exists{ + return false, false, Preimage{} + } + + for _, preimage := range preimages{ + if fingerprint2.CheckIdentityFP(identityFP, messageContents, preimage.Data){ + return true, true, preimage + } + } + + return true, false, Preimage{} } // AddUpdateCallback adds the callback to be called for changes to the identity. diff --git a/storage/edge/edge_test.go b/storage/edge/edge_test.go index b03e3240601b95184e5976c70a789de9e8910be6..a1796cf322d8498bc91e1c897ae61b88b84af1a5 100644 --- a/storage/edge/edge_test.go +++ b/storage/edge/edge_test.go @@ -148,7 +148,7 @@ func TestStore_Add(t *testing.T) { "\nexpected: %d\nreceived: %d", identities[0], 3, len(pis)) } - expected := Preimage{identities[0].Bytes(), preimage.Default, identities[0].Bytes()} + expected := Preimage{preimage.Generate(identities[0].Bytes(),preimage.Default), preimage.Default, identities[0].Bytes()} if !reflect.DeepEqual(pis[expected.key()], expected) { t.Errorf("First Preimage of first Preimages does not match expected."+ "\nexpected: %+v\nreceived: %+v", expected, pis[expected.key()]) @@ -173,7 +173,7 @@ func TestStore_Add(t *testing.T) { "\nexpected: %d\nreceived: %d", identities[1], 2, len(pis)) } - expected = Preimage{identities[1].Bytes(), preimage.Default, identities[1].Bytes()} + expected = Preimage{preimage.Generate(identities[1].Bytes(),preimage.Default), preimage.Default, identities[1].Bytes()} if !reflect.DeepEqual(pis[expected.key()], expected) { t.Errorf("First Preimage of second Preimages does not match expected."+ "\nexpected: %+v\nreceived: %+v", expected, pis[expected.key()]) @@ -242,6 +242,8 @@ func TestStore_Remove(t *testing.T) { } }() + + id1Chan := make(chan struct { identity *id.ID deleted bool @@ -291,9 +293,9 @@ func TestStore_Remove(t *testing.T) { t.Errorf("Remove returned an error: %+v", err) } - if len(s.edge) != 2 { + if len(s.edge) != 3 { t.Errorf("Length of edge incorrect.\nexpected: %d\nreceived: %d", - 1, len(s.edge)) + 2, len(s.edge)) } pis := s.edge[*identities[0]] @@ -317,9 +319,9 @@ func TestStore_Remove(t *testing.T) { pis = s.edge[*identities[1]] - if len(pis) != 0 { + if len(pis) != 1 { t.Errorf("Length of preimages for identity %s inocrrect."+ - "\nexpected: %d\nreceived: %d", identities[1], 0, len(pis)) + "\nexpected: %d\nreceived: %d", identities[1], 1, len(pis)) } wg.Wait() @@ -347,15 +349,25 @@ func TestStore_Get(t *testing.T) { t.Errorf("No Preimages found for identity %s.", identities[0]) } - expected := Preimages{ - identities[0].String(): Preimage{identities[0].Bytes(), preimage.Default, identities[0].Bytes()}, - preimages[0].key(): preimages[0], - preimages[2].key(): preimages[2], + expected := []Preimage{ + {preimage.Generate(identities[0].Bytes(),preimage.Default), preimage.Default, identities[0].Bytes()}, + preimages[0], + preimages[2], } - if !reflect.DeepEqual(expected, pis) { - t.Errorf("First Preimages for identity %s does not match expected."+ - "\nexpected: %+v\nreceived: %+v", identities[0], expected, pis) + if len(expected)!=len(pis){ + t.Errorf("First Preimages for identity %s does not match expected, difrent lengths of %d and %d"+ + "\nexpected: %+v\nreceived: %+v", identities[0],len(expected), len(pis), expected, pis) + } + + top: + for i, lookup := range expected{ + for _, checked := range pis{ + if reflect.DeepEqual(lookup,checked){ + continue top + } + } + t.Errorf("Entree %d in expected %v not found in received %v", i, lookup, pis) } pis, exists = s.Get(identities[1]) @@ -363,14 +375,24 @@ func TestStore_Get(t *testing.T) { t.Errorf("No Preimages found for identity %s.", identities[1]) } - expected = Preimages{ - identities[1].String(): Preimage{identities[1].Bytes(), preimage.Default, identities[1].Bytes()}, - preimages[1].key(): preimages[1], + expected = []Preimage{ + {preimage.Generate(identities[1].Bytes(),preimage.Default), preimage.Default, identities[1].Bytes()}, + preimages[1], + } + + if len(expected)!=len(pis){ + t.Errorf("First Preimages for identity %s does not match expected, difrent lengths of %d and %d"+ + "\nexpected: %+v\nreceived: %+v", identities[0],len(expected), len(pis), expected, pis) } - if !reflect.DeepEqual(expected, pis) { - t.Errorf("First Preimages for identity %s does not match expected."+ - "\nexpected: %+v\nreceived: %+v", identities[1], expected, pis) + top2: + for i, lookup := range expected{ + for _, checked := range pis{ + if reflect.DeepEqual(lookup,checked){ + continue top2 + } + } + t.Errorf("Entree %d in expected %v not found in received %v", i, lookup, pis) } } @@ -452,15 +474,15 @@ func TestLoadStore(t *testing.T) { t.Fatalf("LoadStore error: %v", err) } - expectedPis := []Preimages{ + expectedPis := [][]Preimage{ { - identities[0].String(): Preimage{identities[0].Bytes(), preimage.Default, identities[0].Bytes()}, - preimages[0].key(): preimages[0], - preimages[2].key(): preimages[2], + Preimage{preimage.Generate(identities[0].Bytes(), preimage.Default), preimage.Default, identities[0].Bytes()}, + preimages[0], + preimages[2], }, { - identities[1].String(): Preimage{identities[1].Bytes(), preimage.Default, identities[1].Bytes()}, - preimages[1].key(): preimages[1], + Preimage{preimage.Generate(identities[1].Bytes(), preimage.Default), preimage.Default, identities[1].Bytes()}, + preimages[1], }, } @@ -470,10 +492,22 @@ func TestLoadStore(t *testing.T) { t.Errorf("Identity %s does not exist in loaded store", identity) } - if !reflect.DeepEqual(pis, expectedPis[i]) { - t.Errorf("Identity %s does not have expected preimages in loaded store."+ - "\nExpected: %v\nRecieved", expectedPis[i], pis) + if len(expectedPis[i])!=len(pis){ + t.Errorf("First Preimages for identity %s does not match expected, difrent lengths of %d and %d"+ + "\nexpected: %+v\nreceived: %+v", identities[0],len(expectedPis[i]), len(pis), expectedPis[i], pis) } + + top: + for idx, lookup := range expectedPis[i]{ + for _, checked := range pis{ + if reflect.DeepEqual(lookup,checked){ + continue top + } + } + t.Errorf("Entree %d in expected %v not found in received %v", idx, lookup, pis) + } + + } } diff --git a/storage/edge/preimage.go b/storage/edge/preimage.go index 0a10b1e6c17fb25891fbb37aa1e5cf3924213217..a8e4c89432dddc25596495fe69b59e7786ceab45 100644 --- a/storage/edge/preimage.go +++ b/storage/edge/preimage.go @@ -32,12 +32,13 @@ type Preimages map[string]Preimage // newPreimages makes a Preimages object for the given identity and populates // it with the default preimage for the identity. Does not store to disk. func newPreimages(identity *id.ID) Preimages { + defaultPreimage := Preimage{ + Data: preimage.MakeDefault(identity), + Type: preimage.Default, + Source: identity[:], + } pis := Preimages{ - identity.String(): { - Data: preimage.MakeDefault(identity), - Type: preimage.Default, - Source: identity[:], - }, + defaultPreimage.key(): defaultPreimage, } return pis