forked from BuidlGuidl/buidlguidl-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
241 lines (206 loc) · 6.43 KB
/
index.js
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
import { exec, execSync, spawn } from "child_process";
import os from "os";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { dirname } from "path";
// import { setupDebugLogging } from "./helpers";
import { initializeMonitoring } from "./monitor.js";
import {
installMacLinuxConsensusClient,
installMacLinuxExecutionClient,
installWindowsConsensusClient,
installWindowsExecutionClient,
} from "./node_clients/install.js";
import { initializeWSConnection } from "./ws_connection/wsConnection.js";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
/// Set default command line option values
let executionClient = "geth";
let consensusClient = "prysm";
const installDir = os.homedir();
const lockFilePath = path.join(os.homedir(), "bgnode", "script.lock");
const CONFIG = {
debugLogPath: path.join(os.homedir(), "bgnode", "debugIndex.log"),
};
// /// just for debugging
// setupDebugLogging(CONFIG.debugLogPath);
const gethVer = "1.14.3";
const rethVer = "1.0.0";
const prysmVer = "5.0.3";
const lighthouseVer = "5.1.3";
function createJwtSecret(jwtDir) {
if (!fs.existsSync(jwtDir)) {
console.log(`\nCreating '${jwtDir}'`);
fs.mkdirSync(jwtDir, { recursive: true });
}
if (!fs.existsSync(`${jwtDir}/jwt.hex`)) {
console.log("Generating JWT.hex file.");
execSync(`cd "${jwtDir}" && openssl rand -hex 32 > jwt.hex`, {
stdio: "inherit",
});
}
}
let executionChild;
let consensusChild;
let executionExited = false;
let consensusExited = false;
function handleExit() {
console.log("Received exit signal");
try {
// Check if both child processes have exited
const checkExit = () => {
if (executionExited && consensusExited) {
console.log("Both clients exited!");
removeLockFile();
process.exit(0);
}
};
// Gracefully kill the execution client
if (executionChild && !executionExited) {
console.log("Exiting execution client...");
executionChild.kill("SIGINT");
executionChild.on("exit", (code) => {
executionExited = true;
console.log("Execution client exited");
checkExit();
});
} else {
executionExited = true;
}
// Gracefully kill the consensus client
if (consensusChild && !consensusExited) {
console.log("Exiting consensus client...");
consensusChild.kill("SIGINT");
consensusChild.on("exit", (code) => {
consensusExited = true;
console.log("Consensus client exited");
checkExit();
});
} else {
consensusExited = true;
}
// Initial check in case both children are already not running
checkExit();
} catch (error) {
console.log("Error form handle exit", error);
}
}
process.on("SIGINT", handleExit);
/// SIGTERM for using kill command to shut down process
process.on("SIGTERM", handleExit);
process.on("SIGUSR2", () => {
// console.log("SIGUSR2 received");
handleExit();
});
function startClient(clientName, installDir) {
let clientCommand, clientArgs;
if (clientName === "geth") {
clientCommand = path.join(__dirname, "node_clients/geth.js");
clientArgs = [];
} else if (clientName === "reth") {
clientCommand = path.join(__dirname, "node_clients/reth.js");
clientArgs = [];
} else if (clientName === "prysm") {
clientCommand = path.join(__dirname, "node_clients/prysm.js");
clientArgs = [];
} else if (clientName === "lighthouse") {
clientCommand = path.join(__dirname, "node_clients/lighthouse.js");
clientArgs = [];
} else {
clientCommand = path.join(installDir, "bgnode", clientName, clientName);
clientArgs = [];
}
const child = spawn("node", [clientCommand, ...clientArgs], {
stdio: ["inherit", "pipe", "inherit"],
cwd: process.env.HOME,
env: { ...process.env, INSTALL_DIR: installDir },
});
if (clientName === "geth") {
executionChild = child;
} else if (clientName === "reth") {
consensusChild = child;
} else if (clientName === "prysm") {
consensusChild = child;
} else if (clientName === "lighthouse") {
consensusChild = child;
}
child.on("exit", (code) => {
console.log(`${clientName} process exited with code ${code}`);
if (clientName === "geth") {
executionExited = true;
} else if (clientName === "prysm") {
consensusExited = true;
}
});
child.on("error", (err) => {
console.log(`Error from start client: ${err.message}`);
});
console.log(clientName, "started");
child.stdout.on("error", (err) => {
console.error(`Error on stdout of ${clientName}: ${err.message}`);
});
}
function isAlreadyRunning() {
try {
if (fs.existsSync(lockFilePath)) {
const pid = fs.readFileSync(lockFilePath, "utf8");
try {
process.kill(pid, 0);
return true;
} catch (e) {
if (e.code === "ESRCH") {
fs.unlinkSync(lockFilePath);
return false;
}
throw e;
}
}
return false;
} catch (err) {
console.error("Error checking for existing process:", err);
return false;
}
}
function createLockFile() {
fs.writeFileSync(lockFilePath, process.pid.toString(), "utf8");
// console.log(process.pid.toString())
}
function removeLockFile() {
if (fs.existsSync(lockFilePath)) {
fs.unlinkSync(lockFilePath);
}
}
const jwtDir = path.join(installDir, "bgnode", "jwt");
const platform = os.platform();
if (["darwin", "linux"].includes(platform)) {
installMacLinuxExecutionClient(executionClient, platform, gethVer, rethVer);
installMacLinuxConsensusClient(consensusClient, platform, prysmVer);
} else if (platform === "win32") {
installWindowsExecutionClient(executionClient);
installWindowsConsensusClient(consensusClient);
}
let messageForHeader = "";
let runsClient = false;
createJwtSecret(jwtDir);
const wsConfig = {
executionClient: executionClient,
consensusClient: consensusClient,
gethVer: gethVer,
rethVer: rethVer,
prysmVer: prysmVer,
lighthouseVer: lighthouseVer,
};
if (!isAlreadyRunning()) {
startClient(executionClient, installDir);
startClient(consensusClient, installDir);
initializeWSConnection(wsConfig);
messageForHeader = "Node execution";
runsClient = true;
createLockFile();
} else {
console.log("Node already started. Initializing monitoring only.");
messageForHeader = "Only dashboard, client already running";
runsClient = false;
}
initializeMonitoring(messageForHeader, gethVer, rethVer, prysmVer, runsClient);