-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathckb-runner.ts
217 lines (193 loc) · 6.42 KB
/
ckb-runner.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import env from '../env'
import path from 'path'
import fs from 'fs'
import { ChildProcess, StdioNull, StdioPipe, spawn } from 'child_process'
import process from 'process'
import logger from '../utils/logger'
import SettingsService from './settings'
import MigrateSubject from '../models/subjects/migrate-subject'
import IndexerService from './indexer'
import { resetSyncTaskQueue } from '../block-sync-renderer'
import { getUsablePort } from '../utils/get-usable-port'
import { updateToml } from '../utils/toml'
import { BUNDLED_URL_PREFIX } from '../utils/const'
import NoDiskSpaceSubject from '../models/subjects/no-disk-space'
const platform = (): string => {
switch (process.platform) {
case 'win32':
return 'win'
case 'linux':
return 'linux'
case 'darwin':
return 'mac'
default:
return ''
}
}
const { app } = env
let ckb: ChildProcess | null = null
const ckbPath = (): string => {
return app.isPackaged ? path.join(path.dirname(app.getAppPath()), '..', './bin') : path.join(__dirname, '../../bin')
}
const ckbBinary = (): string => {
const binary = app.isPackaged ? path.resolve(ckbPath(), './ckb') : path.resolve(ckbPath(), `./${platform()}`, './ckb')
switch (platform()) {
case 'win':
return binary + '.exe'
case 'mac':
if (app.isPackaged) {
return binary
}
return `${binary}-${process.arch === 'arm64' ? 'arm64' : 'x64'}`
default:
return binary
}
}
let rpcPort: number = 8114
let listenPort: number = 8115
const initCkb = async () => {
logger.info('CKB:\tInitializing node...')
return new Promise<void>((resolve, reject) => {
if (fs.existsSync(path.join(SettingsService.getInstance().getNodeDataPath(), 'ckb.toml'))) {
logger.log('CKB:\tinit: config file detected, skip ckb init.')
return resolve()
}
const initCmd = spawn(ckbBinary(), [
'init',
'--chain',
'mainnet',
'-C',
SettingsService.getInstance().getNodeDataPath(),
])
initCmd.stderr.on('data', data => {
logger.error('CKB:\tinit fail:', data.toString())
})
initCmd.stdout.on('data', data => {
logger.log('CKB:\tinit result:', data.toString())
})
initCmd.on('error', error => {
// Mostly ckb binary is not found
logger.error('CKB:\tinit fail:', error)
reject()
})
initCmd.on('close', () => {
// `ckb init` always quits no matter it fails (usually due to config file already existing) or not.
resolve()
})
})
}
let isLookingValidTarget: boolean = false
let lastLogTime: number
export const getLookingValidTargetStatus = () => isLookingValidTarget
export const getNodeUrl = () => `${BUNDLED_URL_PREFIX}${rpcPort}`
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...`)
await stopCkbNode()
}
await initCkb()
rpcPort = await getUsablePort(rpcPort)
listenPort = await getUsablePort(rpcPort >= listenPort ? rpcPort + 1 : listenPort)
updateToml(path.join(SettingsService.getInstance().getNodeDataPath(), 'ckb.toml'), {
rpc: `listen_address = "127.0.0.1:${rpcPort}"`,
network: `listen_addresses = ["/ip4/0.0.0.0/tcp/${listenPort}"]`,
})
const options = ['run', '-C', SettingsService.getInstance().getNodeDataPath(), '--indexer']
const stdio: (StdioNull | StdioPipe)[] = ['ignore', 'pipe', 'pipe']
if (app.isPackaged && process.env.CKB_NODE_ASSUME_VALID_TARGET) {
options.push('--assume-valid-target', process.env.CKB_NODE_ASSUME_VALID_TARGET)
}
logger.info(`CKB:\tckb full node will with rpc port ${rpcPort}, listen port ${listenPort}, with options`, options)
const currentProcess = spawn(ckbBinary(), options, { stdio })
currentProcess.stderr?.on('data', data => {
const dataString: string = data.toString()
logger.error('CKB:\trun fail:', dataString)
if (dataString.includes('CKB wants to migrate the data into new format')) {
MigrateSubject.next({ type: 'need-migrate' })
}
})
currentProcess.stdout?.on('data', data => {
const dataString: string = data.toString()
if (/No space left/.test(dataString)) {
NoDiskSpaceSubject.next(true)
logger.error('CKB:\trun fail:', dataString)
return
}
if (
dataString.includes(
`can't find assume valid target temporarily, hash: Byte32(${process.env.CKB_NODE_ASSUME_VALID_TARGET})`
)
) {
isLookingValidTarget = true
lastLogTime = Date.now()
} else if (lastLogTime && Date.now() - lastLogTime > 10000) {
isLookingValidTarget = false
}
})
currentProcess.on('error', error => {
logger.error('CKB:\trun fail:', error)
isLookingValidTarget = false
if (Object.is(ckb, currentProcess)) {
ckb = null
}
})
currentProcess.on('close', code => {
logger.info(`CKB:\tprocess closed with code ${code}`)
isLookingValidTarget = false
if (Object.is(ckb, currentProcess)) {
ckb = null
}
})
ckb = currentProcess
removeOldIndexerIfRunSuccess()
}
export const stopCkbNode = () => {
return new Promise<void>(resolve => {
if (ckb) {
logger.info('CKB:\tkilling node')
ckb.once('close', () => resolve())
ckb.kill()
ckb = null
} else {
resolve()
}
})
}
/**
* remove ckb data
*/
export const clearCkbNodeCache = async () => {
await stopCkbNode()
fs.rmSync(SettingsService.getInstance().getNodeDataPath(), { recursive: true, force: true })
await startCkbNode()
resetSyncTaskQueue.asyncPush(true)
}
export function migrateCkbData() {
logger.info('CKB migrate:\tstarting...')
const options = ['migrate', '-C', SettingsService.getInstance().getNodeDataPath(), '--force']
MigrateSubject.next({ type: 'migrating' })
let migrate: ChildProcess | null = spawn(ckbBinary(), options, { stdio: ['ignore', 'pipe', 'pipe'] })
let lastErrorData = ''
migrate.stderr &&
migrate.stderr.on('data', data => {
logger.error('CKB migrate:\trun fail:', data.toString())
lastErrorData = data.toString()
})
migrate.on('close', code => {
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 })
}
migrate = null
})
}