This repository has been archived by the owner on Jul 6, 2023. It is now read-only.
forked from DoctorLai/SteemWitnessAutoSwitch
-
Notifications
You must be signed in to change notification settings - Fork 3
/
monitor-witness.js
88 lines (72 loc) · 2.61 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
'use strict';
const fs = require("fs");
const blurt = require('@blurtfoundation/blurtjs');
const config = JSON.parse(fs.readFileSync("./config.json"));
const functions = require('./functions');
const log = functions.log;
const runInterval = functions.runInterval;
// Connect to the specified RPC node
const rpc_node = config.rpc_nodes ? config.rpc_nodes[0] : (config.rpc_node ? config.rpc_node : 'https://blurtd.privex.io');
blurt.api.setOptions({uri: rpc_node, url: rpc_node, useAppbase:true });
let missData = [];
startProcess();
runInterval(startProcess, config.interval * 1000, 99999999999999);
function getWitness(id) {
return new Promise((resolve, reject) => {
blurt.api.getWitnessByAccount(id, function(err, result) {
if (!err) {
resolve(result);
} else {
reject(err);
}
});
});
}
function switchTo(signing_key) {
log("Switching to " + signing_key);
const props = {};
blurt.broadcast.witnessUpdate(config.key, config.account, config.url, signing_key, props, config.fee, function(err, result) {
// TODO: You can send a email/SMS here
console.log(err, result, stdout);
});
}
function reportMissing(missed) {
log("Report missing: " + missed);
// TODO: You can send a email/SMS here
}
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 === "BLT1111111111111111111111111111111114T1Anm") {
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) {
throw "Error, no signing key to use.";
}
switchTo(config.signing_keys[0]);
// reset data
missData = [];
}
}