Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove old indexer data and fix typo. #2565

Merged
merged 4 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions packages/neuron-wallet/src/controllers/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import SyncApiController from 'controllers/sync-api'
import { SETTINGS_WINDOW_TITLE } from 'utils/const'
import { stopCkbNode } from 'services/ckb-runner'
import startMonitor from 'services/monitor'
import IndexerService from 'services/indexer'

const app = electronApp

Expand Down Expand Up @@ -52,6 +53,8 @@ export default class AppController {
await this.openWindow()

startMonitor()

IndexerService.cleanOldIndexerData()
}

/**
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
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)
})
})
})