Skip to content

Commit

Permalink
fix: Save sync_progress with chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu committed Jan 17, 2024
1 parent 27f2a37 commit c8313be
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export default class LightSynchronizer extends Synchronizer {
const deleteScript = syncScripts.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.resetSyncProgress(addScripts)
await SyncProgressService.initSyncProgress(addScripts)
await SyncProgressService.updateSyncProgressFlag(walletIds)
await SyncProgressService.removeByHashesAndAddressType(
SyncAddressType.Multisig,
Expand Down
16 changes: 9 additions & 7 deletions packages/neuron-wallet/src/services/sync-progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SyncProgress, { SyncAddressType } from '../database/chain/entities/sync-p
import WalletService from './wallets'

export default class SyncProgressService {
static async resetSyncProgress(
static async initSyncProgress(
params: {
script: CKBComponents.Script
scriptType: CKBRPC.ScriptType
Expand All @@ -13,13 +13,15 @@ export default class SyncProgressService {
blockNumber: string
}[]
) {
await getConnection()
const syncProgresses = params.map(v => SyncProgress.fromObject(v))
const existProgresses = await getConnection()
.getRepository(SyncProgress)
.createQueryBuilder()
.insert()
.orIgnore()
.values(params.map(v => SyncProgress.fromObject(v)))
.execute()
.find({
select: ['hash'],
})
const existHashes = new Set(existProgresses.map(v => v.hash))
const newSyncProgreses = syncProgresses.filter(v => !existHashes.has(v.hash))
await getConnection().manager.save(newSyncProgreses, { chunk: 100 })
}

static async updateSyncProgressFlag(existWalletIds: string[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import AddressMeta from '../../src/database/address/meta'
const getSyncStatusMock = jest.fn()
const getCurrentWalletMinSyncedBlockNumberMock = jest.fn()
const getAllSyncStatusToMapMock = jest.fn()
const resetSyncProgressMock = jest.fn()
const initSyncProgressMock = jest.fn()
const updateSyncStatusMock = jest.fn()
const updateSyncProgressFlagMock = jest.fn()
const getWalletMinLocalSavedBlockNumberMock = jest.fn()
Expand All @@ -28,7 +28,7 @@ function mockReset() {
getSyncStatusMock.mockReset()
getCurrentWalletMinSyncedBlockNumberMock.mockReset()
getAllSyncStatusToMapMock.mockReset()
resetSyncProgressMock.mockReset()
initSyncProgressMock.mockReset()
updateSyncStatusMock.mockReset()
getWalletMinLocalSavedBlockNumberMock.mockReset()
getOtherTypeSyncProgressMock.mockReset()
Expand All @@ -52,7 +52,7 @@ jest.mock('../../src/services/sync-progress', () => {
static getSyncStatus: any = () => getSyncStatusMock()
static getCurrentWalletMinSyncedBlockNumber: any = () => getCurrentWalletMinSyncedBlockNumberMock()
static getAllSyncStatusToMap: any = () => getAllSyncStatusToMapMock()
static resetSyncProgress: any = (arg: any) => resetSyncProgressMock(arg)
static initSyncProgress: any = (arg: any) => initSyncProgressMock(arg)
static updateSyncStatus: any = (hash: string, update: any) => updateSyncStatusMock(hash, update)
static updateSyncProgressFlag: any = (walletIds: string[]) => updateSyncProgressFlagMock(walletIds)
static getWalletMinLocalSavedBlockNumber: any = () => getWalletMinLocalSavedBlockNumberMock()
Expand Down Expand Up @@ -166,7 +166,7 @@ describe('test light synchronizer', () => {
'partial'
)
expect(setScriptsMock).toHaveBeenLastCalledWith([], 'delete')
expect(resetSyncProgressMock).toBeCalledWith([
expect(initSyncProgressMock).toBeCalledWith([
{
script: addressMeta.generateACPLockScript().toSDK(),
scriptType: 'lock',
Expand Down

1 comment on commit c8313be

@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 7549263100

Please sign in to comment.