chore: code review comments applied

- sync clock set for all settings prop participating in syncing
- if other sql error happens we return immediately
This commit is contained in:
Sale Djenic 2022-12-12 11:20:32 +01:00 committed by saledjenic
parent 1340a55c1d
commit d98d83d308
7 changed files with 45 additions and 105 deletions

View File

@ -1 +1 @@
0.117.1 0.117.2

View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"sync" "sync"
"time"
"github.com/status-im/status-go/appdatabase" "github.com/status-im/status-go/appdatabase"
"github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/eth-node/types"
@ -144,6 +145,14 @@ INSERT INTO settings (
return err return err
} }
if s.DisplayName != "" {
now := time.Now().Unix()
err = db.SetSettingLastSynced(DisplayName, uint64(now))
if err != nil {
return err
}
}
return nodecfg.SaveConfigWithTx(tx, &n) return nodecfg.SaveConfigWithTx(tx, &n)
} }

View File

@ -151,7 +151,6 @@ type Messenger struct {
downloadHistoryArchiveTasksWaitGroup sync.WaitGroup downloadHistoryArchiveTasksWaitGroup sync.WaitGroup
verificationDatabase *verification.Persistence verificationDatabase *verification.Persistence
savedAddressesManager *wallet.SavedAddressesManager savedAddressesManager *wallet.SavedAddressesManager
backedupMessagesHandler backupHandler
// TODO(samyoul) Determine if/how the remaining usage of this mutex can be removed // TODO(samyoul) Determine if/how the remaining usage of this mutex can be removed
mutex sync.Mutex mutex sync.Mutex
@ -480,9 +479,6 @@ func NewMessenger(
}, },
logger: logger, logger: logger,
savedAddressesManager: savedAddressesManager, savedAddressesManager: savedAddressesManager,
backedupMessagesHandler: backupHandler{
postponeHandling: true,
},
} }
if c.outputMessagesCSV { if c.outputMessagesCSV {
@ -688,7 +684,6 @@ func (m *Messenger) Start() (*MessengerResponse, error) {
m.broadcastLatestUserStatus() m.broadcastLatestUserStatus()
m.timeoutAutomaticStatusUpdates() m.timeoutAutomaticStatusUpdates()
m.startBackupLoop() m.startBackupLoop()
m.startWaitingForTheLatestBackedupMessageLoop()
err = m.startAutoMessageLoop() err = m.startAutoMessageLoop()
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -310,9 +310,6 @@ func (m *Messenger) backupProfile(ctx context.Context, clock uint64) ([]*protobu
if err != nil { if err != nil {
return nil, err return nil, err
} }
if displayNameClock == 0 {
displayNameClock = clock
}
keyUID := m.account.KeyUID keyUID := m.account.KeyUID
images, err := m.multiAccounts.GetIdentityImages(keyUID) images, err := m.multiAccounts.GetIdentityImages(keyUID)

View File

@ -1,8 +1,7 @@
package protocol package protocol
import ( import (
"sync" "database/sql"
"time"
"go.uber.org/zap" "go.uber.org/zap"
@ -13,73 +12,14 @@ import (
) )
const ( const (
// timeToPostponeBackedUpMessagesHandling - the idea is to wait for 30 secs in loading state with the hope that at least
// one message from the set of last backed up messages will arrive, that we can know which set of messages to work with
timeToPostponeBackedUpMessagesHandling = 30 * time.Second
SyncWakuSectionKeyProfile = "profile" SyncWakuSectionKeyProfile = "profile"
SyncWakuSectionKeyContacts = "contacts" SyncWakuSectionKeyContacts = "contacts"
SyncWakuSectionKeyCommunities = "communities" SyncWakuSectionKeyCommunities = "communities"
SyncWakuSectionKeySettings = "settings" SyncWakuSectionKeySettings = "settings"
) )
type backupHandler struct {
messagesToProceed []protobuf.Backup
lastKnownTime uint64
postponeHandling bool
postponeTasksWaitGroup sync.WaitGroup
}
func (bh *backupHandler) addMessage(message protobuf.Backup) {
if message.Clock < bh.lastKnownTime {
return
}
if message.Clock > bh.lastKnownTime {
bh.messagesToProceed = nil
bh.lastKnownTime = message.Clock
}
bh.messagesToProceed = append(bh.messagesToProceed, message)
}
func (m *Messenger) startWaitingForTheLatestBackedupMessageLoop() {
ticker := time.NewTicker(timeToPostponeBackedUpMessagesHandling)
m.backedupMessagesHandler.postponeTasksWaitGroup.Add(1)
go func() {
defer m.backedupMessagesHandler.postponeTasksWaitGroup.Done()
for {
select {
case <-ticker.C:
if !m.backedupMessagesHandler.postponeHandling {
return
}
m.backedupMessagesHandler.postponeHandling = false
for _, msg := range m.backedupMessagesHandler.messagesToProceed {
messageState := m.buildMessageState()
errors := m.HandleBackup(messageState, msg)
if len(errors) > 0 {
for _, err := range errors {
m.logger.Warn("failed to handle postponed Backup messages", zap.Error(err))
}
}
}
m.backedupMessagesHandler.messagesToProceed = nil
case <-m.quit:
ticker.Stop()
return
}
}
}()
}
func (m *Messenger) HandleBackup(state *ReceivedMessageState, message protobuf.Backup) []error { func (m *Messenger) HandleBackup(state *ReceivedMessageState, message protobuf.Backup) []error {
var errors []error var errors []error
if m.backedupMessagesHandler.postponeHandling {
m.backedupMessagesHandler.addMessage(message)
return errors
}
err := m.handleBackedUpProfile(message.Profile, message.Clock) err := m.handleBackedUpProfile(message.Profile, message.Clock)
if err != nil { if err != nil {
@ -128,18 +68,28 @@ func (m *Messenger) handleBackedUpProfile(message *protobuf.BackedUpProfile, bac
return err return err
} }
contentSet := false
response := wakusync.WakuBackedUpDataResponse{ response := wakusync.WakuBackedUpDataResponse{
Profile: &wakusync.BackedUpProfile{}, Profile: &wakusync.BackedUpProfile{},
} }
if dbDisplayNameClock < message.DisplayNameClock { if dbDisplayNameClock < message.DisplayNameClock {
err := m.SetDisplayName(message.DisplayName, false) err = m.SetDisplayName(message.DisplayName, false)
response.AddDisplayName(message.DisplayName, err == nil) if err != nil {
return err
}
contentSet = true
response.AddDisplayName(message.DisplayName)
} }
syncWithBackedUpImages := false syncWithBackedUpImages := false
dbImages, err := m.multiAccounts.GetIdentityImages(message.KeyUid) dbImages, err := m.multiAccounts.GetIdentityImages(message.KeyUid)
if err != nil { if err != nil {
if err != sql.ErrNoRows {
return err
}
// if images are deleted and no images were backed up, then we need to delete them on other devices,
// that's why we don't return in case of `sql.ErrNoRows`
syncWithBackedUpImages = true syncWithBackedUpImages = true
} }
if len(dbImages) == 0 { if len(dbImages) == 0 {
@ -155,7 +105,11 @@ func (m *Messenger) handleBackedUpProfile(message *protobuf.BackedUpProfile, bac
if syncWithBackedUpImages { if syncWithBackedUpImages {
if len(message.Pictures) == 0 { if len(message.Pictures) == 0 {
err = m.multiAccounts.DeleteIdentityImage(message.KeyUid) err = m.multiAccounts.DeleteIdentityImage(message.KeyUid)
response.AddImages(nil, err == nil) if err != nil {
return err
}
contentSet = true
response.AddImages(nil)
} else { } else {
idImages := make([]images.IdentityImage, len(message.Pictures)) idImages := make([]images.IdentityImage, len(message.Pictures))
for i, pic := range message.Pictures { for i, pic := range message.Pictures {
@ -171,11 +125,15 @@ func (m *Messenger) handleBackedUpProfile(message *protobuf.BackedUpProfile, bac
idImages[i] = img idImages[i] = img
} }
err = m.multiAccounts.StoreIdentityImages(message.KeyUid, idImages, false) err = m.multiAccounts.StoreIdentityImages(message.KeyUid, idImages, false)
response.AddImages(idImages, err == nil) if err != nil {
return err
}
contentSet = true
response.AddImages(idImages)
} }
} }
if m.config.messengerSignalsHandler != nil { if m.config.messengerSignalsHandler != nil && contentSet {
m.config.messengerSignalsHandler.SendWakuBackedUpProfile(&response) m.config.messengerSignalsHandler.SendWakuBackedUpProfile(&response)
} }
@ -193,6 +151,7 @@ func (m *Messenger) handleBackedUpSettings(message *protobuf.SyncSetting) error
return nil return nil
} }
if settingField != nil {
response := wakusync.WakuBackedUpDataResponse{ response := wakusync.WakuBackedUpDataResponse{
Setting: settingField, Setting: settingField,
} }
@ -200,6 +159,7 @@ func (m *Messenger) handleBackedUpSettings(message *protobuf.SyncSetting) error
if m.config.messengerSignalsHandler != nil { if m.config.messengerSignalsHandler != nil {
m.config.messengerSignalsHandler.SendWakuBackedUpSettings(&response) m.config.messengerSignalsHandler.SendWakuBackedUpSettings(&response)
} }
}
return nil return nil
} }

View File

@ -122,9 +122,6 @@ func (s *MessengerBackupSuite) TestBackupContacts() {
"contacts not backed up", "contacts not backed up",
) )
s.Require().NoError(err) s.Require().NoError(err)
bob2.backedupMessagesHandler.postponeTasksWaitGroup.Wait()
s.Require().Len(bob2.AddedContacts(), 2) s.Require().Len(bob2.AddedContacts(), 2)
actualContacts = bob2.AddedContacts() actualContacts = bob2.AddedContacts()
@ -202,8 +199,6 @@ func (s *MessengerBackupSuite) TestBackupProfile() {
) )
s.Require().NoError(err) s.Require().NoError(err)
bob2.backedupMessagesHandler.postponeTasksWaitGroup.Wait()
// Check bob2 // Check bob2
storedBob2DisplayName, err = bob2.settings.DisplayName() storedBob2DisplayName, err = bob2.settings.DisplayName()
s.Require().NoError(err) s.Require().NoError(err)
@ -303,8 +298,6 @@ func (s *MessengerBackupSuite) TestBackupSettings() {
) )
s.Require().NoError(err) s.Require().NoError(err)
bob2.backedupMessagesHandler.postponeTasksWaitGroup.Wait()
// Check bob2 // Check bob2
storedBob2DisplayName, err = bob2.settings.DisplayName() storedBob2DisplayName, err = bob2.settings.DisplayName()
s.Require().NoError(err) s.Require().NoError(err)
@ -372,8 +365,6 @@ func (s *MessengerBackupSuite) TestBackupContactsGreaterThanBatch() {
) )
s.Require().NoError(err) s.Require().NoError(err)
bob2.backedupMessagesHandler.postponeTasksWaitGroup.Wait()
s.Require().Less(BackupContactsPerBatch*2, len(bob2.Contacts())) s.Require().Less(BackupContactsPerBatch*2, len(bob2.Contacts()))
s.Require().Len(bob2.AddedContacts(), BackupContactsPerBatch*2) s.Require().Len(bob2.AddedContacts(), BackupContactsPerBatch*2)
} }
@ -448,8 +439,6 @@ func (s *MessengerBackupSuite) TestBackupRemovedContact() {
// Bob 2 should remove the contact // Bob 2 should remove the contact
s.Require().NoError(err) s.Require().NoError(err)
bob2.backedupMessagesHandler.postponeTasksWaitGroup.Wait()
s.Require().Len(bob2.AddedContacts(), 1) s.Require().Len(bob2.AddedContacts(), 1)
s.Require().Equal(contactID1, bob2.AddedContacts()[0].ID) s.Require().Equal(contactID1, bob2.AddedContacts()[0].ID)
@ -502,8 +491,6 @@ func (s *MessengerBackupSuite) TestBackupLocalNickname() {
) )
s.Require().NoError(err) s.Require().NoError(err)
bob2.backedupMessagesHandler.postponeTasksWaitGroup.Wait()
for _, c := range bob2.Contacts() { for _, c := range bob2.Contacts() {
if c.ID == contactID1 { if c.ID == contactID1 {
actualContact = c actualContact = c
@ -557,8 +544,6 @@ func (s *MessengerBackupSuite) TestBackupBlockedContacts() {
) )
s.Require().NoError(err) s.Require().NoError(err)
bob2.backedupMessagesHandler.postponeTasksWaitGroup.Wait()
s.Require().Len(bob2.AddedContacts(), 1) s.Require().Len(bob2.AddedContacts(), 1)
actualContacts := bob2.AddedContacts() actualContacts := bob2.AddedContacts()
@ -633,8 +618,6 @@ func (s *MessengerBackupSuite) TestBackupCommunities() {
s.Require().NoError(err) s.Require().NoError(err)
bob2.backedupMessagesHandler.postponeTasksWaitGroup.Wait()
communities, err = bob2.JoinedCommunities() communities, err = bob2.JoinedCommunities()
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(communities, 1) s.Require().Len(communities, 1)

View File

@ -6,17 +6,13 @@ import (
type BackedUpProfile struct { type BackedUpProfile struct {
DisplayName string `json:"displayName,omitempty"` DisplayName string `json:"displayName,omitempty"`
DisplayNameStored bool `json:"displayNameStored,omitempty"`
Images []images.IdentityImage `json:"images,omitempty,omitempty"` Images []images.IdentityImage `json:"images,omitempty,omitempty"`
ImagesStored bool `json:"imagesStored,omitempty"`
} }
func (sfwr *WakuBackedUpDataResponse) AddDisplayName(displayName string, stored bool) { func (sfwr *WakuBackedUpDataResponse) AddDisplayName(displayName string) {
sfwr.Profile.DisplayName = displayName sfwr.Profile.DisplayName = displayName
sfwr.Profile.DisplayNameStored = stored
} }
func (sfwr *WakuBackedUpDataResponse) AddImages(images []images.IdentityImage, stored bool) { func (sfwr *WakuBackedUpDataResponse) AddImages(images []images.IdentityImage) {
sfwr.Profile.ImagesStored = stored
sfwr.Profile.Images = images sfwr.Profile.Images = images
} }