-
Notifications
You must be signed in to change notification settings - Fork 33
/
status.ts
289 lines (265 loc) · 12.1 KB
/
status.ts
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
import { Db } from "./db";
import { RpcClient } from "./rpc";
import { ChainSyncCheckpoint, Info } from "./info";
import * as fs from 'fs';
import { Config } from "./config";
import * as https from 'https';
import { CacheSet } from "./cache";
var pjson = require('./package.json');
var os = require('os-utils');
enum context {
"SLPDB" = "SLPDB"
}
export class SlpdbStatus {
static db: Db;
static startCmd: string;
static version = pjson.version;
static versionHash: string|null = null;
static deplVersionHash: string|null = null;
static context: context = context.SLPDB;
static lastIncomingTxnZmq: { utc: string, unix: number}|null = null;
static lastIncomingBlockZmq: { utc: string, unix: number}|null = null;
static lastOutgoingTxnZmq: { utc: string, unix: number}|null = null;
static lastOutgoingBlockZmq: { utc: string, unix: number}|null = null;
static slpProcessedBlockHeight: number|null = null;
static state: SlpdbState;
static stateHistory = new CacheSet<{ utc: string, state: SlpdbState }>(10);
static network: string = '';
static pastStackTraces: any[] = [];
static doubleSpendHistory: any[] = [];
static reorgHistory: any[] = [];
static rpc: RpcClient;
static getSlpMempoolSize = function() { return -1; }
static getSlpTokensCount = function() { return -1; }
static getSyncdCheckpoint: () => Promise<ChainSyncCheckpoint> = async function() { return { hash: '', height: -1 }; }
constructor(db: Db, startCmd: string[]) {
SlpdbStatus.db = db;
SlpdbStatus.setState(SlpdbState.PRE_STARTUP);
SlpdbStatus.versionHash = SlpdbStatus.getVersion();
SlpdbStatus.deplVersionHash = SlpdbStatus.getDeplVersion();
let last = (a: string[]) => { let i = a.length-1; return a[i]; }
SlpdbStatus.startCmd = "".concat(...startCmd.map(s => last(s.split('/')).concat(' '))).trimEnd();
}
static setState(state: SlpdbState) {
SlpdbStatus.state = state;
SlpdbStatus.stateHistory.push({ utc: (new Date()).toUTCString(), state });
}
static updateTimeIncomingTxnZmq() {
let date = new Date();
SlpdbStatus.lastIncomingTxnZmq = { utc: date.toUTCString(), unix: Math.floor(date.getTime()/1000) }
}
static updateTimeIncomingBlockZmq() {
let date = new Date();
SlpdbStatus.lastIncomingBlockZmq = { utc: date.toUTCString(), unix: Math.floor(date.getTime()/1000) }
}
static updateTimeOutgoingBlockZmq() {
let date = new Date();
SlpdbStatus.lastOutgoingBlockZmq = { utc: date.toUTCString(), unix: Math.floor(date.getTime()/1000) }
}
static updateTimeOutgoingTxnZmq() {
let date = new Date();
SlpdbStatus.lastOutgoingTxnZmq = { utc: date.toUTCString(), unix: Math.floor(date.getTime()/1000) }
}
static async updateSlpProcessedBlockHeight(height: number) {
SlpdbStatus.slpProcessedBlockHeight = height;
await SlpdbStatus.saveStatus();
}
static async changeStateToStartupBlockSync({ network, getSyncdCheckpoint, getSlpTokensCount }: { network: string, getSyncdCheckpoint: () => Promise<ChainSyncCheckpoint>, getSlpTokensCount: () => number }) {
SlpdbStatus.network = network;
SlpdbStatus.getSyncdCheckpoint = getSyncdCheckpoint;
SlpdbStatus.getSlpTokensCount = getSlpTokensCount;
SlpdbStatus.setState(SlpdbState.STARTUP_BLOCK_SYNC);
await SlpdbStatus.saveStatus();
}
static async changeStateToRunning({ getSlpMempoolSize }: { getSlpMempoolSize: () => number }) {
SlpdbStatus.setState(SlpdbState.RUNNING);
SlpdbStatus.getSlpMempoolSize = getSlpMempoolSize;
await SlpdbStatus.saveStatus();
}
static async changeStateToExitOnError(trace: string) {
SlpdbStatus.setState(SlpdbState.EXITED_ON_ERROR);
SlpdbStatus.pastStackTraces.unshift(trace);
if(SlpdbStatus.pastStackTraces.length > 5)
SlpdbStatus.pastStackTraces.pop();
await SlpdbStatus.saveStatus();
}
static async saveStatus() {
let dbo = await SlpdbStatus.toDbo();
await SlpdbStatus.db.statusUpdate(dbo);
}
static async logExitReason(errorMsg: string) {
if (errorMsg === "SIGINT") {
SlpdbStatus.setState(SlpdbState.EXITED_SIGINT);
await SlpdbStatus.saveStatus();
} else if (errorMsg === "SIGTERM") {
SlpdbStatus.setState(SlpdbState.EXITED_SIGTERM);
await SlpdbStatus.saveStatus();
} else if (errorMsg === "SIGQUIT") {
SlpdbStatus.setState(SlpdbState.EXITED_SIGQUIT);
await SlpdbStatus.saveStatus();
} else {
await SlpdbStatus.changeStateToExitOnError(errorMsg);
}
}
private static async toDbo() {
let checkpoint = await SlpdbStatus.getSyncdCheckpoint();
let mempoolInfo = null;
try {
mempoolInfo = await RpcClient.getMempoolInfo();
} catch (_) { }
let stackTraces = SlpdbStatus.pastStackTraces.map(t => {
if(typeof t === 'string')
return t;
else {
try {
return t.toString();
} catch(_) { }
try {
return JSON.stringify(t);
} catch(_) {
return "Unknown stack trace.";
}
}
})
let date = new Date();
let status = {
version: this.version,
versionHash: this.versionHash,
deplVersionHash: this.deplVersionHash,
startCmd: this.startCmd,
context: this.context,
lastStatusUpdate: { utc: date.toUTCString(), unix: Math.floor(date.getTime()/1000) },
lastIncomingTxnZmq: this.lastIncomingTxnZmq,
lastIncomingBlockZmq: this.lastIncomingBlockZmq,
lastOutgoingTxnZmq: this.lastOutgoingTxnZmq,
lastOutgoingBlockZmq: this.lastOutgoingBlockZmq,
state: this.state,
stateHistory: Array.from(this.stateHistory.toSet()),
network: this.network,
bchBlockHeight: checkpoint.height,
bchBlockHash: checkpoint.hash,
slpProcessedBlockHeight: this.slpProcessedBlockHeight,
mempoolInfoBch: mempoolInfo,
mempoolSizeSlp: this.getSlpMempoolSize(),
tokensCount: this.getSlpTokensCount(),
pastStackTraces: stackTraces,
doubleSpends: this.doubleSpendHistory,
reorgs: this.reorgHistory,
mongoDbStats: await this.db.db.stats({ scale: 1048576 }),
publicUrl: await Info.getTelemetryName(),
telemetryHash: await Info.getTelemetrySecretHash(),
system: { loadAvg1: os.loadavg(1), loadAvg5: os.loadavg(5), loadAvg15: os.loadavg(15), platform: os.platform(), cpuCount: os.cpuCount(), freeMem: os.freemem(), totalMem: os.totalmem(), uptime: os.sysUptime(), processUptime: os.processUptime() }
};
await this.updateTelemetry(status);
return status;
}
private static async updateTelemetry(status: StatusDbo) {
if (Config.telemetry.enable) {
try {
let data = JSON.stringify({ status: status });
let options = {
hostname: Config.telemetry.host,
port: Config.telemetry.port,
path: '/status',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
'Authorization': await Info.getTelemetrySecret()
}
};
let req = https.request(options, res => {
console.log(`[INFO] Telementry response code: ${res.statusCode}`);
res.on('data', d => {
console.log(`[INFO] Telemetry response from ${Config.telemetry.host}: ${d.toString('utf8')}`);
try { JSON.parse(d).secretKey ? Info.setTelemetrySecret(JSON.parse(d).secretKey) : null; } catch (_) {}
});
});
req.on('error', error => {
let reason = error.message;
if (Config.telemetry.host === '') {
reason = "Env var 'telemetry_host' is not set";
}
console.log("[ERROR] Telemetry update failed. Reason:", reason);
});
console.log(`[INFO] Sending telemetry update to ${Config.telemetry.host} for ${await Info.getTelemetryName()}...`);
req.write(data);
req.end();
} catch (err) {
console.log(`[ERROR] Could not updateTelemetry: ${err}`);
}
}
}
static async loadPreviousAttributes() {
let dbo = await SlpdbStatus.db.statusFetch("SLPDB");
try {
SlpdbStatus.pastStackTraces = dbo.pastStackTraces;
let history = new CacheSet<{ utc: string, state: SlpdbState }>(10);
dbo.stateHistory.forEach((state: { utc: string, state: SlpdbState }) => { history.push(state); });
Array.from(SlpdbStatus.stateHistory.toSet()).forEach((state: { utc: string, state: SlpdbState }) => { history.push(state); });
SlpdbStatus.stateHistory = history;
} catch(_) {}
}
static getVersion() {
try {
const rev = fs.readFileSync('.git/HEAD').toString();
if (rev.indexOf(':') === -1) {
return rev.trim();
} else {
return fs.readFileSync('.git/' + rev.trim().substring(5)).toString().trim();
}
} catch (_) {
return null;
}
}
static getDeplVersion() {
try {
const rev = fs.readFileSync('._git/HEAD').toString();
if (rev.indexOf(':') === -1) {
return rev.trim();
} else {
return fs.readFileSync('._git/' + rev.trim().substring(5)).toString().trim();
}
} catch (_) {
return null;
}
}
}
export enum SlpdbState {
"PRE_STARTUP" = "PRE_STARTUP", // phase 1) checking connections with mongodb and bitcoin rpc
"STARTUP_BLOCK_SYNC" = "STARTUP_BLOCK_SYNC", // phase 2) indexing blockchain data into confirmed collection (allows crawling tokens dag quickly)
"STARTUP_TOKEN_PROCESSING" = "STARTUP_TOKEN_PROCESSING", // phase 3) load/update token graphs, hold a cache (allows fastest SLP validation)
"RUNNING" = "RUNNING", // phase 4) startup completed, running normally
"EXITED_ON_ERROR" = "EXITED_ON_ERROR", // process exited due to an error during normal operation
"EXITED_SIGINT" = "EXITED_SIGINT", // process exited normally, clean shutdown or finished running a command
"EXITED_SIGTERM" = "EXITED_SIGTERM", // process exited normally, clean shutdown or finished running a command
"EXITED_SIGQUIT" = "EXITED_SIGQUIT" // process exited normally, clean shutdown or finished running a command
}
interface StatusDbo {
version: string;
versionHash: string | null;
deplVersionHash: string | null;
startCmd: string;
context: context;
lastStatusUpdate: { utc: string; unix: number; };
lastIncomingTxnZmq: { utc: string; unix: number; } | null;
lastIncomingBlockZmq: { utc: string; unix: number; } | null;
lastOutgoingTxnZmq: { utc: string; unix: number; } | null;
lastOutgoingBlockZmq: { utc: string; unix: number; } | null;
state: SlpdbState;
stateHistory: { utc: string, state: SlpdbState }[];
network: string;
bchBlockHeight: number;
bchBlockHash: string | null;
slpProcessedBlockHeight: number | null;
mempoolInfoBch: {} | null;
mempoolSizeSlp: number;
tokensCount: number;
pastStackTraces: string[];
doubleSpends: any[];
reorgs: any[];
mongoDbStats: any;
publicUrl: string;
telemetryHash: string|null;
system: any;
}