Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions packages/neuron-wallet/src/controllers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,11 +481,10 @@ export default class ApiController {
}
}
SettingsService.getInstance().ckbDataPath = dataPath
await stopMonitor('ckb-indexer')
await startMonitor('ckb', true)
if (finallyClearCache) {
await IndexerService.clearCache(true)
await IndexerService.clearCache(true, true)
}
startMonitor(undefined, true)
return {
status: ResponseCode.Success,
result: SettingsService.getInstance().ckbDataPath
Expand Down
21 changes: 11 additions & 10 deletions packages/neuron-wallet/src/services/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { ChildProcess, spawn } from 'child_process'
import process from 'process'
import { dialog } from 'electron'
import logger from 'utils/logger'
import { Network } from 'models/network'
import { MAINNET_GENESIS_HASH, Network } from 'models/network'
import SyncedBlockNumber from 'models/synced-block-number'
import NetworksService from './networks'
import CommonUtils from 'utils/common'
import { resetSyncTaskQueue } from 'block-sync-renderer'
import { clean as cleanChain } from 'database/chain'
import SettingsService from './settings'
import startMonitor, { stopMonitor } from './monitor'

const platform = (): string => {
switch (process.platform) {
Expand Down Expand Up @@ -68,16 +68,16 @@ export default class IndexerService {
await IndexerService.ensurePortUsable()
}

static clearCache = async (clearIndexerFolder = false) => {
await resetSyncTaskQueue.asyncPush(false)
static clearCache = async (clearIndexerFolder = false, forceClearMainnet?: boolean) => {
await stopMonitor('ckb-indexer')
await cleanChain()

if (clearIndexerFolder) {
IndexerService.getInstance().clearData()
IndexerService.getInstance().clearData(forceClearMainnet)
await new SyncedBlockNumber().setNextBlock(BigInt(0))
}

await resetSyncTaskQueue.asyncPush(true)
await startMonitor('ckb-indexer')
}

static createFolder(dir: string) {
Expand Down Expand Up @@ -142,6 +142,7 @@ export default class IndexerService {
try {
const bin = IndexerService.getBinary()
const params = ['-c', network.remote, '-s', dataPath, '-l', `127.0.0.1:${IndexerService.PORT}`]
logger.info(params)
const indexer = spawn(bin, params)
this.indexer = indexer
logger.info(`Indexer:\tstart: PORT: ${IndexerService.PORT}...`)
Expand Down Expand Up @@ -176,9 +177,9 @@ export default class IndexerService {
})
}

clearData = () => {
clearData = (forceClearMainnet?: boolean) => {
const network = NetworksService.getInstance().getCurrent()
const dataPath = this.#getDataPath(network)
const dataPath = this.#getDataPath(network, forceClearMainnet)
logger.debug(`Removing data ${dataPath}`)
fs.rmSync(dataPath, { recursive: true, force: true })

Expand All @@ -188,12 +189,12 @@ export default class IndexerService {



#getDataPath = (network: Network): string => {
#getDataPath = (network: Network, forceClearMainnet?: boolean): string => {
let indexerDataPath = SettingsService.getInstance().indexerDataPath
if (!indexerDataPath) {
indexerDataPath = path.resolve(env.fileBasePath, IndexerService.indexerDataFolder, 'data')
SettingsService.getInstance().indexerDataPath = indexerDataPath
}
return path.resolve(indexerDataPath, `${network.genesisHash}`)
return path.resolve(indexerDataPath, `${forceClearMainnet ? MAINNET_GENESIS_HASH : network.genesisHash}`)
}
}
13 changes: 7 additions & 6 deletions packages/neuron-wallet/src/services/monitor/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default abstract class Monitor {
if (this.isReStarting) {
return
}
const timeout = timer(intervalTime / 2).pipe(map(() => true))
const timeout = timer(intervalTime / 2).pipe(map(() => false))
const isLiving = await race(timeout, from(this.isLiving())).toPromise()
if (!isLiving) {
logger.info(`Monitor: is restarting ${this.name} process`)
Expand All @@ -35,18 +35,19 @@ export default abstract class Monitor {
}
}

startMonitor(intervalTime: number = 10000, startNow: boolean = false) {
async startMonitor(intervalTime: number = 10000, startNow: boolean = false) {
this.interval = interval(intervalTime)
if (startNow) {
this.monitor(intervalTime)
}
if (!this.subcription?.closed) {
this.subcription?.unsubscribe()
}
this.subcription = this.interval.subscribe(async () => this.monitor(intervalTime))
if (startNow) {
await this.monitor(intervalTime)
}
}

clearMonitor() {
async stopMonitor() {
this.subcription?.unsubscribe()
await this.stop()
}
}
18 changes: 4 additions & 14 deletions packages/neuron-wallet/src/services/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@ import CkbMonitor from './ckb-monitor'

const monitors: Base[] = []

export default function startMonitor(name?: string, startNow?: boolean) {
export default async function startMonitor(name?: string, startNow?: boolean) {
if (!monitors.length) {
monitors.push(new CkbIndexerMonitor(), new CkbMonitor())
}
monitors
.filter(v => !name || v.name === name)
.forEach((v: Base) => {
v.startMonitor(undefined, startNow)
})
const filterMonitors = monitors.filter(v => !name || v.name === name)
await Promise.all(filterMonitors.map((v: Base) => v.startMonitor(undefined, startNow)))
}

export async function stopMonitor(name?: string) {
await Promise.all(
monitors
.filter(v => !name || v.name === name)
.map(v => {
v.clearMonitor()
return v.stop()
})
)
await Promise.all(monitors.filter(v => !name || v.name === name).map(v => v.stopMonitor()))
}
11 changes: 4 additions & 7 deletions packages/neuron-wallet/tests/services/monitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,29 +107,29 @@ describe('base monitor', () => {
})

describe('start monitor', () => {
afterEach(async () => {
await monitor.stopMonitor()
})
it('is living', async () => {
isLivingMock.mockResolvedValue(true)
monitor.startMonitor(100)
await wait(200)
expect(isLivingMock).toHaveBeenCalled()
expect(restartMock).toHaveBeenCalledTimes(0)
monitor.clearMonitor()
})
it('not living', async () => {
isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false)
monitor.startMonitor(100)
await wait(200)
expect(isLivingMock).toHaveBeenCalled()
expect(restartMock).toHaveBeenCalled()
monitor.clearMonitor()
})
it('isLiving timeout', async () => {
isLivingMock.mockImplementation(() => wait(200))
monitor.startMonitor(100)
await wait(200)
expect(isLivingMock).toHaveBeenCalled()
expect(restartMock).toHaveBeenCalledTimes(0)
monitor.clearMonitor()
expect(restartMock).toHaveBeenCalledTimes(1)
})
it('not living wait restart', async () => {
isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false)
Expand All @@ -138,15 +138,13 @@ describe('base monitor', () => {
await wait(800)
expect(isLivingMock).toHaveBeenCalled()
expect(restartMock).toHaveBeenCalledTimes(1)
monitor.clearMonitor()
})
it('start monitor with first', async () => {
isLivingMock.mockResolvedValue(true).mockResolvedValueOnce(false)
monitor.startMonitor(1000, true)
await wait(500)
expect(isLivingMock).toHaveBeenCalled()
expect(restartMock).toHaveBeenCalled()
monitor.clearMonitor()
})
it('twice start monitor', async () => {
isLivingMock.mockReset()
Expand All @@ -155,7 +153,6 @@ describe('base monitor', () => {
monitor.startMonitor(500)
await wait(800)
expect(isLivingMock).toHaveBeenCalledTimes(1)
monitor.clearMonitor()
})
})
})
Expand Down