forked from leapdao/eth-node-healthcheck
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·63 lines (51 loc) · 2.07 KB
/
Copy pathindex.js
File metadata and controls
executable file
·63 lines (51 loc) · 2.07 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
#!/usr/bin/env node
const ethers = require('ethers');
const http = require('http');
const port = process.env.PORT || 80
const localRpcUrl = process.env.RPC_URL || 'http://localhost:8545';
const networkRpcUrls = (process.env.NETWORK_URLS || 'https://rpc.bitkubchain.io').split(',')
const MAX_BLOCK_DIFFERENCE = process.env.MAX_BLOCK_DIFFERENCE || 3;
const getNetworkBlockNum = async (rpcUrl) => {
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
return provider.getBlockNumber();
}
const ramdomBetween = (min, max) => {
return Math.floor(Math.random() * (max - min) + min)
}
const onHealthcheckRequest = async (req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
let localBlockNum;
let networkBlockNum;
const networkRpcIndex = ramdomBetween(0, networkRpcUrls.length)
const networkRpcUrl = networkRpcUrls[networkRpcIndex]
try {
networkBlockNum = await getNetworkBlockNum(networkRpcUrl)
} catch (error) {
console.log(`Fetch network ${networks[networkIndex]}, error: Cannot connect network.`)
console.error(e);
networkBlockNum = 0;
}
try {
localBlockNum = await getNetworkBlockNum(localRpcUrl)
} catch (e) {
console.log(`Fetch local ${networkRpcUrl}, error: Cannot connect local.`)
console.error(e);
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(e);
return;
}
console.log(`Fetch network ${networkRpcUrl} -> local ${localRpcUrl}, last block: ${networkBlockNum} --> ${localBlockNum}`)
let responseStatus = networkBlockNum - localBlockNum > MAX_BLOCK_DIFFERENCE ? 500 : 200;
if (localBlockNum > 10000 && networkBlockNum <= 0) { // don't let etherscan f**k us
responseStatus = 200;
} else if (networkBlockNum < localBlockNum) {
responseStatus = 200;
}
res.writeHead(responseStatus, { 'Content-Type': 'text/plain' });
res.end((localBlockNum - networkBlockNum).toString());
};
http.createServer(onHealthcheckRequest)
.listen(port, () => {
console.log(`Start port ${port}`);
});