diff --git a/packages/neuron-wallet/src/block-sync-renderer/sync/light-synchronizer.ts b/packages/neuron-wallet/src/block-sync-renderer/sync/light-synchronizer.ts index aafa963859..2f7829c674 100644 --- a/packages/neuron-wallet/src/block-sync-renderer/sync/light-synchronizer.ts +++ b/packages/neuron-wallet/src/block-sync-renderer/sync/light-synchronizer.ts @@ -236,8 +236,8 @@ export default class LightSynchronizer extends Synchronizer { const deleteScript = retainedSyncScripts.filter(v => !allScriptHashes.has(scriptToHash(v.script))) await this.lightRpc.setScripts(deleteScript, 'delete') const walletIds = [...new Set(this.addressMetas.map(v => v.walletId))] - await SyncProgressService.initSyncProgress(addScripts) await SyncProgressService.updateSyncProgressFlag(walletIds) + await SyncProgressService.initSyncProgress(addScripts) } private async initMultisigSyncProgress() { @@ -345,16 +345,16 @@ export default class LightSynchronizer extends Synchronizer { } async processTxsInNextBlockNumber() { - const [nextBlockNumber, txHashesInNextBlock] = await this.getTxHashesWithNextUnprocessedBlockNumber() - const minSyncBlockNumber = await SyncProgressService.getCurrentWalletMinSyncedBlockNumber( - this.syncMultisig ? SyncAddressType.Multisig : undefined - ) + const [nextBlockNumber, txHashesInNextBlock, walletIds] = await this.getTxHashesWithNextUnprocessedBlockNumber() + const minSyncBlockNumber = this.syncMultisig + ? await SyncProgressService.getCurrentWalletMinSyncedBlockNumber(SyncAddressType.Multisig) + : await SyncProgressService.getMinSyncedBlockNumberInWallets(walletIds) if ( nextBlockNumber !== undefined && txHashesInNextBlock.length && // For light client, if tx hash has been called with fetch_transaction, the tx can not return by get_transactions // So before derived address synced to bigger than next synced block number, do not sync the next block number - minSyncBlockNumber >= parseInt(nextBlockNumber) && + (minSyncBlockNumber === undefined || minSyncBlockNumber >= parseInt(nextBlockNumber)) && // check whether the tx is sync from light client, after split the light client and full DB file, this check will remove (await this.checkTxExist(txHashesInNextBlock)) ) { diff --git a/packages/neuron-wallet/src/block-sync-renderer/sync/synchronizer.ts b/packages/neuron-wallet/src/block-sync-renderer/sync/synchronizer.ts index ff9c576f8c..e3b58c0eb7 100644 --- a/packages/neuron-wallet/src/block-sync-renderer/sync/synchronizer.ts +++ b/packages/neuron-wallet/src/block-sync-renderer/sync/synchronizer.ts @@ -69,7 +69,7 @@ export abstract class Synchronizer { await this.processNextBlockNumberQueue?.drain() } - protected async getTxHashesWithNextUnprocessedBlockNumber(): Promise<[string | undefined, string[]]> { + protected async getTxHashesWithNextUnprocessedBlockNumber(): Promise<[string | undefined, string[], string[]]> { const txHashCachesByNextBlockNumberAndAddress = await Promise.all( [...this.addressesByWalletId.keys()].map(async walletId => IndexerCacheService.nextUnprocessedTxsGroupedByBlockNumber(walletId) @@ -87,12 +87,15 @@ export abstract class Synchronizer { const nextUnprocessedBlockNumber = [...groupedTxHashCaches.keys()].sort((a, b) => parseInt(a) - parseInt(b)).shift() if (!nextUnprocessedBlockNumber) { - return [undefined, []] + return [undefined, [], []] } const txHashCachesInNextUnprocessedBlockNumber = groupedTxHashCaches.get(nextUnprocessedBlockNumber) - - return [nextUnprocessedBlockNumber, txHashCachesInNextUnprocessedBlockNumber!.map(({ txHash }) => txHash)] + return [ + nextUnprocessedBlockNumber, + txHashCachesInNextUnprocessedBlockNumber!.map(({ txHash }) => txHash), + [...new Set(txHashCachesInNextUnprocessedBlockNumber!.map(({ walletId }) => walletId))], + ] } protected async notifyAndSyncNext(indexerTipNumber: number) { diff --git a/packages/neuron-wallet/src/services/sync-progress.ts b/packages/neuron-wallet/src/services/sync-progress.ts index 0d5060ec1b..67de4b1d9c 100644 --- a/packages/neuron-wallet/src/services/sync-progress.ts +++ b/packages/neuron-wallet/src/services/sync-progress.ts @@ -19,6 +19,9 @@ export default class SyncProgressService { .getRepository(SyncProgress) .find({ select: ['hash'], + where: { + delete: false, + }, }) const existHashes = new Set(existProgresses.map(v => v.hash)) const newSyncProgreses = syncProgresses.filter(v => !existHashes.has(v.hash)) @@ -101,6 +104,20 @@ export default class SyncProgressService { return item?.syncedBlockNumber || 0 } + static async getMinSyncedBlockNumberInWallets(walletIds: string[]) { + const item = await getConnection() + .getRepository(SyncProgress) + .createQueryBuilder() + .where({ + delete: false, + addressType: SyncAddressType.Default, + walletId: In(walletIds), + }) + .orderBy('syncedBlockNumber', 'ASC') + .getOne() + return item?.syncedBlockNumber + } + static async getWalletMinLocalSavedBlockNumber() { const items = await getConnection() .getRepository(SyncProgress) diff --git a/packages/neuron-wallet/tests/block-sync-renderer/synchronizer.test.ts b/packages/neuron-wallet/tests/block-sync-renderer/synchronizer.test.ts index fc51c62735..dad25e6599 100644 --- a/packages/neuron-wallet/tests/block-sync-renderer/synchronizer.test.ts +++ b/packages/neuron-wallet/tests/block-sync-renderer/synchronizer.test.ts @@ -142,7 +142,7 @@ describe('unit tests for IndexerConnector', () => { stubbedNextUnprocessedTxsGroupedByBlockNumberFn.mockResolvedValue([]) // @ts-ignore private method const result = await synchronizer.getTxHashesWithNextUnprocessedBlockNumber() - expect(result).toStrictEqual([undefined, []]) + expect(result).toStrictEqual([undefined, [], []]) }) it('get cached tx and sort by block number', async () => { stubbedNextUnprocessedTxsGroupedByBlockNumberFn.mockImplementation(walletId => @@ -166,7 +166,7 @@ describe('unit tests for IndexerConnector', () => { ) // @ts-ignore private method const result = await synchronizer.getTxHashesWithNextUnprocessedBlockNumber() - expect(result).toStrictEqual(['2', ['hash2']]) + expect(result).toStrictEqual(['2', ['hash2'], [walletId2]]) }) })