2020-07-10 14:13:07 +07:00
|
|
|
const config = require('../../mempool-config.json');
|
2020-07-03 23:45:19 +07:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import { BisqBlocks, BisqBlock, BisqTransaction } from '../interfaces';
|
|
|
|
|
|
|
|
class Bisq {
|
|
|
|
private latestBlockHeight = 0;
|
|
|
|
private blocks: BisqBlock[] = [];
|
|
|
|
private transactions: BisqTransaction[] = [];
|
|
|
|
private transactionsIndex: { [txId: string]: BisqTransaction } = {};
|
|
|
|
private blocksIndex: { [hash: string]: BisqBlock } = {};
|
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
startBisqService(): void {
|
|
|
|
this.loadBisqDumpFile();
|
2020-07-10 14:13:07 +07:00
|
|
|
|
|
|
|
let fsWait: NodeJS.Timeout | null = null;
|
|
|
|
fs.watch(config.BSQ_BLOCKS_DATA_PATH, (event, filename) => {
|
|
|
|
if (filename) {
|
|
|
|
if (fsWait) {
|
|
|
|
clearTimeout(fsWait);
|
|
|
|
}
|
|
|
|
fsWait = setTimeout(() => {
|
2020-07-13 15:16:12 +07:00
|
|
|
console.log(`${filename} file change detected.`);
|
2020-07-10 14:13:07 +07:00
|
|
|
this.loadBisqDumpFile();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
});
|
2020-07-03 23:45:19 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
getTransaction(txId: string): BisqTransaction | undefined {
|
|
|
|
return this.transactionsIndex[txId];
|
|
|
|
}
|
|
|
|
|
|
|
|
getTransactions(start: number, length: number): [BisqTransaction[], number] {
|
|
|
|
return [this.transactions.slice(start, length + start), this.transactions.length];
|
|
|
|
}
|
|
|
|
|
|
|
|
getBlock(hash: string): BisqBlock | undefined {
|
2020-07-13 15:16:12 +07:00
|
|
|
console.log(hash);
|
|
|
|
console.log(this.blocksIndex[hash]);
|
2020-07-03 23:45:19 +07:00
|
|
|
return this.blocksIndex[hash];
|
|
|
|
}
|
|
|
|
|
2020-07-13 15:16:12 +07:00
|
|
|
getBlocks(start: number, length: number): [BisqBlock[], number] {
|
|
|
|
return [this.blocks.slice(start, length + start), this.blocks.length];
|
|
|
|
}
|
|
|
|
|
|
|
|
private async loadBisqDumpFile(): Promise<void> {
|
|
|
|
try {
|
|
|
|
const data = await this.loadData();
|
|
|
|
await this.loadBisqBlocksDump(data);
|
|
|
|
this.buildIndex();
|
|
|
|
} catch (e) {
|
|
|
|
console.log('loadBisqDumpFile() error.', e.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 23:45:19 +07:00
|
|
|
private buildIndex() {
|
2020-07-13 15:16:12 +07:00
|
|
|
const start = new Date().getTime();
|
|
|
|
this.transactions = [];
|
|
|
|
this.transactionsIndex = {};
|
2020-07-03 23:45:19 +07:00
|
|
|
this.blocks.forEach((block) => {
|
2020-07-13 15:16:12 +07:00
|
|
|
if (!this.blocksIndex[block.hash]) {
|
|
|
|
this.blocksIndex[block.hash] = block;
|
2020-07-11 00:17:13 +07:00
|
|
|
}
|
2020-07-03 23:45:19 +07:00
|
|
|
block.txs.forEach((tx) => {
|
2020-07-13 15:16:12 +07:00
|
|
|
this.transactions.push(tx);
|
2020-07-03 23:45:19 +07:00
|
|
|
this.transactionsIndex[tx.id] = tx;
|
|
|
|
});
|
|
|
|
});
|
2020-07-13 15:16:12 +07:00
|
|
|
const time = new Date().getTime() - start;
|
|
|
|
console.log('Bisq data index rebuilt in ' + time + ' ms');
|
2020-07-03 23:45:19 +07:00
|
|
|
}
|
|
|
|
|
2020-07-11 00:17:13 +07:00
|
|
|
private async loadBisqBlocksDump(cacheData: string): Promise<void> {
|
2020-07-03 23:45:19 +07:00
|
|
|
const start = new Date().getTime();
|
2020-07-10 14:13:07 +07:00
|
|
|
if (cacheData && cacheData.length !== 0) {
|
2020-07-13 15:16:12 +07:00
|
|
|
console.log('Loading Bisq data from dump...');
|
2020-07-03 23:45:19 +07:00
|
|
|
const data: BisqBlocks = JSON.parse(cacheData);
|
2020-07-11 00:17:13 +07:00
|
|
|
if (data.blocks && data.blocks.length !== this.blocks.length) {
|
2020-07-03 23:45:19 +07:00
|
|
|
this.blocks = data.blocks;
|
2020-07-13 15:16:12 +07:00
|
|
|
this.blocks.reverse();
|
2020-07-03 23:45:19 +07:00
|
|
|
this.latestBlockHeight = data.chainHeight;
|
2020-07-13 15:16:12 +07:00
|
|
|
const time = new Date().getTime() - start;
|
|
|
|
console.log('Bisq dump loaded in ' + time + ' ms');
|
2020-07-03 23:45:19 +07:00
|
|
|
} else {
|
|
|
|
throw new Error(`Bisq dump didn't contain any blocks`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private loadData(): Promise<string> {
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-07-10 14:13:07 +07:00
|
|
|
fs.readFile(config.BSQ_BLOCKS_DATA_PATH, 'utf8', (err, data) => {
|
2020-07-03 23:45:19 +07:00
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new Bisq();
|