From 8c23eae5fe2844b08c4b8d74f64e71748887aa6f Mon Sep 17 00:00:00 2001 From: softsimon Date: Fri, 10 Jul 2020 14:13:07 +0700 Subject: [PATCH] Auto reload bisq dump file. --- backend/mempool-config.sample.json | 1 + backend/src/api/bisq.ts | 42 +++++++++++++++++++++++++----- backend/src/index.ts | 1 + backend/src/routes.ts | 22 +++++++++++----- 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index 11407a026..b4b83669f 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -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" diff --git a/backend/src/api/bisq.ts b/backend/src/api/bisq.ts index 31756bb86..19e911319 100644 --- a/backend/src/api/bisq.ts +++ b/backend/src/api/bisq.ts @@ -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 { - 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 { 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); } diff --git a/backend/src/index.ts b/backend/src/index.ts index eaa0e8b3e..2626be5fc 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -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) ; } diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 8655af188..e2efbb4ba 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -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();