diff --git a/backend/src/api/bisq/bisq.ts b/backend/src/api/bisq/bisq.ts index 5c5101890..9770ff7ae 100644 --- a/backend/src/api/bisq/bisq.ts +++ b/backend/src/api/bisq/bisq.ts @@ -3,6 +3,7 @@ import * as fs from 'fs'; import * as request from 'request'; import { BisqBlocks, BisqBlock, BisqTransaction, BisqStats, BisqTrade } from './interfaces'; import { Common } from '../common'; +import { Block } from '../../interfaces'; class Bisq { private static BLOCKS_JSON_FILE_PATH = '/all/blocks.json'; @@ -35,6 +36,14 @@ class Bisq { 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 { return this.transactionIndex[txId]; } diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index 971f00514..a4b20a076 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -8,7 +8,7 @@ class Blocks { private blocks: Block[] = []; private currentBlockHeight = 0; private lastDifficultyAdjustmentTime = 0; - private newBlockCallback: ((block: Block, txIds: string[], transactions: TransactionExtended[]) => void) | undefined; + private newBlockCallbacks: ((block: Block, txIds: string[], transactions: TransactionExtended[]) => void)[] = []; constructor() { } @@ -21,7 +21,7 @@ class Blocks { } public setNewBlockCallback(fn: (block: Block, txIds: string[], transactions: TransactionExtended[]) => void) { - this.newBlockCallback = fn; + this.newBlockCallbacks.push(fn); } public async updateBlocks() { @@ -95,8 +95,8 @@ class Blocks { this.blocks.shift(); } - if (this.newBlockCallback) { - this.newBlockCallback(block, txIds, transactions); + if (this.newBlockCallbacks.length) { + this.newBlockCallbacks.forEach((cb) => cb(block, txIds, transactions)); } } diff --git a/backend/src/index.ts b/backend/src/index.ts index ec7a2d617..36676d944 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -84,6 +84,7 @@ class Server { if (config.BISQ_ENABLED) { bisq.startBisqService(); bisq.setPriceCallbackFunction((price) => websocketHandler.setExtraInitProperties('bsq-price', price)); + blocks.setNewBlockCallback(bisq.handleNewBitcoinBlock.bind(bisq)); } if (config.BISQ_MARKET_ENABLED) {