-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
monitor-witness.js
154 lines (134 loc) · 4.5 KB
/
monitor-witness.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
'use strict';
const fs = require("fs");
const steem = require('steem');
const AWS = require('aws-sdk')
const nodemailer = require('nodemailer')
const config = JSON.parse(fs.readFileSync("./config.json"));
const execSync = require('child_process').execSync;
const functions = require('./functions');
const log = functions.log;
const runInterval = functions.runInterval;
// The key to broadcast if we want to disable the witness
const disabled_key = "STM1111111111111111111111111111111114T1Anm";
// Use AWS with config
const sns = config.aws ? new AWS.SNS({
region: config.aws.region,
accessKeyId: config.aws.accessKeyId,
secretAccessKey: config.aws.secretAccessKey
}) : null
const transporter = nodemailer.createTransport({
port: config.email.port, // true for 465, false for other ports
host: config.email.host,
auth: {
user: config.email.user,
pass: config.email.pass,
},
secure: true,
});
// Connect to the specified RPC node
const rpc_node = config.rpc_nodes ? config.rpc_nodes[0] : (config.rpc_node ? config.rpc_node : 'https://api.steemit.com');
steem.api.setOptions({ transport: 'https', uri: rpc_node, url: rpc_node });
let missData = [];
let lastSms = null;
startProcess();
runInterval(startProcess, config.interval * 1000, 99999999999999);
function getWitness(id) {
return new Promise((resolve, reject) => {
steem.api.getWitnessByAccount(id, function (err, result) {
if (!err) {
resolve(result);
} else {
reject(err);
}
});
});
}
function mail(subject, body) {
const text = `${body} \n`
const htmlText = text.replace('\n', '<br/>')
transporter.sendMail({
from: config.email.from,
to: config.email.recipient,
subject: subject,
text: text,
html: htmlText
}, function (error) {
if (error) console.log(error)
else {
console.log('sent email to ' + recipient)
}
});
}
function sms(subject) {
if (sns === null) {
console.warn('not set aws config!');
return;
}
sns.publish({
Message: `${config.account} ${subject}`,
PhoneNumber: config.aws.number
}, function (err, result) {
console.log(err, result)
if (err) {
console.log(err)
}
lastSms = new Date().getTime() + (1 * 24 * 60 * 60 * 1000)
console.log('sent sms to ' + config.aws.number)
})
}
function switchTo(signing_key) {
log("Switching to " + signing_key);
const props = {};
steem.broadcast.witnessUpdate(config.key, config.account, config.url, signing_key, props, config.fee, function (err, result) {
mail("Switching Witness", "Your Witness Node has been switched to " + signing_key);
console.log(err, result, stdout);
});
}
function reportMissing(missed) {
log("Report missing: " + missed);
mail("Missing a Block", "Your Witness Node hass missed a block - total missing: " + missed);
if (new Date().getTime() > lastSms)
{
console.log('Sending SMS')
sms("Missing a Block")
}
else console.log('Sms already sent today')
}
function startMissingBlocks() {
return (missData[missData.length - 1] - missData[0]) >= config.threshold;
}
async function startProcess() {
const account = await getWitness(config.account);
const signing_key = account.signing_key;
// already disabled, so no point to switch
if (signing_key === disabled_key) {
throw "disabled already.";
}
const total_missed = account.total_missed;
log(signing_key + " total missed = " + total_missed);
missData.push(total_missed);
// remove outdated entries to avoid memory growing
if (missData.length > (config.period / config.interval)) {
missData.shift();
}
if (missData.length > 2) {
if (missData[missData.length - 1] - missData[missData.length - 2] > 0) {
reportMissing(total_missed);
}
}
if (startMissingBlocks()) {
// remove current signing key
const index = config.signing_keys.indexOf(signing_key);
if (index > - 1) {
config.signing_keys.splice(index, 1);
}
if (config.signing_keys.length === 0) {
// disable it just in case
switchTo(disabled_key, total_missed);
throw `Error, no signing key to use. Thus disable it by switching to ${disabled_key}`;
}
switchTo(config.signing_keys[0]);
// reset data
missData = [];
}
}