@metamask-previews/account-tree-controller 7.5.5-preview-68ba7f84e → 7.5.5-preview-5959fe813

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -142,9 +142,9 @@ class BackupAndSyncService {
142
142
  *
143
143
  * @returns A promise that resolves when the sync is complete.
144
144
  */
145
- async performFullSync() {
145
+ performFullSync() {
146
146
  if (!this.isBackupAndSyncEnabled) {
147
- return undefined;
147
+ return Promise.resolve(undefined);
148
148
  }
149
149
  // If there's an ongoing sync (including first sync), return it
150
150
  if (__classPrivateFieldGet(this, _BackupAndSyncService_ongoingFullSyncPromise, "f")) {
@@ -169,9 +169,9 @@ class BackupAndSyncService {
169
169
  *
170
170
  * @returns A promise that resolves when the sync is complete.
171
171
  */
172
- async performFullSyncAtLeastOnce() {
172
+ performFullSyncAtLeastOnce() {
173
173
  if (!this.isBackupAndSyncEnabled) {
174
- return undefined;
174
+ return Promise.resolve(undefined);
175
175
  }
176
176
  if (!__classPrivateFieldGet(this, _BackupAndSyncService_firstOngoingFullSyncPromise, "f")) {
177
177
  __classPrivateFieldSet(this, _BackupAndSyncService_firstOngoingFullSyncPromise, __classPrivateFieldGet(this, _BackupAndSyncService_atomicSyncQueue, "f").clearAndEnqueue(() => __classPrivateFieldGet(this, _BackupAndSyncService_instances, "m", _BackupAndSyncService_performFullSyncInner).call(this)), "f");
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uDAA0D;AAG1D,gDAAsD;AAGtD,qDAAkD;AAElD,0DAA0D;AAC1D,mDAM6B;AAM7B,wDAKkC;AAClC,iDAM2B;AAE3B,kEAAyD;AAEzD;;;;;;;;GAQG;AACH,MAAa,oBAAoB;IAoB/B,YAAY,OAA6B;;QAnBhC,gDAA+B;QAExC;;WAEG;QACM,wDAAkC;QAE3C;;;WAGG;QACH,uDAAg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AAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type { UserStorageController } from '@metamask/profile-sync-controller';\n\nimport { backupAndSyncLogger } from '../../logger.js';\nimport type { AccountTreeControllerState } from '../../types.js';\nimport type { AccountWalletEntropyObject } from '../../wallet.js';\nimport { TraceName } from '../analytics/index.js';\nimport type { ProfileId } from '../authentication/index.js';\nimport { getProfileId } from '../authentication/index.js';\nimport {\n createLocalGroupsFromUserStorage,\n performLegacyAccountSyncing,\n syncGroupsMetadata,\n syncGroupMetadata,\n syncWalletMetadata,\n} from '../syncing/index.js';\nimport type {\n BackupAndSyncContext,\n UserStorageSyncedWallet,\n UserStorageSyncedWalletGroup,\n} from '../types.js';\nimport {\n getAllGroupsFromUserStorage,\n getGroupFromUserStorage,\n getWalletFromUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage/index.js';\nimport {\n createStateSnapshot,\n restoreStateFromSnapshot,\n getLocalEntropyWallets,\n getLocalGroupsForEntropyWallet,\n toErrorMessage,\n} from '../utils/index.js';\nimport type { StateSnapshot } from '../utils/index.js';\nimport { AtomicSyncQueue } from './atomic-sync-queue.js';\n\n/**\n * Service responsible for managing all backup and sync operations.\n *\n * This service handles:\n * - Full sync operations\n * - Single item sync operations\n * - Sync queue management\n * - Sync state management\n */\nexport class BackupAndSyncService {\n readonly #context: BackupAndSyncContext;\n\n /**\n * Queue manager for atomic sync operations.\n */\n readonly #atomicSyncQueue: AtomicSyncQueue;\n\n /**\n * Cached promise for ongoing full sync operations.\n * Ensures multiple callers await the same sync operation.\n */\n #ongoingFullSyncPromise: Promise<void> | null = null;\n\n /**\n * Cached promise for the first ongoing full sync operation.\n * Ensures multiple callers await the same sync operation.\n */\n #firstOngoingFullSyncPromise: Promise<void> | null = null;\n\n constructor(context: BackupAndSyncContext) {\n this.#context = context;\n this.#atomicSyncQueue = new AtomicSyncQueue();\n }\n\n /**\n * Checks if syncing is currently in progress.\n *\n * @returns True if syncing is in progress.\n */\n get isInProgress(): boolean {\n return this.#context.controller.state.isAccountTreeSyncingInProgress;\n }\n\n /**\n * Checks if the account tree has been synced at least once.\n *\n * @returns True if the account tree has been synced at least once.\n */\n get hasSyncedAtLeastOnce(): boolean {\n return this.#context.controller.state\n .hasAccountTreeSyncingSyncedAtLeastOnce;\n }\n\n /**\n * Checks if backup and sync is enabled by checking UserStorageController state.\n *\n * @returns True if backup and sync + account syncing is enabled.\n */\n get isBackupAndSyncEnabled(): boolean {\n const userStorageControllerState = this.#context.messenger.call(\n 'UserStorageController:getState',\n );\n const { isAccountSyncingEnabled, isBackupAndSyncEnabled } =\n userStorageControllerState;\n\n return isBackupAndSyncEnabled && isAccountSyncingEnabled;\n }\n\n /**\n * Clears the atomic queue and resets ongoing operations.\n */\n clearState(): void {\n this.#atomicSyncQueue.clear();\n this.#ongoingFullSyncPromise = null;\n this.#firstOngoingFullSyncPromise = null;\n }\n\n /**\n * Handles changes to the user storage state.\n * Used to clear the backup and sync service state.\n *\n * @param state - The new user storage state.\n */\n handleUserStorageStateChange(\n state: UserStorageController.UserStorageControllerState,\n ): void {\n if (!state.isAccountSyncingEnabled || !state.isBackupAndSyncEnabled) {\n // If either syncing is disabled, clear the account tree state\n this.clearState();\n }\n }\n\n /**\n * Gets the entropy wallet associated with the given wallet ID.\n *\n * @param walletId - The wallet ID to look up.\n * @returns The associated entropy wallet, or undefined if not found.\n */\n #getEntropyWallet(\n walletId: AccountWalletId,\n ): AccountWalletEntropyObject | undefined {\n const wallet = this.#context.controller.state.accountTree.wallets[walletId];\n return wallet?.type === AccountWalletType.Entropy ? wallet : undefined;\n }\n\n /**\n * Sets up cleanup for ongoing sync promise tracking without affecting error propagation.\n *\n * @param promise - The promise to track and clean up\n * @returns The same promise (for chaining)\n */\n #setupOngoingPromiseCleanup(promise: Promise<void>): Promise<void> {\n this.#ongoingFullSyncPromise = promise;\n // Set up cleanup without affecting the returned promise\n promise\n .finally(() => {\n this.#ongoingFullSyncPromise = null;\n })\n .catch(() => {\n // Only ignore errors from the cleanup operation itself\n // The original promise errors are still propagated to callers\n });\n return promise;\n }\n\n /**\n * Enqueues a single wallet sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param walletId - The wallet ID to sync.\n */\n enqueueSingleWalletSync(walletId: AccountWalletId): void {\n if (!this.isBackupAndSyncEnabled || !this.hasSyncedAtLeastOnce) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleWalletSyncInner(walletId),\n );\n }\n\n /**\n * Enqueues a single group sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param groupId - The group ID to sync.\n */\n enqueueSingleGroupSync(groupId: AccountGroupId): void {\n if (\n !this.isBackupAndSyncEnabled ||\n !this.hasSyncedAtLeastOnce ||\n // This prevents rate limiting scenarios where full syncs trigger group creations\n // that in turn enqueue the same single group syncs that the full sync just did.\n // This can very rarely lead to inconsistencies, but will be fixed on the next full sync.\n // TODO: let's improve this in the future by tracking the updates done in the full sync and\n // comparing against that.\n this.isInProgress\n ) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleGroupSyncInner(groupId),\n );\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n * If a full sync is already in progress, it will return the ongoing promise.\n * This clears the atomic sync queue before starting the full sync.\n *\n * NOTE: in some very edge cases, this can be ran concurrently if triggered quickly after\n * toggling back and forth the backup and sync feature from the UI.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSync(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n // If there's an ongoing sync (including first sync), return it\n if (this.#ongoingFullSyncPromise) {\n return this.#ongoingFullSyncPromise;\n }\n\n // Create a new ongoing sync (sequential calls after previous completed)\n const newSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(() =>\n this.#performFullSyncInner(),\n );\n\n // First sync setup - create and cache the first sync promise\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = newSyncPromise;\n }\n\n return this.#setupOngoingPromiseCleanup(newSyncPromise);\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * If the first ever full sync is already in progress, it will return the ongoing promise.\n * If the first ever full sync has already completed, it will resolve and NOT start a new sync.\n *\n * This clears the atomic sync queue before starting the full sync.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSyncAtLeastOnce(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(\n () => this.#performFullSyncInner(),\n );\n // eslint-disable-next-line no-void\n void this.#setupOngoingPromiseCleanup(this.#firstOngoingFullSyncPromise);\n }\n\n return this.#firstOngoingFullSyncPromise;\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * This method performs a comprehensive sync operation that:\n * 1. Identifies all local entropy wallets that can be synchronized\n * 2. Performs legacy account syncing if needed (for backwards compatibility)\n * - Disables subsequent legacy syncing by setting a flag in user storage\n * - Exits early if multichain account syncing is disabled after legacy sync\n * 3. Executes multichain account syncing for each wallet:\n * - Syncs wallet metadata bidirectionally\n * - Creates missing local groups from user storage data (or pushes local groups if none exist remotely)\n * - Refreshes local state to reflect newly created groups\n * - Syncs group metadata bidirectionally\n *\n * The sync is atomic per wallet with rollback on errors, but continues processing other wallets\n * if individual wallet sync fails. A global lock prevents concurrent sync operations.\n *\n * During this process, all other atomic multichain related user storage updates are blocked.\n *\n * @throws Will throw if the sync operation encounters unrecoverable errors\n */\n async #performFullSyncInner(): Promise<void> {\n // Prevent multiple syncs from running at the same time.\n // Also prevents atomic updates from being applied while syncing is in progress.\n if (this.isInProgress) {\n return;\n }\n\n // Set isAccountTreeSyncingInProgress immediately to prevent race conditions\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = true;\n },\n );\n\n // Encapsulate the sync logic in a function to allow tracing\n const bigSyncFn = async () => {\n try {\n // 1. Identifies all local entropy wallets that can be synchronized\n const localSyncableWallets = getLocalEntropyWallets(this.#context);\n\n if (!localSyncableWallets.length) {\n // No wallets to sync, just return. This shouldn't happen.\n return;\n }\n\n // 2. Iterate over each local wallet\n for (const wallet of localSyncableWallets) {\n const entropySourceId = wallet.metadata.entropy.id;\n\n let walletProfileId: ProfileId;\n let walletFromUserStorage: UserStorageSyncedWallet | null;\n let groupsFromUserStorage: UserStorageSyncedWalletGroup[];\n\n try {\n walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n [walletFromUserStorage, groupsFromUserStorage] = await Promise.all([\n getWalletFromUserStorage(this.#context, entropySourceId),\n getAllGroupsFromUserStorage(this.#context, entropySourceId),\n ]);\n\n // 2.1 Decide if we need to perform legacy account syncing\n if (\n !walletFromUserStorage ||\n !walletFromUserStorage.isLegacyAccountSyncingDisabled\n ) {\n // 2.2 Perform legacy account syncing\n // This will migrate legacy account data to the new structure.\n // This operation will only be performed once.\n await performLegacyAccountSyncing(\n this.#context,\n entropySourceId,\n walletProfileId,\n );\n }\n } catch (error) {\n const errorMessage = toErrorMessage(error);\n\n backupAndSyncLogger(\n `Legacy syncing failed for wallet ${wallet.id}: ${errorMessage}`,\n );\n throw new Error(\n `Legacy syncing failed for wallet: ${errorMessage}`,\n );\n }\n\n // 3. Execute multichain account syncing\n let stateSnapshot: StateSnapshot | undefined;\n\n // Capture the local-write flag so it can be reverted together with\n // the state snapshot if this wallet is rolled back. Remote writes are\n // durable and intentionally excluded.\n const localWriteBeforeWallet =\n this.#context.mutationTracker?.getLocalWrite() ?? false;\n\n try {\n // 3.1 Wallet syncing\n // Create a state snapshot before processing each wallet for potential rollback\n stateSnapshot = createStateSnapshot(this.#context);\n\n // Sync wallet metadata bidirectionally\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n\n // 3.2 Groups syncing\n // If groups data does not exist in user storage yet, create it\n if (!groupsFromUserStorage.length) {\n // If no groups exist in user storage, we can push all groups from the wallet to the user storage and exit\n await pushGroupToUserStorageBatch(\n this.#context,\n getLocalGroupsForEntropyWallet(this.#context, wallet.id),\n entropySourceId,\n );\n\n continue; // No need to proceed with metadata comparison if groups are new\n }\n\n // Create local groups for each group from user storage if they do not exist\n // This will ensure that we have all groups available locally before syncing metadata\n await createLocalGroupsFromUserStorage(\n this.#context,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n\n // Sync group metadata bidirectionally\n await syncGroupsMetadata(\n this.#context,\n wallet,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n const errorMessage = toErrorMessage(error);\n const errorString = `Error during multichain account syncing for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n\n // Attempt to rollback state changes for this wallet\n try {\n if (!stateSnapshot) {\n throw new Error(\n `State snapshot is missing for wallet ${wallet.id}`,\n );\n }\n restoreStateFromSnapshot(this.#context, stateSnapshot);\n // Revert the local-write flag too, so a rolled-back wallet does\n // not keep the run marked as having changed local state.\n this.#context.mutationTracker?.setLocalWrite(\n localWriteBeforeWallet,\n );\n backupAndSyncLogger(\n `Rolled back state changes for wallet ${wallet.id}`,\n );\n } catch (rollbackError) {\n backupAndSyncLogger(\n `Failed to rollback state for wallet ${wallet.id}:`,\n rollbackError instanceof Error\n ? rollbackError.message\n : String(rollbackError),\n );\n }\n\n // Continue with next wallet instead of failing the entire sync\n continue;\n }\n }\n } catch (error) {\n backupAndSyncLogger('Error during multichain account syncing:', error);\n throw error;\n }\n\n this.#context.controllerStateUpdateFn((state) => {\n state.hasAccountTreeSyncingSyncedAtLeastOnce = true;\n });\n };\n\n // Execute the big sync function and ensure state cleanup.\n // The sync runs untraced so we can decide afterwards whether it did any\n // real work; the span is then backdated to preserve the real duration.\n const { mutationTracker } = this.#context;\n mutationTracker?.reset();\n const startTime = Date.now();\n try {\n await bigSyncFn();\n } finally {\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = false;\n },\n );\n\n if (mutationTracker?.hasOccurred()) {\n try {\n await this.#context.traceFn(\n {\n name: TraceName.AccountSyncFull,\n startTime,\n },\n () => undefined,\n );\n } catch (traceError) {\n backupAndSyncLogger(\n 'Failed to emit AccountSyncFull trace:',\n traceError,\n );\n }\n }\n }\n }\n\n /**\n * Performs a single wallet's bidirectional metadata sync with user storage.\n *\n * @param walletId - The wallet ID to sync.\n */\n async #performSingleWalletSyncInner(\n walletId: AccountWalletId,\n ): Promise<void> {\n try {\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n const walletFromUserStorage = await getWalletFromUserStorage(\n this.#context,\n entropySourceId,\n );\n\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(\n `Error in single wallet sync for ${walletId}:`,\n error,\n );\n throw error;\n }\n }\n\n /**\n * Performs a single group's bidirectional metadata sync with user storage.\n *\n * @param groupId - The group ID to sync.\n */\n async #performSingleGroupSyncInner(groupId: AccountGroupId): Promise<void> {\n try {\n const walletId = this.#context.groupIdToWalletId.get(groupId);\n if (!walletId) {\n return;\n }\n\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const group = wallet.groups[groupId];\n if (!group) {\n return;\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n // Get the specific group from user storage\n const groupFromUserStorage = await getGroupFromUserStorage(\n this.#context,\n entropySourceId,\n group.metadata.entropy.groupIndex,\n );\n\n await syncGroupMetadata(\n this.#context,\n group,\n groupFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(`Error in single group sync for ${groupId}:`, error);\n throw error;\n }\n }\n}\n"]}
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,uDAA0D;AAG1D,gDAAsD;AAGtD,qDAAkD;AAElD,0DAA0D;AAC1D,mDAM6B;AAM7B,wDAKkC;AAClC,iDAM2B;AAE3B,kEAAyD;AAEzD;;;;;;;;GAQG;AACH,MAAa,oBAAoB;IAoB/B,YAAY,OAA6B;;QAnBhC,gDAA+B;QAExC;;WAEG;QACM,wDAAkC;QAE3C;;;WAGG;QACH,uDAAgD,IAAI,EAAC;QAErD;;;WAGG;QACH,4DAAqD,IAAI,EAAC;QAGxD,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;QACxB,uBAAA,IAAI,yCAAoB,IAAI,sCAAe,EAAE,MAAA,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACH,IAAI,YAAY;QACd,OAAO,uBAAA,IAAI,qCAAS,CAAC,UAAU,CAAC,KAAK,CAAC,8BAA8B,CAAC;IACvE,CAAC;IAED;;;;OAIG;IACH,IAAI,oBAAoB;QACtB,OAAO,uBAAA,IAAI,qCAAS,CAAC,UAAU,CAAC,KAAK;aAClC,sCAAsC,CAAC;IAC5C,CAAC;IAED;;;;OAIG;IACH,IAAI,sBAAsB;QACxB,MAAM,0BAA0B,GAAG,uBAAA,IAAI,qCAAS,CAAC,SAAS,CAAC,IAAI,CAC7D,gCAAgC,CACjC,CAAC;QACF,MAAM,EAAE,uBAAuB,EAAE,sBAAsB,EAAE,GACvD,0BAA0B,CAAC;QAE7B,OAAO,sBAAsB,IAAI,uBAAu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CAAC;QACrC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO;QACT,CAAC;QAED,MAAM,eAAe,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,MAAM,eAAe,GAAG,MAAM,IAAA,uBAAY,EACxC,uBAAA,IAAI,qCAAS,EACb,eAAe,CAChB,CAAC;QAEF,2CAA2C;QAC3C,MAAM,oBAAoB,GAAG,MAAM,IAAA,kCAAuB,EACxD,uBAAA,IAAI,qCAAS,EACb,eAAe,EACf,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAClC,CAAC;QAEF,MAAM,IAAA,4BAAiB,EACrB,uBAAA,IAAI,qCAAS,EACb,KAAK,EACL,oBAAoB,EACpB,eAAe,EACf,eAAe,CAChB,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,IAAA,+BAAmB,EAAC,kCAAkC,OAAO,GAAG,EAAE,KAAK,CAAC,CAAC;QACzE,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type { UserStorageController } from '@metamask/profile-sync-controller';\n\nimport { backupAndSyncLogger } from '../../logger.js';\nimport type { AccountTreeControllerState } from '../../types.js';\nimport type { AccountWalletEntropyObject } from '../../wallet.js';\nimport { TraceName } from '../analytics/index.js';\nimport type { ProfileId } from '../authentication/index.js';\nimport { getProfileId } from '../authentication/index.js';\nimport {\n createLocalGroupsFromUserStorage,\n performLegacyAccountSyncing,\n syncGroupsMetadata,\n syncGroupMetadata,\n syncWalletMetadata,\n} from '../syncing/index.js';\nimport type {\n BackupAndSyncContext,\n UserStorageSyncedWallet,\n UserStorageSyncedWalletGroup,\n} from '../types.js';\nimport {\n getAllGroupsFromUserStorage,\n getGroupFromUserStorage,\n getWalletFromUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage/index.js';\nimport {\n createStateSnapshot,\n restoreStateFromSnapshot,\n getLocalEntropyWallets,\n getLocalGroupsForEntropyWallet,\n toErrorMessage,\n} from '../utils/index.js';\nimport type { StateSnapshot } from '../utils/index.js';\nimport { AtomicSyncQueue } from './atomic-sync-queue.js';\n\n/**\n * Service responsible for managing all backup and sync operations.\n *\n * This service handles:\n * - Full sync operations\n * - Single item sync operations\n * - Sync queue management\n * - Sync state management\n */\nexport class BackupAndSyncService {\n readonly #context: BackupAndSyncContext;\n\n /**\n * Queue manager for atomic sync operations.\n */\n readonly #atomicSyncQueue: AtomicSyncQueue;\n\n /**\n * Cached promise for ongoing full sync operations.\n * Ensures multiple callers await the same sync operation.\n */\n #ongoingFullSyncPromise: Promise<void> | null = null;\n\n /**\n * Cached promise for the first ongoing full sync operation.\n * Ensures multiple callers await the same sync operation.\n */\n #firstOngoingFullSyncPromise: Promise<void> | null = null;\n\n constructor(context: BackupAndSyncContext) {\n this.#context = context;\n this.#atomicSyncQueue = new AtomicSyncQueue();\n }\n\n /**\n * Checks if syncing is currently in progress.\n *\n * @returns True if syncing is in progress.\n */\n get isInProgress(): boolean {\n return this.#context.controller.state.isAccountTreeSyncingInProgress;\n }\n\n /**\n * Checks if the account tree has been synced at least once.\n *\n * @returns True if the account tree has been synced at least once.\n */\n get hasSyncedAtLeastOnce(): boolean {\n return this.#context.controller.state\n .hasAccountTreeSyncingSyncedAtLeastOnce;\n }\n\n /**\n * Checks if backup and sync is enabled by checking UserStorageController state.\n *\n * @returns True if backup and sync + account syncing is enabled.\n */\n get isBackupAndSyncEnabled(): boolean {\n const userStorageControllerState = this.#context.messenger.call(\n 'UserStorageController:getState',\n );\n const { isAccountSyncingEnabled, isBackupAndSyncEnabled } =\n userStorageControllerState;\n\n return isBackupAndSyncEnabled && isAccountSyncingEnabled;\n }\n\n /**\n * Clears the atomic queue and resets ongoing operations.\n */\n clearState(): void {\n this.#atomicSyncQueue.clear();\n this.#ongoingFullSyncPromise = null;\n this.#firstOngoingFullSyncPromise = null;\n }\n\n /**\n * Handles changes to the user storage state.\n * Used to clear the backup and sync service state.\n *\n * @param state - The new user storage state.\n */\n handleUserStorageStateChange(\n state: UserStorageController.UserStorageControllerState,\n ): void {\n if (!state.isAccountSyncingEnabled || !state.isBackupAndSyncEnabled) {\n // If either syncing is disabled, clear the account tree state\n this.clearState();\n }\n }\n\n /**\n * Gets the entropy wallet associated with the given wallet ID.\n *\n * @param walletId - The wallet ID to look up.\n * @returns The associated entropy wallet, or undefined if not found.\n */\n #getEntropyWallet(\n walletId: AccountWalletId,\n ): AccountWalletEntropyObject | undefined {\n const wallet = this.#context.controller.state.accountTree.wallets[walletId];\n return wallet?.type === AccountWalletType.Entropy ? wallet : undefined;\n }\n\n /**\n * Sets up cleanup for ongoing sync promise tracking without affecting error propagation.\n *\n * @param promise - The promise to track and clean up\n * @returns The same promise (for chaining)\n */\n #setupOngoingPromiseCleanup(promise: Promise<void>): Promise<void> {\n this.#ongoingFullSyncPromise = promise;\n // Set up cleanup without affecting the returned promise\n promise\n .finally(() => {\n this.#ongoingFullSyncPromise = null;\n })\n .catch(() => {\n // Only ignore errors from the cleanup operation itself\n // The original promise errors are still propagated to callers\n });\n return promise;\n }\n\n /**\n * Enqueues a single wallet sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param walletId - The wallet ID to sync.\n */\n enqueueSingleWalletSync(walletId: AccountWalletId): void {\n if (!this.isBackupAndSyncEnabled || !this.hasSyncedAtLeastOnce) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleWalletSyncInner(walletId),\n );\n }\n\n /**\n * Enqueues a single group sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param groupId - The group ID to sync.\n */\n enqueueSingleGroupSync(groupId: AccountGroupId): void {\n if (\n !this.isBackupAndSyncEnabled ||\n !this.hasSyncedAtLeastOnce ||\n // This prevents rate limiting scenarios where full syncs trigger group creations\n // that in turn enqueue the same single group syncs that the full sync just did.\n // This can very rarely lead to inconsistencies, but will be fixed on the next full sync.\n // TODO: let's improve this in the future by tracking the updates done in the full sync and\n // comparing against that.\n this.isInProgress\n ) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleGroupSyncInner(groupId),\n );\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n * If a full sync is already in progress, it will return the ongoing promise.\n * This clears the atomic sync queue before starting the full sync.\n *\n * NOTE: in some very edge cases, this can be ran concurrently if triggered quickly after\n * toggling back and forth the backup and sync feature from the UI.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n performFullSync(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return Promise.resolve(undefined);\n }\n\n // If there's an ongoing sync (including first sync), return it\n if (this.#ongoingFullSyncPromise) {\n return this.#ongoingFullSyncPromise;\n }\n\n // Create a new ongoing sync (sequential calls after previous completed)\n const newSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(() =>\n this.#performFullSyncInner(),\n );\n\n // First sync setup - create and cache the first sync promise\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = newSyncPromise;\n }\n\n return this.#setupOngoingPromiseCleanup(newSyncPromise);\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * If the first ever full sync is already in progress, it will return the ongoing promise.\n * If the first ever full sync has already completed, it will resolve and NOT start a new sync.\n *\n * This clears the atomic sync queue before starting the full sync.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n performFullSyncAtLeastOnce(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return Promise.resolve(undefined);\n }\n\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(\n () => this.#performFullSyncInner(),\n );\n // eslint-disable-next-line no-void\n void this.#setupOngoingPromiseCleanup(this.#firstOngoingFullSyncPromise);\n }\n\n return this.#firstOngoingFullSyncPromise;\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * This method performs a comprehensive sync operation that:\n * 1. Identifies all local entropy wallets that can be synchronized\n * 2. Performs legacy account syncing if needed (for backwards compatibility)\n * - Disables subsequent legacy syncing by setting a flag in user storage\n * - Exits early if multichain account syncing is disabled after legacy sync\n * 3. Executes multichain account syncing for each wallet:\n * - Syncs wallet metadata bidirectionally\n * - Creates missing local groups from user storage data (or pushes local groups if none exist remotely)\n * - Refreshes local state to reflect newly created groups\n * - Syncs group metadata bidirectionally\n *\n * The sync is atomic per wallet with rollback on errors, but continues processing other wallets\n * if individual wallet sync fails. A global lock prevents concurrent sync operations.\n *\n * During this process, all other atomic multichain related user storage updates are blocked.\n *\n * @throws Will throw if the sync operation encounters unrecoverable errors\n */\n async #performFullSyncInner(): Promise<void> {\n // Prevent multiple syncs from running at the same time.\n // Also prevents atomic updates from being applied while syncing is in progress.\n if (this.isInProgress) {\n return;\n }\n\n // Set isAccountTreeSyncingInProgress immediately to prevent race conditions\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = true;\n },\n );\n\n // Encapsulate the sync logic in a function to allow tracing\n const bigSyncFn = async () => {\n try {\n // 1. Identifies all local entropy wallets that can be synchronized\n const localSyncableWallets = getLocalEntropyWallets(this.#context);\n\n if (!localSyncableWallets.length) {\n // No wallets to sync, just return. This shouldn't happen.\n return;\n }\n\n // 2. Iterate over each local wallet\n for (const wallet of localSyncableWallets) {\n const entropySourceId = wallet.metadata.entropy.id;\n\n let walletProfileId: ProfileId;\n let walletFromUserStorage: UserStorageSyncedWallet | null;\n let groupsFromUserStorage: UserStorageSyncedWalletGroup[];\n\n try {\n walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n [walletFromUserStorage, groupsFromUserStorage] = await Promise.all([\n getWalletFromUserStorage(this.#context, entropySourceId),\n getAllGroupsFromUserStorage(this.#context, entropySourceId),\n ]);\n\n // 2.1 Decide if we need to perform legacy account syncing\n if (\n !walletFromUserStorage ||\n !walletFromUserStorage.isLegacyAccountSyncingDisabled\n ) {\n // 2.2 Perform legacy account syncing\n // This will migrate legacy account data to the new structure.\n // This operation will only be performed once.\n await performLegacyAccountSyncing(\n this.#context,\n entropySourceId,\n walletProfileId,\n );\n }\n } catch (error) {\n const errorMessage = toErrorMessage(error);\n\n backupAndSyncLogger(\n `Legacy syncing failed for wallet ${wallet.id}: ${errorMessage}`,\n );\n throw new Error(\n `Legacy syncing failed for wallet: ${errorMessage}`,\n );\n }\n\n // 3. Execute multichain account syncing\n let stateSnapshot: StateSnapshot | undefined;\n\n // Capture the local-write flag so it can be reverted together with\n // the state snapshot if this wallet is rolled back. Remote writes are\n // durable and intentionally excluded.\n const localWriteBeforeWallet =\n this.#context.mutationTracker?.getLocalWrite() ?? false;\n\n try {\n // 3.1 Wallet syncing\n // Create a state snapshot before processing each wallet for potential rollback\n stateSnapshot = createStateSnapshot(this.#context);\n\n // Sync wallet metadata bidirectionally\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n\n // 3.2 Groups syncing\n // If groups data does not exist in user storage yet, create it\n if (!groupsFromUserStorage.length) {\n // If no groups exist in user storage, we can push all groups from the wallet to the user storage and exit\n await pushGroupToUserStorageBatch(\n this.#context,\n getLocalGroupsForEntropyWallet(this.#context, wallet.id),\n entropySourceId,\n );\n\n continue; // No need to proceed with metadata comparison if groups are new\n }\n\n // Create local groups for each group from user storage if they do not exist\n // This will ensure that we have all groups available locally before syncing metadata\n await createLocalGroupsFromUserStorage(\n this.#context,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n\n // Sync group metadata bidirectionally\n await syncGroupsMetadata(\n this.#context,\n wallet,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n const errorMessage = toErrorMessage(error);\n const errorString = `Error during multichain account syncing for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n\n // Attempt to rollback state changes for this wallet\n try {\n if (!stateSnapshot) {\n throw new Error(\n `State snapshot is missing for wallet ${wallet.id}`,\n );\n }\n restoreStateFromSnapshot(this.#context, stateSnapshot);\n // Revert the local-write flag too, so a rolled-back wallet does\n // not keep the run marked as having changed local state.\n this.#context.mutationTracker?.setLocalWrite(\n localWriteBeforeWallet,\n );\n backupAndSyncLogger(\n `Rolled back state changes for wallet ${wallet.id}`,\n );\n } catch (rollbackError) {\n backupAndSyncLogger(\n `Failed to rollback state for wallet ${wallet.id}:`,\n rollbackError instanceof Error\n ? rollbackError.message\n : String(rollbackError),\n );\n }\n\n // Continue with next wallet instead of failing the entire sync\n continue;\n }\n }\n } catch (error) {\n backupAndSyncLogger('Error during multichain account syncing:', error);\n throw error;\n }\n\n this.#context.controllerStateUpdateFn((state) => {\n state.hasAccountTreeSyncingSyncedAtLeastOnce = true;\n });\n };\n\n // Execute the big sync function and ensure state cleanup.\n // The sync runs untraced so we can decide afterwards whether it did any\n // real work; the span is then backdated to preserve the real duration.\n const { mutationTracker } = this.#context;\n mutationTracker?.reset();\n const startTime = Date.now();\n try {\n await bigSyncFn();\n } finally {\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = false;\n },\n );\n\n if (mutationTracker?.hasOccurred()) {\n try {\n await this.#context.traceFn(\n {\n name: TraceName.AccountSyncFull,\n startTime,\n },\n () => undefined,\n );\n } catch (traceError) {\n backupAndSyncLogger(\n 'Failed to emit AccountSyncFull trace:',\n traceError,\n );\n }\n }\n }\n }\n\n /**\n * Performs a single wallet's bidirectional metadata sync with user storage.\n *\n * @param walletId - The wallet ID to sync.\n */\n async #performSingleWalletSyncInner(\n walletId: AccountWalletId,\n ): Promise<void> {\n try {\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n const walletFromUserStorage = await getWalletFromUserStorage(\n this.#context,\n entropySourceId,\n );\n\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(\n `Error in single wallet sync for ${walletId}:`,\n error,\n );\n throw error;\n }\n }\n\n /**\n * Performs a single group's bidirectional metadata sync with user storage.\n *\n * @param groupId - The group ID to sync.\n */\n async #performSingleGroupSyncInner(groupId: AccountGroupId): Promise<void> {\n try {\n const walletId = this.#context.groupIdToWalletId.get(groupId);\n if (!walletId) {\n return;\n }\n\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const group = wallet.groups[groupId];\n if (!group) {\n return;\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n // Get the specific group from user storage\n const groupFromUserStorage = await getGroupFromUserStorage(\n this.#context,\n entropySourceId,\n group.metadata.entropy.groupIndex,\n );\n\n await syncGroupMetadata(\n this.#context,\n group,\n groupFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(`Error in single group sync for ${groupId}:`, error);\n throw error;\n }\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,8BAA8B;AAE7E,OAAO,KAAK,EAAE,qBAAqB,EAAE,0CAA0C;AAe/E,OAAO,KAAK,EACV,oBAAoB,EAGrB,qBAAoB;AAiBrB;;;;;;;;GAQG;AACH,qBAAa,oBAAoB;;gBAoBnB,OAAO,EAAE,oBAAoB;IAKzC;;;;OAIG;IACH,IAAI,YAAY,IAAI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GAAG,IAAI;IAoBrD;;;;;;;;;;OAUG;IACH,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAuBhC;;;;;;;;;;OAUG;IACH,0BAA0B,IAAI,OAAO,CAAC,IAAI,CAAC;CAoU5C"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,8BAA8B;AAE7E,OAAO,KAAK,EAAE,qBAAqB,EAAE,0CAA0C;AAe/E,OAAO,KAAK,EACV,oBAAoB,EAGrB,qBAAoB;AAiBrB;;;;;;;;GAQG;AACH,qBAAa,oBAAoB;;gBAoBnB,OAAO,EAAE,oBAAoB;IAKzC;;;;OAIG;IACH,IAAI,YAAY,IAAI,OAAO,CAE1B;IAED;;;;OAIG;IACH,IAAI,oBAAoB,IAAI,OAAO,CAGlC;IAED;;;;OAIG;IACH,IAAI,sBAAsB,IAAI,OAAO,CAQpC;IAED;;OAEG;IACH,UAAU,IAAI,IAAI;IAMlB;;;;;OAKG;IACH,4BAA4B,CAC1B,KAAK,EAAE,qBAAqB,CAAC,0BAA0B,GACtD,IAAI;IAwCP;;;;;OAKG;IACH,uBAAuB,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI;IAWxD;;;;;OAKG;IACH,sBAAsB,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI;IAoBrD;;;;;;;;;;OAUG;IACG,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAuBtC;;;;;;;;;;OAUG;IACG,0BAA0B,IAAI,OAAO,CAAC,IAAI,CAAC;CAoUlD"}
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,8BAA8B;AAE7E,OAAO,KAAK,EAAE,qBAAqB,EAAE,0CAA0C;AAe/E,OAAO,KAAK,EACV,oBAAoB,EAGrB,qBAAoB;AAiBrB;;;;;;;;GAQG;AACH,qBAAa,oBAAoB;;gBAoBnB,OAAO,EAAE,oBAAoB;IAKzC;;;;OAIG;IACH,IAAI,YAAY,IAAI,OAAO,CAE1B;IAED;;;;OAIG;IACH,IAAI,oBAAoB,IAAI,OAAO,CAGlC;IAED;;;;OAIG;IACH,IAAI,sBAAsB,IAAI,OAAO,CAQpC;IAED;;OAEG;IACH,UAAU,IAAI,IAAI;IAMlB;;;;;OAKG;IACH,4BAA4B,CAC1B,KAAK,EAAE,qBAAqB,CAAC,0BAA0B,GACtD,IAAI;IAwCP;;;;;OAKG;IACH,uBAAuB,CAAC,QAAQ,EAAE,eAAe,GAAG,IAAI;IAWxD;;;;;OAKG;IACH,sBAAsB,CAAC,OAAO,EAAE,cAAc,GAAG,IAAI;IAoBrD;;;;;;;;;;OAUG;IACH,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC;IAuBhC;;;;;;;;;;OAUG;IACH,0BAA0B,IAAI,OAAO,CAAC,IAAI,CAAC;CAoU5C"}
@@ -139,9 +139,9 @@ export class BackupAndSyncService {
139
139
  *
140
140
  * @returns A promise that resolves when the sync is complete.
141
141
  */
142
- async performFullSync() {
142
+ performFullSync() {
143
143
  if (!this.isBackupAndSyncEnabled) {
144
- return undefined;
144
+ return Promise.resolve(undefined);
145
145
  }
146
146
  // If there's an ongoing sync (including first sync), return it
147
147
  if (__classPrivateFieldGet(this, _BackupAndSyncService_ongoingFullSyncPromise, "f")) {
@@ -166,9 +166,9 @@ export class BackupAndSyncService {
166
166
  *
167
167
  * @returns A promise that resolves when the sync is complete.
168
168
  */
169
- async performFullSyncAtLeastOnce() {
169
+ performFullSyncAtLeastOnce() {
170
170
  if (!this.isBackupAndSyncEnabled) {
171
- return undefined;
171
+ return Promise.resolve(undefined);
172
172
  }
173
173
  if (!__classPrivateFieldGet(this, _BackupAndSyncService_firstOngoingFullSyncPromise, "f")) {
174
174
  __classPrivateFieldSet(this, _BackupAndSyncService_firstOngoingFullSyncPromise, __classPrivateFieldGet(this, _BackupAndSyncService_atomicSyncQueue, "f").clearAndEnqueue(() => __classPrivateFieldGet(this, _BackupAndSyncService_instances, "m", _BackupAndSyncService_performFullSyncInner).call(this)), "f");
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAG1D,OAAO,EAAE,mBAAmB,EAAE,yBAAwB;AAGtD,OAAO,EAAE,SAAS,EAAE,+BAA8B;AAElD,OAAO,EAAE,YAAY,EAAE,oCAAmC;AAC1D,OAAO,EACL,gCAAgC,EAChC,2BAA2B,EAC3B,kBAAkB,EAClB,iBAAiB,EACjB,kBAAkB,EACnB,6BAA4B;AAM7B,OAAO,EACL,2BAA2B,EAC3B,uBAAuB,EACvB,wBAAwB,EACxB,2BAA2B,EAC5B,kCAAiC;AAClC,OAAO,EACL,mBAAmB,EACnB,wBAAwB,EACxB,sBAAsB,EACtB,8BAA8B,EAC9B,cAAc,EACf,2BAA0B;AAE3B,OAAO,EAAE,eAAe,EAAE,gCAA+B;AAEzD;;;;;;;;GAQG;AACH,MAAM,OAAO,oBAAoB;IAoB/B,YAAY,OAA6B;;QAnBhC,gDAA+B;QAExC;;WAEG;QACM,wDAAkC;QAE3C;;;WAGG;QACH,uDAAgD,IAAI,EAAC;QAErD;;;WAGG;QACH,4DAAqD,IAAI,EAAC;QAGxD,uBAAA,IAAI,iCAAY,OAAO,MAAA,CAAC;QACxB,uBAAA,IAAI,yCAAoB,IAAI,eAAe,EAAE,MAAA,CAAC;IAChD,CAAC;IAED;;;;OAIG;IACH,IAAI,YAAY;QACd,OAAO,uBAAA,IAAI,qCAAS,CAAC,UAAU,CAAC,KAAK,CAAC,8BAA8B,CAAC;IACvE,CAAC;IAED;;;;OAIG;IACH,IAAI,oBAAoB;QACtB,OAAO,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EAAE,CAAC;YACZ,OAAO,CAAC,4BAA4B;QACtC,CAAC;QAED,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACrC,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,OAAO;QACT,CAAC;QAED,MAAM,eAAe,GAAG,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACnD,MAAM,eAAe,GAAG,MAAM,YAAY,CACxC,uBAAA,IAAI,qCAAS,EACb,eAAe,CAChB,CAAC;QAEF,2CAA2C;QAC3C,MAAM,oBAAoB,GAAG,MAAM,uBAAuB,CACxD,uBAAA,IAAI,qCAAS,EACb,eAAe,EACf,KAAK,CAAC,QAAQ,CAAC,OAAO,CAAC,UAAU,CAClC,CAAC;QAEF,MAAM,iBAAiB,CACrB,uBAAA,IAAI,qCAAS,EACb,KAAK,EACL,oBAAoB,EACpB,eAAe,EACf,eAAe,CAChB,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,mBAAmB,CAAC,kCAAkC,OAAO,GAAG,EAAE,KAAK,CAAC,CAAC;QACzE,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type { UserStorageController } from '@metamask/profile-sync-controller';\n\nimport { backupAndSyncLogger } from '../../logger.js';\nimport type { AccountTreeControllerState } from '../../types.js';\nimport type { AccountWalletEntropyObject } from '../../wallet.js';\nimport { TraceName } from '../analytics/index.js';\nimport type { ProfileId } from '../authentication/index.js';\nimport { getProfileId } from '../authentication/index.js';\nimport {\n createLocalGroupsFromUserStorage,\n performLegacyAccountSyncing,\n syncGroupsMetadata,\n syncGroupMetadata,\n syncWalletMetadata,\n} from '../syncing/index.js';\nimport type {\n BackupAndSyncContext,\n UserStorageSyncedWallet,\n UserStorageSyncedWalletGroup,\n} from '../types.js';\nimport {\n getAllGroupsFromUserStorage,\n getGroupFromUserStorage,\n getWalletFromUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage/index.js';\nimport {\n createStateSnapshot,\n restoreStateFromSnapshot,\n getLocalEntropyWallets,\n getLocalGroupsForEntropyWallet,\n toErrorMessage,\n} from '../utils/index.js';\nimport type { StateSnapshot } from '../utils/index.js';\nimport { AtomicSyncQueue } from './atomic-sync-queue.js';\n\n/**\n * Service responsible for managing all backup and sync operations.\n *\n * This service handles:\n * - Full sync operations\n * - Single item sync operations\n * - Sync queue management\n * - Sync state management\n */\nexport class BackupAndSyncService {\n readonly #context: BackupAndSyncContext;\n\n /**\n * Queue manager for atomic sync operations.\n */\n readonly #atomicSyncQueue: AtomicSyncQueue;\n\n /**\n * Cached promise for ongoing full sync operations.\n * Ensures multiple callers await the same sync operation.\n */\n #ongoingFullSyncPromise: Promise<void> | null = null;\n\n /**\n * Cached promise for the first ongoing full sync operation.\n * Ensures multiple callers await the same sync operation.\n */\n #firstOngoingFullSyncPromise: Promise<void> | null = null;\n\n constructor(context: BackupAndSyncContext) {\n this.#context = context;\n this.#atomicSyncQueue = new AtomicSyncQueue();\n }\n\n /**\n * Checks if syncing is currently in progress.\n *\n * @returns True if syncing is in progress.\n */\n get isInProgress(): boolean {\n return this.#context.controller.state.isAccountTreeSyncingInProgress;\n }\n\n /**\n * Checks if the account tree has been synced at least once.\n *\n * @returns True if the account tree has been synced at least once.\n */\n get hasSyncedAtLeastOnce(): boolean {\n return this.#context.controller.state\n .hasAccountTreeSyncingSyncedAtLeastOnce;\n }\n\n /**\n * Checks if backup and sync is enabled by checking UserStorageController state.\n *\n * @returns True if backup and sync + account syncing is enabled.\n */\n get isBackupAndSyncEnabled(): boolean {\n const userStorageControllerState = this.#context.messenger.call(\n 'UserStorageController:getState',\n );\n const { isAccountSyncingEnabled, isBackupAndSyncEnabled } =\n userStorageControllerState;\n\n return isBackupAndSyncEnabled && isAccountSyncingEnabled;\n }\n\n /**\n * Clears the atomic queue and resets ongoing operations.\n */\n clearState(): void {\n this.#atomicSyncQueue.clear();\n this.#ongoingFullSyncPromise = null;\n this.#firstOngoingFullSyncPromise = null;\n }\n\n /**\n * Handles changes to the user storage state.\n * Used to clear the backup and sync service state.\n *\n * @param state - The new user storage state.\n */\n handleUserStorageStateChange(\n state: UserStorageController.UserStorageControllerState,\n ): void {\n if (!state.isAccountSyncingEnabled || !state.isBackupAndSyncEnabled) {\n // If either syncing is disabled, clear the account tree state\n this.clearState();\n }\n }\n\n /**\n * Gets the entropy wallet associated with the given wallet ID.\n *\n * @param walletId - The wallet ID to look up.\n * @returns The associated entropy wallet, or undefined if not found.\n */\n #getEntropyWallet(\n walletId: AccountWalletId,\n ): AccountWalletEntropyObject | undefined {\n const wallet = this.#context.controller.state.accountTree.wallets[walletId];\n return wallet?.type === AccountWalletType.Entropy ? wallet : undefined;\n }\n\n /**\n * Sets up cleanup for ongoing sync promise tracking without affecting error propagation.\n *\n * @param promise - The promise to track and clean up\n * @returns The same promise (for chaining)\n */\n #setupOngoingPromiseCleanup(promise: Promise<void>): Promise<void> {\n this.#ongoingFullSyncPromise = promise;\n // Set up cleanup without affecting the returned promise\n promise\n .finally(() => {\n this.#ongoingFullSyncPromise = null;\n })\n .catch(() => {\n // Only ignore errors from the cleanup operation itself\n // The original promise errors are still propagated to callers\n });\n return promise;\n }\n\n /**\n * Enqueues a single wallet sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param walletId - The wallet ID to sync.\n */\n enqueueSingleWalletSync(walletId: AccountWalletId): void {\n if (!this.isBackupAndSyncEnabled || !this.hasSyncedAtLeastOnce) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleWalletSyncInner(walletId),\n );\n }\n\n /**\n * Enqueues a single group sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param groupId - The group ID to sync.\n */\n enqueueSingleGroupSync(groupId: AccountGroupId): void {\n if (\n !this.isBackupAndSyncEnabled ||\n !this.hasSyncedAtLeastOnce ||\n // This prevents rate limiting scenarios where full syncs trigger group creations\n // that in turn enqueue the same single group syncs that the full sync just did.\n // This can very rarely lead to inconsistencies, but will be fixed on the next full sync.\n // TODO: let's improve this in the future by tracking the updates done in the full sync and\n // comparing against that.\n this.isInProgress\n ) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleGroupSyncInner(groupId),\n );\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n * If a full sync is already in progress, it will return the ongoing promise.\n * This clears the atomic sync queue before starting the full sync.\n *\n * NOTE: in some very edge cases, this can be ran concurrently if triggered quickly after\n * toggling back and forth the backup and sync feature from the UI.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSync(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n // If there's an ongoing sync (including first sync), return it\n if (this.#ongoingFullSyncPromise) {\n return this.#ongoingFullSyncPromise;\n }\n\n // Create a new ongoing sync (sequential calls after previous completed)\n const newSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(() =>\n this.#performFullSyncInner(),\n );\n\n // First sync setup - create and cache the first sync promise\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = newSyncPromise;\n }\n\n return this.#setupOngoingPromiseCleanup(newSyncPromise);\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * If the first ever full sync is already in progress, it will return the ongoing promise.\n * If the first ever full sync has already completed, it will resolve and NOT start a new sync.\n *\n * This clears the atomic sync queue before starting the full sync.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n async performFullSyncAtLeastOnce(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return undefined;\n }\n\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(\n () => this.#performFullSyncInner(),\n );\n // eslint-disable-next-line no-void\n void this.#setupOngoingPromiseCleanup(this.#firstOngoingFullSyncPromise);\n }\n\n return this.#firstOngoingFullSyncPromise;\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * This method performs a comprehensive sync operation that:\n * 1. Identifies all local entropy wallets that can be synchronized\n * 2. Performs legacy account syncing if needed (for backwards compatibility)\n * - Disables subsequent legacy syncing by setting a flag in user storage\n * - Exits early if multichain account syncing is disabled after legacy sync\n * 3. Executes multichain account syncing for each wallet:\n * - Syncs wallet metadata bidirectionally\n * - Creates missing local groups from user storage data (or pushes local groups if none exist remotely)\n * - Refreshes local state to reflect newly created groups\n * - Syncs group metadata bidirectionally\n *\n * The sync is atomic per wallet with rollback on errors, but continues processing other wallets\n * if individual wallet sync fails. A global lock prevents concurrent sync operations.\n *\n * During this process, all other atomic multichain related user storage updates are blocked.\n *\n * @throws Will throw if the sync operation encounters unrecoverable errors\n */\n async #performFullSyncInner(): Promise<void> {\n // Prevent multiple syncs from running at the same time.\n // Also prevents atomic updates from being applied while syncing is in progress.\n if (this.isInProgress) {\n return;\n }\n\n // Set isAccountTreeSyncingInProgress immediately to prevent race conditions\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = true;\n },\n );\n\n // Encapsulate the sync logic in a function to allow tracing\n const bigSyncFn = async () => {\n try {\n // 1. Identifies all local entropy wallets that can be synchronized\n const localSyncableWallets = getLocalEntropyWallets(this.#context);\n\n if (!localSyncableWallets.length) {\n // No wallets to sync, just return. This shouldn't happen.\n return;\n }\n\n // 2. Iterate over each local wallet\n for (const wallet of localSyncableWallets) {\n const entropySourceId = wallet.metadata.entropy.id;\n\n let walletProfileId: ProfileId;\n let walletFromUserStorage: UserStorageSyncedWallet | null;\n let groupsFromUserStorage: UserStorageSyncedWalletGroup[];\n\n try {\n walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n [walletFromUserStorage, groupsFromUserStorage] = await Promise.all([\n getWalletFromUserStorage(this.#context, entropySourceId),\n getAllGroupsFromUserStorage(this.#context, entropySourceId),\n ]);\n\n // 2.1 Decide if we need to perform legacy account syncing\n if (\n !walletFromUserStorage ||\n !walletFromUserStorage.isLegacyAccountSyncingDisabled\n ) {\n // 2.2 Perform legacy account syncing\n // This will migrate legacy account data to the new structure.\n // This operation will only be performed once.\n await performLegacyAccountSyncing(\n this.#context,\n entropySourceId,\n walletProfileId,\n );\n }\n } catch (error) {\n const errorMessage = toErrorMessage(error);\n\n backupAndSyncLogger(\n `Legacy syncing failed for wallet ${wallet.id}: ${errorMessage}`,\n );\n throw new Error(\n `Legacy syncing failed for wallet: ${errorMessage}`,\n );\n }\n\n // 3. Execute multichain account syncing\n let stateSnapshot: StateSnapshot | undefined;\n\n // Capture the local-write flag so it can be reverted together with\n // the state snapshot if this wallet is rolled back. Remote writes are\n // durable and intentionally excluded.\n const localWriteBeforeWallet =\n this.#context.mutationTracker?.getLocalWrite() ?? false;\n\n try {\n // 3.1 Wallet syncing\n // Create a state snapshot before processing each wallet for potential rollback\n stateSnapshot = createStateSnapshot(this.#context);\n\n // Sync wallet metadata bidirectionally\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n\n // 3.2 Groups syncing\n // If groups data does not exist in user storage yet, create it\n if (!groupsFromUserStorage.length) {\n // If no groups exist in user storage, we can push all groups from the wallet to the user storage and exit\n await pushGroupToUserStorageBatch(\n this.#context,\n getLocalGroupsForEntropyWallet(this.#context, wallet.id),\n entropySourceId,\n );\n\n continue; // No need to proceed with metadata comparison if groups are new\n }\n\n // Create local groups for each group from user storage if they do not exist\n // This will ensure that we have all groups available locally before syncing metadata\n await createLocalGroupsFromUserStorage(\n this.#context,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n\n // Sync group metadata bidirectionally\n await syncGroupsMetadata(\n this.#context,\n wallet,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n const errorMessage = toErrorMessage(error);\n const errorString = `Error during multichain account syncing for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n\n // Attempt to rollback state changes for this wallet\n try {\n if (!stateSnapshot) {\n throw new Error(\n `State snapshot is missing for wallet ${wallet.id}`,\n );\n }\n restoreStateFromSnapshot(this.#context, stateSnapshot);\n // Revert the local-write flag too, so a rolled-back wallet does\n // not keep the run marked as having changed local state.\n this.#context.mutationTracker?.setLocalWrite(\n localWriteBeforeWallet,\n );\n backupAndSyncLogger(\n `Rolled back state changes for wallet ${wallet.id}`,\n );\n } catch (rollbackError) {\n backupAndSyncLogger(\n `Failed to rollback state for wallet ${wallet.id}:`,\n rollbackError instanceof Error\n ? rollbackError.message\n : String(rollbackError),\n );\n }\n\n // Continue with next wallet instead of failing the entire sync\n continue;\n }\n }\n } catch (error) {\n backupAndSyncLogger('Error during multichain account syncing:', error);\n throw error;\n }\n\n this.#context.controllerStateUpdateFn((state) => {\n state.hasAccountTreeSyncingSyncedAtLeastOnce = true;\n });\n };\n\n // Execute the big sync function and ensure state cleanup.\n // The sync runs untraced so we can decide afterwards whether it did any\n // real work; the span is then backdated to preserve the real duration.\n const { mutationTracker } = this.#context;\n mutationTracker?.reset();\n const startTime = Date.now();\n try {\n await bigSyncFn();\n } finally {\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = false;\n },\n );\n\n if (mutationTracker?.hasOccurred()) {\n try {\n await this.#context.traceFn(\n {\n name: TraceName.AccountSyncFull,\n startTime,\n },\n () => undefined,\n );\n } catch (traceError) {\n backupAndSyncLogger(\n 'Failed to emit AccountSyncFull trace:',\n traceError,\n );\n }\n }\n }\n }\n\n /**\n * Performs a single wallet's bidirectional metadata sync with user storage.\n *\n * @param walletId - The wallet ID to sync.\n */\n async #performSingleWalletSyncInner(\n walletId: AccountWalletId,\n ): Promise<void> {\n try {\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n const walletFromUserStorage = await getWalletFromUserStorage(\n this.#context,\n entropySourceId,\n );\n\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(\n `Error in single wallet sync for ${walletId}:`,\n error,\n );\n throw error;\n }\n }\n\n /**\n * Performs a single group's bidirectional metadata sync with user storage.\n *\n * @param groupId - The group ID to sync.\n */\n async #performSingleGroupSyncInner(groupId: AccountGroupId): Promise<void> {\n try {\n const walletId = this.#context.groupIdToWalletId.get(groupId);\n if (!walletId) {\n return;\n }\n\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const group = wallet.groups[groupId];\n if (!group) {\n return;\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n // Get the specific group from user storage\n const groupFromUserStorage = await getGroupFromUserStorage(\n this.#context,\n entropySourceId,\n group.metadata.entropy.groupIndex,\n );\n\n await syncGroupMetadata(\n this.#context,\n group,\n groupFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(`Error in single group sync for ${groupId}:`, error);\n throw error;\n }\n }\n}\n"]}
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../../../src/backup-and-sync/service/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAG1D,OAAO,EAAE,mBAAmB,EAAE,yBAAwB;AAGtD,OAAO,EAAE,SAAS,EAAE,+BAA8B;AAElD,OAAO,EAAE,YAAY,EAAE,oCAAmC;AAC1D,OAAO,EACL,gCAAgC,EAChC,2BAA2B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AAQ,CAAC,OAAO,CAAC,UAAU,CAClC,CAAC;QAEF,MAAM,iBAAiB,CACrB,uBAAA,IAAI,qCAAS,EACb,KAAK,EACL,oBAAoB,EACpB,eAAe,EACf,eAAe,CAChB,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,mBAAmB,CAAC,kCAAkC,OAAO,GAAG,EAAE,KAAK,CAAC,CAAC;QACzE,MAAM,KAAK,CAAC;IACd,CAAC;AACH,CAAC","sourcesContent":["import type { AccountGroupId, AccountWalletId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type { UserStorageController } from '@metamask/profile-sync-controller';\n\nimport { backupAndSyncLogger } from '../../logger.js';\nimport type { AccountTreeControllerState } from '../../types.js';\nimport type { AccountWalletEntropyObject } from '../../wallet.js';\nimport { TraceName } from '../analytics/index.js';\nimport type { ProfileId } from '../authentication/index.js';\nimport { getProfileId } from '../authentication/index.js';\nimport {\n createLocalGroupsFromUserStorage,\n performLegacyAccountSyncing,\n syncGroupsMetadata,\n syncGroupMetadata,\n syncWalletMetadata,\n} from '../syncing/index.js';\nimport type {\n BackupAndSyncContext,\n UserStorageSyncedWallet,\n UserStorageSyncedWalletGroup,\n} from '../types.js';\nimport {\n getAllGroupsFromUserStorage,\n getGroupFromUserStorage,\n getWalletFromUserStorage,\n pushGroupToUserStorageBatch,\n} from '../user-storage/index.js';\nimport {\n createStateSnapshot,\n restoreStateFromSnapshot,\n getLocalEntropyWallets,\n getLocalGroupsForEntropyWallet,\n toErrorMessage,\n} from '../utils/index.js';\nimport type { StateSnapshot } from '../utils/index.js';\nimport { AtomicSyncQueue } from './atomic-sync-queue.js';\n\n/**\n * Service responsible for managing all backup and sync operations.\n *\n * This service handles:\n * - Full sync operations\n * - Single item sync operations\n * - Sync queue management\n * - Sync state management\n */\nexport class BackupAndSyncService {\n readonly #context: BackupAndSyncContext;\n\n /**\n * Queue manager for atomic sync operations.\n */\n readonly #atomicSyncQueue: AtomicSyncQueue;\n\n /**\n * Cached promise for ongoing full sync operations.\n * Ensures multiple callers await the same sync operation.\n */\n #ongoingFullSyncPromise: Promise<void> | null = null;\n\n /**\n * Cached promise for the first ongoing full sync operation.\n * Ensures multiple callers await the same sync operation.\n */\n #firstOngoingFullSyncPromise: Promise<void> | null = null;\n\n constructor(context: BackupAndSyncContext) {\n this.#context = context;\n this.#atomicSyncQueue = new AtomicSyncQueue();\n }\n\n /**\n * Checks if syncing is currently in progress.\n *\n * @returns True if syncing is in progress.\n */\n get isInProgress(): boolean {\n return this.#context.controller.state.isAccountTreeSyncingInProgress;\n }\n\n /**\n * Checks if the account tree has been synced at least once.\n *\n * @returns True if the account tree has been synced at least once.\n */\n get hasSyncedAtLeastOnce(): boolean {\n return this.#context.controller.state\n .hasAccountTreeSyncingSyncedAtLeastOnce;\n }\n\n /**\n * Checks if backup and sync is enabled by checking UserStorageController state.\n *\n * @returns True if backup and sync + account syncing is enabled.\n */\n get isBackupAndSyncEnabled(): boolean {\n const userStorageControllerState = this.#context.messenger.call(\n 'UserStorageController:getState',\n );\n const { isAccountSyncingEnabled, isBackupAndSyncEnabled } =\n userStorageControllerState;\n\n return isBackupAndSyncEnabled && isAccountSyncingEnabled;\n }\n\n /**\n * Clears the atomic queue and resets ongoing operations.\n */\n clearState(): void {\n this.#atomicSyncQueue.clear();\n this.#ongoingFullSyncPromise = null;\n this.#firstOngoingFullSyncPromise = null;\n }\n\n /**\n * Handles changes to the user storage state.\n * Used to clear the backup and sync service state.\n *\n * @param state - The new user storage state.\n */\n handleUserStorageStateChange(\n state: UserStorageController.UserStorageControllerState,\n ): void {\n if (!state.isAccountSyncingEnabled || !state.isBackupAndSyncEnabled) {\n // If either syncing is disabled, clear the account tree state\n this.clearState();\n }\n }\n\n /**\n * Gets the entropy wallet associated with the given wallet ID.\n *\n * @param walletId - The wallet ID to look up.\n * @returns The associated entropy wallet, or undefined if not found.\n */\n #getEntropyWallet(\n walletId: AccountWalletId,\n ): AccountWalletEntropyObject | undefined {\n const wallet = this.#context.controller.state.accountTree.wallets[walletId];\n return wallet?.type === AccountWalletType.Entropy ? wallet : undefined;\n }\n\n /**\n * Sets up cleanup for ongoing sync promise tracking without affecting error propagation.\n *\n * @param promise - The promise to track and clean up\n * @returns The same promise (for chaining)\n */\n #setupOngoingPromiseCleanup(promise: Promise<void>): Promise<void> {\n this.#ongoingFullSyncPromise = promise;\n // Set up cleanup without affecting the returned promise\n promise\n .finally(() => {\n this.#ongoingFullSyncPromise = null;\n })\n .catch(() => {\n // Only ignore errors from the cleanup operation itself\n // The original promise errors are still propagated to callers\n });\n return promise;\n }\n\n /**\n * Enqueues a single wallet sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param walletId - The wallet ID to sync.\n */\n enqueueSingleWalletSync(walletId: AccountWalletId): void {\n if (!this.isBackupAndSyncEnabled || !this.hasSyncedAtLeastOnce) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleWalletSyncInner(walletId),\n );\n }\n\n /**\n * Enqueues a single group sync operation (fire-and-forget).\n * If the first full sync has not yet occurred, it does nothing.\n *\n * @param groupId - The group ID to sync.\n */\n enqueueSingleGroupSync(groupId: AccountGroupId): void {\n if (\n !this.isBackupAndSyncEnabled ||\n !this.hasSyncedAtLeastOnce ||\n // This prevents rate limiting scenarios where full syncs trigger group creations\n // that in turn enqueue the same single group syncs that the full sync just did.\n // This can very rarely lead to inconsistencies, but will be fixed on the next full sync.\n // TODO: let's improve this in the future by tracking the updates done in the full sync and\n // comparing against that.\n this.isInProgress\n ) {\n return;\n }\n\n // eslint-disable-next-line no-void\n void this.#atomicSyncQueue.enqueue(() =>\n this.#performSingleGroupSyncInner(groupId),\n );\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n * If a full sync is already in progress, it will return the ongoing promise.\n * This clears the atomic sync queue before starting the full sync.\n *\n * NOTE: in some very edge cases, this can be ran concurrently if triggered quickly after\n * toggling back and forth the backup and sync feature from the UI.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n performFullSync(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return Promise.resolve(undefined);\n }\n\n // If there's an ongoing sync (including first sync), return it\n if (this.#ongoingFullSyncPromise) {\n return this.#ongoingFullSyncPromise;\n }\n\n // Create a new ongoing sync (sequential calls after previous completed)\n const newSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(() =>\n this.#performFullSyncInner(),\n );\n\n // First sync setup - create and cache the first sync promise\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = newSyncPromise;\n }\n\n return this.#setupOngoingPromiseCleanup(newSyncPromise);\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * If the first ever full sync is already in progress, it will return the ongoing promise.\n * If the first ever full sync has already completed, it will resolve and NOT start a new sync.\n *\n * This clears the atomic sync queue before starting the full sync.\n *\n * @returns A promise that resolves when the sync is complete.\n */\n performFullSyncAtLeastOnce(): Promise<void> {\n if (!this.isBackupAndSyncEnabled) {\n return Promise.resolve(undefined);\n }\n\n if (!this.#firstOngoingFullSyncPromise) {\n this.#firstOngoingFullSyncPromise = this.#atomicSyncQueue.clearAndEnqueue(\n () => this.#performFullSyncInner(),\n );\n // eslint-disable-next-line no-void\n void this.#setupOngoingPromiseCleanup(this.#firstOngoingFullSyncPromise);\n }\n\n return this.#firstOngoingFullSyncPromise;\n }\n\n /**\n * Performs a full synchronization of the local account tree with user storage, ensuring consistency\n * between local state and cloud-stored account data.\n *\n * This method performs a comprehensive sync operation that:\n * 1. Identifies all local entropy wallets that can be synchronized\n * 2. Performs legacy account syncing if needed (for backwards compatibility)\n * - Disables subsequent legacy syncing by setting a flag in user storage\n * - Exits early if multichain account syncing is disabled after legacy sync\n * 3. Executes multichain account syncing for each wallet:\n * - Syncs wallet metadata bidirectionally\n * - Creates missing local groups from user storage data (or pushes local groups if none exist remotely)\n * - Refreshes local state to reflect newly created groups\n * - Syncs group metadata bidirectionally\n *\n * The sync is atomic per wallet with rollback on errors, but continues processing other wallets\n * if individual wallet sync fails. A global lock prevents concurrent sync operations.\n *\n * During this process, all other atomic multichain related user storage updates are blocked.\n *\n * @throws Will throw if the sync operation encounters unrecoverable errors\n */\n async #performFullSyncInner(): Promise<void> {\n // Prevent multiple syncs from running at the same time.\n // Also prevents atomic updates from being applied while syncing is in progress.\n if (this.isInProgress) {\n return;\n }\n\n // Set isAccountTreeSyncingInProgress immediately to prevent race conditions\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = true;\n },\n );\n\n // Encapsulate the sync logic in a function to allow tracing\n const bigSyncFn = async () => {\n try {\n // 1. Identifies all local entropy wallets that can be synchronized\n const localSyncableWallets = getLocalEntropyWallets(this.#context);\n\n if (!localSyncableWallets.length) {\n // No wallets to sync, just return. This shouldn't happen.\n return;\n }\n\n // 2. Iterate over each local wallet\n for (const wallet of localSyncableWallets) {\n const entropySourceId = wallet.metadata.entropy.id;\n\n let walletProfileId: ProfileId;\n let walletFromUserStorage: UserStorageSyncedWallet | null;\n let groupsFromUserStorage: UserStorageSyncedWalletGroup[];\n\n try {\n walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n [walletFromUserStorage, groupsFromUserStorage] = await Promise.all([\n getWalletFromUserStorage(this.#context, entropySourceId),\n getAllGroupsFromUserStorage(this.#context, entropySourceId),\n ]);\n\n // 2.1 Decide if we need to perform legacy account syncing\n if (\n !walletFromUserStorage ||\n !walletFromUserStorage.isLegacyAccountSyncingDisabled\n ) {\n // 2.2 Perform legacy account syncing\n // This will migrate legacy account data to the new structure.\n // This operation will only be performed once.\n await performLegacyAccountSyncing(\n this.#context,\n entropySourceId,\n walletProfileId,\n );\n }\n } catch (error) {\n const errorMessage = toErrorMessage(error);\n\n backupAndSyncLogger(\n `Legacy syncing failed for wallet ${wallet.id}: ${errorMessage}`,\n );\n throw new Error(\n `Legacy syncing failed for wallet: ${errorMessage}`,\n );\n }\n\n // 3. Execute multichain account syncing\n let stateSnapshot: StateSnapshot | undefined;\n\n // Capture the local-write flag so it can be reverted together with\n // the state snapshot if this wallet is rolled back. Remote writes are\n // durable and intentionally excluded.\n const localWriteBeforeWallet =\n this.#context.mutationTracker?.getLocalWrite() ?? false;\n\n try {\n // 3.1 Wallet syncing\n // Create a state snapshot before processing each wallet for potential rollback\n stateSnapshot = createStateSnapshot(this.#context);\n\n // Sync wallet metadata bidirectionally\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n\n // 3.2 Groups syncing\n // If groups data does not exist in user storage yet, create it\n if (!groupsFromUserStorage.length) {\n // If no groups exist in user storage, we can push all groups from the wallet to the user storage and exit\n await pushGroupToUserStorageBatch(\n this.#context,\n getLocalGroupsForEntropyWallet(this.#context, wallet.id),\n entropySourceId,\n );\n\n continue; // No need to proceed with metadata comparison if groups are new\n }\n\n // Create local groups for each group from user storage if they do not exist\n // This will ensure that we have all groups available locally before syncing metadata\n await createLocalGroupsFromUserStorage(\n this.#context,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n\n // Sync group metadata bidirectionally\n await syncGroupsMetadata(\n this.#context,\n wallet,\n groupsFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n const errorMessage = toErrorMessage(error);\n const errorString = `Error during multichain account syncing for wallet ${wallet.id}: ${errorMessage}`;\n\n backupAndSyncLogger(errorString);\n\n // Attempt to rollback state changes for this wallet\n try {\n if (!stateSnapshot) {\n throw new Error(\n `State snapshot is missing for wallet ${wallet.id}`,\n );\n }\n restoreStateFromSnapshot(this.#context, stateSnapshot);\n // Revert the local-write flag too, so a rolled-back wallet does\n // not keep the run marked as having changed local state.\n this.#context.mutationTracker?.setLocalWrite(\n localWriteBeforeWallet,\n );\n backupAndSyncLogger(\n `Rolled back state changes for wallet ${wallet.id}`,\n );\n } catch (rollbackError) {\n backupAndSyncLogger(\n `Failed to rollback state for wallet ${wallet.id}:`,\n rollbackError instanceof Error\n ? rollbackError.message\n : String(rollbackError),\n );\n }\n\n // Continue with next wallet instead of failing the entire sync\n continue;\n }\n }\n } catch (error) {\n backupAndSyncLogger('Error during multichain account syncing:', error);\n throw error;\n }\n\n this.#context.controllerStateUpdateFn((state) => {\n state.hasAccountTreeSyncingSyncedAtLeastOnce = true;\n });\n };\n\n // Execute the big sync function and ensure state cleanup.\n // The sync runs untraced so we can decide afterwards whether it did any\n // real work; the span is then backdated to preserve the real duration.\n const { mutationTracker } = this.#context;\n mutationTracker?.reset();\n const startTime = Date.now();\n try {\n await bigSyncFn();\n } finally {\n this.#context.controllerStateUpdateFn(\n (state: AccountTreeControllerState) => {\n state.isAccountTreeSyncingInProgress = false;\n },\n );\n\n if (mutationTracker?.hasOccurred()) {\n try {\n await this.#context.traceFn(\n {\n name: TraceName.AccountSyncFull,\n startTime,\n },\n () => undefined,\n );\n } catch (traceError) {\n backupAndSyncLogger(\n 'Failed to emit AccountSyncFull trace:',\n traceError,\n );\n }\n }\n }\n }\n\n /**\n * Performs a single wallet's bidirectional metadata sync with user storage.\n *\n * @param walletId - The wallet ID to sync.\n */\n async #performSingleWalletSyncInner(\n walletId: AccountWalletId,\n ): Promise<void> {\n try {\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n const walletFromUserStorage = await getWalletFromUserStorage(\n this.#context,\n entropySourceId,\n );\n\n await syncWalletMetadata(\n this.#context,\n wallet,\n walletFromUserStorage,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(\n `Error in single wallet sync for ${walletId}:`,\n error,\n );\n throw error;\n }\n }\n\n /**\n * Performs a single group's bidirectional metadata sync with user storage.\n *\n * @param groupId - The group ID to sync.\n */\n async #performSingleGroupSyncInner(groupId: AccountGroupId): Promise<void> {\n try {\n const walletId = this.#context.groupIdToWalletId.get(groupId);\n if (!walletId) {\n return;\n }\n\n const wallet = this.#getEntropyWallet(walletId);\n if (!wallet) {\n return; // Only sync entropy wallets\n }\n\n const group = wallet.groups[groupId];\n if (!group) {\n return;\n }\n\n const entropySourceId = wallet.metadata.entropy.id;\n const walletProfileId = await getProfileId(\n this.#context,\n entropySourceId,\n );\n\n // Get the specific group from user storage\n const groupFromUserStorage = await getGroupFromUserStorage(\n this.#context,\n entropySourceId,\n group.metadata.entropy.groupIndex,\n );\n\n await syncGroupMetadata(\n this.#context,\n group,\n groupFromUserStorage,\n entropySourceId,\n walletProfileId,\n );\n } catch (error) {\n backupAndSyncLogger(`Error in single group sync for ${groupId}:`, error);\n throw error;\n }\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metamask-previews/account-tree-controller",
3
- "version": "7.5.5-preview-68ba7f84e",
3
+ "version": "7.5.5-preview-5959fe813",
4
4
  "description": "Controller to group account together based on some pre-defined rules",
5
5
  "keywords": [
6
6
  "Ethereum",
@@ -75,10 +75,10 @@
75
75
  "@metamask/auto-changelog": "^6.1.0",
76
76
  "@metamask/providers": "^22.1.0",
77
77
  "@ts-bridge/cli": "^0.6.4",
78
- "@types/jest": "^29.5.14",
78
+ "@types/jest": "^30.0.0",
79
79
  "deepmerge": "^4.2.2",
80
- "jest": "^29.7.0",
81
- "ts-jest": "^29.2.5",
80
+ "jest": "^30.4.2",
81
+ "ts-jest": "^29.4.11",
82
82
  "tsx": "^4.20.5",
83
83
  "typedoc": "^0.25.13",
84
84
  "typedoc-plugin-missing-exports": "^2.0.0",