-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
88 lines (77 loc) · 2.83 KB
/
index.js
File metadata and controls
88 lines (77 loc) · 2.83 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
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
const { CasperServiceByJsonRPC } = require('casper-js-sdk');
const axios = require('axios');
const haproxyConf = require('./haproxy.conf');
const fs = require('fs');
const awaitTimeout = (delay, reason) =>
new Promise((resolve, reject) =>
setTimeout(
() => (reason === undefined ? resolve() : reject(reason)),
delay,
),
);
const wrapPromise = (promise, delay, reason) =>
Promise.race([promise, awaitTimeout(delay, reason)]);
async function getStatus(address, apiVersion, chainspecName, height) {
console.log(address);
try {
const status = (await axios.get('http://' + address + '/status', { timeout: 3000 })).data;
if (status.api_version === apiVersion && status.chainspec_name === chainspecName && status.last_added_block_info.height >= height) {
console.log('http://' + address.replace(/:[0-9]*/g, ':7777'));
try {
const block = await wrapPromise(new CasperServiceByJsonRPC('http://' + address.replace(/:[0-9]*/g, ':7777') + '/rpc').getBlockInfoByHeight(height), 3000, { reason: 'timeout' });
if (block.block.header.height === height) {
console.log('good');
addresses.push(address);
} else {
console.log(block);
console.log('bad block');
}
} catch (e) {
console.log(e);
console.log('timeout ?');
}
}
} catch (e) {
console.log(e);
console.log('timeout');
}
}
async function peersStatus(rpc) {
if(rpc === 'testnet') {
const backends = '\tserver www1 78.46.68.30:7777 maxconn 5000 check inter 10s\n';
const configFile = haproxyConf(5000, 8500, backends, 'casper-test');
console.log(configFile);
fs.writeFile('haproxy.cfg', configFile, function (err) {
if (err) throw err;
console.log('Saved!');
process.exit();
});
} else {
const casperClient = new CasperServiceByJsonRPC(rpc);
const status = await casperClient.getStatus();
const peers = (await casperClient.getPeers()).peers;
let arrayIp = [];
for (const peer of peers) {
arrayIp.push(peer.address.replace(/:[0-9]*/g, ':8888'));
}
arrayIp = [...new Set(arrayIp)];
console.log(arrayIp);
for (const ip of arrayIp) {
await getStatus(ip, status.api_version, status.chainspec_name, status.last_added_block_info.height);
}
let backends = '';
for (let i = 0; i < addresses.length; i++) {
backends += '\tserver www' + (i + 1) + ' ' + addresses[i].replace(/:[0-9]*/g, ':7777') + ' maxconn 20 check inter 10s\n';
}
const configFile = haproxyConf(addresses.length * 20, 8500, backends, status.chainspec_name);
console.log(configFile);
fs.writeFile('haproxy.cfg', configFile, function (err) {
if (err) throw err;
console.log('Saved!');
process.exit();
});
}
}
let addresses = [];
const args = process.argv.slice(2)
peersStatus(args[0])