Fix for Bisq blocks data watchers stopps working randomly. Restaring watcher when block height has diverged.

This commit is contained in:
softsimon 2020-09-27 17:21:18 +07:00
parent f4a78a0e78
commit 3450de774f
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
3 changed files with 14 additions and 4 deletions

View File

@ -3,6 +3,7 @@ import * as fs from 'fs';
import * as request from 'request'; import * as request from 'request';
import { BisqBlocks, BisqBlock, BisqTransaction, BisqStats, BisqTrade } from './interfaces'; import { BisqBlocks, BisqBlock, BisqTransaction, BisqStats, BisqTrade } from './interfaces';
import { Common } from '../common'; import { Common } from '../common';
import { Block } from '../../interfaces';
class Bisq { class Bisq {
private static BLOCKS_JSON_FILE_PATH = '/all/blocks.json'; private static BLOCKS_JSON_FILE_PATH = '/all/blocks.json';
@ -35,6 +36,14 @@ class Bisq {
this.startSubDirectoryWatcher(); this.startSubDirectoryWatcher();
} }
handleNewBitcoinBlock(block: Block): void {
if (block.height - 2 > this.latestBlockHeight && this.latestBlockHeight !== 0) {
console.log(`Bitcoin block height (#${block.height}) has diverged from the latest Bisq block height (#${this.latestBlockHeight}). Restarting watchers...`);
this.startTopDirectoryWatcher();
this.startSubDirectoryWatcher();
}
}
getTransaction(txId: string): BisqTransaction | undefined { getTransaction(txId: string): BisqTransaction | undefined {
return this.transactionIndex[txId]; return this.transactionIndex[txId];
} }

View File

@ -8,7 +8,7 @@ class Blocks {
private blocks: Block[] = []; private blocks: Block[] = [];
private currentBlockHeight = 0; private currentBlockHeight = 0;
private lastDifficultyAdjustmentTime = 0; private lastDifficultyAdjustmentTime = 0;
private newBlockCallback: ((block: Block, txIds: string[], transactions: TransactionExtended[]) => void) | undefined; private newBlockCallbacks: ((block: Block, txIds: string[], transactions: TransactionExtended[]) => void)[] = [];
constructor() { } constructor() { }
@ -21,7 +21,7 @@ class Blocks {
} }
public setNewBlockCallback(fn: (block: Block, txIds: string[], transactions: TransactionExtended[]) => void) { public setNewBlockCallback(fn: (block: Block, txIds: string[], transactions: TransactionExtended[]) => void) {
this.newBlockCallback = fn; this.newBlockCallbacks.push(fn);
} }
public async updateBlocks() { public async updateBlocks() {
@ -95,8 +95,8 @@ class Blocks {
this.blocks.shift(); this.blocks.shift();
} }
if (this.newBlockCallback) { if (this.newBlockCallbacks.length) {
this.newBlockCallback(block, txIds, transactions); this.newBlockCallbacks.forEach((cb) => cb(block, txIds, transactions));
} }
} }

View File

@ -84,6 +84,7 @@ class Server {
if (config.BISQ_ENABLED) { if (config.BISQ_ENABLED) {
bisq.startBisqService(); bisq.startBisqService();
bisq.setPriceCallbackFunction((price) => websocketHandler.setExtraInitProperties('bsq-price', price)); bisq.setPriceCallbackFunction((price) => websocketHandler.setExtraInitProperties('bsq-price', price));
blocks.setNewBlockCallback(bisq.handleNewBitcoinBlock.bind(bisq));
} }
if (config.BISQ_MARKET_ENABLED) { if (config.BISQ_MARKET_ENABLED) {