Skip to content

Commit

Permalink
fix: Remove old indexer data and fix typo. (nervosnetwork#2565)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanguoyu authored Feb 10, 2023
1 parent a0887b6 commit c46fe25
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/neuron-ui/src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions packages/neuron-wallet/src/services/ckb-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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...`)
Expand Down Expand Up @@ -118,6 +127,8 @@ export const startCkbNode = async () => {
ckb = null
})
resetSyncTaskQueue.push(true)

removeOldIndexerIfRunSuccess()
}

export const stopCkbNode = () => {
Expand Down Expand Up @@ -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 })
}
Expand Down
9 changes: 9 additions & 0 deletions packages/neuron-wallet/src/services/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down
8 changes: 8 additions & 0 deletions packages/neuron-wallet/src/services/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions packages/neuron-wallet/tests/services/ckb-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
75 changes: 75 additions & 0 deletions packages/neuron-wallet/tests/services/indexer.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
})
})

0 comments on commit c46fe25

Please sign in to comment.