From bcdfc93764f2493f1eb029ca3bc881923a8fccbd Mon Sep 17 00:00:00 2001 From: William Grant Date: Fri, 7 Jun 2024 16:43:14 +1000 Subject: [PATCH] fix: revert swarmpolling to before merging unstable --- ts/session/apis/snode_api/swarmPolling.ts | 58 +++++++++++------------ 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/ts/session/apis/snode_api/swarmPolling.ts b/ts/session/apis/snode_api/swarmPolling.ts index a8f2ea4ee..910a6e913 100644 --- a/ts/session/apis/snode_api/swarmPolling.ts +++ b/ts/session/apis/snode_api/swarmPolling.ts @@ -1,17 +1,7 @@ /* eslint-disable no-await-in-loop */ /* eslint-disable more/no-then */ /* eslint-disable @typescript-eslint/no-misused-promises */ -import { - compact, - concat, - difference, - flatten, - isEmpty, - last, - sample, - toNumber, - uniqBy, -} from 'lodash'; +import { compact, concat, flatten, isEmpty, last, sample, toNumber, uniqBy } from 'lodash'; import { Data, Snode } from '../../../data/data'; import { SignalService } from '../../../protobuf'; import * as Receiver from '../../../receiver/receiver'; @@ -237,10 +227,16 @@ export class SwarmPolling { namespaces: Array ) { const polledPubkey = pubkey.key; - const toPollFrom = await this.getNodesToPollFrom(pubkey.key); - let resultsFromAllNamespaces: RetrieveMessagesResultsBatched | null; + + const swarmSnodes = await snodePool.getSwarmFor(polledPubkey); + let toPollFrom: Snode | undefined; try { + toPollFrom = sample(swarmSnodes); + + if (!toPollFrom) { + throw new Error(`pollOnceForKey: no snode in swarm for ${ed25519Str(polledPubkey)}`); + } // Note: always print something so we know if the polling is hanging window.log.info( `about to pollNodeForKey of ${ed25519Str(pubkey.key)} from snode: ${ed25519Str(toPollFrom.pubkey_ed25519)} namespaces: ${namespaces} ` @@ -570,22 +566,6 @@ export class SwarmPolling { } } - private async getNodesToPollFrom(polledPubkey: string) { - const swarmSnodes = await snodePool.getSwarmFor(polledPubkey); - - // Select nodes for which we already have lastHashes - const alreadyPolled = swarmSnodes.filter((n: Snode) => this.lastHashes[n.pubkey_ed25519]); - let toPollFrom = alreadyPolled.length ? alreadyPolled[0] : null; - - // If we need more nodes, select randomly from the remaining nodes: - if (!toPollFrom) { - const notPolled = difference(swarmSnodes, alreadyPolled); - toPollFrom = sample(notPolled) as Snode; - } - - return toPollFrom; - } - private loadGroupIds() { const convos = getConversationController().getConversations(); @@ -694,7 +674,15 @@ export class SwarmPolling { } const pubkey = UserUtils.getOurPubKeyFromCache(); - const toPollFrom = await this.getNodesToPollFrom(pubkey.key); + + const swarmSnodes = await snodePool.getSwarmFor(pubkey.key); + const toPollFrom = sample(swarmSnodes); + + if (!toPollFrom) { + throw new Error( + `[pollOnceForOurDisplayName] no snode in swarm for ${ed25519Str(pubkey.key)}` + ); + } if (abortSignal?.aborted) { throw new NotFoundError( @@ -702,6 +690,11 @@ export class SwarmPolling { ); } + // Note: always print something so we know if the polling is hanging + window.log.info( + `WIP: [onboarding] about to pollOnceForOurDisplayName of ${ed25519Str(pubkey.key)} from snode: ${ed25519Str(toPollFrom.pubkey_ed25519)} namespaces: ${[SnodeNamespaces.UserProfile]} ` + ); + const resultsFromUserProfile = await SnodeAPIRetrieve.retrieveNextMessages( toPollFrom, [''], @@ -711,6 +704,11 @@ export class SwarmPolling { null ); + // Note: always print something so we know if the polling is hanging + window.log.info( + `WIP: [onboarding] pollOnceForOurDisplayName of ${ed25519Str(pubkey.key)} from snode: ${ed25519Str(toPollFrom.pubkey_ed25519)} namespaces: ${[SnodeNamespaces.UserProfile]} returned: ${resultsFromUserProfile?.length}` + ); + // check if we just fetched the details from the config namespaces. // If yes, merge them together and exclude them from the rest of the messages. if (!resultsFromUserProfile?.length) {