Skip to content

Commit

Permalink
fix: Ignore delete wallet when ignore duplicate script.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu committed Aug 3, 2024
1 parent 0f3e0c2 commit 4d820db
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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))
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions packages/neuron-wallet/src/services/sync-progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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]])
})
})

Expand Down

0 comments on commit 4d820db

Please sign in to comment.