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

feat: Export the start block number and slice the light client log. #3144

Merged
merged 2 commits into from
May 10, 2024
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
60 changes: 37 additions & 23 deletions packages/neuron-wallet/src/controllers/export-debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import SettingsService from '../services/settings'
import { generateRPC } from '../utils/ckb-rpc'
import { CKBLightRunner } from '../services/light-runner'
import { LIGHT_CLIENT_MAINNET, LIGHT_CLIENT_TESTNET } from '../utils/const'
import WalletsService from '../services/wallets'

export default class ExportDebugController {
#I18N_PATH = 'export-debug-info'
Expand Down Expand Up @@ -78,6 +79,7 @@ export default class ExportDebugController {
])
const { platform, arch } = process
const release = os.release()
const wallets = WalletsService.getInstance().getAll()
const status = {
neuron: {
version: neuronVersion,
Expand All @@ -95,30 +97,30 @@ export default class ExportDebugController {
release,
vcredist,
},
wallets: wallets.map((v, idx) => ({
idx,
startBlockNumber: v.startBlockNumber,
})),
}
this.archive.append(JSON.stringify(status), {
name: 'status.json',
})
}

private addBundledCKBLog = () => {
const name = 'bundled-ckb.log'
const SIZE_TO_READ = 32_000

return new Promise((resolve, reject) => {
const logPath = path.resolve(SettingsService.getInstance().getNodeDataPath(), 'data', 'logs', 'run.log')
if (!fs.existsSync(logPath)) {
private readLastSizeOfFile(filepath: string, size = 32_000) {
return new Promise<string>((resolve, reject) => {
if (!fs.existsSync(filepath)) {
return reject(new Error('File not found'))
}

const fileStats = fs.statSync(logPath)
const position = fileStats.size - SIZE_TO_READ
const fileStats = fs.statSync(filepath)
const position = fileStats.size - size

fs.open(logPath, 'r', (openErr, fd) => {
fs.open(filepath, 'r', (openErr, fd) => {
if (openErr) {
return reject(openErr)
}
fs.read(fd, Buffer.alloc(SIZE_TO_READ), 0, SIZE_TO_READ, position, (readErr, _, buffer) => {
fs.read(fd, Buffer.alloc(size), 0, size, position, (readErr, _, buffer) => {
fs.close(fd, closeErr => {
const err = closeErr || readErr
if (err) {
Expand All @@ -129,20 +131,30 @@ export default class ExportDebugController {
})
})
})
.then((log: string) => {
this.archive.append(log, { name })
})
.catch(err => {
this.archive.append(err.message, { name })
})
}

private addBundledCKBLog = async () => {
const name = 'bundled-ckb.log'
try {
const logPath = path.resolve(SettingsService.getInstance().getNodeDataPath(), 'data', 'logs', 'run.log')
const log = await this.readLastSizeOfFile(logPath)
this.archive.append(log, { name })
} catch (error) {
this.archive.append(error.message, { name })
}
}

private async addHdPublicKeyInfoCsv() {
try {
const addressMetas = await AddressService.getAddressesByAllWallets()
let csv = 'walletId,addressType,addressIndex,publicKeyInBlake160\n'
const wallets = WalletsService.getInstance().getAll()
const idToIdx = new Map<string, number>()
wallets.forEach((v, idx) => idToIdx.set(v.id, idx))
let csv = 'index,addressType,addressIndex,publicKeyInBlake160\n'
for (const addressMeta of addressMetas) {
const row = `${addressMeta.walletId},${addressMeta.addressType},${addressMeta.addressIndex},${addressMeta.blake160}\n`
const row = `${idToIdx.get(addressMeta.walletId) ?? addressMeta.walletId},${addressMeta.addressType},${
addressMeta.addressIndex
},${addressMeta.blake160}\n`
csv += row
}
const csvFileName = 'hd_public_key_info.csv'
Expand All @@ -162,14 +174,16 @@ export default class ExportDebugController {
})
}

private addBundledCKBLightClientLog() {
private async addBundledCKBLightClientLog() {
const mainnetLogPath = CKBLightRunner.getInstance().getLogPath(LIGHT_CLIENT_MAINNET)
const testnetLogPath = CKBLightRunner.getInstance().getLogPath(LIGHT_CLIENT_TESTNET)
if (fs.existsSync(mainnetLogPath)) {
this.archive.file(mainnetLogPath, { name: 'bundled-ckb-lignt-client-mainnet.log' })
const mainnetLog = await this.readLastSizeOfFile(mainnetLogPath)
this.archive.append(mainnetLog, { name: 'bundled-ckb-lignt-client-mainnet.log' })
}
const testnetLogPath = CKBLightRunner.getInstance().getLogPath(LIGHT_CLIENT_TESTNET)
if (fs.existsSync(testnetLogPath)) {
this.archive.file(testnetLogPath, { name: 'bundled-ckb-lignt-client-testnet.log' })
const testnetLog = await this.readLastSizeOfFile(testnetLogPath)
this.archive.append(testnetLog, { name: 'bundled-ckb-lignt-client-testnet.log' })
}
}
}
16 changes: 14 additions & 2 deletions packages/neuron-wallet/tests/controllers/export-debug.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jest.mock('fs', () => {
createWriteStream: () => null,
readFileSync: () => JSON.stringify({}),
writeFileSync: () => jest.fn(),
existsSync: () => jest.fn(),
existsSync: () => jest.fn()(),
}
})

Expand Down Expand Up @@ -88,6 +88,18 @@ jest.mock('../../src/services/light-runner', () => {
}
})

jest.mock('../../src/services/wallets', () => {
return {
getInstance() {
return {
getAll() {
return []
},
}
},
}
})

import { dialog } from 'electron'
import logger from '../../src/utils/logger'
import ExportDebugController from '../../src/controllers/export-debug'
Expand Down Expand Up @@ -148,7 +160,7 @@ describe('Test ExportDebugController', () => {
expect(showErrorBoxMock).not.toHaveBeenCalled()
expect(logger.error).not.toHaveBeenCalled()

const csv = ['walletId,addressType,addressIndex,publicKeyInBlake160\n', '0,0,0,hash1\n', '1,1,1,hash2\n'].join('')
const csv = ['index,addressType,addressIndex,publicKeyInBlake160\n', '0,0,0,hash1\n', '1,1,1,hash2\n'].join('')
expect(archiveAppendMock).toHaveBeenCalledWith(csv, expect.objectContaining({ name: 'hd_public_key_info.csv' }))
})
})
Expand Down
Loading