-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpcHealthCheck.js
93 lines (82 loc) · 2.94 KB
/
rpcHealthCheck.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
const axios = require('axios');
const fs = require('fs').promises;
// Store failing RPCs
let failingRpcs = {
evm: [],
cosmos: []
};
// Check EVM RPCs
async function checkEvmRpc(rpcUrl) {
try {
const response = await axios.post(rpcUrl, {
jsonrpc: "2.0",
method: "eth_blockNumber",
params: [],
id: 1
});
if (response.data.result) {
console.log(`EVM RPC ${rpcUrl} is healthy. Latest block: ${parseInt(response.data.result, 16)}`);
} else {
console.log(`EVM RPC ${rpcUrl} returned an invalid response.`);
failingRpcs.evm.push(rpcUrl);
}
} catch (error) {
console.log(`EVM RPC ${rpcUrl} failed: ${error.message}`);
failingRpcs.evm.push(rpcUrl);
}
}
// Check Cosmos RPCs
async function checkCosmosRpc(rpcUrl) {
try {
const response = await axios.get(`${rpcUrl}/status`);
if (response.data && response.data.result) {
console.log(`Cosmos RPC ${rpcUrl} is healthy. Latest block: ${response.data.result.sync_info.latest_block_height}`);
} else {
console.log(`Cosmos RPC ${rpcUrl} returned an invalid response.`);
failingRpcs.cosmos.push(rpcUrl);
}
} catch (error) {
console.log(`Cosmos RPC ${rpcUrl} failed: ${error.message}`);
failingRpcs.cosmos.push(rpcUrl);
}
}
// Load the RPC list from the JSON file
async function loadRpcList() {
try {
const data = await fs.readFile('rpcLinks.json', 'utf8');
return JSON.parse(data);
} catch (error) {
console.error("Error reading RPC links file:", error);
throw error;
}
}
// Check all RPCs in parallel and log the failing ones
async function checkRpcHealth() {
try {
const rpcList = await loadRpcList();
console.log("Checking EVM RPCs in parallel...");
const evmPromises = rpcList.evm.map(evmRpc => checkEvmRpc(evmRpc)); // Check all EVM RPCs in parallel
console.log("Checking Cosmos RPCs in parallel...");
const cosmosPromises = rpcList.cosmos.map(cosmosRpc => checkCosmosRpc(cosmosRpc)); // Check all Cosmos RPCs in parallel
// Wait for all checks to complete
await Promise.all([...evmPromises, ...cosmosPromises]);
// Log failing RPCs
console.log("\n--- Summary of Failing RPCs ---");
if (failingRpcs.evm.length > 0) {
console.log("Failing EVM RPCs:");
failingRpcs.evm.forEach(url => console.log(url));
} else {
console.log("No failing EVM RPCs.");
}
if (failingRpcs.cosmos.length > 0) {
console.log("Failing Cosmos RPCs:");
failingRpcs.cosmos.forEach(url => console.log(url));
} else {
console.log("No failing Cosmos RPCs.");
}
} catch (error) {
console.error("Error during the RPC health check process:", error);
}
}
// Run Health Check
checkRpcHealth();