forked from ravenrebels/ravencoin-rpc-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetRPCNode.js
57 lines (50 loc) · 1.24 KB
/
getRPCNode.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
const NeuraiRPC = require("@ravenrebels/ravencoin-rpc");
const getConfig = require("./getConfig");
const config = getConfig();
const allNodes = [];
//At startup initialize all RPCs, you can have one or multiple Neoxa nodes
for (const node of config.nodes) {
const rpc = NeuraiRPC.getRPC(node.username, node.password, node.neurai_url);
allNodes.push({ name: node.name, rpc });
}
/* Every x seconds, check the status of the nodes */
async function healthCheck() {
for (const node of allNodes) {
try {
const a = await node.rpc("getbestblockhash", []);
node.bestblockhash = a;
node.active = true;
} catch {
node.active = false;
}
}
}
setInterval(healthCheck, 10 * 1000);
healthCheck();
function getRPCNode() {
for (const n of allNodes) {
if (n.active === true) {
return {
rpc: n.rpc,
name: n.name,
};
}
}
//We did not find any active node so we return the first
return {
name: allNodes[0].name,
rpc: allNodes[0].rpc,
};
}
function getNodes() {
const list = [];
for (const n of allNodes) {
list.push({
active: n.active,
bestblockhash: n.bestblockhash,
name: n.name,
});
}
return list;
}
module.exports = { getRPCNode, getNodes };