forked from hicommonwealth/nodeup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·99 lines (89 loc) · 2.75 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
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env node
const { program, option } = require('commander');
const { ApiPromise, WsProvider } = require('@polkadot/api');
const { u128 } = require('@polkadot/types');
const { spec } = require('@edgeware/node-types');
const { promisify } = require('util');
const uuidv4 = require('uuid/v4');
const fs = require('fs');
const readFileAsync = promisify(fs.readFile);
const writeFileAsync = promisify(fs.writeFile);
const checkNode = async (nodeUrl, types) => {
//
// try to initialize on first run
//
try {
await readFileAsync('/tmp/nodeup.lastblock');
} catch (e) {
console.log('Initializing...');
await writeFileAsync('/tmp/nodeup.lastblock', (0).toString());
}
//
// set a timeout manually, since ApiPromise won't let us do this
// if the timeout is reached, kill the process with exitcode 1
//
console.log(`Connecting to API for ${nodeUrl}...`);
let connected;
setTimeout(() => {
if (connected) return;
console.log('Connection timed out');
process.exit(1);
}, 9000);
//
// initialize the api
//
const api = await ApiPromise.create({
provider: new WsProvider(nodeUrl),
...spec,
});
console.log('Connected');
connected = true;
//
// get relevant chain data
//
const [block, health, peers] = await Promise.all([
api.rpc.chain.getBlock(),
api.rpc.system.health(),
api.rpc.system.peers(),
]);
const nPeers = health.peers.toNumber();
const bestBlock = +block.block.header.number;
// if we have no peers we should restart
if (nPeers === 0) {
process.exit(1)
}
console.log(`${nPeers} peers connected`);
// Calculate the number of peers more then 10 blocks ahead
const nPeersAhead = peers.toArray()
.map((p) => +p.bestNumber > bestBlock + 10)
.filter((ahead) => ahead === true)
.length;
//
// if it hasn't changed since the last run, and we are behind the
// majority of peer nodes, we may be stalled and should exit with an
// error
//
console.log(`${bestBlock} is our best block`);
if (nPeersAhead > nPeers / 2) {
const storage = await readFileAsync('/tmp/nodeup.lastblock');
const lastBlocknum = parseInt(storage.toString());
if (lastBlocknum === bestBlock) {
console.log(`${nPeersAhead} of ${nPeers} peers are ahead of us`);
console.log('throwing an error since the best block has not updated recently');
process.exit(1);
}
await writeFileAsync('/tmp/nodeup.lastblock', bestBlock);
}
process.exit(0);
};
program
.name('nodeup')
.option('-u, --url <url>', 'Url of node to connect to')
.parse(process.argv);
const programOptions = program.opts();
let url = 'wss://mainnet2.edgewa.re';
if (programOptions.url) {
url = programOptions.url;
}
console.log(`Checking node at ${url}`);
checkNode(url);