This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
stats-client.js
237 lines (215 loc) · 6.89 KB
/
stats-client.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
var async = require('async');
var request = require('request-json');
var socketIo = require('socket.io-client');
//var config = require('./config.json');
var fs = require('fs');
var readlineSync = require('readline-sync');
// Don't modify this URL
const serverURL = 'http://stats.atlantis.dero.live:8080/nodes';
const VERSION = '0.3';
var config = null;
var socket = null;
var refreshTime = 0;
var timeout = null;
var currentBlockHeight = 0;
var intervalRefresh = null;
if (!fs.existsSync('config.json')) {
var configFile = {};
configFile.myDaemon = "http://127.0.0.1:20206";
var name = readlineSync.question('What is the public name of your node?');
while (name.length < 3 || name.length > 24) {
name = readlineSync.question('What is the public name of your node? (3 charac min - 24 max)');
}
configFile.myName = name;
var description = readlineSync.question('What is your public node description? ');
while (description.length > 512) {
description = readlineSync.question('What is your public node description? (512 charac max)');
}
configFile.myDescription = description;
fs.writeFile("config.json", JSON.stringify(configFile), 'utf8', function(err) {
if(err) {
return console.log(err);
}
console.log("Config.json created.");
config = JSON.parse(fs.readFileSync('config.json'));
init();
});
} else {
config = JSON.parse(fs.readFileSync('config.json'));
init();
}
function init() {
socket = socketIo.connect(serverURL);
socket.on('connect', function () {
socket.emit('version', VERSION);
});
socket.on('refreshTime', function(interval) {
console.log('--- New refresh time '+interval);
if (timeout) clearTimeout(timeout);
if (intervalRefresh) clearInterval(intervalRefresh);
refreshTime = interval;
timeout = setTimeout(broadcastDaemon, refreshTime);
});
socket.on('block_information', function(hash) {
async.parallel({
getBlockHeader: function(callback) {
var paramsRequest = {
"jsonrpc": '2.0',
"id": 0,
"method": "getblockheaderbyhash",
"params": { "hash": hash }
};
var client = request.createClient(config.myDaemon);
client.headers['Content-Type'] = 'application/json';
client.post('json_rpc', paramsRequest, function(err, res, body) {
var info = body.result;
callback(null, info);
});
}
}, function (error, result) {
socket.emit('update_block', result);
});
});
socket.on('refresh-keep-alive', function() {
getInfoFromDaemon();
intervalRefresh = setInterval(function() {
getInfoFromDaemon();
}, 4000);
});
socket.on('stop-your-refresh', function() {
clearInterval(intervalRefresh);
});
socket.on('banned', function (reason) {
console.error('BANNED -- '+reason);
return process.exit(22);
});
socket.on('security-check', function (hash) {
console.log('Security check asked');
var paramsRequest = {
"jsonrpc": '2.0',
"id": 0,
"method": "getblockheaderbyhash",
"params": { "hash": hash }
};
var client = request.createClient(config.myDaemon);
client.headers['Content-Type'] = 'application/json';
client.post('json_rpc', paramsRequest, function(err, res, body) {
if (body === undefined) {
console.error('Error : The daemon did not respond or the data verification failed');
process.exit(22);
}
var info = (body.result !== undefined ? body.result : body);
socket.emit('security-check', info);
});
});
socket.on('security-result', function(state) {
switch (state) {
case "SUCCESS": {
console.log('Security checks completed. Welcome.');
break;
}
case "WAITING": {
console.log('You seem to be in sync. The security check is currently in progress. A new control will soon be realized');
break;
}
}
});
}
function broadcastDaemon() {
if (config.myName == "") {
console.error('you must fill out all required fields. NAME, DAEMON NODE');
return process.exit(22);
}
async.parallel({
latency: function(callback) {
socket.emit('latency', Date.now(), function(startTime) {
var latency = Date.now() - startTime;
callback(null, latency);
});
},
get_info: function(callback) {
var paramsRequest = {
"jsonrpc": '2.0',
"id": 0,
"method": "get_info"
};
var client = request.createClient(config.myDaemon);
client.headers['Content-Type'] = 'application/json';
client.post('json_rpc', paramsRequest, function(err, res, body) {
if (body === undefined) {
callback('Error Daemon', null);
return;
}
var info = body.result;
callback(null, {
target: info.target,
total_supply: info.total_supply,
incoming_connections: info.incoming_connections_count,
outgoing_connections: info.outgoing_connections_count,
txPool: info.tx_pool_size,
version: info.version
});
});
},
lastBlockHeader: function (callback) {
var paramsRequest = {
"jsonrpc": '2.0',
"id": 0,
"method": "getlastblockheader"
};
var client = request.createClient(config.myDaemon);
client.headers['Content-Type'] = 'application/json';
client.post('json_rpc', paramsRequest, function(err, res, body) {
if (body === undefined) {
callback('Error Daemon', null);
return;
}
var blockHeader = body.result.block_header;
callback(null, {
difficulty: blockHeader.difficulty,
hash: blockHeader.hash,
height: blockHeader.height,
topoheight: blockHeader.topoheight,
timestamp: blockHeader.timestamp,
tips: blockHeader.tips,
reward: blockHeader.reward
});
});
},
informations: function(callback) {
callback(null, {
'updated': Date.now(),
'name': config.myName,
'description': config.myDescription
});
}
}, function (error, result) {
if (error) {
console.error('Error - Probably daemon');
timeout = setTimeout(broadcastDaemon, refreshTime);
return;
}
if (currentBlockHeight < result.lastBlockHeader.topoheight) {
currentBlockHeight = result.lastBlockHeader.topoheight;
socket.emit('nodes', result);
}
timeout = setTimeout(broadcastDaemon, refreshTime);
});
}
function getInfoFromDaemon() {
var paramsRequest = {
"jsonrpc": '2.0',
"id": 0,
"method": "get_info"
};
var client = request.createClient(config.myDaemon);
client.headers['Content-Type'] = 'application/json';
client.post('json_rpc', paramsRequest, function(err, res, body) {
if (body === undefined) {
console.error('Error with daemon');
return;
}
var info = body.result;
socket.emit('refresh-keep-alive', { txPool: info.tx_pool_size });
});
}