From 35162e1a84b827537dcf492d6ad7899438672e93 Mon Sep 17 00:00:00 2001 From: Sam Holmes Date: Tue, 8 Jul 2025 17:55:07 -0700 Subject: [PATCH] Fix syncing races between periodic and change-server syncing This fixes the race between periodic syncing and change-server subscriptions by introducing a `usesChangeServer` flag on `EdgeCurrencyInfo` which tells the core to not invoke periodic syncing. In addition, we introduce a `resubscribing` state to the subscription status to differentiate initial `subscription` with intermittent subscription issues from the change server. --- CHANGELOG.md | 3 +++ src/core/currency/currency-pixie.ts | 18 +++++++++--------- .../currency/wallet/currency-wallet-pixie.ts | 6 +++--- .../currency/wallet/currency-wallet-reducer.ts | 1 + src/types/types.ts | 1 + 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ca9d27d..cd1290ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## Unreleased +- added: Added `usesChangeServer` flag to `EdgeCurrencyInfo` to optimize syncing for engines which subscribe to the change-server. +- fixed: Race condition between engine change-server subscription and periodic sync. + ## 2.32.1 (2025-07-18) - fixed: Don't write the enabled tokens file until after we've read it from disk. diff --git a/src/core/currency/currency-pixie.ts b/src/core/currency/currency-pixie.ts index 70141f5d..d53d2b98 100644 --- a/src/core/currency/currency-pixie.ts +++ b/src/core/currency/currency-pixie.ts @@ -217,7 +217,9 @@ export const currency: TamePixie = combinePixies({ if (changeService?.connected === true && changeServiceConnected) { const filteredWallets = supportedWallets.filter(([, wallet]) => wallet.changeServiceSubscriptions.some( - subscription => subscription.status === 'subscribing' + subscription => + subscription.status === 'subscribing' || + subscription.status === 'resubscribing' ) ) const indexToWalletId: Array<{ @@ -282,7 +284,7 @@ export const currency: TamePixie = combinePixies({ // subscribe to the address: case 0: // Try subscribing again later: - status = 'subscribing' + status = 'resubscribing' break // Change server does support this wallet plugin, and there are no // changes for the address: @@ -298,15 +300,13 @@ export const currency: TamePixie = combinePixies({ break } - // The status for the subscription is already set to subscribing, so - // we don't need to update it: - if (status === 'subscribing') { - continue - } - // Update the status for the subscription: const subscriptions = wallet.changeServiceSubscriptions - .filter(subscription => subscription.status === 'subscribing') + .filter( + subscription => + subscription.status === 'subscribing' || + subscription.status === 'resubscribing' + ) .map(subscription => ({ ...subscription, status diff --git a/src/core/currency/wallet/currency-wallet-pixie.ts b/src/core/currency/wallet/currency-wallet-pixie.ts index 7bccf4d4..640c2962 100644 --- a/src/core/currency/wallet/currency-wallet-pixie.ts +++ b/src/core/currency/wallet/currency-wallet-pixie.ts @@ -373,12 +373,12 @@ export const walletPixie: TamePixie = combinePixies({ !props.state.paused && !props.walletState.paused && props.walletState.engineStarted && - (props.walletState.changeServiceSubscriptions.length === 0 || + (props.walletState.currencyInfo.usesChangeServer !== true || props.walletState.changeServiceSubscriptions.some( subscription => subscription.status === 'avoiding' || - subscription.status === 'subscribing' || - subscription.status === 'reconnecting' + subscription.status === 'reconnecting' || + subscription.status === 'resubscribing' )) ? props : undefined diff --git a/src/core/currency/wallet/currency-wallet-reducer.ts b/src/core/currency/wallet/currency-wallet-reducer.ts index ca26097d..e7ea110d 100644 --- a/src/core/currency/wallet/currency-wallet-reducer.ts +++ b/src/core/currency/wallet/currency-wallet-reducer.ts @@ -106,6 +106,7 @@ export type ChangeServiceSubscriptionStatus = | 'listening' // The wallet is connected and listening for changes | 'reconnecting' // The wallet is reconnecting to the change service while its not available | 'subscribing' // The wallet is in the process of subscribing (supported) + | 'resubscribing' // The wallet is in the process of resubscribing due to a change-server issue (supported) | 'syncing' // The wallet is syncing historical data export interface CurrencyWalletNext { diff --git a/src/types/types.ts b/src/types/types.ts index 9175f371..93f69adf 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -505,6 +505,7 @@ export interface EdgeCurrencyInfo { unsafeBroadcastTx?: boolean unsafeMakeSpend?: boolean unsafeSyncNetwork?: boolean + usesChangeServer?: boolean /** @deprecated The default user settings are always `{}` */ defaultSettings?: JsonObject