From c8044bf400e7b3e1e6de72838c2b432a66d475eb Mon Sep 17 00:00:00 2001 From: Igor Sirotin Date: Sat, 9 Mar 2024 09:42:06 +0000 Subject: [PATCH] chore: disable curated communities loop in tests (#4894) --- protocol/common/code_control_flags.go | 11 +++++ protocol/common/feature_flags.go | 4 -- .../communities_messenger_helpers_test.go | 25 ---------- protocol/messenger.go | 6 ++- protocol/messenger_builder_test.go | 1 + protocol/messenger_config.go | 13 ++---- protocol/messenger_config_test.go | 46 +++++++++++++++++++ protocol/messenger_curated_communities.go | 8 +++- protocol/messenger_mailserver_cycle.go | 4 +- protocol/messenger_storenode_comunity_test.go | 1 + protocol/messenger_storenode_request_test.go | 3 +- 11 files changed, 77 insertions(+), 45 deletions(-) create mode 100644 protocol/common/code_control_flags.go create mode 100644 protocol/messenger_config_test.go diff --git a/protocol/common/code_control_flags.go b/protocol/common/code_control_flags.go new file mode 100644 index 000000000..4a99a59f3 --- /dev/null +++ b/protocol/common/code_control_flags.go @@ -0,0 +1,11 @@ +package common + +type CodeControlFlags struct { + // AutoRequestHistoricMessages indicates whether we should automatically request + // historic messages on getting online, connecting to store node, etc. + AutoRequestHistoricMessages bool + + // CuratedCommunitiesUpdateLoopEnabled indicates whether we should disable the curated communities update loop. + // Usually should be disabled in tests. + CuratedCommunitiesUpdateLoopEnabled bool +} diff --git a/protocol/common/feature_flags.go b/protocol/common/feature_flags.go index 907c4c690..daf4960a8 100644 --- a/protocol/common/feature_flags.go +++ b/protocol/common/feature_flags.go @@ -26,8 +26,4 @@ type FeatureFlags struct { // Peersyncing indicates whether we should advertise and sync messages with other peers Peersyncing bool - - // AutoRequestHistoricMessages indicates whether we should automatically request - // historic messages on getting online, connecting to store node, etc. - AutoRequestHistoricMessages bool } diff --git a/protocol/communities_messenger_helpers_test.go b/protocol/communities_messenger_helpers_test.go index 75ec1f660..1585e3e7e 100644 --- a/protocol/communities_messenger_helpers_test.go +++ b/protocol/communities_messenger_helpers_test.go @@ -14,7 +14,6 @@ import ( hexutil "github.com/ethereum/go-ethereum/common/hexutil" "github.com/status-im/status-go/account" - "github.com/status-im/status-go/appdatabase" "github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/multiaccounts/accounts" @@ -26,9 +25,7 @@ import ( "github.com/status-im/status-go/protocol/protobuf" "github.com/status-im/status-go/protocol/requests" "github.com/status-im/status-go/services/communitytokens" - mailserversDB "github.com/status-im/status-go/services/mailservers" walletToken "github.com/status-im/status-go/services/wallet/token" - "github.com/status-im/status-go/t/helpers" "github.com/status-im/status-go/transactions" ) @@ -590,25 +587,3 @@ func waitOnCommunitiesEvent(user *Messenger, condition func(*communities.Subscri return errCh } - -func WithTestStoreNode(s *suite.Suite, id string, address string, fleet string, collectiblesServiceMock *CollectiblesServiceMock) Option { - return func(c *config) error { - sqldb, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{}) - s.Require().NoError(err) - - db := mailserversDB.NewDB(sqldb) - err = db.Add(mailserversDB.Mailserver{ - ID: id, - Name: id, - Address: address, - Fleet: fleet, - }) - s.Require().NoError(err) - - c.mailserversDatabase = db - c.clusterConfig = params.ClusterConfig{Fleet: fleet} - c.communityTokensService = collectiblesServiceMock - - return nil - } -} diff --git a/protocol/messenger.go b/protocol/messenger.go index e3c7bdf32..4efe166b0 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -834,7 +834,9 @@ func (m *Messenger) Start() (*MessengerResponse, error) { m.startSyncSettingsLoop() m.startSettingsChangesLoop() m.startCommunityRekeyLoop() - m.startCuratedCommunitiesUpdateLoop() + if m.config.codeControlFlags.CuratedCommunitiesUpdateLoopEnabled { + m.startCuratedCommunitiesUpdateLoop() + } m.startMessageSegmentsCleanupLoop() if err := m.cleanTopics(); err != nil { @@ -960,7 +962,7 @@ func (m *Messenger) handleConnectionChange(online bool) { } // Start fetching messages from store nodes - if online && m.config.featureFlags.AutoRequestHistoricMessages { + if online && m.config.codeControlFlags.AutoRequestHistoricMessages { m.asyncRequestAllHistoricMessages() } diff --git a/protocol/messenger_builder_test.go b/protocol/messenger_builder_test.go index 08d665787..b1f3528f6 100644 --- a/protocol/messenger_builder_test.go +++ b/protocol/messenger_builder_test.go @@ -82,6 +82,7 @@ func newTestMessenger(waku types.Waku, config testMessengerConfig) (*Messenger, WithDatasync(), WithToplevelDatabaseMigrations(), WithBrowserDatabase(nil), + WithCuratedCommunitiesUpdateLoop(false), } options = append(options, config.extraOptions...) diff --git a/protocol/messenger_config.go b/protocol/messenger_config.go index 564a9cf8e..c4a6aeb4c 100644 --- a/protocol/messenger_config.go +++ b/protocol/messenger_config.go @@ -76,7 +76,8 @@ type config struct { // Config for the envelopes monitor envelopesMonitorConfig *transport.EnvelopesMonitorConfig - featureFlags common.FeatureFlags + featureFlags common.FeatureFlags + codeControlFlags common.CodeControlFlags appDb *sql.DB walletDb *sql.DB @@ -125,7 +126,8 @@ func messengerDefaultConfig() config { messageResendMaxCount: 3, } - c.featureFlags.AutoRequestHistoricMessages = true + c.codeControlFlags.AutoRequestHistoricMessages = true + c.codeControlFlags.CuratedCommunitiesUpdateLoopEnabled = true return c } @@ -409,10 +411,3 @@ func WithAccountManager(accountManager account.Manager) Option { return nil } } - -func WithAutoRequestHistoricMessages(enabled bool) Option { - return func(c *config) error { - c.featureFlags.AutoRequestHistoricMessages = enabled - return nil - } -} diff --git a/protocol/messenger_config_test.go b/protocol/messenger_config_test.go new file mode 100644 index 000000000..940b858eb --- /dev/null +++ b/protocol/messenger_config_test.go @@ -0,0 +1,46 @@ +package protocol + +import ( + "github.com/stretchr/testify/suite" + + "github.com/status-im/status-go/appdatabase" + "github.com/status-im/status-go/params" + "github.com/status-im/status-go/services/mailservers" + "github.com/status-im/status-go/t/helpers" +) + +func WithTestStoreNode(s *suite.Suite, id string, address string, fleet string, collectiblesServiceMock *CollectiblesServiceMock) Option { + return func(c *config) error { + sqldb, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{}) + s.Require().NoError(err) + + db := mailservers.NewDB(sqldb) + err = db.Add(mailservers.Mailserver{ + ID: id, + Name: id, + Address: address, + Fleet: fleet, + }) + s.Require().NoError(err) + + c.mailserversDatabase = db + c.clusterConfig = params.ClusterConfig{Fleet: fleet} + c.communityTokensService = collectiblesServiceMock + + return nil + } +} + +func WithAutoRequestHistoricMessages(enabled bool) Option { + return func(c *config) error { + c.codeControlFlags.AutoRequestHistoricMessages = enabled + return nil + } +} + +func WithCuratedCommunitiesUpdateLoop(enabled bool) Option { + return func(c *config) error { + c.codeControlFlags.CuratedCommunitiesUpdateLoopEnabled = enabled + return nil + } +} diff --git a/protocol/messenger_curated_communities.go b/protocol/messenger_curated_communities.go index 905cee51a..9d1f02089 100644 --- a/protocol/messenger_curated_communities.go +++ b/protocol/messenger_curated_communities.go @@ -20,7 +20,12 @@ const ( // Regularly gets list of curated communities and signals them to client func (m *Messenger) startCuratedCommunitiesUpdateLoop() { - logger := m.logger.Named("startCuratedCommunitiesUpdateLoop") + logger := m.logger.Named("curatedCommunitiesUpdateLoop") + + if m.contractMaker == nil { + logger.Warn("not starting curated communities loop: contract maker not initialized") + return + } go func() { // Initialize interval to 0 for immediate execution @@ -76,7 +81,6 @@ func (m *Messenger) startCuratedCommunitiesUpdateLoop() { func (m *Messenger) getCuratedCommunitiesFromContract() (*communities.CuratedCommunities, error) { if m.contractMaker == nil { - m.logger.Warn("contract maker not initialized") return nil, errors.New("contract maker not initialized") } diff --git a/protocol/messenger_mailserver_cycle.go b/protocol/messenger_mailserver_cycle.go index 6eef05b3e..2ea085ea8 100644 --- a/protocol/messenger_mailserver_cycle.go +++ b/protocol/messenger_mailserver_cycle.go @@ -409,7 +409,7 @@ func (m *Messenger) connectToMailserver(ms mailservers.Mailserver) error { signal.SendMailserverAvailable(m.mailserverCycle.activeMailserver.Address, m.mailserverCycle.activeMailserver.ID) // Query mailserver - if m.config.featureFlags.AutoRequestHistoricMessages { + if m.config.codeControlFlags.AutoRequestHistoricMessages { go func() { _, err := m.performMailserverRequest(&ms, func(_ mailservers.Mailserver) (*MessengerResponse, error) { return m.RequestAllHistoricMessages(false, false) @@ -564,7 +564,7 @@ func (m *Messenger) handleMailserverCycleEvent(connectedPeers []ConnectedPeer) e signal.SendMailserverAvailable(m.mailserverCycle.activeMailserver.Address, m.mailserverCycle.activeMailserver.ID) } // Query mailserver - if m.config.featureFlags.AutoRequestHistoricMessages { + if m.config.codeControlFlags.AutoRequestHistoricMessages { go func() { _, err := m.RequestAllHistoricMessages(false, true) if err != nil { diff --git a/protocol/messenger_storenode_comunity_test.go b/protocol/messenger_storenode_comunity_test.go index 1f2ef9756..5ce77f6e3 100644 --- a/protocol/messenger_storenode_comunity_test.go +++ b/protocol/messenger_storenode_comunity_test.go @@ -131,6 +131,7 @@ func (s *MessengerStoreNodeCommunitySuite) newMessenger(name, storenodeAddress s options := []Option{ WithAutoRequestHistoricMessages(false), + WithCuratedCommunitiesUpdateLoop(false), } if storenodeAddress != "" { diff --git a/protocol/messenger_storenode_request_test.go b/protocol/messenger_storenode_request_test.go index 7a4beaa30..55cc0e4b7 100644 --- a/protocol/messenger_storenode_request_test.go +++ b/protocol/messenger_storenode_request_test.go @@ -219,6 +219,7 @@ func (s *MessengerStoreNodeRequestSuite) newMessenger(shh types.Waku, logger *za options := []Option{ WithAutoRequestHistoricMessages(false), + WithCuratedCommunitiesUpdateLoop(false), } if mailserverAddress != "" { @@ -1233,7 +1234,7 @@ func (s *MessengerStoreNodeRequestSuite) TestFetchingHistoryWhenOnline() { { // Enable auto request historic messages, so that when bob goes online it will fetch historic messages // We don't enable it earlier to control when we connect to the store node. - s.bob.config.featureFlags.AutoRequestHistoricMessages = true + s.bob.config.codeControlFlags.AutoRequestHistoricMessages = true WaitForPeersConnected(&s.Suite, gethbridge.GetGethWakuV2From(s.bobWaku), func() []string { err := s.bob.DialPeer(storeAddress)