diff --git a/packages/neuron-ui/src/locales/en.json b/packages/neuron-ui/src/locales/en.json index 283ba5aa61..a14ac6c820 100644 --- a/packages/neuron-ui/src/locales/en.json +++ b/packages/neuron-ui/src/locales/en.json @@ -506,7 +506,7 @@ "experimental-message-hardware": "This is an experimental feature. Please pay attention to the risk and use with caution.", "experimental-message": "This is an experimental feature, it could change at any time. Please use with caution.", "rebuild-sync": "For better user experience, Neuron has adopted a new storage, which requires a migrating of data (estimated 20 ~ 60min).\nSorry for the inconvenience.", - "migrate": "Migrating", + "migrate": "Migrate", "secp256k1/blake160-address-required": "Secp256k1/blake160 address is required", "fields": { "wallet": "Wallet", diff --git a/packages/neuron-wallet/src/services/ckb-runner.ts b/packages/neuron-wallet/src/services/ckb-runner.ts index ba13e652dc..e8c07f814a 100644 --- a/packages/neuron-wallet/src/services/ckb-runner.ts +++ b/packages/neuron-wallet/src/services/ckb-runner.ts @@ -7,6 +7,7 @@ import logger from 'utils/logger' import SettingsService from './settings' import MigrateSubject from 'models/subjects/migrate-subject' import { resetSyncTaskQueue } from 'block-sync-renderer' +import IndexerService from './indexer' const platform = (): string => { switch (process.platform) { @@ -66,6 +67,14 @@ let isLookingValidTarget: boolean = false let lastLogTime: number export const getLookingValidTargetStatus = () => isLookingValidTarget +const removeOldIndexerIfRunSuccess = () => { + setTimeout(() => { + if (ckb !== null) { + IndexerService.cleanOldIndexerData() + } + }, 10000) +} + export const startCkbNode = async () => { if (ckb !== null) { logger.info(`CKB:\tckb is not closed, close it before start...`) @@ -118,6 +127,8 @@ export const startCkbNode = async () => { ckb = null }) resetSyncTaskQueue.push(true) + + removeOldIndexerIfRunSuccess() } export const stopCkbNode = () => { @@ -160,6 +171,7 @@ export function migrateCkbData() { logger.info(`CKB migrate:\tprocess process exited with code ${code}`) if (code === 0) { MigrateSubject.next({ type: 'finish' }) + IndexerService.cleanOldIndexerData() } else { MigrateSubject.next({ type: 'failed', reason: lastErrorData }) } diff --git a/packages/neuron-wallet/src/services/indexer.ts b/packages/neuron-wallet/src/services/indexer.ts index 0b9009d0cd..34da534354 100644 --- a/packages/neuron-wallet/src/services/indexer.ts +++ b/packages/neuron-wallet/src/services/indexer.ts @@ -29,6 +29,15 @@ export default class IndexerService { await startMonitor('ckb') } + static cleanOldIndexerData() { + const oldIndexerDataPath = SettingsService.getInstance().indexerDataPath + if (oldIndexerDataPath && fs.existsSync(oldIndexerDataPath)) { + logger.debug(`Removing old indexer data ${oldIndexerDataPath}`) + fs.rmSync(oldIndexerDataPath, { recursive: true, force: true }) + SettingsService.getInstance().indexerDataPath = '' + } + } + clearData = () => { const dataPath = this.getDataPath() logger.debug(`Removing data ${dataPath}`) diff --git a/packages/neuron-wallet/src/services/settings.ts b/packages/neuron-wallet/src/services/settings.ts index 2e0c8dde7e..dff8009fcb 100644 --- a/packages/neuron-wallet/src/services/settings.ts +++ b/packages/neuron-wallet/src/services/settings.ts @@ -24,6 +24,14 @@ export default class SettingsService extends Store { return this.readSync('locale') } + get indexerDataPath(): string { + return this.readSync('indexerDataPath') + } + + set indexerDataPath(dataPath: string) { + this.writeSync('indexerDataPath', dataPath) + } + set locale(lng: Locale) { if (locales.includes(lng)) { this.writeSync('locale', lng) diff --git a/packages/neuron-wallet/tests/services/ckb-runner.test.ts b/packages/neuron-wallet/tests/services/ckb-runner.test.ts index 866c3209a4..35d2b20e5a 100644 --- a/packages/neuron-wallet/tests/services/ckb-runner.test.ts +++ b/packages/neuron-wallet/tests/services/ckb-runner.test.ts @@ -69,6 +69,9 @@ jest.mock('../../src/block-sync-renderer', () => ({ push: jest.fn() } })) +jest.mock('../../src/services/indexer', () => ({ + cleanOldIndexerData: jest.fn() +})) const { startCkbNode, stopCkbNode, getLookingValidTargetStatus, migrateCkbData } = require('../../src/services/ckb-runner') describe('ckb runner', () => { diff --git a/packages/neuron-wallet/tests/services/indexer.test.ts b/packages/neuron-wallet/tests/services/indexer.test.ts new file mode 100644 index 0000000000..22a31c2ec6 --- /dev/null +++ b/packages/neuron-wallet/tests/services/indexer.test.ts @@ -0,0 +1,75 @@ +import IndexerService from '../../src/services/indexer' + +const existsSyncMock = jest.fn() +const rmSyncMock = jest.fn() + +jest.mock('fs', () => { + return { + existsSync: () => existsSyncMock(), + readFileSync: () => jest.fn(), + writeFileSync: () => jest.fn(), + rmSync: () => rmSyncMock() + } +}) + +const setIndexerDataPathMock = jest.fn() +const getIndexerDataPathMock = jest.fn() +jest.mock('../../src/services/settings', () => { + return class { + static getInstance() { + return { + get indexerDataPath() { + return getIndexerDataPathMock() + }, + set indexerDataPath(value: string) { + setIndexerDataPathMock(value) + } + } + } + } +}) + +jest.mock('../../src/utils/logger', () => ({ + debug: () => jest.fn() +})) + +jest.mock('../../src/models/synced-block-number', () => ({ + +})) + +jest.mock('../../src/database/chain', () => ({ + +})) + +jest.mock('../../src/services/monitor', () => ({ + +})) + +describe('test IndexerService', () => { + beforeEach(() => { + existsSyncMock.mockReset() + rmSyncMock.mockReset() + setIndexerDataPathMock.mockReset() + getIndexerDataPathMock.mockReset() + }) + describe('test remove old indexer data', () => { + it('old indexer data path exist', () => { + existsSyncMock.mockReturnValueOnce(true) + getIndexerDataPathMock.mockReturnValueOnce('indexer-path') + IndexerService.cleanOldIndexerData() + expect(rmSyncMock).toBeCalled() + expect(setIndexerDataPathMock).toBeCalledWith('') + }) + it('old indexer data not exist', () => { + existsSyncMock.mockReturnValueOnce(false) + IndexerService.cleanOldIndexerData() + expect(rmSyncMock).toBeCalledTimes(0) + }) + it('old indexer data is empty', () => { + getIndexerDataPathMock.mockReturnValueOnce('') + existsSyncMock.mockReturnValueOnce(true) + IndexerService.cleanOldIndexerData() + expect(rmSyncMock).toBeCalledTimes(0) + }) + }) +})