-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
308 lines (235 loc) · 9.83 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
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
process.env["NTBA_FIX_319"] = 1;
const TelegramBot = require('node-telegram-bot-api');
const moment = require('moment');
const config = require('./config.json');
const validate_config = require('./lib/ValidateConfig.js')
const Logger = require('./lib/Logger.js');
const WitnessMonitor = require('./lib/WitnessMonitor.js')
const logger = new Logger(config.debug_level);
const bot = new TelegramBot(config.telegram_token, {polling: true});
function check_config(config) {
const validation_result = validate_config(config);
if (validation_result !== undefined) {
console.log('Invalid configuration file:')
for (let field in validation_result) {
for (let error of validation_result[field]) {
console.log(` - ${field}: ${error}`);
}
}
process.exit();
}
}
function check_authorization(chatId) {
if (!config.telegram_authorized_users.includes(chatId.toString())) {
bot.sendMessage(chatId, `You (${chatId}) are not authorized.`);
return false;
}
return true;
}
function send_stats(recipient_id) {
const current_stats = witness_monitor.current_statistics();
let stats = [
`Total missed blocks: \`${current_stats.total_missed}\``,
`Missed blocks in current time window: \`${current_stats.window_missed}\``,
`Total votes: \`${current_stats.total_votes}\` (${current_stats.is_activated ? "active" : "inactive"})`,
`Current signing key: \`${current_stats.signing_key}\``,
`Feed publications: `
]
current_stats.feed_publications.forEach(feed_stat => {
stats.push(` - ${feed_stat.toString()}`)
});
bot.sendMessage(recipient_id, stats.join('\n'), { parse_mode: 'Markdown' });
}
function send_settings(recipient_id) {
const settings = [
`API node: \`${config.api_node}\``,
`Witness monitored: \`${config.witness_id}\``,
`Checking interval: \`${config.checking_interval} sec\``,
`Node failed connection attempt notification threshold: \`${config.retries_threshold}\``,
`Desynchronization threshold: \`${config.stale_blockchain_threshold}\` sec`,
`Missed block threshold: \`${config.missed_block_threshold}\``,
`Missed block reset time window: \`${config.reset_period} sec\``,
`Public signing keys: ${config.witness_signing_keys.map(k => '`' + k + '`').join(', ')}`,
`Recap time period: \`${config.recap_time} min\``,
`Feeds to check: \`${describe_feeds_to_check(config.feeds_to_check)}\``,
`Feed check interval: \`${config.feed_checking_interval} min\``,
];
bot.sendMessage(recipient_id, settings.join('\n'), { parse_mode: 'Markdown' })
}
function describe_feeds_to_check(feeds_to_check) {
return Object.entries(feeds_to_check).map(e => `${e[0]} (${e[1]} min)`).join(', ')
}
bot.on('polling_error', (error) => {
logger.error(error);
});
bot.onText(/\/start/, (msg) => {
const chatId = msg.from.id;
if (config.telegram_authorized_users.includes(chatId.toString())) {
bot.sendMessage(chatId, `Hello ${msg.from.first_name}, type /help to get the list of commands.`);
} else {
bot.sendMessage(chatId, `Hello ${msg.from.first_name}, sorry but there is nothing for you here.`);
}
});
bot.onText(/\/help/, (msg) => {
const help = [
`\`/stats\`: Return the current configuration and statistics of the monitoring session.`,
`\`/switch\`: IMMEDIATELY update your signing key to the next available signing key.`,
`\`/signing_keys <BTS_public_signing_key1> <BTS_public_signing_key2>\`: Set a new list of public keys.`,
`\`/new_node wss://<api_node_url>\`: Set a new API node to connect to.`,
`\`/threshold X\`: Set the missed block threshold before updating signing key to X blocks.`,
`\`/interval Y\`: Set the checking interval to every Y seconds.`,
`\`/interval Y\`: Set the checking interval to every Y seconds.`,
`\`/window Z\` : Set the time until missed blocks counter is reset to Z seconds.`,
`\`/recap T\` : Set the auto-notification interval of latest stats to every T minutes. Set to 0 to disable.`,
`\`/retries N\` : Set the threshold for failed API node connection attempts to N times before notifying you in telegram.`,
`\`/feed_checking_interval I\`: Set the interval of publication feed check to I minutes.`,
`\`/feeds <symbol1>:<threshold1> <symbol2>:<threshold2> ...\`: Set the feeds (and their threshold in minutes) to check to the provided list.`,
`\`/reset\` : Reset the missed blocks counter in the current time-window.`,
`\`/pause\` : Pause monitoring.`,
`\`/resume\`: Resume monitoring.`
];
bot.sendMessage(msg.from.id, help.join('\n'), { parse_mode: 'Markdown' });
});
bot.onText(/\/reset/, (msg, match) => {
const chatId = msg.chat.id;
if (check_authorization(chatId)) {
witness_monitor.reset_missed_block_window();
bot.sendMessage(chatId, 'Session missed block counter set to 0.');
}
});
bot.onText(/\/signing_keys (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const keys = match[1].split(' ');
if (check_authorization(chatId)) {
config.witness_signing_keys = keys;
bot.sendMessage(chatId, `Signing keys set to: ${config.witness_signing_keys.map(k => '`' + k + '`').join(', ')}`,
{ parse_mode: 'Markdown' });
}
});
bot.onText(/\/new_node (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const node = match[1];
if (check_authorization(chatId)) {
config.api_node = node;
bot.sendMessage(chatId, `API node set to: ${config.api_node}`);
}
});
bot.onText(/\/threshold (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const thresh = match[1];
if (check_authorization(chatId)) {
config.missed_block_threshold = thresh;
bot.sendMessage(chatId, `Missed block threshold set to: ${config.missed_block_threshold}`);
}
});
bot.onText(/\/recap (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const recap = match[1];
if (check_authorization(chatId)) {
config.recap_time = recap;
if (config.recap_time > 0) {
bot.sendMessage(chatId, `Recap time period set to: ${config.recap_time} minutes.`);
} else {
bot.sendMessage(chatId, 'Recap disabled.');
}
}
});
bot.onText(/\/window (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const wind = match[1];
if (check_authorization(chatId)) {
config.reset_period = wind;
bot.sendMessage(chatId, `Missed block reset time window set to: ${config.reset_period}s`);
}
});
bot.onText(/\/retries (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const ret = match[1];
if (check_authorization(chatId)) {
config.retries_threshold = ret;
bot.sendMessage(chatId, `Failed node connection attempt notification threshold set to: ${config.retries_threshold}`);
}
});
bot.onText(/\/interval (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const new_int = match[1];
if (check_authorization(chatId)) {
config.checking_interval = new_int;
bot.sendMessage(chatId, `Checking interval set to: ${config.checking_interval}s.`);
}
});
bot.onText(/\/stats/, (msg, match) => {
const chatId = msg.chat.id;
if (check_authorization(chatId)) {
send_stats(chatId);
}
});
bot.onText(/\/settings/, (msg, match) => {
const chatId = msg.chat.id;
if (check_authorization(chatId)) {
send_settings(chatId);
}
});
bot.onText(/\/feed_checking_interval (.+)/, (msg, match) => {
const chatId = msg.chat.id;
const new_int = match[1];
if (check_authorization(chatId)) {
config.feed_checking_interval = new_int;
witness_monitor.reset_feed_check();
bot.sendMessage(chatId, `Feed checking interval set to: ${config.feed_checking_interval}m.`);
}
});
bot.onText(/\/feeds (.+)/, (msg, match) => {
const chatId = msg.chat.id;
// Argument format: asset:threshold asset:threshold
const new_feeds = match[1].split(' ').reduce((map, obj) => {
const [ asset_name, threshold ] = obj.split(':')
map[asset_name] = parseInt(threshold)
return map
}, {});
if (check_authorization(chatId)) {
config.feeds_to_check = new_feeds;
witness_monitor.reset_feed_check();
bot.sendMessage(chatId, `Feeds to check set to: ${describe_feeds_to_check(config.feeds_to_check)}.`);
}
});
bot.onText(/\/pause/, (msg, match) => {
const chatId = msg.chat.id;
if (check_authorization(chatId)) {
witness_monitor.pause();
bot.sendMessage(chatId, 'Witness monitoring paused. Use /resume to resume monitoring.');
}
});
bot.onText(/\/switch/, (msg, match) => {
const chatId = msg.chat.id;
if (check_authorization(chatId)) {
bot.sendMessage(chatId, 'Attempting to update signing key...');
witness_monitor.force_update_signing_key();
}
});
bot.onText(/\/resume/, (msg, match) => {
const chatId = msg.chat.id;
if (check_authorization(chatId)) {
witness_monitor.resume();
bot.sendMessage(chatId, 'Witness monitoring resumed.');
}
});
check_config(config);
const witness_monitor = new WitnessMonitor(config, logger);
var last_recap_send = moment();
for (let user_id of config.telegram_authorized_users) {
witness_monitor.on('started', () => {
bot.sendMessage(user_id, 'Bot (re)started.');
send_settings(user_id);
});
witness_monitor.on('notify', (msg) => {
bot.sendMessage(user_id, msg);
});
witness_monitor.on('checked', () => {
if (config.recap_time > 0 && moment().diff(last_recap_send, 'minutes') >= config.recap_time) {
last_recap_send = moment();
send_stats(user_id);
}
});
}
witness_monitor.start_monitoring();