-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (69 loc) · 1.95 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
import dotenv from 'dotenv';
import {spawn} from "child_process";
import http from 'http';
dotenv.config();
let validating = false;
async function get_local_data() {
try {
const child = spawn("palomad",['status']);
for await (const data of child.stdout) {
return JSON.parse(data);
};
for await (const data of child.stderr) {
captureException(`CHECK IS SERVER IS UP: ${data}`);
};
} catch (err) {
//captureException(err);
console.log(err);
return null;
}
}
let latest_block_time = null;
let failure_count = 0;
async function check_and_sync(local_data) {
try {
validating = true;
if (process.env.DISABLE_VOTE_POWER !== "true") {
if (local_data.validator_info.voting_power) {
console.log(local_data.validator_info.voting_power);
validating = local_data.validator_info.voting_power > 0;
}
}
if (local_data.sync_info.latest_block_time) {
if (latest_block_time === local_data.sync_info.latest_block_time) {
failure_count++;
if (failure_count >= 4) {
console.log(
"latest_block_time has remained the same for 4 consecutive checks"
);
validating = false;
}
} else {
latest_block_time = local_data.sync_info.latest_block_time;
failure_count = 0;
}
}
} catch (err) {
console.log(err);
}
}
async function main() {
console.log('checking local');
let local_data = await get_local_data();
await check_and_sync(local_data);
return 0;
}
await main();
setInterval(main, 1000 * 60);
const requestListener = function (req, res) {
try {
res.setHeader('Content-Type', 'text/plain');
res.writeHead(200);
//let response =
res.end(validating.toLocaleString());
} catch (error) {
console.log(error);
}
}
const server = http.createServer(requestListener);
server.listen(process.env.PORT);