forked from cubedro/eth-netstats
-
Notifications
You must be signed in to change notification settings - Fork 14
/
app.js
376 lines (335 loc) · 9.68 KB
/
app.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
var _ = require('lodash');
var logger = require('./lib/utils/logger');
var chalk = require('chalk');
var http = require('http');
var fs = require('fs');
var WebSocket = require('ws');
var exec = require('child_process').exec;
// use mainnet by default
var env = process.env.NODE_ENV || "production";
var config = require('./lib/utils/config.json')[env];
var rpc_cert = fs.readFileSync(config.cert_path);
var rpc_user = config.user;
var rpc_password = config.pass;
var app = require('./lib/express');
server = http.createServer(app);
// Init socket vars
var Primus = require('primus');
var api;
var client;
var server;
// Init Client Socket connection
client = new Primus(server, {
transformer: 'websockets',
pathname: '/primus',
parser: 'JSON'
});
client.use('emit', require('primus-emit'));
// Init collections
var Collection = require('./lib/collection');
var Nodes = new Collection();
Nodes.add( {id : 'localhost'}, function (err, info)
{
if (err) console.log(err);
});
Nodes.setChartsCallback(function (err, charts)
{
if(err !== null)
{
console.error('COL', 'CHR', 'Charts error:', err);
}
else
{
client.write({
action: 'charts',
data: charts
});
}
});
// reconnect interval = 5 seconds
var reconnectInterval = 5 * 1000;
var reconnecting = false;
var ws;
// Initiate the websocket connection. The dcrd generated certificate acts as
// its own certificate authority, so it needs to be specified in the 'ca' array
// for the certificate to properly validate.
var connect = function() {
reconnecting = false;
console.log('Connecting to Decred daemon');
ws = new WebSocket('wss://'+config.host+':'+config.port+'/ws', {
headers: {
'Authorization': 'Basic '+new Buffer(rpc_user+':'+rpc_password).toString('base64')
},
cert: rpc_cert,
ca: [rpc_cert]
});
ws.on('open', function() {
console.log('CONNECTED');
// Send a JSON-RPC command to be notified when blocks are connected and
// disconnected from the chain.
ws.send('{"jsonrpc":"1.0","id":"notifyblocks","method":"notifyblocks","params":[]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
// gets block info on restart, not only on new blocks
ws.send('{"jsonrpc":"1.0","id":"getbestblockhash","method":"getbestblockhash","params":[]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
/* Update stuff each minute */
var activeNodesInterval = setInterval(function() {
ws.send('{"jsonrpc":"1.0","id":"getpeerinfo","method":"getpeerinfo","params":[]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
ws.send('{"jsonrpc":"1.0","id":"ticketsmempool","method":"getrawmempool","params":[false, "tickets"]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
//dcrctl getrawmempool false tickets
// ws.send('{"jsonrpc":"1.0","id":"getstakeinfo","method":"getstakeinfo","params":[]}', function(err) {
// if (err) {
// console.log('Socket error: ' + err);
// }
// });
ws.send('{"jsonrpc":"1.0","id":"getmininginfo","method":"getmininginfo","params":[]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
ws.send('{"jsonrpc":"1.0","id":"estimatestakediff","method":"estimatestakediff","params":[]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
}, 60000);
// except for getticketpoolvalue which eats a huge amount of CPU, so run it less frequently.
// var activeNodesInterval = setInterval(function() {
// ws.send('{"jsonrpc":"1.0","id":"getticketpoolvalue","method":"getticketpoolvalue","params":[]}', function(err) {
// if (err) {
// console.log('Socket error: ' + err);
// }
// });
// }, 5 * 60000);
});
ws.on('message', function(data, flags) {
try {
data = JSON.parse(data);
} catch(e) {
console.log(e);
return;
}
/* Get New Block by hash */
if (data.params) {
ws.send('{"jsonrpc":"1.0","id":"getbestblockhash","method":"getbestblockhash","params":[]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
return;
}
var result = data.result;
if (result && data.id && data.id == 'getbestblockhash') {
// requests last block
ws.send('{"jsonrpc":"1.0","id":"getblock","method":"getblock","params":["'+result+'"]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
} else if (result && data.id && data.id == 'getblock') {
addNewBlock(result);
} else if (result && data.id && data.id == 'getmininginfo') {
updateNetworkHashrate(result);
} else if (result && data.id && data.id == 'getstakeinfo') {
updateStake(result);
} else if (result && data.id && data.id == 'ticketsmempool') {
updateTicketsMempool(result);
} else if (result && data.id && data.id == 'estimatestakediff') {
updateEstimateStake(result);
} else if (result && data.id && data.id == 'getpeerinfo') {
updatePeerInfo(result);
} else if ( result && data.id && data.id == 'getcoinsupply' ) {
updateSupply(result);
} else if (result && data.id && data.id == 'getticketpoolvalue' ) {
updateLocked(result);
}
});
ws.on('error', function(derp) {
console.log('ERROR:' + derp);
if (!reconnecting) {
console.log('Trying to reconnect.');
reconnecting = true;
setTimeout(connect, reconnectInterval);
}
});
ws.on('close', function(data) {
console.log('DISCONNECTED');
if (!reconnecting) {
console.log('Trying to reconnect.');
reconnecting = true;
setTimeout(connect, reconnectInterval);
}
});
};
/* Connect to decred daemon on start */
connect();
client.on('connection', function (clientSpark)
{
clientSpark.on('ready', function (data)
{
clientSpark.emit('init', { nodes: Nodes.all() });
Nodes.getCharts();
client.write({ action: 'peers', data: {peers : Nodes.peers()} });
});
clientSpark.on('client-pong', function (data)
{
var serverTime = _.get(data, "serverTime", 0);
var latency = Math.ceil( (_.now() - serverTime) / 2 );
clientSpark.emit('client-latency', { latency: latency });
});
});
var latencyTimeout = setInterval( function ()
{
client.write({
action: 'client-ping',
data: {
serverTime: _.now()
}
});
}, 5000);
// Cleanup old inactive nodes
var nodeCleanupTimeout = setInterval( function ()
{
client.write({
action: 'init',
data: Nodes.all()
});
Nodes.getCharts();
}, 1000*60*60);
function addNewBlock (block) {
Nodes.addBlock('localhost', block, function (err, stats)
{
if(err !== null)
{
console.error('API', 'BLK', 'Block error:', err);
}
else
{
if(stats !== null)
{
client.write({
action: 'block',
data: stats
});
console.success('API', 'BLK', 'Block:', block['height']);
Nodes.getCharts();
}
}
});
/* Update coin supply */
ws.send('{"jsonrpc":"1.0","id":"getcoinsupply","method":"getcoinsupply","params":[]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
// I might want to update getticketpoolvalue when i get a new block too
ws.send('{"jsonrpc":"1.0","id":"getticketpoolvalue","method":"getticketpoolvalue","params":[]}', function(err) {
if (err) {
console.log('Socket error: ' + err);
}
});
}
function updateSupply (data) {
Nodes.updateSupply(data, function (err, stats) {
if(err !== null)
{
console.error('API', 'UPD', 'updateSupply error:', err);
} else {
console.success('API', 'UPD', 'Updated availiable supply');
return;
}
});
}
function updateLocked (data) {
Nodes.updateLocked(data, function (err, stats) {
if(err !== null)
{
console.error('API', 'UPD', 'updateLocked error:', err);
} else {
console.success('API', 'UPD', 'Updated locked coins');
return;
}
});
}
function updatePeerInfo(data) {
Nodes.updatePeers(data, function(err, peers) {
if (err) {
console.log(err);
} else {
console.success('API', 'UPD', 'Updated peers');
}
client.write({ action: 'peers', data: {peers : Nodes.peers()} });
});
}
function updateNetworkHashrate (data) {
Nodes.updateMiningInfo(data, function (err, stats) {
if(err !== null)
{
console.error('API', 'BLK', 'MiningInfo error:', err);
} else {
console.success('API', 'UPD', 'Updated mininginfo');
client.write({
action: 'mininginfo',
data: stats
});
}
});
}
function updateTicketsMempool (data) {
Nodes.updateTicketsMempool(data, function (err, stats) {
if(err !== null)
{
console.error('API', 'BLK', 'TicketsMempool error:', err);
} else {
console.success('API', 'UPD', 'Updated TicketsMempool info');
client.write({
action: 'tiketsmempool',
data: stats
});
}
});
}
function updateStake (data) {
Nodes.updateStakeInfo(data, function (err, stats) {
if(err !== null)
{
console.error('API', 'BLK', 'StakeInfo error:', err);
} else {
console.success('API', 'UPD', 'Updated stake info');
client.write({
action: 'stakeinfo',
data: stats
});
}
});
}
function updateEstimateStake (data) {
Nodes.updateEstimateStake(data, function (err, stats) {
if(err !== null)
{
console.error('API', 'BLK', 'EstimateStake error:', err);
} else {
console.success('API', 'UPD', 'EstimateStake fee info');
client.write({
action: 'estimatestakediff',
data: stats
});
}
});
}
server.listen(process.env.PORT || 3000);
module.exports = server;