-
Notifications
You must be signed in to change notification settings - Fork 294
Expand file tree
/
Copy pathsetup.js
More file actions
200 lines (187 loc) · 5.42 KB
/
setup.js
File metadata and controls
200 lines (187 loc) · 5.42 KB
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
// @flow
import { BrowserWindow } from 'electron';
import { createWriteStream, readFileSync } from 'fs';
import { exec, spawn } from 'child_process';
import { CardanoNode } from './CardanoNode';
import { exportWallets } from './utils';
import {
NODE_KILL_TIMEOUT,
NODE_SHUTDOWN_TIMEOUT,
NODE_STARTUP_MAX_RETRIES,
NODE_STARTUP_TIMEOUT,
NODE_UPDATE_TIMEOUT,
} from '../config';
import { logger } from '../utils/logging';
import type { LauncherConfig } from '../config';
import type {
CardanoNodeState,
CardanoStatus,
TlsConfig,
} from '../../common/types/cardano-node.types';
import type { ExportWalletsRendererRequest } from '../../common/ipc/api';
import {
cardanoAwaitUpdateChannel,
cardanoFaultInjectionChannel,
cardanoRestartChannel,
cardanoStateChangeChannel,
getCachedCardanoStatusChannel,
cardanoTlsConfigChannel,
setCachedCardanoStatusChannel,
exportWalletsChannel,
} from '../ipc/cardano.ipc';
import { safeExitWithCode } from '../utils/safeExitWithCode';
const startCardanoNode = (
node: CardanoNode,
launcherConfig: LauncherConfig
) => {
const {
logsPrefix,
nodeImplementation,
nodeConfig,
tlsPath,
stateDir,
cluster,
block0Path,
block0Hash,
secretPath,
configPath,
syncTolerance,
cliBin,
} = launcherConfig;
const logFilePath = `${logsPrefix}/pub/`;
const config = {
logFilePath,
nodeImplementation,
nodeConfig,
tlsPath,
stateDir,
cluster,
block0Path,
block0Hash,
secretPath,
configPath,
syncTolerance,
cliBin,
startupTimeout: NODE_STARTUP_TIMEOUT,
startupMaxRetries: NODE_STARTUP_MAX_RETRIES,
shutdownTimeout: NODE_SHUTDOWN_TIMEOUT,
killTimeout: NODE_KILL_TIMEOUT,
updateTimeout: NODE_UPDATE_TIMEOUT,
};
return node.start(config);
};
const restartCardanoNode = async (node: CardanoNode) => {
try {
await node.restart();
} catch (error) {
logger.error('Could not restart CardanoNode', { error });
}
};
/**
* Configures, starts and manages the CardanoNode responding to node
* state changes, app events and IPC messages coming from the renderer.
*
* @param launcherConfig {LauncherConfig}
* @param mainWindow
*/
export const setupCardanoNode = (
launcherConfig: LauncherConfig,
mainWindow: BrowserWindow,
locale: string
): CardanoNode => {
const cardanoNode = new CardanoNode(
logger,
{
// Dependencies on node.js apis are passed as props to ease testing
spawn,
exec,
readFileSync,
createWriteStream,
broadcastTlsConfig: (config: ?TlsConfig) => {
if (!mainWindow.isDestroyed())
cardanoTlsConfigChannel.send(config, mainWindow);
},
broadcastStateChange: (state: CardanoNodeState) => {
if (!mainWindow.isDestroyed())
cardanoStateChangeChannel.send(state, mainWindow);
},
},
{
// CardanoNode lifecycle hooks
onStarting: () => {},
onRunning: () => {},
onStopping: () => {},
onStopped: () => {},
onUpdating: () => {},
onUpdated: () => {},
onCrashed: code => {
const restartTimeout = cardanoNode.startupTries > 0 ? 30000 : 0;
logger.info(
`CardanoNode crashed with code ${code}. Restarting in ${restartTimeout}ms...`,
{ code, restartTimeout }
);
setTimeout(() => restartCardanoNode(cardanoNode), restartTimeout);
},
onError: () => {},
onUnrecoverable: () => {},
}
);
startCardanoNode(cardanoNode, launcherConfig);
getCachedCardanoStatusChannel.onRequest(() => {
logger.info('ipcMain: Received request from renderer for cardano status', {
status: cardanoNode.status,
});
return Promise.resolve(cardanoNode.status);
});
setCachedCardanoStatusChannel.onReceive((status: ?CardanoStatus) => {
logger.info(
'ipcMain: Received request from renderer to cache cardano status',
{ status }
);
cardanoNode.saveStatus(status);
return Promise.resolve();
});
cardanoStateChangeChannel.onRequest(() => {
logger.info('ipcMain: Received request from renderer for node state', {
state: cardanoNode.state,
});
return Promise.resolve(cardanoNode.state);
});
cardanoTlsConfigChannel.onRequest(() => {
logger.info('ipcMain: Received request from renderer for tls config');
return Promise.resolve(cardanoNode.tlsConfig);
});
cardanoAwaitUpdateChannel.onReceive(() => {
logger.info('ipcMain: Received request from renderer to await update');
setTimeout(async () => {
await cardanoNode.expectNodeUpdate();
logger.info(
'CardanoNode applied an update. Exiting Daedalus with code 20.'
);
safeExitWithCode(20);
});
return Promise.resolve();
});
cardanoRestartChannel.onReceive(() => {
logger.info('ipcMain: Received request from renderer to restart node');
return cardanoNode.restart(true); // forced restart
});
cardanoFaultInjectionChannel.onReceive(fault => {
logger.info(
'ipcMain: Received request to inject a fault into cardano node',
{ fault }
);
return cardanoNode.setFault(fault);
});
exportWalletsChannel.onRequest(
({ exportSourcePath }: ExportWalletsRendererRequest) => {
logger.info('ipcMain: Received request from renderer to export wallets', {
exportSourcePath,
});
return Promise.resolve(
exportWallets(exportSourcePath, launcherConfig, mainWindow, locale)
);
}
);
return cardanoNode;
};