-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathnode.ts
295 lines (269 loc) · 10.1 KB
/
node.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import fs from 'fs'
import path from 'path'
import { BI } from '@ckb-lumos/lumos'
import { app as electronApp, dialog, shell, app } from 'electron'
import { t } from 'i18next'
import { interval, BehaviorSubject, merge, Subject } from 'rxjs'
import { distinctUntilChanged, sampleTime, flatMap, delay, retry, debounceTime } from 'rxjs/operators'
import env from '../env'
import { ConnectionStatusSubject } from '../models/subjects/node'
import { CurrentNetworkIDSubject } from '../models/subjects/networks'
import { NetworkType } from '../models/network'
import NetworksService from '../services/networks'
import RpcService from '../services/rpc-service'
import { startCkbNode, stopCkbNode } from '../services/ckb-runner'
import { START_WITHOUT_INDEXER } from '../utils/const'
import logger from '../utils/logger'
import redistCheck from '../utils/redist-check'
import { rpcRequest } from '../utils/rpc-request'
import { generateRPC } from '../utils/ckb-rpc'
import startMonitor, { stopMonitor } from './monitor'
import { CKBLightRunner } from './light-runner'
import SettingsService from './settings'
export enum VerifyCkbVersionResult {
Same,
Compatible,
ShouldUpdate,
Incompatible,
}
type CKBNodeType = 'full' | 'light'
class NodeService {
private static instance: NodeService
static getInstance(): NodeService {
if (!NodeService.instance) {
NodeService.instance = new NodeService()
}
return NodeService.instance
}
public delayTime = 0
public intervalTime = 1000
public tipNumberSubject = new BehaviorSubject<string>('0')
public connectionStatusSubject = new BehaviorSubject<boolean>(false)
#startNodeSubject = new Subject<void>()
private _tipBlockNumber: string = '0'
#startedBundledNode: boolean = false
get startedBundledNode() {
return this.#startedBundledNode
}
private constructor() {
this.start()
this.syncConnectionStatus()
CurrentNetworkIDSubject.subscribe(this.whenNetworkUpdate)
this.#startNodeSubject.pipe(debounceTime(1000)).subscribe(this.startNode)
}
public get tipBlockNumber(): string {
return this._tipBlockNumber
}
public syncConnectionStatus = () => {
const periodSync = this.connectionStatusSubject.pipe(sampleTime(10000))
const realtimeSync = this.connectionStatusSubject.pipe(distinctUntilChanged())
merge(periodSync, realtimeSync)
.pipe(debounceTime(500))
.subscribe(connected => {
const currentNetwork = NetworksService.getInstance().getCurrent()
const isBundledNode = currentNetwork.readonly
ConnectionStatusSubject.next({
url: currentNetwork.remote,
connected,
isBundledNode,
startedBundledNode: isBundledNode ? this.#startedBundledNode : false,
})
})
}
private whenNetworkUpdate = () => {
this.stop?.()
this.#startedBundledNode = false
this.tipNumberSubject.next('0')
this.connectionStatusSubject.next(false)
}
public start = () => {
this.stop?.()
const subscribe = this.tipNumber()
this.stop = subscribe.unsubscribe.bind(subscribe)
}
public stop?: () => void
public tipNumber = () => {
return interval(this.intervalTime)
.pipe(
delay(this.delayTime),
flatMap(() => {
const currentNetwork = NetworksService.getInstance().getCurrent()
return generateRPC(currentNetwork.remote, currentNetwork.type)
.getTipBlockNumber()
.then(tipNumber => {
this.connectionStatusSubject.next(true)
return tipNumber
})
.catch(err => {
this.connectionStatusSubject.next(false)
throw err
})
}),
retry(3),
distinctUntilChanged()
)
.subscribe(
tipNumber => {
if (!this.delayTime) {
this.delayTime = 0
}
const tip: string = BI.from(tipNumber).toString()
this._tipBlockNumber = tip
this.tipNumberSubject.next(tip)
},
() => {
if (this.delayTime < 10 * this.intervalTime) {
this.delayTime = 2 * this.intervalTime
}
this.start()
}
)
}
public async tryStartNodeOnDefaultURI() {
await stopMonitor('ckb')
const isDefaultCKBNeedStart = await this.isDefaultCKBNeedRestart()
if (isDefaultCKBNeedStart) {
const currentNetwork = NetworksService.getInstance().getCurrent()
if (SettingsService.getInstance().isFirstSync && currentNetwork.type === NetworkType.Default) {
logger.info("CKB:\tThis is the first sync, please wait for the user's confirmation")
return
}
logger.info('CKB:\texternal RPC on default uri not detected, starting bundled CKB node.')
const redistReady = await redistCheck()
await (redistReady ? this.#startNodeSubject.next() : this.showGuideDialog())
await startMonitor()
} else {
logger.info('CKB:\tThe internal node is not selected so starting the bundled CKB node is skipped')
}
this.start()
}
public async verifyExternalCkbNode() {
logger.info('CKB:\tstart verify external ckb node')
const network = NetworksService.getInstance().getCurrent()
if (!network.readonly) {
const localNodeInfo = await new RpcService(network.remote, network.type).localNodeInfo()
const type: CKBNodeType = network.type === NetworkType.Light ? 'light' : 'full'
const internalNodeVersion = this.getInternalNodeVersion(type)
const neuronVersion = app.getVersion()
if (!internalNodeVersion || !localNodeInfo.version) return
return {
ckbIsCompatible: this.isCkbCompatibility(neuronVersion, localNodeInfo.version, type),
withIndexer: await this.isStartWithIndexer(),
shouldUpdate: this.verifyCKbNodeShouldUpdate(internalNodeVersion, localNodeInfo.version),
}
}
}
public async isDefaultCKBNeedRestart() {
const network = NetworksService.getInstance().getCurrent()
if (!network.readonly) {
return false
}
try {
await new RpcService(network.remote, network.type).localNodeInfo()
return false
} catch (err) {
return true
}
}
public startNode = async () => {
try {
const network = NetworksService.getInstance().getCurrent()
if (network.type === NetworkType.Light) {
await stopCkbNode()
await CKBLightRunner.getInstance().start()
} else {
await CKBLightRunner.getInstance().stop()
await startCkbNode()
}
this.#startedBundledNode = true
} catch (error) {
this.#startedBundledNode = false
logger.info('CKB:\tfail to start bundled CKB with error:')
logger.error(error)
}
}
public async startNodeIgnoreExternal() {
logger.info('CKB:\tignore running external node, and start node with another port')
await stopMonitor('ckb')
this.start()
const redistReady = await redistCheck()
await (redistReady ? this.startNode() : this.showGuideDialog())
await startMonitor()
}
private showGuideDialog = () => {
const I18N_PATH = `messageBox.ckb-dependency`
return dialog
.showMessageBox({
type: 'info',
buttons: ['install-and-exit'].map(label => t(`${I18N_PATH}.buttons.${label}`)),
defaultId: 0,
title: t(`${I18N_PATH}.title`),
message: t(`${I18N_PATH}.message`),
detail: t(`${I18N_PATH}.detail`),
cancelId: 0,
noLink: true,
})
.then(() => {
const VC_REDIST_URL = `https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads`
shell.openExternal(VC_REDIST_URL)
env.app.quit()
return false
})
}
private getInternalNodeVersion(type: CKBNodeType) {
const appPath = electronApp.isPackaged ? electronApp.getAppPath() : path.join(__dirname, '../../../..')
const ckbVersionPath = path.join(appPath, type === 'light' ? '.ckb-light-version' : '.ckb-version')
if (fs.existsSync(ckbVersionPath)) {
try {
return fs.readFileSync(ckbVersionPath, 'utf8')?.split('\n')?.[0]?.slice(1)
} catch (err) {
logger.error('App\t: get ckb node version failed')
}
}
}
private getNeuronCompatibilityCKB() {
const appPath = electronApp.isPackaged ? electronApp.getAppPath() : path.join(__dirname, '../../../..')
const compatiblePath = path.join(appPath, 'compatible.json')
if (fs.existsSync(compatiblePath)) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const content = require(compatiblePath)
return (content?.compatible ?? {}) as Record<
string,
{
full: string[]
light: string[]
}
>
} catch (err) {
logger.error('App\t: get compatible failed', err)
}
}
}
private isCkbCompatibility(neuronVersion: string, externalCKBVersion: string, type: CKBNodeType = 'full') {
const compatibilities = this.getNeuronCompatibilityCKB()
const neuronCompatibleVersion = neuronVersion.split('.').slice(0, 2).join('.')
const externalCKBCompatibleVersion = externalCKBVersion.split('.').slice(0, 2).join('.')
return compatibilities?.[neuronCompatibleVersion]?.[type]?.includes(externalCKBCompatibleVersion)
}
private verifyCKbNodeShouldUpdate(neuronCKBVersion: string, externalCKBVersion: string) {
const [internalMajor, internalMinor] = neuronCKBVersion.split('.')?.map(v => +v) ?? []
const [externalMajor, externalMinor] = externalCKBVersion.split('.')?.map(v => +v) ?? []
return internalMajor < externalMajor || (internalMajor === externalMajor && internalMinor < externalMinor)
}
private async isStartWithIndexer() {
const network = NetworksService.getInstance().getCurrent()
try {
const res = await rpcRequest<{ error?: { code: number } }>(network.remote, { method: 'get_indexer_tip' })
if (res.error?.code === START_WITHOUT_INDEXER) {
logger.info('Node:\tthe ckb node does not start with --indexer')
return false
}
return true
} catch (error) {
logger.info('Node:\tcalling get_indexer_tip failed')
return false
}
}
}
export default NodeService