Skip to content

Commit 944da6c

Browse files
No more web sockets (#24)
* change checkin to https and every minute and when you find a new block * let's try this * force checkin once a minute * force checkin once a minute
1 parent a653dd8 commit 944da6c

File tree

7 files changed

+208
-206
lines changed

7 files changed

+208
-206
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import https from "https";
2+
import macaddress from "macaddress";
3+
import os from "os";
4+
import { debugToFile } from "../helpers.js";
5+
import {
6+
getMemoryUsage,
7+
getCpuUsage,
8+
getDiskUsage,
9+
} from "../getSystemStats.js";
10+
import { localClient } from "../monitor_components/viemClients.js";
11+
import { installDir } from "../commandLineOptions.js";
12+
13+
export let checkIn;
14+
15+
function getMacAddress() {
16+
return new Promise((resolve, reject) => {
17+
macaddress.all((err, all) => {
18+
if (err) {
19+
reject(`Error getting MAC address: ${err}`);
20+
return;
21+
}
22+
23+
// Get the first non-internal MAC address
24+
let macAddress = null;
25+
for (const interfaceName in all) {
26+
const mac = all[interfaceName].mac;
27+
if (mac && mac !== "00:00:00:00:00:00") {
28+
macAddress = mac;
29+
break;
30+
}
31+
}
32+
33+
resolve(macAddress);
34+
});
35+
});
36+
}
37+
38+
export function initializeHttpConnection(httpConfig) {
39+
let lastCheckInTime = 0;
40+
let lastCheckedBlockNumber = -1;
41+
const minCheckInInterval = 60000; // Minimum 60 seconds between check-ins
42+
43+
checkIn = async function (force = false) {
44+
const now = Date.now();
45+
if (!force && now - lastCheckInTime < minCheckInInterval) {
46+
return;
47+
}
48+
49+
let currentBlockNumber;
50+
try {
51+
currentBlockNumber = await localClient.getBlockNumber();
52+
} catch (error) {
53+
debugToFile(`Failed to get block number: ${error}`, () => {});
54+
return;
55+
}
56+
57+
if (!force && currentBlockNumber === lastCheckedBlockNumber) {
58+
return;
59+
}
60+
61+
lastCheckInTime = now;
62+
lastCheckedBlockNumber = currentBlockNumber;
63+
64+
let executionClientResponse = httpConfig.executionClient;
65+
let consensusClientResponse = httpConfig.consensusClient;
66+
67+
if (httpConfig.executionClient === "geth") {
68+
executionClientResponse += " v" + httpConfig.gethVer;
69+
} else if (httpConfig.executionClient === "reth") {
70+
executionClientResponse += " v" + httpConfig.rethVer;
71+
}
72+
73+
if (httpConfig.consensusClient === "prysm") {
74+
consensusClientResponse += " v" + httpConfig.prysmVer;
75+
} else if (httpConfig.consensusClient === "lighthouse") {
76+
consensusClientResponse += " v" + httpConfig.lighthouseVer;
77+
}
78+
79+
let possibleBlockNumber;
80+
let possibleBlockHash;
81+
try {
82+
possibleBlockNumber = await localClient.getBlockNumber();
83+
const block = await localClient.getBlock();
84+
possibleBlockHash = block.hash;
85+
} catch (error) {
86+
debugToFile(`Failed to get block number: ${error}`, () => {});
87+
}
88+
89+
try {
90+
const cpuUsage = await getCpuUsage();
91+
const memoryUsage = await getMemoryUsage();
92+
const diskUsage = await getDiskUsage(installDir);
93+
const macAddress = await getMacAddress();
94+
95+
const params = new URLSearchParams({
96+
id: `${os.hostname()}-${macAddress}-${os.platform()}-${os.arch()}`,
97+
node_version: `${process.version}`,
98+
execution_client: executionClientResponse,
99+
consensus_client: consensusClientResponse,
100+
cpu_usage: `${cpuUsage.toFixed(1)}`,
101+
memory_usage: `${memoryUsage}`,
102+
storage_usage: `${diskUsage}`,
103+
block_number: possibleBlockNumber ? possibleBlockNumber.toString() : "",
104+
block_hash: possibleBlockHash ? possibleBlockHash : "",
105+
});
106+
107+
const options = {
108+
hostname: "rpc.buidlguidl.com",
109+
port: 48544,
110+
path: `/checkin?${params.toString()}`,
111+
method: "GET",
112+
};
113+
114+
const req = https.request(options, (res) => {
115+
let data = "";
116+
res.on("data", (chunk) => {
117+
data += chunk;
118+
});
119+
res.on("end", () => {
120+
debugToFile(`Checkin response: ${data}`, () => {});
121+
});
122+
});
123+
124+
req.on("error", (error) => {
125+
debugToFile(`Checkin error: ${error}`, () => {});
126+
});
127+
128+
req.end();
129+
} catch (error) {
130+
debugToFile(`checkIn() Error: ${error}`, () => {});
131+
}
132+
};
133+
134+
// Immediate check-in when monitoring starts
135+
checkIn(true);
136+
137+
// Set up block listener
138+
localClient.watchBlocks(
139+
{
140+
onBlock: (block) => {
141+
if (block.number > 0) {
142+
checkIn(true); // Force check-in for each new block
143+
}
144+
},
145+
},
146+
(error) => {
147+
debugToFile(`Error in block watcher: ${error}`, () => {});
148+
}
149+
);
150+
151+
// Regular interval check-in
152+
setInterval(() => checkIn(true), 60000); // Force check-in every 60 seconds
153+
}

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
installWindowsConsensusClient,
1212
installWindowsExecutionClient,
1313
} from "./ethereum_client_scripts/install.js";
14-
import { initializeWSConnection } from "./ws_connection/wsConnection.js";
14+
import { initializeHttpConnection } from "./https_connection/httpsConnection.js";
1515
import {
1616
executionClient,
1717
consensusClient,
@@ -297,7 +297,7 @@ let runsClient = false;
297297

298298
createJwtSecret(jwtDir);
299299

300-
const wsConfig = {
300+
const httpConfig = {
301301
executionClient: executionClient,
302302
consensusClient: consensusClient,
303303
gethVer: gethVer,
@@ -312,7 +312,7 @@ if (!isAlreadyRunning()) {
312312
startClient(executionClient, installDir);
313313
startClient(consensusClient, installDir);
314314

315-
initializeWSConnection(wsConfig);
315+
initializeHttpConnection(httpConfig);
316316

317317
runsClient = true;
318318
createLockFile();

monitor_components/updateLogicExecution.js

Lines changed: 18 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { mainnetClient, localClient, isSyncing } from "./viemClients.js";
1111
import { exec } from "child_process";
1212
import { populateRethStageGauge } from "./rethStageGauge.js";
1313
import { populateGethStageGauge } from "./gethStageGauge.js";
14+
import { checkIn } from "../https_connection/httpsConnection.js";
1415

1516
const progress = loadProgress();
1617
let gethStageProgress = [
@@ -82,6 +83,7 @@ export function setupLogStreaming(
8283
) {
8384
let logBuffer = [];
8485
let lastSize = 0;
86+
let lastKnownBlockNumber = 0;
8587

8688
const ensureBufferFillsWidget = () => {
8789
const visibleHeight = executionLog.height - 2; // Account for border
@@ -98,7 +100,7 @@ export function setupLogStreaming(
98100
}
99101
};
100102

101-
const updateLogContent = () => {
103+
const updateLogContent = async () => {
102104
try {
103105
const stats = fs.statSync(logFilePath);
104106
const newSize = stats.size;
@@ -116,7 +118,7 @@ export function setupLogStreaming(
116118
terminal: false,
117119
});
118120

119-
newRl.on("line", (line) => {
121+
newRl.on("line", async (line) => {
120122
globalLine = line;
121123
logBuffer.push(formatLogLines(line));
122124

@@ -134,6 +136,20 @@ export function setupLogStreaming(
134136
saveChainDlProgress(line);
135137
}
136138

139+
// Check for new block
140+
const blockNumberMatch = line.match(/block=(\d+)/);
141+
if (blockNumberMatch) {
142+
const currentBlockNumber = parseInt(blockNumberMatch[1], 10);
143+
if (currentBlockNumber > lastKnownBlockNumber) {
144+
lastKnownBlockNumber = currentBlockNumber;
145+
try {
146+
await checkIn(); // Call checkIn when a new block is found
147+
} catch (error) {
148+
debugToFile(`Error calling checkIn: ${error}`, () => {});
149+
}
150+
}
151+
}
152+
137153
screen.render();
138154
});
139155

@@ -161,84 +177,6 @@ export function setupLogStreaming(
161177
});
162178
}
163179

164-
// export function setupLogStreaming(
165-
// logFilePath,
166-
// executionLog,
167-
// screen,
168-
// rethStageGauge,
169-
// gethStageGauge,
170-
// chainInfoBox
171-
// ) {
172-
// let logBuffer = [];
173-
// let lastSize = 0;
174-
175-
// const updateLogContent = () => {
176-
// try {
177-
// const stats = fs.statSync(logFilePath);
178-
// const newSize = stats.size;
179-
180-
// if (newSize > lastSize) {
181-
// const newStream = fs.createReadStream(logFilePath, {
182-
// encoding: "utf8",
183-
// start: lastSize,
184-
// end: newSize,
185-
// });
186-
187-
// const newRl = readline.createInterface({
188-
// input: newStream,
189-
// output: process.stdout,
190-
// terminal: false,
191-
// });
192-
193-
// newRl.on("line", (line) => {
194-
// globalLine = line;
195-
// logBuffer.push(formatLogLines(line));
196-
197-
// debugToFile(`logBuffer.length: ${logBuffer.length}`, () => {});
198-
199-
// if (logBuffer.length > executionLog.height - 2) {
200-
// logBuffer.shift();
201-
// }
202-
203-
// executionLog.setContent(logBuffer.join("\n"));
204-
205-
// if (executionClient == "geth") {
206-
// if (screen.children.includes(gethStageGauge)) {
207-
// populateGethStageGauge(gethStageProgress);
208-
// }
209-
210-
// saveHeaderDlProgress(line);
211-
// saveStateDlProgress(line);
212-
// saveChainDlProgress(line);
213-
// }
214-
215-
// screen.render();
216-
// });
217-
218-
// newRl.on("close", () => {
219-
// lastSize = newSize;
220-
// });
221-
222-
// newRl.on("error", (err) => {
223-
// debugToFile(`Error reading log file: ${err}`, () => {});
224-
// });
225-
// }
226-
// } catch (error) {
227-
// debugToFile(`Error accessing log file: ${error}`, () => {});
228-
// }
229-
// };
230-
231-
// // Initial read to load existing content
232-
// updateLogContent();
233-
234-
// // Watch for file changes
235-
// fs.watchFile(logFilePath, (curr, prev) => {
236-
// if (curr.mtime > prev.mtime) {
237-
// updateLogContent();
238-
// }
239-
// });
240-
// }
241-
242180
let statusMessage = "INITIALIZING...";
243181

244182
async function createGethMessage(syncingStatus, blockNumber, latestBlock) {

package-lock.json

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"axios": "^1.7.2",
1414
"blessed": "^0.1.81",
1515
"blessed-contrib": "^4.11.0",
16+
"https": "^1.0.0",
1617
"macaddress": "^0.5.3",
1718
"minimist": "^1.2.8",
1819
"node-pty": "^1.0.0",

0 commit comments

Comments
 (0)