Skip to content

Commit

Permalink
fix: Fix light client sync when import existed wallet.
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu committed May 6, 2024
1 parent 0a3d8a9 commit a99ed75
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,11 @@ export default class LightSynchronizer extends Synchronizer {
if (!this.addressMetas.length && !appendScripts.length) {
return
}
const existSyncArgses = await SyncProgressService.getExistSyncArgses()
const syncScripts = await this.lightRpc.getScripts()
const retainedSyncScripts = syncScripts.filter(v => existSyncArgses.has(v.script.args))
const existSyncscripts: Record<string, LightScriptFilter> = {}
syncScripts.forEach(v => {
retainedSyncScripts.forEach(v => {
existSyncscripts[scriptToHash(v.script)] = v
})
const currentWalletId = WalletService.getInstance().getCurrent()?.id
Expand Down Expand Up @@ -228,7 +230,7 @@ export default class LightSynchronizer extends Synchronizer {
...allScripts.map(v => scriptToHash(v.script)),
...appendScripts.map(v => scriptToHash(v.script)),
])
const deleteScript = syncScripts.filter(v => !allScriptHashes.has(scriptToHash(v.script)))
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)
Expand Down
12 changes: 12 additions & 0 deletions packages/neuron-wallet/src/services/sync-progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ export default class SyncProgressService {
return res
}

static async getExistSyncArgses() {
const syncProgresses = await getConnection().getRepository(SyncProgress).createQueryBuilder().getMany()
return new Set(syncProgresses.map(v => v.args))
}

static async getAllSyncStatusToMap() {
const result: Map<CKBComponents.Hash, SyncProgress> = new Map()
const syncProgresses = await getConnection()
Expand Down Expand Up @@ -132,4 +137,11 @@ export default class SyncProgressService {
.set({ localSavedBlockNumber: 0, syncedBlockNumber: 0 })
.execute()
}

static async deleteWalletSyncProgress(walletId: string) {
await getConnection().getRepository(SyncProgress).delete({
walletId,
addressType: SyncAddressType.Default,
})
}
}
3 changes: 3 additions & 0 deletions packages/neuron-wallet/src/services/wallets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getConnection } from '../database/chain/connection'
import NetworksService from './networks'
import { NetworkType } from '../models/network'
import { resetSyncTaskQueue } from '../block-sync-renderer'
import SyncProgressService from './sync-progress'

const fileService = FileService.getInstance()

Expand Down Expand Up @@ -435,6 +436,7 @@ export default class WalletService {
this.setCurrent(newWallet.id)

await AddressService.deleteByWalletId(existingWalletId)
await SyncProgressService.deleteWalletSyncProgress(existingWalletId)

const newWallets = wallets.filter(w => w.id !== existingWalletId)
this.listStore.writeSync(this.walletsKey, [...newWallets, newWallet])
Expand Down Expand Up @@ -489,6 +491,7 @@ export default class WalletService {
}

await AddressService.deleteByWalletId(id)
await SyncProgressService.deleteWalletSyncProgress(id)

this.listStore.writeSync(this.walletsKey, newWallets)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const getWalletMinLocalSavedBlockNumberMock = jest.fn()
const removeByHashesAndAddressType = jest.fn()
const getOtherTypeSyncProgressMock = jest.fn()
const getOtherTypeSyncBlockNumberMock = jest.fn()
const getExistSyncArgsesMock = jest.fn()

const setScriptsMock = jest.fn()
const getScriptsMock = jest.fn()
Expand Down Expand Up @@ -61,6 +62,7 @@ jest.mock('../../src/services/sync-progress', () => {

static getOtherTypeSyncProgress: any = () => getOtherTypeSyncProgressMock()
static getOtherTypeSyncBlockNumber: any = () => getOtherTypeSyncBlockNumberMock()
static getExistSyncArgses: any = () => getExistSyncArgsesMock()
}
})

Expand Down Expand Up @@ -134,8 +136,9 @@ describe('test light synchronizer', () => {
'partial'
)
})
it('there is not exist sync scripts with light client', async () => {
it('when syncing script in the local DB', async () => {
getScriptsMock.mockResolvedValue([{ script, blockNumber: '0xaa' }])
getExistSyncArgsesMock.mockResolvedValue(new Set([script.args]))
const addressMeta = AddressMeta.fromObject({
walletId: 'walletId',
address,
Expand Down Expand Up @@ -182,6 +185,67 @@ describe('test light synchronizer', () => {
])
expect(updateSyncProgressFlagMock).toBeCalledWith(['walletId'])
})
it.only('when syncing script not in the local DB', async () => {
getScriptsMock.mockResolvedValue([{ script, blockNumber: '0xaa' }])
getExistSyncArgsesMock.mockResolvedValue(new Set())
const addressMeta = AddressMeta.fromObject({
walletId: 'walletId',
address,
path: '',
addressIndex: 10,
addressType: 0,
blake160: script.args,
})
const connect = new LightSynchronizer([addressMeta], '')
//@ts-ignore
await connect.initSyncProgress()
expect(setScriptsMock).toHaveBeenNthCalledWith(
1,
[
{
script: addressMeta.generateDefaultLockScript().toSDK(),
scriptType: 'lock',
walletId: 'walletId',
blockNumber: '0x0',
},
{
script: addressMeta.generateACPLockScript().toSDK(),
scriptType: 'lock',
walletId: 'walletId',
blockNumber: '0x0',
},
{
script: addressMeta.generateLegacyACPLockScript().toSDK(),
scriptType: 'lock',
walletId: 'walletId',
blockNumber: '0x0',
},
],
'partial'
)
expect(setScriptsMock).toHaveBeenLastCalledWith([], 'delete')
expect(initSyncProgressMock).toBeCalledWith([
{
script: addressMeta.generateDefaultLockScript().toSDK(),
scriptType: 'lock',
walletId: 'walletId',
blockNumber: '0x0',
},
{
script: addressMeta.generateACPLockScript().toSDK(),
scriptType: 'lock',
walletId: 'walletId',
blockNumber: '0x0',
},
{
script: addressMeta.generateLegacyACPLockScript().toSDK(),
scriptType: 'lock',
walletId: 'walletId',
blockNumber: '0x0',
},
])
expect(updateSyncProgressFlagMock).toBeCalledWith(['walletId'])
})
it('set new script with the synced min block number', async () => {
getScriptsMock.mockResolvedValue([])
const addressMeta = AddressMeta.fromObject({
Expand Down

1 comment on commit a99ed75

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Packaging for test is done in 8965897356

Please sign in to comment.