forked from janoside/btc-rpc-explorer
-
Notifications
You must be signed in to change notification settings - Fork 137
/
app.js
executable file
·651 lines (489 loc) · 18.9 KB
/
app.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#!/usr/bin/env node
'use strict';
var os = require('os');
var path = require('path');
var dotenv = require("dotenv");
var fs = require('fs');
var configPaths = [ path.join(os.homedir(), '.config', 'bch-rpc-explorer.env'), path.join(process.cwd(), '.env') ];
configPaths.filter(fs.existsSync).forEach(path => {
console.log('Loading env file:', path);
dotenv.config({ path });
});
global.cacheStats = {};
// debug module is already loaded by the time we do dotenv.config
// so refresh the status of DEBUG env var
var debug = require("debug");
debug.enable(process.env.DEBUG || "bchexp:app,bchexp:error");
var debugLog = debug("bchexp:app");
var debugLogError = debug("bchexp:error");
var express = require('express');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require("express-session");
var csurf = require("csurf");
var config = require("./app/config.js");
var simpleGit = require('simple-git');
var utils = require("./app/utils.js");
var moment = require("moment");
var Decimal = require('decimal.js');
var bitcoinCore = require("bitcoin-core");
var pug = require("pug");
var momentDurationFormat = require("moment-duration-format");
var coreApi = require("./app/api/coreApi.js");
var coins = require("./app/coins.js");
var request = require("request");
var qrcode = require("qrcode");
var addressApi = require("./app/api/addressApi.js");
var electrumAddressApi = require("./app/api/electrumAddressApi.js");
var coreApi = require("./app/api/coreApi.js");
var auth = require('./app/auth.js');
var marked = require("marked");
var package_json = require('./package.json');
global.appVersion = package_json.version;
var crawlerBotUserAgentStrings = [ "Googlebot", "Bingbot", "Slurp", "DuckDuckBot", "Baiduspider", "YandexBot", "Sogou", "Exabot", "facebot", "ia_archiver" ];
var baseActionsRouter = require('./routes/baseActionsRouter.js');
var apiActionsRouter = require('./routes/apiRouter.js');
var snippetActionsRouter = require('./routes/snippetRouter.js');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
// ref: https://blog.stigok.com/post/disable-pug-debug-output-with-expressjs-web-app
app.engine('pug', (path, options, fn) => {
options.debug = false;
return pug.__express.call(null, path, options, fn);
});
app.set('view engine', 'pug');
// basic http authentication
if (process.env.BTCEXP_BASIC_AUTH_PASSWORD) {
app.disable('x-powered-by');
app.use(auth(process.env.BTCEXP_BASIC_AUTH_PASSWORD));
}
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
//app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: config.cookieSecret,
resave: false,
saveUninitialized: false
}));
app.use(express.static(path.join(__dirname, 'public')));
process.on("unhandledRejection", (reason, p) => {
debugLog("Unhandled Rejection at: Promise", p, "reason:", reason, "stack:", (reason != null ? reason.stack : "null"));
});
function loadMiningPoolConfigs() {
debugLog("Loading mining pools config");
global.miningPoolsConfigs = [];
var miningPoolsConfigDir = path.join(__dirname, "public", "txt", "mining-pools-configs", global.coinConfig.ticker);
fs.readdir(miningPoolsConfigDir, function(err, files) {
if (err) {
utils.logError("3ufhwehe", err, {configDir:miningPoolsConfigDir, desc:"Unable to scan directory"});
return;
}
files.forEach(function(file) {
var filepath = path.join(miningPoolsConfigDir, file);
var contents = fs.readFileSync(filepath, 'utf8');
global.miningPoolsConfigs.push(JSON.parse(contents));
});
for (var i = 0; i < global.miningPoolsConfigs.length; i++) {
for (var x in global.miningPoolsConfigs[i].payout_addresses) {
if (global.miningPoolsConfigs[i].payout_addresses.hasOwnProperty(x)) {
global.specialAddresses[x] = {type:"minerPayout", minerInfo:global.miningPoolsConfigs[i].payout_addresses[x]};
}
}
}
});
}
function getSourcecodeProjectMetadata() {
var options = {
url: "https://api.github.com/repos/sickpig/bch-rpc-explorer",
headers: {
'User-Agent': 'request'
}
};
request(options, function(error, response, body) {
if (error == null && response && response.statusCode && response.statusCode == 200) {
var responseBody = JSON.parse(body);
global.sourcecodeProjectMetadata = responseBody;
} else {
utils.logError("3208fh3ew7eghfg", {error:error, response:response, body:body});
}
});
}
function loadChangelog() {
var filename = "CHANGELOG.md";
fs.readFile(path.join(__dirname, filename), 'utf8', function(err, data) {
if (err) {
utils.logError("2379gsd7sgd334", err);
} else {
global.changelogMarkdown = data;
}
});
}
function loadHistoricalDataForChain(chain) {
debugLog(`Loading historical data for chain=${chain}`);
if (config.donations.addresses && config.donations.addresses[coinConfig.ticker]) {
global.specialAddresses[config.donations.addresses[coinConfig.ticker].address] = {type:"donation"};
}
if (global.coinConfig.historicalData) {
global.coinConfig.historicalData.forEach(function(item) {
if (item.chain == chain) {
if (item.type == "blockheight") {
global.specialBlocks[item.blockHash] = item;
} else if (item.type == "tx") {
global.specialTransactions[item.txid] = item;
} else if (item.type == "address") {
global.specialAddresses[item.address] = {type:"fun", addressInfo:item};
}
}
});
}
}
function verifyRpcConnection() {
if (!global.activeBlockchain) {
debugLog(`Verifying RPC connection...`);
coreApi.getNetworkInfo().then(function(getnetworkinfo) {
coreApi.getBlockchainInfo().then(function(getblockchaininfo) {
global.activeBlockchain = getblockchaininfo.chain;
// we've verified rpc connection, no need to keep trying
clearInterval(global.verifyRpcConnectionIntervalId);
onRpcConnectionVerified(getnetworkinfo, getblockchaininfo);
}).catch(function(err) {
utils.logError("329u0wsdgewg6ed", err);
});
}).catch(function(err) {
utils.logError("32ugegdfsde", err);
});
}
}
function onRpcConnectionVerified(getnetworkinfo, getblockchaininfo) {
// localservicenames introduced in 0.19
var services = getnetworkinfo.localservicesnames ? ("[" + getnetworkinfo.localservicesnames.join(", ") + "]") : getnetworkinfo.localservices;
global.getnetworkinfo = getnetworkinfo;
var bitcoinCoreVersionRegex = /^.*\/BCH Unlimited\:(.*)\/.*$/;
var match = bitcoinCoreVersionRegex.exec(getnetworkinfo.subversion);
if (match) {
global.btcNodeVersion = match[1];
var semver4PartRegex = /^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/;
var semver4PartMatch = semver4PartRegex.exec(global.btcNodeVersion);
if (semver4PartMatch) {
var p0 = semver4PartMatch[1];
var p1 = semver4PartMatch[2];
var p2 = semver4PartMatch[3];
var p3 = semver4PartMatch[4];
// drop last segment, which usually indicates a bug fix release which is (hopefully) irrelevant for RPC API versioning concerns
global.btcNodeSemver = `${p0}.${p1}.${p2}`;
} else {
var semver3PartRegex = /^([0-9]+)\.([0-9]+)\.([0-9]+)$/;
var semver3PartMatch = semver3PartRegex.exec(global.btcNodeVersion);
if (semver3PartMatch) {
var p0 = semver3PartMatch[1];
var p1 = semver3PartMatch[2];
var p2 = semver3PartMatch[3];
global.btcNodeSemver = `${p0}.${p1}.${p2}`;
} else {
// short-circuit: force all RPC calls to pass their version checks - this will likely lead to errors / instability / unexpected results
global.btcNodeSemver = "1000.1000.0"
}
}
} else {
// short-circuit: force all RPC calls to pass their version checks - this will likely lead to errors / instability / unexpected results
global.btcNodeSemver = "1000.1000.0"
debugLogError(`Unable to parse node version string: ${getnetworkinfo.subversion} - RPC versioning will likely be unreliable. Is your node a version of Bitcoin Core?`);
}
debugLog(`RPC Connected: version=${getnetworkinfo.version} subversion=${getnetworkinfo.subversion}, parsedVersion(used for RPC versioning)=${global.btcNodeSemver}, protocolversion=${getnetworkinfo.protocolversion}, chain=${getblockchaininfo.chain}, services=${services}`);
// load historical/fun items for this chain
loadHistoricalDataForChain(global.activeBlockchain);
if (global.activeBlockchain == "main") {
if (global.exchangeRates == null) {
utils.refreshExchangeRates();
}
// refresh exchange rate periodically
setInterval(utils.refreshExchangeRates, 1800000);
// UTXO pull
refreshUtxoSetSummary();
setInterval(refreshUtxoSetSummary, 30 * 60 * 1000);
// 1d / 7d volume
refreshNetworkVolumes();
setInterval(refreshNetworkVolumes, 30 * 60 * 1000);
}
}
function refreshUtxoSetSummary() {
if (config.slowDeviceMode) {
global.utxoSetSummary = null;
global.utxoSetSummaryPending = false;
debugLog("Skipping performance-intensive task: fetch UTXO set summary. This is skipped due to the flag 'slowDeviceMode' which defaults to 'true' to protect slow nodes. Set this flag to 'false' to enjoy UTXO set summary details.");
return;
}
// flag that we're working on calculating UTXO details (to differentiate cases where we don't have the details and we're not going to try computing them)
global.utxoSetSummaryPending = true;
coreApi.getUtxoSetSummary().then(function(result) {
global.utxoSetSummary = result;
result.lastUpdated = Date.now();
debugLog("Refreshed utxo summary: " + JSON.stringify(result));
});
}
function refreshNetworkVolumes() {
if (config.slowDeviceMode) {
debugLog("Skipping performance-intensive task: fetch last 24 hrs of blockstats to calculate transaction volume. This is skipped due to the flag 'slowDeviceMode' which defaults to 'true' to protect slow nodes. Set this flag to 'false' to enjoy UTXO set summary details.");
return;
}
var cutoff1d = new Date().getTime() - (60 * 60 * 24 * 1000);
var cutoff7d = new Date().getTime() - (60 * 60 * 24 * 7 * 1000);
coreApi.getBlockchainInfo().then(function(result) {
var promises = [];
var blocksPerDay = 144 + 20; // 20 block padding
for (var i = 0; i < (blocksPerDay * 1); i++) {
if (result.blocks - i >= 0) {
promises.push(coreApi.getBlockStats(result.blocks - i));
}
}
var startBlock = result.blocks;
var endBlock1d = result.blocks;
var endBlock7d = result.blocks;
var endBlockTime1d = 0;
var endBlockTime7d = 0;
Promise.all(promises).then(function(results) {
var volume1d = new Decimal(0);
var volume7d = new Decimal(0);
var blocks1d = 0;
var blocks7d = 0;
if (results && results.length > 0 && results[0] != null) {
for (var i = 0; i < results.length; i++) {
if (results[i].time * 1000 > cutoff1d) {
volume1d = volume1d.plus(new Decimal(results[i].total_out));
volume1d = volume1d.plus(new Decimal(results[i].subsidy));
volume1d = volume1d.plus(new Decimal(results[i].totalfee));
blocks1d++;
endBlock1d = results[i].height;
endBlockTime1d = results[i].time;
}
if (results[i].time * 1000 > cutoff7d) {
volume7d = volume7d.plus(new Decimal(results[i].total_out));
volume7d = volume7d.plus(new Decimal(results[i].subsidy));
volume7d = volume7d.plus(new Decimal(results[i].totalfee));
blocks7d++;
endBlock7d = results[i].height;
endBlockTime7d = results[i].time;
}
}
debugLog("Volume 1d", volume1d);
debugLog("Volume 7d", volume7d);
global.networkVolume = {d1:{amt:volume1d, blocks:blocks1d, startBlock:startBlock, endBlock:endBlock1d, startTime:results[0].time, endTime:endBlockTime1d}};
debugLog(`Network volume: ${JSON.stringify(global.networkVolume)}`);
} else {
debugLog("Unable to load network volume, likely due to bitcoind version older than BCH Unlimited 1.8.0 (the first version to support getblockstats).");
}
});
});
}
app.onStartup = function() {
global.appStartTime = new Date().getTime();
global.config = config;
global.coinConfig = coins[config.coin];
global.coinConfigs = coins;
global.specialTransactions = {};
global.specialBlocks = {};
global.specialAddresses = {};
loadChangelog();
if (global.sourcecodeVersion == null && fs.existsSync('.git')) {
simpleGit(".").log(["-n 1"], function(err, log) {
if (err) {
utils.logError("3fehge9ee", err, {desc:"Error accessing git repo"});
debugLog(`Starting ${global.coinConfig.ticker} RPC Explorer, v${global.appVersion} (code: unknown commit)`);
} else {
global.sourcecodeVersion = log.all[0].hash.substring(0, 10);
global.sourcecodeDate = log.all[0].date.substring(0, "0000-00-00".length);
debugLog(`Starting ${global.coinConfig.ticker} RPC Explorer, v${global.appVersion} (commit: '${global.sourcecodeVersion}', date: ${global.sourcecodeDate})`);
}
app.continueStartup();
});
} else {
debugLog(`Starting ${global.coinConfig.ticker} RPC Explorer, v${global.appVersion}`);
app.continueStartup();
}
}
app.continueStartup = function() {
var rpcCred = config.credentials.rpc;
debugLog(`Connecting to RPC node at ${rpcCred.host}:${rpcCred.port}`);
var rpcClientProperties = {
host: rpcCred.host,
port: rpcCred.port,
username: rpcCred.username,
password: rpcCred.password,
timeout: rpcCred.timeout
};
global.rpcClient = new bitcoinCore(rpcClientProperties);
var rpcClientNoTimeoutProperties = {
host: rpcCred.host,
port: rpcCred.port,
username: rpcCred.username,
password: rpcCred.password,
timeout: 0
};
global.rpcClientNoTimeout = new bitcoinCore(rpcClientNoTimeoutProperties);
// keep trying to verify rpc connection until we succeed
// note: see verifyRpcConnection() for associated clearInterval() after success
verifyRpcConnection();
global.verifyRpcConnectionIntervalId = setInterval(verifyRpcConnection, 30000);
if (config.donations.addresses) {
var getDonationAddressQrCode = function(coinId) {
qrcode.toDataURL(config.donations.addresses[coinId].address, function(err, url) {
global.donationAddressQrCodeUrls[coinId] = url;
});
};
global.donationAddressQrCodeUrls = {};
config.donations.addresses.coins.forEach(function(item) {
getDonationAddressQrCode(item);
});
}
if (config.addressApi) {
var supportedAddressApis = addressApi.getSupportedAddressApis();
if (!supportedAddressApis.includes(config.addressApi)) {
utils.logError("32907ghsd0ge", `Unrecognized value for BTCEXP_ADDRESS_API: '${config.addressApi}'. Valid options are: ${supportedAddressApis}`);
}
if (config.addressApi == "electrumx") {
if (config.electrumXServers && config.electrumXServers.length > 0) {
electrumAddressApi.connectToServers().then(function() {
global.electrumAddressApi = electrumAddressApi;
}).catch(function(err) {
utils.logError("31207ugf4e0fed", err, {electrumXServers:config.electrumXServers});
});
} else {
utils.logError("327hs0gde", "You must set the 'BTCEXP_ELECTRUMX_SERVERS' environment variable when BTCEXP_ADDRESS_API=electrumx.");
}
}
}
loadMiningPoolConfigs();
getSourcecodeProjectMetadata();
if (config.demoSite) {
setInterval(getSourcecodeProjectMetadata, 3600000);
}
utils.logMemoryUsage();
setInterval(utils.logMemoryUsage, 5000);
};
app.use(function(req, res, next) {
req.startTime = Date.now();
req.startMem = process.memoryUsage().heapUsed;
next();
});
app.use(function(req, res, next) {
// make session available in templates
res.locals.session = req.session;
if (config.credentials.rpc && req.session.host == null) {
req.session.host = config.credentials.rpc.host;
req.session.port = config.credentials.rpc.port;
req.session.username = config.credentials.rpc.username;
}
var userAgent = req.headers['user-agent'];
for (var i = 0; i < crawlerBotUserAgentStrings.length; i++) {
if (userAgent.indexOf(crawlerBotUserAgentStrings[i]) != -1) {
res.locals.crawlerBot = true;
}
}
// make a bunch of globals available to templates
res.locals.config = global.config;
res.locals.coinConfig = global.coinConfig;
res.locals.activeBlockchain = global.activeBlockchain;
res.locals.exchangeRates = global.exchangeRates;
res.locals.utxoSetSummary = global.utxoSetSummary;
res.locals.utxoSetSummaryPending = global.utxoSetSummaryPending;
res.locals.networkVolume = global.networkVolume;
res.locals.host = req.session.host;
res.locals.port = req.session.port;
res.locals.genesisBlockHash = coreApi.getGenesisBlockHash();
res.locals.genesisCoinbaseTransactionId = coreApi.getGenesisCoinbaseTransactionId();
res.locals.pageErrors = [];
// currency format type
if (!req.session.currencyFormatType) {
var cookieValue = req.cookies['user-setting-currencyFormatType'];
if (cookieValue) {
req.session.currencyFormatType = cookieValue;
} else {
req.session.currencyFormatType = "";
}
}
// theme
if (!req.session.uiTheme) {
var cookieValue = req.cookies['user-setting-uiTheme'];
if (cookieValue) {
req.session.uiTheme = cookieValue;
} else {
req.session.uiTheme = "dark";
}
}
// blockPage.showTechSummary
if (!req.session.blockPageShowTechSummary) {
var cookieValue = req.cookies['user-setting-blockPageShowTechSummary'];
if (cookieValue) {
req.session.blockPageShowTechSummary = cookieValue;
} else {
req.session.blockPageShowTechSummary = "true";
}
}
// homepage banner
if (!req.session.hideHomepageBanner) {
var cookieValue = req.cookies['user-setting-hideHomepageBanner'];
if (cookieValue) {
req.session.hideHomepageBanner = cookieValue;
} else {
req.session.hideHomepageBanner = "false";
}
}
res.locals.currencyFormatType = req.session.currencyFormatType;
global.currencyFormatType = req.session.currencyFormatType;
if (!["/", "/connect"].includes(req.originalUrl)) {
if (utils.redirectToConnectPageIfNeeded(req, res)) {
return;
}
}
if (req.session.userMessage) {
res.locals.userMessage = req.session.userMessage;
if (req.session.userMessageType) {
res.locals.userMessageType = req.session.userMessageType;
} else {
res.locals.userMessageType = "warning";
}
req.session.userMessage = null;
req.session.userMessageType = null;
}
if (req.session.query) {
res.locals.query = req.session.query;
req.session.query = null;
}
// make some var available to all request
// ex: req.cheeseStr = "cheese";
next();
});
app.use(csurf(), (req, res, next) => {
res.locals.csrfToken = req.csrfToken();
next();
});
app.use('/', baseActionsRouter);
app.use('/api/', apiActionsRouter);
app.use('/snippet/', snippetActionsRouter);
/// catch 404 and forwarding to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: (String(app.get('env')) === 'development') ? err:{}
});
});
app.locals.moment = moment;
app.locals.Decimal = Decimal;
app.locals.utils = utils;
app.locals.marked = marked;
module.exports = app;