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