2020-06-07 12:30:32 +02:00
|
|
|
import { Express, Request, Response, NextFunction } from 'express';
|
2019-07-21 16:59:47 +02:00
|
|
|
import * as express from 'express';
|
|
|
|
import * as http from 'http';
|
|
|
|
import * as https from 'https';
|
|
|
|
import * as WebSocket from 'ws';
|
2020-09-21 22:52:54 +02:00
|
|
|
import * as cluster from 'cluster';
|
2020-11-15 08:22:47 +01:00
|
|
|
import axios from 'axios';
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2020-07-22 19:04:29 +02:00
|
|
|
import { checkDbConnection } from './database';
|
2020-10-19 06:57:02 +02:00
|
|
|
import config from './config';
|
2020-02-16 16:15:07 +01:00
|
|
|
import routes from './routes';
|
2019-07-21 16:59:47 +02:00
|
|
|
import blocks from './api/blocks';
|
2020-02-16 16:15:07 +01:00
|
|
|
import memPool from './api/mempool';
|
|
|
|
import diskCache from './api/disk-cache';
|
2019-07-21 16:59:47 +02:00
|
|
|
import statistics from './api/statistics';
|
2020-02-26 11:49:53 +01:00
|
|
|
import websocketHandler from './api/websocket-handler';
|
2019-07-21 16:59:47 +02:00
|
|
|
import fiatConversion from './api/fiat-conversion';
|
2020-09-10 09:46:23 +02:00
|
|
|
import bisq from './api/bisq/bisq';
|
|
|
|
import bisqMarkets from './api/bisq/markets';
|
2020-10-07 15:15:42 +02:00
|
|
|
import donations from './api/donations';
|
2020-10-13 10:27:52 +02:00
|
|
|
import logger from './logger';
|
2020-10-13 12:48:43 +02:00
|
|
|
import backendInfo from './api/backend-info';
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
class Server {
|
2020-09-21 22:52:54 +02:00
|
|
|
private wss: WebSocket.Server | undefined;
|
|
|
|
private server: https.Server | http.Server | undefined;
|
|
|
|
private app: Express;
|
2020-10-18 16:47:47 +02:00
|
|
|
private retryOnElectrsErrorAfterSeconds = 5;
|
2019-07-21 16:59:47 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.app = express();
|
2020-02-16 16:15:07 +01:00
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
if (!config.MEMPOOL.SPAWN_CLUSTER_PROCS) {
|
2020-09-21 22:52:54 +02:00
|
|
|
this.startServer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
2020-10-19 06:57:02 +02:00
|
|
|
logger.notice(`Mempool Server (Master) is running on port ${config.MEMPOOL.HTTP_PORT} (${backendInfo.getShortCommitHash()})`);
|
2020-09-21 22:52:54 +02:00
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
const numCPUs = config.MEMPOOL.SPAWN_CLUSTER_PROCS;
|
2020-09-21 22:52:54 +02:00
|
|
|
for (let i = 0; i < numCPUs; i++) {
|
2020-09-29 19:25:43 +02:00
|
|
|
const env = { workerId: i };
|
|
|
|
const worker = cluster.fork(env);
|
|
|
|
worker.process['env'] = env;
|
2020-09-21 22:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cluster.on('exit', (worker, code, signal) => {
|
2020-09-29 19:25:43 +02:00
|
|
|
const workerId = worker.process['env'].workerId;
|
2020-10-13 11:43:09 +02:00
|
|
|
logger.warn(`Mempool Worker PID #${worker.process.pid} workerId: ${workerId} died. Restarting in 10 seconds... ${signal || code}`);
|
2020-09-29 19:25:43 +02:00
|
|
|
setTimeout(() => {
|
|
|
|
const env = { workerId: workerId };
|
|
|
|
const newWorker = cluster.fork(env);
|
|
|
|
newWorker.process['env'] = env;
|
|
|
|
}, 10000);
|
2020-09-21 22:52:54 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.startServer(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startServer(worker = false) {
|
2019-07-21 16:59:47 +02:00
|
|
|
this.app
|
2020-06-07 12:30:32 +02:00
|
|
|
.use((req: Request, res: Response, next: NextFunction) => {
|
2019-07-21 16:59:47 +02:00
|
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
|
|
next();
|
|
|
|
})
|
2020-10-07 15:15:42 +02:00
|
|
|
.use(express.urlencoded({ extended: true }))
|
|
|
|
.use(express.json());
|
2020-02-16 16:15:07 +01:00
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
this.server = http.createServer(this.app);
|
|
|
|
this.wss = new WebSocket.Server({ server: this.server });
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
if (config.DATABASE.ENABLED) {
|
2020-07-22 19:04:29 +02:00
|
|
|
checkDbConnection();
|
2020-10-19 06:57:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config.STATISTICS.ENABLED && config.DATABASE.ENABLED) {
|
2020-07-22 19:04:29 +02:00
|
|
|
statistics.startStatistics();
|
|
|
|
}
|
|
|
|
|
2020-02-26 11:49:53 +01:00
|
|
|
this.setUpHttpApiRoutes();
|
2019-07-21 16:59:47 +02:00
|
|
|
this.setUpWebsocketHandling();
|
2020-10-23 11:27:02 +02:00
|
|
|
this.runMainUpdateLoop();
|
2019-07-21 16:59:47 +02:00
|
|
|
|
|
|
|
fiatConversion.startService();
|
2020-02-26 11:49:53 +01:00
|
|
|
diskCache.loadMempoolCache();
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
if (config.BISQ_BLOCKS.ENABLED) {
|
2020-07-03 18:45:19 +02:00
|
|
|
bisq.startBisqService();
|
2020-07-14 16:26:02 +02:00
|
|
|
bisq.setPriceCallbackFunction((price) => websocketHandler.setExtraInitProperties('bsq-price', price));
|
2020-09-27 12:21:18 +02:00
|
|
|
blocks.setNewBlockCallback(bisq.handleNewBitcoinBlock.bind(bisq));
|
2020-07-03 18:45:19 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
if (config.BISQ_MARKETS.ENABLED) {
|
2020-09-10 09:46:23 +02:00
|
|
|
bisqMarkets.startBisqService();
|
|
|
|
}
|
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
this.server.listen(config.MEMPOOL.HTTP_PORT, () => {
|
2020-09-21 22:52:54 +02:00
|
|
|
if (worker) {
|
2020-10-13 10:27:52 +02:00
|
|
|
logger.info(`Mempool Server worker #${process.pid} started`);
|
2020-09-21 22:52:54 +02:00
|
|
|
} else {
|
2020-10-19 06:57:02 +02:00
|
|
|
logger.notice(`Mempool Server is running on port ${config.MEMPOOL.HTTP_PORT} (${backendInfo.getShortCommitHash()})`);
|
2020-09-21 22:52:54 +02:00
|
|
|
}
|
2019-07-21 16:59:47 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-23 11:27:02 +02:00
|
|
|
async runMainUpdateLoop() {
|
2020-10-18 16:47:47 +02:00
|
|
|
try {
|
|
|
|
await memPool.$updateMemPoolInfo();
|
|
|
|
await blocks.$updateBlocks();
|
|
|
|
await memPool.$updateMempool();
|
2020-10-23 11:27:02 +02:00
|
|
|
setTimeout(this.runMainUpdateLoop.bind(this), config.ELECTRS.POLL_RATE_MS);
|
2020-10-18 16:47:47 +02:00
|
|
|
this.retryOnElectrsErrorAfterSeconds = 5;
|
|
|
|
} catch (e) {
|
2020-10-23 11:27:02 +02:00
|
|
|
const loggerMsg = `runMainLoop error: ${(e.message || e)}. Retrying in ${this.retryOnElectrsErrorAfterSeconds} sec.`;
|
|
|
|
if (this.retryOnElectrsErrorAfterSeconds > 5) {
|
|
|
|
logger.warn(loggerMsg);
|
|
|
|
} else {
|
|
|
|
logger.debug(loggerMsg);
|
|
|
|
}
|
|
|
|
setTimeout(this.runMainUpdateLoop.bind(this), 1000 * this.retryOnElectrsErrorAfterSeconds);
|
2020-10-18 17:48:15 +02:00
|
|
|
this.retryOnElectrsErrorAfterSeconds *= 2;
|
|
|
|
this.retryOnElectrsErrorAfterSeconds = Math.min(this.retryOnElectrsErrorAfterSeconds, 60);
|
2020-10-18 16:47:47 +02:00
|
|
|
}
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 11:49:53 +01:00
|
|
|
setUpWebsocketHandling() {
|
2020-09-21 22:52:54 +02:00
|
|
|
if (this.wss) {
|
|
|
|
websocketHandler.setWebsocketServer(this.wss);
|
|
|
|
}
|
2020-02-26 11:49:53 +01:00
|
|
|
websocketHandler.setupConnectionHandling();
|
|
|
|
statistics.setNewStatisticsEntryCallback(websocketHandler.handleNewStatistic.bind(websocketHandler));
|
|
|
|
blocks.setNewBlockCallback(websocketHandler.handleNewBlock.bind(websocketHandler));
|
|
|
|
memPool.setMempoolChangedCallback(websocketHandler.handleMempoolChange.bind(websocketHandler));
|
2020-10-07 15:15:42 +02:00
|
|
|
donations.setNotfyDonationStatusCallback(websocketHandler.handleNewDonation.bind(websocketHandler));
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
|
2020-02-26 11:49:53 +01:00
|
|
|
setUpHttpApiRoutes() {
|
2019-07-21 16:59:47 +02:00
|
|
|
this.app
|
2020-10-19 06:57:02 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'transaction-times', routes.getTransactionTimes)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'fees/recommended', routes.getRecommendedFees)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'fees/mempool-blocks', routes.getMempoolBlocks)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'backend-info', routes.getBackendInfo)
|
2020-02-26 11:49:53 +01:00
|
|
|
;
|
2020-07-03 18:45:19 +02:00
|
|
|
|
2020-10-19 13:47:10 +02:00
|
|
|
if (config.STATISTICS.ENABLED && config.DATABASE.ENABLED) {
|
|
|
|
this.app
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/2h', routes.get2HStatistics)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/24h', routes.get24HStatistics.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1w', routes.get1WHStatistics.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1m', routes.get1MStatistics.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/3m', routes.get3MStatistics.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/6m', routes.get6MStatistics.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'statistics/1y', routes.get1YStatistics.bind(routes))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
if (config.BISQ_BLOCKS.ENABLED) {
|
2020-07-03 18:45:19 +02:00
|
|
|
this.app
|
2020-10-19 06:57:02 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/stats', routes.getBisqStats)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/tx/:txId', routes.getBisqTransaction)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/block/:hash', routes.getBisqBlock)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/blocks/tip/height', routes.getBisqTip)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/blocks/:index/:length', routes.getBisqBlocks)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/address/:address', routes.getBisqAddress)
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/txs/:index/:length', routes.getBisqTransactions)
|
2020-07-03 18:45:19 +02:00
|
|
|
;
|
|
|
|
}
|
2020-09-10 09:46:23 +02:00
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
if (config.BISQ_MARKETS.ENABLED) {
|
2020-09-10 09:46:23 +02:00
|
|
|
this.app
|
2020-10-19 06:57:02 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/markets/currencies', routes.getBisqMarketCurrencies.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/markets/depth', routes.getBisqMarketDepth.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/markets/hloc', routes.getBisqMarketHloc.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/markets/markets', routes.getBisqMarketMarkets.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/markets/offers', routes.getBisqMarketOffers.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/markets/ticker', routes.getBisqMarketTicker.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/markets/trades', routes.getBisqMarketTrades.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'bisq/markets/volumes', routes.getBisqMarketVolumes.bind(routes))
|
2020-09-10 09:46:23 +02:00
|
|
|
;
|
|
|
|
}
|
2020-10-07 15:15:42 +02:00
|
|
|
|
2020-10-19 06:57:02 +02:00
|
|
|
if (config.SPONSORS.ENABLED) {
|
2020-10-07 15:15:42 +02:00
|
|
|
this.app
|
2020-10-19 06:57:02 +02:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'donations', routes.getDonations.bind(routes))
|
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'donations/images/:id', routes.getSponsorImage.bind(routes))
|
|
|
|
.post(config.MEMPOOL.API_URL_PREFIX + 'donations', routes.createDonationRequest.bind(routes))
|
|
|
|
.post(config.MEMPOOL.API_URL_PREFIX + 'donations-webhook', routes.donationWebhook.bind(routes))
|
2020-10-07 15:15:42 +02:00
|
|
|
;
|
2020-10-12 05:50:10 +02:00
|
|
|
} else {
|
|
|
|
this.app
|
2020-11-15 08:22:47 +01:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'donations', async (req, res) => {
|
2020-11-15 08:31:34 +01:00
|
|
|
try {
|
|
|
|
const response = await axios.get('https://mempool.space/api/v1/donations', { responseType: 'stream' });
|
|
|
|
response.data.pipe(res);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
2020-10-18 16:37:27 +02:00
|
|
|
})
|
2020-11-15 08:22:47 +01:00
|
|
|
.get(config.MEMPOOL.API_URL_PREFIX + 'donations/images/:id', async (req, res) => {
|
2020-11-15 08:31:34 +01:00
|
|
|
try {
|
|
|
|
const response = await axios.get('https://mempool.space/api/v1/donations/images/' + req.params.id, { responseType: 'stream' });
|
|
|
|
response.data.pipe(res);
|
|
|
|
} catch (e) {
|
|
|
|
res.status(500).end();
|
|
|
|
}
|
2020-10-12 05:50:10 +02:00
|
|
|
});
|
2020-10-07 15:15:42 +02:00
|
|
|
}
|
2020-02-26 11:49:53 +01:00
|
|
|
}
|
2020-02-16 16:15:07 +01:00
|
|
|
}
|
2019-11-06 08:35:02 +01:00
|
|
|
|
2020-02-16 16:15:07 +01:00
|
|
|
const server = new Server();
|