mirror of
https://github.com/mempool/mempool.git
synced 2025-01-18 05:12:35 +01:00
Auto reload bisq dump file.
This commit is contained in:
parent
4ff9663812
commit
8c23eae5fe
@ -14,6 +14,7 @@
|
||||
"TX_PER_SECOND_SPAN_SECONDS": 150,
|
||||
"ELECTRS_API_URL": "https://www.blockstream.info/testnet/api",
|
||||
"BISQ_ENABLED": false,
|
||||
"BSQ_BLOCKS_DATA_PATH": "/mempool/data/all/blocks.json",
|
||||
"SSL": false,
|
||||
"SSL_CERT_FILE_PATH": "/etc/letsencrypt/live/mysite/fullchain.pem",
|
||||
"SSL_KEY_FILE_PATH": "/etc/letsencrypt/live/mysite/privkey.pem"
|
||||
|
@ -1,8 +1,8 @@
|
||||
const config = require('../../mempool-config.json');
|
||||
import * as fs from 'fs';
|
||||
import { BisqBlocks, BisqBlock, BisqTransaction } from '../interfaces';
|
||||
|
||||
class Bisq {
|
||||
static FILE_NAME = './blocks.json';
|
||||
private latestBlockHeight = 0;
|
||||
private blocks: BisqBlock[] = [];
|
||||
private transactions: BisqTransaction[] = [];
|
||||
@ -13,11 +13,29 @@ class Bisq {
|
||||
|
||||
startBisqService(): void {
|
||||
this.loadBisqDumpFile();
|
||||
|
||||
let fsWait: NodeJS.Timeout | null = null;
|
||||
fs.watch(config.BSQ_BLOCKS_DATA_PATH, (event, filename) => {
|
||||
if (filename) {
|
||||
if (fsWait) {
|
||||
clearTimeout(fsWait);
|
||||
}
|
||||
fsWait = setTimeout(() => {
|
||||
console.log(`${filename} file changed. Reloading dump file.`);
|
||||
this.loadBisqDumpFile();
|
||||
}, 1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async loadBisqDumpFile(): Promise<void> {
|
||||
await this.loadBisqBlocksDump();
|
||||
this.buildIndex();
|
||||
try {
|
||||
const data = await this.loadData();
|
||||
await this.loadBisqBlocksDump(data);
|
||||
this.buildIndex();
|
||||
} catch (e) {
|
||||
console.log('loadBisqDumpFile() error.', e.message);
|
||||
}
|
||||
}
|
||||
|
||||
getTransaction(txId: string): BisqTransaction | undefined {
|
||||
@ -28,11 +46,22 @@ class Bisq {
|
||||
return [this.transactions.slice(start, length + start), this.transactions.length];
|
||||
}
|
||||
|
||||
getBlockTransactions(blockHash: string, start: number, length: number): [BisqTransaction[], number] {
|
||||
const block = this.blocksIndex[blockHash];
|
||||
if (!block) {
|
||||
return [[], -1];
|
||||
}
|
||||
return [block.txs.slice(start, length + start), block.txs.length];
|
||||
}
|
||||
|
||||
getBlock(hash: string): BisqBlock | undefined {
|
||||
return this.blocksIndex[hash];
|
||||
}
|
||||
|
||||
private buildIndex() {
|
||||
this.transactions = [];
|
||||
this.transactionsIndex = {};
|
||||
this.blocksIndex = {};
|
||||
this.blocks.forEach((block) => {
|
||||
this.blocksIndex[block.hash] = block;
|
||||
block.txs.forEach((tx) => {
|
||||
@ -45,10 +74,9 @@ class Bisq {
|
||||
console.log('Bisq data index rebuilt');
|
||||
}
|
||||
|
||||
private async loadBisqBlocksDump() {
|
||||
private async loadBisqBlocksDump(cacheData: string) {
|
||||
const start = new Date().getTime();
|
||||
const cacheData = await this.loadData();
|
||||
if (cacheData) {
|
||||
if (cacheData && cacheData.length !== 0) {
|
||||
console.log('Parsing Bisq data from dump file');
|
||||
const data: BisqBlocks = JSON.parse(cacheData);
|
||||
if (data.blocks) {
|
||||
@ -65,7 +93,7 @@ class Bisq {
|
||||
|
||||
private loadData(): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(Bisq.FILE_NAME, 'utf8', (err, data) => {
|
||||
fs.readFile(config.BSQ_BLOCKS_DATA_PATH, 'utf8', (err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
}
|
||||
|
@ -94,6 +94,7 @@ class Server {
|
||||
this.app
|
||||
.get(config.API_ENDPOINT + 'bisq/tx/:txId', routes.getBisqTransaction)
|
||||
.get(config.API_ENDPOINT + 'bisq/block/:hash', routes.getBisqBlock)
|
||||
.get(config.API_ENDPOINT + 'bisq/block/:hash/txs/:index/:length', routes.getBisqBlockTransactions)
|
||||
.get(config.API_ENDPOINT + 'bisq/txs/:index/:length', routes.getBisqTransactions)
|
||||
;
|
||||
}
|
||||
|
@ -100,12 +100,8 @@ class Routes {
|
||||
const index = parseInt(req.params.index, 10) || 0;
|
||||
const length = parseInt(req.params.length, 10) > 100 ? 100 : parseInt(req.params.length, 10) || 25;
|
||||
const [transactions, count] = bisq.getTransactions(index, length);
|
||||
if (transactions) {
|
||||
res.header('X-Total-Count', count.toString());
|
||||
res.send(transactions);
|
||||
} else {
|
||||
res.status(404).send('Bisq transaction not found');
|
||||
}
|
||||
res.header('X-Total-Count', count.toString());
|
||||
res.send(transactions);
|
||||
}
|
||||
|
||||
public getBisqBlock(req: Request, res: Response) {
|
||||
@ -116,6 +112,20 @@ class Routes {
|
||||
res.status(404).send('Bisq block not found');
|
||||
}
|
||||
}
|
||||
|
||||
public getBisqBlockTransactions(req: Request, res: Response) {
|
||||
const blockHash = req.params.hash || '';
|
||||
const index = parseInt(req.params.index, 10) || 0;
|
||||
const length = parseInt(req.params.length, 10) > 100 ? 100 : parseInt(req.params.length, 10) || 25;
|
||||
const [transactions, count] = bisq.getBlockTransactions(blockHash, index, length);
|
||||
if (count === -1) {
|
||||
res.header('X-Total-Count', '0');
|
||||
res.send([]);
|
||||
return;
|
||||
}
|
||||
res.header('X-Total-Count', count.toString());
|
||||
res.send(transactions);
|
||||
}
|
||||
}
|
||||
|
||||
export default new Routes();
|
||||
|
Loading…
Reference in New Issue
Block a user